xref: /linux/tools/testing/selftests/timers/change_skew.c (revision a8d74fe7fed55fe53066dca66708273f3dd9bb2f)
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 
33d8694245SJohn Stultz #define NSEC_PER_SEC 1000000000LL
34d8694245SJohn Stultz 
35d8694245SJohn Stultz 
36d8694245SJohn Stultz int change_skew_test(int ppm)
37d8694245SJohn Stultz {
38d8694245SJohn Stultz 	struct timex tx;
39d8694245SJohn Stultz 	int ret;
40d8694245SJohn Stultz 
41d8694245SJohn Stultz 	tx.modes = ADJ_FREQUENCY;
42d8694245SJohn Stultz 	tx.freq = ppm << 16;
43d8694245SJohn Stultz 
44d8694245SJohn Stultz 	ret = adjtimex(&tx);
45d8694245SJohn Stultz 	if (ret < 0) {
46d8694245SJohn Stultz 		printf("Error adjusting freq\n");
47d8694245SJohn Stultz 		return ret;
48d8694245SJohn Stultz 	}
49d8694245SJohn Stultz 
50d8694245SJohn Stultz 	ret = system("./raw_skew");
51d8694245SJohn Stultz 	ret |= system("./inconsistency-check");
52d8694245SJohn Stultz 	ret |= system("./nanosleep");
53d8694245SJohn Stultz 
54d8694245SJohn Stultz 	return ret;
55d8694245SJohn Stultz }
56d8694245SJohn Stultz 
57d8694245SJohn Stultz 
58*a8d74fe7SWolfram Sang int main(int argc, char **argv)
59d8694245SJohn Stultz {
60d8694245SJohn Stultz 	struct timex tx;
61d8694245SJohn Stultz 	int i, ret;
62d8694245SJohn Stultz 
63d8694245SJohn Stultz 	int ppm[5] = {0, 250, 500, -250, -500};
64d8694245SJohn Stultz 
65d8694245SJohn Stultz 	/* Kill ntpd */
66d8694245SJohn Stultz 	ret = system("killall -9 ntpd");
67d8694245SJohn Stultz 
68d8694245SJohn Stultz 	/* Make sure there's no offset adjustment going on */
69d8694245SJohn Stultz 	tx.modes = ADJ_OFFSET;
70d8694245SJohn Stultz 	tx.offset = 0;
71d8694245SJohn Stultz 	ret = adjtimex(&tx);
72d8694245SJohn Stultz 
73d8694245SJohn Stultz 	if (ret < 0) {
74d8694245SJohn Stultz 		printf("Maybe you're not running as root?\n");
75d8694245SJohn Stultz 		return -1;
76d8694245SJohn Stultz 	}
77d8694245SJohn Stultz 
78d8694245SJohn Stultz 	for (i = 0; i < 5; i++) {
79d8694245SJohn Stultz 		printf("Using %i ppm adjustment\n", ppm[i]);
80d8694245SJohn Stultz 		ret = change_skew_test(ppm[i]);
81d8694245SJohn Stultz 		if (ret)
82d8694245SJohn Stultz 			break;
83d8694245SJohn Stultz 	}
84d8694245SJohn Stultz 
85d8694245SJohn Stultz 	/* Set things back */
86d8694245SJohn Stultz 	tx.modes = ADJ_FREQUENCY;
87d8694245SJohn Stultz 	tx.offset = 0;
88d8694245SJohn Stultz 	adjtimex(&tx);
89d8694245SJohn Stultz 
90d8694245SJohn Stultz 	if (ret) {
91d8694245SJohn Stultz 		printf("[FAIL]");
92d8694245SJohn Stultz 		return ksft_exit_fail();
93d8694245SJohn Stultz 	}
94d8694245SJohn Stultz 	printf("[OK]");
95d8694245SJohn Stultz 	return ksft_exit_pass();
96d8694245SJohn Stultz }
97