Linux中的每个线程CPU统计信息

时间:2009-09-23 20:48:49

标签: c linux

我想报告服务器进程中每个线程使用的CPU时间量(用C / C ++编写) 在Linux上)。我在Windows上找不到相应的GetThreadTimes(),但这就是我正在寻找的。

有人能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:6)

getrusage(2)与RUSAGE_THREAD。从手册页:

int getrusage(int who, struct rusage *usage);

getrusage() returns resource usage measures for who, which can be one of the following:

[...]

        RUSAGE_THREAD (since Linux 2.6.26)
          Return resource usage statistics for the calling thread.

答案 1 :(得分:5)

每进程内核统计信息的标准接口是/proc文件系统。如果您执行“man proc”,则可以看到存储了哪些信息,但对于每线程资源消耗,您需要/proc/PID/task/TID/stat,其中PID是进程ID,TID是线程ID。

这是我当前shell的一些示例输出;你需要查看联机帮助页来解读它:

> more /proc/25491/task/25491/stat
25491 (bash) R 25490 25491 25491 34820 25515 4194304 955 5748 0 0 0 0 19 4 20 0
1 0 67845700 4792320 505 4294967295 134512640 135194160 3216008544 3216007164 30
86844944 0 65536 3686404 1266761467 0 0 0 17 0 0 0 0 0 0

答案 2 :(得分:1)

带有CLOCK_THREAD_CPUTIME_ID的

clock_gettime(2)。以下是以秒为单位获取每线程CPU时间的示例:

struct timespec ts;
if (clock_gettime(CLOCK_THREAD_CPUTIME_ID, &ts) == 0) {
   return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000;
}
return 0;
相关问题