vireshk

pselect

Jul 19th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. $ cat pselect01.c
  2. #include <stdio.h>
  3. #include <fcntl.h>
  4. #include <sys/select.h>
  5. #include <sys/time.h>
  6. #include <sys/types.h>
  7. #include <time.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10.  
  11. struct tcase {
  12. struct timespec tv;
  13. unsigned int iterations;
  14. };
  15.  
  16. static unsigned int monotonic_resolution;
  17.  
  18. static struct tcase tcases[] = {
  19. {{0, 1000000}, 500},
  20. {{0, 2000000}, 500},
  21. {{0, 10000000}, 300},
  22. {{0, 100000000}, 1},
  23. {{1, 0}, 1},
  24. };
  25.  
  26. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  27.  
  28. static struct timespec start_time, stop_time;
  29.  
  30. static inline struct timespec tst_timespec_diff(struct timespec t1,
  31. struct timespec t2)
  32. {
  33. struct timespec res;
  34.  
  35. res.tv_sec = t1.tv_sec - t2.tv_sec;
  36.  
  37. if (t1.tv_nsec < t2.tv_nsec) {
  38. res.tv_sec--;
  39. res.tv_nsec = 1000000000 - (t2.tv_nsec - t1.tv_nsec);
  40. } else {
  41. res.tv_nsec = t1.tv_nsec - t2.tv_nsec;
  42. }
  43.  
  44. return res;
  45. }
  46.  
  47. static inline long long tst_timespec_to_us(struct timespec t)
  48. {
  49. return t.tv_sec * 1000000 + (t.tv_nsec + 500) / 1000;
  50. }
  51.  
  52. static void verify_pselect(int i)
  53. {
  54. fd_set readfds;
  55. struct timespec tv;
  56. long long requested_us, slept_us = 0;
  57. int threshold;
  58. struct tcase *t = &tcases[i];
  59.  
  60. FD_ZERO(&readfds);
  61. FD_SET(0, &readfds);
  62.  
  63. tv = t->tv;
  64.  
  65. clock_gettime(CLOCK_MONOTONIC, &start_time);
  66. pselect(0, &readfds, NULL, NULL, &tv, NULL);
  67. clock_gettime(CLOCK_MONOTONIC, &stop_time);
  68.  
  69. printf("expected %lu us, got %llu us\n", tv.tv_nsec/1000, tst_timespec_to_us(tst_timespec_diff(stop_time, start_time)));
  70. }
  71.  
  72. void main(void)
  73. {
  74. unsigned int i;
  75. for (i = 0; i < 4; i++)
  76. verify_pselect(i);
  77. }
Add Comment
Please, Sign In to add comment