xref: /linux/tools/testing/selftests/timers/change_skew.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1d8694245SJohn Stultz /* ADJ_FREQ Skew change test
2d8694245SJohn Stultz  *		by: john stultz (johnstul@us.ibm.com)
3d8694245SJohn Stultz  *		(C) Copyright IBM 2012
4d8694245SJohn Stultz  *		Licensed under the GPLv2
5d8694245SJohn Stultz  *
6d8694245SJohn Stultz  *  NOTE: This is a meta-test which cranks the ADJ_FREQ knob and
7d8694245SJohn Stultz  *  then uses other tests to detect problems. Thus this test requires
8d8694245SJohn Stultz  *  that the raw_skew, inconsistency-check and nanosleep tests be
9d8694245SJohn Stultz  *  present in the same directory it is run from.
10d8694245SJohn Stultz  *
11d8694245SJohn Stultz  *  To build:
12d8694245SJohn Stultz  *	$ gcc change_skew.c -o change_skew -lrt
13d8694245SJohn Stultz  *
14d8694245SJohn Stultz  *   This program is free software: you can redistribute it and/or modify
15d8694245SJohn Stultz  *   it under the terms of the GNU General Public License as published by
16d8694245SJohn Stultz  *   the Free Software Foundation, either version 2 of the License, or
17d8694245SJohn Stultz  *   (at your option) any later version.
18d8694245SJohn Stultz  *
19d8694245SJohn Stultz  *   This program is distributed in the hope that it will be useful,
20d8694245SJohn Stultz  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
21d8694245SJohn Stultz  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22d8694245SJohn Stultz  *   GNU General Public License for more details.
23d8694245SJohn Stultz  */
24d8694245SJohn Stultz 
25d8694245SJohn Stultz 
26d8694245SJohn Stultz #include <stdio.h>
27d8694245SJohn Stultz #include <stdlib.h>
28d8694245SJohn Stultz #include <sys/time.h>
29d8694245SJohn Stultz #include <sys/timex.h>
30d8694245SJohn Stultz #include <time.h>
31d8694245SJohn Stultz #include "../kselftest.h"
32d8694245SJohn Stultz 
change_skew_test(int ppm)33d8694245SJohn Stultz int change_skew_test(int ppm)
34d8694245SJohn Stultz {
35d8694245SJohn Stultz 	struct timex tx;
36d8694245SJohn Stultz 	int ret;
37d8694245SJohn Stultz 
38d8694245SJohn Stultz 	tx.modes = ADJ_FREQUENCY;
39d8694245SJohn Stultz 	tx.freq = ppm << 16;
40d8694245SJohn Stultz 
41d8694245SJohn Stultz 	ret = adjtimex(&tx);
42d8694245SJohn Stultz 	if (ret < 0) {
43d8694245SJohn Stultz 		printf("Error adjusting freq\n");
44d8694245SJohn Stultz 		return ret;
45d8694245SJohn Stultz 	}
46d8694245SJohn Stultz 
47d8694245SJohn Stultz 	ret = system("./raw_skew");
48d8694245SJohn Stultz 	ret |= system("./inconsistency-check");
49d8694245SJohn Stultz 	ret |= system("./nanosleep");
50d8694245SJohn Stultz 
51d8694245SJohn Stultz 	return ret;
52d8694245SJohn Stultz }
53d8694245SJohn Stultz 
54d8694245SJohn Stultz 
main(int argc,char ** argv)55a8d74fe7SWolfram Sang int main(int argc, char **argv)
56d8694245SJohn Stultz {
57d8694245SJohn Stultz 	struct timex tx;
58d8694245SJohn Stultz 	int i, ret;
59d8694245SJohn Stultz 
60d8694245SJohn Stultz 	int ppm[5] = {0, 250, 500, -250, -500};
61d8694245SJohn Stultz 
62d8694245SJohn Stultz 	/* Kill ntpd */
63d8694245SJohn Stultz 	ret = system("killall -9 ntpd");
64d8694245SJohn Stultz 
65d8694245SJohn Stultz 	/* Make sure there's no offset adjustment going on */
66d8694245SJohn Stultz 	tx.modes = ADJ_OFFSET;
67d8694245SJohn Stultz 	tx.offset = 0;
68d8694245SJohn Stultz 	ret = adjtimex(&tx);
69d8694245SJohn Stultz 
70d8694245SJohn Stultz 	if (ret < 0) {
71d8694245SJohn Stultz 		printf("Maybe you're not running as root?\n");
72d8694245SJohn Stultz 		return -1;
73d8694245SJohn Stultz 	}
74d8694245SJohn Stultz 
75d8694245SJohn Stultz 	for (i = 0; i < 5; i++) {
76d8694245SJohn Stultz 		printf("Using %i ppm adjustment\n", ppm[i]);
77d8694245SJohn Stultz 		ret = change_skew_test(ppm[i]);
78d8694245SJohn Stultz 		if (ret)
79d8694245SJohn Stultz 			break;
80d8694245SJohn Stultz 	}
81d8694245SJohn Stultz 
82d8694245SJohn Stultz 	/* Set things back */
83d8694245SJohn Stultz 	tx.modes = ADJ_FREQUENCY;
84d8694245SJohn Stultz 	tx.offset = 0;
85d8694245SJohn Stultz 	adjtimex(&tx);
86d8694245SJohn Stultz 
87d8694245SJohn Stultz 	if (ret) {
88d8694245SJohn Stultz 		printf("[FAIL]");
89*bc7e5d23SNathan Chancellor 		ksft_exit_fail();
90d8694245SJohn Stultz 	}
91d8694245SJohn Stultz 	printf("[OK]");
92*bc7e5d23SNathan Chancellor 	ksft_exit_pass();
93d8694245SJohn Stultz }
94