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