1 .\" Copyright (c) 2020 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH time_namespaces 7 (date) "Linux man-pages (unreleased)"
8 time_namespaces \- overview of Linux time namespaces
10 Time namespaces virtualize the values of two system clocks:
14 .B CLOCK_MONOTONIC_COARSE
16 .BR CLOCK_MONOTONIC_RAW ),
17 a nonsettable clock that represents monotonic time since\[em]as
18 described by POSIX\[em]"some unspecified point in the past".
22 .BR CLOCK_BOOTTIME_ALARM ),
23 a nonsettable clock that is identical to
25 except that it also includes any time that the system is suspended.
27 Thus, the processes in a time namespace share per-namespace values
29 This affects various APIs that measure against these clocks, including:
30 .BR clock_gettime (2),
31 .BR clock_nanosleep (2),
33 .BR timer_settime (2),
34 .BR timerfd_settime (2),
38 Currently, the only way to create a time namespace is by calling
43 This call creates a new time namespace but does
45 place the calling process in the new namespace.
46 Instead, the calling process's
47 subsequently created children are placed in the new namespace.
48 This allows clock offsets (see below) for the new namespace
49 to be set before the first process is placed in the namespace.
51 .IR /proc/ pid /ns/time_for_children
52 symbolic link shows the time namespace in which
53 the children of a process will be created.
54 (A process can use a file descriptor opened on
55 this symbolic link in a call to
57 in order to move into the namespace.)
59 .SS \fI/proc/\fPpid\fI/timens_offsets\fP
60 Associated with each time namespace are offsets,
61 expressed with respect to the initial time namespace,
62 that define the values of the monotonic and
63 boot-time clocks in that namespace.
64 These offsets are exposed via the file
65 .IR /proc/ pid /timens_offsets .
67 the offsets are expressed as lines consisting of
68 three space-delimited fields:
72 <clock-id> <offset-secs> <offset-nanosecs>
78 is a string that identifies the clock whose offsets are being shown.
87 The remaining fields express the offset (seconds plus nanoseconds) for the
88 clock in this time namespace.
89 These offsets are expressed relative to the clock values in
90 the initial time namespace.
93 value can be negative, subject to restrictions noted below;
97 In the initial time namespace, the contents of the
103 $ \fBcat /proc/self/timens_offsets\fP
109 In a new time namespace that has had no member processes,
110 the clock offsets can be modified by writing newline-terminated
111 records of the same form to the
114 The file can be written to multiple times,
115 but after the first process has been created in or has entered the namespace,
117 on this file fail with the error
119 In order to write to the
121 file, a process must have the
123 capability in the user namespace that owns the time namespace.
127 file can fail with the following errors:
132 value is greater than 999,999,999.
140 The caller does not have the
147 value is out of range.
152 can't be set to a value which would make the current
153 time on the corresponding clock inside the namespace a negative value; and
156 can't be set to a value such that the time on the corresponding clock
157 inside the namespace would exceed half of the value of the kernel constant
159 (this limits the clock value to a maximum of approximately 146 years).
162 In a new time namespace created by
166 file are inherited from the time namespace of the creating process.
168 Use of time namespaces requires a kernel that is configured with the
172 Note that time namespaces do not virtualize the
175 Virtualization of this clock was avoided for reasons of complexity
176 and overhead within the kernel.
178 For compatibility with the initial implementation, when writing a
181 .IR /proc/ pid /timens_offsets
182 file, the numerical values of the IDs can be written
183 instead of the symbolic names show above; i.e., 1 instead of
187 For readability, the use of the symbolic names over the numbers is preferred.
189 The motivation for adding time namespaces was to allow
190 the monotonic and boot-time clocks to maintain consistent values
191 during container migration and checkpoint/restore.
193 The following shell session demonstrates the operation of time namespaces.
194 We begin by displaying the inode number of the time namespace
195 of a shell in the initial time namespace:
199 $ \fBreadlink /proc/$$/ns/time\fP
204 Continuing in the initial time namespace, we display the system uptime using
208 example program shown in
210 to display the values of various clocks:
214 $ \fBuptime \-\-pretty\fP
215 up 21 hours, 17 minutes
216 $ \fB./clock_times\fP
217 CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 36m 41s)
218 CLOCK_TAI : 1585989438.972 (18356 days + 8h 37m 18s)
219 CLOCK_MONOTONIC: 56338.247 (15h 38m 58s)
220 CLOCK_BOOTTIME : 76633.544 (21h 17m 13s)
226 to create a time namespace and execute a
229 From the new shell, we use the built-in
231 command to write records to the
233 file adjusting the offset for the
236 and the offset for the
238 clock forward 7 days:
242 $ \fBPS1="ns2# " sudo unshare \-T \-\- bash \-\-norc\fP
243 ns2# \fBecho "monotonic $((2*24*60*60)) 0" > /proc/$$/timens_offsets\fP
244 ns2# \fBecho "boottime $((7*24*60*60)) 0" > /proc/$$/timens_offsets\fP
248 Above, we started the
252 option so that no start-up scripts were executed.
253 This ensures that no child processes are created from the
254 shell before we have a chance to update the
260 to display the contents of the
265 creates the first process in the new time namespace,
266 after which further attempts to update the
268 file produce an error.
272 ns2# \fBcat /proc/$$/timens_offsets\fP
275 ns2# \fBecho "boottime $((9*24*60*60)) 0" > /proc/$$/timens_offsets\fP
276 bash: echo: write error: Permission denied
280 Continuing in the new namespace, we execute
288 ns2# \fBuptime \-\-pretty\fP
289 up 1 week, 21 hours, 18 minutes
290 ns2# \fB./clock_times\fP
291 CLOCK_REALTIME : 1585989457.056 (18356 days + 8h 37m 37s)
292 CLOCK_TAI : 1585989494.057 (18356 days + 8h 38m 14s)
293 CLOCK_MONOTONIC: 229193.332 (2 days + 15h 39m 53s)
294 CLOCK_BOOTTIME : 681488.629 (7 days + 21h 18m 8s)
298 From the above output, we can see that the monotonic
299 and boot-time clocks have different values in the new time namespace.
302 .IR /proc/ pid /ns/time
304 .IR /proc/ pid /ns/time_for_children
305 symbolic links, we see that the shell is a member of the initial time
306 namespace, but its children are created in the new namespace.
310 ns2# \fBreadlink /proc/$$/ns/time\fP
312 ns2# \fBreadlink /proc/$$/ns/time_for_children\fP
314 ns2# \fBreadlink /proc/self/ns/time\fP # Creates a child process
319 Returning to the shell in the initial time namespace,
320 we see that the monotonic and boot-time clocks
321 are unaffected by the
323 changes that were made in the other time namespace:
327 $ \fBuptime \-\-pretty\fP
328 up 21 hours, 19 minutes
329 $ \fB./clock_times\fP
330 CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 38m 51s)
331 CLOCK_TAI : 1585989438.972 (18356 days + 8h 39m 28s)
332 CLOCK_MONOTONIC: 56338.247 (15h 41m 8s)
333 CLOCK_BOOTTIME : 76633.544 (21h 19m 23s)
339 .BR clock_settime (2),
340 .\" clone3() support for time namespaces is a work in progress