xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/timers/clocksource-switch.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /* Clocksource change test
2*4882a593Smuzhiyun  *		by: john stultz (johnstul@us.ibm.com)
3*4882a593Smuzhiyun  *		(C) Copyright IBM 2012
4*4882a593Smuzhiyun  *		Licensed under the GPLv2
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  *  NOTE: This is a meta-test which quickly changes the clocksourc and
7*4882a593Smuzhiyun  *  then uses other tests to detect problems. Thus this test requires
8*4882a593Smuzhiyun  *  that the inconsistency-check and nanosleep tests be present in the
9*4882a593Smuzhiyun  *  same directory it is run from.
10*4882a593Smuzhiyun  *
11*4882a593Smuzhiyun  *  To build:
12*4882a593Smuzhiyun  *	$ gcc clocksource-switch.c -o clocksource-switch -lrt
13*4882a593Smuzhiyun  *
14*4882a593Smuzhiyun  *   This program is free software: you can redistribute it and/or modify
15*4882a593Smuzhiyun  *   it under the terms of the GNU General Public License as published by
16*4882a593Smuzhiyun  *   the Free Software Foundation, either version 2 of the License, or
17*4882a593Smuzhiyun  *   (at your option) any later version.
18*4882a593Smuzhiyun  *
19*4882a593Smuzhiyun  *   This program is distributed in the hope that it will be useful,
20*4882a593Smuzhiyun  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21*4882a593Smuzhiyun  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22*4882a593Smuzhiyun  *   GNU General Public License for more details.
23*4882a593Smuzhiyun  */
24*4882a593Smuzhiyun 
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun #include <stdio.h>
27*4882a593Smuzhiyun #include <unistd.h>
28*4882a593Smuzhiyun #include <stdlib.h>
29*4882a593Smuzhiyun #include <sys/time.h>
30*4882a593Smuzhiyun #include <sys/timex.h>
31*4882a593Smuzhiyun #include <time.h>
32*4882a593Smuzhiyun #include <sys/types.h>
33*4882a593Smuzhiyun #include <sys/stat.h>
34*4882a593Smuzhiyun #include <fcntl.h>
35*4882a593Smuzhiyun #include <string.h>
36*4882a593Smuzhiyun #include <sys/wait.h>
37*4882a593Smuzhiyun #include "../kselftest.h"
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 
get_clocksources(char list[][30])40*4882a593Smuzhiyun int get_clocksources(char list[][30])
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun 	int fd, i;
43*4882a593Smuzhiyun 	size_t size;
44*4882a593Smuzhiyun 	char buf[512];
45*4882a593Smuzhiyun 	char *head, *tmp;
46*4882a593Smuzhiyun 
47*4882a593Smuzhiyun 	fd = open("/sys/devices/system/clocksource/clocksource0/available_clocksource", O_RDONLY);
48*4882a593Smuzhiyun 
49*4882a593Smuzhiyun 	size = read(fd, buf, 512);
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun 	close(fd);
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	for (i = 0; i < 10; i++)
54*4882a593Smuzhiyun 		list[i][0] = '\0';
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	head = buf;
57*4882a593Smuzhiyun 	i = 0;
58*4882a593Smuzhiyun 	while (head - buf < size) {
59*4882a593Smuzhiyun 		/* Find the next space */
60*4882a593Smuzhiyun 		for (tmp = head; *tmp != ' '; tmp++) {
61*4882a593Smuzhiyun 			if (*tmp == '\n')
62*4882a593Smuzhiyun 				break;
63*4882a593Smuzhiyun 			if (*tmp == '\0')
64*4882a593Smuzhiyun 				break;
65*4882a593Smuzhiyun 		}
66*4882a593Smuzhiyun 		*tmp = '\0';
67*4882a593Smuzhiyun 		strcpy(list[i], head);
68*4882a593Smuzhiyun 		head = tmp + 1;
69*4882a593Smuzhiyun 		i++;
70*4882a593Smuzhiyun 	}
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun 	return i-1;
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun 
get_cur_clocksource(char * buf,size_t size)75*4882a593Smuzhiyun int get_cur_clocksource(char *buf, size_t size)
76*4882a593Smuzhiyun {
77*4882a593Smuzhiyun 	int fd;
78*4882a593Smuzhiyun 
79*4882a593Smuzhiyun 	fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_RDONLY);
80*4882a593Smuzhiyun 
81*4882a593Smuzhiyun 	size = read(fd, buf, size);
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 	return 0;
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
change_clocksource(char * clocksource)86*4882a593Smuzhiyun int change_clocksource(char *clocksource)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	int fd;
89*4882a593Smuzhiyun 	ssize_t size;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	fd = open("/sys/devices/system/clocksource/clocksource0/current_clocksource", O_WRONLY);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	if (fd < 0)
94*4882a593Smuzhiyun 		return -1;
95*4882a593Smuzhiyun 
96*4882a593Smuzhiyun 	size = write(fd, clocksource, strlen(clocksource));
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (size < 0)
99*4882a593Smuzhiyun 		return -1;
100*4882a593Smuzhiyun 
101*4882a593Smuzhiyun 	close(fd);
102*4882a593Smuzhiyun 	return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun 
run_tests(int secs)106*4882a593Smuzhiyun int run_tests(int secs)
107*4882a593Smuzhiyun {
108*4882a593Smuzhiyun 	int ret;
109*4882a593Smuzhiyun 	char buf[255];
110*4882a593Smuzhiyun 
111*4882a593Smuzhiyun 	sprintf(buf, "./inconsistency-check -t %i", secs);
112*4882a593Smuzhiyun 	ret = system(buf);
113*4882a593Smuzhiyun 	if (WIFEXITED(ret) && WEXITSTATUS(ret))
114*4882a593Smuzhiyun 		return WEXITSTATUS(ret);
115*4882a593Smuzhiyun 	ret = system("./nanosleep");
116*4882a593Smuzhiyun 	return WIFEXITED(ret) ? WEXITSTATUS(ret) : 0;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun char clocksource_list[10][30];
121*4882a593Smuzhiyun 
main(int argv,char ** argc)122*4882a593Smuzhiyun int main(int argv, char **argc)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun 	char orig_clk[512];
125*4882a593Smuzhiyun 	int count, i, status;
126*4882a593Smuzhiyun 	pid_t pid;
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 	get_cur_clocksource(orig_clk, 512);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	count = get_clocksources(clocksource_list);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	if (change_clocksource(clocksource_list[0])) {
133*4882a593Smuzhiyun 		printf("Error: You probably need to run this as root\n");
134*4882a593Smuzhiyun 		return -1;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/* Check everything is sane before we start switching asyncrhonously */
138*4882a593Smuzhiyun 	for (i = 0; i < count; i++) {
139*4882a593Smuzhiyun 		printf("Validating clocksource %s\n", clocksource_list[i]);
140*4882a593Smuzhiyun 		if (change_clocksource(clocksource_list[i])) {
141*4882a593Smuzhiyun 			status = -1;
142*4882a593Smuzhiyun 			goto out;
143*4882a593Smuzhiyun 		}
144*4882a593Smuzhiyun 		if (run_tests(5)) {
145*4882a593Smuzhiyun 			status = -1;
146*4882a593Smuzhiyun 			goto out;
147*4882a593Smuzhiyun 		}
148*4882a593Smuzhiyun 	}
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	printf("Running Asynchronous Switching Tests...\n");
152*4882a593Smuzhiyun 	pid = fork();
153*4882a593Smuzhiyun 	if (!pid)
154*4882a593Smuzhiyun 		return run_tests(60);
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	while (pid != waitpid(pid, &status, WNOHANG))
157*4882a593Smuzhiyun 		for (i = 0; i < count; i++)
158*4882a593Smuzhiyun 			if (change_clocksource(clocksource_list[i])) {
159*4882a593Smuzhiyun 				status = -1;
160*4882a593Smuzhiyun 				goto out;
161*4882a593Smuzhiyun 			}
162*4882a593Smuzhiyun out:
163*4882a593Smuzhiyun 	change_clocksource(orig_clk);
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	if (status)
166*4882a593Smuzhiyun 		return ksft_exit_fail();
167*4882a593Smuzhiyun 	return ksft_exit_pass();
168*4882a593Smuzhiyun }
169