man-pages/man3/pthread_getcpuclockid.3.html
2021-03-31 01:06:50 +01:00

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">&nbsp;</A>
<H2>NAME</H2>
pthread_getcpuclockid - retrieve ID of a thread's CPU time clock
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;</B>
<B>#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;</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">&nbsp;</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">&nbsp;</A>
<H2>RETURN VALUE</H2>
On success, this function returns 0;
on error, it returns a nonzero error number.
<A NAME="lbAF">&nbsp;</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">&nbsp;</A>
<H2>VERSIONS</H2>
This function is available in glibc since version 2.2.
<A NAME="lbAH">&nbsp;</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">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<A NAME="lbAJ">&nbsp;</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">&nbsp;</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">&nbsp;</A>
<H3>Program source</H3>
/* Link with &quot;-lrt&quot; */
<P>
#include &lt;<A HREF="file:///usr/include/time.h">time.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/pthread.h">pthread.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/errno.h">errno.h</A>&gt;
<P>
#define handle_error(msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
#define handle_error_en(en, msg) \
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;do&nbsp;{&nbsp;errno&nbsp;=&nbsp;en;&nbsp;perror(msg);&nbsp;exit(EXIT_FAILURE);&nbsp;}&nbsp;while&nbsp;(0)
<P>
static void *
thread_start(void *arg)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Subthread&nbsp;starting&nbsp;infinite&nbsp;loop\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(;;)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;continue;
}
<P>
static void
pclock(char *msg, clockid_t cid)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;struct&nbsp;timespec&nbsp;ts;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%s&quot;,&nbsp;msg);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(clock_gettime(cid,&nbsp;&amp;ts)&nbsp;==&nbsp;-1)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error(&quot;clock_gettime&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;%4ld.%03ld\n&quot;,&nbsp;ts.tv_sec,&nbsp;ts.tv_nsec&nbsp;/&nbsp;1000000);
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pthread_t&nbsp;thread;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;clockid_t&nbsp;cid;
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j,&nbsp;s;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_create(&amp;thread,&nbsp;NULL,&nbsp;thread_start,&nbsp;NULL);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_create&quot;);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Main&nbsp;thread&nbsp;sleeping\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;<A HREF="/cgi-bin/man/man2html?1+sleep">sleep</A>(1);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;printf(&quot;Main&nbsp;thread&nbsp;consuming&nbsp;some&nbsp;CPU&nbsp;time...\n&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;0;&nbsp;j&nbsp;&lt;&nbsp;2000000;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;getppid();
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pclock(&quot;Process&nbsp;total&nbsp;CPU&nbsp;time:&nbsp;&quot;,&nbsp;CLOCK_PROCESS_CPUTIME_ID);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_getcpuclockid(pthread_self(),&nbsp;&amp;cid);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_getcpuclockid&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pclock(&quot;Main&nbsp;thread&nbsp;CPU&nbsp;time:&nbsp;&nbsp;&nbsp;&quot;,&nbsp;cid);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;preceding&nbsp;4&nbsp;lines&nbsp;of&nbsp;code&nbsp;could&nbsp;have&nbsp;been&nbsp;replaced&nbsp;by:
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pclock(&quot;Main&nbsp;thread&nbsp;CPU&nbsp;time:&nbsp;&nbsp;&nbsp;&quot;,&nbsp;CLOCK_THREAD_CPUTIME_ID);&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;s&nbsp;=&nbsp;pthread_getcpuclockid(thread,&nbsp;&amp;cid);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(s&nbsp;!=&nbsp;0)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;handle_error_en(s,&nbsp;&quot;pthread_getcpuclockid&quot;);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;pclock(&quot;Subthread&nbsp;CPU&nbsp;time:&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&quot;,&nbsp;cid);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;Terminates&nbsp;both&nbsp;threads&nbsp;*/
}
<A NAME="lbAM">&nbsp;</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">&nbsp;</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">&nbsp;</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>