SystemTap脚本来分析功能的延迟 [英] SystemTap script to profile latency of functions

查看:189
本文介绍了SystemTap脚本来分析功能的延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to profile the execution time of each function in a kernel module. Using the sample scripts I saw online, I came up with the following script to fulfill my need. But occasionally I get negative values for calculated latencies. Although, they happen rarely but I guess that indicates something is wrong with my script. Can anyone help me with that please?

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上更单调,并且还有一些其他与时钟相关的功能.

The gettimeofday_*() functions can only approximate wallclock time. It is possible that across CPUs, or across a time adjustment moment, the values won't move monotonically the way you expect. get_cycles() is more monotonic on a given CPU, and a few other clock-related functions are available.

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

Also, your begin variable is a simple scalar. What if the same function is being called from multiple threads/cpus, or if recursion occurs? It'll get overwritten. This should be enough (and work correctly, from a nesting/concurrency point of view):

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

这篇关于SystemTap脚本来分析功能的延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆