Path: blob/master/Documentation/cgroups/cpusets.txt
10821 views
CPUSETS1-------23Copyright (C) 2004 BULL SA.4Written by [email protected]56Portions Copyright (c) 2004-2006 Silicon Graphics, Inc.7Modified by Paul Jackson <[email protected]>8Modified by Christoph Lameter <[email protected]>9Modified by Paul Menage <[email protected]>10Modified by Hidetoshi Seto <[email protected]>1112CONTENTS:13=========14151. Cpusets161.1 What are cpusets ?171.2 Why are cpusets needed ?181.3 How are cpusets implemented ?191.4 What are exclusive cpusets ?201.5 What is memory_pressure ?211.6 What is memory spread ?221.7 What is sched_load_balance ?231.8 What is sched_relax_domain_level ?241.9 How do I use cpusets ?252. Usage Examples and Syntax262.1 Basic Usage272.2 Adding/removing cpus282.3 Setting flags292.4 Attaching processes303. Questions314. Contact32331. Cpusets34==========35361.1 What are cpusets ?37----------------------3839Cpusets provide a mechanism for assigning a set of CPUs and Memory40Nodes to a set of tasks. In this document "Memory Node" refers to41an on-line node that contains memory.4243Cpusets constrain the CPU and Memory placement of tasks to only44the resources within a task's current cpuset. They form a nested45hierarchy visible in a virtual file system. These are the essential46hooks, beyond what is already present, required to manage dynamic47job placement on large systems.4849Cpusets use the generic cgroup subsystem described in50Documentation/cgroups/cgroups.txt.5152Requests by a task, using the sched_setaffinity(2) system call to53include CPUs in its CPU affinity mask, and using the mbind(2) and54set_mempolicy(2) system calls to include Memory Nodes in its memory55policy, are both filtered through that task's cpuset, filtering out any56CPUs or Memory Nodes not in that cpuset. The scheduler will not57schedule a task on a CPU that is not allowed in its cpus_allowed58vector, and the kernel page allocator will not allocate a page on a59node that is not allowed in the requesting task's mems_allowed vector.6061User level code may create and destroy cpusets by name in the cgroup62virtual file system, manage the attributes and permissions of these63cpusets and which CPUs and Memory Nodes are assigned to each cpuset,64specify and query to which cpuset a task is assigned, and list the65task pids assigned to a cpuset.6667681.2 Why are cpusets needed ?69----------------------------7071The management of large computer systems, with many processors (CPUs),72complex memory cache hierarchies and multiple Memory Nodes having73non-uniform access times (NUMA) presents additional challenges for74the efficient scheduling and memory placement of processes.7576Frequently more modest sized systems can be operated with adequate77efficiency just by letting the operating system automatically share78the available CPU and Memory resources amongst the requesting tasks.7980But larger systems, which benefit more from careful processor and81memory placement to reduce memory access times and contention,82and which typically represent a larger investment for the customer,83can benefit from explicitly placing jobs on properly sized subsets of84the system.8586This can be especially valuable on:8788* Web Servers running multiple instances of the same web application,89* Servers running different applications (for instance, a web server90and a database), or91* NUMA systems running large HPC applications with demanding92performance characteristics.9394These subsets, or "soft partitions" must be able to be dynamically95adjusted, as the job mix changes, without impacting other concurrently96executing jobs. The location of the running jobs pages may also be moved97when the memory locations are changed.9899The kernel cpuset patch provides the minimum essential kernel100mechanisms required to efficiently implement such subsets. It101leverages existing CPU and Memory Placement facilities in the Linux102kernel to avoid any additional impact on the critical scheduler or103memory allocator code.1041051061.3 How are cpusets implemented ?107---------------------------------108109Cpusets provide a Linux kernel mechanism to constrain which CPUs and110Memory Nodes are used by a process or set of processes.111112The Linux kernel already has a pair of mechanisms to specify on which113CPUs a task may be scheduled (sched_setaffinity) and on which Memory114Nodes it may obtain memory (mbind, set_mempolicy).115116Cpusets extends these two mechanisms as follows:117118- Cpusets are sets of allowed CPUs and Memory Nodes, known to the119kernel.120- Each task in the system is attached to a cpuset, via a pointer121in the task structure to a reference counted cgroup structure.122- Calls to sched_setaffinity are filtered to just those CPUs123allowed in that task's cpuset.124- Calls to mbind and set_mempolicy are filtered to just125those Memory Nodes allowed in that task's cpuset.126- The root cpuset contains all the systems CPUs and Memory127Nodes.128- For any cpuset, one can define child cpusets containing a subset129of the parents CPU and Memory Node resources.130- The hierarchy of cpusets can be mounted at /dev/cpuset, for131browsing and manipulation from user space.132- A cpuset may be marked exclusive, which ensures that no other133cpuset (except direct ancestors and descendants) may contain134any overlapping CPUs or Memory Nodes.135- You can list all the tasks (by pid) attached to any cpuset.136137The implementation of cpusets requires a few, simple hooks138into the rest of the kernel, none in performance critical paths:139140- in init/main.c, to initialize the root cpuset at system boot.141- in fork and exit, to attach and detach a task from its cpuset.142- in sched_setaffinity, to mask the requested CPUs by what's143allowed in that task's cpuset.144- in sched.c migrate_live_tasks(), to keep migrating tasks within145the CPUs allowed by their cpuset, if possible.146- in the mbind and set_mempolicy system calls, to mask the requested147Memory Nodes by what's allowed in that task's cpuset.148- in page_alloc.c, to restrict memory to allowed nodes.149- in vmscan.c, to restrict page recovery to the current cpuset.150151You should mount the "cgroup" filesystem type in order to enable152browsing and modifying the cpusets presently known to the kernel. No153new system calls are added for cpusets - all support for querying and154modifying cpusets is via this cpuset file system.155156The /proc/<pid>/status file for each task has four added lines,157displaying the task's cpus_allowed (on which CPUs it may be scheduled)158and mems_allowed (on which Memory Nodes it may obtain memory),159in the two formats seen in the following example:160161Cpus_allowed: ffffffff,ffffffff,ffffffff,ffffffff162Cpus_allowed_list: 0-127163Mems_allowed: ffffffff,ffffffff164Mems_allowed_list: 0-63165166Each cpuset is represented by a directory in the cgroup file system167containing (on top of the standard cgroup files) the following168files describing that cpuset:169170- cpuset.cpus: list of CPUs in that cpuset171- cpuset.mems: list of Memory Nodes in that cpuset172- cpuset.memory_migrate flag: if set, move pages to cpusets nodes173- cpuset.cpu_exclusive flag: is cpu placement exclusive?174- cpuset.mem_exclusive flag: is memory placement exclusive?175- cpuset.mem_hardwall flag: is memory allocation hardwalled176- cpuset.memory_pressure: measure of how much paging pressure in cpuset177- cpuset.memory_spread_page flag: if set, spread page cache evenly on allowed nodes178- cpuset.memory_spread_slab flag: if set, spread slab cache evenly on allowed nodes179- cpuset.sched_load_balance flag: if set, load balance within CPUs on that cpuset180- cpuset.sched_relax_domain_level: the searching range when migrating tasks181182In addition, the root cpuset only has the following file:183- cpuset.memory_pressure_enabled flag: compute memory_pressure?184185New cpusets are created using the mkdir system call or shell186command. The properties of a cpuset, such as its flags, allowed187CPUs and Memory Nodes, and attached tasks, are modified by writing188to the appropriate file in that cpusets directory, as listed above.189190The named hierarchical structure of nested cpusets allows partitioning191a large system into nested, dynamically changeable, "soft-partitions".192193The attachment of each task, automatically inherited at fork by any194children of that task, to a cpuset allows organizing the work load195on a system into related sets of tasks such that each set is constrained196to using the CPUs and Memory Nodes of a particular cpuset. A task197may be re-attached to any other cpuset, if allowed by the permissions198on the necessary cpuset file system directories.199200Such management of a system "in the large" integrates smoothly with201the detailed placement done on individual tasks and memory regions202using the sched_setaffinity, mbind and set_mempolicy system calls.203204The following rules apply to each cpuset:205206- Its CPUs and Memory Nodes must be a subset of its parents.207- It can't be marked exclusive unless its parent is.208- If its cpu or memory is exclusive, they may not overlap any sibling.209210These rules, and the natural hierarchy of cpusets, enable efficient211enforcement of the exclusive guarantee, without having to scan all212cpusets every time any of them change to ensure nothing overlaps a213exclusive cpuset. Also, the use of a Linux virtual file system (vfs)214to represent the cpuset hierarchy provides for a familiar permission215and name space for cpusets, with a minimum of additional kernel code.216217The cpus and mems files in the root (top_cpuset) cpuset are218read-only. The cpus file automatically tracks the value of219cpu_online_map using a CPU hotplug notifier, and the mems file220automatically tracks the value of node_states[N_HIGH_MEMORY]--i.e.,221nodes with memory--using the cpuset_track_online_nodes() hook.2222232241.4 What are exclusive cpusets ?225--------------------------------226227If a cpuset is cpu or mem exclusive, no other cpuset, other than228a direct ancestor or descendant, may share any of the same CPUs or229Memory Nodes.230231A cpuset that is cpuset.mem_exclusive *or* cpuset.mem_hardwall is "hardwalled",232i.e. it restricts kernel allocations for page, buffer and other data233commonly shared by the kernel across multiple users. All cpusets,234whether hardwalled or not, restrict allocations of memory for user235space. This enables configuring a system so that several independent236jobs can share common kernel data, such as file system pages, while237isolating each job's user allocation in its own cpuset. To do this,238construct a large mem_exclusive cpuset to hold all the jobs, and239construct child, non-mem_exclusive cpusets for each individual job.240Only a small amount of typical kernel memory, such as requests from241interrupt handlers, is allowed to be taken outside even a242mem_exclusive cpuset.2432442451.5 What is memory_pressure ?246-----------------------------247The memory_pressure of a cpuset provides a simple per-cpuset metric248of the rate that the tasks in a cpuset are attempting to free up in249use memory on the nodes of the cpuset to satisfy additional memory250requests.251252This enables batch managers monitoring jobs running in dedicated253cpusets to efficiently detect what level of memory pressure that job254is causing.255256This is useful both on tightly managed systems running a wide mix of257submitted jobs, which may choose to terminate or re-prioritize jobs that258are trying to use more memory than allowed on the nodes assigned to them,259and with tightly coupled, long running, massively parallel scientific260computing jobs that will dramatically fail to meet required performance261goals if they start to use more memory than allowed to them.262263This mechanism provides a very economical way for the batch manager264to monitor a cpuset for signs of memory pressure. It's up to the265batch manager or other user code to decide what to do about it and266take action.267268==> Unless this feature is enabled by writing "1" to the special file269/dev/cpuset/memory_pressure_enabled, the hook in the rebalance270code of __alloc_pages() for this metric reduces to simply noticing271that the cpuset_memory_pressure_enabled flag is zero. So only272systems that enable this feature will compute the metric.273274Why a per-cpuset, running average:275276Because this meter is per-cpuset, rather than per-task or mm,277the system load imposed by a batch scheduler monitoring this278metric is sharply reduced on large systems, because a scan of279the tasklist can be avoided on each set of queries.280281Because this meter is a running average, instead of an accumulating282counter, a batch scheduler can detect memory pressure with a283single read, instead of having to read and accumulate results284for a period of time.285286Because this meter is per-cpuset rather than per-task or mm,287the batch scheduler can obtain the key information, memory288pressure in a cpuset, with a single read, rather than having to289query and accumulate results over all the (dynamically changing)290set of tasks in the cpuset.291292A per-cpuset simple digital filter (requires a spinlock and 3 words293of data per-cpuset) is kept, and updated by any task attached to that294cpuset, if it enters the synchronous (direct) page reclaim code.295296A per-cpuset file provides an integer number representing the recent297(half-life of 10 seconds) rate of direct page reclaims caused by298the tasks in the cpuset, in units of reclaims attempted per second,299times 1000.3003013021.6 What is memory spread ?303---------------------------304There are two boolean flag files per cpuset that control where the305kernel allocates pages for the file system buffers and related in306kernel data structures. They are called 'cpuset.memory_spread_page' and307'cpuset.memory_spread_slab'.308309If the per-cpuset boolean flag file 'cpuset.memory_spread_page' is set, then310the kernel will spread the file system buffers (page cache) evenly311over all the nodes that the faulting task is allowed to use, instead312of preferring to put those pages on the node where the task is running.313314If the per-cpuset boolean flag file 'cpuset.memory_spread_slab' is set,315then the kernel will spread some file system related slab caches,316such as for inodes and dentries evenly over all the nodes that the317faulting task is allowed to use, instead of preferring to put those318pages on the node where the task is running.319320The setting of these flags does not affect anonymous data segment or321stack segment pages of a task.322323By default, both kinds of memory spreading are off, and memory324pages are allocated on the node local to where the task is running,325except perhaps as modified by the task's NUMA mempolicy or cpuset326configuration, so long as sufficient free memory pages are available.327328When new cpusets are created, they inherit the memory spread settings329of their parent.330331Setting memory spreading causes allocations for the affected page332or slab caches to ignore the task's NUMA mempolicy and be spread333instead. Tasks using mbind() or set_mempolicy() calls to set NUMA334mempolicies will not notice any change in these calls as a result of335their containing task's memory spread settings. If memory spreading336is turned off, then the currently specified NUMA mempolicy once again337applies to memory page allocations.338339Both 'cpuset.memory_spread_page' and 'cpuset.memory_spread_slab' are boolean flag340files. By default they contain "0", meaning that the feature is off341for that cpuset. If a "1" is written to that file, then that turns342the named feature on.343344The implementation is simple.345346Setting the flag 'cpuset.memory_spread_page' turns on a per-process flag347PF_SPREAD_PAGE for each task that is in that cpuset or subsequently348joins that cpuset. The page allocation calls for the page cache349is modified to perform an inline check for this PF_SPREAD_PAGE task350flag, and if set, a call to a new routine cpuset_mem_spread_node()351returns the node to prefer for the allocation.352353Similarly, setting 'cpuset.memory_spread_slab' turns on the flag354PF_SPREAD_SLAB, and appropriately marked slab caches will allocate355pages from the node returned by cpuset_mem_spread_node().356357The cpuset_mem_spread_node() routine is also simple. It uses the358value of a per-task rotor cpuset_mem_spread_rotor to select the next359node in the current task's mems_allowed to prefer for the allocation.360361This memory placement policy is also known (in other contexts) as362round-robin or interleave.363364This policy can provide substantial improvements for jobs that need365to place thread local data on the corresponding node, but that need366to access large file system data sets that need to be spread across367the several nodes in the jobs cpuset in order to fit. Without this368policy, especially for jobs that might have one thread reading in the369data set, the memory allocation across the nodes in the jobs cpuset370can become very uneven.3713721.7 What is sched_load_balance ?373--------------------------------374375The kernel scheduler (kernel/sched.c) automatically load balances376tasks. If one CPU is underutilized, kernel code running on that377CPU will look for tasks on other more overloaded CPUs and move those378tasks to itself, within the constraints of such placement mechanisms379as cpusets and sched_setaffinity.380381The algorithmic cost of load balancing and its impact on key shared382kernel data structures such as the task list increases more than383linearly with the number of CPUs being balanced. So the scheduler384has support to partition the systems CPUs into a number of sched385domains such that it only load balances within each sched domain.386Each sched domain covers some subset of the CPUs in the system;387no two sched domains overlap; some CPUs might not be in any sched388domain and hence won't be load balanced.389390Put simply, it costs less to balance between two smaller sched domains391than one big one, but doing so means that overloads in one of the392two domains won't be load balanced to the other one.393394By default, there is one sched domain covering all CPUs, except those395marked isolated using the kernel boot time "isolcpus=" argument.396397This default load balancing across all CPUs is not well suited for398the following two situations:3991) On large systems, load balancing across many CPUs is expensive.400If the system is managed using cpusets to place independent jobs401on separate sets of CPUs, full load balancing is unnecessary.4022) Systems supporting realtime on some CPUs need to minimize403system overhead on those CPUs, including avoiding task load404balancing if that is not needed.405406When the per-cpuset flag "cpuset.sched_load_balance" is enabled (the default407setting), it requests that all the CPUs in that cpusets allowed 'cpuset.cpus'408be contained in a single sched domain, ensuring that load balancing409can move a task (not otherwised pinned, as by sched_setaffinity)410from any CPU in that cpuset to any other.411412When the per-cpuset flag "cpuset.sched_load_balance" is disabled, then the413scheduler will avoid load balancing across the CPUs in that cpuset,414--except-- in so far as is necessary because some overlapping cpuset415has "sched_load_balance" enabled.416417So, for example, if the top cpuset has the flag "cpuset.sched_load_balance"418enabled, then the scheduler will have one sched domain covering all419CPUs, and the setting of the "cpuset.sched_load_balance" flag in any other420cpusets won't matter, as we're already fully load balancing.421422Therefore in the above two situations, the top cpuset flag423"cpuset.sched_load_balance" should be disabled, and only some of the smaller,424child cpusets have this flag enabled.425426When doing this, you don't usually want to leave any unpinned tasks in427the top cpuset that might use non-trivial amounts of CPU, as such tasks428may be artificially constrained to some subset of CPUs, depending on429the particulars of this flag setting in descendant cpusets. Even if430such a task could use spare CPU cycles in some other CPUs, the kernel431scheduler might not consider the possibility of load balancing that432task to that underused CPU.433434Of course, tasks pinned to a particular CPU can be left in a cpuset435that disables "cpuset.sched_load_balance" as those tasks aren't going anywhere436else anyway.437438There is an impedance mismatch here, between cpusets and sched domains.439Cpusets are hierarchical and nest. Sched domains are flat; they don't440overlap and each CPU is in at most one sched domain.441442It is necessary for sched domains to be flat because load balancing443across partially overlapping sets of CPUs would risk unstable dynamics444that would be beyond our understanding. So if each of two partially445overlapping cpusets enables the flag 'cpuset.sched_load_balance', then we446form a single sched domain that is a superset of both. We won't move447a task to a CPU outside it cpuset, but the scheduler load balancing448code might waste some compute cycles considering that possibility.449450This mismatch is why there is not a simple one-to-one relation451between which cpusets have the flag "cpuset.sched_load_balance" enabled,452and the sched domain configuration. If a cpuset enables the flag, it453will get balancing across all its CPUs, but if it disables the flag,454it will only be assured of no load balancing if no other overlapping455cpuset enables the flag.456457If two cpusets have partially overlapping 'cpuset.cpus' allowed, and only458one of them has this flag enabled, then the other may find its459tasks only partially load balanced, just on the overlapping CPUs.460This is just the general case of the top_cpuset example given a few461paragraphs above. In the general case, as in the top cpuset case,462don't leave tasks that might use non-trivial amounts of CPU in463such partially load balanced cpusets, as they may be artificially464constrained to some subset of the CPUs allowed to them, for lack of465load balancing to the other CPUs.4664671.7.1 sched_load_balance implementation details.468------------------------------------------------469470The per-cpuset flag 'cpuset.sched_load_balance' defaults to enabled (contrary471to most cpuset flags.) When enabled for a cpuset, the kernel will472ensure that it can load balance across all the CPUs in that cpuset473(makes sure that all the CPUs in the cpus_allowed of that cpuset are474in the same sched domain.)475476If two overlapping cpusets both have 'cpuset.sched_load_balance' enabled,477then they will be (must be) both in the same sched domain.478479If, as is the default, the top cpuset has 'cpuset.sched_load_balance' enabled,480then by the above that means there is a single sched domain covering481the whole system, regardless of any other cpuset settings.482483The kernel commits to user space that it will avoid load balancing484where it can. It will pick as fine a granularity partition of sched485domains as it can while still providing load balancing for any set486of CPUs allowed to a cpuset having 'cpuset.sched_load_balance' enabled.487488The internal kernel cpuset to scheduler interface passes from the489cpuset code to the scheduler code a partition of the load balanced490CPUs in the system. This partition is a set of subsets (represented491as an array of struct cpumask) of CPUs, pairwise disjoint, that cover492all the CPUs that must be load balanced.493494The cpuset code builds a new such partition and passes it to the495scheduler sched domain setup code, to have the sched domains rebuilt496as necessary, whenever:497- the 'cpuset.sched_load_balance' flag of a cpuset with non-empty CPUs changes,498- or CPUs come or go from a cpuset with this flag enabled,499- or 'cpuset.sched_relax_domain_level' value of a cpuset with non-empty CPUs500and with this flag enabled changes,501- or a cpuset with non-empty CPUs and with this flag enabled is removed,502- or a cpu is offlined/onlined.503504This partition exactly defines what sched domains the scheduler should505setup - one sched domain for each element (struct cpumask) in the506partition.507508The scheduler remembers the currently active sched domain partitions.509When the scheduler routine partition_sched_domains() is invoked from510the cpuset code to update these sched domains, it compares the new511partition requested with the current, and updates its sched domains,512removing the old and adding the new, for each change.5135145151.8 What is sched_relax_domain_level ?516--------------------------------------517518In sched domain, the scheduler migrates tasks in 2 ways; periodic load519balance on tick, and at time of some schedule events.520521When a task is woken up, scheduler try to move the task on idle CPU.522For example, if a task A running on CPU X activates another task B523on the same CPU X, and if CPU Y is X's sibling and performing idle,524then scheduler migrate task B to CPU Y so that task B can start on525CPU Y without waiting task A on CPU X.526527And if a CPU run out of tasks in its runqueue, the CPU try to pull528extra tasks from other busy CPUs to help them before it is going to529be idle.530531Of course it takes some searching cost to find movable tasks and/or532idle CPUs, the scheduler might not search all CPUs in the domain533every time. In fact, in some architectures, the searching ranges on534events are limited in the same socket or node where the CPU locates,535while the load balance on tick searches all.536537For example, assume CPU Z is relatively far from CPU X. Even if CPU Z538is idle while CPU X and the siblings are busy, scheduler can't migrate539woken task B from X to Z since it is out of its searching range.540As the result, task B on CPU X need to wait task A or wait load balance541on the next tick. For some applications in special situation, waiting5421 tick may be too long.543544The 'cpuset.sched_relax_domain_level' file allows you to request changing545this searching range as you like. This file takes int value which546indicates size of searching range in levels ideally as follows,547otherwise initial value -1 that indicates the cpuset has no request.548549-1 : no request. use system default or follow request of others.5500 : no search.5511 : search siblings (hyperthreads in a core).5522 : search cores in a package.5533 : search cpus in a node [= system wide on non-NUMA system]554( 4 : search nodes in a chunk of node [on NUMA system] )555( 5 : search system wide [on NUMA system] )556557The system default is architecture dependent. The system default558can be changed using the relax_domain_level= boot parameter.559560This file is per-cpuset and affect the sched domain where the cpuset561belongs to. Therefore if the flag 'cpuset.sched_load_balance' of a cpuset562is disabled, then 'cpuset.sched_relax_domain_level' have no effect since563there is no sched domain belonging the cpuset.564565If multiple cpusets are overlapping and hence they form a single sched566domain, the largest value among those is used. Be careful, if one567requests 0 and others are -1 then 0 is used.568569Note that modifying this file will have both good and bad effects,570and whether it is acceptable or not depends on your situation.571Don't modify this file if you are not sure.572573If your situation is:574- The migration costs between each cpu can be assumed considerably575small(for you) due to your special application's behavior or576special hardware support for CPU cache etc.577- The searching cost doesn't have impact(for you) or you can make578the searching cost enough small by managing cpuset to compact etc.579- The latency is required even it sacrifices cache hit rate etc.580then increasing 'sched_relax_domain_level' would benefit you.5815825831.9 How do I use cpusets ?584--------------------------585586In order to minimize the impact of cpusets on critical kernel587code, such as the scheduler, and due to the fact that the kernel588does not support one task updating the memory placement of another589task directly, the impact on a task of changing its cpuset CPU590or Memory Node placement, or of changing to which cpuset a task591is attached, is subtle.592593If a cpuset has its Memory Nodes modified, then for each task attached594to that cpuset, the next time that the kernel attempts to allocate595a page of memory for that task, the kernel will notice the change596in the task's cpuset, and update its per-task memory placement to597remain within the new cpusets memory placement. If the task was using598mempolicy MPOL_BIND, and the nodes to which it was bound overlap with599its new cpuset, then the task will continue to use whatever subset600of MPOL_BIND nodes are still allowed in the new cpuset. If the task601was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed602in the new cpuset, then the task will be essentially treated as if it603was MPOL_BIND bound to the new cpuset (even though its NUMA placement,604as queried by get_mempolicy(), doesn't change). If a task is moved605from one cpuset to another, then the kernel will adjust the task's606memory placement, as above, the next time that the kernel attempts607to allocate a page of memory for that task.608609If a cpuset has its 'cpuset.cpus' modified, then each task in that cpuset610will have its allowed CPU placement changed immediately. Similarly,611if a task's pid is written to another cpusets 'cpuset.tasks' file, then its612allowed CPU placement is changed immediately. If such a task had been613bound to some subset of its cpuset using the sched_setaffinity() call,614the task will be allowed to run on any CPU allowed in its new cpuset,615negating the effect of the prior sched_setaffinity() call.616617In summary, the memory placement of a task whose cpuset is changed is618updated by the kernel, on the next allocation of a page for that task,619and the processor placement is updated immediately.620621Normally, once a page is allocated (given a physical page622of main memory) then that page stays on whatever node it623was allocated, so long as it remains allocated, even if the624cpusets memory placement policy 'cpuset.mems' subsequently changes.625If the cpuset flag file 'cpuset.memory_migrate' is set true, then when626tasks are attached to that cpuset, any pages that task had627allocated to it on nodes in its previous cpuset are migrated628to the task's new cpuset. The relative placement of the page within629the cpuset is preserved during these migration operations if possible.630For example if the page was on the second valid node of the prior cpuset631then the page will be placed on the second valid node of the new cpuset.632633Also if 'cpuset.memory_migrate' is set true, then if that cpuset's634'cpuset.mems' file is modified, pages allocated to tasks in that635cpuset, that were on nodes in the previous setting of 'cpuset.mems',636will be moved to nodes in the new setting of 'mems.'637Pages that were not in the task's prior cpuset, or in the cpuset's638prior 'cpuset.mems' setting, will not be moved.639640There is an exception to the above. If hotplug functionality is used641to remove all the CPUs that are currently assigned to a cpuset,642then all the tasks in that cpuset will be moved to the nearest ancestor643with non-empty cpus. But the moving of some (or all) tasks might fail if644cpuset is bound with another cgroup subsystem which has some restrictions645on task attaching. In this failing case, those tasks will stay646in the original cpuset, and the kernel will automatically update647their cpus_allowed to allow all online CPUs. When memory hotplug648functionality for removing Memory Nodes is available, a similar exception649is expected to apply there as well. In general, the kernel prefers to650violate cpuset placement, over starving a task that has had all651its allowed CPUs or Memory Nodes taken offline.652653There is a second exception to the above. GFP_ATOMIC requests are654kernel internal allocations that must be satisfied, immediately.655The kernel may drop some request, in rare cases even panic, if a656GFP_ATOMIC alloc fails. If the request cannot be satisfied within657the current task's cpuset, then we relax the cpuset, and look for658memory anywhere we can find it. It's better to violate the cpuset659than stress the kernel.660661To start a new job that is to be contained within a cpuset, the steps are:6626631) mkdir /sys/fs/cgroup/cpuset6642) mount -t cgroup -ocpuset cpuset /sys/fs/cgroup/cpuset6653) Create the new cpuset by doing mkdir's and write's (or echo's) in666the /sys/fs/cgroup/cpuset virtual file system.6674) Start a task that will be the "founding father" of the new job.6685) Attach that task to the new cpuset by writing its pid to the669/sys/fs/cgroup/cpuset tasks file for that cpuset.6706) fork, exec or clone the job tasks from this founding father task.671672For example, the following sequence of commands will setup a cpuset673named "Charlie", containing just CPUs 2 and 3, and Memory Node 1,674and then start a subshell 'sh' in that cpuset:675676mount -t cgroup -ocpuset cpuset /sys/fs/cgroup/cpuset677cd /sys/fs/cgroup/cpuset678mkdir Charlie679cd Charlie680/bin/echo 2-3 > cpuset.cpus681/bin/echo 1 > cpuset.mems682/bin/echo $$ > tasks683sh684# The subshell 'sh' is now running in cpuset Charlie685# The next line should display '/Charlie'686cat /proc/self/cpuset687688There are ways to query or modify cpusets:689- via the cpuset file system directly, using the various cd, mkdir, echo,690cat, rmdir commands from the shell, or their equivalent from C.691- via the C library libcpuset.692- via the C library libcgroup.693(http://sourceforge.net/projects/libcg/)694- via the python application cset.695(http://code.google.com/p/cpuset/)696697The sched_setaffinity calls can also be done at the shell prompt using698SGI's runon or Robert Love's taskset. The mbind and set_mempolicy699calls can be done at the shell prompt using the numactl command700(part of Andi Kleen's numa package).7017022. Usage Examples and Syntax703============================7047052.1 Basic Usage706---------------707708Creating, modifying, using the cpusets can be done through the cpuset709virtual filesystem.710711To mount it, type:712# mount -t cgroup -o cpuset cpuset /sys/fs/cgroup/cpuset713714Then under /sys/fs/cgroup/cpuset you can find a tree that corresponds to the715tree of the cpusets in the system. For instance, /sys/fs/cgroup/cpuset716is the cpuset that holds the whole system.717718If you want to create a new cpuset under /sys/fs/cgroup/cpuset:719# cd /sys/fs/cgroup/cpuset720# mkdir my_cpuset721722Now you want to do something with this cpuset.723# cd my_cpuset724725In this directory you can find several files:726# ls727cgroup.clone_children cpuset.memory_pressure728cgroup.event_control cpuset.memory_spread_page729cgroup.procs cpuset.memory_spread_slab730cpuset.cpu_exclusive cpuset.mems731cpuset.cpus cpuset.sched_load_balance732cpuset.mem_exclusive cpuset.sched_relax_domain_level733cpuset.mem_hardwall notify_on_release734cpuset.memory_migrate tasks735736Reading them will give you information about the state of this cpuset:737the CPUs and Memory Nodes it can use, the processes that are using738it, its properties. By writing to these files you can manipulate739the cpuset.740741Set some flags:742# /bin/echo 1 > cpuset.cpu_exclusive743744Add some cpus:745# /bin/echo 0-7 > cpuset.cpus746747Add some mems:748# /bin/echo 0-7 > cpuset.mems749750Now attach your shell to this cpuset:751# /bin/echo $$ > tasks752753You can also create cpusets inside your cpuset by using mkdir in this754directory.755# mkdir my_sub_cs756757To remove a cpuset, just use rmdir:758# rmdir my_sub_cs759This will fail if the cpuset is in use (has cpusets inside, or has760processes attached).761762Note that for legacy reasons, the "cpuset" filesystem exists as a763wrapper around the cgroup filesystem.764765The command766767mount -t cpuset X /sys/fs/cgroup/cpuset768769is equivalent to770771mount -t cgroup -ocpuset,noprefix X /sys/fs/cgroup/cpuset772echo "/sbin/cpuset_release_agent" > /sys/fs/cgroup/cpuset/release_agent7737742.2 Adding/removing cpus775------------------------776777This is the syntax to use when writing in the cpus or mems files778in cpuset directories:779780# /bin/echo 1-4 > cpuset.cpus -> set cpus list to cpus 1,2,3,4781# /bin/echo 1,2,3,4 > cpuset.cpus -> set cpus list to cpus 1,2,3,4782783To add a CPU to a cpuset, write the new list of CPUs including the784CPU to be added. To add 6 to the above cpuset:785786# /bin/echo 1-4,6 > cpuset.cpus -> set cpus list to cpus 1,2,3,4,6787788Similarly to remove a CPU from a cpuset, write the new list of CPUs789without the CPU to be removed.790791To remove all the CPUs:792793# /bin/echo "" > cpuset.cpus -> clear cpus list7947952.3 Setting flags796-----------------797798The syntax is very simple:799800# /bin/echo 1 > cpuset.cpu_exclusive -> set flag 'cpuset.cpu_exclusive'801# /bin/echo 0 > cpuset.cpu_exclusive -> unset flag 'cpuset.cpu_exclusive'8028032.4 Attaching processes804-----------------------805806# /bin/echo PID > tasks807808Note that it is PID, not PIDs. You can only attach ONE task at a time.809If you have several tasks to attach, you have to do it one after another:810811# /bin/echo PID1 > tasks812# /bin/echo PID2 > tasks813...814# /bin/echo PIDn > tasks8158168173. Questions818============819820Q: what's up with this '/bin/echo' ?821A: bash's builtin 'echo' command does not check calls to write() against822errors. If you use it in the cpuset file system, you won't be823able to tell whether a command succeeded or failed.824825Q: When I attach processes, only the first of the line gets really attached !826A: We can only return one error code per call to write(). So you should also827put only ONE pid.8288294. Contact830==========831832Web: http://www.bullopensource.org/cpuset833834835