次の例は、実行可能ファイルDプログラムcswpercpu.d
を示しています。 このプログラムでは、タイムスタンプが表示され、コンテキスト・スイッチの数、CPUごと、およびすべてのCPUsの合計がCPU数または"total"
とともに1秒当たりに1回出力されます:
#!/usr/sbin/dtrace -qs /* cswpercpu.d -- Print number of context switches per CPU once per second */ #pragma D option quiet dtrace:::BEGIN { /* Print the header */ 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
コンテキスト切替えに費やした合計時間と、コンテキスト切替えごとの平均時間を試すことができます。 たとえば、sched:::off-cpu
プローブのアクションでtimestamp
の値にスレッド・ローカル変数を初期化し、この値をsched:::on-cpu
のアクションのtimestamp
の値から差し引くことによって、試験を実行できます。 sum()
集計関数およびavg()
集計関数をそれぞれ使用します。