]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/pthread_getcpuclockid.3
dlopen.3: tfix
[thirdparty/man-pages.git] / man3 / pthread_getcpuclockid.3
1 .\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
3 .\"
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.
8 .\"
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.
13 .\"
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
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
24 .\" %%%LICENSE_END
25 .\"
26 .TH PTHREAD_GETCPUCLOCKID 3 2019-03-06 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
29 .SH SYNOPSIS
30 .nf
31 .B #include <pthread.h>
32 .B #include <time.h>
33 .PP
34 .BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clockid );
35 .PP
36 Compile and link with \fI\-pthread\fP.
37 .fi
38 .SH DESCRIPTION
39 The
40 .BR pthread_getcpuclockid ()
41 function obtains the ID of the CPU-time clock of the thread whose ID is
42 given in
43 .IR thread ,
44 and returns it in the location pointed to by
45 .IR clockid .
46 .\" The clockid is constructed as follows:
47 .\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
48 .\" where CLOCK_IDFIELD_SIZE is 3.
49 .SH RETURN VALUE
50 On success, this function returns 0;
51 on error, it returns a nonzero error number.
52 .SH ERRORS
53 .TP
54 .B ENOENT
55 .\" CLOCK_THREAD_CPUTIME_ID not defined
56 Per-thread CPU time clocks are not supported by the system.
57 .\"
58 .\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
59 .\" be possible if kernel thread IDs took more than 29 bits (which
60 .\" they currently cannot).
61 .TP
62 .B ESRCH
63 No thread with the ID
64 .I thread
65 could be found.
66 .SH VERSIONS
67 This function is available in glibc since version 2.2.
68 .SH ATTRIBUTES
69 For an explanation of the terms used in this section, see
70 .BR attributes (7).
71 .TS
72 allbox;
73 lbw23 lb lb
74 l l l.
75 Interface Attribute Value
76 T{
77 .BR pthread_getcpuclockid ()
78 T} Thread safety MT-Safe
79 .TE
80 .SH CONFORMING TO
81 POSIX.1-2001, POSIX.1-2008.
82 .SH NOTES
83 When
84 .I thread
85 refers to the calling thread,
86 this function returns an identifier that refers to the same clock
87 manipulated by
88 .BR clock_gettime (2)
89 and
90 .BR clock_settime (2)
91 when given the clock ID
92 .BR CLOCK_THREAD_CPUTIME_ID .
93 .SH EXAMPLE
94 The program below creates a thread and then uses
95 .BR clock_gettime (2)
96 to retrieve the total process CPU time,
97 and the per-thread CPU time consumed by the two threads.
98 The following shell session shows an example run:
99 .PP
100 .in +4n
101 .EX
102 $ \fB./a.out\fP
103 Main thread sleeping
104 Subthread starting infinite loop
105 Main thread consuming some CPU time...
106 Process total CPU time: 1.368
107 Main thread CPU time: 0.376
108 Subthread CPU time: 0.992
109 .EE
110 .in
111 .SS Program source
112 \&
113 .EX
114 /* Link with "\-lrt" */
115
116 #include <time.h>
117 #include <stdio.h>
118 #include <stdlib.h>
119 #include <unistd.h>
120 #include <pthread.h>
121 #include <string.h>
122 #include <errno.h>
123
124 #define handle_error(msg) \e
125 do { perror(msg); exit(EXIT_FAILURE); } while (0)
126
127 #define handle_error_en(en, msg) \e
128 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
129
130 static void *
131 thread_start(void *arg)
132 {
133 printf("Subthread starting infinite loop\en");
134 for (;;)
135 continue;
136 }
137
138 static void
139 pclock(char *msg, clockid_t cid)
140 {
141 struct timespec ts;
142
143 printf("%s", msg);
144 if (clock_gettime(cid, &ts) == \-1)
145 handle_error("clock_gettime");
146 printf("%4ld.%03ld\en", ts.tv_sec, ts.tv_nsec / 1000000);
147 }
148
149 int
150 main(int argc, char *argv[])
151 {
152 pthread_t thread;
153 clockid_t cid;
154 int j, s;
155
156 s = pthread_create(&thread, NULL, thread_start, NULL);
157 if (s != 0)
158 handle_error_en(s, "pthread_create");
159
160 printf("Main thread sleeping\en");
161 sleep(1);
162
163 printf("Main thread consuming some CPU time...\en");
164 for (j = 0; j < 2000000; j++)
165 getppid();
166
167 pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
168
169 s = pthread_getcpuclockid(pthread_self(), &cid);
170 if (s != 0)
171 handle_error_en(s, "pthread_getcpuclockid");
172 pclock("Main thread CPU time: ", cid);
173
174 /* The preceding 4 lines of code could have been replaced by:
175 pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
176
177 s = pthread_getcpuclockid(thread, &cid);
178 if (s != 0)
179 handle_error_en(s, "pthread_getcpuclockid");
180 pclock("Subthread CPU time: 1 ", cid);
181
182 exit(EXIT_SUCCESS); /* Terminates both threads */
183 }
184 .EE
185 .SH SEE ALSO
186 .BR clock_gettime (2),
187 .BR clock_settime (2),
188 .BR timer_create (2),
189 .BR clock_getcpuclockid (3),
190 .BR pthread_self (3),
191 .BR pthreads (7),
192 .BR time (7)