public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Wragg <david@wragg.org>
To: linux-kernel@vger.kernel.org
Subject: [PATCH] procfs: export context switch counts in /proc/*/stat
Date: Mon, 18 Dec 2006 23:50:08 +0000	[thread overview]
Message-ID: <87zm9k228f.fsf@wragg.org> (raw)

The kernel already maintains context switch counts for each task, and
exposes them through getrusage(2).  These counters can also be used
more generally to track which processes on the system are active
(i.e. getting scheduled to run), but getrusage is too constrained to
use it in that way.

This patch (against 2.6.19/2.6.19.1) adds the four context switch
values (voluntary context switches, involuntary context switches, and
the same values accumulated from terminated child processes) to the
end of /proc/*/stat, similarly to min_flt, maj_flt and the time used
values.

Signed-off-by: David Wragg <david@wragg.org>

diff -uprN --exclude='*.o' --exclude='*~' --exclude='.*' linux-2.6.19.1/fs/proc/array.c linux-2.6.19.1.build/fs/proc/array.c
--- linux-2.6.19.1/fs/proc/array.c	2006-12-18 14:35:36.000000000 +0000
+++ linux-2.6.19.1.build/fs/proc/array.c	2006-12-18 14:43:21.000000000 +0000
@@ -327,6 +327,8 @@ static int do_task_stat(struct task_stru
 	unsigned long cmin_flt = 0, cmaj_flt = 0;
 	unsigned long  min_flt = 0,  maj_flt = 0;
 	cputime_t cutime, cstime, utime, stime;
+	unsigned long cnvcsw = 0, cnivcsw = 0;
+	unsigned long  nvcsw = 0,  nivcsw = 0;
 	unsigned long rsslim = 0;
 	char tcomm[sizeof(task->comm)];
 	unsigned long flags;
@@ -369,6 +371,8 @@ static int do_task_stat(struct task_stru
 		cmaj_flt = sig->cmaj_flt;
 		cutime = sig->cutime;
 		cstime = sig->cstime;
+		cnvcsw = sig->cnvcsw;
+		cnivcsw = sig->cnivcsw;
 		rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
 
 		/* add up live thread stats at the group level */
@@ -379,6 +383,8 @@ static int do_task_stat(struct task_stru
 				maj_flt += t->maj_flt;
 				utime = cputime_add(utime, t->utime);
 				stime = cputime_add(stime, t->stime);
+				nvcsw += t->nvcsw;
+				nivcsw += t->nivcsw;
 				t = next_thread(t);
 			} while (t != task);
 
@@ -386,6 +392,8 @@ static int do_task_stat(struct task_stru
 			maj_flt += sig->maj_flt;
 			utime = cputime_add(utime, sig->utime);
 			stime = cputime_add(stime, sig->stime);
+			nvcsw += sig->nvcsw;
+			nivcsw += sig->nivcsw;
 		}
 
 		sid = sig->session;
@@ -404,6 +412,8 @@ static int do_task_stat(struct task_stru
 		maj_flt = task->maj_flt;
 		utime = task->utime;
 		stime = task->stime;
+		nvcsw = task->nvcsw;
+		nivcsw = task->nivcsw;		
 	}
 
 	/* scale priority and nice values from timeslices to -20..20 */
@@ -420,7 +430,7 @@ static int do_task_stat(struct task_stru
 
 	res = sprintf(buffer,"%d (%s) %c %d %d %d %d %d %lu %lu \
 %lu %lu %lu %lu %lu %ld %ld %ld %ld %d 0 %llu %lu %ld %lu %lu %lu %lu %lu \
-%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu\n",
+%lu %lu %lu %lu %lu %lu %lu %lu %d %d %lu %lu %llu %lu %lu %lu %lu\n",
 		task->pid,
 		tcomm,
 		state,
@@ -465,7 +475,12 @@ static int do_task_stat(struct task_stru
 		task_cpu(task),
 		task->rt_priority,
 		task->policy,
-		(unsigned long long)delayacct_blkio_ticks(task));
+		(unsigned long long)delayacct_blkio_ticks(task),
+		nvcsw,
+		cnvcsw,
+		nivcsw,
+		cnivcsw);
+                
 	if(mm)
 		mmput(mm);
 	return res;



             reply	other threads:[~2006-12-18 23:51 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-12-18 23:50 David Wragg [this message]
2006-12-19  6:39 ` [PATCH] procfs: export context switch counts in /proc/*/stat Benjamin LaHaise
2006-12-19 11:47   ` David Wragg
  -- strict thread matches above, loose matches on Subject: below --
2006-12-20  5:40 Albert Cahalan
2006-12-20 13:20 ` David Wragg
2006-12-20 13:48   ` Arjan van de Ven
2006-12-20 14:38     ` David Wragg
2006-12-20 14:51       ` Arjan van de Ven
2006-12-20 15:13         ` David Wragg
2006-12-20 17:36   ` Albert Cahalan
2006-12-24  1:40     ` David Wragg
2006-12-21  6:02 Al Boldi

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=87zm9k228f.fsf@wragg.org \
    --to=david@wragg.org \
    --cc=linux-kernel@vger.kernel.org \
    /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