1 /* Clocksource change test
2 * by: john stultz (johnstul@us.ibm.com)
3 * (C) Copyright IBM 2012
4 * Licensed under the GPLv2
6 * NOTE: This is a meta-test which quickly changes the clocksourc and
7 * then uses other tests to detect problems. Thus this test requires
8 * that the inconsistency-check and nanosleep tests be present in the
9 * same directory it is run from.
12 * $ gcc clocksource-switch.c -o clocksource-switch -lrt
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
30 #include <sys/timex.h>
32 #include <sys/types.h>
38 #include "../kselftest.h"
40 static inline int ksft_exit_pass(void)
44 static inline int ksft_exit_fail(void)
51 int get_clocksources(char list[][30])
58 fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
60 size = read(fd, buf, 512);
64 for (i = 0; i < 30; i++)
69 while (head - buf < size) {
70 /* Find the next space */
71 for (tmp = head; *tmp != ' '; tmp++) {
78 strcpy(list[i], head);
86 int get_cur_clocksource(char *buf, size_t size)
90 fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
92 size = read(fd, buf, size);
97 int change_clocksource(char *clocksource)
102 fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
107 size = write(fd, clocksource, strlen(clocksource));
117 int run_tests(int secs)
122 sprintf(buf, "./inconsistency-check -t %i", secs);
126 ret = system("./nanosleep");
131 char clocksource_list[10][30];
133 int main(int argv, char **argc)
136 int count, i, status;
139 get_cur_clocksource(orig_clk, 512);
141 count = get_clocksources(clocksource_list);
143 if (change_clocksource(clocksource_list[0])) {
144 printf("Error: You probably need to run this as root\n");
148 /* Check everything is sane before we start switching asyncrhonously */
149 for (i = 0; i < count; i++) {
150 printf("Validating clocksource %s\n", clocksource_list[i]);
151 if (change_clocksource(clocksource_list[i])) {
162 printf("Running Asyncrhonous Switching Tests...\n");
165 return run_tests(60);
167 while (pid != waitpid(pid, &status, WNOHANG))
168 for (i = 0; i < count; i++)
169 if (change_clocksource(clocksource_list[i])) {
174 change_clocksource(orig_clk);
177 return ksft_exit_fail();
178 return ksft_exit_pass();