OSDN Git Service

bab561deffd2238cbc768f319f8ac715ef5b2ff1
[linuxjm/LDP_man-pages.git] / original / man7 / time.7
1 .\" Copyright (c) 2006 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein.  The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" 2008-06-24, mtk: added some details about where jiffies come into
24 .\"     play; added section on high-resolution timers.
25 .\"
26 .TH TIME 7 2010-02-25 "Linux" "Linux Programmer's Manual"
27 .SH NAME
28 time \- overview of time and timers
29 .SH DESCRIPTION
30 .SS "Real time and process time"
31 .I "Real time"
32 is defined as time measured from some fixed point,
33 either from a standard point in the past
34 (see the description of the Epoch and calendar time below),
35 or from some point (e.g., the start) in the life of a process
36 .RI ( "elapsed time" ).
37
38 .I "Process time"
39 is defined as the amount of CPU time used by a process.
40 This is sometimes divided into
41 .I user
42 and
43 .I system
44 components.
45 User CPU time is the time spent executing code in user mode.
46 System CPU time is the time spent by the kernel executing
47 in system mode on behalf of the process (e.g., executing system calls).
48 The
49 .BR time (1)
50 command can be used to determine the amount of CPU time consumed
51 during the execution of a program.
52 A program can determine the amount of CPU time it has consumed using
53 .BR times (2),
54 .BR getrusage (2),
55 or
56 .BR clock (3).
57 .SS "The Hardware Clock"
58 Most computers have a (battery-powered) hardware clock which the kernel
59 reads at boot time in order to initialize the software clock.
60 For further details, see
61 .BR rtc (4)
62 and
63 .BR hwclock (8).
64 .SS "The Software Clock, HZ, and Jiffies"
65 The accuracy of various system calls that set timeouts,
66 (e.g.,
67 .BR select (2),
68 .BR sigtimedwait (2))
69 .\" semtimedop(), mq_timedwait(), io_getevents(), poll() are the same
70 .\" futexes and thus sem_timedwait() seem to use high-res timers.
71 and measure CPU time (e.g.,
72 .BR getrusage (2))
73 is limited by the resolution of the
74 .IR "software clock" ,
75 a clock maintained by the kernel which measures time in
76 .IR jiffies .
77 The size of a jiffy is determined by the value of the kernel constant
78 .IR HZ .
79
80 The value of
81 .I HZ
82 varies across kernel versions and hardware platforms.
83 On i386 the situation is as follows:
84 on kernels up to and including 2.4.x, HZ was 100,
85 giving a jiffy value of 0.01 seconds;
86 starting with 2.6.0, HZ was raised to 1000, giving a jiffy of
87 0.001 seconds.
88 Since kernel 2.6.13, the HZ value is a kernel
89 configuration parameter and can be 100, 250 (the default) or 1000,
90 yielding a jiffies value of, respectively, 0.01, 0.004, or 0.001 seconds.
91 Since kernel 2.6.20, a further frequency is available:
92 300, a number that divides evenly for the common video
93 frame rates (PAL, 25 HZ; NTSC, 30 HZ).
94
95 The
96 .BR times (2)
97 system call is a special case.
98 It reports times with a granularity defined by the kernel constant
99 .IR USER_HZ .
100 Userspace applications can determine the value of this constant using
101 .IR sysconf(_SC_CLK_TCK) .
102 .\" glibc gets this info with a little help from the ELF loader;
103 .\" see glibc elf/dl-support.c and kernel fs/binfmt_elf.c.
104 .\"
105 .SS "High-Resolution Timers"
106 Before Linux 2.6.21, the accuracy of timer and sleep system calls
107 (see below) was also limited by the size of the jiffy.
108
109 Since Linux 2.6.21, Linux supports high-resolution timers (HRTs),
110 optionally configurable via
111 .BR CONFIG_HIGH_RES_TIMERS .
112 On a system that supports HRTs, the accuracy of sleep and timer
113 system calls is no longer constrained by the jiffy,
114 but instead can be as accurate as the hardware allows
115 (microsecond accuracy is typical of modern hardware).
116 You can determine whether high-resolution timers are supported by
117 checking the resolution returned by a call to
118 .BR clock_getres (2)
119 or looking at the "resolution" entries in
120 .IR /proc/timer_list .
121
122 HRTs are not supported on all hardware architectures.
123 (Support is provided on x86, arm, and powerpc, among others.)
124 .SS "The Epoch"
125 UNIX systems represent time in seconds since the
126 .IR Epoch ,
127 1970-01-01 00:00:00 +0000 (UTC).
128
129 A program can determine the
130 .I "calendar time"
131 using
132 .BR gettimeofday (2),
133 which returns time (in seconds and microseconds) that have
134 elapsed since the Epoch;
135 .BR time (2)
136 provides similar information, but only with accuracy to the
137 nearest second.
138 The system time can be changed using
139 .BR settimeofday (2).
140 .SS "Broken-down time"
141 Certain library functions use a structure of
142 type
143 .I tm
144 to represent
145 .IR "broken-down time" ,
146 which stores time value separated out into distinct components
147 (year, month, day, hour, minute, second, etc.).
148 This structure is described in
149 .BR ctime (3),
150 which also describes functions that convert between calendar time and
151 broken-down time.
152 Functions for converting between broken-down time and printable
153 string representations of the time are described in
154 .BR ctime (3),
155 .BR strftime (3),
156 and
157 .BR strptime (3).
158 .SS "Sleeping and Setting Timers"
159 Various system calls and functions allow a program to sleep
160 (suspend execution) for a specified period of time; see
161 .BR nanosleep (2),
162 .BR clock_nanosleep (2),
163 and
164 .BR sleep (3).
165
166 Various system calls allow a process to set a timer that expires
167 at some point in the future, and optionally at repeated intervals;
168 see
169 .BR alarm (2),
170 .BR getitimer (2),
171 .BR timerfd_create (2),
172 and
173 .BR timer_create (2).
174 .SH "SEE ALSO"
175 .BR date (1),
176 .BR time (1),
177 .BR adjtimex (2),
178 .BR alarm (2),
179 .BR clock_gettime (2),
180 .BR clock_nanosleep (2),
181 .BR getitimer (2),
182 .BR getrlimit (2),
183 .BR getrusage (2),
184 .BR gettimeofday (2),
185 .BR nanosleep (2),
186 .BR stat (2),
187 .BR time (2),
188 .BR timer_create (2),
189 .BR timerfd_create (2),
190 .BR times (2),
191 .BR utime (2),
192 .BR adjtime (3),
193 .BR clock (3),
194 .BR clock_getcpuclockid (3),
195 .BR ctime (3),
196 .BR pthread_getcpuclockid (3),
197 .BR sleep (3),
198 .BR strftime (3),
199 .BR strptime (3),
200 .BR timeradd (3),
201 .BR usleep (3),
202 .BR rtc (4),
203 .BR hwclock (8)