[LWN Logo]
[LWN.net]
From:	 Ingo Molnar <mingo@elte.hu>
To:	 <linux-kernel@vger.kernel.org>
Subject: [patch] sched_[set|get]_affinity() syscall, 2.4.15-pre9
Date:	 Thu, 22 Nov 2001 09:59:47 +0100 (CET)
Cc:	 <linux-smp@vger.kernel.org>


the attached set-affinity-A1 patch is relative to the scheduler
fixes/cleanups in 2.4.15-pre9. It implements the following two
new system calls:

 asmlinkage int sys_sched_set_affinity(pid_t pid, unsigned int mask_len,
    unsigned long *new_mask_ptr);

 asmlinkage int sys_sched_get_affinity(pid_t pid, unsigned int
    *user_mask_len_ptr, unsigned long *user_mask_ptr);

as a testcase, softirq.c is updated to use this mechanizm, plus see the
attached loop_affine.c code.

the sched_set_affinity() syscall also ensures that the target process will
run on the right CPU (or CPUs).

I think this interface is the right way to expose user-selectable affinity
to user-space - there are more complex affinity interfaces in existence,
but i believe that the discovery of actual caching hierarchy is and should
be up to a different mechanizm, i dont think it should be mixed into the
affinity syscalls. Using a mask of linear CPU IDs is IMO sufficient to
express user-space affinity wishes.

There are no security issues wrt. cpus_allowed, so these syscalls are
available to every process. (there are permission restrictions of course,
similar to those of existing scheduler syscalls.)

sched_get_affinity(pid, &mask_len, NULL) can be used to query the kernel's
supported CPU bitmask length. This should help us in achieving a stable
libc interface once we get over the 32/64 CPUs limit.

the attached loop_affine.c code tests both syscalls:

 mars:~> ./loop_affine
 current process's affinity: 4 bytes mask, value 000000ff.
 trying to set process: affinity to 00000001.
 current process's affinity: 4 bytes mask, value 00000001.
 speed: 2162052 loops.
 speed: 2162078 loops.
 [...]

i've tested the patch on both SMP and UP systems. On UP the syscalls are
pretty pointless, but they show that the internal state of the scheduler
folds nicely into the UP case as well:

 mars:~> ./loop_affine
 current process's affinity: 4 bytes mask, value 00000001.
 trying to set process: affinity to 00000001.
 current process's affinity: 4 bytes mask, value 00000001.
 speed: 2160880 loops.
 speed: 2160511 loops.
 [...]

comments? Is there any reason to do a more complex interface than this?

	Ingo
[2. text/plain; set-affinity-2.4.15-A1]...

[3. text/plain; loop_affine.c]...