Skip to content

Commit b663a79

Browse files
Maxim UvarovLinus Torvalds
authored andcommitted
taskstats: add context-switch counters
Make available to the user the following task and process performance statistics: * Involuntary Context Switches (task_struct->nivcsw) * Voluntary Context Switches (task_struct->nvcsw) Statistics information is available from: 1. taskstats interface (Documentation/accounting/) 2. /proc/PID/status (task only). This data is useful for detecting hyperactivity patterns between processes. [akpm@linux-foundation.org: cleanup] Signed-off-by: Maxim Uvarov <muvarov@ru.mvista.com> Cc: Shailabh Nagar <nagar@watson.ibm.com> Cc: Balbir Singh <balbir@in.ibm.com> Cc: Jay Lan <jlan@engr.sgi.com> Cc: Jonathan Lim <jlim@sgi.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent a6c15c2 commit b663a79

5 files changed

Lines changed: 41 additions & 3 deletions

File tree

‎Documentation/accounting/getdelays.c‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ char name[100];
4949
int dbg;
5050
int print_delays;
5151
int print_io_accounting;
52+
int print_task_context_switch_counts;
5253
__u64 stime, utime;
5354

5455
#define PRINTF(fmt, arg...) { \
@@ -195,7 +196,7 @@ void print_delayacct(struct taskstats *t)
195196
"IO %15s%15s\n"
196197
" %15llu%15llu\n"
197198
"MEM %15s%15s\n"
198-
" %15llu%15llu\n\n",
199+
" %15llu%15llu\n"
199200
"count", "real total", "virtual total", "delay total",
200201
t->cpu_count, t->cpu_run_real_total, t->cpu_run_virtual_total,
201202
t->cpu_delay_total,
@@ -204,6 +205,14 @@ void print_delayacct(struct taskstats *t)
204205
"count", "delay total", t->swapin_count, t->swapin_delay_total);
205206
}
206207

208+
void task_context_switch_counts(struct taskstats *t)
209+
{
210+
printf("\n\nTask %15s%15s\n"
211+
" %15lu%15lu\n",
212+
"voluntary", "nonvoluntary",
213+
t->nvcsw, t->nivcsw);
214+
}
215+
207216
void print_ioacct(struct taskstats *t)
208217
{
209218
printf("%s: read=%llu, write=%llu, cancelled_write=%llu\n",
@@ -235,7 +244,7 @@ int main(int argc, char *argv[])
235244
struct msgtemplate msg;
236245

237246
while (1) {
238-
c = getopt(argc, argv, "diw:r:m:t:p:vl");
247+
c = getopt(argc, argv, "qdiw:r:m:t:p:vl");
239248
if (c < 0)
240249
break;
241250

@@ -248,6 +257,10 @@ int main(int argc, char *argv[])
248257
printf("printing IO accounting\n");
249258
print_io_accounting = 1;
250259
break;
260+
case 'q':
261+
printf("printing task/process context switch rates\n");
262+
print_task_context_switch_counts = 1;
263+
break;
251264
case 'w':
252265
logfile = strdup(optarg);
253266
printf("write to file %s\n", logfile);
@@ -389,6 +402,8 @@ int main(int argc, char *argv[])
389402
print_delayacct((struct taskstats *) NLA_DATA(na));
390403
if (print_io_accounting)
391404
print_ioacct((struct taskstats *) NLA_DATA(na));
405+
if (print_task_context_switch_counts)
406+
task_context_switch_counts((struct taskstats *) NLA_DATA(na));
392407
if (fd) {
393408
if (write(fd, NLA_DATA(na), na->nla_len) < 0) {
394409
err(1,"write error\n");

‎Documentation/accounting/taskstats-struct.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ There are three different groups of fields in the struct taskstats:
2222
/* Extended accounting fields end */
2323
Their values are collected if CONFIG_TASK_XACCT is set.
2424

25+
4) Per-task and per-thread context switch count statistics
26+
2527
Future extension should add fields to the end of the taskstats struct, and
2628
should not change the relative position of each field within the struct.
2729

@@ -158,4 +160,8 @@ struct taskstats {
158160

159161
/* Extended accounting fields end */
160162

163+
4) Per-task and per-thread statistics
164+
__u64 nvcsw; /* Context voluntary switch counter */
165+
__u64 nivcsw; /* Context involuntary switch counter */
166+
161167
}

‎fs/proc/array.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,15 @@ static inline char *task_cap(struct task_struct *p, char *buffer)
289289
cap_t(p->cap_effective));
290290
}
291291

292+
static inline char *task_context_switch_counts(struct task_struct *p,
293+
char *buffer)
294+
{
295+
return buffer + sprintf(buffer, "voluntary_ctxt_switches:\t%lu\n"
296+
"nonvoluntary_ctxt_switches:\t%lu\n",
297+
p->nvcsw,
298+
p->nivcsw);
299+
}
300+
292301
int proc_pid_status(struct task_struct *task, char * buffer)
293302
{
294303
char * orig = buffer;
@@ -307,6 +316,7 @@ int proc_pid_status(struct task_struct *task, char * buffer)
307316
#if defined(CONFIG_S390)
308317
buffer = task_show_regs(task, buffer);
309318
#endif
319+
buffer = task_context_switch_counts(task, buffer);
310320
return buffer - orig;
311321
}
312322

‎include/linux/taskstats.h‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
*/
3232

3333

34-
#define TASKSTATS_VERSION 4
34+
#define TASKSTATS_VERSION 5
3535
#define TS_COMM_LEN 32 /* should be >= TASK_COMM_LEN
3636
* in linux/sched.h */
3737

@@ -149,6 +149,9 @@ struct taskstats {
149149
__u64 read_bytes; /* bytes of read I/O */
150150
__u64 write_bytes; /* bytes of write I/O */
151151
__u64 cancelled_write_bytes; /* bytes of cancelled write I/O */
152+
153+
__u64 nvcsw; /* voluntary_ctxt_switches */
154+
__u64 nivcsw; /* nonvoluntary_ctxt_switches */
152155
};
153156

154157

‎kernel/taskstats.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ static int fill_pid(pid_t pid, struct task_struct *tsk,
196196

197197
/* fill in basic acct fields */
198198
stats->version = TASKSTATS_VERSION;
199+
stats->nvcsw = tsk->nvcsw;
200+
stats->nivcsw = tsk->nivcsw;
199201
bacct_add_tsk(stats, tsk);
200202

201203
/* fill in extended acct fields */
@@ -242,6 +244,8 @@ static int fill_tgid(pid_t tgid, struct task_struct *first,
242244
*/
243245
delayacct_add_tsk(stats, tsk);
244246

247+
stats->nvcsw += tsk->nvcsw;
248+
stats->nivcsw += tsk->nivcsw;
245249
} while_each_thread(first, tsk);
246250

247251
unlock_task_sighand(first, &flags);

0 commit comments

Comments
 (0)