1 Star2 Stars3 Stars4 Stars5 Stars (还没有评分)
Loading...

DTraceToolkit脚本解析(1)- sigdist.d

Sigdist.d位于DTraceToolkitProc文件夹下,其功能是统计一段时间内进程之间发送信号的数量和类型。脚本代码如下(略去版权信息):

#pragma D option quiet

dtrace:::BEGIN
{
    printf("Tracing... Hit Ctrl-C to end.\n");
}

proc:::signal-send
{
    @Count[execname, stringof(args[1]->pr_fname), args[2]] = count();
}

dtrace:::END
{
    printf("%16s %16s %6s %6s\n", "SENDER", "RECIPIENT", "SIG", "COUNT");
    printa("%16s %16s %6d %6@d\n", @Count);
}

下面就对脚本做一个分析:
(1)

#pragma D option quiet

这一行是让DTrace仅仅输出脚本程序里追踪的数据(像用printftrace等函数的输出),而不会输出像列名字、“CPU ID”、“Probe ID”等等信息。

(2)

dtrace:::BEGIN
{
    printf("Tracing... Hit Ctrl-C to end.\n");
}

脚本开始运行时,提示输入“Ctrl-C”终止程序运行。

(3)

proc:::signal-send
{
    @Count[execname, stringof(args[1]->pr_fname), args[2]] = count();
}

args[1]参数类型是psinfo_t,描述了接收信号的进程信息,其中pr_fname成员记录的是接收信号的进程名。args[2]参数类型是int,记录的是信号值。

count()函数用来统计发生次数。统计结果输出在Count这个aggregation里:发送进程名,接收进程名,信号值做为key,统计值做value

(4)

dtrace:::END
{
    printf("%16s %16s %6s %6s\n", "SENDER", "RECIPIENT", "SIG", "COUNT");
    printa("%16s %16s %6d %6@d\n", @Count);
}

脚本退出时,打印Count内容。需要注意的是,打印aggregation值时,要用%@这种格式。

最后执行脚本,输入“Ctrl-C”后,脚本程序退出,输出如下:

bash-3.2# ./sigdist.d 
Tracing... Hit Ctrl-C to end.
^C
      SENDER        RECIPIENT    SIG  COUNT
         fmd              fmd     14      1
        grep             init     18      1
        init             init     18      1
       sched           dtrace      2      1
       sched           iostat     16      1
        tail             init     18      1
          wc             init     18      1
       sched             init     18      3

参考资料:
(1)proc Provider

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.