tst-cputimer1.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /* Tests for POSIX timer implementation using process CPU clock. */
  2. #include <unistd.h>
  3. #if _POSIX_THREADS && defined _POSIX_CPUTIME
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <time.h>
  9. #include <pthread.h>
  10. #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
  11. #define TEST_CLOCK_MISSING(clock) \
  12. (setup_test () ? "process CPU clock timer support" : NULL)
  13. /* This function is intended to rack up both user and system time. */
  14. static void *
  15. chew_cpu (void *arg)
  16. {
  17. while (1)
  18. {
  19. static volatile char buf[4096];
  20. for (int i = 0; i < 100; ++i)
  21. for (size_t j = 0; j < sizeof buf; ++j)
  22. buf[j] = 0xaa;
  23. int nullfd = open ("/dev/null", O_WRONLY);
  24. for (int i = 0; i < 100; ++i)
  25. for (size_t j = 0; j < sizeof buf; ++j)
  26. buf[j] = 0xbb;
  27. write (nullfd, (char *) buf, sizeof buf);
  28. close (nullfd);
  29. }
  30. return NULL;
  31. }
  32. static int
  33. setup_test (void)
  34. {
  35. /* Test timers on our own process CPU clock by having a worker thread
  36. eating CPU. First make sure we can make such timers at all. */
  37. timer_t t;
  38. if (timer_create (TEST_CLOCK, NULL, &t) != 0)
  39. {
  40. printf ("timer_create: %m\n");
  41. return 1;
  42. }
  43. timer_delete (t);
  44. pthread_t th;
  45. int e = pthread_create (&th, NULL, chew_cpu, NULL);
  46. if (e != 0)
  47. {
  48. printf ("pthread_create: %s\n", strerror (e));
  49. exit (1);
  50. }
  51. return 0;
  52. }
  53. #else
  54. # define TEST_CLOCK_MISSING(clock) "process clocks"
  55. #endif
  56. #include "tst-timer4.c"