procfs: export context switch counts in /proc/*/stat
PATCH 1/1 procfs: export context switch counts in /proc/*/stat
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.
fs/proc/array.c +17 -2
@@ -327,6 +327,8 @@ static int do_task_stat(struct task_struunsigned 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_strutask_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;
Quoted 5 lines · David Wragg
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 tomin_flt,maj_fltand the time used values.
Please put these into new files, as the stat files in /proc are horribly overloaded and have always been somewhat problematic when it comes to changing how things are reported due to internal changes to the kernel. Cheers,
-ben
Signature · 2 lines
"Time is of no importance, Mr. President, only life is important." Don't Email: <[email protected]>.
Quoted 11 lines · Benjamin LaHaise
Quoted 5 lines · David Wragg
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 tomin_flt,maj_fltand the time used values.Please put these into new files, as the stat files in /proc are horribly overloaded and have always been somewhat problematic when it comes to changing how things are reported due to internal changes to the kernel. Cheers,
The delay accounting value was added to the end of /proc/pid/stat back in July without discussion, so I assumed this approach was still considered satisfactory.
Putting just these four values into a new file would seem a little odd, since they have a lot in common with the other getrusage values that are already in /proc/pid/stat. One possibility is to add /proc/pid/rusage, mirroring the full struct rusage in text form, since struct rusage is already part of the kernel ABI (though Linux doesn't fill in half of the values).
Or perhaps it makes sense to reorganize all the values from /proc/pid/stat and its siblings into a sysfs-like one-value-per-file structure, though that might introduce atomicity and efficiency issues (calculating some of the values involves iterating over the threads in the process; with everything in one file, these loops are folded together).
Any thoughts?
David