Posts Tagged ‘cpu’

How to set CPU affinity for a process in FreeBSD

September 2nd, 2010 No comments

beastieProcessor affinity means, on a multi-CPU machine, the process(es)run only on dedicated set of CPUs. In other words processes are bound to isolated (subset) of the CPUs. This feature can be usedduring performance benchmarking, and also while deploying an application.

To get the CPU model and number of active CPUs try the following command:

$ sysctl hw.model hw.ncpu

The cpuset command

The cpuset command can be used to assign processor sets to processes, run commands constrained to a given set or list of processors, and query information about processor binding, sets, and available processors in the system.

cpuset requires a target to modify or query. The target may be specified as a command, process id, thread id, a cpuset id, an irq or a jail id. Using -g the target’s set id or mask may be queried. Using -l or -s the target’s CPU mask or set id may be set. If no target is specified, cpuset operates on itself. Not all combinations of operations and targets are supported. For example, you may not set the id of an existing set or query and launch a command at the same time.

There are two sets applicable to each process and one private mask per thread. Every process in the system belongs to a cpuset. By default processes are started in set 1. The mask or id may be queried using -c. Each thread also has a private mask of CPUs it is allowed to run on that must be a subset of the assigned set. And finally, there is a root set, numbered 0, that is immutable. This last set is the list of all possible CPUs in the system and is queried using -r.

When running a command it may join a set specified with -s otherwise a new set is created. In addition, a mask for the command may be specified using -l. When used in conjunction with -c the mask modifies the sup- plied or created set rather than the private mask for the thread.

The options are as follows:

-c           The requested operation should reference the cpuset avail-
             able via the target specifier.

-g           Causes cpuset to print either a list of valid CPUs or, using
             -i, the id of the target.

-i           When used with the -g option print the id rather than the
             valid mask of the target.

-j jailid    Specifies a jail id as the target of the operation.

-l cpu-list  Specifies a list of CPUs to apply to a target.  Specifica-
             tion may include numbers seperated by '-' for ranges and
             commas separating individual numbers.

-p pid       Specifies a pid as the target of the operation.

-s setid     Specifies a set id as the target of the operation.

-r           The requested operation should reference the root set avail-
             able via the target specifier.

-t tid       Specifies a thread id as the target of the operation.

-x irq       Specifies an irq as the target of the operation.


Create a new group with CPUs 0-4 inclusive and run /bin/sh on it:

cpuset -c -l 0-4 /bin/sh

Query the mask of CPUs the is allowed to run on:

cpuset -g -p

Restrict /bin/sh to run on CPUs 0 and 2 while its group is still allowed
to run on CPUs 0-4:

cpuset -l 0,2 -p

Modify the cpuset /bin/sh belongs to restricting it to CPUs 0 and 2:

cpuset -l 0,2 -c -p

Modify the cpuset all threads are in by default to contain only the first
4 CPUs, leaving the rest idle:

cpuset -l 0-3 -s 1

Print the id of the cpuset /bin/sh is in:

cpuset -g -i -p

Move the pid into the specified cpuset setid so it may be managed with
other pids in that set:

cpuset -s  -p


Categories: FREEBSD, HOW-TOS Tags: , ,

FreeBSD Get CPU & Memory Information

July 21st, 2010 No comments

beastieTo get information about CPU and Memory under FreeBSD use the following commands:

Getting CPU information:
From dmesg:

$ dmesg | grep CPU


$ grep CPU /var/run/dmesg.boot | less

Using sysctl:
CPU model:

$ sysctl hw.model

CPU clock rate:

$ sysctl hw.clockrate

No of cpus:

$ sysctl hw.ncpu

Get all information:

$ sysctl -a | grep -i cpu | less

Getting memory information:
From dmesg:

$ dmesg | grep memory


$ grep memory /var/run/dmesg.boot

Using sysctl:

$ sysctl -a | grep mem | less

FreeBSD find out memory usage

There is a Linux like free command for FreeBSD. You can get it from

$ git clone
$ cd free; make
$ sudo mv free /usr/local/bin/free
$ free -m -t


                   total          active            free        inactive            wire          cached
Memory:             1917              59            1053             575             111              41
Summary:            1917             246            1670

Freecolor is a free replacement that displays free memory graphically as a bargraph. It supports the same options as free. Install freecolor, enter:

# pkg_add -r freecolor

To see memory details, enter:

# freecolor -m


Physical  : [#######################............] 67% (162/239)
Swap      : [##################################.] 99% (599/600)
# freecolor -m -o


             total       used       free     shared    buffers     cached
Mem:           239         77        162          0          0          0
Swap:          600          0        599
Categories: FREEBSD Tags: , ,