CPUSET Proposal
Subsystems (6)
Hi,
We have developped a new feature in the Linux kernel, controlling CPU placements, which are useful on large SMP machines, especially NUMA ones. We call it CPUSETS, and we would highly appreciate to know about anyone who would be interested in such a feature. This has been somewhat inspired by the pset or cpumemset patches existing for Linux 2.4.
CPUSETs are lightweight objects in the linux kernel that enable users to partition their multiprocessor machine by creating execution areas. A virtualization layer has been added so it becomes possible to split a machine in terms of CPUs.
Furthermore, HPC applications often need to bind their processes to a specific CPU, and can achieve this by calling sched_setaffinity() in the recent Linux kernels. But running several HPC applications on a large system will result in several processes running on the same processor. This problem is addressed by the CPUSET mechanism.
CPUSETS allow to:
----------------
1/ create sets of CPUs on the system, and bind applications to them
2/ translate the masks of CPUs given to sched_setaffinity() so they stay
inside the set of CPUs. With this mechanism, processors are virtualized, for the use of sched_setaffinity() and /proc information. Thus, any former application using this syscall to bind processes to processors will work with virtual CPUs without any change.
3/ provide a way to create sets of cpus *inside* a set of cpus : hence a
system administrator can partition a system among users, and users can partition their partition among their applications.
4/ Change on the fly the execution area of a whole set of processes (to
give more resources to a critical application, for example).
...
5/ In the future, probably associate a memory allocation policy (such as local node, or round robin) to a set of cpus.
These features have been implemented as a kernel patch for Linux 2.6 and a suite of userland tools.
You can find the associated manpages and a slightly more detailed explanation here: http://www.bullopensource.org/cpuset/
Any feedback, comment or opinion is welcome:
[email protected],
[email protected]
Thanks,
Simon and Sylvain.
Makefile +1 -1
@@ -1,7 +1,7 @@ VERSION = 2 PATCHLEVEL = 6 SUBLEVEL = 0 -EXTRAVERSION = -test4 +EXTRAVERSION = -test4+cpusets # *DOCUMENTATION* # To see a list of typical targets execute "make help"
arch/ia64/Kconfig +31 -0
@@ -388,6 +388,37 @@ If you don't know what to do here, say N. +config CPUSETS + bool "cpusets" + depends on SMP + help + This options will let you create and manage sets of cpu where you + can run the processes. + + Say N if unsure. + +config CPUSETS_PROC + bool "/proc/cpusets support" + depends on CPUSETS + help + Get some info about the existing cpusets in your system. + To use this option, you have to ensure that the "/proc file system + support" (CONFIG_PROC_FS) is enabled, too. + +config CPUSETS_PROC_CPUINFO + bool "/proc/cpuinfo uses current cpuset" + depends on CPUSETS_PROC + help + With this option enabled, a process reading /proc/cpuinfo will + only see the CPUs that are in its current cpuset. + +config CPUSETS_PROC_STAT + bool "/proc/stat uses current cpuset" + depends on CPUSETS_PROC + help + With this option enabled, a process reading /proc/stat will + only see the CPUs that are in its current cpuset. + config PREEMPT bool "Preemptible Kernel" help
arch/ia64/kernel/entry.S +8 -0
@@ -1481,11 +1481,19 @@ data8 ia64_ni_syscall data8 ia64_ni_syscall // 1265 data8 ia64_ni_syscall +#ifdef CONFIG_CPUSETS + data8 sys_cpuset_create + data8 sys_cpuset_destroy + data8 sys_cpuset_alloc + data8 sys_cpuset_attach // 1270 + data8 sys_cpuset_getfreecpus +#else data8 ia64_ni_syscall data8 ia64_ni_syscall data8 ia64_ni_syscall data8 ia64_ni_syscall // 1270 data8 ia64_ni_syscall +#endif data8 ia64_ni_syscall data8 ia64_ni_syscall data8 ia64_ni_syscall
arch/ia64/kernel/setup.c +14 -0
@@ -46,6 +46,10 @@#include <asm/system.h> #include <asm/unistd.h> +#ifdef CONFIG_CPUSETS_PROC_CPUINFO +# include <linux/cpuset_types.h> +#endif +#if defined(CONFIG_SMP ) && (IA64_CPU_SIZE >PAGE_SIZE )# error "struct cpuinfo_ia64 too big!"#endif
@@ -360,6 +364,16 @@unsigned long mask;int i; +#ifdef CONFIG_CPUSETS_PROC_CPUINFO +#define GET_BIT (mask, index) ((mask) & (1 << (index))) +/* show only CPUs in current cpuset */ +if (!current->cpuset) +BUG (); + +if (!GET_BIT (current->cpuset->cpus_allowed, cpunum)) +return 0 ; +#endif /* CONFIG_CPUSETS_PROC_CPUINFO */ + mask = c->features;switch (c->family) {
fs/proc/base.c +15 -0
@@ -59,6 +59,7 @@PROC_PID_CPU ,PROC_PID_MOUNTS ,PROC_PID_WCHAN , +PROC_PID_CPUSET , #ifdef CONFIG_SECURITYPROC_PID_ATTR , PROC_PID_ATTR_CURRENT,
@@ -99,6 +100,9 @@#ifdef CONFIG_KALLSYMS E(PROC_PID_WCHAN, "wchan", S_IFREG|S_IRUGO),#endif +#ifdef CONFIG_CPUSETS_PROC +E (PROC_PID_CPUSET ,"cpuset" ,S_IFREG |S_IRUGO ), +#endif {0,0,NULL,0} };#ifdef CONFIG_SECURITY
@@ -292,6 +296,11 @@ }#endif /* CONFIG_KALLSYMS */ + +#ifdef CONFIG_CPUSETS_PROC +int proc_pid_cpuset (struct task_struct *task,char *buffer); +#endif /* CONFIG_CPUSETS_PROC */ +/************************************************************************/ /* Here the fs part begins */ /************************************************************************/
@@ -1217,6 +1226,12 @@ ei->op.proc_read = proc_pid_wchan;break ;#endif +#ifdef CONFIG_CPUSETS_PROC +case PROC_PID_CPUSET : + inode->i_fop = &proc_info_file_operations; + ei->op.proc_read = proc_pid_cpuset; +break ; +#endif default :printk ("procfs: impossible type (%d)" ,p->type);iput (inode);
fs/proc/base.c.orig +10 -0
@@ -56,6 +56,7 @@ PROC_PID_STAT, PROC_PID_STATM, PROC_PID_MAPS, + PROC_PID_CPU, PROC_PID_MOUNTS, PROC_PID_WCHAN, #ifdef CONFIG_SECURITY
@@ -83,6 +84,9 @@ E(PROC_PID_CMDLINE, "cmdline", S_IFREG|S_IRUGO), E(PROC_PID_STAT, "stat", S_IFREG|S_IRUGO), E(PROC_PID_STATM, "statm", S_IFREG|S_IRUGO), +#ifdef CONFIG_SMP + E(PROC_PID_CPU, "cpu", S_IFREG|S_IRUGO), +#endif E(PROC_PID_MAPS, "maps", S_IFREG|S_IRUGO), E(PROC_PID_MEM, "mem", S_IFREG|S_IRUSR|S_IWUSR), E(PROC_PID_CWD, "cwd", S_IFLNK|S_IRWXUGO),
@@ -1181,6 +1185,12 @@ case PROC_PID_MAPS: inode->i_fop = &proc_maps_operations; break; +#ifdef CONFIG_SMP + case PROC_PID_CPU: + inode->i_fop = &proc_info_file_operations; + ei->op.proc_read = proc_pid_cpu; + break; +#endif case PROC_PID_MEM: inode->i_op = &proc_mem_inode_operations; inode->i_fop = &proc_mem_operations;
fs/proc/proc_misc.c +20 -0
@@ -51,6 +51,10 @@#include <asm/tlb.h> #include <asm/div64.h> +#ifdef CONFIG_CPUSETS_PROC_STAT +# include <linux/cpuset_types.h> +#endif +#define LOAD_INT (x) ((x) >> FSHIFT)#define LOAD_FRAC (x) LOAD_INT(((x) & (FIXED_1-1)) * 100)/*
@@ -379,10 +383,21 @@ jif = ((u64 )now.tv_sec *HZ ) + (now.tv_usec/(1000000 /HZ )) - jif;do_div (jif,HZ ); +#ifdef CONFIG_CPUSETS_PROC_STAT +#define GET_BIT (mask, index) ((mask) & (1 << (index))) +#endif +for (i =0 ; i <NR_CPUS ; i++) {int j;if (!cpu_online (i))continue ; + +#ifdef CONFIG_CPUSETS_PROC_STAT +/* show only CPUs in current cpuset */ +if (!GET_BIT (current->cpuset->cpus_allowed, i)) +continue ; +#endif + user +=kstat_cpu (i).cpustat.user; nice +=kstat_cpu (i).cpustat.nice; system +=kstat_cpu (i).cpustat.system;
@@ -404,6 +419,11 @@ jiffies_to_clock_t(softirq));for (i =0 ; i <NR_CPUS ; i++){if (!cpu_online (i))continue ; +#ifdef CONFIG_CPUSETS_PROC_STAT +/* show only CPUs in current cpuset */ +if (!GET_BIT (current->cpuset->cpus_allowed, i)) +continue ; +#endif len += sprintf(page + len, "cpu%d %u %u %u %u %u %u %u\n", i, jiffies_to_clock_t(kstat_cpu(i).cpustat.user),
include/asm-ia64/unistd.h +12 -0
@@ -254,6 +254,18 @@#define NR_syscalls 256/* length of syscall table */ +#define __NR_sys_cpuset_create 1267 +#define __NR_sys_cpuset_destroy 1268 +#define __NR_sys_cpuset_alloc 1269 +#define __NR_sys_cpuset_attach 1270 +#define __NR_sys_cpuset_getfreecpus 1271 + +#define __NR_sys_cpuset_create 1267 +#define __NR_sys_cpuset_destroy 1268 +#define __NR_sys_cpuset_alloc 1269 +#define __NR_sys_cpuset_attach 1270 +#define __NR_sys_cpuset_getfreecpus 1271 +#if !defined(__ASSEMBLY__) && !defined(ASSEMBLER )extern long __ia64_syscall (long a0,long a1,long a2,long a3,long a4,long nr);
include/linux/init_task.h +9 -0
@@ -56,6 +56,12 @@ .siglock = SPIN_LOCK_UNLOCKED, \ } +#ifdef CONFIG_CPUSETS +#define CPUSET_TSKINIT (a,b) .a = b, +#else +#define CPUSET_TSKINIT (a,b) +#endif +/* * INIT_TASK is used to set up the first task table, touch at * your own risk!. Base=0, limit=0x1fffff (=2MB)
@@ -108,6 +114,9 @@ .proc_lock = SPIN_LOCK_UNLOCKED, \ .switch_lock = SPIN_LOCK_UNLOCKED, \ .journal_info = NULL, \ + CPUSET_TSKINIT(cpus_wanted, CPU_MASK_ALL) \ + CPUSET_TSKINIT(cpuset, &top_cpuset) \ + CPUSET_TSKINIT(cpuset_attach_lock, SPIN_LOCK_UNLOCKED) \ }
include/linux/sched.h +8 -0
@@ -29,6 +29,7 @@#include <linux/completion.h> #include <linux/pid.h> #include <linux/percpu.h> +#include <linux/cpuset.h> struct exec_domain ;
@@ -461,6 +462,13 @@unsigned long ptrace_message;siginfo_t *last_siginfo;/* For ptrace use. */ + +/* cpuset info */ +#ifdef CONFIG_CPUSETS +struct cpuset * cpuset; +unsigned long cpus_wanted; +spinlock_t cpuset_attach_lock; +#endif };extern void __put_task_struct (struct task_struct *tsk);
init/main.c +5 -0
@@ -84,6 +84,7 @@extern void free_initmem (void );extern void populate_rootfs (void );extern void driver_init (void ); +extern void cpusets_init (void );#ifdef CONFIG_TC extern void tc_init (void );
@@ -452,6 +453,10 @@#if defined(CONFIG_SYSVIPC )ipc_init ();#endif +#ifdef CONFIG_CPUSETS +cpusets_init (); +#endif +check_bugs ();printk ("POSIX conformance testing by UNIFIX\n" );
kernel/Makefile +2 -0
@@ -19,6 +19,8 @@ obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o obj-$(CONFIG_COMPAT) += compat.o obj-$(CONFIG_IKCONFIG) += configs.o +obj-$(CONFIG_CPUSETS) += cpuset.o + # files to be removed upon make clean clean-files := ikconfig.h
kernel/exit.c +14 -0
@@ -54,6 +54,19 @@BUG_ON (p->state <TASK_ZOMBIE ); + +#ifdef CONFIG_CPUSETS +spin_lock (&p->cpuset_attach_lock); +release_cpuset (p->cpuset); + +/* mark that this process's cpuset has already been released +* another process might still try to cpuset_attach this process +*/ + p->cpuset =NULL ; +spin_unlock (&p->cpuset_attach_lock); +#endif /* CONFIG_CPUSETS */ + +atomic_dec (&p->user->processes);spin_lock (&p->proc_lock); proc_dentry =proc_pid_unhash (p);
@@ -85,6 +98,7 @@spin_unlock (&p->proc_lock);proc_pid_flush (proc_dentry);release_thread (p); +put_task_struct (p); }
kernel/fork.c +9 -0
@@ -31,6 +31,10 @@#include <linux/ptrace.h> #include <linux/mount.h> +#ifdef CONFIG_CPUSETS +#include <linux/cpuset.h> +#endif +#include <asm/pgtable.h> #include <asm/pgalloc.h> #include <asm/uaccess.h>
@@ -1008,6 +1012,11 @@if (p->ptrace &PT_PTRACED )__ptrace_link (p, current->parent); +#ifdef CONFIG_CPUSETS +use_cpuset (p->cpuset); +#endif + +attach_pid (p,PIDTYPE_PID , p->pid);if (thread_group_leader (p)) {attach_pid (p,PIDTYPE_TGID , p->tgid);
kernel/sched.c +12 -0
@@ -35,6 +35,10 @@#include <linux/cpu.h> #include <linux/percpu.h> +#ifdef CONFIG_CPUSETS +#include <linux/cpuset.h> +#endif +#ifdef CONFIG_NUMA #define cpu_to_node_mask (cpu) node_to_cpumask(cpu_to_node(cpu))#else
@@ -1954,7 +1958,11 @@ !capable(CAP_SYS_NICE)) goto out_unlock; +#ifdef CONFIG_CPUSETS + retval =cpuset_setaffinity (p, new_mask); +#else retval =set_cpus_allowed (p, new_mask); +#endif out_unlock:put_task_struct (p);
@@ -1987,7 +1995,11 @@ goto out_unlock; retval =0 ; +#ifdef CONFIG_CPUSETS + mask = p->cpus_wanted; +#else cpus_and (mask, p->cpus_allowed, cpu_online_map); +#endif out_unlock:read_unlock (&tasklist_lock);
include/linux/cpuset_types.h +39 -0
@@ -0,0 +1,39 @@ +#ifndef _LINUX_CPUSET_TYPES_H +#define _LINUX_CPUSET_TYPES_H + + +struct cpuset { +cpuset_t id; +int flags; +int has_been_attached; + +/* bitmask of the cpus present in this cpuset */ +cpumask_t cpus_allowed; + +/* bitmask of the cpus reserved in this cpuset */ +cpumask_t cpus_reserved; + +/* bitmask of the cpus reserved with CPUSET_STRICT */ +cpumask_t cpus_strictly_reserved; + +struct cpuset * parent; +struct list_head list;/* for the whole list */ + +struct list_head children; +struct list_head brothers; + +/* overall users (processes + children) */ +/* will be replaced by atomic_t in the future */ +atomic_t count; + +spinlock_t attach_lock; + +/* owner */ +uid_t uid; +uid_t suid; + + +}; + + +#endif
include/linux/cpuset.h +25 -0
@@ -0,0 +1,25 @@ +/* +* BULL cpuset interface +*/ + +#ifndef _LINUX_CPUSET_H +#define _LINUX_CPUSET_H + +typedef unsigned int cpuset_t ; + +#define CPUSET_STRICT 0x00000001 +#define CPUSET_AUTOCLEAN 0x00000002 + +#ifdef __KERNEL__ + +extern struct cpuset top_cpuset; + +void use_cpuset (struct cpuset *); +void release_cpuset (struct cpuset *); + +struct task_struct ; +int cpuset_setaffinity (struct task_struct * task,unsigned long mask); + +#endif /* __KERNEL__ */ + +#endif /* _LINUX_CPUSET_H */
kernel/cpuset.c +748 -0
748 lines not on this page · raw message
Simon,
Could you please make it VERY clear that the system call numbers the patch is using are NOT OFFICIAL numbers and are therefore likely to change and/or collide unless and until they get accepted into the official tree?
BTW: What do cpusets provide that couldn't be done with user-level tools on top of the existing sched_setaffinity() system call?
Thanks,
--david
Quoted 2 lines · David Mosberger
BTW: What do cpusets provide that couldn't be done with user-level tools on top of the existing
sched_setaffinity()system call?
You've got one point here. As the cpumasks are inherited, you'd just have to make sure that a user cannot escape his cpuset by e.g. not allowing the mask to get bigger than the mask of the parent process (which can be owned by root, thus unchangeable). The management of the cpusets can then be taken over by a user space daemon.
Erich
Quoted 6 lines · David Mosberger
Simon,
Could you please make it VERY clear that the system call numbers the patch is using are NOT OFFICIAL numbers and are therefore likely to change and/or collide unless and until they get accepted into the official tree?
OK, I've updated http://www.bullopensource.org/cpuset/ to highlight this, and also added the missing userland cpuset.[ch] files, plus some more documentation.
As suggested by Paul, maybe we'll eventually make the syscalls disappear and replace them by some /proc interaction.
Simon.