Skip to contentSkip to conversation

2.5: proc interface for setting task affinity

PATCH New Robert Love 1 participant 1 message Feb 27, 2002

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 2.5: proc interface for setting task affinity

The attached patch implements a proc-based user interface for setting and retrieving a task's CPU affinity (task->cpus_allowed).

To retrieve <pid>'s affinity:

cat /proc/<pid>/affinity

and to set it:

echo n > /proc/<pid>/affinity

Essentially the debate is over, I have been fairly well convinced a syscall solution is superior. I do note, however, the beauty in the above solution. I have posted a syscall implementation as well, in another thread, but post this for completeness.

This patch uses set_cpus_allowed in Ingo's new scheduler to do the heavy work. It is based off my previous patch which borrowed heavily from Ingo's previous affinity patch.

Security is enforced: user must possess CAP_SYS_NICE or be the same uid or euid of the pid in question. This patch also implements a proc_write method for the proc_pid code - it adds to the patches size, but the method is reusable.

Patch is against 2.5.6-pre1. Enjoy,

Robert Love

Documentation/filesystems/proc.txt +7 -1
diff -urN linus/Documentation/filesystems/proc.txt linux/Documentation/filesystems/proc.txt
@@ -118,6 +118,7 @@
 Table 1-1: Process specific entries in /proc 
 ..............................................................................
  File    Content                                        
+ affinity Writable bitmask of allowed CPUs			(2.5)(smp)
  cmdline Command line arguments                         
  cpu	 Current and last cpu in wich it was executed		(2.4)(smp)
  cwd	 Link to the current working directory
@@ -159,12 +160,17 @@
   CapPrm: 0000000000000000 
   CapEff: 0000000000000000 
 
-
 This shows you nearly the same information you would get if you viewed it with
 the ps  command.  In  fact,  ps  uses  the  proc  file  system  to  obtain its
 information. The  statm  file  contains  more  detailed  information about the
 process memory usage. Its seven fields are explained in Table 1-2.
 
+For writable entries, such as affinity:
+
+ >echo 1 > /proc/PID/affinity
+
+To set the affinity of PID to 0x00000001 (=> only CPU0).
+
 
 Table 1-2: Contents of the statm files 
 ..............................................................................
fs/proc/array.c +39 -4

52 lines not on this page · raw message

fs/proc/base.c +45 -0

77 lines not on this page · raw message

include/linux/capability.h +1 -0
diff -urN linus/include/linux/capability.h linux/include/linux/capability.h
@@ -242,6 +242,7 @@
 /* Allow use of FIFO and round-robin (realtime) scheduling on own
    processes and setting the scheduling algorithm used by another
    process. */
+/* Allow setting CPU affinity */
 
 #define CAP_SYS_NICE         23
 
include/linux/proc_fs.h +2 -1
diff -urN linus/include/linux/proc_fs.h linux/include/linux/proc_fs.h
@@ -208,9 +208,10 @@
 struct proc_inode {
 	struct task_struct *task;
 	int type;
-	union {
+	struct {
 		int (*proc_get_link)(struct inode *, struct dentry **, struct vfsmount **);
 		int (*proc_read)(struct task_struct *task, char *page);
+		int (*proc_write)(struct task_struct *task, char *page, size_t bytes);
 	} op;
 	struct file *file;
 	struct proc_dir_entry *pde;