1 /* Measure mqueue timeout latency
2 * by: john stultz (john.stultz@linaro.org)
3 * (C) Copyright Linaro 2013
5 * Inspired with permission from example test by:
6 * Romain Francoise <romain@orebokech.com>
7 * Licensed under the GPLv2
10 * $ gcc mqueue-lat.c -o mqueue-lat -lrt
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.
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.
27 #include <sys/timex.h>
33 #include "../kselftest.h"
35 static inline int ksft_exit_pass(void)
39 static inline int ksft_exit_fail(void)
45 #define NSEC_PER_SEC 1000000000ULL
47 #define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */
48 #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
51 long long timespec_sub(struct timespec a, struct timespec b)
53 long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
55 ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
59 struct timespec timespec_add(struct timespec ts, unsigned long long ns)
62 while (ts.tv_nsec >= NSEC_PER_SEC) {
63 ts.tv_nsec -= NSEC_PER_SEC;
69 int mqueue_lat_test(void)
74 struct timespec start, end, now, target;
77 q = mq_open("/foo", O_CREAT | O_RDONLY, 0666, NULL);
86 clock_gettime(CLOCK_MONOTONIC, &start);
88 for (i = 0; i < count; i++) {
89 char buf[attr.mq_msgsize];
91 clock_gettime(CLOCK_REALTIME, &now);
93 target = timespec_add(now, TARGET_TIMEOUT); /* 100ms */
95 ret = mq_timedreceive(q, buf, sizeof(buf), NULL, &target);
96 if (ret < 0 && errno != ETIMEDOUT) {
97 perror("mq_timedreceive");
101 clock_gettime(CLOCK_MONOTONIC, &end);
105 if ((timespec_sub(start, end)/count) > TARGET_TIMEOUT + UNRESONABLE_LATENCY)
111 int main(int argc, char **argv)
115 printf("Mqueue latency : ");
117 ret = mqueue_lat_test();
119 printf("[FAILED]\n");
120 return ksft_exit_fail();
123 return ksft_exit_pass();