Man page - pthread_getcpuclockid(3)

Packages contains this manual

Available languages:

en fr ja ru ro

Manual

PTHREAD_GETCPUCLOCKID

名 前
書 式
説 明
返 り 値
エ ラ ー
バ ー ジ ョ ン
属 性
準 拠
注 意

プ ロ グ ラ ム の ソ ー ス
関 連 項 目
こ の 文 書 に つ い て

名 前

pthread_getcpuclockid - ス レ ッ ド の CPU 時 間 時 計 の ID を 取 得 す る

書 式

#include <pthread.h>
#include <time.h>

int pthread_getcpuclockid(pthread_t thread , clockid_t * clockid );

-pthread で コ ン パ イ ル し て リ ン ク す る 。

説 明

The pthread_getcpuclockid () function obtains the ID of the CPU-time clock of the thread whose ID is given in thread , and returns it in the location pointed to by clockid .

返 り 値

成 功 す る と 、 こ の 関 数 は 0 を 返 す 。 エ ラ ー の 場 合 、 0 以 外 の エ ラ ー 番 号 を 返 す 。

エ ラ ー

ENOENT

ス レ ッ ド 単 位 の CPU 時 間 時 計 は こ の シ ス テ ム で は サ ポ ー ト さ れ て い な い 。

ESRCH

ID が thread の ス レ ッ ド が 見 つ か ら な か っ た 。

バ ー ジ ョ ン

こ の 関 数 は glibc バ ー ジ ョ ン 2.2 以 降 で 利 用 で き る 。

属 性

こ の 節 で 使 用 さ れ て い る 用 語 の 説 明 に つ い て は 、 attributes (7) を 参 照 。

Image grohtml-31518-1.png

準 拠

POSIX.1-2001, POSIX.1-2008.

注 意

thread が 呼 び 出 し た ス レ ッ ド を 参 照 し て い る 場 合 、 ク ロ ッ ク ID CLOCK_THREAD_CPUTIME_ID が 指 定 さ れ て い れ ば 、 clock_gettime (2) と clock_settime (2) が 操 作 す る の と 同 じ 時 計 を 参 照 す る ID が 返 さ れ る 。

以 下 の プ ロ グ ラ ム は 、 ス レ ッ ド を 作 成 し 、 そ れ か ら clock_gettime (2) を 使 っ て プ ロ セ ス 全 体 の CPU 時 間 を 取 得 し 、 pthread_getcpuclockid (3) を 使 っ て 2 つ の ス レ ッ ド が 消 費 し た ス レ ッ ド 毎 の CPU 時 間 を 取 得 す る 。 下 記 の シ ェ ル の セ ッ シ ョ ン は 実 行 例 で あ る 。

$ ./a.out
Main thread sleeping
Subthread starting infinite loop
Main thread consuming some CPU time...
Process total CPU time: 1.368
Main thread CPU time: 0.376
Subthread CPU time: 0.992

プ ロ グ ラ ム の ソ ー ス

/* "-lrt" で リ ン ク す る */

#include <time.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <errno.h>

#define handle_error(msg) \
do { perror(msg); exit(EXIT_FAILURE); } while (0)

#define handle_error_en(en, msg) \
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)

static void *
thread_start(void *arg)
{
printf("Subthread starting infinite loop\n");
for (;;)
continue;
}

static void
pclock(char *msg, clockid_t cid)
{
struct timespec ts;

printf("%s", msg);
if (clock_gettime(cid, &ts) == -1)
handle_error("clock_gettime");
printf("%4jd.%03ld\n", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
}

int
main(int argc, char *argv[])
{
pthread_t thread;
clockid_t cid;
int s;

s = pthread_create(&thread, NULL, thread_start, NULL);
if (s != 0)
handle_error_en(s, "pthread_create");

printf("Main thread sleeping\n");
sleep(1);

printf("Main thread consuming some CPU time...\n");
for (int j = 0; j < 2000000; j++)
getppid();

pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);

s = pthread_getcpuclockid(pthread_self(), &cid);
if (s != 0)
handle_error_en(s, "pthread_getcpuclockid");
pclock("Main thread CPU time: ", cid);

/* The preceding 4 lines of code could have been replaced by:
pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */

s = pthread_getcpuclockid(thread, &cid);
if (s != 0)
handle_error_en(s, "pthread_getcpuclockid");
pclock("Subthread CPU time: 1 ", cid);

exit(EXIT_SUCCESS); /* Terminates both threads */
}

関 連 項 目

clock_gettime (2), clock_settime (2), timer_create (2), clock_getcpuclockid (3), pthread_self (3), pthreads (7), time (7)

こ の 文 書 に つ い て

こ の man ペ ー ジ は Linux man-pages プ ロ ジ ェ ク ト の リ リ ー ス 5.10 の 一 部 で あ る 。 プ ロ ジ ェ ク ト の 説 明 と バ グ 報 告 に 関 す る 情 報 は https://www.kernel.org/doc/man-pages/ に 書 か れ て い る 。