2 * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 // Test that boottime value in /proc/uptime and CLOCK_BOOTTIME increment
17 // monotonically while shifting across CPUs. We don't test idle time
18 // monotonicity due to broken iowait task counting, cf: comment above
19 // get_cpu_idle_time_us()
24 #include <sys/syscall.h>
29 #include <sys/types.h>
33 #include "proc-uptime.h"
35 static inline int sys_sched_getaffinity(pid_t pid, unsigned int len, unsigned long *m)
37 return syscall(SYS_sched_getaffinity, pid, len, m);
40 static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned long *m)
42 return syscall(SYS_sched_setaffinity, pid, len, m);
47 uint64_t u0, u1, c0, c1;
53 /* find out "nr_cpu_ids" */
57 len += sizeof(unsigned long);
60 } while (sys_sched_getaffinity(0, len, m) == -1 && errno == EINVAL);
62 fd = open("/proc/uptime", O_RDONLY);
66 c0 = clock_boottime();
68 for (cpu = 0; cpu < len * 8; cpu++) {
70 m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
72 /* CPU might not exist, ignore error */
73 sys_sched_setaffinity(0, len, m);
76 c1 = clock_boottime();
78 /* Is /proc/uptime monotonic ? */
81 /* Is CLOCK_BOOTTIME monotonic ? */
84 /* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */