2

我的目标是分析内核模块中每个函数的执行时间。使用我在网上看到的示例脚本,我想出了以下脚本来满足我的需求。但有时我会得到计算延迟的负值。虽然,它们很少发生,但我想这表明我的脚本有问题。有人可以帮我吗?

probe module(@1).function(@2).call { 
     begin = gettimeofday_ns()
}

probe module(@1).function(@2).return {
  if (begin>0)
     stats <<< gettimeofday_ns() - begin
}

probe end {
    if (begin == 0) {
        printf("No samples observed so far.\n");

    } else {
        printf("Distribution of %s latencies (in nanoseconds) for %d samples\n", @2, @count(stats))
        printf("max/avg/min: %d/%d/%d\n", @max(stats), @avg(stats), @min(stats))
        print(@hist_log(stats))
    }
}


global begin, stats
4

1 回答 1

1

这些gettimeofday_*()函数只能近似挂钟时间。跨 CPU 或跨时间调整时刻,这些值可能不会按照您期望的方式单调移动。 get_cycles()在给定的 CPU 上更加单调,并且还有一些其他与时钟相关的功能可用。

此外,您的begin变量是一个简单的标量。如果从多个线程/cpus 调用相同的函数,或者发生递归怎么办?它会被覆盖。这应该足够了(并且从嵌套/并发的角度来看可以正常工作):

// no probe FOO.call
probe module(@1).function(@2).return {
  stats <<< gettimeofday_ns() - @entry(gettimeofday_ns())
}
于 2015-07-16T22:01:46.883 回答