Blame man7/time.7

Packit 7cfc04
.\" Copyright (c) 2006 by Michael Kerrisk <mtk.manpages@gmail.com>
Packit 7cfc04
.\"
Packit 7cfc04
.\" %%%LICENSE_START(VERBATIM)
Packit 7cfc04
.\" Permission is granted to make and distribute verbatim copies of this
Packit 7cfc04
.\" manual provided the copyright notice and this permission notice are
Packit 7cfc04
.\" preserved on all copies.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Permission is granted to copy and distribute modified versions of this
Packit 7cfc04
.\" manual under the conditions for verbatim copying, provided that the
Packit 7cfc04
.\" entire resulting derived work is distributed under the terms of a
Packit 7cfc04
.\" permission notice identical to this one.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Since the Linux kernel and libraries are constantly changing, this
Packit 7cfc04
.\" manual page may be incorrect or out-of-date.  The author(s) assume no
Packit 7cfc04
.\" responsibility for errors or omissions, or for damages resulting from
Packit 7cfc04
.\" the use of the information contained herein.  The author(s) may not
Packit 7cfc04
.\" have taken the same level of care in the production of this manual,
Packit 7cfc04
.\" which is licensed free of charge, as they might when working
Packit 7cfc04
.\" professionally.
Packit 7cfc04
.\"
Packit 7cfc04
.\" Formatted or processed versions of this manual, if unaccompanied by
Packit 7cfc04
.\" the source, must acknowledge the copyright and authors of this work.
Packit 7cfc04
.\" %%%LICENSE_END
Packit 7cfc04
.\"
Packit 7cfc04
.\" 2008-06-24, mtk: added some details about where jiffies come into
Packit 7cfc04
.\"     play; added section on high-resolution timers.
Packit 7cfc04
.\"
Packit 7cfc04
.TH TIME 7 2016-03-15 "Linux" "Linux Programmer's Manual"
Packit 7cfc04
.SH NAME
Packit 7cfc04
time \- overview of time and timers
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
.SS Real time and process time
Packit 7cfc04
.I "Real time"
Packit 7cfc04
is defined as time measured from some fixed point,
Packit 7cfc04
either from a standard point in the past
Packit 7cfc04
(see the description of the Epoch and calendar time below),
Packit 7cfc04
or from some point (e.g., the start) in the life of a process
Packit 7cfc04
.RI ( "elapsed time" ).
Packit 7cfc04
.PP
Packit 7cfc04
.I "Process time"
Packit 7cfc04
is defined as the amount of CPU time used by a process.
Packit 7cfc04
This is sometimes divided into
Packit 7cfc04
.I user
Packit 7cfc04
and
Packit 7cfc04
.I system
Packit 7cfc04
components.
Packit 7cfc04
User CPU time is the time spent executing code in user mode.
Packit 7cfc04
System CPU time is the time spent by the kernel executing
Packit 7cfc04
in system mode on behalf of the process (e.g., executing system calls).
Packit 7cfc04
The
Packit 7cfc04
.BR time (1)
Packit 7cfc04
command can be used to determine the amount of CPU time consumed
Packit 7cfc04
during the execution of a program.
Packit 7cfc04
A program can determine the amount of CPU time it has consumed using
Packit 7cfc04
.BR times (2),
Packit 7cfc04
.BR getrusage (2),
Packit 7cfc04
or
Packit 7cfc04
.BR clock (3).
Packit 7cfc04
.SS The hardware clock
Packit 7cfc04
Most computers have a (battery-powered) hardware clock which the kernel
Packit 7cfc04
reads at boot time in order to initialize the software clock.
Packit 7cfc04
For further details, see
Packit 7cfc04
.BR rtc (4)
Packit 7cfc04
and
Packit 7cfc04
.BR hwclock (8).
Packit 7cfc04
.SS The software clock, HZ, and jiffies
Packit 7cfc04
The accuracy of various system calls that set timeouts,
Packit 7cfc04
(e.g.,
Packit 7cfc04
.BR select (2),
Packit 7cfc04
.BR sigtimedwait (2))
Packit 7cfc04
.\" semtimedop(), mq_timedwait(), io_getevents(), poll() are the same
Packit 7cfc04
.\" futexes and thus sem_timedwait() seem to use high-res timers.
Packit 7cfc04
and measure CPU time (e.g.,
Packit 7cfc04
.BR getrusage (2))
Packit 7cfc04
is limited by the resolution of the
Packit 7cfc04
.IR "software clock" ,
Packit 7cfc04
a clock maintained by the kernel which measures time in
Packit 7cfc04
.IR jiffies .
Packit 7cfc04
The size of a jiffy is determined by the value of the kernel constant
Packit 7cfc04
.IR HZ .
Packit 7cfc04
.PP
Packit 7cfc04
The value of
Packit 7cfc04
.I HZ
Packit 7cfc04
varies across kernel versions and hardware platforms.
Packit 7cfc04
On i386 the situation is as follows:
Packit 7cfc04
on kernels up to and including 2.4.x, HZ was 100,
Packit 7cfc04
giving a jiffy value of 0.01 seconds;
Packit 7cfc04
starting with 2.6.0, HZ was raised to 1000, giving a jiffy of
Packit 7cfc04
0.001 seconds.
Packit 7cfc04
Since kernel 2.6.13, the HZ value is a kernel
Packit 7cfc04
configuration parameter and can be 100, 250 (the default) or 1000,
Packit 7cfc04
yielding a jiffies value of, respectively, 0.01, 0.004, or 0.001 seconds.
Packit 7cfc04
Since kernel 2.6.20, a further frequency is available:
Packit 7cfc04
300, a number that divides evenly for the common video
Packit 7cfc04
frame rates (PAL, 25 HZ; NTSC, 30 HZ).
Packit 7cfc04
.PP
Packit 7cfc04
The
Packit 7cfc04
.BR times (2)
Packit 7cfc04
system call is a special case.
Packit 7cfc04
It reports times with a granularity defined by the kernel constant
Packit 7cfc04
.IR USER_HZ .
Packit 7cfc04
User-space applications can determine the value of this constant using
Packit 7cfc04
.IR sysconf(_SC_CLK_TCK) .
Packit 7cfc04
.\" glibc gets this info with a little help from the ELF loader;
Packit 7cfc04
.\" see glibc elf/dl-support.c and kernel fs/binfmt_elf.c.
Packit 7cfc04
.\"
Packit 7cfc04
.SS High-resolution timers
Packit 7cfc04
Before Linux 2.6.21, the accuracy of timer and sleep system calls
Packit 7cfc04
(see below) was also limited by the size of the jiffy.
Packit 7cfc04
.PP
Packit 7cfc04
Since Linux 2.6.21, Linux supports high-resolution timers (HRTs),
Packit 7cfc04
optionally configurable via
Packit 7cfc04
.BR CONFIG_HIGH_RES_TIMERS .
Packit 7cfc04
On a system that supports HRTs, the accuracy of sleep and timer
Packit 7cfc04
system calls is no longer constrained by the jiffy,
Packit 7cfc04
but instead can be as accurate as the hardware allows
Packit 7cfc04
(microsecond accuracy is typical of modern hardware).
Packit 7cfc04
You can determine whether high-resolution timers are supported by
Packit 7cfc04
checking the resolution returned by a call to
Packit 7cfc04
.BR clock_getres (2)
Packit 7cfc04
or looking at the "resolution" entries in
Packit 7cfc04
.IR /proc/timer_list .
Packit 7cfc04
.PP
Packit 7cfc04
HRTs are not supported on all hardware architectures.
Packit 7cfc04
(Support is provided on x86, arm, and powerpc, among others.)
Packit 7cfc04
.SS The Epoch
Packit 7cfc04
UNIX systems represent time in seconds since the
Packit 7cfc04
.IR Epoch ,
Packit 7cfc04
1970-01-01 00:00:00 +0000 (UTC).
Packit 7cfc04
.PP
Packit 7cfc04
A program can determine the
Packit 7cfc04
.I "calendar time"
Packit 7cfc04
using
Packit 7cfc04
.BR gettimeofday (2),
Packit 7cfc04
which returns time (in seconds and microseconds) that have
Packit 7cfc04
elapsed since the Epoch;
Packit 7cfc04
.BR time (2)
Packit 7cfc04
provides similar information, but only with accuracy to the
Packit 7cfc04
nearest second.
Packit 7cfc04
The system time can be changed using
Packit 7cfc04
.BR settimeofday (2).
Packit 7cfc04
.SS Broken-down time
Packit 7cfc04
Certain library functions use a structure of
Packit 7cfc04
type
Packit 7cfc04
.I tm
Packit 7cfc04
to represent
Packit 7cfc04
.IR "broken-down time" ,
Packit 7cfc04
which stores time value separated out into distinct components
Packit 7cfc04
(year, month, day, hour, minute, second, etc.).
Packit 7cfc04
This structure is described in
Packit 7cfc04
.BR ctime (3),
Packit 7cfc04
which also describes functions that convert between calendar time and
Packit 7cfc04
broken-down time.
Packit 7cfc04
Functions for converting between broken-down time and printable
Packit 7cfc04
string representations of the time are described in
Packit 7cfc04
.BR ctime (3),
Packit 7cfc04
.BR strftime (3),
Packit 7cfc04
and
Packit 7cfc04
.BR strptime (3).
Packit 7cfc04
.SS Sleeping and setting timers
Packit 7cfc04
Various system calls and functions allow a program to sleep
Packit 7cfc04
(suspend execution) for a specified period of time; see
Packit 7cfc04
.BR nanosleep (2),
Packit 7cfc04
.BR clock_nanosleep (2),
Packit 7cfc04
and
Packit 7cfc04
.BR sleep (3).
Packit 7cfc04
.PP
Packit 7cfc04
Various system calls allow a process to set a timer that expires
Packit 7cfc04
at some point in the future, and optionally at repeated intervals;
Packit 7cfc04
see
Packit 7cfc04
.BR alarm (2),
Packit 7cfc04
.BR getitimer (2),
Packit 7cfc04
.BR timerfd_create (2),
Packit 7cfc04
and
Packit 7cfc04
.BR timer_create (2).
Packit 7cfc04
.SS Timer slack
Packit 7cfc04
Since Linux 2.6.28, it is possible to control the "timer slack"
Packit 7cfc04
value for a thread.
Packit 7cfc04
The timer slack is the length of time by
Packit 7cfc04
which the kernel may delay the wake-up of certain
Packit 7cfc04
system calls that block with a timeout.
Packit 7cfc04
Permitting this delay allows the kernel to coalesce wake-up events,
Packit 7cfc04
thus possibly reducing the number of system wake-ups and saving power.
Packit 7cfc04
For more details, see the description of
Packit 7cfc04
.B PR_SET_TIMERSLACK
Packit 7cfc04
in
Packit 7cfc04
.BR prctl (2).
Packit 7cfc04
.SH SEE ALSO
Packit 7cfc04
.ad l
Packit 7cfc04
.nh
Packit 7cfc04
.BR date (1),
Packit 7cfc04
.BR time (1),
Packit 7cfc04
.BR timeout (1),
Packit 7cfc04
.BR adjtimex (2),
Packit 7cfc04
.BR alarm (2),
Packit 7cfc04
.BR clock_gettime (2),
Packit 7cfc04
.BR clock_nanosleep (2),
Packit 7cfc04
.BR getitimer (2),
Packit 7cfc04
.BR getrlimit (2),
Packit 7cfc04
.BR getrusage (2),
Packit 7cfc04
.BR gettimeofday (2),
Packit 7cfc04
.BR nanosleep (2),
Packit 7cfc04
.BR stat (2),
Packit 7cfc04
.BR time (2),
Packit 7cfc04
.BR timer_create (2),
Packit 7cfc04
.BR timerfd_create (2),
Packit 7cfc04
.BR times (2),
Packit 7cfc04
.BR utime (2),
Packit 7cfc04
.BR adjtime (3),
Packit 7cfc04
.BR clock (3),
Packit 7cfc04
.BR clock_getcpuclockid (3),
Packit 7cfc04
.BR ctime (3),
Packit 7cfc04
.BR ntp_adjtime (3),
Packit 7cfc04
.BR ntp_gettime (3),
Packit 7cfc04
.BR pthread_getcpuclockid (3),
Packit 7cfc04
.BR sleep (3),
Packit 7cfc04
.BR strftime (3),
Packit 7cfc04
.BR strptime (3),
Packit 7cfc04
.BR timeradd (3),
Packit 7cfc04
.BR usleep (3),
Packit 7cfc04
.BR rtc (4),
Packit 7cfc04
.BR hwclock (8)
Packit 7cfc04
.SH COLOPHON
Packit 7cfc04
This page is part of release 4.15 of the Linux
Packit 7cfc04
.I man-pages
Packit 7cfc04
project.
Packit 7cfc04
A description of the project,
Packit 7cfc04
information about reporting bugs,
Packit 7cfc04
and the latest version of this page,
Packit 7cfc04
can be found at
Packit 7cfc04
\%https://www.kernel.org/doc/man\-pages/.