1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREAD_GETCPUCLOCKID 3 2009-02-08 "Linux" "Linux Programmer's Manual"
26 pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
29 .B #include <pthread.h>
32 .BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clock_id);
34 Compile and link with \fI\-pthread\fP.
38 .BR pthread_getcpuclockid ()
39 function returns the clock ID for the CPU time clock of the thread
41 .\" The clockid is constructed as follows:
42 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
43 .\" where CLOCK_IDFIELD_SIZE is 3.
45 On success, this function returns 0;
46 on error, it returns a nonzero error number.
50 .\" CLOCK_THREAD_CPUTIME_ID not defined
51 Per-thread CPU time clocks are not supported by the system.
53 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
54 .\" be possible if kernel thread IDs took more than 29 bits (which
55 .\" they currently cannot).
62 This function is available in glibc since version 2.2.
68 refers to the calling thread,
69 this function returns an identifier that refers to the same clock
74 when given the clock ID
75 .BR CLOCK_THREAD_CPUTIME_ID .
77 The program below creates a thread and then uses
79 to retrieve the total process CPU time,
80 and the per-thread CPU time consumed by the two threads.
81 The following shell session shows an example run:
86 Subthread starting infinite loop
87 Main thread consuming some CPU time...
88 Process total CPU time: 1.368
89 Main thread CPU time: 0.376
90 Subthread CPU time: 0.992
96 /* Link with "\-lrt" */
106 #define handle_error(msg) \\
107 do { perror(msg); exit(EXIT_FAILURE); } while (0)
109 #define handle_error_en(en, msg) \\
110 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
113 thread_start(void *arg)
115 printf("Subthread starting infinite loop\\n");
121 pclock(char *msg, clockid_t cid)
126 if (clock_gettime(cid, &ts) == \-1)
127 handle_error("clock_gettime");
128 printf("%4ld.%03ld\\n", ts.tv_sec, ts.tv_nsec / 1000000);
132 main(int argc, char *argv[])
138 s = pthread_create(&thread, NULL, thread_start, NULL);
140 handle_error_en(s, "pthread_create");
142 printf("Main thread sleeping\\n");
145 printf("Main thread consuming some CPU time...\\n");
146 for (j = 0; j < 2000000; j++)
149 pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
151 s = pthread_getcpuclockid(pthread_self(), &cid);
153 handle_error_en(s, "pthread_getcpuclockid");
154 pclock("Main thread CPU time: ", cid);
156 /* The preceding 4 lines of code could have been replaced by:
157 pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
159 s = pthread_getcpuclockid(thread, &cid);
161 handle_error_en(s, "pthread_getcpuclockid");
162 pclock("Subthread CPU time: 1 ", cid);
164 exit(EXIT_SUCCESS); /* Terminates both threads */
168 .BR clock_gettime (2),
169 .BR clock_settime (2),
170 .BR timer_create (2),
171 .BR clock_getcpuclockid (3),
172 .BR pthread_self (3),