xref: /titanic_53/usr/src/cmd/sa/timex.c (revision 231ecbdf6cfa117492d49356673032a7cdbd4030)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51337fffaSsr161167  * Common Development and Distribution License (the "License").
61337fffaSsr161167  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d2117003Sdp /*
22*231ecbdfSas158974  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23d2117003Sdp  * Use is subject to license terms.
24d2117003Sdp  */
251337fffaSsr161167 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /*	All Rights Reserved	*/
287c478bd9Sstevel@tonic-gate 
29d2117003Sdp #pragma ident	"%Z%%M%	%I%	%E% SMI"
30d2117003Sdp 
317c478bd9Sstevel@tonic-gate #include <sys/types.h>
327c478bd9Sstevel@tonic-gate #include <sys/times.h>
33*231ecbdfSas158974 #include <sys/time.h>
347c478bd9Sstevel@tonic-gate #include <sys/param.h>
35d2117003Sdp #include <sys/wait.h>
36d2117003Sdp #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
38d2117003Sdp #include <stdlib.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
40d2117003Sdp #include <strings.h>
417c478bd9Sstevel@tonic-gate #include <time.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <pwd.h>
447c478bd9Sstevel@tonic-gate 
45*231ecbdfSas158974 #define	NSEC_TO_TICK(nsec)	((nsec) / nsec_per_tick)
46*231ecbdfSas158974 #define	NSEC_TO_TICK_ROUNDUP(nsec) NSEC_TO_TICK((nsec) + \
47*231ecbdfSas158974 	nsec_per_tick/2)
48*231ecbdfSas158974 
497c478bd9Sstevel@tonic-gate char	fname[20];
50*231ecbdfSas158974 static int hz;
51*231ecbdfSas158974 static int nsec_per_tick;
527c478bd9Sstevel@tonic-gate 
53*231ecbdfSas158974 void printt(char *, hrtime_t);
54d2117003Sdp void hmstime(char[]);
55d2117003Sdp void diag(char *);
56d2117003Sdp 
57d2117003Sdp int
58d2117003Sdp main(int argc, char **argv)
597c478bd9Sstevel@tonic-gate {
607c478bd9Sstevel@tonic-gate 	struct	tms buffer, obuffer;
617c478bd9Sstevel@tonic-gate 	int	status;
627c478bd9Sstevel@tonic-gate 	register pid_t	p;
637c478bd9Sstevel@tonic-gate 	int	c;
64*231ecbdfSas158974 	hrtime_t before, after, timediff;
657c478bd9Sstevel@tonic-gate 	char	stime[9], etime[9];
667c478bd9Sstevel@tonic-gate 	char	cmd[80];
677c478bd9Sstevel@tonic-gate 	int	pflg = 0, sflg = 0, oflg = 0;
687c478bd9Sstevel@tonic-gate 	char	aopt[25];
69d2117003Sdp 	FILE	*pipin;
707c478bd9Sstevel@tonic-gate 	char	ttyid[12], line[150];
717c478bd9Sstevel@tonic-gate 	char	eol;
727c478bd9Sstevel@tonic-gate 	char	fld[20][12];
737c478bd9Sstevel@tonic-gate 	int	iline = 0, i, nfld;
747c478bd9Sstevel@tonic-gate 	int	ichar, iblok;
757c478bd9Sstevel@tonic-gate 	long	chars = 0, bloks = 0;
767c478bd9Sstevel@tonic-gate 
77*231ecbdfSas158974 	aopt[0] = '\0';
789ba7e32fSdduvall 
79*231ecbdfSas158974 	hz = sysconf(_SC_CLK_TCK);
80*231ecbdfSas158974 	nsec_per_tick = NANOSEC / hz;
81*231ecbdfSas158974 
827c478bd9Sstevel@tonic-gate 	/* check options; */
837c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
847c478bd9Sstevel@tonic-gate 		switch (c)  {
857c478bd9Sstevel@tonic-gate 		case 's':  sflg++;  break;
867c478bd9Sstevel@tonic-gate 		case 'o':  oflg++;  break;
877c478bd9Sstevel@tonic-gate 		case 'p':  pflg++;  break;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		case 'f':  strcat(aopt, "-f ");  break;
907c478bd9Sstevel@tonic-gate 		case 'h':  strcat(aopt, "-h ");  break;
917c478bd9Sstevel@tonic-gate 		case 'k':  strcat(aopt, "-k ");  break;
927c478bd9Sstevel@tonic-gate 		case 'm':  strcat(aopt, "-m ");  break;
937c478bd9Sstevel@tonic-gate 		case 'r':  strcat(aopt, "-r ");  break;
947c478bd9Sstevel@tonic-gate 		case 't':  strcat(aopt, "-t ");  break;
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 		case '?':  diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd");
977c478bd9Sstevel@tonic-gate 				break;
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 	if (optind >= argc)	diag("Missing command");
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/*
1027c478bd9Sstevel@tonic-gate 	 * Check to see if accounting is installed and print a somewhat
1037c478bd9Sstevel@tonic-gate 	 * meaninful message if not.
1047c478bd9Sstevel@tonic-gate 	 */
1057c478bd9Sstevel@tonic-gate 	if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
1067c478bd9Sstevel@tonic-gate 		oflg = 0;
1077c478bd9Sstevel@tonic-gate 		pflg = 0;
1087c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1097c478bd9Sstevel@tonic-gate 		    "Information from -p and -o options not available\n");
1107c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1117c478bd9Sstevel@tonic-gate 		    " because process accounting is not operational.\n");
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	if (sflg) {
1157c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/tmx%ld", getpid());
1167c478bd9Sstevel@tonic-gate 		sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
1177c478bd9Sstevel@tonic-gate 		system(cmd);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(stime);
120*231ecbdfSas158974 	before = gethrtime();
121*231ecbdfSas158974 	(void) times(&obuffer);
122*231ecbdfSas158974 	if ((p = fork()) == (pid_t)-1) {
123*231ecbdfSas158974 		perror("Fork Failed");
124*231ecbdfSas158974 		(void) unlink(fname);
125*231ecbdfSas158974 		exit(EXIT_FAILURE);
126*231ecbdfSas158974 	}
1277c478bd9Sstevel@tonic-gate 	if (p == 0) {
1287c478bd9Sstevel@tonic-gate 		setgid(getgid());
1297c478bd9Sstevel@tonic-gate 		execvp(*(argv+optind), (argv+optind));
130d2117003Sdp 		fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
131*231ecbdfSas158974 		exit(EXIT_FAILURE);
1327c478bd9Sstevel@tonic-gate 	}
1337c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
1347c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
135d2117003Sdp 	while (wait(&status) != p)
136d2117003Sdp 		;
1377c478bd9Sstevel@tonic-gate 	if ((status&0377) != 0)
1387c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Command terminated abnormally.\n");
1397c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_DFL);
1407c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_DFL);
141*231ecbdfSas158974 	(void) times(&buffer);
142*231ecbdfSas158974 	after = gethrtime();
143*231ecbdfSas158974 	timediff = after - before;
1447c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(etime);
1457c478bd9Sstevel@tonic-gate 	if (sflg) system(cmd);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
148*231ecbdfSas158974 	printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
1499ba7e32fSdduvall 	printt("user", buffer.tms_cutime - obuffer.tms_cutime);
1509ba7e32fSdduvall 	printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
1517c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (oflg+pflg) {
1547c478bd9Sstevel@tonic-gate 		if (isatty(0))
1557c478bd9Sstevel@tonic-gate 			sprintf(ttyid, "-l %s", ttyname(0)+5);
1567c478bd9Sstevel@tonic-gate 		sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
1577c478bd9Sstevel@tonic-gate 		    stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
1587c478bd9Sstevel@tonic-gate 		pipin = popen(cmd, "r");
1597c478bd9Sstevel@tonic-gate 		while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
1607c478bd9Sstevel@tonic-gate 			if (pflg)
1617c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s\n", line);
1627c478bd9Sstevel@tonic-gate 			if (oflg)  {
1637c478bd9Sstevel@tonic-gate 				nfld = sscanf(line,
1647c478bd9Sstevel@tonic-gate 				    "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1657c478bd9Sstevel@tonic-gate 				    fld[0], fld[1], fld[2], fld[3], fld[4],
1667c478bd9Sstevel@tonic-gate 				    fld[5], fld[6], fld[7], fld[8], fld[9],
1677c478bd9Sstevel@tonic-gate 				    fld[10], fld[11], fld[12], fld[13], fld[14],
168d2117003Sdp 				    fld[15], fld[16], fld[17], fld[18],
169d2117003Sdp 				    fld[19]);
1707c478bd9Sstevel@tonic-gate 				if (++iline == 3)
1717c478bd9Sstevel@tonic-gate 					for (i = 0; i < nfld; i++)  {
1727c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "CHARS")
1737c478bd9Sstevel@tonic-gate 						    == 0)
1747c478bd9Sstevel@tonic-gate 							ichar = i+2;
1757c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "BLOCKS")
1767c478bd9Sstevel@tonic-gate 						    == 0)
1777c478bd9Sstevel@tonic-gate 							iblok = i+2;
1787c478bd9Sstevel@tonic-gate 					}
1797c478bd9Sstevel@tonic-gate 				if (iline > 4)  {
1807c478bd9Sstevel@tonic-gate 					chars += atol(fld[ichar]);
1817c478bd9Sstevel@tonic-gate 					bloks += atol(fld[iblok]);
1827c478bd9Sstevel@tonic-gate 				}
1837c478bd9Sstevel@tonic-gate 			}
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		pclose(pipin);
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 		if (oflg)
1887c478bd9Sstevel@tonic-gate 			if (iline > 4)
1897c478bd9Sstevel@tonic-gate 				fprintf(stderr,
190d2117003Sdp 				    "\nCHARS TRNSFD = %ld\n"
191d2117003Sdp 				    "BLOCKS READ  = %ld\n", chars, bloks);
1927c478bd9Sstevel@tonic-gate 			else
1937c478bd9Sstevel@tonic-gate 				fprintf(stderr,
1947c478bd9Sstevel@tonic-gate 				    "\nNo process records found!\n");
1957c478bd9Sstevel@tonic-gate 	}
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	if (sflg)  {
198d2117003Sdp 		sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
1997c478bd9Sstevel@tonic-gate 		system(cmd);
2007c478bd9Sstevel@tonic-gate 		unlink(fname);
2017c478bd9Sstevel@tonic-gate 	}
202*231ecbdfSas158974 	exit(WEXITSTATUS(status));
2037c478bd9Sstevel@tonic-gate }
2047c478bd9Sstevel@tonic-gate 
205d2117003Sdp void
206*231ecbdfSas158974 printt(char *label, hrtime_t ticks)
2079ba7e32fSdduvall {
208*231ecbdfSas158974 	long tk;	/* number of leftover ticks   */
209*231ecbdfSas158974 	long ss;	/* number of seconds */
210*231ecbdfSas158974 	long mm;	/* number of minutes */
211*231ecbdfSas158974 	long hh;	/* number of hours   */
212*231ecbdfSas158974 	longlong_t total = ticks;
2137c478bd9Sstevel@tonic-gate 
214*231ecbdfSas158974 	tk	= total % hz;	/* ticks % hz		*/
215*231ecbdfSas158974 	total	/= hz;
216*231ecbdfSas158974 	ss	= total % 60;	/* ticks / hz % 60	*/
217*231ecbdfSas158974 	total	/= 60;
218*231ecbdfSas158974 	mm	= total % 60;	/* ticks / hz / 60 % 60 */
219*231ecbdfSas158974 	hh	= total / 60;	/* ticks / hz / 60 / 60 */
220*231ecbdfSas158974 
221*231ecbdfSas158974 	(void) fprintf(stderr, "%s ", label);
222*231ecbdfSas158974 
223*231ecbdfSas158974 	/* Display either padding or the elapsed hours */
224*231ecbdfSas158974 	if (hh == 0L) {
225*231ecbdfSas158974 		(void) fprintf(stderr, "%6c", ' ');
226*231ecbdfSas158974 	} else {
227*231ecbdfSas158974 		(void) fprintf(stderr, "%5ld:", hh);
2287c478bd9Sstevel@tonic-gate 	}
229*231ecbdfSas158974 
230*231ecbdfSas158974 	/*
231*231ecbdfSas158974 	 * Display either nothing or the elapsed minutes, zero
232*231ecbdfSas158974 	 * padding (if hours > 0) or space padding (if not).
233*231ecbdfSas158974 	 */
234*231ecbdfSas158974 	if (mm == 0L && hh == 0L) {
235*231ecbdfSas158974 		(void) fprintf(stderr, "%3c", ' ');
236*231ecbdfSas158974 	} else if (mm != 0L && hh == 0L) {
237*231ecbdfSas158974 		(void) fprintf(stderr, "%2ld:", mm);
238*231ecbdfSas158974 	} else {
239*231ecbdfSas158974 		(void) fprintf(stderr, "%02ld:", mm);
2407c478bd9Sstevel@tonic-gate 	}
241*231ecbdfSas158974 
242*231ecbdfSas158974 	/*
243*231ecbdfSas158974 	 * Display the elapsed seconds; seconds are always
244*231ecbdfSas158974 	 * zero padded.
245*231ecbdfSas158974 	 */
246*231ecbdfSas158974 	if (hh == 0L && mm == 0L) {
247*231ecbdfSas158974 		(void) fprintf(stderr, "%2ld.", ss);
248*231ecbdfSas158974 	} else {
249*231ecbdfSas158974 		(void) fprintf(stderr, "%02ld.", ss);
250*231ecbdfSas158974 	}
251*231ecbdfSas158974 
252*231ecbdfSas158974 	/* Display hundredths of a second. */
253*231ecbdfSas158974 	(void) fprintf(stderr, "%02ld\n", tk * 100/hz);
2547c478bd9Sstevel@tonic-gate }
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate /*
257d2117003Sdp  * hmstime() sets current time in hh:mm:ss string format in stime;
2587c478bd9Sstevel@tonic-gate  */
2597c478bd9Sstevel@tonic-gate 
260d2117003Sdp void
261d2117003Sdp hmstime(char stime[])
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	char	*ltime;
2647c478bd9Sstevel@tonic-gate 	time_t tme;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	tme = time((time_t *)0);
2677c478bd9Sstevel@tonic-gate 	ltime = ctime(&tme);
2687c478bd9Sstevel@tonic-gate 	strncpy(stime, ltime+11, 8);
2697c478bd9Sstevel@tonic-gate 	stime[8] = '\0';
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
272d2117003Sdp void
273d2117003Sdp diag(char *s)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	fprintf(stderr, "%s\n", s);
2767c478bd9Sstevel@tonic-gate 	unlink(fname);
277*231ecbdfSas158974 	exit(EXIT_FAILURE);
2787c478bd9Sstevel@tonic-gate }
279