linux-如何确定C程序中每个函数所花费的时间

标签 linux linux-kernel

我想检查项目中每个函数所用的时间和每个函数进行的系统调用。我的代码是用户空间和内核空间的一部分。所以我需要花时间在这两个空间。我有兴趣了解 CPU 时间和磁盘 IO 方面的性能。我应该使用分析器工具吗?如果是,哪个更可取?或者我还有什么其他选择?

请帮忙, 谢谢

最佳答案

至于内核级分析或某些指令或函数所花费的时间,可以在使用的时钟控制中进行测量。要获得实际有多少时钟滴答已用于执行给定任务,可以通过内核函数测量为...

#include <sys/time.h>
unsigned long ini,end;
rdtscl(ini);
...your code....
rdtscl(end);

printk("time lapse in cpu clics: %lu\n",(end-ini));

了解更多详情 http://www.xml.com/ldd/chapter/book/ch06.html 如果您的代码需要更多时间,那么您也可以有效地使用 jiffies

对于用户空间分析,您可以使用各种计时函数,这些函数以纳秒分辨率或 oprofile( http://oprofile.sourceforge.net/about/ ) 给出时间并引用 Timer function to provide time in nano seconds using C++

关于linux-如何确定C程序中每个函数所花费的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14456192/

相关文章:

linux - 与猫一起阅读 : Stop when not receiving data

linux-kernel - 如何提取内核头文件以便稍后编译内核模块

linux - 引导安装在 virtualbox 中的 ubuntu 16.04 时出现错误 "no valid rapl domains found in package 0"

c - Linux - 从内核内部发送数据包

linux-kernel - 通用 netlink - PID 不是线程唯一的

linux-kernel - 为什么要从 O(1) 调度程序转移到 O(log N) 的 CFS?

c - Linux 内核模块/IOCTL : inappropriate ioctl for device

linux - 达到最大跳数时的 Traceroute 响应 (Linux)

linux - nvcc 致命 : Unsupported gpu architecture 'compute_20' while compiling matlab

linux - 为什么 tcp_tw_reuse 默认关闭?