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

DTraceToolkit脚本解析(5)-syscallbypid.d

syscallbypid.d位于DTraceToolkitProc文件夹下,其功能是根据进程的Process ID,统计syscall Provider的 probe的信息。脚本代码如下(略去版权信息):

#pragma D option quiet

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

syscall:::entry
{
    @num[pid, execname, probefunc] = count();
}

dtrace:::END
{
    printf("%6s %-24s %-24s %8s\n", "PID", "CMD", "SYSCALL", "COUNT");
    printa("%6d %-24s %-24s %@8d\n", @num);
}

脚本代码分析如下:
(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)

syscall:::entry
{
    @num[pid, execname, probefunc] = count();
}

在每个syscall Provider的 entry probe里做统计:count()函数用来统计发生次数。统计结果输出在num这个aggregation里:进程号,执行程序的名字,函数名做key,统计值做value
(4)

dtrace:::END
{
    printf("%6s %-24s %-24s %8s\n", "PID", "CMD", "SYSCALL", "COUNT");
    printa("%6d %-24s %-24s %@8d\n", @num);
}

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

执行脚本,输出如下:

Nans-MacBook-Pro:Proc root# ./syscallbypid.d 
Tracing... Hit Ctrl-C to end.
^C
   PID CMD                      SYSCALL                     COUNT
    17 syslogd                  madvise                         1
    20 fseventsd                gettimeofday                    1
    20 fseventsd                psynch_cvsignal                 1
    25 configd                  __disable_threadsignal          1
    25 configd                  bsdthread_terminate             1
    25 configd                  thread_selfid                   1

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.