tst-cputimer2.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /* Tests for POSIX timer implementation using thread 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 <pthread.h>
  11. #include <support/xunistd.h>
  12. static clockid_t worker_thread_clock;
  13. #define TEST_CLOCK worker_thread_clock
  14. #define TEST_CLOCK_MISSING(clock) \
  15. (setup_test () ? "thread 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 *arg)
  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. xwrite (nullfd, (char *) buf, sizeof buf);
  31. close (nullfd);
  32. }
  33. return NULL;
  34. }
  35. static int
  36. setup_test (void)
  37. {
  38. /* Test timers on a thread CPU clock by having a worker thread eating
  39. CPU. First make sure we can make such timers at all. */
  40. pthread_t th;
  41. int e = pthread_create (&th, NULL, chew_cpu, NULL);
  42. if (e != 0)
  43. {
  44. printf ("pthread_create: %s\n", strerror (e));
  45. exit (1);
  46. }
  47. e = pthread_getcpuclockid (th, &worker_thread_clock);
  48. if (e == EPERM || e == ENOENT || e == ENOTSUP)
  49. {
  50. puts ("pthread_getcpuclockid does not support other threads");
  51. return 1;
  52. }
  53. if (e != 0)
  54. {
  55. printf ("pthread_getcpuclockid: %s\n", strerror (e));
  56. exit (1);
  57. }
  58. timer_t t;
  59. if (timer_create (TEST_CLOCK, NULL, &t) != 0)
  60. {
  61. printf ("timer_create: %m\n");
  62. return 1;
  63. }
  64. timer_delete (t);
  65. return 0;
  66. }
  67. #else
  68. # define TEST_CLOCK_MISSING(clock) "process clocks"
  69. #endif
  70. #include "tst-timer4.c"