tst-cputimer3.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /* Tests for POSIX timer implementation using another process's CPU clock. */
  2. #include <unistd.h>
  3. #if _POSIX_THREADS && defined _POSIX_CPUTIME
  4. #include <errno.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include <signal.h>
  11. #include <sys/wait.h>
  12. static clockid_t child_clock;
  13. #define TEST_CLOCK child_clock
  14. #define TEST_CLOCK_MISSING(clock) \
  15. (setup_test () ? "other-process CPU clock timer support" : NULL)
  16. /* This function is intended to rack up both user and system time. */
  17. static void
  18. chew_cpu (void)
  19. {
  20. while (1)
  21. {
  22. static volatile char buf[4096];
  23. for (int i = 0; i < 100; ++i)
  24. for (size_t j = 0; j < sizeof buf; ++j)
  25. buf[j] = 0xaa;
  26. int nullfd = open ("/dev/null", O_WRONLY);
  27. for (int i = 0; i < 100; ++i)
  28. for (size_t j = 0; j < sizeof buf; ++j)
  29. buf[j] = 0xbb;
  30. write (nullfd, (char *) buf, sizeof buf);
  31. close (nullfd);
  32. if (getppid () == 1)
  33. _exit (2);
  34. }
  35. }
  36. static pid_t child;
  37. static void
  38. cleanup_child (void)
  39. {
  40. if (child <= 0)
  41. return;
  42. if (kill (child, SIGKILL) < 0 && errno != ESRCH)
  43. printf ("cannot kill child %d: %m\n", child);
  44. else
  45. {
  46. int status;
  47. errno = 0;
  48. if (waitpid (child, &status, 0) != child)
  49. printf ("waitpid %d: %m\n", child);
  50. }
  51. }
  52. #define CLEANUP_HANDLER cleanup_child ()
  53. static int
  54. setup_test (void)
  55. {
  56. /* Test timers on a process CPU clock by having a child process eating
  57. CPU. First make sure we can make such timers at all. */
  58. int pipefd[2];
  59. if (pipe (pipefd) < 0)
  60. {
  61. printf ("pipe: %m\n");
  62. exit (1);
  63. }
  64. child = fork ();
  65. if (child == 0)
  66. {
  67. char c;
  68. close (pipefd[1]);
  69. if (read (pipefd[0], &c, 1) == 1)
  70. chew_cpu ();
  71. _exit (1);
  72. }
  73. if (child < 0)
  74. {
  75. printf ("fork: %m\n");
  76. exit (1);
  77. }
  78. atexit (&cleanup_child);
  79. close (pipefd[0]);
  80. int e = clock_getcpuclockid (child, &child_clock);
  81. if (e == EPERM)
  82. {
  83. puts ("clock_getcpuclockid does not support other processes");
  84. return 1;
  85. }
  86. if (e != 0)
  87. {
  88. printf ("clock_getcpuclockid: %s\n", strerror (e));
  89. exit (1);
  90. }
  91. timer_t t;
  92. if (timer_create (TEST_CLOCK, NULL, &t) != 0)
  93. {
  94. printf ("timer_create: %m\n");
  95. return 1;
  96. }
  97. timer_delete (t);
  98. /* Get the child started chewing. */
  99. if (write (pipefd[1], "x", 1) != 1)
  100. {
  101. printf ("write to pipe: %m\n");
  102. return 1;
  103. }
  104. close (pipefd[1]);
  105. return 0;
  106. }
  107. #else
  108. # define TEST_CLOCK_MISSING(clock) "process clocks"
  109. #endif
  110. #include "tst-timer4.c"