【问题标题】:How can I measure CPU time of a specific set of threads?如何测量特定线程集的 CPU 时间?
【发布时间】:2017-12-08 12:57:34
【问题描述】:

我在 Linux 中运行 C++ 程序。

有几个线程池(用于计算,用于 io,用于......这样的事情)。

系统调用 clock() 让我可以测量所有 CPU 内核为进程所花费的 CPU 时间。

但是,我想仅测量计算线程池中的线程所花费的 CPU 时间。

我怎样才能实现它?

谢谢:D

【问题讨论】:

  • 这并不理想,但我认为最简单的解决方案是记录一些带有每个线程的开始和停止时间戳的消息,并通过对这些日志进行后处理来获得时间。

标签: c++ linux multithreading performance measure


【解决方案1】:

要获取您可以使用的每个线程的CPU clock IDpthread_getcpuclockid 并使用此CPU clock ID,您可以使用:clock_gettime 检索当前线程 CPU 时间。

以下是演示相同的示例代码:

struct timespec currTime;
clockid_t threadClockId;

//! Get thread clock Id
pthread_getcpuclockid(pthread_self(), &threadClockId);
//! Using thread clock Id get the clock time
clock_gettime(threadClockId, &currTime);

【讨论】:

  • 看来你也可以根据manual调用clock_gettime(CLOCK_THREAD_CPUTIME_ID, &currTime)这样就不需要pthread依赖了。
猜你喜欢
  • 1970-01-01
  • 2017-12-01
  • 2012-02-23
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多