tst-cputimer2.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. static clockid_t worker_thread_clock;
  12. #define TEST_CLOCK worker_thread_clock
  13. #define TEST_CLOCK_MISSING(clock) \
  14. (setup_test () ? "thread CPU clock timer support" : NULL)
  15. /* This function is intended to rack up both user and system time. */
  16. static void *
  17. chew_cpu (void *arg)
  18. {
  19. while (1)
  20. {
  21. static volatile char buf[4096];
  22. for (int i = 0; i < 100; ++i)
  23. for (size_t j = 0; j < sizeof buf; ++j)
  24. buf[j] = 0xaa;
  25. int nullfd = open ("/dev/null", O_WRONLY);
  26. for (int i = 0; i < 100; ++i)
  27. for (size_t j = 0; j < sizeof buf; ++j)
  28. buf[j] = 0xbb;
  29. write (nullfd, (char *) buf, sizeof buf);
  30. close (nullfd);
  31. }
  32. return NULL;
  33. }
  34. static int
  35. setup_test (void)
  36. {
  37. /* Test timers on a thread CPU clock by having a worker thread eating
  38. CPU. First make sure we can make such timers at all. */
  39. pthread_t th;
  40. int e = pthread_create (&th, NULL, chew_cpu, NULL);
  41. if (e != 0)
  42. {
  43. printf ("pthread_create: %s\n", strerror (e));
  44. exit (1);
  45. }
  46. e = pthread_getcpuclockid (th, &worker_thread_clock);
  47. if (e == EPERM || e == ENOENT || e == ENOTSUP)
  48. {
  49. puts ("pthread_getcpuclockid does not support other threads");
  50. return 1;
  51. }
  52. if (e != 0)
  53. {
  54. printf ("pthread_getcpuclockid: %s\n", strerror (e));
  55. exit (1);
  56. }
  57. timer_t t;
  58. if (timer_create (TEST_CLOCK, NULL, &t) != 0)
  59. {
  60. printf ("timer_create: %m\n");
  61. return 1;
  62. }
  63. timer_delete (t);
  64. return 0;
  65. }
  66. #else
  67. # define TEST_CLOCK_MISSING(clock) "process clocks"
  68. #endif
  69. #include "tst-timer4.c"