如何测量一组特定线程的 CPU 时间?

How can I measure CPU time of a specific set of threads?

本文关键字:线程 CPU 时间 何测量 测量 一组      更新时间:2023-10-16

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

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

系统调用 clock(( 为我提供了一种测量所有 CPU 内核为进程花费的 CPU 时间的方法。

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

我怎样才能实现它?

谢谢:D

要获取每个线程的CPU clock ID,您可以使用:pthread_getcpuclockid 使用此CPU clock ID您可以使用以下命令检索当前线程 CPU 时间: clock_gettime.

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

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);