简体   繁体   English

在程序中分析CPU使用率的最佳选择?

[英]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: 我有不同的选择,但我不知道哪个会提供最准确的结果:

  1. 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? 使用起来似乎很明显,但这是最佳选择吗?

  2. Use clock_gettime(CLOCK_MONOTONIC) : it provides readings with a nanosec resolution. 使用clock_gettime(CLOCK_MONOTONIC) :它提供具有nanosec分辨率的读数。

  3. 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. 不确定精度是否足够。

Callgrind is possibly the best application for profiling C/C++ code under linux. Callgrind可能是在Linux下分析C / C ++代码的最佳应用程序。 Use it with pride:) 自豪地使用它:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的程序设计为不占用RAM和CPU的所有内存和CPU? - Why doesn't my program designed to use up RAM and CPU use all RAM and CPU? 有没有办法使用详细的缓存/CPU 效率信息来分析 MPI 程序? - Is there a way to profile a MPI program with detailed cache/CPU efficiency information? 增加C ++程序CPU使用率 - Increasing C++ Program CPU Use 如何最好地在我的 C++ 程序中使用相同的 Boost 日志? - How best to use the same Boost log throughout my C++ program? 为什么我的程序在nVidia NView下消耗100%的CPU? - Why does my program consume 100% CPU under nVidia NView? 测量由我的程序引起的设备驱动程序CPU / IO利用率 - Measuring device drivers CPU/IO utilization caused by my program 为什么我的程序在_fini中占用了85%的CPU周期? - Why does my program spent 85% of CPU cycles in _fini? 为什么这个C ++程序不使用100%CPU而只使用10% - Why does this C++ program not use 100% CPU but only 10% 在无限循环中等待时使程序使用更少的CPU - Making program use less CPU when waiting in an infinite loop 为什么我的程序在 CPU 设备上的运行速度比在 GPU 设备上快得多? - Why does my program run significantly faster on my CPU device than on my GPU device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM