mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Denis Cheng <crquan@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Al Viro <viro@zeniv.linux.org.uk>,
	Linus Torvalds <torvalds@linux-foundation.org>
Cc: linux-kernel@vger.kernel.org, Eli M Dow <emdow@us.ibm.com>
Subject: [PATCH] PROC_FS: get and set the smp affinity of tasks by read-write /proc/<pid>/smp_affinity
Date: Fri,  4 Jan 2008 15:03:41 +0800	[thread overview]
Message-ID: <1199430221-28417-1-git-send-email-crquan@gmail.com> (raw)

this adds a read-write /proc/<pid>/smp_affinity entry,
just like what /proc/irq/<irq>/smp_affinity does,
so now we can get and set the affinity of tasks by procfs,
this is especially useful used in shell scripts.

this also adds a read-write /proc/<pid>/tasks/<tid>/smp_affinity
for the same purpose.

Cc: Eli M Dow <emdow@us.ibm.com>
Signed-off-by: Denis Cheng <crquan@gmail.com>
---
 fs/proc/base.c |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 57 insertions(+), 0 deletions(-)

diff --git a/fs/proc/base.c b/fs/proc/base.c
index 7411bfb..ca0cbc2 100644
--- a/fs/proc/base.c
+++ b/fs/proc/base.c
@@ -1083,6 +1083,57 @@ static const struct file_operations proc_pid_sched_operations = {
 
 #endif
 
+#ifdef CONFIG_SMP
+static ssize_t smp_affinity_read(struct file *file,
+		char __user *buf, size_t count, loff_t *ppos)
+{
+	cpumask_t mask;
+	char *page;
+	ssize_t length;
+	pid_t pid = pid_nr(proc_pid(file->f_dentry->d_inode));
+
+	length = sched_getaffinity(pid, &mask);
+	if (length < 0)
+		return length;
+
+	if (count > PROC_BLOCK_SIZE)
+		count = PROC_BLOCK_SIZE;
+
+	page = (char *)__get_free_page(GFP_TEMPORARY);
+	if (!page)
+		return -ENOMEM;
+
+	length = cpumask_scnprintf(page, count, mask);
+	length += sprintf(page + length, "\n");
+	length = simple_read_from_buffer(buf, count, ppos, page, length);
+	free_page((unsigned long) page);
+
+	return length;
+}
+
+static ssize_t smp_affinity_write(struct file *file,
+		const char __user *buf, size_t count, loff_t *ppos)
+{
+	cpumask_t mask;
+	int ret;
+	pid_t pid = pid_nr(proc_pid(file->f_dentry->d_inode));
+
+	ret = cpumask_parse_user(buf, count, mask);
+	if (ret < 0)
+		return ret;
+	ret = sched_setaffinity(pid, mask);
+	if (ret < 0)
+		return ret;
+
+	return count;
+}
+
+static const struct file_operations proc_smp_affinity_operations = {
+	.read  = smp_affinity_read,
+	.write = smp_affinity_write,
+};
+#endif
+
 static void *proc_pid_follow_link(struct dentry *dentry, struct nameidata *nd)
 {
 	struct inode *inode = dentry->d_inode;
@@ -2230,6 +2281,9 @@ static const struct pid_entry tgid_base_stuff[] = {
 #ifdef CONFIG_SCHEDSTATS
 	INF("schedstat",  S_IRUGO, pid_schedstat),
 #endif
+#ifdef CONFIG_SMP
+	REG("smp_affinity", S_IRUGO|S_IWUSR, smp_affinity),
+#endif
 #ifdef CONFIG_PROC_PID_CPUSET
 	REG("cpuset",     S_IRUGO, cpuset),
 #endif
@@ -2555,6 +2609,9 @@ static const struct pid_entry tid_base_stuff[] = {
 #ifdef CONFIG_SCHEDSTATS
 	INF("schedstat", S_IRUGO, pid_schedstat),
 #endif
+#ifdef CONFIG_SMP
+	REG("smp_affinity", S_IRUGO|S_IWUSR, smp_affinity),
+#endif
 #ifdef CONFIG_PROC_PID_CPUSET
 	REG("cpuset",    S_IRUGO, cpuset),
 #endif
-- 
1.5.3.5


             reply	other threads:[~2008-01-04  7:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-04  7:03 Denis Cheng [this message]
2008-01-04  7:39 ` Paul Jackson
2008-01-04  7:54 ` Paul Jackson
2008-01-04 15:38   ` rae l
2008-01-04 19:13     ` Paul Jackson
2008-01-10  0:33 ` Andrew Morton
2008-01-10  2:25   ` rae l

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1199430221-28417-1-git-send-email-crquan@gmail.com \
    --to=crquan@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=emdow@us.ibm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=viro@zeniv.linux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome