tst-cputimer3.c 2.7 KB

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