kq_timer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #include <sys/event.h>
  2. #include <sys/time.h>
  3. #include <stdio.h>
  4. #include <sched.h>
  5. #include <err.h>
  6. #include <errno.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <getopt.h>
  11. #include <signal.h>
  12. #include <stdbool.h>
  13. static int verbose;
  14. const struct sched_param param = {.sched_priority = 15};
  15. static struct option longopts[] = {
  16. { "period", required_argument, NULL, 'p' },
  17. { "verbose", no_argument , &verbose, 1 },
  18. { NULL, 0, NULL, 0 }
  19. };
  20. void fd_close(int *fd) {
  21. close(*fd);
  22. *fd = -1;
  23. }
  24. #define _smartfd __attribute((cleanup(fd_close)))
  25. void file_close(FILE **fd) {
  26. fclose(*fd);
  27. *fd = NULL;
  28. }
  29. #define _smartfile __attribute((cleanup(file_close)))
  30. [[noreturn]] static int usage(void)
  31. {
  32. fputs("kq_timer [--verbose] [--period ms]\n", stderr);
  33. exit (EXIT_SUCCESS);
  34. }
  35. __attribute__((const)) static double diff_ms(const struct timespec * restrict time1, const struct timespec * restrict time0)
  36. {
  37. return (double)(time1->tv_sec - time0->tv_sec) * 1000.0
  38. + (double)(time1->tv_nsec - time0->tv_nsec) / 1000000.0;
  39. }
  40. static void sig_handler(int sig)
  41. {
  42. signal(SIGINT, SIG_DFL);
  43. }
  44. int main(int argc, char *argv[])
  45. {
  46. struct kevent change; /* event we want to monitor */
  47. struct kevent event; /* event that was triggered */
  48. _smartfd int kq;
  49. int nev; /* handlers */
  50. int ch; /* opt long */
  51. int period = 1000; /* Default to 1 sec. */
  52. size_t counter = 0;
  53. size_t total = 0;
  54. _smartfile FILE *fd = NULL;
  55. char template[64];
  56. int rc = EXIT_FAILURE;
  57. struct timespec t0 = { 0, 0};
  58. while ((ch = getopt_long(argc, argv, "hvp:", longopts, NULL)) != -1) {
  59. switch (ch) {
  60. case 'v':
  61. verbose = 1;
  62. break;
  63. case 'p':
  64. period = atoi(optarg);
  65. break;
  66. case 0:
  67. /* long option */
  68. break;
  69. default:
  70. usage();
  71. }
  72. }
  73. argc -= optind;
  74. argv += optind;
  75. if (*argv)
  76. usage();
  77. /* create a new kernel event queue */
  78. if ((kq = kqueue()) == -1)
  79. err(EXIT_FAILURE, "kqueue() failure");
  80. /* initalise kevent structure */
  81. EV_SET(&change, 1, EVFILT_TIMER, EV_ADD | EV_ENABLE, NOTE_MSECONDS, 1, 0);
  82. /* Dummy file to store measurements */
  83. strncpy(template, "/tmp/timer.XXXXXX", sizeof template);
  84. if (mkstemp(template) == -1) {
  85. warn("mkstemp() failure");
  86. return EXIT_FAILURE;
  87. }
  88. fd = fopen(template, "w");
  89. if (fd == NULL) {
  90. warn("fopen() failure");
  91. return EXIT_FAILURE;
  92. }
  93. printf("Log file: %s\n", template);
  94. /* process priority */
  95. if (-1 == sched_setscheduler(0, SCHED_RR, &param)) {
  96. warn("set scheduler() failure");
  97. }
  98. signal(SIGINT, sig_handler);
  99. while (true) {
  100. nev = kevent(kq, &change, 1, &event, 1, NULL);
  101. if (nev < 0) {
  102. if (errno != EINTR)
  103. warn("kevent wait:");
  104. /* interrupted */
  105. break;
  106. }
  107. if (nev > 0) {
  108. /* measurements */
  109. struct timespec t1;
  110. clock_gettime(CLOCK_MONOTONIC, &t1);
  111. if (!(t0.tv_sec == 0 && t0.tv_nsec == 0)) {
  112. fprintf(fd, "%06.3f\n", diff_ms(&t1, &t0));
  113. ++total;
  114. }
  115. t0 = t1;
  116. if (event.flags & EV_ERROR) {
  117. fprintf(stderr, "EV_ERROR: %s\n", strerror((int)event.data));
  118. rc = EXIT_FAILURE;
  119. break;
  120. }
  121. /* verbose mode */
  122. ++counter;
  123. if (counter == period) {
  124. if (verbose) {
  125. fputs(".", stdout);
  126. fflush(stdout);
  127. }
  128. counter = 0;
  129. }
  130. }
  131. }
  132. rc = EXIT_SUCCESS;
  133. fprintf(stdout, "\n%zu entries stored.\n", total);
  134. fprintf(stdout, "%zu cycles counted.\n", counter);
  135. return rc;
  136. }