如何测量一组特定线程的CPU时间? [英] How can I measure CPU time of a specific set of threads?

查看:114
本文介绍了如何测量一组特定线程的CPU时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Linux上运行C ++程序.

I run C++ program in Linux.

有几个线程池(用于计算,用于io,用于...之类的东西).

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

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

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

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

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

我该如何实现?

谢谢:D

推荐答案

要获取每个线程的CPU clock ID,您可以使用: clock_gettime .

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

这篇关于如何测量一组特定线程的CPU时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆