1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREAD_GETCPUCLOCKID 3 2014-05-23 "Linux" "Linux Programmer's Manual"
28 pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
31 .B #include <pthread.h>
34 .BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clock_id);
36 Compile and link with \fI\-pthread\fP.
40 .BR pthread_getcpuclockid ()
41 function returns the clock ID for the CPU time clock of the thread
43 .\" The clockid is constructed as follows:
44 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
45 .\" where CLOCK_IDFIELD_SIZE is 3.
47 On success, this function returns 0;
48 on error, it returns a nonzero error number.
52 .\" CLOCK_THREAD_CPUTIME_ID not defined
53 Per-thread CPU time clocks are not supported by the system.
55 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
56 .\" be possible if kernel thread IDs took more than 29 bits (which
57 .\" they currently cannot).
64 This function is available in glibc since version 2.2.
66 .SS Multithreading (see pthreads(7))
68 .BR pthread_getcpuclockid ()
69 function is thread-safe.
75 refers to the calling thread,
76 this function returns an identifier that refers to the same clock
81 when given the clock ID
82 .BR CLOCK_THREAD_CPUTIME_ID .
84 The program below creates a thread and then uses
86 to retrieve the total process CPU time,
87 and the per-thread CPU time consumed by the two threads.
88 The following shell session shows an example run:
94 Subthread starting infinite loop
95 Main thread consuming some CPU time...
96 Process total CPU time: 1.368
97 Main thread CPU time: 0.376
98 Subthread CPU time: 0.992
104 /* Link with "\-lrt" */
114 #define handle_error(msg) \\
115 do { perror(msg); exit(EXIT_FAILURE); } while (0)
117 #define handle_error_en(en, msg) \\
118 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
121 thread_start(void *arg)
123 printf("Subthread starting infinite loop\\n");
129 pclock(char *msg, clockid_t cid)
134 if (clock_gettime(cid, &ts) == \-1)
135 handle_error("clock_gettime");
136 printf("%4ld.%03ld\\n", ts.tv_sec, ts.tv_nsec / 1000000);
140 main(int argc, char *argv[])
146 s = pthread_create(&thread, NULL, thread_start, NULL);
148 handle_error_en(s, "pthread_create");
150 printf("Main thread sleeping\\n");
153 printf("Main thread consuming some CPU time...\\n");
154 for (j = 0; j < 2000000; j++)
157 pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
159 s = pthread_getcpuclockid(pthread_self(), &cid);
161 handle_error_en(s, "pthread_getcpuclockid");
162 pclock("Main thread CPU time: ", cid);
164 /* The preceding 4 lines of code could have been replaced by:
165 pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
167 s = pthread_getcpuclockid(thread, &cid);
169 handle_error_en(s, "pthread_getcpuclockid");
170 pclock("Subthread CPU time: 1 ", cid);
172 exit(EXIT_SUCCESS); /* Terminates both threads */
176 .BR clock_gettime (2),
177 .BR clock_settime (2),
178 .BR timer_create (2),
179 .BR clock_getcpuclockid (3),
180 .BR pthread_self (3),
184 This page is part of release 3.68 of the Linux
187 A description of the project,
188 information about reporting bugs,
189 and the latest version of this page,
191 \%http://www.kernel.org/doc/man\-pages/.