2009-01-12 19:03:02 +00:00
|
|
|
.\" Copyright (c) 2009 Linux Foundation, written by Michael Kerrisk
|
|
|
|
.\" <mtk.manpages@gmail.com>
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to make and distribute verbatim copies of this
|
|
|
|
.\" manual provided the copyright notice and this permission notice are
|
|
|
|
.\" preserved on all copies.
|
|
|
|
.\"
|
|
|
|
.\" Permission is granted to copy and distribute modified versions of this
|
|
|
|
.\" manual under the conditions for verbatim copying, provided that the
|
|
|
|
.\" entire resulting derived work is distributed under the terms of a
|
|
|
|
.\" permission notice identical to this one.
|
|
|
|
.\"
|
|
|
|
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
|
|
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
|
|
.\" responsibility for errors or omissions, or for damages resulting from
|
|
|
|
.\" the use of the information contained herein. The author(s) may not
|
|
|
|
.\" have taken the same level of care in the production of this manual,
|
|
|
|
.\" which is licensed free of charge, as they might when working
|
|
|
|
.\" professionally.
|
|
|
|
.\"
|
|
|
|
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
|
|
.\" the source, must acknowledge the copyright and authors of this work.
|
|
|
|
.\"
|
2009-02-07 09:23:45 +00:00
|
|
|
.TH PTHREAD_GETCPUCLOCKID 3 2009-02-08 "Linux" "Linux Programmer's Manual"
|
2009-01-12 19:03:02 +00:00
|
|
|
.SH NAME
|
|
|
|
pthread_getcpuclockid \- retrieve ID of a thread's CPU time clock
|
|
|
|
.SH SYNOPSIS
|
|
|
|
.nf
|
|
|
|
.B #include <pthread.h>
|
|
|
|
.B #include <time.h>
|
|
|
|
|
2009-02-07 09:23:45 +00:00
|
|
|
.BI "int pthread_getcpuclockid(pthread_t " thread ", clockid_t *" clock_id);
|
2009-01-12 19:03:02 +00:00
|
|
|
.sp
|
|
|
|
Compile and link with \fI\-pthread\fP.
|
|
|
|
.SH DESCRIPTION
|
|
|
|
The
|
|
|
|
.BR pthread_getcpuclockid ()
|
2009-09-27 07:26:25 +00:00
|
|
|
function returns the clock ID for the CPU time clock of the thread
|
2009-01-12 19:03:02 +00:00
|
|
|
.IR thread .
|
|
|
|
.\" The clockid is constructed as follows:
|
|
|
|
.\" *clockid = CLOCK_THREAD_CPUTIME_ID | (pd->tid << CLOCK_IDFIELD_SIZE)
|
|
|
|
.\" where CLOCK_IDFIELD_SIZE is 3.
|
|
|
|
.SH RETURN VALUE
|
|
|
|
On success, this function returns 0;
|
intro.1, time.1, adjtimex.2, capget.2, eventfd.2, fcntl.2, getrlimit.2, getsockopt.2, gettimeofday.2, intro.2, ioctl_list.2, ioperm.2, mlock.2, pivot_root.2, poll.2, prctl.2, ptrace.2, sched_setscheduler.2, select_tut.2, semget.2, sigaltstack.2, signalfd.2, sysctl.2, timer_settime.2, timerfd_create.2, wait.2, CPU_SET.3, argz_add.3, assert_perror.3, atexit.3, backtrace.3, bcmp.3, clearenv.3, ctime.3, dl_iterate_phdr.3, dlopen.3, ecvt.3, errno.3, error.3, ether_aton.3, exit.3, fenv.3, ferror.3, finite.3, flockfile.3, fnmatch.3, fpathconf.3, fpclassify.3, ftime.3, ftok.3, ftw.3, fwide.3, getaddrinfo.3, gethostbyname.3, getlogin.3, getnameinfo.3, getnetent.3, getopt.3, getprotoent.3, getrpcent.3, getservent.3, glob.3, hsearch.3, inet.3, isalpha.3, iswalnum.3, iswalpha.3, iswblank.3, iswcntrl.3, iswctype.3, iswdigit.3, iswgraph.3, iswlower.3, iswprint.3, iswpunct.3, iswspace.3, iswupper.3, iswxdigit.3, longjmp.3, lsearch.3, malloc.3, matherr.3, mblen.3, mbsinit.3, mbtowc.3, on_exit.3, printf.3, pthread_attr_init.3, pthread_attr_setaffinity_np.3, pthread_attr_setdetachstate.3, pthread_attr_setguardsize.3, pthread_attr_setinheritsched.3, pthread_attr_setschedparam.3, pthread_attr_setschedpolicy.3, pthread_attr_setscope.3, pthread_attr_setstack.3, pthread_attr_setstackaddr.3, pthread_attr_setstacksize.3, pthread_cancel.3, pthread_cleanup_push.3, pthread_equal.3, pthread_getattr_np.3, pthread_getcpuclockid.3, pthread_setaffinity_np.3, pthread_setcancelstate.3, pthread_setconcurrency.3, pthread_setschedparam.3, pthread_setschedprio.3, ptsname.3, putenv.3, putgrent.3, raise.3, rcmd.3, regex.3, rexec.3, rpc.3, rpmatch.3, rtnetlink.3, scandir.3, sem_init.3, setaliasent.3, setbuf.3, setenv.3, setjmp.3, signbit.3, stdio_ext.3, strtod.3, strtol.3, strtoul.3, system.3, termios.3, timeradd.3, tzset.3, ualarm.3, wctomb.3, xdr.3, st.4, tty_ioctl.4, core.5, elf.5, proc.5, bootparam.7, capabilities.7, icmp.7, ip.7, ipv6.7, math_error.7, mdoc.samples.7, mq_overview.7, pthreads.7, raw.7, regex.7, socket.7, tcp.7, tzselect.8: Global fix: s/non-zero/nonzero/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "non-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 16:40:55 +00:00
|
|
|
on error, it returns a nonzero error number.
|
2009-01-12 19:03:02 +00:00
|
|
|
.SH ERRORS
|
|
|
|
.TP
|
|
|
|
.B ENOENT
|
|
|
|
.\" CLOCK_THREAD_CPUTIME_ID not defined
|
|
|
|
Per-thread CPU time clocks are not supported by the system.
|
|
|
|
.\"
|
|
|
|
.\" Looking at nptl/pthread_getcpuclockid.c an ERANGE error would
|
|
|
|
.\" be possible if kernel thread IDs took more than 29 bits (which
|
|
|
|
.\" they currently cannot).
|
|
|
|
.TP
|
|
|
|
.B ESRCH
|
|
|
|
No thread with the ID
|
|
|
|
.I thread
|
|
|
|
could be found.
|
|
|
|
.SH VERSIONS
|
|
|
|
This function is available in glibc since version 2.2.
|
|
|
|
.SH CONFORMING TO
|
|
|
|
POSIX.1-2001.
|
|
|
|
.SH NOTES
|
|
|
|
When
|
|
|
|
.I thread
|
|
|
|
refers to the calling thread,
|
|
|
|
this function returns an identifier that refers to the same clock
|
|
|
|
manipulated by
|
clock_nanosleep.2, getrusage.2, timerfd_create.2, clock.3, clock_getcpuclockid.3, ftime.3, pthread_create.3, pthread_getcpuclockid.3, pthread_tryjoin_np.3, sem_wait.3, time.7: Global fix: Fix xrefs to clock_*.? pages to reflect move to section 2
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 21:10:39 +00:00
|
|
|
.BR clock_gettime (2)
|
2009-01-12 19:03:02 +00:00
|
|
|
and
|
clock_nanosleep.2, getrusage.2, timerfd_create.2, clock.3, clock_getcpuclockid.3, ftime.3, pthread_create.3, pthread_getcpuclockid.3, pthread_tryjoin_np.3, sem_wait.3, time.7: Global fix: Fix xrefs to clock_*.? pages to reflect move to section 2
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 21:10:39 +00:00
|
|
|
.BR clock_settime (2)
|
2009-01-12 19:03:02 +00:00
|
|
|
when given the clock ID
|
|
|
|
.BR CLOCK_THREAD_CPUTIME_ID .
|
|
|
|
.SH EXAMPLE
|
|
|
|
The program below creates a thread and then uses
|
clock_nanosleep.2, getrusage.2, timerfd_create.2, clock.3, clock_getcpuclockid.3, ftime.3, pthread_create.3, pthread_getcpuclockid.3, pthread_tryjoin_np.3, sem_wait.3, time.7: Global fix: Fix xrefs to clock_*.? pages to reflect move to section 2
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 21:10:39 +00:00
|
|
|
.BR clock_gettime (2)
|
2009-01-12 19:03:02 +00:00
|
|
|
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:
|
|
|
|
.in +4n
|
|
|
|
.nf
|
|
|
|
$ \fB./a.out\fP
|
|
|
|
Main thread sleeping
|
ioctl_list.2, mount.2, select_tut.2, stat.2, getifaddrs.3, pthread_cleanup_push_defer_np.3, pthread_getcpuclockid.3, proc.5, bootparam.7: Global fix: s/sub-/sub/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "sub-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:36:10 +00:00
|
|
|
Subthread starting infinite loop
|
2009-01-12 19:03:02 +00:00
|
|
|
Main thread consuming some CPU time...
|
|
|
|
Process total CPU time: 1.368
|
|
|
|
Main thread CPU time: 0.376
|
ioctl_list.2, mount.2, select_tut.2, stat.2, getifaddrs.3, pthread_cleanup_push_defer_np.3, pthread_getcpuclockid.3, proc.5, bootparam.7: Global fix: s/sub-/sub/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "sub-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:36:10 +00:00
|
|
|
Subthread CPU time: 0.992
|
2009-01-12 19:03:02 +00:00
|
|
|
.fi
|
|
|
|
.in
|
|
|
|
.SS Program source
|
|
|
|
\&
|
|
|
|
.nf
|
|
|
|
/* Link with "-lrt" */
|
|
|
|
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#define handle_error(msg) \\
|
|
|
|
do { perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
|
|
|
|
|
|
#define handle_error_en(en, msg) \\
|
|
|
|
do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
|
|
|
|
|
|
|
|
static void *
|
|
|
|
thread_start(void *arg)
|
|
|
|
{
|
ioctl_list.2, mount.2, select_tut.2, stat.2, getifaddrs.3, pthread_cleanup_push_defer_np.3, pthread_getcpuclockid.3, proc.5, bootparam.7: Global fix: s/sub-/sub/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "sub-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:36:10 +00:00
|
|
|
printf("Subthread starting infinite loop\\n");
|
2009-01-12 19:03:02 +00:00
|
|
|
for (;;)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
pclock(char *msg, clockid_t cid)
|
|
|
|
{
|
|
|
|
struct timespec ts;
|
|
|
|
|
|
|
|
printf("%s", msg);
|
|
|
|
if (clock_gettime(cid, &ts) == \-1)
|
|
|
|
handle_error("clock_gettime");
|
|
|
|
printf("%4ld.%03ld\\n", ts.tv_sec, ts.tv_nsec / 1000000);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
pthread_t thread;
|
|
|
|
clockid_t cid;
|
|
|
|
int j, s;
|
|
|
|
|
|
|
|
s = pthread_create(&thread, NULL, thread_start, NULL);
|
|
|
|
if (s != 0)
|
|
|
|
handle_error_en(s, "pthread_create");
|
|
|
|
|
|
|
|
printf("Main thread sleeping\\n");
|
|
|
|
sleep(1);
|
|
|
|
|
|
|
|
printf("Main thread consuming some CPU time...\\n");
|
|
|
|
for (j = 0; j < 2000000; j++)
|
|
|
|
getppid();
|
|
|
|
|
|
|
|
pclock("Process total CPU time: ", CLOCK_PROCESS_CPUTIME_ID);
|
|
|
|
|
|
|
|
s = pthread_getcpuclockid(pthread_self(), &cid);
|
|
|
|
if (s != 0)
|
|
|
|
handle_error_en(s, "pthread_getcpuclockid");
|
|
|
|
pclock("Main thread CPU time: ", cid);
|
|
|
|
|
|
|
|
/* The preceding 4 lines of code could have been replaced by:
|
|
|
|
pclock("Main thread CPU time: ", CLOCK_THREAD_CPUTIME_ID); */
|
|
|
|
|
|
|
|
s = pthread_getcpuclockid(thread, &cid);
|
|
|
|
if (s != 0)
|
|
|
|
handle_error_en(s, "pthread_getcpuclockid");
|
ioctl_list.2, mount.2, select_tut.2, stat.2, getifaddrs.3, pthread_cleanup_push_defer_np.3, pthread_getcpuclockid.3, proc.5, bootparam.7: Global fix: s/sub-/sub/
The tendency in English, as prescribed in style guides like
Chicago MoS, is towards removing hyphens after prefixes
like "sub-" etc.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2010-01-16 17:36:10 +00:00
|
|
|
pclock("Subthread CPU time: 1 ", cid);
|
2009-01-12 19:03:02 +00:00
|
|
|
|
|
|
|
exit(EXIT_SUCCESS); /* Terminates both threads */
|
|
|
|
}
|
|
|
|
.fi
|
|
|
|
.SH SEE ALSO
|
clock_nanosleep.2, getrusage.2, timerfd_create.2, clock.3, clock_getcpuclockid.3, ftime.3, pthread_create.3, pthread_getcpuclockid.3, pthread_tryjoin_np.3, sem_wait.3, time.7: Global fix: Fix xrefs to clock_*.? pages to reflect move to section 2
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 21:10:39 +00:00
|
|
|
.BR clock_gettime (2),
|
|
|
|
.BR clock_settime (2),
|
clock_nanosleep.2, execve.2, fork.2, nanosleep.2, sigaction.2, timerfd_create.2, pthread_getcpuclockid.3, ualarm.3, usleep.3, pthreads.7, time.7: Global fix: s/(3)/(2)/ in section number xrefs for timer_*() API
The POSIX timers API is implemented (mostly) within the kernel,
so thse interfaces are system calls. Although there are as yet
no man pages, when they are added they should be in Section 2,
not 3. Therefore fix those pages that currently refer to these
interfaces as being in Section 3.
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 20:21:03 +00:00
|
|
|
.BR timer_create (2),
|
clock_nanosleep.2, getrusage.2, timerfd_create.2, clock.3, clock_getcpuclockid.3, ftime.3, pthread_create.3, pthread_getcpuclockid.3, pthread_tryjoin_np.3, sem_wait.3, time.7: Global fix: Fix xrefs to clock_*.? pages to reflect move to section 2
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2009-02-07 21:10:39 +00:00
|
|
|
.BR clock_getcpuclockid (3),
|
2009-01-12 19:03:02 +00:00
|
|
|
.BR pthread_self (3),
|
|
|
|
.BR pthreads (7),
|
|
|
|
.BR time (7)
|