new bitmap list format (for cpusets)
Subsystems (13)
PATCH 1/1 new bitmap list format (for cpusets)
[This is a copy of the bitmap list format patch that I submitted to
lkml on 9 Aug 2004, after removing the prefix character fluff^Wstuff. I include it here again just to get it associated with the current cpuset patch, which will follow in a second email 10 seconds later. -pj]
A bitmap print and parse format that provides lists of ranges of numbers, to be first used for by cpusets (next patch).
Cpusets provide a way to manage subsets of CPUs and Memory Nodes for scheduling and memory placement, via a new virtual file system, usually mounted at /dev/cpuset. Manipulation of cpusets can be done directly via this file system, from the shell.
However, manipulating 512 bit cpumasks or 256 bit nodemasks (which will get bigger) via hex mask strings is painful for humans.
The intention is to provide a format for the cpu and memory mask files in /dev/cpusets that will stand the test of time. This format is supported by a couple of new lib/bitmap.c routines, for printing and parsing these strings. Wrappers for cpumask and nodemask are provided.
include/linux/bitmap.h | 8 +++ include/linux/cpumask.h | 22 +++++++++- include/linux/nodemask.h | 22 +++++++++- lib/bitmap.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 150 insertions(+), 5 deletions(-)
include/linux/bitmap.h +7 -1
@@ -41,7 +41,9 @@* bitmap_shift_right(dst, src, n, nbits) *dst = *src >> n * bitmap_shift_left(dst, src, n, nbits) *dst = *src << n * bitmap_scnprintf(buf, len, src, nbits) Print bitmap src to buf -* bitmap_parse(ubuf, ulen, dst, nbits) Parse bitmap dst from buf +* bitmap_parse(ubuf, ulen, dst, nbits) Parse bitmap dst from user buf +* bitmap_scnlistprintf(buf, len, src, nbits) Print bitmap src as list to buf +* bitmap_parselist(buf, dst, nbits) Parse bitmap dst from list */ /*
@@ -98,6 +100,10 @@ extern int bitmap_scnprintf(char *buf, u const unsigned long *src, int nbits);extern int bitmap_parse (const char __user *ubuf,unsigned int ulen,unsigned long *dst,int nbits); +extern int bitmap_scnlistprintf (char *buf,unsigned int len, +const unsigned long *src,int nbits); +extern int bitmap_parselist (const char *buf,unsigned long *maskp, +int nmaskbits);extern int bitmap_find_free_region (unsigned long *bitmap,int bits,int order);extern void bitmap_release_region (unsigned long *bitmap,int pos,int order);extern int bitmap_allocate_region (unsigned long *bitmap,int pos,int order);
include/linux/cpumask.h +20 -2
@@ -10,6 +10,8 @@* * For details of cpumask_scnprintf() and cpumask_parse(), * see bitmap_scnprintf() and bitmap_parse() in lib/bitmap.c. +* For details of cpulist_scnprintf() and cpulist_parse(), see +* bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c. * * The available cpumask operations are: *
@@ -46,6 +48,8 @@* * int cpumask_scnprintf(buf, len, mask) Format cpumask for printing * int cpumask_parse(ubuf, ulen, mask) Parse ascii string as cpumask +* int cpulist_scnprintf(buf, len, mask) Format cpumask as list for printing +* int cpulist_parse(buf, map) Parse ascii string as cpulist * * for_each_cpu_mask(cpu, mask) for-loop cpu over mask *
@@ -268,14 +272,28 @@ static inline int __cpumask_scnprintf(chreturn bitmap_scnprintf (buf, len, srcp->bits, nbits); } -#define cpumask_parse (ubuf, ulen, src) \ - __cpumask_parse((ubuf), (ulen), &(src), NR_CPUS) +#define cpumask_parse (ubuf, ulen, dst) \ + __cpumask_parse((ubuf), (ulen), &(dst), NR_CPUS)static inline int __cpumask_parse (const char __user *buf,int len,cpumask_t *dstp,int nbits) {return bitmap_parse (buf, len, dstp->bits, nbits); } +#define cpulist_scnprintf (buf, len, src) \ + __cpulist_scnprintf((buf), (len), &(src), NR_CPUS) +static inline int __cpulist_scnprintf (char *buf,int len, +const cpumask_t *srcp,int nbits) +{ +return bitmap_scnlistprintf (buf, len, srcp->bits, nbits); +} + +#define cpulist_parse (buf, dst) __cpulist_parse((buf), &(dst), NR_CPUS) +static inline int __cpulist_parse (const char *buf,cpumask_t *dstp,int nbits) +{ +return bitmap_parselist (buf, dstp->bits, nbits); +} +#if NR_CPUS >1 #define for_each_cpu_mask (cpu, mask) \ for ((cpu) = first_cpu(mask); \
include/linux/nodemask.h +20 -2
@@ -10,6 +10,8 @@* * For details of nodemask_scnprintf() and nodemask_parse(), * see bitmap_scnprintf() and bitmap_parse() in lib/bitmap.c. +* For details of nodelist_scnprintf() and nodelist_parse(), see +* bitmap_scnlistprintf() and bitmap_parselist(), also in bitmap.c. * * The available nodemask operations are: *
@@ -46,6 +48,8 @@* * int nodemask_scnprintf(buf, len, mask) Format nodemask for printing * int nodemask_parse(ubuf, ulen, mask) Parse ascii string as nodemask +* int nodelist_scnprintf(buf, len, mask) Format nodemask as list for printing +* int nodelist_parse(buf, map) Parse ascii string as nodelist * * for_each_node_mask(node, mask) for-loop node over mask *
@@ -272,14 +276,28 @@ static inline int __nodemask_scnprintf(creturn bitmap_scnprintf (buf, len, srcp->bits, nbits); } -#define nodemask_parse (ubuf, ulen, src) \ - __nodemask_parse((ubuf), (ulen), &(src), MAX_NUMNODES) +#define nodemask_parse (ubuf, ulen, dst) \ + __nodemask_parse((ubuf), (ulen), &(dst), MAX_NUMNODES)static inline int __nodemask_parse (const char __user *buf,int len,nodemask_t *dstp,int nbits) {return bitmap_parse (buf, len, dstp->bits, nbits); } +#define nodelist_scnprintf (buf, len, src) \ + __nodelist_scnprintf((buf), (len), &(src), MAX_NUMNODES) +static inline int __nodelist_scnprintf (char *buf,int len, +const nodemask_t *srcp,int nbits) +{ +return bitmap_scnlistprintf (buf, len, srcp->bits, nbits); +} + +#define nodelist_parse (buf, dst) __nodelist_parse((buf), &(dst), MAX_NUMNODES) +static inline int __nodelist_parse (const char *buf,nodemask_t *dstp,int nbits) +{ +return bitmap_parselist (buf, dstp->bits, nbits); +} +#if MAX_NUMNODES >1 #define for_each_node_mask (node, mask) \ for ((node) = first_node(mask); \
lib/bitmap.c +103 -0
115 lines not on this page · raw message
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373PATCH 1/1 cpusets - big numa cpu and memory placement
Andrew,
This my cpuset patch, with the following changes in the last two weeks:
1) Updated to 2.6.8.1-mm1
2) [Simon Derr <[email protected]>] Fix new cpuset to begin empty, not copied from parent. Needed to avoid breaking exclusive property.
3) [Dinakar Guniguntala <[email protected]>] Finish initializing top cpuset from cpu_possible_map after smp_init() called.
4) [Paul Jackson <[email protected]>] Check on each call to __alloc_pages() if the current tasks cpuset mems_allowed has changed. Use a cpuset generation number, bumped on any cpuset memory placement change, to make this check efficient. Update the tasks mems_allowed from its cpuset, if the cpuset has changed.
5) [Paul Jackson <[email protected]>] If a task is moved to another cpuset, then update its cpus_allowed, using set_cpus_allowed().
6) [Paul Jackson <[email protected]>] Update Documentation/cpusets.txt to reflect above changes (4) and (5).
I continue to recommend the following patch for inclusion in your 2.6.9-*mm series, when that opens. It provides an important facility for high performance computing on large systems. Simon Derr of Bull (France) and myself are the primary authors. Erich Focht has indicated that NEC is also a potential user of this patch on the TX-7 NUMA machines, and that he "would very much welcome the inclusion of cpusets."
I offer this update to lkml, in order to invite continued feedback.
The one prerequiste patch for this cpuset patch was just posted before this one. That was a patch to provide a new bitmap list format, of which cpusets is the first user.
This patch has been built on top of 2.6.8.1-mm1, for the arch's:
i386 x86_64 sparc ia64 powerpc-405 powerpc-750 sparc64
with and without CONFIG_CPUSET. It has been booted and tested on ia64 (sn2_defconfig, SN2 hardware). The 'alpha' arch also built, except for what seems to be an unrelated toolchain problem (crosstool ld sigsegv) in the final link step.
===
Cpusets provide a mechanism for assigning a set of CPUs and Memory Nodes to a set of tasks.
Cpusets constrain the CPU and Memory placement of tasks to only the processor and memory resources within a tasks current cpuset. They form a nested hierarchy visible in a virtual file system. These are the essential hooks, beyond what is already present, required to manage dynamic job placement on large systems.
Cpusets require small kernel hooks in init, exit, fork, mempolicy, sched_setaffinity, page_alloc and vmscan. And they require a "struct cpuset" pointer, a cpuset_mems_generation, and a "mems_allowed" nodemask_t (to go along with the "cpus_allowed" cpumask_t that's already there) in each task struct.
These hooks:
1) establish and propagate cpusets,
2) enforce CPU placement in sched_setaffinity,
3) enforce Memory placement in mbind and sys_set_mempolicy,
4) restrict page allocation and scanning to mems_allowed, and
5) restrict migration and set_cpus_allowed to cpus_allowed.
The other required hook, restricting task scheduling to CPUs in a tasks cpus_allowed mask, is already present.
Cpusets extend the usefulness of, the existing placement support that was added to Linux 2.6 kernels: sched_setaffinity() for CPU placement, and mbind() and set_mempolicy() for memory placement. On smaller or dedicated use systems, the existing calls are often sufficient.
On larger NUMA systems, running more than one, performance critical, job, it is necessary to be able to manage jobs in their entirety. This includes providing a job with exclusive CPU and memory that no other job can use, and being able to list all tasks currently in a cpuset.
A given job running within a cpuset, would likely use the existing placement calls to manage its CPU and memory placement in more detail.
Cpusets are named, nested sets of CPUs and Memory Nodes. Each cpuset is represented by a directory in the cpuset virtual file system, normally mounted at /dev/cpuset.
Each cpuset directory provides the following files, which can be read and written:
cpus:
List of CPUs allowed to tasks in that cpuset.
mems:
List of Memory Nodes allowed to tasks in that cpuset.
tasks:
List of pid's of tasks in that cpuset.
cpu_exclusive:
Flag (0 or 1) - if set, cpuset has exclusive use of its CPUs (no sibling or cousin cpuset may overlap CPUs).
mem_exclusive:
Flag (0 or 1) - if set, cpuset has exclusive use of its Memory Nodes (no sibling or cousin may overlap).
notify_on_release:
Flag (0 or 1) - if set, then /sbin/cpuset_release_agent will be invoked, with the name (/dev/cpuset relative path) of that cpuset in argv[1], when the last user of it (task or child cpuset) goes away. This supports automatic cleanup of abandoned cpusets.
In addition one new filetype is added to the /proc file system:
/proc/<pid>/cpuset:
For each task (pid), list its cpuset path, relative to the root of the cpuset file system. This file is read-only.
New cpusets are created using 'mkdir' (at the shell or in C). Old ones are removed using 'rmdir'. The above files are accessed using read(2) and write(2) system calls, or shell commands such as 'cat' and 'echo'.
The CPUs and Memory Nodes in a given cpuset are always a subset of its parent. The root cpuset has all possible CPUs and Memory Nodes in the system. A cpuset may be exclusive (cpu or memory) only if its parent is similarly exclusive.
See further Documentation/cpusets.txt, at the top of the following patch.
Documentation/cpusets.txt | 380 +++++++++++ fs/proc/base.c | 19 include/linux/cpuset.h | 63 + include/linux/sched.h | 7 init/Kconfig | 10 init/main.c | 5 kernel/Makefile | 1 kernel/cpuset.c | 1550 ++++++++++++++++++++++++++++++++++++++++++++++ kernel/exit.c | 2 kernel/fork.c | 3 kernel/sched.c | 9 mm/mempolicy.c | 14 mm/page_alloc.c | 13 mm/vmscan.c | 19 14 files changed, 2090 insertions(+), 5 deletions(-)
Documentation/cpusets.txt +387 -0
@@ -0,0 +1,387 @@ + CPUSETS + ------- + +Copyright (C) 2004 BULL SA. +Written by [email protected] + +Portions Copyright (c) 2004 Silicon Graphics, Inc. +Modified by Paul Jackson <[email protected]> + +CONTENTS: +========= + +1. Cpusets + 1.1 What are cpusets ? + 1.2 Why are cpusets needed ? + 1.3 How are cpusets implemented ? + 1.4 How do I use cpusets ? +2. Usage Examples and Syntax + 2.1 Basic Usage + 2.2 Adding/removing cpus + 2.3 Setting flags + 2.4 Attaching processes +3. Questions +4. Contact + +1. Cpusets +========== + +1.1 What are cpusets ? +---------------------- + +Cpusets provide a mechanism for assigning a set of CPUs and Memory +Nodes to a set of tasks. + +Cpusets constrain the CPU and Memory placement of tasks to only +the resources within a tasks current cpuset. They form a nested +hierarchy visible in a virtual file system. These are the essential +hooks, beyond what is already present, required to manage dynamic +job placement on large systems. + +Each task has a pointer to a cpuset. Multiple tasks may reference +the same cpuset. Requests by a task, using the sched_setaffinity(2) +system call to include CPUs in its CPU affinity mask, and using the +mbind(2) and set_mempolicy(2) system calls to include Memory Nodes +in its memory policy, are both filtered through that tasks cpuset, +filtering out any CPUs or Memory Nodes not in that cpuset. The +scheduler will not schedule a task on a CPU that is not allowed in +its cpus_allowed vector, and the kernel page allocator will not +allocate a page on a node that is not allowed in the requesting tasks +mems_allowed vector. + +If a cpuset is cpu or mem exclusive, no other cpuset, other than a direct +ancestor or descendent, may share any of the same CPUs or Memory Nodes. + +User level code may create and destroy cpusets by name in the cpuset +virtual file system, manage the attributes and permissions of these +cpusets and which CPUs and Memory Nodes are assigned to each cpuset, +specify and query to which cpuset a task is assigned, and list the +task pids assigned to a cpuset. + + +1.2 Why are cpusets needed ? +---------------------------- + +The management of large computer systems, with many processors (CPUs), +complex memory cache hierarchies and multiple Memory Nodes having +non-uniform access times (NUMA) presents additional challenges for +the efficient scheduling and memory placement of processes. + +Frequently more modest sized systems can be operated with adequate +efficiency just by letting the operating system automatically share +the available CPU and Memory resources amongst the requesting tasks. + +But larger systems, which benefit more from careful processor and +memory placement to reduce memory access times and contention, +and which typically represent a larger investment for the customer, +can benefit from explictly placing jobs on properly sized subsets of +the system. + +This can be especially valuable on: + + * Web Servers running multiple instances of the same web application, + * Servers running different applications (for instance, a web server + and a database), or + * NUMA systems running large HPC applications with demanding + performance characteristics. + +These subsets, or "soft partitions" must be able to be dynamically +adjusted, as the job mix changes, without impacting other concurrently +executing jobs. + +The kernel cpuset patch provides the minimum essential kernel +mechanisms required to efficiently implement such subsets. It +leverages existing CPU and Memory Placement facilities in the Linux +kernel to avoid any additional impact on the critical scheduler or +memory allocator code. + + +1.3 How are cpusets implemented ? +--------------------------------- + +Cpusets provide a Linux kernel (2.6.7 and above) mechanism to constrain +which CPUs and Memory Nodes are used by a process or set of processes. + +The Linux kernel already has a pair of mechanisms to specify on which +CPUs a task may be scheduled (sched_setaffinity) and on which Memory +Nodes it may obtain memory (mbind, set_mempolicy). + +Cpusets extends these two mechanisms as follows: + + - Cpusets are sets of allowed CPUs and Memory Nodes, known to the + kernel. + - Each task in the system is attached to a cpuset, via a pointer + in the task structure to a reference counted cpuset structure. + - Calls to sched_setaffinity are filtered to just those CPUs + allowed in that tasks cpuset. + - Calls to mbind and set_mempolicy are filtered to just + those Memory Nodes allowed in that tasks cpuset. + - The root cpuset contains all the systems CPUs and Memory + Nodes. + - For any cpuset, one can define child cpusets containing a subset + of the parents CPU and Memory Node resources. + - The hierarchy of cpusets can be mounted at /dev/cpuset, for + browsing and manipulation from user space. + - A cpuset may be marked exclusive, which ensures that no other + cpuset (except direct ancestors and descendents) may contain + any overlapping CPUs or Memory Nodes. + - You can list all the tasks (by pid) attached to any cpuset. + +The implementation of cpusets requires a few, simple hooks +into the rest of the kernel, none in performance critical paths: + + - in main/init.c, to initialize the root cpuset at system boot. + - in fork and exit, to attach and detach a task from its cpuset. + - in sched_setaffinity, to mask the requested CPUs by what's + allowed in that tasks cpuset. + - in sched.c migrate_all_tasks(), to keep migrating tasks within + the CPUs allowed by their cpuset, if possible. + - in the mbind and set_mempolicy system calls, to mask the requested + Memory Nodes by what's allowed in that tasks cpuset. + - in page_alloc, to restrict memory to allowed nodes. + - in vmscan.c, to restrict page recovery to the current cpuset. + +In addition a new file system, of type "cpuset" may be mounted, +typically at /dev/cpuset, to enable browsing and modifying the cpusets +presently known to the kernel. No new system calls are added for +cpusets - all support for querying and modifying cpusets is via +this cpuset file system. + +Each task under /proc has an added file named 'cpuset', displaying +the cpuset name, as the path relative to the root of the cpuset file +system. + +Each cpuset is represented by a directory in the cpuset file system +containing the following files describing that cpuset: + + - cpus: list of CPUs in that cpuset + - mems: list of Memory Nodes in that cpuset + - cpu_exclusive flag: is cpu placement exclusive? + - mem_exclusive flag: is memory placement exclusive? + - tasks: list of tasks (by pid) attached to that cpuset + +New cpusets are created using the mkdir system call or shell +command. The properties of a cpuset, such as its flags, allowed +CPUs and Memory Nodes, and attached tasks, are modified by writing +to the appropriate file in that cpusets directory, as listed above. + +The named hierarchical structure of nested cpusets allows partitioning +a large system into nested, dynamically changeable, "soft-partitions". + +The attachment of each task, automatically inherited at fork by any +children of that task, to a cpuset allows organizing the work load +on a system into related sets of tasks such that each set is constrained +to using the CPUs and Memory Nodes of a particular cpuset. A task +may be re-attached to any other cpuset, if allowed by the permissions +on the necessary cpuset file system directories. + +Such management of a system "in the large" integrates smoothly with +the detailed placement done on individual tasks and memory regions +using the sched_setaffinity, mbind and set_mempolicy system calls. + +The following rules apply to each cpuset: + + - Its CPUs and Memory Nodes must be a subset of its parents. + - It can only be marked exclusive if its parent is. + - If its cpu or memory is exclusive, they may not overlap any sibling. + +These rules, and the natural hierarchy of cpusets, enable efficient +enforcement of the exclusive guarantee, without having to scan all +cpusets every time any of them change to ensure nothing overlaps a +exclusive cpuset. Also, the use of a Linux virtual file system (vfs) +to represent the cpuset hierarchy provides for a familiar permission +and name space for cpusets, with a minimum of additional kernel code. + +1.4 How do I use cpusets ? +-------------------------- + +In order to minimize the impact of cpusets on critical kernel +code, such as the scheduler, and due to the fact that the kernel +does not support one task updating the memory placement of another +task directly, the impact on a task of changing its cpuset CPU +or Memory Node placement, or of changing to which cpuset a task +is attached, is subtle. + +If a cpuset has its Memory Nodes modified, then for each task attached +to that cpuset, the next time that the kernel attempts to allocate +a page of memory for that task, the kernel will notice the change +in the tasks cpuset, and update its per-task memory placement to +remain within the new cpusets memory placement. If the task was using +mempolicy MPOL_BIND, and the nodes to which it was bound overlap with +its new cpuset, then the task will continue to use whatever subset +of MPOL_BIND nodes are still allowed in the new cpuset. If the task +was using MPOL_BIND and now none of its MPOL_BIND nodes are allowed +in the new cpuset, then the task will be essentially treated as if it +was MPOL_BIND bound to the new cpuset (even though its numa placement, +as queried by get_mempolicy(), doesn't change). If a task is moved +from one cpuset to another, then the kernel will adjust the tasks +memory placement, as above, the next time that the kernel attempts +to allocate a page of memory for that task. + +If a cpuset has its CPUs modified, then each task using that +cpuset does _not_ change its behavior automatically. In order to +minimize the impact on the critical scheduling code in the kernel, +tasks will continue to use their prior CPU placement until they +are rebound to their cpuset, by rewriting their pid to the 'tasks' +file of their cpuset. If a task had been bound to some subset of its +cpuset using the sched_setaffinity() call, and if any of that subset +is still allowed in its new cpuset settings, then the task will be +restricted to the intersection of the CPUs it was allowed on before, +and its new cpuset CPU placement. If, on the other hand, there is +no overlap between a tasks prior placement and its new cpuset CPU +placement, then the task will be allowed to run on any CPU allowed +in its new cpuset. If a task is moved from one cpuset to another, +its CPU placement is updated in the same way as if the tasks pid is +rewritten to the 'tasks' file of its current cpuset. + +In summary, the memory placement of a task whose cpuset is changed is +updated by the kernel, on the next allocation of a page for that task, +but the processor placement is not updated, until that tasks pid is +rewritten to the 'tasks' file of its cpuset. This is done to avoid +impacting the scheduler code in the kernel with a check for changes +in a tasks processor placement. + +To start a new job that is to be contained within a cpuset, the steps are: + + 1) mkdir /dev/cpuset + 2) mount -t cpuset none /dev/cpuset + 3) Create the new cpuset by doing mkdir's and write's (or echo's) in + the /dev/cpuset virtual file system. + 4) Start a task that will be the "founding father" of the new job. + 5) Attach that task to the new cpuset by writing its pid to the + /dev/cpuset tasks file for that cpuset. + 6) fork, exec or clone the job tasks from this founding father task. + +For example, the following sequence of commands will setup a cpuset +named "Charlie", containing just CPUs 2 and 3, and Memory Node 1, +and then start a subshell 'sh' in that cpuset: + + mount -t cpuset none /dev/cpuset + cd /dev/cpuset + mkdir Charlie + cd Charlie + /bin/echo 2-3 > cpus + /bin/echo 1 > mems + /bin/echo $$ > tasks + sh + # The subshell 'sh' is now running in cpuset Charlie + # The next line should display '/Charlie' + cat /proc/self/cpuset + +In the case that a change of cpuset includes wanting to move already +allocated memory pages, consider further the work of IWAMOTO +Toshihiro <[email protected]> for page remapping and memory +hotremoval, which can be found at: + + http://people.valinux.co.jp/~iwamoto/mh.html + +The integration of cpusets with such memory migration is not yet +available. + +In the future, a C library interface to cpusets will likely be +available. For now, the only way to query or modify cpusets is +via the cpuset file system, using the various cd, mkdir, echo, cat, +rmdir commands from the shell, or their equivalent from C. + +The sched_setaffinity calls can also be done at the shell prompt using +SGI's runon or Robert Love's taskset. The mbind and set_mempolicy +calls can be done at the shell prompt using the numactl command +(part of Andi Kleen's numa package). + +2. Usage Examples and Syntax +============================ + +2.1 Basic Usage +--------------- + +Creating, modifying, using the cpusets can be done through the cpuset +virtual filesystem. + +To mount it, type: +# mount -t cpuset none /dev/cpuset + +Then under /dev/cpuset you can find a tree that corresponds to the +tree of the cpusets in the system. For instance, /dev/cpuset +is the cpuset that holds the whole system. + +If you want to create a new cpuset under /dev/cpuset: +# cd /dev/cpuset +# mkdir my_cpuset + +Now you want to do something with this cpuset. +# cd my_cpuset + +In this directory you can find several files: +# ls +cpus cpu_exclusive mems mem_exclusive tasks + +Reading them will give you information about the state of this cpuset: +the CPUs and Memory Nodes it can use, the processes that are using +it, its properties. By writing to these files you can manipulate +the cpuset. + +Set some flags: +# /bin/echo 1 > cpu_exclusive + +Add some cpus: +# /bin/echo 0-7 > cpus + +Now attach your shell to this cpuset: +# /bin/echo $$ > tasks + +You can also create cpusets inside your cpuset by using mkdir in this +directory. +# mkdir my_sub_cs + +To remove a cpuset, just use rmdir: +# rmdir my_sub_cs +This will fail if the cpuset is in use (has cpusets inside, or has +processes attached). + +2.2 Adding/removing cpus +------------------------ + +This is the syntax to use when writing in the cpus or mems files +in cpuset directories: + +# /bin/echo 1-4 > cpus -> set cpus list to cpus 1,2,3,4 +# /bin/echo 1,2,3,4 > cpus -> set cpus list to cpus 1,2,3,4 + +2.3 Setting flags +----------------- + +The syntax is very simple: + +# /bin/echo 1 > cpu_exclusive -> set flag 'cpu_exclusive' +# /bin/echo 0 > cpu_exclusive -> unset flag 'cpu_exclusive' + +2.4 Attaching processes +----------------------- + +# /bin/echo PID > tasks + +Note that it is PID, not PIDs. You can only attach ONE task at a time. +If you have several tasks to attach, you have to do it one after another: + +# /bin/echo PID1 > tasks +# /bin/echo PID2 > tasks + ... +# /bin/echo PIDn > tasks + + +3. Questions +============ + +Q: what's up with this '/bin/echo' ? +A: bash's builtin 'echo' command does not check calls to write() against + errors. If you use it in the cpuset file system, you won't be + able to tell whether a command succeeded or failed. + +Q: When I attach processes, only the first of the line gets really attached ! +A: We can only return one error code per call to write(). So you should also + put only ONE pid. + +4. Contact +========== + +Web: http://www.bullopensource.org/cpuset
fs/proc/base.c +19 -0
@@ -27,6 +27,7 @@#include <linux/namei.h> #include <linux/namespace.h> #include <linux/mm.h> +#include <linux/cpuset.h> #include <linux/smp_lock.h> #include <linux/kallsyms.h> #include <linux/mount.h>
@@ -63,6 +64,9 @@ enum pid_directory_inos {#ifdef CONFIG_SCHEDSTATS PROC_TGID_SCHEDSTAT,#endif +#ifdef CONFIG_CPUSETS + PROC_TGID_CPUSET, +#endif #ifdef CONFIG_SECURITY PROC_TGID_ATTR , PROC_TGID_ATTR_CURRENT,
@@ -89,6 +93,9 @@ enum pid_directory_inos {#ifdef CONFIG_SCHEDSTATS PROC_TID_SCHEDSTAT,#endif +#ifdef CONFIG_CPUSETS + PROC_TID_CPUSET, +#endif #ifdef CONFIG_SECURITY PROC_TID_ATTR , PROC_TID_ATTR_CURRENT,
@@ -132,6 +139,9 @@ static struct pid_entry tgid_base_stuff[#ifdef CONFIG_SCHEDSTATS E(PROC_TGID_SCHEDSTAT, "schedstat", S_IFREG|S_IRUGO),#endif +#ifdef CONFIG_CPUSETS + E(PROC_TGID_CPUSET, "cpuset", S_IFREG|S_IRUGO), +#endif {0,0,NULL,0} }; static struct pid_entry tid_base_stuff[] = {
@@ -157,6 +167,9 @@ static struct pid_entry tid_base_stuff[]#ifdef CONFIG_SCHEDSTATS E(PROC_TID_SCHEDSTAT, "schedstat",S_IFREG|S_IRUGO),#endif +#ifdef CONFIG_CPUSETS +E (PROC_TID_CPUSET ,"cpuset" ,S_IFREG |S_IRUGO ), +#endif {0,0,NULL,0} };
@@ -1378,6 +1391,12 @@ static struct dentry *proc_pident_lookup ei->op.proc_read = proc_pid_schedstat;break ;#endif +#ifdef CONFIG_CPUSETS +case PROC_TID_CPUSET : +case PROC_TGID_CPUSET : + inode->i_fop = &proc_cpuset_operations; +break ; +#endif default :printk ("procfs: impossible type (%d)" ,p->type);iput (inode);
include/linux/cpuset.h +63 -0
63 lines not on this page · raw message
include/linux/sched.h +7 -0
@@ -13,6 +13,7 @@#include <linux/rbtree.h> #include <linux/thread_info.h> #include <linux/cpumask.h> +#include <linux/nodemask.h> #include <asm/system.h> #include <asm/semaphore.h>
@@ -391,6 +392,7 @@ extern struct file_operations proc_schedstruct io_context ;/* See blkdev.h */ void exit_io_context (void ); +struct cpuset ;#define NGROUPS_SMALL 32#define NGROUPS_PER_BLOCK ((int)(PAGE_SIZE / sizeof(gid_t)))
@@ -590,6 +592,11 @@ struct task_struct {struct rw_semaphore pagg_sem;#endif +#ifdef CONFIG_CPUSETS +struct cpuset *cpuset; +nodemask_t mems_allowed; +int cpuset_mems_generation; +#endif };static inline pid_t process_group (struct task_struct *tsk)
init/Kconfig +10 -0
@@ -276,6 +276,16 @@ config EPOLL Disabling this option will cause the kernel to be built without support for epoll family of system calls. +config CPUSETS + bool "Cpuset support" + help + This options will let you create and manage CPUSET's which + allow dynamically partitioning a system into sets of CPUs and + Memory Nodes and assigning tasks to run only within those sets. + This is primarily useful on large SMP or NUMA systems. + + Say N if unsure. + source "drivers/block/Kconfig.iosched" config CC_OPTIMIZE_FOR_SIZE
init/main.c +5 -0
@@ -41,6 +41,7 @@#include <linux/kallsyms.h> #include <linux/writeback.h> #include <linux/cpu.h> +#include <linux/cpuset.h> #include <linux/efi.h> #include <linux/unistd.h> #include <linux/rmap.h>
@@ -562,6 +563,8 @@ asmlinkage void __init start_kernel(void#ifdef CONFIG_PROC_FS proc_root_init ();#endif +cpuset_init (); +check_bugs ();acpi_early_init ();/* before LAPIC and SMP init */
@@ -700,6 +703,8 @@ static int init(void * unused)smp_init ();sched_init_smp (); +cpuset_init_smp (); +/* * Do this before initcalls, because some drivers want to access * firmware files.
kernel/Makefile +1 -0
@@ -20,6 +20,7 @@ obj-$(CONFIG_PM) += power/ obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_PAGG) += pagg.o +obj-$(CONFIG_CPUSETS) += cpuset.o obj-$(CONFIG_IKCONFIG) += configs.o obj-$(CONFIG_IKCONFIG_PROC) += configs.o obj-$(CONFIG_STOP_MACHINE) += stop_machine.o
kernel/cpuset.c +1550 -0
1,550 lines not on this page · raw message
kernel/exit.c +2 -0
@@ -24,6 +24,7 @@#include <linux/mount.h> #include <linux/proc_fs.h> #include <linux/mempolicy.h> +#include <linux/cpuset.h> #include <linux/perfctr.h> #include <asm/uaccess.h>
@@ -831,6 +832,7 @@ asmlinkage NORET_TYPE void do_exit(long__exit_fs (tsk);exit_namespace (tsk);exit_thread (); +cpuset_exit (tsk);exit_keys (tsk);if (tsk->signal->leader)
kernel/fork.c +3 -0
@@ -29,6 +29,7 @@#include <linux/mman.h> #include <linux/fs.h> #include <linux/cpu.h> +#include <linux/cpuset.h> #include <linux/security.h> #include <linux/swap.h> #include <linux/syscalls.h>
@@ -1131,6 +1132,8 @@ static task_t *copy_process(unsigned lonif (p->ptrace &PT_PTRACED )__ptrace_link (p, current->parent); +cpuset_fork (p); +attach_pid (p,PIDTYPE_PID , p->pid);if (thread_group_leader (p)) {attach_pid (p,PIDTYPE_TGID , p->tgid);
kernel/sched.c +7 -2
@@ -38,6 +38,7 @@#include <linux/timer.h> #include <linux/rcupdate.h> #include <linux/cpu.h> +#include <linux/cpuset.h> #include <linux/percpu.h> #include <linux/perfctr.h> #include <linux/kthread.h>
@@ -3319,7 +3320,7 @@ out_unlock:asmlinkage long sys_sched_setaffinity (pid_t pid,unsigned int len,unsigned long __user *user_mask_ptr) { -cpumask_t new_mask; +cpumask_t new_mask, cpus_allowed;int retval;task_t *p;
@@ -3352,6 +3353,8 @@ asmlinkage long sys_sched_setaffinity(pi !capable(CAP_SYS_NICE)) goto out_unlock; + cpus_allowed =cpuset_cpus_allowed (p); +cpus_and (new_mask, new_mask, cpus_allowed); retval =set_cpus_allowed (p, new_mask); out_unlock:
@@ -3939,7 +3942,9 @@ static void migrate_all_tasks(int src_cpif (dest_cpu ==NR_CPUS ) dest_cpu =any_online_cpu (tsk->cpus_allowed);if (dest_cpu ==NR_CPUS ) { -cpus_setall (tsk->cpus_allowed); + tsk->cpus_allowed =cpuset_cpus_allowed (tsk); +if (!cpus_intersects (tsk->cpus_allowed, cpu_online_map)) +cpus_setall (tsk->cpus_allowed); dest_cpu =any_online_cpu (tsk->cpus_allowed);/*
mm/mempolicy.c +13 -1
@@ -67,6 +67,7 @@#include <linux/sched.h> #include <linux/mm.h> #include <linux/nodemask.h> +#include <linux/cpuset.h> #include <linux/gfp.h> #include <linux/slab.h> #include <linux/string.h>
@@ -133,6 +134,7 @@ static int get_nodes(unsigned long *nodeunsigned long nlongs;unsigned long endmask; + --maxnode;bitmap_zero (nodes,MAX_NUMNODES );if (maxnode ==0 || !nmask)return 0 ;
@@ -163,6 +165,10 @@ static int get_nodes(unsigned long *nodeif (copy_from_user (nodes, nmask, nlongs*sizeof (unsigned long )))return -EFAULT ; nodes[nlongs-1 ] &= endmask; +/* Update current mems_allowed */ +cpuset_update_current_mems_allowed (); +/* Ignore nodes not set in current->mems_allowed */ +cpuset_restrict_to_mems_allowed (nodes);return mpol_check_policy (mode, nodes); }
@@ -573,8 +579,10 @@ static struct zonelist *zonelist_policy(break ;case MPOL_BIND :/* Lower zones don't get a policy applied */ +/* Careful: current->mems_allowed might have moved */ if (gfp >= policy_zone) -return policy->v.zonelist; +if (cpuset_zonelist_valid_mems_allowed (policy->v.zonelist)) +return policy->v.zonelist;/*FALL THROUGH*/ case MPOL_INTERLEAVE :/* should not happen */ case MPOL_DEFAULT :
@@ -665,6 +673,8 @@ alloc_page_vma(unsigned gfp, struct vm_a {struct mempolicy *pol =get_vma_policy (vma, addr); +cpuset_update_current_mems_allowed (); +if (unlikely (pol->policy ==MPOL_INTERLEAVE )) {unsigned nid;if (vma) {
@@ -702,6 +712,8 @@ struct page *alloc_pages_current(unsigne {struct mempolicy *pol = current->mempolicy; +if (!in_interrupt ()) +cpuset_update_current_mems_allowed ();if (!pol ||in_interrupt ()) pol = &default_policy;if (pol->policy ==MPOL_INTERLEAVE )
mm/page_alloc.c +13 -0
@@ -31,6 +31,7 @@#include <linux/topology.h> #include <linux/sysctl.h> #include <linux/cpu.h> +#include <linux/cpuset.h> #include <linux/nodemask.h> #include <asm/tlbflush.h>
@@ -636,6 +637,9 @@ __alloc_pages(unsigned int gfp_mask, unsif (z->free_pages < min)continue ; +if (!cpuset_zone_allowed (z)) +continue ; + page =buffered_rmqueue (z, order, gfp_mask);if (page) goto got_pg;
@@ -659,6 +663,9 @@ __alloc_pages(unsigned int gfp_mask, unsif (z->free_pages < min)continue ; +if (!cpuset_zone_allowed (z)) +continue ; + page =buffered_rmqueue (z, order, gfp_mask);if (page) goto got_pg;
@@ -668,6 +675,8 @@ __alloc_pages(unsigned int gfp_mask, unsif ((p->flags & (PF_MEMALLOC |PF_MEMDIE )) && !in_interrupt ()) {/* go through the zonelist yet again, ignoring mins */ for (i =0 ; (z = zones[i]) !=NULL ; i++) { +if (!cpuset_zone_allowed (z)) +continue ; page =buffered_rmqueue (z, order, gfp_mask);if (page) goto got_pg;
@@ -702,6 +711,9 @@ rebalance:if (z->free_pages < min)continue ; +if (!cpuset_zone_allowed (z)) +continue ; + page =buffered_rmqueue (z, order, gfp_mask);if (page) goto got_pg;
@@ -1307,6 +1319,7 @@ void __init build_all_zonelists(void)for (i =0 ; i < numnodes ; i++)build_zonelists (NODE_DATA (i));printk ("Built %i zonelists\n" , numnodes); +cpuset_init_current_mems_allowed (); }/*
mm/vmscan.c +17 -2
@@ -31,6 +31,7 @@#include <linux/rmap.h> #include <linux/topology.h> #include <linux/cpu.h> +#include <linux/cpuset.h> #include <linux/notifier.h> #include <linux/rwsem.h>
@@ -851,6 +852,9 @@ shrink_caches(struct zone **zones, strucfor (i =0 ; zones[i] !=NULL ; i++) {struct zone *zone = zones[i]; +if (!cpuset_zone_allowed (zone)) +continue ; + zone->temp_priority = sc->priority;if (zone->prev_priority > sc->priority) zone->prev_priority = sc->priority;
@@ -894,6 +898,9 @@ int try_to_free_pages(struct zone **zonefor (i =0 ; zones[i] !=NULL ; i++) {struct zone *zone = zones[i]; +if (!cpuset_zone_allowed (zone)) +continue ; + zone->temp_priority =DEF_PRIORITY ; lru_pages += zone->nr_active + zone->nr_inactive; }
@@ -935,8 +942,14 @@ int try_to_free_pages(struct zone **zoneif ((gfp_mask & __GFP_FS) && !(gfp_mask & __GFP_NORETRY))out_of_memory (gfp_mask); out: -for (i =0 ; zones[i] !=0 ; i++) - zones[i]->prev_priority = zones[i]->temp_priority; +for (i =0 ; zones[i] !=0 ; i++) { +struct zone *zone = zones[i]; + +if (!cpuset_zone_allowed (zone)) +continue ; + + zone->prev_priority = zone->temp_priority; + }return ret; }
@@ -1144,6 +1157,8 @@ void wakeup_kswapd(struct zone *zone) {if (zone->free_pages > zone->pages_low)return ; +if (!cpuset_zone_allowed (zone)) +return ;if (!waitqueue_active (&zone->zone_pgdat->kswapd_wait))return ;wake_up_interruptible (&zone->zone_pgdat->kswapd_wait);
Signature · 3 lines
I won't rest till it's the best ...
Programmer, Linux Scalability
Paul Jackson <[email protected]> 1.650.933.1373