~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/timers/mqueue-lat.c

Version: ~ [ linux-5.14-rc3 ] ~ [ linux-5.13.5 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.53 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.135 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.198 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.240 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.276 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.276 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 23 #include <stdio.h>
 24 #include <stdlib.h>
 25 #include <time.h>
 26 #include <sys/time.h>
 27 #include <sys/timex.h>
 28 #include <string.h>
 29 #include <signal.h>
 30 #include <errno.h>
 31 #include <mqueue.h>
 32 #ifdef KTEST
 33 #include "../kselftest.h"
 34 #else
 35 static inline int ksft_exit_pass(void)
 36 {
 37         exit(0);
 38 }
 39 static inline int ksft_exit_fail(void)
 40 {
 41         exit(1);
 42 }
 43 #endif
 44 
 45 #define NSEC_PER_SEC 1000000000ULL
 46 
 47 #define TARGET_TIMEOUT          100000000       /* 100ms in nanoseconds */
 48 #define UNRESONABLE_LATENCY     40000000        /* 40ms in nanosecs */
 49 
 50 
 51 long long timespec_sub(struct timespec a, struct timespec b)
 52 {
 53         long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec;
 54 
 55         ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec;
 56         return ret;
 57 }
 58 
 59 struct timespec timespec_add(struct timespec ts, unsigned long long ns)
 60 {
 61         ts.tv_nsec += ns;
 62         while (ts.tv_nsec >= NSEC_PER_SEC) {
 63                 ts.tv_nsec -= NSEC_PER_SEC;
 64                 ts.tv_sec++;
 65         }
 66         return ts;
 67 }
 68 
 69 int mqueue_lat_test(void)
 70 {
 71 
 72         mqd_t q;
 73         struct mq_attr attr;
 74         struct timespec start, end, now, target;
 75         int i, count, ret;
 76 
 77         q = mq_open("/foo", O_CREAT | O_RDONLY, 0666, NULL);
 78         if (q < 0) {
 79                 perror("mq_open");
 80                 return -1;
 81         }
 82         mq_getattr(q, &attr);
 83 
 84 
 85         count = 100;
 86         clock_gettime(CLOCK_MONOTONIC, &start);
 87 
 88         for (i = 0; i < count; i++) {
 89                 char buf[attr.mq_msgsize];
 90 
 91                 clock_gettime(CLOCK_REALTIME, &now);
 92                 target = now;
 93                 target = timespec_add(now, TARGET_TIMEOUT); /* 100ms */
 94 
 95                 ret = mq_timedreceive(q, buf, sizeof(buf), NULL, &target);
 96                 if (ret < 0 && errno != ETIMEDOUT) {
 97                         perror("mq_timedreceive");
 98                         return -1;
 99                 }
100         }
101         clock_gettime(CLOCK_MONOTONIC, &end);
102 
103         mq_close(q);
104 
105         if ((timespec_sub(start, end)/count) > TARGET_TIMEOUT + UNRESONABLE_LATENCY)
106                 return -1;
107 
108         return 0;
109 }
110 
111 int main(int argc, char **argv)
112 {
113         int ret;
114 
115         printf("Mqueue latency :                          ");
116 
117         ret = mqueue_lat_test();
118         if (ret < 0) {
119                 printf("[FAILED]\n");
120                 return ksft_exit_fail();
121         }
122         printf("[OK]\n");
123         return ksft_exit_pass();
124 }
125 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp