Debian Codegraph @

doc/tutorial.txt

raw
1 Cpuset (cset) Tutorial
2 ======================
3
4 Alex Tsariounov <[email protected]> +
5 Copyright (c) 2009-2011 Novell Inc., cset v1.6pre1 +
6 Verbatim copying and distribution of this entire article are permitted
7 worldwide, without royalty, in any medium, provided this notice is preserved.
8
9 This tutorial describes basic and advanced usage of the *cset* command to
10 manipulate cpusets on a Linux system. See also the manpages that come with
11 the *cset* command: cset(1), cset-shield(1), cset-set(1), and cset-proc(1) for
12 more details. Additionally, the *cset* command has online help.
13
14
15 Introduction
16 ============
17 In the Linux kernel, the cpuset facility provides a mechanism for creating
18 logical entities called "cpusets" that encompass definitions of CPUs and NUMA
19 Memory Nodes (if NUMA is available). Cpusets constrain the CPU and Memory
20 placement of a task to only the resources defined within that cpuset. These
21 cpusets can then be arranged into a nested hierarchy visible in the "cpuset"
22 virtual filesystem. Sets of tasks can be assigned to these cpusets to
23 constrain the resources that they use. The tasks can be moved from one cpuset
24 to another to utilize other resources defined in those other cpusets.
25
26 The *cset* command is a Python application that provides a command line front
27 end for the Linux cpusets functionality. Working with cpusets directly can be
28 confusing and slightly complex. The *cset* tool hides that complexity behind
29 an easy-to-use command line interface.
30
31 There are two distinct use cases for *cset*: the basic shielding use case and
32 the "advanced" case of using raw +set+ and +proc+ subcommands. The basic
33 shielding function is accessed with the +shield+ subcommand and described in
34 the next section. Using the raw +set+ and +proc+ subcommands allows one to
35 set up arbitrarily complex cpusets and is described in the later sections.
36
37 Note that in general, one either uses the +shield+ subcommand 'or' a
38 combination of the +set+ and +proc+ subcommands. One rarely, if ever, uses
39 all of these subcommands together. Doing so will likely become too
40 confusing. Additionally, the +shield+ subcommand sets up its required cpusets
41 with exclusively marked CPUs. This can interfere with your cpuset
42 strategy. If you find that you need more functionality for your strategy than
43 +shield+ provides, go ahead and transition to using +set+ and +proc+
44 exclusively. It is straightforward to implement what +shield+ does with a few
45 extra +set+ and +proc+ subcommands.
46
47
48 Obtaining Online Help
49 ---------------------
50 For a full list of *cset* subcommands::
51 +# cset help+
52
53 For in-depth help on individual subcommands::
54 +# cset help <subcommand>+
55
56 For options of individual subcommands::
57 +# cset <subcommand> (-h | --help)+
58
59
60 The Basic Shielding Model
61 =========================
62 Although any set up of cpusets can really be described as "shielding," there
63 is one prevalent shielding model in use that is so common that *cset* has a
64 subcommand that is dedicated to its use. This subcommand is called +shield+.
65
66 The concept behind this model is the use of three cpusets. The 'root' cpuset
67 which is always present in all configurations and contains all CPUs. The
68 'system' cpuset which contains CPUs which are used for system tasks. These
69 are the normal tasks that are not "important," but which need to run on the
70 system. And finally, the 'user' cpuset which contains CPUs which are used for
71 "important" tasks. The 'user' cpuset is the shield. Only those tasks that
72 are somehow important, usually tasks whose performance determines the overall
73 rating for the machine, are run in the 'user' cpuset.
74
75 The +shield+ subcommand manages all of these cpusets and lets you define the
76 CPUs and Memory Nodes that are in the 'shielded' and 'unshielded' sets. The
77 subcommand automatically moves all movable tasks on the system into the
78 'unshielded' cpuset on shield activation, and back into the 'root' cpuset on
79 shield tear down. The subcommand then lets you move tasks into and out of the
80 shield. Additionally, you can move special tasks (kernel threads) which
81 normally run in the 'root' cpuset into the 'unshielded' set so that your
82 shield will have even less disturbance.
83
84 The +shield+ subcommand abstracts the management of these cpusets away from
85 you and provides options that drive how the shield is set up, which tasks are
86 to be shielded and which tasks are not, and status of the shield. In fact,
87 you need not be bothered with the naming of the required cpusets or even where
88 the cpuset filesystem is mounted. *Cset* and the +shield+ subcommand takes
89 care of all that.
90
91 If you find yourself needing to define more cpusets for your application, then
92 it is likely that this simple shielding is not a rich enough model for you.
93 In this case, you should transition to using the +set+ and +proc+ subcommands
94 described in a later section.
95
96
97 A Simple Shielding Example
98 --------------------------
99 Assume that we have a 4-way machine that is not NUMA. This means there are 4
100 CPUs at our disposal and there is only one Memory Node available. On such
101 machines, we do not need to specify any memory node parameters to *cset*, it
102 sets up the only available memory node by default.
103
104 Usually, one wants to dedicate as many CPUs to the shield as possible and
105 leave a minimal set of CPUs for normal system processing. The reasoning for
106 this is because the performance of the important tasks will rule the
107 performance of the installation as a whole and these important tasks need as
108 many resources available to them as possible, exclusive of other, unimportant
109 tasks that are running on the system.
110
111 NOTE: I use the word "task" to represent either a process or a thread that is
112 running on the system.
113
114
115 Setup and Teardown of the Shield
116 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
117 To set up a shield of 3 CPUs with 1 CPU left for low priority system
118 processing, issue the following command.
119
120 -------------------------------------------------------------------
121 [zuul:cpuset-trunk]# cset shield -c 1-3
122 cset: --> activating shielding:
123 cset: moving 176 tasks from root into system cpuset...
124 [==================================================]%
125 cset: "system" cpuset of CPUSPEC(0) with 176 tasks running
126 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
127 -------------------------------------------------------------------
128
129 This command does a number of things. First, a 'user' cpuset is created with
130 what's called a CPUSPEC (CPU specification) from the +-c/\--cpu+ option. This
131 CPUSPEC specifies to use CPUs 1 through 3 inclusively. Next, the command
132 creates a 'system' cpuset with a CPUSPEC that is the inverse of the +-c+
133 option for the current machine. On this machine that cpuset will only contain
134 the first CPU, CPU0. Next, all userspace processes running in the 'root'
135 cpuset are transfered to the 'system' cpuset. This makes all those processes
136 run only on CPU0. The effect of this is that the shield consists of CPUs 1
137 through 3 and they are now idling.
138
139 Note that the command did not move the kernel threads that are running in the
140 'root' cpuset to the 'system' cpuset. This is because you may want these
141 kernel threads to use all available CPUs. If you do not, the you can use the
142 +-k/\--kthread+ option as described below.
143
144 The shield setup command above outputs the information of which cpusets were
145 created and how many tasks are running on each. If you want to see the
146 current status of the shield again, issue this command:
147
148 -------------------------------------------------------------------
149 [zuul:cpuset-trunk]# cset shield
150 cset: --> shielding system active with
151 cset: "system" cpuset of CPUSPEC(0) with 176 tasks running
152 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
153 -------------------------------------------------------------------
154
155 Which shows us that the shield is set up and that 176 tasks are running in the
156 'system' cpuset--the "unshielded" cpuset.
157
158 It is important to move all possible tasks from the 'root' cpuset to the
159 unshielded 'system' cpuset because a task's cpuset property is inherited by
160 its children. Since we've moved all running tasks (including init) to the
161 unshielded 'system' cpuset, that means that any new tasks that are spawned
162 will *also* run in the unshielded 'system' cpuset.
163
164 Some kernel threads can be moved into the unshielded 'system' cpuset as well.
165 These are the threads that are not bound to specific CPUs. If a kernel thread
166 is bound to a specific CPU, then it is generally not a good idea to move that
167 thread to the 'system' set because at worst it may hang the system and at best
168 it will slow the system down significantly. These threads are usually the IRQ
169 threads on a real time Linux kernel, for example, and you may want to not move
170 these kernel threads into 'system'. If you leave them in the 'root' cpuset,
171 then they will have access to all CPUs.
172
173 However, if your application demands an even "quieter" shield, then you can
174 move all movable kernel threads into the unshielded 'system' set with the
175 following command.
176
177 --------------------------------------------------------------------
178 [zuul:cpuset-trunk]# cset shield -k on
179 cset: --> activating kthread shielding
180 cset: kthread shield activated, moving 70 tasks into system cpuset...
181 [==================================================]%
182 cset: done
183 --------------------------------------------------------------------
184
185 You can see that this moved an additional 70 tasks to the unshielded 'system'
186 cpuset. Note that the +-k/\--kthread on+ parameter can be given at the shield
187 creation time as well and you do not need to perform these two steps
188 separately if you know that you will want kernel thread shielding as well.
189 Executing *cset shield* again shows us the current state of the shield.
190
191 --------------------------------------------------------------------
192 [zuul:cpuset-trunk]# cset shield
193 cset: --> shielding system active with
194 cset: "system" cpuset of CPUSPEC(0) with 246 tasks running
195 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
196 --------------------------------------------------------------------
197
198 You can get a detailed listing of what is running in the shield by specifying
199 either +-s/\--shield+ or +-u/\--unshield+ to the +shield+ subcommand and using
200 the verbose flag. You will get output similar to the following.
201
202 --------------------------------------------------------------------
203 [zuul:cpuset-trunk]# cset shield --unshield -v
204 cset: "system" cpuset of CPUSPEC(0) with 251 tasks running
205 USER PID PPID SPPr TASK NAME
206 -------- ----- ----- ---- ---------
207 root 1 0 Soth init [5]
208 root 2 0 Soth [kthreadd]
209 root 84 2 Sf50 [IRQ-9]
210 ...
211 alext 31796 31789 Soth less
212 root 32653 25222 Roth python ./cset shield --unshield -v
213 --------------------------------------------------------------------
214
215 Note that I abbreviated the listing; we do have 251 tasks running in the
216 'system' set. The output is self-explanatory; however, the "SPPr" field may
217 need a little explanation. "SPPr" stands for State, Policy and Priority. You
218 can see that the initial two tasks are Stopped and running in timeshare
219 priority, marked as "oth" (for "other"). The [IRQ-9] task is also stopped,
220 but marked at real time FIFO policy with a priority of 50. The last task in
221 the listing is the *cset* command itself and is marked as running. Also note
222 that adding a second +-v/\--verbose+ option will not restrict the output to
223 fit into an 80 character screen.
224
225 Tear down of the shield, stopping the shield in other words, is done with the
226 +-r/\--reset+ option to the +shield+ subcommand. When this command is issued,
227 both the 'system' and 'user' cpusets are deleted and any tasks that are
228 running in both of those cpusets are moved to the 'root' cpuset. Once so
229 moved, all tasks will have access to all resources on the system. For
230 example:
231
232 --------------------------------------------------------------------
233 [zuul:cpuset-trunk]# cset shield --reset
234 cset: --> deactivating/reseting shielding
235 cset: moving 0 tasks from "/user" user set to root set...
236 cset: moving 250 tasks from "/system" system set to root set...
237 [==================================================]%
238 cset: deleting "/user" and "/system" sets
239 cset: done
240 --------------------------------------------------------------------
241
242
243 Moving Interesting Tasks Into and Out of the Shield
244 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
245 Now that we have a shield running, the objective is to run our "important"
246 processes in that shield. These processes can be anything, but usually they
247 are directly related to the purpose of the machine. There are two ways to run
248 tasks in the shield:
249
250 . Exec a process into the shield
251 . Move an already running task into the shield
252
253
254 Execing a Process into the Shield
255 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
256 Running a new process in the shield can be done with the +-e/\--exec+ option
257 to the +shield+ subcommand. This is the simplest way to get a task to run in
258 the shield. For this example, let's exec a new bash shell into the shield
259 with the following commands.
260
261 --------------------------------------------------------------------
262 [zuul:cpuset-trunk]# cset shield -s
263 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
264 cset: done
265
266 [zuul:cpuset-trunk]# cset shield -e bash
267 cset: --> last message, executed args into cpuset "/user", new pid is: 13300
268
269 [zuul:cpuset-trunk]# cset shield -s -v
270 cset: "user" cpuset of CPUSPEC(1-3) with 2 tasks running
271 USER PID PPID SPPr TASK NAME
272 -------- ----- ----- ---- ---------
273 root 13300 8583 Soth bash
274 root 13329 13300 Roth python ./cset shield -s -v
275
276 [zuul:cpuset-trunk]# exit
277
278 [zuul:cpuset-trunk]# cset shield -s
279 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
280 cset: done
281 --------------------------------------------------------------------
282
283 The first command above lists the status of the shield. We see that the
284 shield is defined as CPUs 1 through 3 inclusive and currently there are no
285 tasks running in it.
286
287 The second command execs the bash shell into the shield with the +-e+
288 option. The last message of cset lists the PID of the new process.
289
290 NOTE: *cset* follows the tradition of separating the tool options from the
291 command to be execed options with a double dash (+\--+). This is not shown in
292 this simple example, but if the command you want to exec also takes options,
293 separate them with the double dash like so: +# cset shield -e mycommand \-- -v+
294 The +-v+ will be passed to +mycommand+, and not to *cset*.
295
296 The next command lists the status of the shield again. You will note that
297 there are actually two tasks running shielded: our new shell and the *cset*
298 status command itself. Remember that the cpuset property of a task is
299 inherited by its children. Since we ran the new shell in the shield, its
300 child, which is the status command, also ran in the shield.
301
302 TIP: Execing a shell into the shield is a useful way to experiment with
303 running tasks in the shield since all children of the shell will also run in
304 the shield.
305
306 The last command exits the shell after which we request a shield status again
307 and see that once again, it does not contain any tasks.
308
309 You may have noticed in the output above that both the new shell and the
310 status command are running as the root user. This is because *cset* needs to
311 run as root and so all it's children will also run as root. If you need to
312 run a process under a different user and or group, you may use the +\--user+
313 and +\--group+ options for +exec+ as follows.
314
315 --------------------------------------------------------------------
316 [zuul:cpuset-trunk]# cset shield --user=alext --group=users -e bash
317 cset: --> last message, executed args into cpuset "/user", new pid is: 14212
318
319 alext@zuul> cset shield -s -v
320 cset: "user" cpuset of CPUSPEC(1-3) with 2 tasks running
321 USER PID PPID SPPr TASK NAME
322 -------- ----- ----- ---- ---------
323 alext 14212 8583 Soth bash
324 alext 14241 14212 Roth python ./cset shield -s -v
325 --------------------------------------------------------------------
326
327
328 Moving a Running Task into and out of the Shield
329 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
330 While execing a process into the shield is undoubtably useful, most of the
331 time, you'll want to move already running tasks into and out of the shield.
332 The *cset* shield subcommand includes two options for doing this:
333 +-s/\--shield+ and +-u/\--unshield+. These options require what's called a
334 PIDSPEC (process specification) to also be specified with the +-p/\--pid+
335 option. The PIDSPEC defines which tasks get operated on. The PIDSPEC can be
336 a single process ID, a list of process IDs separated by commas, and a list of
337 process ID ranges separated by dashes, groups of which are separated by
338 commas. For example:
339
340 +\--shield --pid 1234+::
341 This PIDSPEC argument specifies that PID 1234 be shielded.
342
343 +\--shield --pid 1234,42,1934,15000,15001,15002+::
344 This PIDSPEC argument specifies that this list of PIDs only be moved into the
345 shield.
346
347 +\--unshield -p 5000,5100,6010-7000,9232+::
348 This PIDSPEC argument specifies that PIDs 5000,5100 and 9232 be unshielded
349 (moved out of the shield) along with any existing PID that is in the range
350 6010 through 7000 inclusive.
351
352 NOTE: A range in a PIDSPEC does not have to have tasks running for every
353 number in that range. In fact, it is not even an error if there are no tasks
354 running in that range; none will be moved in that case. The range simply
355 specifies to act on any tasks that have a PID or TID that is within that
356 range.
357
358 Use of the appropriate PIDSPEC can thus be handy to move tasks and groups of
359 tasks into and out of the shield. Additionally, there is one more option that
360 can help with multi-threaded processes, and that is the +\--threads+ flag. If
361 this flag is present in a +shield+ or +unshield+ command with a PIDSPEC and if
362 any of the task IDs in the PIDSPEC belong to a thread in a process container,
363 then *all* the sibling threads in that process container will get shielded or
364 unshielded as well. This flag provides an easy mechanism to shield/unshield
365 all threads of a process by simply specifying one thread in that process.
366
367 In the following example, we move the current shell into the shield with a
368 range PIDSPEC and back out with the bash variable for the current PID.
369
370 -----------------------------------------------------------------------
371 [zuul:cpuset-trunk]# echo $$
372 22018
373
374 [zuul:cpuset-trunk]# cset shield -s -p 22010-22020
375 cset: --> shielding following pidspec: 22010-22020
376 cset: done
377
378 [zuul:cpuset-trunk]# cset shield -s -v
379 cset: "user" cpuset of CPUSPEC(1-3) with 2 tasks running
380 USER PID PPID SPPr TASK NAME
381 -------- ----- ----- ---- ---------
382 root 3770 22018 Roth python ./cset shield -s -v
383 root 22018 5034 Soth bash
384 cset: done
385
386 [zuul:cpuset-trunk]# cset shield -u -p $$
387 cset: --> unshielding following pidspec: 22018
388 cset: done
389
390 [zuul:cpuset-trunk]# cset shield -s
391 cset: "user" cpuset of CPUSPEC(1-3) with 0 tasks running
392 cset: done
393 -----------------------------------------------------------------------
394
395 NOTE: Ordinarily, the +shield+ option will shield a PIDSPEC only if it is
396 currently running in the 'system' set--the unshielded set. The +unshield+
397 option will unshield a PIDSPEC only if it is currently running in the 'user'
398 set--the shielded set. If you want to +shield/unshield+ a process that
399 happens to be running in the 'root' set (not common), then use the +\--force+
400 option for these commands.
401
402
403 Full Featured Cpuset Manipulation Commands
404 ==========================================
405 While basic shielding as described above is useful and a common use model for
406 *cset*, there comes a time when more functionality will be desired to
407 implement your strategy. To implement this, *cset* provides two subcommands:
408 +set+, which allows you to manipulate cpusets; and +proc+, which allows you to
409 manipulate processes within those cpusets.
410
411
412 The Set Subcommand
413 ------------------
414 In order to do anything with cpusets, you must be able to create, adjust,
415 rename, move and destroy them. The +set+ subcommand allows the management of
416 cpusets in such a manner.
417
418
419 Creating and Destroying Cpusets with Set
420 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
421 The basic syntax of +set+ for cpuset creation is:
422
423 ----------------------------------------------------------
424 [zuul:cpuset-trunk]# cset set -c 1-3 -s my_cpuset1
425 cset: --> created cpuset "my_cpuset1"
426 ----------------------------------------------------------
427
428 This creates a cpuset named "my_cpuset1" with a CPUSPEC of CPU1, CPU2 and
429 CPU3. The CPUSPEC is the same concept as described in the "Setup and
430 Teardown of the Shield" section above. The +set+ subcommand also takes a
431 +-m/\--mem+ option that lets you specify the memory nodes the set will use as
432 well as flags to make the CPUs and MEMs exclusive to the cpuset. If you are
433 on a non-NUMA machine, just leave the +-m+ option out and the default memory
434 node 0 will be used.
435
436 Just like with +shield+, you can adjust the CPUs and MEMs with subsequent
437 calls to +set+. If, for example, you wish to adjust the "my_cpuset1" cpuset
438 to only use CPUs 1 and 3 (and omit CPU2), then issue the following command.
439
440 ----------------------------------------------------------
441 [zuul:cpuset-trunk]# cset set -c 1,3 -s my_cpuset1
442 cset: --> modified cpuset "my_cpuset
443 ----------------------------------------------------------
444
445 *cset* will then adjust the CPUs that are assigned to the "my_cpuset1" set to
446 only use CPU1 and CPU3.
447
448 To rename a cpuset, use the +-n/\--newname+ option. For example:
449
450 ----------------------------------------------------------
451 [zuul:cpuset-trunk]# cset set -s my_cpuset1 -n super_set
452 cset: --> renaming "/cpusets/my_cpuset1" to "super_set"
453 ----------------------------------------------------------
454
455 Renames the cpuset called "my_cpuset1" to "super_set".
456
457 To destroy a cpuset, use the +-d/\--destroy+ option as follows.
458
459 ----------------------------------------------------------
460 [zuul:cpuset-trunk]# cset set -d super_set
461 cset: --> processing cpuset "super_set", moving 0 tasks to parent "/"...
462 cset: --> deleting cpuset "/super_set"
463 cset: done
464 ----------------------------------------------------------
465
466 This command destroys the newly created cpuset called "super_set". When a
467 cpuset is destroyed, all the tasks running in it are moved to the parent
468 cpuset. The 'root' cpuset, which always exists and always contains all CPUs,
469 can not be destroyed. You may also give the +\--destroy+ option a list of
470 cpusets to destroy.
471
472 NOTE: The *cset* subcommand creates the cpusets based on a mounted cpuset
473 filesystem. You do not need to know where that filesystem is mounted,
474 although it is easy to figure out (by default it's on '/cpusets'). When you
475 give the +set+ subcommand a name for a new cpuset, it is created wherever the
476 cpuset filesystem is mounted at.
477
478 If you want to create a cpuset hierarchy, then you must give a path to the
479 *cset* +set+ subcommand. This path will always begin with the 'root' cpuset,
480 for which the path is '/'. For example.
481
482 ----------------------------------------------------------
483 [zuul:cpuset-trunk]# cset set -c 1,3 -s top_set
484 cset: --> created cpuset "top_set"
485
486 [zuul:cpuset-trunk]# cset set -c 3 -s /top_set/sub_set
487 cset: --> created cpuset "/top_set/sub_set"
488 ----------------------------------------------------------
489
490 These commands created two cpusets: 'top_set' and 'sub_set'. The 'top_set'
491 uses CPU1 and CPU3. It has a subset of 'sub_set' which only uses CPU3. Once
492 you have created a subset with a path, then if the name is unique, you do not
493 have to specify the path in order to affect it. If the name is not unique,
494 then *cset* will complain and ask you to use the path. For example:
495
496 ----------------------------------------------------------
497 [zuul:cpuset-trunk]# cset set -c 1,3 -s sub_set
498 cset: --> modified cpuset "sub_set
499 ----------------------------------------------------------
500
501 This command adds CPU1 to the 'sub_set' cpuset for it's use. Note that using
502 the path in this case is optional.
503
504 If you attempt to destroy a cpuset which has sub-cpusets, *cset* will complain
505 and not do it unless you use the +-r/\--recurse+ and the +\--force+ options.
506 If you do use +\--force+, then all the tasks running in all subsets of the
507 deletion target cpuset will be moved to the target's parent cpuset and all
508 cpusets.
509
510 Moving a cpuset from under a certain cpuset to a different location is
511 currently not implemented and is slated for a later release of *cset*.
512
513
514 Listing Cpusets with Set
515 ~~~~~~~~~~~~~~~~~~~~~~~~
516 To list cpusets, use the +set+ subcommand with the '-l/\--list' option. For
517 example:
518
519 ----------------------------------------------------------
520 [zuul:cpuset-trunk]# cset set -l
521 cset:
522 Name CPUs-X MEMs-X Tasks Subs Path
523 ------------ ---------- - ------- - ----- ---- ----------
524 root 0-3 y 0 y 320 1 /
525 one 3 n 0 n 0 1 /one
526 ----------------------------------------------------------
527
528 This shows that there is currently one cpuset present called 'one'. (Of course
529 that there is also the 'root' set, which is always present.) The output shows
530 that the 'one' cpuset has no tasks running in it. The 'root' cpuset has 320
531 tasks running. The "-X" for "CPUs" and "MEMs" fields denotes whether the CPUs
532 and MEMs in the cpusets are marked exclusive to those cpusets. Note that the
533 'one' cpuset has subsets as indicated by a 1 in the 'Subs' field. You can
534 specify a cpuset to list with the +set+ subcommand as follows.
535
536 ----------------------------------------------------------
537 [zuul:cpuset-trunk]# cset set -l -s one
538 cset:
539 Name CPUs-X MEMs-X Tasks Subs Path
540 ------------ ---------- - ------- - ----- ---- ----------
541 one 3 n 0 n 0 1 /one
542 two 3 n 0 n 0 1 /one/two
543 ----------------------------------------------------------
544
545 This output shows that there is a cpuset called 'two' in cpuset 'one' and it
546 also has subset. You can also ask for a recursive listing as follows.
547
548 ----------------------------------------------------------
549 [zuul:cpuset-trunk]# cset set -l -r
550 cset:
551 Name CPUs-X MEMs-X Tasks Subs Path
552 ------------ ---------- - ------- - ----- ---- ----------
553 root 0-3 y 0 y 320 1 /
554 one 3 n 0 n 0 1 /one
555 two 3 n 0 n 0 1 /one/two
556 three 3 n 0 n 0 0 /one/two/three
557 ----------------------------------------------------------
558
559 This command lists all cpusets existing on the system since it asks for a
560 recursive listing beginning at the 'root' cpuset. Incidentally, should you
561 need to specify the 'root' cpuset you can use either +root+ or +/+ to specify it
562 explicitely--just remember that the 'root' cpuset cannot be deleted or modified.
563
564
565 The Proc Subcommand
566 -------------------
567 Now that we know how to create, rename and destroy cpusets with the +set+
568 subcommand, the next step is to manage threads and processes in those
569 cpusets. The subcommand to do this is called +proc+ and it allows you to exec
570 processes into a cpuset, move existing tasks around existing cpusets, and list
571 tasks running in specified cpusets. For the following examples, let us
572 assume a cpuset setup of two sets as follows:
573
574 ----------------------------------------------------------
575 [zuul:cpuset-trunk]# cset set -l
576 cset:
577 Name CPUs-X MEMs-X Tasks Subs Path
578 ------------ ---------- - ------- - ----- ---- ----------
579 root 0-3 y 0 y 309 2 /
580 two 2 n 0 n 3 0 /two
581 three 3 n 0 n 10 0 /three
582 ----------------------------------------------------------
583
584 Listing Tasks with Proc
585 ~~~~~~~~~~~~~~~~~~~~~~~
586 Operation of the +proc+ subcommand follows the same model as the +set+
587 subcommand. For example, to list tasks in a cpuset, you need to use the
588 +-l/\--list+ option and specify the cpuset by name or, if the name exists
589 multiple times in the cpuset hierarchy, by path. For example:
590
591 ----------------------------------------------------------
592 [zuul:cpuset-trunk]# cset proc -l -s two
593 cset: "two" cpuset of CPUSPEC(2) with 3 tasks running
594 USER PID PPID SPPr TASK NAME
595 -------- ----- ----- ---- ---------
596 root 16141 4300 Soth bash
597 root 16171 16141 Soth bash
598 root 16703 16171 Roth python ./cset proc -l two
599 ----------------------------------------------------------
600
601 This output shows us that the cpuset called 'two' has CPU2 only attached to it
602 and is running three tasks: two shells and the python command to list it.
603 Note that cpusets are inherited so that if a process is contained in a cpuset,
604 then any children it spawns also run within that set. In this case, the
605 python command to list set 'two' was run from a shell already running in set
606 'two'. This can be seen by the PPID (parent process ID) of the python
607 command matching the PID of the shell.
608
609 Additionally, the "SPPr" field needs explanation. "SPPr" stands for State,
610 Policy and Priority. You can see that the initial two tasks are Stopped and
611 running in timeshare priority, marked as "oth" (for "other"). The last task
612 is marked as running, "R" and also at timeshare priority, "oth." If any of
613 these tasks would have been at real time priority, then the policy would be
614 shown as "f" for FIFO or "r" for round robin, and the priority would be a
615 number from 1 to 99. See below for an example.
616
617 ----------------------------------------------------------
618 [zuul:cpuset-trunk]# cset proc -l -s root | head -7
619 cset: "root" cpuset of CPUSPEC(0-3) with 309 tasks running
620 USER PID PPID SPPr TASK NAME
621 -------- ----- ----- ---- ---------
622 root 1 0 Soth init [5]
623 root 2 0 Soth [kthreadd]
624 root 3 2 Sf99 [migration/0]
625 root 4 2 Sf99 [posix_cpu_timer]
626 ----------------------------------------------------------
627
628 This output shows the first few tasks in the 'root' cpuset. Note that both
629 'init' and '[kthread]' are running at timeshare; however, the '[migration/0]'
630 and '[posix_cpu_timer]' kernel threads are running at real time policy of FIFO
631 and priority of 99. Incidentally, this output is from a system running the
632 real time Linux kernel which runs some kernel threads at real time
633 priorities. And finally, note that you can of course use *cset* as any other
634 Linux tool and include it in pipelines as in the example above.
635
636 Taking a peek into the third cpuset called 'three', we see:
637
638 ----------------------------------------------------------
639 [zuul:cpuset-trunk]# cset proc -l -s three
640 cset: "three" cpuset of CPUSPEC(3) with 10 tasks running
641 USER PID PPID SPPr TASK NAME
642 -------- ----- ----- ---- ---------
643 alext 16165 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
644 alext 16169 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
645 alext 16170 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
646 alext 16237 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
647 alext 16491 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
648 alext 16492 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
649 alext 16493 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
650 alext 17243 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
651 alext 17244 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
652 alext 17265 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
653 ----------------------------------------------------------
654
655 This output shows that a lot of 'beagled' tasks are running in this cpuset and
656 it also shows an ellipsis (...) at the end of their listings. If you see this
657 ellipsis, that means that the command was too long to fit onto an 80 character
658 screen. To see the entire commandline, use the +-v/\--verbose+ flag, as per
659 following.
660
661 ----------------------------------------------------------
662 [zuul:cpuset-trunk]# cset proc -l -s three -v | head -4
663 cset: "three" cpuset of CPUSPEC(3) with 10 tasks running
664 USER PID PPID SPPr TASK NAME
665 -------- ----- ----- ---- ---------
666 alext 16165 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg --autostarted --indexing-delay 300
667 ----------------------------------------------------------
668
669
670 Execing Tasks with Proc
671 ~~~~~~~~~~~~~~~~~~~~~~~
672 To exec a task into a cpuset, the +proc+ subcommand needs to be employed with
673 the +-e/\--exec+ option. Let's exec a shell into the cpuset named 'two' in
674 our set. First we check to see what is running that set:
675
676 ----------------------------------------------------------
677 [zuul:cpuset-trunk]# cset proc -l -s two
678 cset: "two" cpuset of CPUSPEC(2) with 0 tasks running
679
680 [zuul:cpuset-trunk]# cset proc -s two -e bash
681 cset: --> last message, executed args into cpuset "/two", new pid is: 20955
682
683 [zuul:cpuset-trunk]# cset proc -l -s two
684 cset: "two" cpuset of CPUSPEC(2) with 2 tasks running
685 USER PID PPID SPPr TASK NAME
686 -------- ----- ----- ---- ---------
687 root 20955 19253 Soth bash
688 root 20981 20955 Roth python ./cset proc -l two
689 ----------------------------------------------------------
690
691 You can see that initially, 'two' had nothing running in it. After the
692 completion of the second command, we list 'two' again and see that there are
693 two tasks running: the shell which we execed and the python *cset* command
694 that is listing the cpuset. The reason for the second task is that the cpuset
695 property of a running task is inherited by all its children. Since we
696 executed the listing command from the new shell which was bound to cpuset
697 'two', the resulting process for the listing is also bound to cpuset 'two'.
698 Let's test that by just running a new shell with no prefixed *cset* command.
699
700 ----------------------------------------------------------
701 [zuul:cpuset-trunk]# bash
702
703 [zuul:cpuset-trunk]# cset proc -l -s two
704 cset: "two" cpuset of CPUSPEC(2) with 3 tasks running
705 USER PID PPID SPPr TASK NAME
706 -------- ----- ----- ---- ---------
707 root 20955 19253 Soth bash
708 root 21118 20955 Soth bash
709 root 21147 21118 Roth python ./cset proc -l two
710 ----------------------------------------------------------
711
712 Here again we see that the second shell, PID 21118, has a parent PID of 20955
713 which is the first shell. Both shells as well as the listing command are
714 running in the 'two' cpuset.
715
716 NOTE: *cset* follows the tradition of separating the tool options from the
717 command to be execed options with a double dash (+\--+). This is not shown in
718 this simple example, but if the command you want to exec also takes options,
719 separate them with the double dash like so: +# cset proc -s myset -e mycommand
720 \-- -v+ The +-v+ will be passed to +mycommand+, and not to *cset*.
721
722 TIP: Execing a shell into a cpuset is a useful way to experiment with running
723 tasks in that cpuset since all children of the shell will also run in the same
724 cpuset.
725
726 Finally, if you misspell the command to be execed, the result may be
727 puzzling. For example:
728
729 ----------------------------------------------------------
730 [zuul:cpuset-trunk]# cset proc -s two -e blah-blah
731 cset: --> last message, executed args into cpuset "/two", new pid is: 21655
732 cset: **> [Errno 2] No such file or directory
733 ----------------------------------------------------------
734
735 The result is no new process even though a new PID is output. The reason for
736 the message is of course that the *cset* process forked in preparation for
737 exec, but the command +blah-blah+ was not found in order to exec it.
738
739
740 Moving Tasks with Proc
741 ~~~~~~~~~~~~~~~~~~~~~~
742 Although the ability to exec a task into a cpuset is fundamental, you will
743 most likely be moving tasks between cpusets more often. Moving tasks is
744 accomplished with the +-m/\--move+ and +-p/\--pid+ options to the +proc+
745 subcommand of *cset*. The move option tells the +proc+ subcommand that a task
746 move is requested. The +-p/\--pid+ option takes an argument called a PIDSPEC
747 (PID Specification). The PIDSPEC defines which tasks get operated on.
748
749 The PIDSPEC can be a single process ID, a list of process IDs separated by
750 commas, and a list of process ID ranges also separated by commas. For
751 example:
752
753 +\--move --pid 1234+::
754 This PIDSPEC argument specifies that task 1234 be moved.
755
756 +\--move --pid 1234,42,1934,15000,15001,15002+::
757 This PIDSPEC argument specifies that this list of tasks only be moved.
758
759 +\--move --pid 5000,5100,6010-7000,9232+::
760 This PIDSPEC argument specifies that tasks 5000,5100 and 9232 be moved along
761 with any existing task that is in the range 6010 through 7000 inclusive.
762
763 NOTE: A range in a PIDSPEC does not have to have running tasks for every
764 number in that range. In fact, it is not even an error if there are no tasks
765 running in that range; none will be moved in that case. The range simply
766 specifies to act on any tasks that have a PID or TID that is within that
767 range.
768
769 In the following example, we move the current shell into the cpuset named
770 'two' with a range PIDSPEC and back out to the 'root' cpuset with the bash
771 variable for the current PID.
772
773 -----------------------------------------------------------------------
774 [zuul:cpuset-trunk]# cset proc -l -s two
775 cset: "two" cpuset of CPUSPEC(2) with 0 tasks running
776
777 [zuul:cpuset-trunk]# echo $$
778 19253
779
780 [zuul:cpuset-trunk]# cset proc -m -p 19250-19260 -t two
781 cset: moving following pidspec: 19253
782 cset: moving 1 userspace tasks to /two
783 cset: done
784
785 [zuul:cpuset-trunk]# cset proc -l -s two
786 cset: "two" cpuset of CPUSPEC(2) with 2 tasks running
787 USER PID PPID SPPr TASK NAME
788 -------- ----- ----- ---- ---------
789 root 19253 16447 Roth bash
790 root 29456 19253 Roth python ./cset proc -l -s two
791
792 [zuul:cpuset-trunk]# cset proc -m -p $$ -t root
793 cset: moving following pidspec: 19253
794 cset: moving 1 userspace tasks to /
795 cset: done
796
797 [zuul:cpuset-trunk]# cset proc -l -s two
798 cset: "two" cpuset of CPUSPEC(2) with 0 tasks running
799 -----------------------------------------------------------------------
800
801 Use of the appropriate PIDSPEC can thus be handy to move tasks and groups of
802 tasks. Additionally, there is one more option that can help with
803 multi-threaded processes, and that is the +\--threads+ flag. If this flag is
804 present in a +proc+ move command with a PIDSPEC and if any of the task IDs in
805 the PIDSPEC belongs to a thread in a process container, then *all* the sibling
806 threads in that process container will also get moved. This flag provides an
807 easy mechanism to move all threads of a process by simply specifying one
808 thread in that process. In the following example, we move all the threads
809 running in cpuset 'three' to cpuset 'two' by using the +\--threads+ flag.
810
811 ----------------------------------------------------------
812 [zuul:cpuset-trunk]# cset set two three
813 cset:
814 Name CPUs-X MEMs-X Tasks Subs Path
815 ------------ ---------- - ------- - ----- ---- ----------
816 two 2 n 0 n 0 0 /two
817 three 3 n 0 n 10 0 /three
818
819 [zuul:cpuset-trunk]# cset proc -l -s three
820 cset: "three" cpuset of CPUSPEC(3) with 10 tasks running
821 USER PID PPID SPPr TASK NAME
822 -------- ----- ----- ---- ---------
823 alext 16165 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
824 alext 16169 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
825 alext 16170 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
826 alext 16237 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
827 alext 16491 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
828 alext 16492 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
829 alext 16493 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
830 alext 17243 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
831 alext 17244 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
832 alext 27133 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
833
834 [zuul:cpuset-trunk]# cset proc -m -p 16165 --threads -t two
835 cset: moving following pidspec: 16491,16493,16492,16170,16165,16169,27133,17244,17243,16237
836 cset: moving 10 userspace tasks to /two
837 [==================================================]%
838 cset: done
839
840 [zuul:cpuset-trunk]# cset set two three
841 cset:
842 Name CPUs-X MEMs-X Tasks Subs Path
843 ------------ ---------- - ------- - ----- ---- ----------
844 two 2 n 0 n 10 0 /two
845 three 3 n 0 n 0 0 /three
846 ----------------------------------------------------------
847
848
849 Moving All Tasks from one Cpuset to Another
850 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
851 There is a special case for moving all tasks currently running in one cpuset
852 to another. This can be a common use case, and when you need to do it,
853 specifying a PIDSPEC with +-p+ is not necessary 'so long as' you use the
854 +-f/\--fromset+ *and* the +-t/\--toset+ options.
855
856 In the following example, we move all 10 +beagled+ threads back to cpuset
857 'three' with this method.
858
859 ----------------------------------------------------------
860 [zuul:cpuset-trunk]# cset proc -l two three
861 cset: "two" cpuset of CPUSPEC(2) with 10 tasks running
862 USER PID PPID SPPr TASK NAME
863 -------- ----- ----- ---- ---------
864 alext 16165 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
865 alext 16169 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
866 alext 16170 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
867 alext 16237 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
868 alext 16491 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
869 alext 16492 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
870 alext 16493 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
871 alext 17243 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
872 alext 17244 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
873 alext 27133 1 Soth beagled /usr/lib64/beagle/BeagleDaemon.exe --bg -...
874 cset: "three" cpuset of CPUSPEC(3) with 0 tasks running
875
876 [zuul:cpuset-trunk]# cset proc -m -f two -t three
877 cset: moving all tasks from two to /three
878 cset: moving 10 userspace tasks to /three
879 [==================================================]%
880 cset: done
881
882 [zuul:cpuset-trunk]# cset set two three
883 cset:
884 Name CPUs-X MEMs-X Tasks Subs Path
885 ------------ ---------- - ------- - ----- ---- ----------
886 two 2 n 0 n 0 0 /two
887 three 3 n 0 n 10 0 /three
888 ----------------------------------------------------------
889
890
891 Moving Kernel Threads with Proc
892 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
893 Kernel threads are special and *cset* detects tasks that are kernel threads
894 and will refuse to move them unless you also add a +-k/\--kthread+ option to
895 your +proc+ move command. Even if you include +-k+, *cset* will 'still'
896 refuse to move the kernel thread if they are bound to specific CPUs. The
897 reason for this is system protection.
898
899 A number of kernel threads, especially on the real time Linux kernel, are
900 bound to specific CPUs and depend on per-CPU kernel variables. If you move
901 these threads to a different CPU than what they are bound to, you risk at best
902 that the system will become horribly slow, and at worst a system hang. If you
903 still insist to move those threads (after all, *cset* needs to give the
904 knowledgeable user access to the keys), then you need to use the +--force+
905 option additionally.
906
907 WARNING: Overriding a task move command with +--force+ can have dire
908 consequences for the system. Please be sure of the command before you force
909 it.
910
911 In the following example, we move all unbound kernel threads running in the
912 'root' cpuset to the cpuset named 'two' by using the +-k+ option.
913
914 ----------------------------------------------------------
915 [zuul:cpuset-trunk]# cset proc -k -f root -t two
916 cset: moving all kernel threads from / to /two
917 cset: moving 70 kernel threads to: /two
918 cset: --> not moving 76 threads (not unbound, use --force)
919 [==================================================]%
920 cset: done
921 ----------------------------------------------------------
922
923 You will note that we used the fromset->toset facility of the +proc+
924 subcommand and we only specified the +-k+ option (not the +-m+ option). This
925 has the effect of moving all kernel threads only.
926
927 Note that only 70 actual kernel threads were moved and 76 were not. The
928 reason that 76 kernel threads were not moved was because they are bound to
929 specific CPUs. Now, let's move those kernel threads back to 'root'.
930
931 ----------------------------------------------------------
932 [zuul:cpuset-trunk]# cset proc -k -f two -t root
933 cset: moving all kernel threads from /two to /
934 cset: ** no task matched move criteria
935 cset: **> kernel tasks are bound, use --force if ok
936
937 [zuul:cpuset-trunk]# cset set -l -s two
938 cset:
939 Name CPUs-X MEMs-X Tasks Subs Path
940 ------------ ---------- - ------- - ----- ---- ----------
941 two 2 n 0 n 70 0 /two
942 ----------------------------------------------------------
943
944 Ah! What's this? *Cset* refused to move the kernel threads back to 'root'
945 because it says that they are "bound." Let's check this with the Linux
946 taskset command.
947
948 ----------------------------------------------------------
949 [zuul:cpuset-trunk]# cset proc -l -s two | head -5
950 cset: "two" cpuset of CPUSPEC(2) with 70 tasks running
951 USER PID PPID SPPr TASK NAME
952 -------- ----- ----- ---- ---------
953 root 2 0 Soth [kthreadd]
954 root 55 2 Soth [khelper]
955
956 [zuul:cpuset-trunk]# taskset -p 2
957 pid 2's current affinity mask: 4
958
959 [zuul:cpuset-trunk]# cset set -l -s two
960 cset:
961 Name CPUs-X MEMs-X Tasks Subs Path
962 ------------ ---------- - ------- - ----- ---- ----------
963 two 2 n 0 n 70 0 /two
964 ----------------------------------------------------------
965
966 Of course, since the cpuset named 'two' only has CPU2 assigned to it, once we
967 moved the unbound kernel threads from 'root' to 'two', their affinity masks
968 got automatically changed to only use CPU2. This is evident from the
969 +taskset+ output which is a hex value. To really move these threads back to
970 'root', we need to force the move as follows.
971
972 ----------------------------------------------------------
973 [zuul:cpuset-trunk]# cset proc -k -f two -t root --force
974 cset: moving all kernel threads from /two to /
975 cset: moving 70 kernel threads to: /
976 [==================================================]%
977 cset: done
978 ----------------------------------------------------------
979
980
981 Destroying Tasks
982 ~~~~~~~~~~~~~~~~
983 There actually is no *cset* subcommand or option to destroy tasks--it's not
984 really needed. Tasks exist and are accessible on the system as normal, even
985 if they happen to be running in one cpuset or another. To destroy tasks, use
986 the usual ^C method or by using the *kill(1)* command.
987
988
989 Implementing "Shielding" with Set and Proc
990 ------------------------------------------
991 With the preceding material on the +set+ and +proc+ subcommands, we now have
992 the background to implement the basic shielding model, just like the +shield+
993 subcommand.
994
995 One may pose the question as to why we want to do this, especially since
996 +shield+ already does it? The answer is that sometimes one needs more
997 functionality than +shield+ has to implement one's shielding strategy. In
998 those case you need to first stop using +shield+ since that subcommand will
999 interfere with the further application of +set+ and +proc+; however, you will
1000 still need to implement the functionality of +shield+ in order to implement
1001 successful shielding.
1002
1003 Remember from the above sections describing +shield+, that shielding has at
1004 minimum three cpusets: 'root', which is always present and contains all CPUs;
1005 'system' which is the "non-shielded" set of CPUs and runs unimportant system
1006 tasks; and 'user', which is the "shielded" set of CPUs and runs your important
1007 tasks. Remember also that +shield+ moves all movable tasks into 'system'
1008 and, optionally, moves unbound kernel threads into 'system' as well.
1009
1010 We start first by creating the 'system' and 'user' cpusets as follows. We
1011 assume that the machine is a four-CPU machine without NUMA memory features.
1012 The 'system' cpuset should hold only CPU0 while the 'user' cpuset should hold
1013 the rest of the CPUs.
1014
1015 ----------------------------------------------------------
1016 [zuul:cpuset-trunk]# cset set -c 0 -s system
1017 cset: --> created cpuset "system"
1018
1019 [zuul:cpuset-trunk]# cset set -c 1-3 -s user
1020 cset: --> created cpuset "user"
1021
1022 [zuul:cpuset-trunk]# cset set -l
1023 cset:
1024 Name CPUs-X MEMs-X Tasks Subs Path
1025 ------------ ---------- - ------- - ----- ---- ----------
1026 root 0-3 y 0 y 333 2 /
1027 user 1-3 n 0 n 0 0 /user
1028 system 0 n 0 n 0 0 /system
1029 ----------------------------------------------------------
1030
1031 Now, we need to move all running user processes into the 'system' cpuset.
1032
1033 ----------------------------------------------------------
1034 [zuul:cpuset-trunk]# cset proc -m -f root -t system
1035 cset: moving all tasks from root to /system
1036 cset: moving 188 userspace tasks to /system
1037 [==================================================]%
1038 cset: done
1039
1040 [zuul:cpuset-trunk]# cset set -l
1041 cset:
1042 Name CPUs-X MEMs-X Tasks Subs Path
1043 ------------ ---------- - ------- - ----- ---- ----------
1044 root 0-3 y 0 y 146 2 /
1045 user 1-3 n 0 n 0 0 /user
1046 system 0 n 0 n 187 0 /system
1047 ----------------------------------------------------------
1048
1049 We now have the basic shielding set up. Since all userspace tasks are running
1050 in 'system', anything that is spawned from them will also run in 'system'.
1051 The 'user' cpuset has nothing running in it unless you put tasks there with
1052 the +proc+ subcommand as described above. If you also want to move movable
1053 kernel threads from 'root' to 'system' (in order to achieve a form of
1054 "interrupt shielding" on a real time Linux kernel for example), you would
1055 execute the following command as well.
1056
1057 ----------------------------------------------------------
1058 [zuul:cpuset-trunk]# cset proc -k -f root -t system
1059 cset: moving all kernel threads from / to /system
1060 cset: moving 70 kernel threads to: /system
1061 cset: --> not moving 76 threads (not unbound, use --force)
1062 [==================================================]%
1063 cset: done
1064
1065 [zuul:cpuset-trunk]# cset set -l
1066 cset:
1067 Name CPUs-X MEMs-X Tasks Subs Path
1068 ------------ ---------- - ------- - ----- ---- ----------
1069 root 0-3 y 0 y 76 2 /
1070 user 1-3 n 0 n 0 0 /user
1071 system 0 n 0 n 257 0 /system
1072 ----------------------------------------------------------
1073
1074 At this point, you have achieved the simple shielding model that the +shield+
1075 subcommand provides. You can now add other cpuset definitions to expand your
1076 shielding strategy beyond that simple model.
1077
1078
1079 Implementing Hierarchy with Set and Proc
1080 ----------------------------------------
1081 One popular extended "shielding" model is based on hierarchical cpusets, each
1082 with diminishing numbers of CPUs. This model is used to create "priority
1083 cpusets" that allow assignment of CPU resources to tasks based on some
1084 arbitrary priority definition. The idea being that a higher priority task
1085 will get access to more CPU resources than a lower priority task.
1086
1087 The example provided here once again assumes a machine with four CPUs and no
1088 NUMA memory features. This base serves to illustrate the point well; however,
1089 note that if your machine has (many) more CPUs, then strategies such as this
1090 and others get more interesting.
1091
1092 We define a shielding set up as in the previous section where we have a
1093 'system' cpuset with just CPU0 that takes care of "unimportant" system tasks.
1094 One usually requires this type of cpuset since it forms the basis of
1095 shielding. We modify the strategy to not use a 'user' cpuset, instead we
1096 create a number of new cpusets each holding one more CPU than the other.
1097 These cpusets will be called 'prio_low' with one CPU, 'prio_med' with two
1098 CPUs, 'prio_high' with three CPUs, and 'prio_all' with all CPUs.
1099
1100 NOTE: One may ask, why create a 'prio_all' with all CPUs when that is
1101 substantially the definition of the 'root' cpuset? The answer is that it is
1102 best to keep a separation between the 'root' cpuset and everything else, even
1103 if a particular cpuset duplicates 'root' exactly. Usually, one builds
1104 automation on top of a cpuset strategy. In these cases, it is best to avoid
1105 using invariant names of cpusets, such as 'root' for example, in this
1106 automation.
1107
1108 All of these 'prio_*' cpusets can be created under root, in a flat way;
1109 however, it is advantageous to create them as a hierarchy. The reasoning for
1110 this is twofold: first, if a cpuset is destroyed, all its tasks are moved to
1111 its parent; second, one can use exclusive CPUs in a hierarchy.
1112
1113 There is a planned addition to the +proc+ subcommand that will allow moving a
1114 specified PIDSPEC of tasks running in a specified cpuset to its parent. This
1115 addition will ease the automation burden.
1116
1117 If a cpuset has CPUs that are exclusive to it, then other cpusets may not make
1118 use of those CPUs unless they are children of that cpuset. This has more
1119 relevance to machines with many CPUs and more complex strategies.
1120
1121 Now, we start with a clean slate and build the appropriate cpusets as
1122 follows.
1123
1124 ----------------------------------------------------------
1125 [zuul:cpuset-trunk]# cset set -r
1126 cset:
1127 Name CPUs-X MEMs-X Tasks Subs Path
1128 ------------ ---------- - ------- - ----- ---- ----------
1129 root 0-3 y 0 y 344 0 /
1130
1131 [zuul:cpuset-trunk]# cset set -c 0-3 prio_all
1132 cset: --> created cpuset "prio_all"
1133
1134 [zuul:cpuset-trunk]# cset set -c 1-3 /prio_all/prio_high
1135 cset: --> created cpuset "/prio_all/prio_high"
1136
1137 [zuul:cpuset-trunk]# cset set -c 2-3 /prio_all/prio_high/prio_med
1138 cset: --> created cpuset "/prio_all/prio_high/prio_med"
1139
1140 [zuul:cpuset-trunk]# cset set -c 3 /prio_all/prio_high/prio_med/prio_low
1141 cset: --> created cpuset "/prio_all/prio_high/prio_med/prio_low"
1142
1143 [zuul:cpuset-trunk]# cset set -c 0 system
1144 cset: --> created cpuset "system"
1145
1146 [zuul:cpuset-trunk]# cset set -l -r
1147 cset:
1148 Name CPUs-X MEMs-X Tasks Subs Path
1149 ------------ ---------- - ------- - ----- ---- ----------
1150 root 0-3 y 0 y 344 2 /
1151 system 0 n 0 n 0 0 /system
1152 prio_all 0-3 n 0 n 0 1 /prio_all
1153 prio_high 1-3 n 0 n 0 1 /prio_all/prio_high
1154 prio_med 2-3 n 0 n 0 1 /prio_all/prio_high/prio_med
1155 prio_low 3 n 0 n 0 0 /prio_all/pr...rio_med/prio_low
1156 ----------------------------------------------------------
1157
1158 NOTE: We used the +-r/\--recurse+ switch to list all the sets in the last
1159 command above. If we had not, then the 'prio_med' and 'prio_low' cpusets
1160 would not have been listed.
1161
1162 The strategy is now implemented and we now move all userspace tasks as well as
1163 all movable kernel threads into the 'system' cpuset to activate the shield.
1164
1165 ----------------------------------------------------------
1166 [zuul:cpuset-trunk]# cset proc -m -k -f root -t system
1167 cset: moving all tasks from root to /system
1168 cset: moving 198 userspace tasks to /system
1169 cset: moving 70 kernel threads to: /system
1170 cset: --> not moving 76 threads (not unbound, use --force)
1171 [==================================================]%
1172 cset: done
1173
1174 [zuul:cpuset-trunk]# cset set -l -r
1175 cset:
1176 Name CPUs-X MEMs-X Tasks Subs Path
1177 ------------ ---------- - ------- - ----- ---- ----------
1178 root 0-3 y 0 y 76 2 /
1179 system 0 n 0 n 268 0 /system
1180 prio_all 0-3 n 0 n 0 1 /prio_all
1181 prio_high 1-3 n 0 n 0 1 /prio_all/prio_high
1182 prio_med 2-3 n 0 n 0 1 /prio_all/prio_high/prio_med
1183 prio_low 3 n 0 n 0 0 /prio_all/pr...rio_med/prio_low
1184 ----------------------------------------------------------
1185
1186 The shield is now active. Since the 'prio_*' cpuset names are unique, one can
1187 assign tasks to them either via either their simple name, or their full path
1188 (as described in the +proc+ section above).
1189
1190 You may have noted that there is an ellipsis in the path of the 'prio_low'
1191 cpuset in the listing above. This is done in order to fit the output onto an
1192 80 character screen. If you want to see the entire line, then you need to use
1193 the +-v/\--verbose+ flag as follows.
1194
1195 ----------------------------------------------------------
1196 [zuul:cpuset-trunk]# cset set -l -r -v
1197 cset:
1198 Name CPUs-X MEMs-X Tasks Subs Path
1199 ------------ ---------- - ------- - ----- ---- ----------
1200 root 0-3 y 0 y 76 2 /
1201 system 0 n 0 n 268 0 /system
1202 prio_all 0-3 n 0 n 0 1 /prio_all
1203 prio_high 1-3 n 0 n 0 1 /prio_all/prio_high
1204 prio_med 2-3 n 0 n 0 1 /prio_all/prio_high/prio_med
1205 prio_low 3 n 0 n 0 0 /prio_all/prio_high/prio_med/prio_low
1206 ----------------------------------------------------------
1207
1208
1209 Using Shortcuts
1210 ===============
1211 The commands listed in the previous sections always used all the required
1212 options. *Cset* however does have a shortcut facility that will execute
1213 certain commands without specifying all options. An effort has been made to
1214 do this with the "principle of least surprise." This means that if you do not
1215 specify options, but do specify parameters, then the outcome of the command
1216 should be intuitive. As much as possible.
1217
1218 Using shortcuts is of course not necessary. In fact, you can not only not use
1219 shortcuts, but you can use long options instead of short, in case you really
1220 enjoy typing... All kidding aside, using long options and not using shortcuts
1221 does have a use case: when you write a script intended to be self-documenting,
1222 or perhaps when you generate *cset* documentation.
1223
1224 To begin, the subcommands +shield+, +set+ and +proc+ can themselves be
1225 shortened to the fewest number of characters that are unambiguous. For
1226 example, the following commands are identical:
1227
1228 ----------------------------------------------------------
1229 # cset shield -s -p 1234 <--> # cset sh -s -p 1234
1230 # cset set -c 1,3 -s newset <--> # cset se -c 1,3 -s newset
1231 # cset proc -s newset -e bash <--> # cset p -s newset -e bash
1232 ----------------------------------------------------------
1233
1234 Note that +proc+ can be shortened to just +p+, while +shield+ and +set+ need
1235 two letters to disambiguate.
1236
1237
1238 Shield Subcommand Shortcuts
1239 ---------------------------
1240 The +shield+ subcommand supports two areas with shortcuts: the case when there
1241 is no options given where to 'shield' is the common use case, and making the
1242 +-p/\--pid+ option 'optional' for the +-s/\--shield+ and +-u/\--unshield+
1243 options.
1244
1245 For the common use case of actually 'shielding' either a PIDSPEC or execing a
1246 command into the shield, the following *cset* commands are equivalent.
1247
1248 -----------------------------------------------------------
1249 # cset shield -s -p 1234,500-649 <--> # cset sh 1234,500-649
1250 # cset shield -s -e bash <--> # cset sh bash
1251 -----------------------------------------------------------
1252
1253 When using the +-s+ or +-u+ shield/unshield options, it is optional to use the
1254 +-p+ option to specify a PIDSPEC. For example:
1255
1256 -----------------------------------------------------------
1257 # cset shield -s -p 1234 <--> # cset sh -s 1234
1258 # cset shield -u -p 1234 <--> # cset sh -u 1234
1259 -----------------------------------------------------------
1260
1261
1262 Set Subcommand Shortcuts
1263 ------------------------
1264 The +set+ subcommand has a limited number of shortcuts. Basically, the
1265 +-s/\--set+ option is optional in most cases and the +-l/\--list+ option is
1266 also optional if you want to list sets. For example, these commands are
1267 equivalent.
1268
1269 -----------------------------------------------------------
1270 # cset set -l -s myset <--> # cset se -l myset
1271 # cset se -l myset <--> # cset se myset
1272
1273 # cset set -c 1,2,3 -s newset <--> # cset se -c 1,2,3 newset
1274 # cset set -d -s newset <--> # cset se -d newset
1275
1276 # cset set -n newname -s oldname <--> # cset se -n newname oldname
1277 -----------------------------------------------------------
1278
1279 In fact, if you want to apply either the list or the destroy options to
1280 multiple cpusets with one *cset* command, you'll need to not use the +-s+
1281 option. For example:
1282
1283 -----------------------------------------------------------
1284 # cset se -d myset yourset ourset
1285 --> destroys cpusets: myset, yourset and ourset
1286
1287 # cset se -l prio_high prio_med prio_low
1288 --> lists only cpusets prio_high, prio_med and prio_low
1289 --> the -l is optional in this case since list is default
1290 -----------------------------------------------------------
1291
1292 Proc Subcommand Shortcuts
1293 -------------------------
1294 For the +proc+ subcommand, the +-s+, +-t+ and +-f+ options to specify the
1295 cpuset, the origination cpuset and the destination cpuset, can sometimes be
1296 optional. For example, the following commands are equivalent.
1297
1298 -----------------------------------------------------------
1299 To list tasks in cpusets:
1300 # cset proc -l -s myset \
1301 # cset proc -l -f myset --> # cset p -l myset
1302 # cset proc -l -t myset /
1303
1304 # cset p -l myset <--> # cset p myset
1305
1306 # cset proc -l -s one two <--> # cset p -l one two
1307 # cset p -l one two <--> # cset p one two
1308
1309 To exec a process into a cpuset:
1310 # cset proc -s myset -e bash <--> # cset p myset -e bash
1311 -----------------------------------------------------------
1312
1313 Movement of tasks into and out of cpusets have the following shortcuts.
1314
1315 -----------------------------------------------------------
1316 To move a PIDSPEC into a cpuset:
1317 # cset proc -m -p 4242,4243 -s myset <--> # cset p -m 4242,4243 myset
1318 # cset proc -m -p 12 -t myset <--> # cset p -m 12 myset
1319
1320 To move all tasks from one cpuset to another:
1321 # cset proc -m -f set1 -t set2 \
1322 # cset proc -m -s set1 -t set2 --> # cset p -m set1 set2
1323 # cset proc -m -f set1 -s set2 /
1324 -----------------------------------------------------------
1325
1326
1327 What To Do if There are Problems
1328 ================================
1329 If you encounter problems with the *cset* application, the best option is to
1330 log a bug with the *cset* bugzilla instance found here:
1331
1332 https://github.com/SUSE/cpuset/issues
1333
1334 If you are using *cset* on a supported operating system such as SLES or SLERT
1335 from Novell, then please use that bugzilla instead at:
1336
1337 https://bugzilla.suse.com
1338
1339 If the problem is repeatable, there is an excellent chance that it will get
1340 fixed quickly. Also, *cset* contains a logging facility that is invaluable
1341 for the developers to diagnose problems. To create a log of a run, use the
1342 +-l/\--log+ option with a filename as an argument to the main *cset*
1343 application. For example.
1344
1345 -------------------------------------------------------------
1346 # cset -l logfile.txt set -n newname oldname
1347 -------------------------------------------------------------
1348
1349 That command saves a lot of debugging information in the 'logfile.txt' file.
1350 Please attach this file to the bug.