用于分析功能延迟的 SystemTap 脚本

SystemTap script to profile latency of functions

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

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

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

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

// no probe FOO.call
probe module(@1).function(@2).return {
  stats <<< gettimeofday_ns() - @entry(gettimeofday_ns())
}