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