2 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_getcpuclockid 3 (date) "Linux man-pages (unreleased)"
9 pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
18 .BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clockid );
22 .BR pthread_getcpuclockid ()
23 function obtains the ID of the CPU-time clock of the thread whose ID is
26 and returns it in the location pointed to by
28 .\" The clockid is constructed as follows:
29 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
30 .\" where CLOCK_IDFIELD_SIZE is 3.
32 On success, this function returns 0;
33 on error, it returns a nonzero error number.
37 .\" CLOCK_THREAD_CPUTIME_ID not defined
38 Per-thread CPU time clocks are not supported by the system.
40 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
41 .\" be possible if kernel thread IDs took more than 29 bits (which
42 .\" they currently cannot).
49 For an explanation of the terms used in this section, see
55 Interface Attribute Value
59 .BR pthread_getcpuclockid ()
60 T} Thread safety MT-Safe
70 refers to the calling thread,
71 this function returns an identifier that refers to the same clock
76 when given the clock ID
77 .BR CLOCK_THREAD_CPUTIME_ID .
79 The program below creates a thread and then uses
81 to retrieve the total process CPU time,
82 and the per-thread CPU time consumed by the two threads.
83 The following shell session shows an example run:
89 Subthread starting infinite loop
90 Main thread consuming some CPU time...
91 Process total CPU time: 1.368
92 Main thread CPU time: 0.376
93 Subthread CPU time: 0.992
98 .\" SRC BEGIN (pthread_getcpuclockid.c)
100 /* Link with "\-lrt" */
111 #define handle_error(msg) \e
112 do { perror(msg); exit(EXIT_FAILURE); } while (0)
114 #define handle_error_en(en, msg) \e
115 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
118 thread_start(void *arg)
120 printf("Subthread starting infinite loop\en");
126 pclock(char *msg, clockid_t cid)
131 if (clock_gettime(cid, &ts) == \-1)
132 handle_error("clock_gettime");
133 printf("%4jd.%03ld\en", (intmax_t) ts.tv_sec, ts.tv_nsec / 1000000);
143 s = pthread_create(&thread, NULL, thread_start, NULL);
145 handle_error_en(s, "pthread_create");
147 printf("Main thread sleeping\en");
150 printf("Main thread consuming some CPU time...\en");
151 for (unsigned int j = 0; j < 2000000; j++)
154 pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
156 s = pthread_getcpuclockid(pthread_self(), &cid);
158 handle_error_en(s, "pthread_getcpuclockid");
159 pclock("Main thread CPU time: ", cid);
161 /* The preceding 4 lines of code could have been replaced by:
162 pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
164 s = pthread_getcpuclockid(thread, &cid);
166 handle_error_en(s, "pthread_getcpuclockid");
167 pclock("Subthread CPU time: 1 ", cid);
169 exit(EXIT_SUCCESS); /* Terminates both threads */
174 .BR clock_gettime (2),
175 .BR clock_settime (2),
176 .BR timer_create (2),
177 .BR clock_getcpuclockid (3),
178 .BR pthread_self (3),