xref: /titanic_50/usr/src/cmd/sa/timex.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.19 */
27*7c478bd9Sstevel@tonic-gate /*	timex.c 1.19 of 7/21/89 	*/
28*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
29*7c478bd9Sstevel@tonic-gate #include <sys/times.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <signal.h>
33*7c478bd9Sstevel@tonic-gate #include <time.h>
34*7c478bd9Sstevel@tonic-gate #include <errno.h>
35*7c478bd9Sstevel@tonic-gate #include <pwd.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate long	atol();
38*7c478bd9Sstevel@tonic-gate extern	time_t time();
39*7c478bd9Sstevel@tonic-gate int	isatty();
40*7c478bd9Sstevel@tonic-gate char	*ttyname();
41*7c478bd9Sstevel@tonic-gate char	*strcat();
42*7c478bd9Sstevel@tonic-gate char	*strncpy();
43*7c478bd9Sstevel@tonic-gate struct	passwd *getpwuid();
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate extern int errno;
46*7c478bd9Sstevel@tonic-gate extern char *sys_errlist[];
47*7c478bd9Sstevel@tonic-gate char	fname[20];
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate /*
50*7c478bd9Sstevel@tonic-gate  * Quant[0] will get set to HZ/10 later.
51*7c478bd9Sstevel@tonic-gate  */
52*7c478bd9Sstevel@tonic-gate char quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 };
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate main(argc, argv)
55*7c478bd9Sstevel@tonic-gate int argc;
56*7c478bd9Sstevel@tonic-gate char **argv;
57*7c478bd9Sstevel@tonic-gate {
58*7c478bd9Sstevel@tonic-gate 	struct	tms buffer, obuffer;
59*7c478bd9Sstevel@tonic-gate 	int	status;
60*7c478bd9Sstevel@tonic-gate 	register pid_t	p;
61*7c478bd9Sstevel@tonic-gate 	int	c;
62*7c478bd9Sstevel@tonic-gate 	time_t	before, after;
63*7c478bd9Sstevel@tonic-gate 	char	stime[9], etime[9];
64*7c478bd9Sstevel@tonic-gate 	char	cmd[80];
65*7c478bd9Sstevel@tonic-gate 	extern	char	*optarg;
66*7c478bd9Sstevel@tonic-gate 	extern	int	optind;
67*7c478bd9Sstevel@tonic-gate 	int	pflg = 0, sflg = 0, oflg = 0;
68*7c478bd9Sstevel@tonic-gate 	char	aopt[25];
69*7c478bd9Sstevel@tonic-gate 	FILE	*popen(), *pipin;
70*7c478bd9Sstevel@tonic-gate 	char	ttyid[12], line[150];
71*7c478bd9Sstevel@tonic-gate 	char	eol;
72*7c478bd9Sstevel@tonic-gate 	char	fld[20][12];
73*7c478bd9Sstevel@tonic-gate 	int	iline = 0, i, nfld;
74*7c478bd9Sstevel@tonic-gate 	int	ichar, iblok;
75*7c478bd9Sstevel@tonic-gate 	long	chars = 0, bloks = 0;
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate 	/* initalize quant array using the sysconf()	*/
78*7c478bd9Sstevel@tonic-gate 	quant[0] = ((int)sysconf(_SC_CLK_TCK))/10;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	aopt[0] = '\0';			/* terminate the string #1245107 */
81*7c478bd9Sstevel@tonic-gate 	/* check options; */
82*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
83*7c478bd9Sstevel@tonic-gate 		switch (c)  {
84*7c478bd9Sstevel@tonic-gate 		case 's':  sflg++;  break;
85*7c478bd9Sstevel@tonic-gate 		case 'o':  oflg++;  break;
86*7c478bd9Sstevel@tonic-gate 		case 'p':  pflg++;  break;
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate 		case 'f':  strcat(aopt, "-f ");  break;
89*7c478bd9Sstevel@tonic-gate 		case 'h':  strcat(aopt, "-h ");  break;
90*7c478bd9Sstevel@tonic-gate 		case 'k':  strcat(aopt, "-k ");  break;
91*7c478bd9Sstevel@tonic-gate 		case 'm':  strcat(aopt, "-m ");  break;
92*7c478bd9Sstevel@tonic-gate 		case 'r':  strcat(aopt, "-r ");  break;
93*7c478bd9Sstevel@tonic-gate 		case 't':  strcat(aopt, "-t ");  break;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 		case '?':  diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd");
96*7c478bd9Sstevel@tonic-gate 				break;
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 	if (optind >= argc)	diag("Missing command");
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate 	/*
101*7c478bd9Sstevel@tonic-gate 	 * Check to see if accounting is installed and print a somewhat
102*7c478bd9Sstevel@tonic-gate 	 * meaninful message if not.
103*7c478bd9Sstevel@tonic-gate 	 */
104*7c478bd9Sstevel@tonic-gate 	if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
105*7c478bd9Sstevel@tonic-gate 		oflg = 0;
106*7c478bd9Sstevel@tonic-gate 		pflg = 0;
107*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
108*7c478bd9Sstevel@tonic-gate 			"Information from -p and -o options not available\n");
109*7c478bd9Sstevel@tonic-gate 		fprintf(stderr,
110*7c478bd9Sstevel@tonic-gate 			" because process accounting is not operational.\n");
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	if (sflg) {
114*7c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/tmx%ld", getpid());
115*7c478bd9Sstevel@tonic-gate 		sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
116*7c478bd9Sstevel@tonic-gate 		system(cmd);
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(stime);
119*7c478bd9Sstevel@tonic-gate 	before = times(&obuffer);
120*7c478bd9Sstevel@tonic-gate 	if ((p = fork()) == (pid_t)-1) diag("Try again.\n");
121*7c478bd9Sstevel@tonic-gate 	if (p == 0) {
122*7c478bd9Sstevel@tonic-gate 		setgid(getgid());
123*7c478bd9Sstevel@tonic-gate 		execvp(*(argv+optind), (argv+optind));
124*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "%s: %s\n", *(argv+optind), sys_errlist[errno]);
125*7c478bd9Sstevel@tonic-gate 		exit(1);
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
128*7c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
129*7c478bd9Sstevel@tonic-gate 	while (wait(&status) != p);
130*7c478bd9Sstevel@tonic-gate 	if ((status&0377) != 0)
131*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Command terminated abnormally.\n");
132*7c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_DFL);
133*7c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_DFL);
134*7c478bd9Sstevel@tonic-gate 	after = times(&buffer);
135*7c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(etime);
136*7c478bd9Sstevel@tonic-gate 	if (sflg) system(cmd);
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
139*7c478bd9Sstevel@tonic-gate 	printt("real", (after-before));
140*7c478bd9Sstevel@tonic-gate 	printt("user", buffer.tms_cutime - obuffer.tms_cutime);
141*7c478bd9Sstevel@tonic-gate 	printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
142*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate 	if (oflg+pflg) {
145*7c478bd9Sstevel@tonic-gate 		if (isatty(0))
146*7c478bd9Sstevel@tonic-gate 			sprintf(ttyid, "-l %s", ttyname(0)+5);
147*7c478bd9Sstevel@tonic-gate 		sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
148*7c478bd9Sstevel@tonic-gate 			stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
149*7c478bd9Sstevel@tonic-gate 		pipin = popen(cmd, "r");
150*7c478bd9Sstevel@tonic-gate 		while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
151*7c478bd9Sstevel@tonic-gate 			if (pflg)
152*7c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s\n", line);
153*7c478bd9Sstevel@tonic-gate 			if (oflg)  {
154*7c478bd9Sstevel@tonic-gate 				nfld = sscanf(line,
155*7c478bd9Sstevel@tonic-gate 				"%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
156*7c478bd9Sstevel@tonic-gate 				fld[0], fld[1], fld[2], fld[3], fld[4],
157*7c478bd9Sstevel@tonic-gate 				fld[5], fld[6], fld[7], fld[8], fld[9],
158*7c478bd9Sstevel@tonic-gate 				fld[10], fld[11], fld[12], fld[13], fld[14],
159*7c478bd9Sstevel@tonic-gate 				fld[15], fld[16], fld[17], fld[18], fld[19]);
160*7c478bd9Sstevel@tonic-gate 				if (++iline == 3)
161*7c478bd9Sstevel@tonic-gate 					for (i = 0; i < nfld; i++)  {
162*7c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "CHARS")
163*7c478bd9Sstevel@tonic-gate 						    == 0)
164*7c478bd9Sstevel@tonic-gate 							ichar = i+2;
165*7c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "BLOCKS")
166*7c478bd9Sstevel@tonic-gate 						    == 0)
167*7c478bd9Sstevel@tonic-gate 							iblok = i+2;
168*7c478bd9Sstevel@tonic-gate 					}
169*7c478bd9Sstevel@tonic-gate 				if (iline > 4)  {
170*7c478bd9Sstevel@tonic-gate 					chars += atol(fld[ichar]);
171*7c478bd9Sstevel@tonic-gate 					bloks += atol(fld[iblok]);
172*7c478bd9Sstevel@tonic-gate 				}
173*7c478bd9Sstevel@tonic-gate 			}
174*7c478bd9Sstevel@tonic-gate 		}
175*7c478bd9Sstevel@tonic-gate 		pclose(pipin);
176*7c478bd9Sstevel@tonic-gate 
177*7c478bd9Sstevel@tonic-gate 		if (oflg)
178*7c478bd9Sstevel@tonic-gate 			if (iline > 4)
179*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
180*7c478bd9Sstevel@tonic-gate 				"\nCHARS TRNSFD = %ld\nBLOCKS READ  = %ld\n",
181*7c478bd9Sstevel@tonic-gate 				chars, bloks);
182*7c478bd9Sstevel@tonic-gate 			else
183*7c478bd9Sstevel@tonic-gate 				fprintf(stderr,
184*7c478bd9Sstevel@tonic-gate 					"\nNo process records found!\n");
185*7c478bd9Sstevel@tonic-gate 	}
186*7c478bd9Sstevel@tonic-gate 
187*7c478bd9Sstevel@tonic-gate 	if (sflg)  {
188*7c478bd9Sstevel@tonic-gate 		sprintf(cmd,
189*7c478bd9Sstevel@tonic-gate 			"/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
190*7c478bd9Sstevel@tonic-gate 		system(cmd);
191*7c478bd9Sstevel@tonic-gate 		unlink(fname);
192*7c478bd9Sstevel@tonic-gate 	}
193*7c478bd9Sstevel@tonic-gate 	exit(status>>8);
194*7c478bd9Sstevel@tonic-gate }
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate char *pad  = "000      ";
197*7c478bd9Sstevel@tonic-gate char *sep  = "\0\0.\0:\0:\0\0";
198*7c478bd9Sstevel@tonic-gate char *nsep = "\0\0.\0 \0 \0\0";
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate printt(s, a)
201*7c478bd9Sstevel@tonic-gate char	*s;
202*7c478bd9Sstevel@tonic-gate time_t	a;
203*7c478bd9Sstevel@tonic-gate {
204*7c478bd9Sstevel@tonic-gate 	char	digit[9];
205*7c478bd9Sstevel@tonic-gate 	register	i;
206*7c478bd9Sstevel@tonic-gate 	char	c;
207*7c478bd9Sstevel@tonic-gate 	int	nonzero;
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < 9; i++) {
210*7c478bd9Sstevel@tonic-gate 		digit[i] = a % quant[i];
211*7c478bd9Sstevel@tonic-gate 		a /= quant[i];
212*7c478bd9Sstevel@tonic-gate 	}
213*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, s);
214*7c478bd9Sstevel@tonic-gate 	nonzero = 0;
215*7c478bd9Sstevel@tonic-gate 	while (--i > 0) {
216*7c478bd9Sstevel@tonic-gate 		c = digit[i] != 0 ? digit[i] + '0':
217*7c478bd9Sstevel@tonic-gate 		    nonzero ? '0':
218*7c478bd9Sstevel@tonic-gate 		    pad[i];
219*7c478bd9Sstevel@tonic-gate 		if (c != '\0') putc(c, stderr);
220*7c478bd9Sstevel@tonic-gate 		nonzero |= digit[i];
221*7c478bd9Sstevel@tonic-gate 		c = nonzero?sep[i]:nsep[i];
222*7c478bd9Sstevel@tonic-gate 		if (c != '\0') putc(c, stderr);
223*7c478bd9Sstevel@tonic-gate 	}
224*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%c", digit[0] * 100/HZ + '0');
225*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
226*7c478bd9Sstevel@tonic-gate }
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate /*
229*7c478bd9Sstevel@tonic-gate ** hmstime() sets current time in hh:mm:ss string format in stime;
230*7c478bd9Sstevel@tonic-gate */
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate hmstime(stime)
233*7c478bd9Sstevel@tonic-gate char	stime[];
234*7c478bd9Sstevel@tonic-gate {
235*7c478bd9Sstevel@tonic-gate 	char	*ltime;
236*7c478bd9Sstevel@tonic-gate 	time_t tme;
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	tme = time((time_t *)0);
239*7c478bd9Sstevel@tonic-gate 	ltime = ctime(&tme);
240*7c478bd9Sstevel@tonic-gate 	strncpy(stime, ltime+11, 8);
241*7c478bd9Sstevel@tonic-gate 	stime[8] = '\0';
242*7c478bd9Sstevel@tonic-gate }
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate diag(s)
245*7c478bd9Sstevel@tonic-gate char *s;
246*7c478bd9Sstevel@tonic-gate {
247*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s\n", s);
248*7c478bd9Sstevel@tonic-gate 	unlink(fname);
249*7c478bd9Sstevel@tonic-gate 	exit(1);
250*7c478bd9Sstevel@tonic-gate }
251