kq_timer.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. __attribute__((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, 1000 * period, 0);
  82. /* Dummy file to store measurements */
  83. strncpy(template, "/tmp/timer.XXXXXX", sizeof template);
  84. int fdx = mkstemp(template);
  85. if (fdx == -1) {
  86. warn("mkstemp() failure");
  87. return EXIT_FAILURE;
  88. }
  89. fd = fdopen(fdx, "w");
  90. if (fd == NULL) {
  91. warn("fopen() failure");
  92. return EXIT_FAILURE;
  93. }
  94. printf("Log file: %s\n", template);
  95. /* process priority */
  96. if (-1 == sched_setscheduler(0, SCHED_RR, &param)) {
  97. warn("set scheduler() failure");
  98. }
  99. signal(SIGINT, sig_handler);
  100. while (true) {
  101. nev = kevent(kq, &change, 1, &event, 1, NULL);
  102. if (nev < 0) {
  103. if (errno != EINTR)
  104. warn("kevent wait:");
  105. /* interrupted */
  106. break;
  107. }
  108. if (nev > 0) {
  109. /* measurements */
  110. struct timespec t1;
  111. clock_gettime(CLOCK_MONOTONIC, &t1);
  112. if (!(t0.tv_sec == 0 && t0.tv_nsec == 0)) {
  113. fprintf(fd, "%06.3f\n", diff_ms(&t1, &t0));
  114. ++total;
  115. }
  116. t0 = t1;
  117. if (event.flags & EV_ERROR) {
  118. fprintf(stderr, "EV_ERROR: %s\n", strerror((int)event.data));
  119. rc = EXIT_FAILURE;
  120. break;
  121. }
  122. /* verbose mode */
  123. ++counter;
  124. if (counter == period) {
  125. if (verbose) {
  126. fputs(".", stdout);
  127. fflush(stdout);
  128. }
  129. counter = 0;
  130. }
  131. }
  132. }
  133. rc = EXIT_SUCCESS;
  134. fprintf(stdout, "\n%zu entries stored.\n", total);
  135. fprintf(stdout, "%zu cycles counted.\n", counter);
  136. return rc;
  137. }