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

TOMOYO Linux Cross Reference
Linux/tools/testing/selftests/timers/nanosleep.c

Version: ~ [ linux-5.1-rc5 ] ~ [ linux-5.0.7 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.34 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.111 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.168 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.178 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.138 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.65 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ 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 /* Make sure timers don't return early
  2  *              by: john stultz (johnstul@us.ibm.com)
  3  *                  John Stultz (john.stultz@linaro.org)
  4  *              (C) Copyright IBM 2012
  5  *              (C) Copyright Linaro 2013 2015
  6  *              Licensed under the GPLv2
  7  *
  8  *  To build:
  9  *      $ gcc nanosleep.c -o nanosleep -lrt
 10  *
 11  *   This program is free software: you can redistribute it and/or modify
 12  *   it under the terms of the GNU General Public License as published by
 13  *   the Free Software Foundation, either version 2 of the License, or
 14  *   (at your option) any later version.
 15  *
 16  *   This program is distributed in the hope that it will be useful,
 17  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 18  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 19  *   GNU General Public License for more details.
 20  */
 21 
 22 #include <errno.h>
 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 "../kselftest.h"
 31 
 32 #define NSEC_PER_SEC 1000000000ULL
 33 
 34 #define CLOCK_REALTIME                  0
 35 #define CLOCK_MONOTONIC                 1
 36 #define CLOCK_PROCESS_CPUTIME_ID        2
 37 #define CLOCK_THREAD_CPUTIME_ID         3
 38 #define CLOCK_MONOTONIC_RAW             4
 39 #define CLOCK_REALTIME_COARSE           5
 40 #define CLOCK_MONOTONIC_COARSE          6
 41 #define CLOCK_BOOTTIME                  7
 42 #define CLOCK_REALTIME_ALARM            8
 43 #define CLOCK_BOOTTIME_ALARM            9
 44 #define CLOCK_HWSPECIFIC                10
 45 #define CLOCK_TAI                       11
 46 #define NR_CLOCKIDS                     12
 47 
 48 #define UNSUPPORTED 0xf00f
 49 
 50 char *clockstring(int clockid)
 51 {
 52         switch (clockid) {
 53         case CLOCK_REALTIME:
 54                 return "CLOCK_REALTIME";
 55         case CLOCK_MONOTONIC:
 56                 return "CLOCK_MONOTONIC";
 57         case CLOCK_PROCESS_CPUTIME_ID:
 58                 return "CLOCK_PROCESS_CPUTIME_ID";
 59         case CLOCK_THREAD_CPUTIME_ID:
 60                 return "CLOCK_THREAD_CPUTIME_ID";
 61         case CLOCK_MONOTONIC_RAW:
 62                 return "CLOCK_MONOTONIC_RAW";
 63         case CLOCK_REALTIME_COARSE:
 64                 return "CLOCK_REALTIME_COARSE";
 65         case CLOCK_MONOTONIC_COARSE:
 66                 return "CLOCK_MONOTONIC_COARSE";
 67         case CLOCK_BOOTTIME:
 68                 return "CLOCK_BOOTTIME";
 69         case CLOCK_REALTIME_ALARM:
 70                 return "CLOCK_REALTIME_ALARM";
 71         case CLOCK_BOOTTIME_ALARM:
 72                 return "CLOCK_BOOTTIME_ALARM";
 73         case CLOCK_TAI:
 74                 return "CLOCK_TAI";
 75         };
 76         return "UNKNOWN_CLOCKID";
 77 }
 78 
 79 /* returns 1 if a <= b, 0 otherwise */
 80 static inline int in_order(struct timespec a, struct timespec b)
 81 {
 82         if (a.tv_sec < b.tv_sec)
 83                 return 1;
 84         if (a.tv_sec > b.tv_sec)
 85                 return 0;
 86         if (a.tv_nsec > b.tv_nsec)
 87                 return 0;
 88         return 1;
 89 }
 90 
 91 struct timespec timespec_add(struct timespec ts, unsigned long long ns)
 92 {
 93         ts.tv_nsec += ns;
 94         while (ts.tv_nsec >= NSEC_PER_SEC) {
 95                 ts.tv_nsec -= NSEC_PER_SEC;
 96                 ts.tv_sec++;
 97         }
 98         return ts;
 99 }
100 
101 int nanosleep_test(int clockid, long long ns)
102 {
103         struct timespec now, target, rel;
104 
105         /* First check abs time */
106         if (clock_gettime(clockid, &now))
107                 return UNSUPPORTED;
108         target = timespec_add(now, ns);
109 
110         if (clock_nanosleep(clockid, TIMER_ABSTIME, &target, NULL))
111                 return UNSUPPORTED;
112         clock_gettime(clockid, &now);
113 
114         if (!in_order(target, now))
115                 return -1;
116 
117         /* Second check reltime */
118         clock_gettime(clockid, &now);
119         rel.tv_sec = 0;
120         rel.tv_nsec = 0;
121         rel = timespec_add(rel, ns);
122         target = timespec_add(now, ns);
123         clock_nanosleep(clockid, 0, &rel, NULL);
124         clock_gettime(clockid, &now);
125 
126         if (!in_order(target, now))
127                 return -1;
128         return 0;
129 }
130 
131 int main(int argc, char **argv)
132 {
133         long long length;
134         int clockid, ret;
135 
136         for (clockid = CLOCK_REALTIME; clockid < NR_CLOCKIDS; clockid++) {
137 
138                 /* Skip cputime clockids since nanosleep won't increment cputime */
139                 if (clockid == CLOCK_PROCESS_CPUTIME_ID ||
140                                 clockid == CLOCK_THREAD_CPUTIME_ID ||
141                                 clockid == CLOCK_HWSPECIFIC)
142                         continue;
143 
144                 printf("Nanosleep %-31s ", clockstring(clockid));
145 
146                 length = 10;
147                 while (length <= (NSEC_PER_SEC * 10)) {
148                         ret = nanosleep_test(clockid, length);
149                         if (ret == UNSUPPORTED) {
150                                 printf("[UNSUPPORTED]\n");
151                                 goto next;
152                         }
153                         if (ret < 0) {
154                                 printf("[FAILED]\n");
155                                 return ksft_exit_fail();
156                         }
157                         length *= 100;
158                 }
159                 printf("[OK]\n");
160 next:
161                 ret = 0;
162         }
163         return ksft_exit_pass();
164 }
165 

~ [ 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