### 2.7.10 Solution to Exercise and Example: Counting Context Switches on a System

The following example shows the executable D program `cswpercpu.d`. The program displays a timestamp and prints the number of context switches, per-CPU, and the total for all CPUs, once per second, together with the CPU number or `"total"`:

```#!/usr/sbin/dtrace -qs

/* cswpercpu.d -- Print number of context switches per CPU once per second */

#pragma D option quiet

dtrace:::BEGIN
{
printf("%-25s %5s %15s", "Timestamp", "CPU", "Ncsw");
}

sched:::on-cpu
{
/* Convert the cpu number to a string */
cpustr = lltostr(cpu);
/* Increment the counters */
@n[cpustr] = count();
@n["total"] = count();
}

profile:::tick-1sec
{
/* Print the date and time before the first result */
printf("\n%-25Y ", walltimestamp);

/* Print the aggregated counts for each CPU and the total for all CPUs */
printa("%5s %@15d\n                          ", @n);

/* Reset the aggregation */
clear(@n);
}```
```# `chmod +x cswpercpu.d`
# `./cswpercpu.d`
Timestamp                   CPU            Ncsw
2013 Nov  6 20:47:26          1             148
0             155
3             200
2             272
total             775

2013 Nov  6 20:47:27          1             348
0             364
3             364
2             417
total            1493

2013 Nov  6 20:47:28          3              47
1             100
0             121
2             178
total             446
`^C````

You might want to experiment with aggregating the total time that is spent context switching and the average time per context switch. For example, you can experiment by initializing a thread-local variable to the value of `timestamp` in the action to a `sched:::off-cpu` probe, and subtracting this value from the value of `timestamp` in the action to `sched:::on-cpu`. Use the `sum()` and `avg()` aggregation functions, respectively.