mqueue-lat.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Measure mqueue timeout latency
  2. * by: john stultz (john.stultz@linaro.org)
  3. * (C) Copyright Linaro 2013
  4. *
  5. * Inspired with permission from example test by:
  6. * Romain Francoise <romain@orebokech.com>
  7. * Licensed under the GPLv2
  8. *
  9. * To build:
  10. * $ gcc mqueue-lat.c -o mqueue-lat -lrt
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <time.h>
  25. #include <sys/time.h>
  26. #include <sys/timex.h>
  27. #include <string.h>
  28. #include <signal.h>
  29. #include <errno.h>
  30. #include <mqueue.h>
  31. #ifdef KTEST
  32. #include "../kselftest.h"
  33. #else
  34. static inline int ksft_exit_pass(void)
  35. {
  36. exit(0);
  37. }
  38. static inline int ksft_exit_fail(void)
  39. {
  40. exit(1);
  41. }
  42. #endif
  43. #define NSEC_PER_SEC 1000000000ULL
  44. #define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */
  45. #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
  46. long long timespec_sub(struct timespec a, struct timespec b)
  47. {
  48. long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
  49. ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
  50. return ret;
  51. }
  52. struct timespec timespec_add(struct timespec ts, unsigned long long ns)
  53. {
  54. ts.tv_nsec += ns;
  55. while (ts.tv_nsec >= NSEC_PER_SEC) {
  56. ts.tv_nsec -= NSEC_PER_SEC;
  57. ts.tv_sec++;
  58. }
  59. return ts;
  60. }
  61. int mqueue_lat_test(void)
  62. {
  63. mqd_t q;
  64. struct mq_attr attr;
  65. struct timespec start, end, now, target;
  66. int i, count, ret;
  67. q = mq_open("/foo", O_CREAT | O_RDONLY, 0666, NULL);
  68. if (q < 0) {
  69. perror("mq_open");
  70. return -1;
  71. }
  72. mq_getattr(q, &attr);
  73. count = 100;
  74. clock_gettime(CLOCK_MONOTONIC, &start);
  75. for (i = 0; i < count; i++) {
  76. char buf[attr.mq_msgsize];
  77. clock_gettime(CLOCK_REALTIME, &now);
  78. target = now;
  79. target = timespec_add(now, TARGET_TIMEOUT); /* 100ms */
  80. ret = mq_timedreceive(q, buf, sizeof(buf), NULL, &target);
  81. if (ret < 0 && errno != ETIMEDOUT) {
  82. perror("mq_timedreceive");
  83. return -1;
  84. }
  85. }
  86. clock_gettime(CLOCK_MONOTONIC, &end);
  87. mq_close(q);
  88. if ((timespec_sub(start, end)/count) > TARGET_TIMEOUT + UNRESONABLE_LATENCY)
  89. return -1;
  90. return 0;
  91. }
  92. int main(int argc, char **argv)
  93. {
  94. int ret;
  95. printf("Mqueue latency : ");
  96. ret = mqueue_lat_test();
  97. if (ret < 0) {
  98. printf("[FAILED]\n");
  99. return ksft_exit_fail();
  100. }
  101. printf("[OK]\n");
  102. return ksft_exit_pass();
  103. }