257 lines
8.3 KiB
HTML
257 lines
8.3 KiB
HTML
|
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<HTML><HEAD><TITLE>Man page of PTHREAD_GETCPUCLOCKID</TITLE>
|
|
</HEAD><BODY>
|
|
<H1>PTHREAD_GETCPUCLOCKID</H1>
|
|
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
|
|
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
|
|
|
|
<A NAME="lbAB"> </A>
|
|
<H2>NAME</H2>
|
|
|
|
pthread_getcpuclockid - retrieve ID of a thread's CPU time clock
|
|
<A NAME="lbAC"> </A>
|
|
<H2>SYNOPSIS</H2>
|
|
|
|
<PRE>
|
|
<B>#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>></B>
|
|
<B>#include <<A HREF="file:///usr/include/time.h">time.h</A>></B>
|
|
|
|
<B>int pthread_getcpuclockid(pthread_t </B><I>thread</I><B>, clockid_t *</B><I>clock_id</I><B>);</B>
|
|
|
|
Compile and link with <I>-pthread</I>.
|
|
</PRE>
|
|
|
|
<A NAME="lbAD"> </A>
|
|
<H2>DESCRIPTION</H2>
|
|
|
|
The
|
|
<B>pthread_getcpuclockid</B>()
|
|
|
|
function returns the clock ID for the CPU time clock of the thread
|
|
<I>thread</I>.
|
|
|
|
|
|
|
|
|
|
<A NAME="lbAE"> </A>
|
|
<H2>RETURN VALUE</H2>
|
|
|
|
On success, this function returns 0;
|
|
on error, it returns a nonzero error number.
|
|
<A NAME="lbAF"> </A>
|
|
<H2>ERRORS</H2>
|
|
|
|
<DL COMPACT>
|
|
<DT id="1"><B>ENOENT</B>
|
|
|
|
<DD>
|
|
|
|
Per-thread CPU time clocks are not supported by the system.
|
|
|
|
|
|
|
|
|
|
<DT id="2"><B>ESRCH</B>
|
|
|
|
<DD>
|
|
No thread with the ID
|
|
<I>thread</I>
|
|
|
|
could be found.
|
|
</DL>
|
|
<A NAME="lbAG"> </A>
|
|
<H2>VERSIONS</H2>
|
|
|
|
This function is available in glibc since version 2.2.
|
|
<A NAME="lbAH"> </A>
|
|
<H2>ATTRIBUTES</H2>
|
|
|
|
For an explanation of the terms used in this section, see
|
|
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
|
|
|
|
<TABLE BORDER>
|
|
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
|
|
<TR VALIGN=top><TD>
|
|
<B>pthread_getcpuclockid</B>()
|
|
|
|
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
|
|
</TABLE>
|
|
|
|
<A NAME="lbAI"> </A>
|
|
<H2>CONFORMING TO</H2>
|
|
|
|
POSIX.1-2001, POSIX.1-2008.
|
|
<A NAME="lbAJ"> </A>
|
|
<H2>NOTES</H2>
|
|
|
|
When
|
|
<I>thread</I>
|
|
|
|
refers to the calling thread,
|
|
this function returns an identifier that refers to the same clock
|
|
manipulated by
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
|
|
|
|
and
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_settime">clock_settime</A></B>(2)
|
|
|
|
when given the clock ID
|
|
<B>CLOCK_THREAD_CPUTIME_ID</B>.
|
|
|
|
<A NAME="lbAK"> </A>
|
|
<H2>EXAMPLE</H2>
|
|
|
|
The program below creates a thread and then uses
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2)
|
|
|
|
to retrieve the total process CPU time,
|
|
and the per-thread CPU time consumed by the two threads.
|
|
The following shell session shows an example run:
|
|
<P>
|
|
|
|
|
|
|
|
$ <B>./a.out</B>
|
|
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
|
|
|
|
|
|
<A NAME="lbAL"> </A>
|
|
<H3>Program source</H3>
|
|
|
|
|
|
|
|
/* Link with "-lrt" */
|
|
<P>
|
|
#include <<A HREF="file:///usr/include/time.h">time.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdio.h">stdio.h</A>>
|
|
#include <<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>>
|
|
#include <<A HREF="file:///usr/include/unistd.h">unistd.h</A>>
|
|
#include <<A HREF="file:///usr/include/pthread.h">pthread.h</A>>
|
|
#include <<A HREF="file:///usr/include/string.h">string.h</A>>
|
|
#include <<A HREF="file:///usr/include/errno.h">errno.h</A>>
|
|
<P>
|
|
#define handle_error(msg) \
|
|
<BR> do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
#define handle_error_en(en, msg) \
|
|
<BR> do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
<P>
|
|
static void *
|
|
thread_start(void *arg)
|
|
{
|
|
<BR> printf("Subthread starting infinite loop\n");
|
|
<BR> for (;;)
|
|
<BR> continue;
|
|
}
|
|
<P>
|
|
static void
|
|
pclock(char *msg, clockid_t cid)
|
|
{
|
|
<BR> struct timespec ts;
|
|
<P>
|
|
<BR> printf("%s", msg);
|
|
<BR> if (clock_gettime(cid, &ts) == -1)
|
|
<BR> handle_error("clock_gettime");
|
|
<BR> printf("%4ld.%03ld\n", ts.tv_sec, ts.tv_nsec / 1000000);
|
|
}
|
|
<P>
|
|
int
|
|
main(int argc, char *argv[])
|
|
{
|
|
<BR> pthread_t thread;
|
|
<BR> clockid_t cid;
|
|
<BR> int j, s;
|
|
<P>
|
|
<BR> s = pthread_create(&thread, NULL, thread_start, NULL);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_create");
|
|
<P>
|
|
<BR> printf("Main thread sleeping\n");
|
|
<BR> <A HREF="/cgi-bin/man/man2html?1+sleep">sleep</A>(1);
|
|
<P>
|
|
<BR> printf("Main thread consuming some CPU time...\n");
|
|
<BR> for (j = 0; j < 2000000; j++)
|
|
<BR> getppid();
|
|
<P>
|
|
<BR> pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
|
|
<P>
|
|
<BR> s = pthread_getcpuclockid(pthread_self(), &cid);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getcpuclockid");
|
|
<BR> pclock("Main thread CPU time: ", cid);
|
|
<P>
|
|
<BR> /* The preceding 4 lines of code could have been replaced by:
|
|
<BR> pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
|
|
<P>
|
|
<BR> s = pthread_getcpuclockid(thread, &cid);
|
|
<BR> if (s != 0)
|
|
<BR> handle_error_en(s, "pthread_getcpuclockid");
|
|
<BR> pclock("Subthread CPU time: 1 ", cid);
|
|
<P>
|
|
<BR> exit(EXIT_SUCCESS); /* Terminates both threads */
|
|
}
|
|
|
|
<A NAME="lbAM"> </A>
|
|
<H2>SEE ALSO</H2>
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_gettime">clock_gettime</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+clock_settime">clock_settime</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?2+timer_create">timer_create</A></B>(2),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+clock_getcpuclockid">clock_getcpuclockid</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?3+pthread_self">pthread_self</A></B>(3),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+pthreads">pthreads</A></B>(7),
|
|
|
|
<B><A HREF="/cgi-bin/man/man2html?7+time">time</A></B>(7)
|
|
|
|
<A NAME="lbAN"> </A>
|
|
<H2>COLOPHON</H2>
|
|
|
|
This page is part of release 5.05 of the Linux
|
|
<I>man-pages</I>
|
|
|
|
project.
|
|
A description of the project,
|
|
information about reporting bugs,
|
|
and the latest version of this page,
|
|
can be found at
|
|
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
|
|
<P>
|
|
|
|
<HR>
|
|
<A NAME="index"> </A><H2>Index</H2>
|
|
<DL>
|
|
<DT id="3"><A HREF="#lbAB">NAME</A><DD>
|
|
<DT id="4"><A HREF="#lbAC">SYNOPSIS</A><DD>
|
|
<DT id="5"><A HREF="#lbAD">DESCRIPTION</A><DD>
|
|
<DT id="6"><A HREF="#lbAE">RETURN VALUE</A><DD>
|
|
<DT id="7"><A HREF="#lbAF">ERRORS</A><DD>
|
|
<DT id="8"><A HREF="#lbAG">VERSIONS</A><DD>
|
|
<DT id="9"><A HREF="#lbAH">ATTRIBUTES</A><DD>
|
|
<DT id="10"><A HREF="#lbAI">CONFORMING TO</A><DD>
|
|
<DT id="11"><A HREF="#lbAJ">NOTES</A><DD>
|
|
<DT id="12"><A HREF="#lbAK">EXAMPLE</A><DD>
|
|
<DL>
|
|
<DT id="13"><A HREF="#lbAL">Program source</A><DD>
|
|
</DL>
|
|
<DT id="14"><A HREF="#lbAM">SEE ALSO</A><DD>
|
|
<DT id="15"><A HREF="#lbAN">COLOPHON</A><DD>
|
|
</DL>
|
|
<HR>
|
|
This document was created by
|
|
<A HREF="/cgi-bin/man/man2html">man2html</A>,
|
|
using the manual pages.<BR>
|
|
Time: 00:05:53 GMT, March 31, 2021
|
|
</BODY>
|
|
</HTML>
|