1 .\" Copyright (c) 2020 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH TIME_NAMESPACES 7 2020-04-01 "Linux" "Linux Programmer's Manual"
28 time_namespaces \- overview of Linux time namespaces
30 Time namespaces virtualize the values of two system clocks:
34 .BR CLOCK_MONOTONIC_COARSE
36 .BR CLOCK_MONOTONIC_RAW ),
37 a nonsettable clock that represents monotonic time since\(emas
38 described by POSIX\(em"some unspecified point in the past".
42 .BR CLOCK_BOOTTIME_ALARM ),
43 a nonsettable clock that is identical to
45 except that it also includes any time that the system is suspended.
47 Thus, the processes in a time namespace share per-namespace values
49 This affects various APIs that measure against these clocks, including:
50 .BR clock_gettime (2),
51 .BR clock_nanosleep (2),
53 .BR timer_settime (2),
54 .BR timerfd_settime (2),
58 Currently, the only way to create a time namespace is by calling
63 This call creates a new time namespace but does
65 place the calling process in the new namespace.
66 Instead, the calling process's
67 subsequently created children are placed in the new namespace.
68 This allows clock offsets (see below) for the new namespace
69 to be set before the first process is placed in the namespace.
71 .IR /proc/[pid]/ns/time_for_children
72 symbolic link shows the time namespace in which
73 the children of a process will be created.
74 (A process can use a file descriptor opened on
75 this symbolic link in a call to
77 in order to move into the namespace.)
79 .SS /proc/PID/timens_offsets
80 Associated with each time namespace are offsets,
81 expressed with respect to the initial time namespace,
82 that define the values of the monotonic and
83 boot-time clocks in that namespace.
84 These offsets are exposed via the file
85 .IR /proc/PID/timens_offsets .
87 the offsets are expressed as lines consisting of
88 three space-delimited fields:
92 <clock-id> <offset-secs> <offset-nanosecs>
98 is a string that identifies the clock whose offsets are being shown.
102 .BR CLOCK_MONOTONIC ,
107 The remaining fields express the offset (seconds plus nanoseconds) for the
108 clock in this time namespace.
109 These offsets are expressed relative to the clock values in
110 the initial time namespace.
113 value can be negative, subject to restrictions noted below;
115 is an unsigned value.
117 In the initial time namespace, the contents of the
123 $ \fBcat /proc/self/timens_offsets\fP
129 In a new time namespace that has had no member processes,
130 the clock offsets can be modified by writing newline-terminated
131 records of the same form to the
134 The file can be written to multiple times,
135 but after the first process has been created in or has entered the namespace,
137 on this file fail with the error
139 In order to write to the
141 file, a process must have the
143 capability in the user namespace that owns the time namespace.
147 file can fail with the following errors:
152 value is greater than 999,999,999.
160 The caller does not have the
168 value is out of range.
173 can't be set to a value which would make the current
174 time on the corresponding clock inside the namespace a negative value; and
177 can't be set to a value such that the time on the corresponding clock
178 inside the namespace would exceed half of the value of the kernel constant
180 (this limits the clock value to a maximum of approximately 146 years).
183 In a new time namespace created by
187 file are inherited from the time namespace of the creating process.
190 Use of time namespaces requires a kernel that is configured with the
194 Note that time namespaces do not virtualize the
197 Virtualization of this clock was avoided for reasons of complexity
198 and overhead within the kernel.
200 For compatibility with the initial implementation, when writing a
203 .IR /proc/[pid]/timens_offsets
204 file, the numerical values of the IDs can be written
205 instead of the symbolic names show above; i.e., 1 instead of
209 For redability, the use of the symbolic names over the numbers is preferred.
211 The motivation for adding time namespaces was to allow
212 the monotonic and boot-time clocks to maintain consistent values
213 during container migration and checkpoint/restore.
216 The following shell session demonstrates the operation of time namespaces.
217 We begin by displaying the inode number of the time namespace
218 of a shell in the initial time namespace:
222 $ \fBreadlink /proc/$$/ns/time\fP
227 Continuing in the initial time namespace, we display the system uptime using
231 example program shown in
233 to display the values of various clocks:
237 $ \fBuptime \-\-pretty\fP
238 up 21 hours, 17 minutes
239 $ \fB./clock_times\fP
240 CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 36m 41s)
241 CLOCK_TAI : 1585989438.972 (18356 days + 8h 37m 18s)
242 CLOCK_MONOTONIC: 56338.247 (15h 38m 58s)
243 CLOCK_BOOTTIME : 76633.544 (21h 17m 13s)
249 to create a time namespace and execute a
252 From the new shell, we use the built-in
254 command to write records to the
256 file adjusting the offset for the
259 and the offset for the
261 clock forward 7 days:
265 $ \fBPS1="ns2# " sudo unshare \-T \-\- bash \-\-norc\fP
266 ns2# \fBecho "monotonic $((2*24*60*60)) 0" > /proc/$$/timens_offsets\fP
267 ns2# \fBecho "boottime $((7*24*60*60)) 0" > /proc/$$/timens_offsets\fP
271 Above, we started the
275 options so that no start-up scripts were executed.
276 This ensures that no child processes are created from the
277 shell before we have a chance to update the
283 to display the contents of the
288 creates the first process in the new time namespace,
289 after which further attempts to update the
291 file produce an error.
295 ns2# \fBcat /proc/$$/timens_offsets\fP
298 ns2# \fBecho "boottime $((9*24*60*60)) 0" > /proc/$$/timens_offsets\fP
299 bash: echo: write error: Permission denied
303 Continuing in the new namespace, we execute
311 ns2# \fBuptime \-\-pretty\fP
312 up 1 week, 21 hours, 18 minutes
313 ns2# \fB./clock_times\fP
314 CLOCK_REALTIME : 1585989457.056 (18356 days + 8h 37m 37s)
315 CLOCK_TAI : 1585989494.057 (18356 days + 8h 38m 14s)
316 CLOCK_MONOTONIC: 229193.332 (2 days + 15h 39m 53s)
317 CLOCK_BOOTTIME : 681488.629 (7 days + 21h 18m 8s)
321 From the above output, we can see that the monotonic
322 and boot-time clocks have different values in the new time namespace.
325 .I /proc/[pid]/ns/time
327 .I /proc/[pid]/ns/time_for_children
328 symbolic links, we see that the shell is a member of the initial time
329 namespace, but its children are created in the new namespace.
334 ns2# \fBreadlink /proc/$$/ns/time\fP
336 ns2# \fBreadlink /proc/$$/ns/time_for_children\fP
338 ns2# \fBreadlink /proc/self/ns/time\fP # Creates a child process
343 Returning to the shell in the initial time namespace,
344 we see that the monotonic and boot-time clocks
345 are unaffected by the
347 changes that were made in the other time namespace:
351 $ \fBuptime \-\-pretty\fP
352 up 21 hours, 19 minutes
353 $ \fB./clock_times\fP
354 CLOCK_REALTIME : 1585989401.971 (18356 days + 8h 38m 51s)
355 CLOCK_TAI : 1585989438.972 (18356 days + 8h 39m 28s)
356 CLOCK_MONOTONIC: 56338.247 (15h 41m 8s)
357 CLOCK_BOOTTIME : 76633.544 (21h 19m 23s)
363 .BR clock_settime (2),
364 .\" clone3() support for time namespaces is a work in progress