Debian Codegraph @

upstream/debian-trixie/man7/time.7

raw
1 .\" Copyright (c) 2006 by Michael Kerrisk <[email protected]>
2 .\"
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
4 .\"
5 .\" 2008-06-24, mtk: added some details about where jiffies come into
6 .\" play; added section on high-resolution timers.
7 .\"
8 .TH time 7 2024-05-02 "Linux man-pages 6.9.1"
9 .SH NAME
10 time \- overview of time and timers
11 .SH DESCRIPTION
12 .SS Real time and process time
13 .I "Real time"
14 is defined as time measured from some fixed point,
15 either from a standard point in the past
16 (see the description of the Epoch and calendar time below),
17 or from some point (e.g., the start) in the life of a process
18 .RI ( "elapsed time" ).
19 .P
20 .I "Process time"
21 is defined as the amount of CPU time used by a process.
22 This is sometimes divided into
23 .I user
24 and
25 .I system
26 components.
27 User CPU time is the time spent executing code in user mode.
28 System CPU time is the time spent by the kernel executing
29 in system mode on behalf of the process (e.g., executing system calls).
30 The
31 .BR time (1)
32 command can be used to determine the amount of CPU time consumed
33 during the execution of a program.
34 A program can determine the amount of CPU time it has consumed using
35 .BR times (2),
36 .BR getrusage (2),
37 or
38 .BR clock (3).
39 .SS The hardware clock
40 Most computers have a (battery-powered) hardware clock which the kernel
41 reads at boot time in order to initialize the software clock.
42 For further details, see
43 .BR rtc (4)
44 and
45 .BR hwclock (8).
46 .SS The software clock, HZ, and jiffies
47 The accuracy of various system calls that set timeouts,
48 (e.g.,
49 .BR select (2),
50 .BR sigtimedwait (2))
51 .\" semtimedop(), mq_timedwait(), io_getevents(), poll() are the same
52 .\" futexes and thus sem_timedwait() seem to use high-res timers.
53 and measure CPU time (e.g.,
54 .BR getrusage (2))
55 is limited by the resolution of the
56 .IR "software clock" ,
57 a clock maintained by the kernel which measures time in
58 .IR jiffies .
59 The size of a jiffy is determined by the value of the kernel constant
60 .IR HZ .
61 .P
62 The value of
63 .I HZ
64 varies across kernel versions and hardware platforms.
65 On i386 the situation is as follows:
66 on kernels up to and including Linux 2.4.x,
67 HZ was 100,
68 giving a jiffy value of 0.01 seconds;
69 starting with Linux 2.6.0,
70 HZ was raised to 1000,
71 giving a jiffy of 0.001 seconds.
72 Since Linux 2.6.13, the HZ value is a kernel
73 configuration parameter and can be 100, 250 (the default) or 1000,
74 yielding a jiffies value of, respectively, 0.01, 0.004, or 0.001 seconds.
75 Since Linux 2.6.20, a further frequency is available:
76 300, a number that divides evenly for the common video frame rates
77 (PAL, 25 Hz; NTSC, 30 Hz).
78 .P
79 The
80 .BR times (2)
81 system call is a special case.
82 It reports times with a granularity defined by the kernel constant
83 .IR USER_HZ .
84 User-space applications can determine the value of this constant using
85 .IR sysconf(_SC_CLK_TCK) .
86 .\" glibc gets this info with a little help from the ELF loader;
87 .\" see glibc elf/dl-support.c and kernel fs/binfmt_elf.c.
88 .\"
89 .SS System and process clocks; time namespaces
90 The kernel supports a range of clocks that measure various kinds of
91 elapsed and virtual (i.e., consumed CPU) time.
92 These clocks are described in
93 .BR clock_gettime (2).
94 A few of the clocks are settable using
95 .BR clock_settime (2).
96 The values of certain clocks are virtualized by time namespaces; see
97 .BR time_namespaces (7).
98 .\"
99 .SS High-resolution timers
100 Before Linux 2.6.21, the accuracy of timer and sleep system calls
101 (see below) was also limited by the size of the jiffy.
102 .P
103 Since Linux 2.6.21, Linux supports high-resolution timers (HRTs),
104 optionally configurable via
105 .BR CONFIG_HIGH_RES_TIMERS .
106 On a system that supports HRTs, the accuracy of sleep and timer
107 system calls is no longer constrained by the jiffy,
108 but instead can be as accurate as the hardware allows
109 (microsecond accuracy is typical of modern hardware).
110 You can determine whether high-resolution timers are supported by
111 checking the resolution returned by a call to
112 .BR clock_getres (2)
113 or looking at the "resolution" entries in
114 .IR /proc/timer_list .
115 .P
116 HRTs are not supported on all hardware architectures.
117 (Support is provided on x86, ARM, and PowerPC, among others.)
118 .SS The Epoch
119 UNIX systems represent time in seconds since the
120 .IR Epoch ,
121 1970-01-01 00:00:00 +0000 (UTC).
122 .P
123 A program can determine the
124 .I "calendar time"
125 via the
126 .BR clock_gettime (2)
127 .B CLOCK_REALTIME
128 clock,
129 which returns time (in seconds and nanoseconds) that have
130 elapsed since the Epoch;
131 .BR time (2)
132 provides similar information, but only with accuracy to the
133 nearest second.
134 The system time can be changed using
135 .BR clock_settime (2).
136 .\"
137 .SS Broken-down time
138 Certain library functions use a structure of
139 type
140 .I tm
141 to represent
142 .IR "broken-down time" ,
143 which stores time value separated out into distinct components
144 (year, month, day, hour, minute, second, etc.).
145 This structure is described in
146 .BR tm (3type),
147 which also describes functions that convert between calendar time and
148 broken-down time.
149 Functions for converting between broken-down time and printable
150 string representations of the time are described in
151 .BR ctime (3),
152 .BR strftime (3),
153 and
154 .BR strptime (3).
155 .SS Sleeping and setting timers
156 Various system calls and functions allow a program to sleep
157 (suspend execution) for a specified period of time; see
158 .BR nanosleep (2),
159 .BR clock_nanosleep (2),
160 and
161 .BR sleep (3).
162 .P
163 Various system calls allow a process to set a timer that expires
164 at some point in the future, and optionally at repeated intervals;
165 see
166 .BR alarm (2),
167 .BR getitimer (2),
168 .BR timerfd_create (2),
169 and
170 .BR timer_create (2).
171 .SS Timer slack
172 Since Linux 2.6.28, it is possible to control the "timer slack"
173 value for a thread.
174 The timer slack is the length of time by
175 which the kernel may delay the wake-up of certain
176 system calls that block with a timeout.
177 Permitting this delay allows the kernel to coalesce wake-up events,
178 thus possibly reducing the number of system wake-ups and saving power.
179 For more details, see the description of
180 .B PR_SET_TIMERSLACK
181 in
182 .BR prctl (2).
183 .SH SEE ALSO
184 .ad l
185 .nh
186 .BR date (1),
187 .BR time (1),
188 .BR timeout (1),
189 .BR adjtimex (2),
190 .BR alarm (2),
191 .BR clock_gettime (2),
192 .BR clock_nanosleep (2),
193 .BR getitimer (2),
194 .BR getrlimit (2),
195 .BR getrusage (2),
196 .BR gettimeofday (2),
197 .BR nanosleep (2),
198 .BR stat (2),
199 .BR time (2),
200 .BR timer_create (2),
201 .BR timerfd_create (2),
202 .BR times (2),
203 .BR utime (2),
204 .BR adjtime (3),
205 .BR clock (3),
206 .BR clock_getcpuclockid (3),
207 .BR ctime (3),
208 .BR ntp_adjtime (3),
209 .BR ntp_gettime (3),
210 .BR pthread_getcpuclockid (3),
211 .BR sleep (3),
212 .BR strftime (3),
213 .BR strptime (3),
214 .BR timeradd (3),
215 .BR usleep (3),
216 .BR rtc (4),
217 .BR time_namespaces (7),
218 .BR hwclock (8)