Lingzhu Xiang | 03c739d | 2012-07-08 08:09:53 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Regression test for hrtimer early expiration during and after leap seconds |
| 3 | * |
| 4 | * A bug in the hrtimer subsystem caused all TIMER_ABSTIME CLOCK_REALTIME |
| 5 | * timers to expire one second early during leap second. |
| 6 | * See http://lwn.net/Articles/504658/. |
| 7 | * |
| 8 | * This is a regression test for the bug. |
| 9 | * |
| 10 | * Lingzhu Xiang <lxiang@redhat.com> Copyright (c) Red Hat, Inc., 2012. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify it |
| 13 | * under the terms of version 2 of the GNU General Public License as |
| 14 | * published by the Free Software Foundation. |
| 15 | * |
| 16 | * This program is distributed in the hope that it would be useful, but |
| 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
Wanlong Gao | fed9641 | 2012-10-24 10:10:29 +0800 | [diff] [blame^] | 21 | * with this program; if not, write the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
Lingzhu Xiang | 03c739d | 2012-07-08 08:09:53 +0800 | [diff] [blame] | 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/time.h> |
| 28 | #include <sys/timex.h> |
| 29 | #include <errno.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <time.h> |
| 32 | #include "test.h" |
| 33 | #include "usctest.h" |
| 34 | #include "common_timers.h" |
| 35 | |
| 36 | #define SECONDS_BEFORE_LEAP 2 |
| 37 | #define SECONDS_AFTER_LEAP 2 |
| 38 | |
| 39 | char *TCID = "leapsec_timer"; |
| 40 | int TST_TOTAL = 1; |
| 41 | |
| 42 | static inline int in_order(struct timespec a, struct timespec b); |
| 43 | static void adjtimex_status(struct timex *tx, int status); |
| 44 | static const char *strtime(const struct timespec *now); |
| 45 | static void test_hrtimer_early_expiration(void); |
| 46 | static void run_leapsec(void); |
| 47 | static void setup(void); |
| 48 | static void cleanup(void); |
| 49 | |
| 50 | int main(int argc, char **argv) |
| 51 | { |
| 52 | char *msg; |
| 53 | int lc; |
| 54 | |
| 55 | msg = parse_opts(argc, argv, NULL, NULL); |
| 56 | if (msg != NULL) |
| 57 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 58 | |
| 59 | setup(); |
| 60 | |
| 61 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 62 | Tst_count = 0; |
| 63 | run_leapsec(); |
| 64 | } |
| 65 | |
| 66 | cleanup(); |
| 67 | tst_exit(); |
| 68 | } |
| 69 | |
| 70 | static inline int in_order(struct timespec a, struct timespec b) |
| 71 | { |
| 72 | if (a.tv_sec < b.tv_sec) |
| 73 | return 1; |
| 74 | if (a.tv_sec > b.tv_sec) |
| 75 | return 0; |
| 76 | if (a.tv_nsec > b.tv_nsec) |
| 77 | return 0; |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static void adjtimex_status(struct timex *tx, int status) |
| 82 | { |
| 83 | const char * const msgs[6] = { |
| 84 | "clock synchronized", |
| 85 | "insert leap second", |
| 86 | "delete leap second", |
| 87 | "leap second in progress", |
| 88 | "leap second has occurred", |
| 89 | "clock not synchronized", |
| 90 | }; |
| 91 | int r; |
| 92 | struct timespec now; |
| 93 | |
| 94 | tx->modes = ADJ_STATUS; |
| 95 | tx->status = status; |
| 96 | r = adjtimex(tx); |
| 97 | now.tv_sec = tx->time.tv_sec; |
| 98 | now.tv_nsec = tx->time.tv_usec * 1000; |
| 99 | |
| 100 | if ((tx->status & status) != status) |
| 101 | tst_brkm(TBROK, cleanup, "adjtimex status %d not set", status); |
| 102 | else if (r < 0) |
| 103 | tst_brkm(TBROK | TERRNO, cleanup, "adjtimex"); |
| 104 | else if (r < 6) |
| 105 | tst_resm(TINFO, "%s adjtimex: %s", strtime(&now), msgs[r]); |
| 106 | else |
| 107 | tst_resm(TINFO, "%s adjtimex: clock state %d", |
| 108 | strtime(&now), r); |
| 109 | } |
| 110 | |
| 111 | static const char *strtime(const struct timespec *now) |
| 112 | { |
| 113 | static char fmt[256], buf[256]; |
| 114 | |
| 115 | if (snprintf(fmt, sizeof(fmt), "%%F %%T.%09ld %%z", now->tv_nsec) < 0) { |
| 116 | buf[0] = '\0'; |
| 117 | return buf; |
| 118 | } |
| 119 | if (!strftime(buf, sizeof(buf), fmt, localtime(&now->tv_sec))) { |
| 120 | buf[0] = '\0'; |
| 121 | return buf; |
| 122 | } |
| 123 | return buf; |
| 124 | } |
| 125 | |
| 126 | static void test_hrtimer_early_expiration(void) |
| 127 | { |
| 128 | struct timespec now, target; |
| 129 | int r, fail; |
| 130 | |
| 131 | clock_gettime(CLOCK_REALTIME, &now); |
| 132 | tst_resm(TINFO, "now is %s", strtime(&now)); |
| 133 | |
| 134 | target = now; |
| 135 | target.tv_sec++; |
| 136 | tst_resm(TINFO, "sleep till %s", strtime(&target)); |
| 137 | r = clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &target, NULL); |
| 138 | if (r < 0) { |
| 139 | tst_resm(TINFO | TERRNO, "clock_nanosleep"); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | clock_gettime(CLOCK_REALTIME, &now); |
| 144 | tst_resm(TINFO, "now is %s", strtime(&now)); |
| 145 | |
| 146 | fail = !in_order(target, now); |
| 147 | tst_resm(fail ? TFAIL : TINFO, "hrtimer early expiration is %s.", |
| 148 | fail ? "detected" : "not detected"); |
| 149 | } |
| 150 | |
| 151 | static void run_leapsec(void) |
| 152 | { |
| 153 | const struct timespec sleeptime = {0, NSEC_PER_SEC / 2}; |
| 154 | struct timespec now, leap, start; |
| 155 | struct timex tx; |
| 156 | |
| 157 | clock_gettime(CLOCK_REALTIME, &now); |
| 158 | start = now; |
| 159 | tst_resm(TINFO, "test start at %s", strtime(&now)); |
| 160 | |
| 161 | test_hrtimer_early_expiration(); |
| 162 | |
| 163 | /* calculate the next leap second */ |
| 164 | now.tv_sec += 86400 - now.tv_sec % 86400; |
| 165 | now.tv_nsec = 0; |
| 166 | leap = now; |
| 167 | tst_resm(TINFO, "scheduling leap second %s", strtime(&leap)); |
| 168 | |
| 169 | /* start before the leap second */ |
| 170 | now.tv_sec -= SECONDS_BEFORE_LEAP; |
| 171 | if (clock_settime(CLOCK_REALTIME, &now) < 0) |
| 172 | tst_brkm(TBROK | TERRNO, cleanup, "clock_settime"); |
| 173 | tst_resm(TINFO, "setting time to %s", strtime(&now)); |
| 174 | |
| 175 | /* reset NTP time state */ |
| 176 | adjtimex_status(&tx, STA_PLL); |
| 177 | adjtimex_status(&tx, 0); |
| 178 | |
| 179 | /* set the leap second insert flag */ |
| 180 | adjtimex_status(&tx, STA_INS); |
| 181 | |
| 182 | /* reliably sleep till after the leap second */ |
| 183 | while (tx.time.tv_sec < leap.tv_sec + SECONDS_AFTER_LEAP) { |
| 184 | adjtimex_status(&tx, tx.status); |
| 185 | clock_nanosleep(CLOCK_MONOTONIC, 0, &sleeptime, NULL); |
| 186 | } |
| 187 | |
| 188 | test_hrtimer_early_expiration(); |
| 189 | |
| 190 | adjtimex_status(&tx, STA_PLL); |
| 191 | adjtimex_status(&tx, 0); |
| 192 | |
| 193 | /* recover from timer expiring state and restore time */ |
| 194 | clock_gettime(CLOCK_REALTIME, &now); |
| 195 | start.tv_sec += now.tv_sec - (leap.tv_sec - SECONDS_BEFORE_LEAP); |
| 196 | start.tv_nsec += now.tv_nsec; |
| 197 | start.tv_sec += start.tv_nsec / NSEC_PER_SEC; |
| 198 | start.tv_nsec = start.tv_nsec % NSEC_PER_SEC; |
| 199 | tst_resm(TINFO, "restoring time to %s", strtime(&start)); |
| 200 | /* calls clock_was_set() in kernel to revert inconsistency */ |
| 201 | if (clock_settime(CLOCK_REALTIME, &start) < 0) |
| 202 | tst_brkm(TBROK | TERRNO, cleanup, "clock_settime"); |
| 203 | |
| 204 | test_hrtimer_early_expiration(); |
| 205 | } |
| 206 | |
| 207 | static void setup(void) |
| 208 | { |
| 209 | tst_require_root(NULL); |
| 210 | tst_sig(NOFORK, DEF_HANDLER, CLEANUP); |
| 211 | TEST_PAUSE; |
| 212 | } |
| 213 | |
| 214 | static void cleanup(void) |
| 215 | { |
| 216 | struct timespec now; |
| 217 | clock_gettime(CLOCK_REALTIME, &now); |
| 218 | /* Calls clock_was_set() in kernel to revert inconsistency. |
| 219 | * The only possible EPERM doesn't matter here. */ |
| 220 | clock_settime(CLOCK_REALTIME, &now); |
| 221 | TEST_CLEANUP; |
| 222 | } |