at v6.18 272 lines 6.3 kB view raw
1/* set_timer latency test 2 * John Stultz (john.stultz@linaro.org) 3 * (C) Copyright Linaro 2014 4 * Licensed under the GPLv2 5 * 6 * This test makes sure the set_timer api is correct 7 * 8 * To build: 9 * $ gcc set-timer-lat.c -o set-timer-lat -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 23#include <errno.h> 24#include <stdio.h> 25#include <unistd.h> 26#include <time.h> 27#include <string.h> 28#include <signal.h> 29#include <stdlib.h> 30#include <pthread.h> 31#include <include/vdso/time64.h> 32#include "../kselftest.h" 33 34/* CLOCK_HWSPECIFIC == CLOCK_SGI_CYCLE (Deprecated) */ 35#define CLOCK_HWSPECIFIC 10 36 37#define UNRESONABLE_LATENCY 40000000 /* 40ms in nanosecs */ 38 39#define TIMER_SECS 1 40int alarmcount; 41int clock_id; 42struct timespec start_time; 43long long max_latency_ns; 44int timer_fired_early; 45 46char *clockstring(int clockid) 47{ 48 switch (clockid) { 49 case CLOCK_REALTIME: 50 return "CLOCK_REALTIME"; 51 case CLOCK_MONOTONIC: 52 return "CLOCK_MONOTONIC"; 53 case CLOCK_PROCESS_CPUTIME_ID: 54 return "CLOCK_PROCESS_CPUTIME_ID"; 55 case CLOCK_THREAD_CPUTIME_ID: 56 return "CLOCK_THREAD_CPUTIME_ID"; 57 case CLOCK_MONOTONIC_RAW: 58 return "CLOCK_MONOTONIC_RAW"; 59 case CLOCK_REALTIME_COARSE: 60 return "CLOCK_REALTIME_COARSE"; 61 case CLOCK_MONOTONIC_COARSE: 62 return "CLOCK_MONOTONIC_COARSE"; 63 case CLOCK_BOOTTIME: 64 return "CLOCK_BOOTTIME"; 65 case CLOCK_REALTIME_ALARM: 66 return "CLOCK_REALTIME_ALARM"; 67 case CLOCK_BOOTTIME_ALARM: 68 return "CLOCK_BOOTTIME_ALARM"; 69 case CLOCK_TAI: 70 return "CLOCK_TAI"; 71 } 72 return "UNKNOWN_CLOCKID"; 73} 74 75 76long long timespec_sub(struct timespec a, struct timespec b) 77{ 78 long long ret = NSEC_PER_SEC * b.tv_sec + b.tv_nsec; 79 80 ret -= NSEC_PER_SEC * a.tv_sec + a.tv_nsec; 81 return ret; 82} 83 84 85void sigalarm(int signo) 86{ 87 long long delta_ns; 88 struct timespec ts; 89 90 clock_gettime(clock_id, &ts); 91 alarmcount++; 92 93 delta_ns = timespec_sub(start_time, ts); 94 delta_ns -= NSEC_PER_SEC * TIMER_SECS * alarmcount; 95 96 if (delta_ns < 0) 97 timer_fired_early = 1; 98 99 if (delta_ns > max_latency_ns) 100 max_latency_ns = delta_ns; 101} 102 103void describe_timer(int flags, int interval) 104{ 105 printf("%-22s %s %s ", 106 clockstring(clock_id), 107 flags ? "ABSTIME":"RELTIME", 108 interval ? "PERIODIC":"ONE-SHOT"); 109} 110 111int setup_timer(int clock_id, int flags, int interval, timer_t *tm1) 112{ 113 struct sigevent se; 114 struct itimerspec its1, its2; 115 int err; 116 117 /* Set up timer: */ 118 memset(&se, 0, sizeof(se)); 119 se.sigev_notify = SIGEV_SIGNAL; 120 se.sigev_signo = SIGRTMAX; 121 se.sigev_value.sival_int = 0; 122 123 max_latency_ns = 0; 124 alarmcount = 0; 125 timer_fired_early = 0; 126 127 err = timer_create(clock_id, &se, tm1); 128 if (err) { 129 if ((clock_id == CLOCK_REALTIME_ALARM) || 130 (clock_id == CLOCK_BOOTTIME_ALARM)) { 131 printf("%-22s %s missing CAP_WAKE_ALARM? : [UNSUPPORTED]\n", 132 clockstring(clock_id), 133 flags ? "ABSTIME":"RELTIME"); 134 /* Indicate timer isn't set, so caller doesn't wait */ 135 return 1; 136 } 137 printf("%s - timer_create() failed\n", clockstring(clock_id)); 138 return -1; 139 } 140 141 clock_gettime(clock_id, &start_time); 142 if (flags) { 143 its1.it_value = start_time; 144 its1.it_value.tv_sec += TIMER_SECS; 145 } else { 146 its1.it_value.tv_sec = TIMER_SECS; 147 its1.it_value.tv_nsec = 0; 148 } 149 its1.it_interval.tv_sec = interval; 150 its1.it_interval.tv_nsec = 0; 151 152 err = timer_settime(*tm1, flags, &its1, &its2); 153 if (err) { 154 printf("%s - timer_settime() failed\n", clockstring(clock_id)); 155 return -1; 156 } 157 158 return 0; 159} 160 161int check_timer_latency(int flags, int interval) 162{ 163 int err = 0; 164 165 describe_timer(flags, interval); 166 printf("timer fired early: %7d : ", timer_fired_early); 167 if (!timer_fired_early) { 168 printf("[OK]\n"); 169 } else { 170 printf("[FAILED]\n"); 171 err = -1; 172 } 173 174 describe_timer(flags, interval); 175 printf("max latency: %10lld ns : ", max_latency_ns); 176 177 if (max_latency_ns < UNRESONABLE_LATENCY) { 178 printf("[OK]\n"); 179 } else { 180 printf("[FAILED]\n"); 181 err = -1; 182 } 183 return err; 184} 185 186int check_alarmcount(int flags, int interval) 187{ 188 describe_timer(flags, interval); 189 printf("count: %19d : ", alarmcount); 190 if (alarmcount == 1) { 191 printf("[OK]\n"); 192 return 0; 193 } 194 printf("[FAILED]\n"); 195 return -1; 196} 197 198int do_timer(int clock_id, int flags) 199{ 200 timer_t tm1; 201 const int interval = TIMER_SECS; 202 int err; 203 204 err = setup_timer(clock_id, flags, interval, &tm1); 205 /* Unsupported case - return 0 to not fail the test */ 206 if (err) 207 return err == 1 ? 0 : err; 208 209 while (alarmcount < 5) 210 sleep(1); 211 212 timer_delete(tm1); 213 return check_timer_latency(flags, interval); 214} 215 216int do_timer_oneshot(int clock_id, int flags) 217{ 218 timer_t tm1; 219 const int interval = 0; 220 struct timeval timeout; 221 int err; 222 223 err = setup_timer(clock_id, flags, interval, &tm1); 224 /* Unsupported case - return 0 to not fail the test */ 225 if (err) 226 return err == 1 ? 0 : err; 227 228 memset(&timeout, 0, sizeof(timeout)); 229 timeout.tv_sec = 5; 230 do { 231 err = select(0, NULL, NULL, NULL, &timeout); 232 } while (err == -1 && errno == EINTR); 233 234 timer_delete(tm1); 235 err = check_timer_latency(flags, interval); 236 err |= check_alarmcount(flags, interval); 237 return err; 238} 239 240int main(void) 241{ 242 struct sigaction act; 243 int signum = SIGRTMAX; 244 int ret = 0; 245 int max_clocks = CLOCK_TAI + 1; 246 247 /* Set up signal handler: */ 248 sigfillset(&act.sa_mask); 249 act.sa_flags = 0; 250 act.sa_handler = sigalarm; 251 sigaction(signum, &act, NULL); 252 253 printf("Setting timers for every %i seconds\n", TIMER_SECS); 254 for (clock_id = 0; clock_id < max_clocks; clock_id++) { 255 256 if ((clock_id == CLOCK_PROCESS_CPUTIME_ID) || 257 (clock_id == CLOCK_THREAD_CPUTIME_ID) || 258 (clock_id == CLOCK_MONOTONIC_RAW) || 259 (clock_id == CLOCK_REALTIME_COARSE) || 260 (clock_id == CLOCK_MONOTONIC_COARSE) || 261 (clock_id == CLOCK_HWSPECIFIC)) 262 continue; 263 264 ret |= do_timer(clock_id, TIMER_ABSTIME); 265 ret |= do_timer(clock_id, 0); 266 ret |= do_timer_oneshot(clock_id, TIMER_ABSTIME); 267 ret |= do_timer_oneshot(clock_id, 0); 268 } 269 if (ret) 270 ksft_exit_fail(); 271 ksft_exit_pass(); 272}