proc-uptime-002.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. #include <stdint.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <fcntl.h>
  31. #include "proc-uptime.h"
  32. static inline int sys_sched_getaffinity(pid_t pid, unsigned int len, unsigned long *m)
  33. {
  34. return syscall(SYS_sched_getaffinity, pid, len, m);
  35. }
  36. static inline int sys_sched_setaffinity(pid_t pid, unsigned int len, unsigned long *m)
  37. {
  38. return syscall(SYS_sched_setaffinity, pid, len, m);
  39. }
  40. int main(void)
  41. {
  42. uint64_t u0, u1, c0, c1;
  43. unsigned int len;
  44. unsigned long *m;
  45. unsigned int cpu;
  46. int fd;
  47. /* find out "nr_cpu_ids" */
  48. m = NULL;
  49. len = 0;
  50. do {
  51. len += sizeof(unsigned long);
  52. free(m);
  53. m = malloc(len);
  54. } while (sys_sched_getaffinity(0, len, m) == -1 && errno == EINVAL);
  55. fd = open("/proc/uptime", O_RDONLY);
  56. assert(fd >= 0);
  57. u0 = proc_uptime(fd);
  58. c0 = clock_boottime();
  59. for (cpu = 0; cpu < len * 8; cpu++) {
  60. memset(m, 0, len);
  61. m[cpu / (8 * sizeof(unsigned long))] |= 1UL << (cpu % (8 * sizeof(unsigned long)));
  62. /* CPU might not exist, ignore error */
  63. sys_sched_setaffinity(0, len, m);
  64. u1 = proc_uptime(fd);
  65. c1 = clock_boottime();
  66. /* Is /proc/uptime monotonic ? */
  67. assert(u1 >= u0);
  68. /* Is CLOCK_BOOTTIME monotonic ? */
  69. assert(c1 >= c0);
  70. /* Is CLOCK_BOOTTIME VS /proc/uptime monotonic ? */
  71. assert(c0 >= u0);
  72. u0 = u1;
  73. c0 = c1;
  74. }
  75. return 0;
  76. }