xref: /freebsd/contrib/ntp/util/timetrim.c (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 #if defined(sgi) || defined(_UNICOSMP)
2 /*
3  * timetrim.c
4  *
5  * "timetrim" allows setting and adjustment of the system clock frequency
6  * trim parameter on Silicon Graphics machines.  The trim value native
7  * units are nanoseconds per second (10**-9), so a trim value of 1 makes
8  * the system clock step ahead 1 nanosecond more per second than a value
9  * of zero.  Xntpd currently uses units of 2**-20 secs for its frequency
10  * offset (drift) values; to convert to a timetrim value, multiply by
11  * 1E9 / 2**20 (about 954).
12  *
13  * "timetrim" with no arguments just prints out the current kernel value.
14  * With a numeric argument, the kernel value is set to the supplied value.
15  * The "-i" flag causes the supplied value to be added to the kernel value.
16  * The "-n" option causes all input and output to be in xntpd units rather
17  * than timetrim native units.
18  *
19  * Note that there is a limit of +-3000000 (0.3%) on the timetrim value
20  * which is (silently?) enforced by the kernel.
21  *
22  */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdio.h>
29 #include <ctype.h>
30 #include <stdlib.h>
31 #ifdef HAVE_SYS_SYSSGI_H
32 # include <sys/syssgi.h>
33 #endif
34 #ifdef HAVE_SYS_SYSTUNE_H
35 # include <sys/systune.h>
36 #endif
37 
38 #define abs(X) (((X) < 0) ? -(X) : (X))
39 #define USAGE "usage: timetrim [-n] [[-i] value]\n"
40 #define SGITONTP(X) ((double)(X) * 1048576.0/1.0e9)
41 #define NTPTOSGI(X) ((long)((X) * 1.0e9/1048576.0))
42 
43 int
44 main(
45 	int argc,
46 	char *argv[]
47 	)
48 {
49 	char *rem;
50 	int incremental = 0, ntpunits = 0;
51 	long timetrim;
52 	double value;
53 
54 	while (--argc && **++argv == '-' && isalpha((int)argv[0][1])) {
55 		switch (argv[0][1]) {
56 		    case 'i':
57 			incremental++;
58 			break;
59 		    case 'n':
60 			ntpunits++;
61 			break;
62 		    default:
63 			fprintf(stderr, USAGE);
64 			exit(1);
65 		}
66 	}
67 
68 #ifdef HAVE_SYS_SYSSGI_H
69 	if (syssgi(SGI_GETTIMETRIM, &timetrim) < 0) {
70 		perror("syssgi");
71 		exit(2);
72 	}
73 #endif
74 #ifdef HAVE_SYS_SYSTUNE_H
75 	if (systune(SYSTUNE_GET, "timetrim", &timetrim) < 0) {
76 		perror("systune");
77 		exit(2);
78 	}
79 #endif
80 
81 	if (argc == 0) {
82 		if (ntpunits)
83 		    fprintf(stdout, "%0.5f\n", SGITONTP(timetrim));
84 		else
85 		    fprintf(stdout, "%ld\n", timetrim);
86 	} else if (argc != 1) {
87 		fprintf(stderr, USAGE);
88 		exit(1);
89 	} else {
90 		value = strtod(argv[0], &rem);
91 		if (*rem != '\0') {
92 			fprintf(stderr, USAGE);
93 			exit(1);
94 		}
95 		if (ntpunits)
96 		    value = NTPTOSGI(value);
97 		if (incremental)
98 		    timetrim += value;
99 		else
100 		    timetrim = value;
101 #ifdef HAVE_SYS_SYSSGI_H
102 		if (syssgi(SGI_SETTIMETRIM, timetrim) < 0) {
103 			perror("syssgi");
104 			exit(2);
105 		}
106 #endif
107 #ifdef HAVE_SYS_SYSTUNE_H
108 		if (systune(SYSTUNE_SET, "timer", "timetrim", &timetrim) < 0) {
109 			perror("systune");
110 			exit(2);
111 		}
112 #endif
113 	}
114 	return 0;
115 }
116 #endif
117