プロセッサ: CPUとプロセス/スレッド
ps(1)やtop(1)を使うとどのプロセッサがどの程度使われているか、
次のDスクリプトは指定したプロセスに関して10秒間データを収集して、
#!/usr/sbin/dtrace -s
#pragma D option quiet
inline int MAX = 10;
dtrace:::BEGIN
{
start = timestamp;
}
sched:::on-cpu
/pid == $target/
{
self->ts = timestamp;
}
sched:::off-cpu
/self->ts/
{
@[cpu] = sum(timestamp - self->ts);
self->ts = 0;
}
profile:::tick-1sec
/++x == MAX/
{
exit(0);
}
dtrace:::END
{
printf("\nCPU distribution over %d milliseconds:\n\n",
(timestamp - start) / 1000000);
printf("CPU microseconds\n--- ------------\n");
normalize(@, 1000);
printa("%3d %@d\n", @);
}
実行すると次のようにそれぞれの論理コアでどの程度プロセスが実行されていたのかを知ることができます。sched:::on-cpuとsched:::off-cpuでタイムスタンプを計測してその差分を加算していくことでこの計算を行っています。
# ./wrun.sh -p 38674 2> /dev/null CPU distribution over 10007 milliseconds: CPU microseconds --- ------------ 1 93 0 385 2 522 3 1735 #
次のDスクリプトは先ほどのwrun.
#!/usr/sbin/dtrace -s
#pragma D option quiet
sched:::enqueue
{
s[args[0]->td_tid, args[1]->p_pid] = timestamp;
}
sched:::dequeue
/this->start = s[args[0]->td_tid, args[1]->p_pid]/
{
this->time = timestamp - this->start;
@lat_avg[cpu] = avg(this->time);
@lat_max[cpu] = max(this->time);
@lat_min[cpu] = min(this->time);
s[args[0]->td_tid, args[1]->p_pid] = 0;
}
tick-1sec
{
printf("%-8s %-12s %-12s %-12s\n",
"CPU", "AVG(ns)", "MAX(ns)", "MIN(ns)");
printa("%-8d %-@12d %-@12d %-@12d\n",
@lat_avg, @lat_max, @lat_min);
trunc(@lat_avg);
trunc(@lat_max);
trunc(@lat_min);
}
実行すると次のような結果になります。Ctrl-Cで実行を終了させることができます。
# ./lat.d 2> /dev/null CPU AVG(ns) MAX(ns) MIN(ns) 1 18385 18385 18385 3 29818 29818 29818 2 33541 33541 33541 CPU AVG(ns) MAX(ns) MIN(ns) 1 27153 50187 4120 3 82593 82593 82593 2 206782 960847 3738 0 2311469 6928036 2883 CPU AVG(ns) MAX(ns) MIN(ns) 2 3014 3014 3014 CPU AVG(ns) MAX(ns) MIN(ns) 0 2759 2759 2759 CPU AVG(ns) MAX(ns) MIN(ns) 1 1882 1882 1882 3 1958 2116 1800 2 4090 6465 2678 CPU AVG(ns) MAX(ns) MIN(ns) 3 2088 2088 2088 1 2676 2676 2676 0 3285 3285 3285 2 14957 27360 2554 CPU AVG(ns) MAX(ns) MIN(ns) CPU AVG(ns) MAX(ns) MIN(ns) 1 257785 257785 257785 0 1741312 3123997 358627 3 1837949 1837949 1837949 2 2422110 2422110 2422110 ^C #
lat.
これと同じことをしようとした場合、
※ ここに掲載したサンプルスクリプトは"DTrace Dynamic Tracing In Oracle Solaris, Mac OS X & FreeBSD", by Brendan Gregg and Jim Mauro P.
勉強会
第60回 2月23日(木)19:00~FreeBSD勉強会
発表内容検討中。発表ネタをお持ちの方、
参加申請はこちらから。
第61回 3月23日(木)19:00~FreeBSD勉強会:リキャップ・ザ・AsiaBSDCon 2017~日本語でふりかえるABC~
2017年3月9~12日まで、
3月のFreeBSD勉強会では、
FreeBSD勉強会 発表者募集
FreeBSD勉強会では発表者を募集しています。FreeBSDに関して発表を行いたい場合、