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