[PATCH] procfs: export context switch counts in /proc/*/stat

From: David Wragg
Date: Mon Dec 18 2006 - 18:51:49 EST


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@xxxxxxxxx>

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;


-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/