The following examples demonstrate querying and setting cpuset
options using shell commands.
Creating and attaching to a cpuset.
To create a new cpuset and attach the current command shell to
it, the steps are:
1) mkdir /dev/cpuset (if not already done)
2) mount -t cpuset none /dev/cpuset (if not already done)
3) Create the new cpuset using mkdir(1).
4) Assign CPUs and memory nodes to the new cpuset.
5) Attach the shell to the new cpuset.
For example, the following sequence of commands will set up a
cpuset named "Charlie", containing just CPUs 2 and 3, and memory
node 1, and then attach the current shell to that cpuset.
$ mkdir /dev/cpuset
$ mount -t cpuset cpuset /dev/cpuset
$ cd /dev/cpuset
$ mkdir Charlie
$ cd Charlie
$ /bin/echo 2-3 > cpuset.cpus
$ /bin/echo 1 > cpuset.mems
$ /bin/echo $$ > tasks
# The current shell is now running in cpuset Charlie
# The next line should display '/Charlie'
$ cat /proc/self/cpuset
Migrating a job to different memory nodes.
To migrate a job (the set of processes attached to a cpuset) to
different CPUs and memory nodes in the system, including moving
the memory pages currently allocated to that job, perform the
following steps.
1) Let's say we want to move the job in cpuset alpha (CPUs 4–7
and memory nodes 2–3) to a new cpuset beta (CPUs 16–19 and
memory nodes 8–9).
2) First create the new cpuset beta.
3) Then allow CPUs 16–19 and memory nodes 8–9 in beta.
4) Then enable memory_migration in beta.
5) Then move each process from alpha to beta.
The following sequence of commands accomplishes this.
$ cd /dev/cpuset
$ mkdir beta
$ cd beta
$ /bin/echo 16-19 > cpuset.cpus
$ /bin/echo 8-9 > cpuset.mems
$ /bin/echo 1 > cpuset.memory_migrate
$ while read i; do /bin/echo $i; done < ../alpha/tasks > tasks
The above should move any processes in alpha to beta, and any
memory held by these processes on memory nodes 2–3 to memory
nodes 8–9, respectively.
Notice that the last step of the above sequence did not do:
$ cp ../alpha/tasks tasks
The while loop, rather than the seemingly easier use of the cp(1)
command, was necessary because only one process PID at a time may
be written to the tasks file.
The same effect (writing one PID at a time) as the while loop can
be accomplished more efficiently, in fewer keystrokes and in
syntax that works on any shell, but alas more obscurely, by using
the -u
(unbuffered) option of sed(1):
$ sed -un p < ../alpha/tasks > tasks