1132451Sroberto#if defined(sgi) || defined(_UNICOSMP)
254359Sroberto/*
354359Sroberto * timetrim.c
454359Sroberto *
554359Sroberto * "timetrim" allows setting and adjustment of the system clock frequency
654359Sroberto * trim parameter on Silicon Graphics machines.  The trim value native
754359Sroberto * units are nanoseconds per second (10**-9), so a trim value of 1 makes
854359Sroberto * the system clock step ahead 1 nanosecond more per second than a value
954359Sroberto * of zero.  Xntpd currently uses units of 2**-20 secs for its frequency
1054359Sroberto * offset (drift) values; to convert to a timetrim value, multiply by
1154359Sroberto * 1E9 / 2**20 (about 954).
1254359Sroberto *
1354359Sroberto * "timetrim" with no arguments just prints out the current kernel value.
1454359Sroberto * With a numeric argument, the kernel value is set to the supplied value.
1554359Sroberto * The "-i" flag causes the supplied value to be added to the kernel value.
1654359Sroberto * The "-n" option causes all input and output to be in xntpd units rather
1754359Sroberto * than timetrim native units.
1854359Sroberto *
1954359Sroberto * Note that there is a limit of +-3000000 (0.3%) on the timetrim value
2054359Sroberto * which is (silently?) enforced by the kernel.
2154359Sroberto *
2254359Sroberto */
2354359Sroberto
2454359Sroberto#ifdef HAVE_CONFIG_H
2554359Sroberto#include <config.h>
2654359Sroberto#endif
2754359Sroberto
2854359Sroberto#include <stdio.h>
2954359Sroberto#include <ctype.h>
3082498Sroberto#include <stdlib.h>
3154359Sroberto#ifdef HAVE_SYS_SYSSGI_H
3254359Sroberto# include <sys/syssgi.h>
3354359Sroberto#endif
34132451Sroberto#ifdef HAVE_SYS_SYSTUNE_H
35132451Sroberto# include <sys/systune.h>
36132451Sroberto#endif
3754359Sroberto
3854359Sroberto#define abs(X) (((X) < 0) ? -(X) : (X))
3954359Sroberto#define USAGE "usage: timetrim [-n] [[-i] value]\n"
4054359Sroberto#define SGITONTP(X) ((double)(X) * 1048576.0/1.0e9)
4154359Sroberto#define NTPTOSGI(X) ((long)((X) * 1.0e9/1048576.0))
4254359Sroberto
4354359Srobertoint
4454359Srobertomain(
4554359Sroberto	int argc,
4654359Sroberto	char *argv[]
4754359Sroberto	)
4854359Sroberto{
4954359Sroberto	char *rem;
5082498Sroberto	int incremental = 0, ntpunits = 0;
5154359Sroberto	long timetrim;
5282498Sroberto	double value;
5354359Sroberto
5482498Sroberto	while (--argc && **++argv == '-' && isalpha((int)argv[0][1])) {
5554359Sroberto		switch (argv[0][1]) {
5654359Sroberto		    case 'i':
5754359Sroberto			incremental++;
5854359Sroberto			break;
5954359Sroberto		    case 'n':
6054359Sroberto			ntpunits++;
6154359Sroberto			break;
6254359Sroberto		    default:
6354359Sroberto			fprintf(stderr, USAGE);
6454359Sroberto			exit(1);
6554359Sroberto		}
6654359Sroberto	}
6754359Sroberto
68132451Sroberto#ifdef HAVE_SYS_SYSSGI_H
6954359Sroberto	if (syssgi(SGI_GETTIMETRIM, &timetrim) < 0) {
7054359Sroberto		perror("syssgi");
7154359Sroberto		exit(2);
7254359Sroberto	}
73132451Sroberto#endif
74132451Sroberto#ifdef HAVE_SYS_SYSTUNE_H
75132451Sroberto	if (systune(SYSTUNE_GET, "timetrim", &timetrim) < 0) {
76132451Sroberto		perror("systune");
77132451Sroberto		exit(2);
78132451Sroberto	}
79132451Sroberto#endif
8054359Sroberto
8154359Sroberto	if (argc == 0) {
8254359Sroberto		if (ntpunits)
8382498Sroberto		    fprintf(stdout, "%0.5f\n", SGITONTP(timetrim));
8454359Sroberto		else
8554359Sroberto		    fprintf(stdout, "%ld\n", timetrim);
8654359Sroberto	} else if (argc != 1) {
8754359Sroberto		fprintf(stderr, USAGE);
8854359Sroberto		exit(1);
8954359Sroberto	} else {
9054359Sroberto		value = strtod(argv[0], &rem);
9154359Sroberto		if (*rem != '\0') {
9254359Sroberto			fprintf(stderr, USAGE);
9354359Sroberto			exit(1);
9454359Sroberto		}
9554359Sroberto		if (ntpunits)
9654359Sroberto		    value = NTPTOSGI(value);
9754359Sroberto		if (incremental)
9854359Sroberto		    timetrim += value;
9954359Sroberto		else
10054359Sroberto		    timetrim = value;
101132451Sroberto#ifdef HAVE_SYS_SYSSGI_H
10254359Sroberto		if (syssgi(SGI_SETTIMETRIM, timetrim) < 0) {
10354359Sroberto			perror("syssgi");
10454359Sroberto			exit(2);
10554359Sroberto		}
106132451Sroberto#endif
107132451Sroberto#ifdef HAVE_SYS_SYSTUNE_H
108132451Sroberto		if (systune(SYSTUNE_SET, "timer", "timetrim", &timetrim) < 0) {
109132451Sroberto			perror("systune");
110132451Sroberto			exit(2);
111132451Sroberto		}
112132451Sroberto#endif
11354359Sroberto	}
11482498Sroberto	return 0;
11554359Sroberto}
11654359Sroberto#endif
117