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