LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Roland McGrath <roland@redhat.com>
To: Linus Torvalds <torvalds@linux-foundation.org>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: Vinay Sridhar <vinay@linux.vnet.ibm.com>,
	Ulrich Drepper <drepper@redhat.com>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] RUSAGE_THREAD
Date: Fri, 18 Jan 2008 17:14:18 -0800 (PST)	[thread overview]
Message-ID: <20080119011418.850DD26F9FD@magilla.localdomain> (raw)
In-Reply-To: Vinay Sridhar's message of  Thursday, 17 January 2008 13:57:05 +0530 <1200558425.5992.17.camel@srivinay.in.ibm.com>


This adds the RUSAGE_THREAD option for the getrusage system call.
Solaris calls this RUSAGE_LWP and uses the same value (1).
That name is not a natural one for Linux, but we keep it as an alias.

Signed-off-by: Roland McGrath <roland@redhat.com>
---
 include/linux/resource.h |    2 ++
 kernel/sys.c             |   31 ++++++++++++++++++++++---------
 2 files changed, 24 insertions(+), 9 deletions(-)

diff --git a/include/linux/resource.h b/include/linux/resource.h
index ae13db7..02b3377 100644
--- a/include/linux/resource.h
+++ b/include/linux/resource.h
@@ -19,6 +19,8 @@ struct task_struct;
 #define	RUSAGE_SELF	0
 #define	RUSAGE_CHILDREN	(-1)
 #define RUSAGE_BOTH	(-2)		/* sys_wait4() uses this */
+#define	RUSAGE_THREAD	1		/* only the calling thread */
+#define	RUSAGE_LWP	RUSAGE_THREAD	/* Solaris name for same */
 
 struct	rusage {
 	struct timeval ru_utime;	/* user time used */
diff --git a/kernel/sys.c b/kernel/sys.c
index d1fe71e..6a62bc4 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1554,6 +1554,19 @@ out:
  *
  */
 
+static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r,
+				     cputime_t *utimep, cputime_t *stimep)
+{
+	*utimep = cputime_add(*utimep, t->utime);
+	*stimep = cputime_add(*stimep, t->stime);
+	r->ru_nvcsw += t->nvcsw;
+	r->ru_nivcsw += t->nivcsw;
+	r->ru_minflt += t->min_flt;
+	r->ru_majflt += t->maj_flt;
+	r->ru_inblock += task_io_get_inblock(t);
+	r->ru_oublock += task_io_get_oublock(t);
+}
+
 static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
 {
 	struct task_struct *t;
@@ -1563,6 +1576,11 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
 	memset((char *) r, 0, sizeof *r);
 	utime = stime = cputime_zero;
 
+	if (who == RUSAGE_THREAD) {
+		accumulate_thread_rusage(p, r, &utime, &stime);
+		goto out;
+	}
+
 	rcu_read_lock();
 	if (!lock_task_sighand(p, &flags)) {
 		rcu_read_unlock();
@@ -1595,14 +1613,7 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
 			r->ru_oublock += p->signal->oublock;
 			t = p;
 			do {
-				utime = cputime_add(utime, t->utime);
-				stime = cputime_add(stime, t->stime);
-				r->ru_nvcsw += t->nvcsw;
-				r->ru_nivcsw += t->nivcsw;
-				r->ru_minflt += t->min_flt;
-				r->ru_majflt += t->maj_flt;
-				r->ru_inblock += task_io_get_inblock(t);
-				r->ru_oublock += task_io_get_oublock(t);
+				accumulate_thread_rusage(t, r, &utime, &stime);
 				t = next_thread(t);
 			} while (t != p);
 			break;
@@ -1614,6 +1625,7 @@ static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
 	unlock_task_sighand(p, &flags);
 	rcu_read_unlock();
 
+out:
 	cputime_to_timeval(utime, &r->ru_utime);
 	cputime_to_timeval(stime, &r->ru_stime);
 }
@@ -1627,7 +1639,8 @@ int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
 
 asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
 {
-	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
+	if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
+	    who != RUSAGE_THREAD)
 		return -EINVAL;
 	return getrusage(current, who, ru);
 }

  parent reply	other threads:[~2008-01-19  1:14 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-01-17  8:27 [RFC] Per-thread getrusage Vinay Sridhar
2008-01-17 15:42 ` Ulrich Drepper
2008-01-19  1:14 ` Roland McGrath
2008-01-19  1:14 ` Roland McGrath [this message]
2008-01-19  6:21   ` [PATCH] RUSAGE_THREAD Ulrich Drepper
2008-01-21  9:55   ` Christoph Hellwig
2008-01-26  7:23   ` Michael Kerrisk
2008-01-28  5:52 ` [RFC] Per-thread getrusage Andrew Morton
2008-01-28  7:48   ` Pavel Emelyanov
2008-01-28  9:10     ` Andrew Morton
2008-01-28  9:38       ` Pavel Emelyanov
2008-01-28  9:45         ` Andrew Morton
2008-01-28  9:57           ` Pavel Emelyanov
2008-01-28 20:43             ` Eric W. Biederman
2008-01-28 21:57               ` Andrew Morton
2008-01-29  8:17                 ` Pavel Emelyanov
2008-01-29  8:29               ` Pavel Emelyanov
2008-01-28  8:24   ` Sripathi Kodi
2008-01-28  9:22     ` Andrew Morton
2008-02-05  5:55 [PATCH] RUSAGE_THREAD Sripathi Kodi
2008-02-22 17:43 ` Michael Kerrisk
2008-02-27 11:41   ` Sripathi Kodi
2008-03-10 15:33     ` Michael Kerrisk

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=20080119011418.850DD26F9FD@magilla.localdomain \
    --to=roland@redhat.com \
    --cc=akpm@linux-foundation.org \
    --cc=drepper@redhat.com \
    --cc=torvalds@linux-foundation.org \
    --cc=vinay@linux.vnet.ibm.com \
    --subject='Re: [PATCH] RUSAGE_THREAD' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).