1 /* Leap second stress test 1 /* Leap second stress test 2 * by: John Stultz (john.stultz@l 2 * by: John Stultz (john.stultz@linaro.org) 3 * (C) Copyright IBM 2012 3 * (C) Copyright IBM 2012 4 * (C) Copyright 2013, 2015 Linar 4 * (C) Copyright 2013, 2015 Linaro Limited 5 * Licensed under the GPLv2 5 * Licensed under the GPLv2 6 * 6 * 7 * This test signals the kernel to insert a l 7 * This test signals the kernel to insert a leap second 8 * every day at midnight GMT. This allows for 8 * every day at midnight GMT. This allows for stressing the 9 * kernel's leap-second behavior, as well as 9 * kernel's leap-second behavior, as well as how well applications 10 * handle the leap-second discontinuity. 10 * handle the leap-second discontinuity. 11 * 11 * 12 * Usage: leap-a-day [-s] [-i <num>] 12 * Usage: leap-a-day [-s] [-i <num>] 13 * 13 * 14 * Options: 14 * Options: 15 * -s: Each iteration, set the date t 15 * -s: Each iteration, set the date to 10 seconds before midnight GMT. 16 * This speeds up the number of l 16 * This speeds up the number of leapsecond transitions tested, 17 * but because it calls settimeof 17 * but because it calls settimeofday frequently, advancing the 18 * time by 24 hours every ~16 sec 18 * time by 24 hours every ~16 seconds, it may cause application 19 * disruption. 19 * disruption. 20 * 20 * 21 * -i: Number of iterations to run (d 21 * -i: Number of iterations to run (default: infinite) 22 * 22 * 23 * Other notes: Disabling NTP prior to runnin 23 * Other notes: Disabling NTP prior to running this is advised, as the two 24 * may conflict in their command 24 * may conflict in their commands to the kernel. 25 * 25 * 26 * To build: 26 * To build: 27 * $ gcc leap-a-day.c -o leap-a-day -lrt 27 * $ gcc leap-a-day.c -o leap-a-day -lrt 28 * 28 * 29 * This program is free software: you can re 29 * This program is free software: you can redistribute it and/or modify 30 * it under the terms of the GNU General Pub 30 * it under the terms of the GNU General Public License as published by 31 * the Free Software Foundation, either vers 31 * the Free Software Foundation, either version 2 of the License, or 32 * (at your option) any later version. 32 * (at your option) any later version. 33 * 33 * 34 * This program is distributed in the hope t 34 * This program is distributed in the hope that it will be useful, 35 * but WITHOUT ANY WARRANTY; without even th 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of 36 * MERCHANTABILITY or FITNESS FOR A PARTICUL 36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 * GNU General Public License for more detai 37 * GNU General Public License for more details. 38 */ 38 */ 39 39 40 40 41 41 42 #include <stdio.h> 42 #include <stdio.h> 43 #include <stdlib.h> 43 #include <stdlib.h> 44 #include <time.h> 44 #include <time.h> 45 #include <sys/time.h> 45 #include <sys/time.h> 46 #include <sys/timex.h> 46 #include <sys/timex.h> 47 #include <sys/errno.h> 47 #include <sys/errno.h> 48 #include <string.h> 48 #include <string.h> 49 #include <signal.h> 49 #include <signal.h> 50 #include <unistd.h> 50 #include <unistd.h> 51 #include "../kselftest.h" 51 #include "../kselftest.h" 52 52 53 #define NSEC_PER_SEC 1000000000ULL 53 #define NSEC_PER_SEC 1000000000ULL 54 #define CLOCK_TAI 11 54 #define CLOCK_TAI 11 55 55 56 time_t next_leap; 56 time_t next_leap; 57 int error_found; 57 int error_found; 58 58 59 /* returns 1 if a <= b, 0 otherwise */ 59 /* returns 1 if a <= b, 0 otherwise */ 60 static inline int in_order(struct timespec a, 60 static inline int in_order(struct timespec a, struct timespec b) 61 { 61 { 62 if (a.tv_sec < b.tv_sec) 62 if (a.tv_sec < b.tv_sec) 63 return 1; 63 return 1; 64 if (a.tv_sec > b.tv_sec) 64 if (a.tv_sec > b.tv_sec) 65 return 0; 65 return 0; 66 if (a.tv_nsec > b.tv_nsec) 66 if (a.tv_nsec > b.tv_nsec) 67 return 0; 67 return 0; 68 return 1; 68 return 1; 69 } 69 } 70 70 71 struct timespec timespec_add(struct timespec t 71 struct timespec timespec_add(struct timespec ts, unsigned long long ns) 72 { 72 { 73 ts.tv_nsec += ns; 73 ts.tv_nsec += ns; 74 while (ts.tv_nsec >= NSEC_PER_SEC) { 74 while (ts.tv_nsec >= NSEC_PER_SEC) { 75 ts.tv_nsec -= NSEC_PER_SEC; 75 ts.tv_nsec -= NSEC_PER_SEC; 76 ts.tv_sec++; 76 ts.tv_sec++; 77 } 77 } 78 return ts; 78 return ts; 79 } 79 } 80 80 81 char *time_state_str(int state) 81 char *time_state_str(int state) 82 { 82 { 83 switch (state) { 83 switch (state) { 84 case TIME_OK: return "TIME_OK"; 84 case TIME_OK: return "TIME_OK"; 85 case TIME_INS: return "TIME_INS"; 85 case TIME_INS: return "TIME_INS"; 86 case TIME_DEL: return "TIME_DEL"; 86 case TIME_DEL: return "TIME_DEL"; 87 case TIME_OOP: return "TIME_OOP"; 87 case TIME_OOP: return "TIME_OOP"; 88 case TIME_WAIT: return "TIME_WAIT"; 88 case TIME_WAIT: return "TIME_WAIT"; 89 case TIME_BAD: return "TIME_BAD"; 89 case TIME_BAD: return "TIME_BAD"; 90 } 90 } 91 return "ERROR"; 91 return "ERROR"; 92 } 92 } 93 93 94 /* clear NTP time_status & time_state */ 94 /* clear NTP time_status & time_state */ 95 int clear_time_state(void) 95 int clear_time_state(void) 96 { 96 { 97 struct timex tx; 97 struct timex tx; 98 int ret; 98 int ret; 99 99 100 /* 100 /* 101 * We have to call adjtime twice here, 101 * We have to call adjtime twice here, as kernels 102 * prior to 6b1859dba01c7 (included in 102 * prior to 6b1859dba01c7 (included in 3.5 and 103 * -stable), had an issue with the sta 103 * -stable), had an issue with the state machine 104 * and wouldn't clear the STA_INS/DEL 104 * and wouldn't clear the STA_INS/DEL flag directly. 105 */ 105 */ 106 tx.modes = ADJ_STATUS; 106 tx.modes = ADJ_STATUS; 107 tx.status = STA_PLL; 107 tx.status = STA_PLL; 108 ret = adjtimex(&tx); 108 ret = adjtimex(&tx); 109 109 110 /* Clear maxerror, as it can cause UNS 110 /* Clear maxerror, as it can cause UNSYNC to be set */ 111 tx.modes = ADJ_MAXERROR; 111 tx.modes = ADJ_MAXERROR; 112 tx.maxerror = 0; 112 tx.maxerror = 0; 113 ret = adjtimex(&tx); 113 ret = adjtimex(&tx); 114 114 115 /* Clear the status */ 115 /* Clear the status */ 116 tx.modes = ADJ_STATUS; 116 tx.modes = ADJ_STATUS; 117 tx.status = 0; 117 tx.status = 0; 118 ret = adjtimex(&tx); 118 ret = adjtimex(&tx); 119 119 120 return ret; 120 return ret; 121 } 121 } 122 122 123 /* Make sure we cleanup on ctrl-c */ 123 /* Make sure we cleanup on ctrl-c */ 124 void handler(int unused) 124 void handler(int unused) 125 { 125 { 126 clear_time_state(); 126 clear_time_state(); 127 exit(0); 127 exit(0); 128 } 128 } 129 129 130 void sigalarm(int signo) 130 void sigalarm(int signo) 131 { 131 { 132 struct timex tx; 132 struct timex tx; 133 int ret; 133 int ret; 134 134 135 tx.modes = 0; 135 tx.modes = 0; 136 ret = adjtimex(&tx); 136 ret = adjtimex(&tx); 137 137 138 if (tx.time.tv_sec < next_leap) { 138 if (tx.time.tv_sec < next_leap) { 139 printf("Error: Early timer exp 139 printf("Error: Early timer expiration! (Should be %ld)\n", next_leap); 140 error_found = 1; 140 error_found = 1; 141 printf("adjtimex: %10ld sec + 141 printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n", 142 tx.tim 142 tx.time.tv_sec, 143 tx.tim 143 tx.time.tv_usec, 144 tx.tai 144 tx.tai, 145 time_s 145 time_state_str(ret)); 146 } 146 } 147 if (ret != TIME_WAIT) { 147 if (ret != TIME_WAIT) { 148 printf("Error: Timer seeing in 148 printf("Error: Timer seeing incorrect NTP state? (Should be TIME_WAIT)\n"); 149 error_found = 1; 149 error_found = 1; 150 printf("adjtimex: %10ld sec + 150 printf("adjtimex: %10ld sec + %6ld us (%i)\t%s\n", 151 tx.tim 151 tx.time.tv_sec, 152 tx.tim 152 tx.time.tv_usec, 153 tx.tai 153 tx.tai, 154 time_s 154 time_state_str(ret)); 155 } 155 } 156 } 156 } 157 157 158 158 159 /* Test for known hrtimer failure */ 159 /* Test for known hrtimer failure */ 160 void test_hrtimer_failure(void) 160 void test_hrtimer_failure(void) 161 { 161 { 162 struct timespec now, target; 162 struct timespec now, target; 163 163 164 clock_gettime(CLOCK_REALTIME, &now); 164 clock_gettime(CLOCK_REALTIME, &now); 165 target = timespec_add(now, NSEC_PER_SE 165 target = timespec_add(now, NSEC_PER_SEC/2); 166 clock_nanosleep(CLOCK_REALTIME, TIMER_ 166 clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL); 167 clock_gettime(CLOCK_REALTIME, &now); 167 clock_gettime(CLOCK_REALTIME, &now); 168 168 169 if (!in_order(target, now)) { 169 if (!in_order(target, now)) { 170 printf("ERROR: hrtimer early e 170 printf("ERROR: hrtimer early expiration failure observed.\n"); 171 error_found = 1; 171 error_found = 1; 172 } 172 } 173 } 173 } 174 174 175 int main(int argc, char **argv) 175 int main(int argc, char **argv) 176 { 176 { 177 timer_t tm1; 177 timer_t tm1; 178 struct itimerspec its1; 178 struct itimerspec its1; 179 struct sigevent se; 179 struct sigevent se; 180 struct sigaction act; 180 struct sigaction act; 181 int signum = SIGRTMAX; 181 int signum = SIGRTMAX; 182 int settime = 1; 182 int settime = 1; 183 int tai_time = 0; 183 int tai_time = 0; 184 int insert = 1; 184 int insert = 1; 185 int iterations = 10; 185 int iterations = 10; 186 int opt; 186 int opt; 187 187 188 /* Process arguments */ 188 /* Process arguments */ 189 while ((opt = getopt(argc, argv, "sti: 189 while ((opt = getopt(argc, argv, "sti:")) != -1) { 190 switch (opt) { 190 switch (opt) { 191 case 'w': 191 case 'w': 192 printf("Only setting l 192 printf("Only setting leap-flag, not changing time. It could take up to a day for leap to trigger.\n"); 193 settime = 0; 193 settime = 0; 194 break; 194 break; 195 case 'i': 195 case 'i': 196 iterations = atoi(opta 196 iterations = atoi(optarg); 197 break; 197 break; 198 case 't': 198 case 't': 199 tai_time = 1; 199 tai_time = 1; 200 break; 200 break; 201 default: 201 default: 202 printf("Usage: %s [-w] 202 printf("Usage: %s [-w] [-i <iterations>]\n", argv[0]); 203 printf(" -w: Se 203 printf(" -w: Set flag and wait for leap second each iteration"); 204 printf(" (d 204 printf(" (default sets time to right before leapsecond)\n"); 205 printf(" -i: Nu 205 printf(" -i: Number of iterations (-1 = infinite, default is 10)\n"); 206 printf(" -t: Pr 206 printf(" -t: Print TAI time\n"); 207 exit(-1); 207 exit(-1); 208 } 208 } 209 } 209 } 210 210 211 /* Make sure TAI support is present if 211 /* Make sure TAI support is present if -t was used */ 212 if (tai_time) { 212 if (tai_time) { 213 struct timespec ts; 213 struct timespec ts; 214 214 215 if (clock_gettime(CLOCK_TAI, & 215 if (clock_gettime(CLOCK_TAI, &ts)) { 216 printf("System doesn't 216 printf("System doesn't support CLOCK_TAI\n"); 217 ksft_exit_fail(); 217 ksft_exit_fail(); 218 } 218 } 219 } 219 } 220 220 221 signal(SIGINT, handler); 221 signal(SIGINT, handler); 222 signal(SIGKILL, handler); 222 signal(SIGKILL, handler); 223 223 224 /* Set up timer signal handler: */ 224 /* Set up timer signal handler: */ 225 sigfillset(&act.sa_mask); 225 sigfillset(&act.sa_mask); 226 act.sa_flags = 0; 226 act.sa_flags = 0; 227 act.sa_handler = sigalarm; 227 act.sa_handler = sigalarm; 228 sigaction(signum, &act, NULL); 228 sigaction(signum, &act, NULL); 229 229 230 if (iterations < 0) 230 if (iterations < 0) 231 printf("This runs continuously 231 printf("This runs continuously. Press ctrl-c to stop\n"); 232 else 232 else 233 printf("Running for %i iterati 233 printf("Running for %i iterations. Press ctrl-c to stop\n", iterations); 234 234 235 printf("\n"); 235 printf("\n"); 236 while (1) { 236 while (1) { 237 int ret; 237 int ret; 238 struct timespec ts; 238 struct timespec ts; 239 struct timex tx; 239 struct timex tx; 240 time_t now; 240 time_t now; 241 241 242 /* Get the current time */ 242 /* Get the current time */ 243 clock_gettime(CLOCK_REALTIME, 243 clock_gettime(CLOCK_REALTIME, &ts); 244 244 245 /* Calculate the next possible 245 /* Calculate the next possible leap second 23:59:60 GMT */ 246 next_leap = ts.tv_sec; 246 next_leap = ts.tv_sec; 247 next_leap += 86400 - (next_lea 247 next_leap += 86400 - (next_leap % 86400); 248 248 249 if (settime) { 249 if (settime) { 250 struct timeval tv; 250 struct timeval tv; 251 251 252 tv.tv_sec = next_leap 252 tv.tv_sec = next_leap - 10; 253 tv.tv_usec = 0; 253 tv.tv_usec = 0; 254 settimeofday(&tv, NULL 254 settimeofday(&tv, NULL); 255 printf("Setting time t 255 printf("Setting time to %s", ctime(&tv.tv_sec)); 256 } 256 } 257 257 258 /* Reset NTP time state */ 258 /* Reset NTP time state */ 259 clear_time_state(); 259 clear_time_state(); 260 260 261 /* Set the leap second insert 261 /* Set the leap second insert flag */ 262 tx.modes = ADJ_STATUS; 262 tx.modes = ADJ_STATUS; 263 if (insert) 263 if (insert) 264 tx.status = STA_INS; 264 tx.status = STA_INS; 265 else 265 else 266 tx.status = STA_DEL; 266 tx.status = STA_DEL; 267 ret = adjtimex(&tx); 267 ret = adjtimex(&tx); 268 if (ret < 0) { 268 if (ret < 0) { 269 printf("Error: Problem 269 printf("Error: Problem setting STA_INS/STA_DEL!: %s\n", 270 270 time_state_str(ret)); 271 ksft_exit_fail(); 271 ksft_exit_fail(); 272 } 272 } 273 273 274 /* Validate STA_INS was set */ 274 /* Validate STA_INS was set */ 275 tx.modes = 0; 275 tx.modes = 0; 276 ret = adjtimex(&tx); 276 ret = adjtimex(&tx); 277 if (tx.status != STA_INS && tx 277 if (tx.status != STA_INS && tx.status != STA_DEL) { 278 printf("Error: STA_INS 278 printf("Error: STA_INS/STA_DEL not set!: %s\n", 279 279 time_state_str(ret)); 280 ksft_exit_fail(); 280 ksft_exit_fail(); 281 } 281 } 282 282 283 if (tai_time) { 283 if (tai_time) { 284 printf("Using TAI time 284 printf("Using TAI time," 285 " no inconsist 285 " no inconsistencies should be seen!\n"); 286 } 286 } 287 287 288 printf("Scheduling leap second 288 printf("Scheduling leap second for %s", ctime(&next_leap)); 289 289 290 /* Set up timer */ 290 /* Set up timer */ 291 printf("Setting timer for %ld 291 printf("Setting timer for %ld - %s", next_leap, ctime(&next_leap)); 292 memset(&se, 0, sizeof(se)); 292 memset(&se, 0, sizeof(se)); 293 se.sigev_notify = SIGEV_SIGNAL 293 se.sigev_notify = SIGEV_SIGNAL; 294 se.sigev_signo = signum; 294 se.sigev_signo = signum; 295 se.sigev_value.sival_int = 0; 295 se.sigev_value.sival_int = 0; 296 if (timer_create(CLOCK_REALTIM 296 if (timer_create(CLOCK_REALTIME, &se, &tm1) == -1) { 297 printf("Error: timer_c 297 printf("Error: timer_create failed\n"); 298 ksft_exit_fail(); 298 ksft_exit_fail(); 299 } 299 } 300 its1.it_value.tv_sec = next_le 300 its1.it_value.tv_sec = next_leap; 301 its1.it_value.tv_nsec = 0; 301 its1.it_value.tv_nsec = 0; 302 its1.it_interval.tv_sec = 0; 302 its1.it_interval.tv_sec = 0; 303 its1.it_interval.tv_nsec = 0; 303 its1.it_interval.tv_nsec = 0; 304 timer_settime(tm1, TIMER_ABSTI 304 timer_settime(tm1, TIMER_ABSTIME, &its1, NULL); 305 305 306 /* Wake up 3 seconds before le 306 /* Wake up 3 seconds before leap */ 307 ts.tv_sec = next_leap - 3; 307 ts.tv_sec = next_leap - 3; 308 ts.tv_nsec = 0; 308 ts.tv_nsec = 0; 309 309 310 310 311 while (clock_nanosleep(CLOCK_R 311 while (clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &ts, NULL)) 312 printf("Something woke 312 printf("Something woke us up, returning to sleep\n"); 313 313 314 /* Validate STA_INS is still s 314 /* Validate STA_INS is still set */ 315 tx.modes = 0; 315 tx.modes = 0; 316 ret = adjtimex(&tx); 316 ret = adjtimex(&tx); 317 if (tx.status != STA_INS && tx 317 if (tx.status != STA_INS && tx.status != STA_DEL) { 318 printf("Something clea 318 printf("Something cleared STA_INS/STA_DEL, setting it again.\n"); 319 tx.modes = ADJ_STATUS; 319 tx.modes = ADJ_STATUS; 320 if (insert) 320 if (insert) 321 tx.status = ST 321 tx.status = STA_INS; 322 else 322 else 323 tx.status = ST 323 tx.status = STA_DEL; 324 ret = adjtimex(&tx); 324 ret = adjtimex(&tx); 325 } 325 } 326 326 327 /* Check adjtimex output every 327 /* Check adjtimex output every half second */ 328 now = tx.time.tv_sec; 328 now = tx.time.tv_sec; 329 while (now < next_leap + 2) { 329 while (now < next_leap + 2) { 330 char buf[26]; 330 char buf[26]; 331 struct timespec tai; 331 struct timespec tai; 332 int ret; 332 int ret; 333 333 334 tx.modes = 0; 334 tx.modes = 0; 335 ret = adjtimex(&tx); 335 ret = adjtimex(&tx); 336 336 337 if (tai_time) { 337 if (tai_time) { 338 clock_gettime( 338 clock_gettime(CLOCK_TAI, &tai); 339 printf("%ld se 339 printf("%ld sec, %9ld ns\t%s\n", 340 340 tai.tv_sec, 341 341 tai.tv_nsec, 342 342 time_state_str(ret)); 343 } else { 343 } else { 344 ctime_r(&tx.ti 344 ctime_r(&tx.time.tv_sec, buf); 345 buf[strlen(buf 345 buf[strlen(buf)-1] = 0; /*remove trailing\n */ 346 346 347 printf("%s + % 347 printf("%s + %6ld us (%i)\t%s\n", 348 348 buf, 349 349 tx.time.tv_usec, 350 350 tx.tai, 351 351 time_state_str(ret)); 352 } 352 } 353 now = tx.time.tv_sec; 353 now = tx.time.tv_sec; 354 /* Sleep for another h 354 /* Sleep for another half second */ 355 ts.tv_sec = 0; 355 ts.tv_sec = 0; 356 ts.tv_nsec = NSEC_PER_ 356 ts.tv_nsec = NSEC_PER_SEC / 2; 357 clock_nanosleep(CLOCK_ 357 clock_nanosleep(CLOCK_MONOTONIC, 0, &ts, NULL); 358 } 358 } 359 /* Switch to using other mode 359 /* Switch to using other mode */ 360 insert = !insert; 360 insert = !insert; 361 361 362 /* Note if kernel has known hr 362 /* Note if kernel has known hrtimer failure */ 363 test_hrtimer_failure(); 363 test_hrtimer_failure(); 364 364 365 printf("Leap complete\n"); 365 printf("Leap complete\n"); 366 if (error_found) { 366 if (error_found) { 367 printf("Errors observe 367 printf("Errors observed\n"); 368 clear_time_state(); 368 clear_time_state(); 369 ksft_exit_fail(); 369 ksft_exit_fail(); 370 } 370 } 371 printf("\n"); 371 printf("\n"); 372 if ((iterations != -1) && !(-- 372 if ((iterations != -1) && !(--iterations)) 373 break; 373 break; 374 } 374 } 375 375 376 clear_time_state(); 376 clear_time_state(); 377 ksft_exit_pass(); 377 ksft_exit_pass(); 378 } 378 } 379 379
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.