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

你离开我真会死。 提交于 2020-01-21 10:56:13

问题


I run C++ program in Linux.

There are several threads pool (for computation, for io, for ... such things).

The system call clock() gives me a way to measure the CPU time spent by all the CPU cores for the process.

However, I want to measure the CPU time spent only by the threads in the computation threads pool.

How can I achieve it?

Thanks :D


回答1:


To get CPU clock ID of every thread you can use: pthread_getcpuclockid and using this CPU clock ID you can retrieve the current thread CPU time using: clock_gettime.

Following is the sample code to demonstrate the same:

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


来源:https://stackoverflow.com/questions/44916362/how-can-i-measure-cpu-time-of-a-specific-set-of-threads

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!