[PATCH] RUSAGE_THREAD

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Roland McGrath
Date: Friday, January 18, 2008 - 6:14 pm

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);
 }
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[RFC] Per-thread getrusage, Vinay Sridhar, (Thu Jan 17, 1:27 am)
Re: [RFC] Per-thread getrusage, Ulrich Drepper, (Thu Jan 17, 8:42 am)
Re: [RFC] Per-thread getrusage, Roland McGrath, (Fri Jan 18, 6:14 pm)
[PATCH] RUSAGE_THREAD, Roland McGrath, (Fri Jan 18, 6:14 pm)
Re: [PATCH] RUSAGE_THREAD, Ulrich Drepper, (Fri Jan 18, 11:21 pm)
Re: [PATCH] RUSAGE_THREAD, Christoph Hellwig, (Mon Jan 21, 2:55 am)
Re: [PATCH] RUSAGE_THREAD, Michael Kerrisk, (Sat Jan 26, 12:23 am)
Re: [RFC] Per-thread getrusage, Andrew Morton, (Sun Jan 27, 10:52 pm)
Re: [RFC] Per-thread getrusage, Pavel Emelyanov, (Mon Jan 28, 12:48 am)
Re: [RFC] Per-thread getrusage, Sripathi Kodi, (Mon Jan 28, 1:24 am)
Re: [RFC] Per-thread getrusage, Andrew Morton, (Mon Jan 28, 2:10 am)
Re: [RFC] Per-thread getrusage, Andrew Morton, (Mon Jan 28, 2:22 am)
Re: [RFC] Per-thread getrusage, Pavel Emelyanov, (Mon Jan 28, 2:38 am)
Re: [RFC] Per-thread getrusage, Andrew Morton, (Mon Jan 28, 2:45 am)
Re: [RFC] Per-thread getrusage, Pavel Emelyanov, (Mon Jan 28, 2:57 am)
Re: [RFC] Per-thread getrusage, Eric W. Biederman, (Mon Jan 28, 1:43 pm)
Re: [RFC] Per-thread getrusage, Andrew Morton, (Mon Jan 28, 2:57 pm)
Re: [RFC] Per-thread getrusage, Pavel Emelyanov, (Tue Jan 29, 1:17 am)
Re: [RFC] Per-thread getrusage, Pavel Emelyanov, (Tue Jan 29, 1:29 am)