~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/proc/proc-uptime-002.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright © 2018 Alexey Dobriyan <adobriyan@gmail.com>
  3  *
  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.
  7  *
  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.
 15  */
 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()
 20 #undef NDEBUG
 21 #include <assert.h>
 22 #include <errno.h>
 23 #include <unistd.h>
 24 #include <sys/syscall.h>
 25 #include <stdlib.h>
 26 #include <string.h>
 27 
 28 #include <stdint.h>
 29 #include <sys/types.h>
 30 #include <sys/stat.h>
 31 #include <fcntl.h>
 32 
 33 #include "proc-uptime.h"
 34 
 35 static inline int sys_sched_getaffinity(pid_t pid, unsigned int len, unsigned long *m)
 36 {
 37         return syscall(SYS_sched_getaffinity, pid, len, m);
 38 }
 39 
 40 static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned long *m)
 41 {
 42         return syscall(SYS_sched_setaffinity, pid, len, m);
 43 }
 44 
 45 int main(void)
 46 {
 47         uint64_t u0, u1, c0, c1;
 48         unsigned int len;
 49         unsigned long *m;
 50         unsigned int cpu;
 51         int fd;
 52 
 53         /* find out "nr_cpu_ids" */
 54         m = NULL;
 55         len = 0;
 56         do {
 57                 len += sizeof(unsigned long);
 58                 free(m);
 59                 m = malloc(len);
 60         } while (sys_sched_getaffinity(0, len, m) == -1 && errno == EINVAL);
 61 
 62         fd = open("/proc/uptime", O_RDONLY);
 63         assert(fd >= 0);
 64 
 65         u0 = proc_uptime(fd);
 66         c0 = clock_boottime();
 67 
 68         for (cpu = 0; cpu < len * 8; cpu++) {
 69                 memset(m, 0, len);
 70                 m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
 71 
 72                 /* CPU might not exist, ignore error */
 73                 sys_sched_setaffinity(0, len, m);
 74 
 75                 u1 = proc_uptime(fd);
 76                 c1 = clock_boottime();
 77 
 78                 /* Is /proc/uptime monotonic ? */
 79                 assert(u1 >= u0);
 80 
 81                 /* Is CLOCK_BOOTTIME monotonic ? */
 82                 assert(c1 >= c0);
 83 
 84                 /* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */
 85                 assert(c0 >= u0);
 86 
 87                 u0 = u1;
 88                 c0 = c1;
 89         }
 90 
 91         return 0;
 92 }
 93 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php