1274d631eSJohn Stultz /* Measure mqueue timeout latency
2274d631eSJohn Stultz * by: john stultz (john.stultz@linaro.org)
3274d631eSJohn Stultz * (C) Copyright Linaro 2013
4274d631eSJohn Stultz *
5274d631eSJohn Stultz * Inspired with permission from example test by:
6274d631eSJohn Stultz * Romain Francoise <romain@orebokech.com>
7274d631eSJohn Stultz * Licensed under the GPLv2
8274d631eSJohn Stultz *
9274d631eSJohn Stultz * To build:
10274d631eSJohn Stultz * $ gcc mqueue-lat.c -o mqueue-lat -lrt
11274d631eSJohn Stultz *
12274d631eSJohn Stultz * This program is free software: you can redistribute it and/or modify
13274d631eSJohn Stultz * it under the terms of the GNU General Public License as published by
14274d631eSJohn Stultz * the Free Software Foundation, either version 2 of the License, or
15274d631eSJohn Stultz * (at your option) any later version.
16274d631eSJohn Stultz *
17274d631eSJohn Stultz * This program is distributed in the hope that it will be useful,
18274d631eSJohn Stultz * but WITHOUT ANY WARRANTY; without even the implied warranty of
19274d631eSJohn Stultz * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20274d631eSJohn Stultz * GNU General Public License for more details.
21274d631eSJohn Stultz */
22274d631eSJohn Stultz
23274d631eSJohn Stultz #include <stdio.h>
24274d631eSJohn Stultz #include <stdlib.h>
25274d631eSJohn Stultz #include <time.h>
26274d631eSJohn Stultz #include <sys/time.h>
27274d631eSJohn Stultz #include <sys/timex.h>
28274d631eSJohn Stultz #include <string.h>
29274d631eSJohn Stultz #include <signal.h>
30274d631eSJohn Stultz #include <errno.h>
31274d631eSJohn Stultz #include <mqueue.h>
32274d631eSJohn Stultz #include "../kselftest.h"
33274d631eSJohn Stultz
34274d631eSJohn Stultz #define NSEC_PER_SEC 1000000000ULL
35274d631eSJohn Stultz
36274d631eSJohn Stultz #define TARGET_TIMEOUT 100000000 /* 100ms in nanoseconds */
37274d631eSJohn Stultz #define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */
38274d631eSJohn Stultz
39274d631eSJohn Stultz
timespec_sub(struct timespec a,struct timespec b)40274d631eSJohn Stultz long long timespec_sub(struct timespec a, struct timespec b)
41274d631eSJohn Stultz {
42274d631eSJohn Stultz long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
43274d631eSJohn Stultz
44274d631eSJohn Stultz ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
45274d631eSJohn Stultz return ret;
46274d631eSJohn Stultz }
47274d631eSJohn Stultz
timespec_add(struct timespec ts,unsigned long long ns)48274d631eSJohn Stultz struct timespec timespec_add(struct timespec ts, unsigned long long ns)
49274d631eSJohn Stultz {
50274d631eSJohn Stultz ts.tv_nsec += ns;
51274d631eSJohn Stultz while (ts.tv_nsec >= NSEC_PER_SEC) {
52274d631eSJohn Stultz ts.tv_nsec -= NSEC_PER_SEC;
53274d631eSJohn Stultz ts.tv_sec++;
54274d631eSJohn Stultz }
55274d631eSJohn Stultz return ts;
56274d631eSJohn Stultz }
57274d631eSJohn Stultz
mqueue_lat_test(void)58274d631eSJohn Stultz int mqueue_lat_test(void)
59274d631eSJohn Stultz {
60274d631eSJohn Stultz
61274d631eSJohn Stultz mqd_t q;
62274d631eSJohn Stultz struct mq_attr attr;
63274d631eSJohn Stultz struct timespec start, end, now, target;
64274d631eSJohn Stultz int i, count, ret;
65274d631eSJohn Stultz
66274d631eSJohn Stultz q = mq_open("/foo", O_CREAT | O_RDONLY, 0666, NULL);
67274d631eSJohn Stultz if (q < 0) {
68274d631eSJohn Stultz perror("mq_open");
69274d631eSJohn Stultz return -1;
70274d631eSJohn Stultz }
71274d631eSJohn Stultz mq_getattr(q, &attr);
72274d631eSJohn Stultz
73274d631eSJohn Stultz
74274d631eSJohn Stultz count = 100;
75274d631eSJohn Stultz clock_gettime(CLOCK_MONOTONIC, &start);
76274d631eSJohn Stultz
77274d631eSJohn Stultz for (i = 0; i < count; i++) {
78274d631eSJohn Stultz char buf[attr.mq_msgsize];
79274d631eSJohn Stultz
80274d631eSJohn Stultz clock_gettime(CLOCK_REALTIME, &now);
81274d631eSJohn Stultz target = now;
82274d631eSJohn Stultz target = timespec_add(now, TARGET_TIMEOUT); /* 100ms */
83274d631eSJohn Stultz
84274d631eSJohn Stultz ret = mq_timedreceive(q, buf, sizeof(buf), NULL, &target);
85274d631eSJohn Stultz if (ret < 0 && errno != ETIMEDOUT) {
86274d631eSJohn Stultz perror("mq_timedreceive");
87274d631eSJohn Stultz return -1;
88274d631eSJohn Stultz }
89274d631eSJohn Stultz }
90274d631eSJohn Stultz clock_gettime(CLOCK_MONOTONIC, &end);
91274d631eSJohn Stultz
92274d631eSJohn Stultz mq_close(q);
93274d631eSJohn Stultz
94274d631eSJohn Stultz if ((timespec_sub(start, end)/count) > TARGET_TIMEOUT + UNRESONABLE_LATENCY)
95274d631eSJohn Stultz return -1;
96274d631eSJohn Stultz
97274d631eSJohn Stultz return 0;
98274d631eSJohn Stultz }
99274d631eSJohn Stultz
main(int argc,char ** argv)100274d631eSJohn Stultz int main(int argc, char **argv)
101274d631eSJohn Stultz {
102274d631eSJohn Stultz int ret;
103274d631eSJohn Stultz
104274d631eSJohn Stultz printf("Mqueue latency : ");
105fe483192SKees Cook fflush(stdout);
106274d631eSJohn Stultz
107274d631eSJohn Stultz ret = mqueue_lat_test();
108274d631eSJohn Stultz if (ret < 0) {
109274d631eSJohn Stultz printf("[FAILED]\n");
110*bc7e5d23SNathan Chancellor ksft_exit_fail();
111274d631eSJohn Stultz }
112274d631eSJohn Stultz printf("[OK]\n");
113*bc7e5d23SNathan Chancellor ksft_exit_pass();
114274d631eSJohn Stultz }
115