1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
/*
* Oracle Linux DTrace.
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
*/
/*
* NAME
* cswpercpu.d - print the number of context switches per CPU per second
*
* SYNOPSIS
* sudo dtrace -s cswpercpu.d
*
* DESCRIPTION
* Every second, print the CPU id, the number of context switches each
* CPU performed, plus the total number of context switches executed
* across all the CPUs. For each info block, include a time stamp.
*
* NOTES
* - The script needs to be terminated with ctrl-C. In case the
* script is running in the background, get it to run in the
* foreground first by using the fg command and then use ctrl-C
* to terminate the process. Otherwise, typing in ctrl-C will do.
*
* - The results are stored in an aggregation called cswpersec.
* Every second, the results are printed with printa() and the
* aggregation is cleared.
*
* - In addition to using the CPU ID as a key in the cswpersec
* aggregation, also the string "total" is used. This entry
* is always printed last, because by default, printa() prints
* the results sorted by the value. The total count for any of
* the CPU Ids is always equal or less than "total".
*/
/*
* To avoid that the carefully crafted output is mixed with the
* default output by the dtrace command, enable quiet mode.
*/
#pragma D option quiet
/*
* Print the header.
*/
BEGIN
{
printf("%-20s %5s %15s", "Timestamp", "CPU", "#csw");
}
/*
* Fires when a process is scheduled to run on a CPU.
*/
sched:::on-cpu
{
/*
* Convert the CPU ID to a string. This needs to be done because
* key "total" is a string.
*/
this->cpustr = lltostr(cpu);
/*
* Update the count.
*/
@cswpersec[this->cpustr] = count();
@cswpersec["total"] = count();
}
/*
* Fires every second.
*/
profile:::tick-1sec
{
/*
* Print the date and time first
*/
printf("\n%-20Y ", walltimestamp);
/*
* Print the aggregated counts for each CPU and the total for all CPUs.
* Use some formatting magic to get a special table lay-out.
*/
printa("%5s %@15d\n ", @cswpersec);
/*
* Reset the aggregation.
*/
clear(@cswpersec);
}
|