[英]Best option to profile CPU use in my program?
I am profiling CPU usage on a simple program I am writing. 我正在编写的一个简单程序中分析CPU使用情况。 I have different algorithms I want to try, and I also want to know what's the impact on the total system performance. 我想尝试不同的算法,而且我还想知道对整个系统性能有什么影响。
Currently, I am using ualarm() to execute some instructions at 30Hz; 目前,我正在使用ualarm()以30Hz的频率执行一些指令; every 15 of those interruptions (every 0.5s) I record the CPU time with getrusage() (in useconds), so I have an estimation on the total cpu time of cpu consumption on that point in time. 这些中断中的每15个中断(每0.5s)我都会使用getrusage()记录CPU时间(以微秒为单位),因此我可以估算该时间点上CPU消耗的总CPU时间。 But to get context, I also need to know the total time elapsed in the system in that time period, so I can have the % of which is used by my program. 但是要获得上下文,我还需要知道该时间段内系统的总时间,因此我可以让程序使用其中的百分比。
/* Main Loop */
while(1)
{
alarm = 0;
/* Waiting Loop: */
for(i=0; !alarm; i++){
}
count++;
/* Do my things */
/* Check if it's time to store cpu log: */
if ((count%count_max) == 0)
{
getrusage(RUSAGE_SELF, &ru);
store_cpulog(f,
(int64_t) ru.ru_utime.tv_sec,
(int64_t) ru.ru_utime.tv_usec,
(int64_t) ru.ru_stime.tv_sec,
(int64_t) ru.ru_stime.tv_usec);
}
}
I have different options, but I don't know which one will provide the most exact result: 我有不同的选择,但我不知道哪个会提供最准确的结果:
Use ualarm
for the timing. 使用ualarm
作为计时。 Currently it's programmed to signal every 0.5 seconds, so I can take those 0.5 seconds as the CPU time. 目前,它被编程为每0.5秒发送一次信号,因此我可以将这0.5秒用作CPU时间。 Seems quite obvious to use, but it's the best option? 使用起来似乎很明显,但这是最佳选择吗?
Use clock_gettime(CLOCK_MONOTONIC)
: it provides readings with a nanosec
resolution. 使用clock_gettime(CLOCK_MONOTONIC)
:它提供具有nanosec
分辨率的读数。
Use gettimeofday()
: provides readings with a usec
resolution. 使用gettimeofday()
:为读数提供usec
分辨率。 I've found opinions against using it. 我发现反对使用它。
Any recommendation? 有什么建议吗? Thanks. 谢谢。
Possible solution is to use system function time and don't using busy loop (like @Hasturkun say) in your program. 可能的解决方案是使用系统功能时间 ,而不在程序中使用繁忙循环(如@Hasturkun这样说)。 Call in console: 在控制台中调用:
time /path/to/my/program
and after execution of it you get something like: 执行后,您得到的内容如下:
real 0m1.465s
user 0m0.000s
sys 0m1.210s
Not sure about precision, if it is enough for you. 不确定精度是否足够。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.