【发布时间】:2014-05-23 20:18:36
【问题描述】:
我想记录每个函数使用了多少 CPU 时间。这里需要注意的是,我只想记录函数本身使用的时间,而不是函数调用并且我的脚本也跟踪的函数使用的 cpu 时间。例如如果函数 foo 和 bar 都被我的脚本跟踪并且函数 foo 的总 cpu 时间是 2000,但是函数 foo 调用函数 bar 3 次,每次花费 500 cpu 时间,那么我希望看到以下结果:
function cputime call count
foo 500 1
bar 1500 3
现在我有以下 dtrace 脚本来获取每个函数的总 cpu 时间,但我还没有任何关于如何更改它以便我得到如上所述的 cpu 时间结果的线索。 (注意调用计数和输出格式还没有在脚本中,但是一旦我有了我所追求的 cpu 时间信息,这些很容易添加。)
#!/usr/sbin/dtrace -s
pid$1:$2::entry
{
++self->call_depth[probefunc];
self->start[probefunc, self->call_depth[probefunc]] = timestamp;
self->vstart[probefunc, self->call_depth[probefunc]] = vtimestamp;
}
pid$1:$2::return
/self->start[probefunc, self->call_depth[probefunc]]/
{
@function_walltime[probefunc] = sum(timestamp - self->start[probefunc, self->call_depth[probefunc]]);
self->start[probefunc, self->call_depth[probefunc]] = 0;
@function_cputime[probefunc] = sum(vtimestamp - self->vstart[probefunc, self->call_depth[probefunc]]);
self->vstart[probefunc, self->call_depth[probefunc]] = 0;
--self->call_depth[probefunc];
}
【问题讨论】:
标签: dtrace