xref: /linux/tools/testing/selftests/timers/clocksource-switch.c (revision 486b257357c23743d9729ea715136d105fff461e)
1 /* Clocksource change test
2  *		by: john stultz (johnstul@us.ibm.com)
3  *		(C) Copyright IBM 2012
4  *		Licensed under the GPLv2
5  *
6  *  NOTE: This is a meta-test which quickly changes the clocksource 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.
10  *
11  *  To build:
12  *	$ gcc clocksource-switch.c -o clocksource-switch -lrt
13  *
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.
18  *
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.
23  */
24 
25 
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32 #include <sys/timex.h>
33 #include <sys/types.h>
34 #include <sys/wait.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include "kselftest.h"
38 
39 
40 int get_clocksources(char list[][30])
41 {
42 	int fd, i;
43 	ssize_t size;
44 	char buf[512];
45 	char *head, *tmp;
46 
47 	fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
48 	if (fd < 0)
49 		return 0;
50 
51 	size = read(fd, buf, sizeof(buf) - 1);
52 
53 	close(fd);
54 
55 	if (size <= 0)
56 		return 0;
57 
58 	buf[size] = '\0';
59 
60 	for (i = 0; i < 10; i++)
61 		list[i][0] = '\0';
62 
63 	head = buf;
64 	i = 0;
65 	while (head - buf < size) {
66 		/* Find the next space */
67 		for (tmp = head; *tmp != ' '; tmp++) {
68 			if (*tmp == '\n')
69 				break;
70 			if (*tmp == '\0')
71 				break;
72 		}
73 		*tmp = '\0';
74 		strcpy(list[i], head);
75 		head = tmp + 1;
76 		i++;
77 	}
78 
79 	return i-1;
80 }
81 
82 int get_cur_clocksource(char *buf, size_t size)
83 {
84 	ssize_t len;
85 	int fd;
86 
87 	fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
88 	if (fd < 0)
89 		return -1;
90 
91 	len = read(fd, buf, size - 1);
92 
93 	close(fd);
94 
95 	if (len <= 0)
96 		return -1;
97 
98 	buf[len] = '\0';
99 
100 	return 0;
101 }
102 
103 int change_clocksource(char *clocksource)
104 {
105 	int fd;
106 	ssize_t size;
107 
108 	fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
109 
110 	if (fd < 0)
111 		return -1;
112 
113 	size = write(fd, clocksource, strlen(clocksource));
114 
115 	if (size < 0)
116 		return -1;
117 
118 	close(fd);
119 	return 0;
120 }
121 
122 
123 int run_tests(int secs)
124 {
125 	int ret;
126 	char buf[255];
127 
128 	sprintf(buf, "./inconsistency-check -t %i", secs);
129 	ret = system(buf);
130 	if (WIFEXITED(ret) && WEXITSTATUS(ret))
131 		return WEXITSTATUS(ret);
132 	ret = system("./nanosleep");
133 	return WIFEXITED(ret) ? WEXITSTATUS(ret) : 0;
134 }
135 
136 
137 char clocksource_list[10][30];
138 
139 int main(int argc, char **argv)
140 {
141 	char orig_clk[512];
142 	int count, i, status, opt;
143 	int do_sanity_check = 1;
144 	int runtime = 60;
145 	pid_t pid;
146 
147 	/* Process arguments */
148 	while ((opt = getopt(argc, argv, "st:")) != -1) {
149 		switch (opt) {
150 		case 's':
151 			do_sanity_check = 0;
152 			break;
153 		case 't':
154 			runtime = atoi(optarg);
155 			break;
156 		default:
157 			printf("Usage: %s [-s] [-t <secs>]\n", argv[0]);
158 			printf("	-s: skip sanity checks\n");
159 			printf("	-t: Number of seconds to run\n");
160 			exit(-1);
161 		}
162 	}
163 
164 	get_cur_clocksource(orig_clk, 512);
165 
166 	count = get_clocksources(clocksource_list);
167 
168 	if (change_clocksource(clocksource_list[0])) {
169 		printf("Error: You probably need to run this as root\n");
170 		return -1;
171 	}
172 
173 	/* Check everything is sane before we start switching asynchronously */
174 	if (do_sanity_check) {
175 		for (i = 0; i < count; i++) {
176 			ksft_print_msg("Validating clocksource %s\n",
177 					clocksource_list[i]);
178 			if (change_clocksource(clocksource_list[i])) {
179 				status = -1;
180 				goto out;
181 			}
182 			if (run_tests(5)) {
183 				status = -1;
184 				goto out;
185 			}
186 		}
187 	}
188 
189 	ksft_print_msg("Running Asynchronous Switching Tests...\n");
190 	pid = fork();
191 	if (!pid)
192 		return run_tests(runtime);
193 
194 	while (pid != waitpid(pid, &status, WNOHANG))
195 		for (i = 0; i < count; i++)
196 			if (change_clocksource(clocksource_list[i])) {
197 				status = -1;
198 				goto out;
199 			}
200 out:
201 	change_clocksource(orig_clk);
202 
203 	/* Print at the end to not mix output with child process */
204 	ksft_print_header();
205 	ksft_set_plan(1);
206 	ksft_test_result(!status, "clocksource-switch\n");
207 	ksft_exit(!status);
208 }
209