xref: /titanic_51/usr/src/cmd/sa/timex.c (revision 24292ef779c1060fa735368b7a0855a422f6eab8)
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*24292ef7Sesaxe  * Copyright 2008 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>
33231ecbdfSas158974 #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 
45231ecbdfSas158974 #define	NSEC_TO_TICK(nsec)	((nsec) / nsec_per_tick)
46231ecbdfSas158974 #define	NSEC_TO_TICK_ROUNDUP(nsec) NSEC_TO_TICK((nsec) + \
47231ecbdfSas158974 	nsec_per_tick/2)
48231ecbdfSas158974 
497c478bd9Sstevel@tonic-gate char	fname[20];
50231ecbdfSas158974 static int hz;
51231ecbdfSas158974 static int nsec_per_tick;
527c478bd9Sstevel@tonic-gate 
53231ecbdfSas158974 void printt(char *, hrtime_t);
54d2117003Sdp void hmstime(char[]);
55*24292ef7Sesaxe void usage();
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;
64231ecbdfSas158974 	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 
77231ecbdfSas158974 	aopt[0] = '\0';
789ba7e32fSdduvall 
79231ecbdfSas158974 	hz = sysconf(_SC_CLK_TCK);
80231ecbdfSas158974 	nsec_per_tick = NANOSEC / hz;
81231ecbdfSas158974 
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 
96*24292ef7Sesaxe 		case '?':  usage();
977c478bd9Sstevel@tonic-gate 				break;
987c478bd9Sstevel@tonic-gate 		}
99*24292ef7Sesaxe 	if (optind >= argc) {
100*24292ef7Sesaxe 		fprintf(stderr, "timex: Missing command\n");
101*24292ef7Sesaxe 		usage();
102*24292ef7Sesaxe 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	/*
1057c478bd9Sstevel@tonic-gate 	 * Check to see if accounting is installed and print a somewhat
1067c478bd9Sstevel@tonic-gate 	 * meaninful message if not.
1077c478bd9Sstevel@tonic-gate 	 */
1087c478bd9Sstevel@tonic-gate 	if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
1097c478bd9Sstevel@tonic-gate 		oflg = 0;
1107c478bd9Sstevel@tonic-gate 		pflg = 0;
1117c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1127c478bd9Sstevel@tonic-gate 		    "Information from -p and -o options not available\n");
1137c478bd9Sstevel@tonic-gate 		fprintf(stderr,
1147c478bd9Sstevel@tonic-gate 		    " because process accounting is not operational.\n");
1157c478bd9Sstevel@tonic-gate 	}
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (sflg) {
1187c478bd9Sstevel@tonic-gate 		sprintf(fname, "/tmp/tmx%ld", getpid());
1197c478bd9Sstevel@tonic-gate 		sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
1207c478bd9Sstevel@tonic-gate 		system(cmd);
1217c478bd9Sstevel@tonic-gate 	}
1227c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(stime);
123231ecbdfSas158974 	before = gethrtime();
124231ecbdfSas158974 	(void) times(&obuffer);
125231ecbdfSas158974 	if ((p = fork()) == (pid_t)-1) {
126231ecbdfSas158974 		perror("Fork Failed");
127231ecbdfSas158974 		(void) unlink(fname);
128231ecbdfSas158974 		exit(EXIT_FAILURE);
129231ecbdfSas158974 	}
1307c478bd9Sstevel@tonic-gate 	if (p == 0) {
1317c478bd9Sstevel@tonic-gate 		setgid(getgid());
1327c478bd9Sstevel@tonic-gate 		execvp(*(argv+optind), (argv+optind));
133d2117003Sdp 		fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
134231ecbdfSas158974 		exit(EXIT_FAILURE);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
1377c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_IGN);
138d2117003Sdp 	while (wait(&status) != p)
139d2117003Sdp 		;
1407c478bd9Sstevel@tonic-gate 	if ((status&0377) != 0)
1417c478bd9Sstevel@tonic-gate 		fprintf(stderr, "Command terminated abnormally.\n");
1427c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_DFL);
1437c478bd9Sstevel@tonic-gate 	signal(SIGQUIT, SIG_DFL);
144231ecbdfSas158974 	(void) times(&buffer);
145231ecbdfSas158974 	after = gethrtime();
146231ecbdfSas158974 	timediff = after - before;
1477c478bd9Sstevel@tonic-gate 	if (pflg + oflg) hmstime(etime);
1487c478bd9Sstevel@tonic-gate 	if (sflg) system(cmd);
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
151231ecbdfSas158974 	printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
1529ba7e32fSdduvall 	printt("user", buffer.tms_cutime - obuffer.tms_cutime);
1539ba7e32fSdduvall 	printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
1547c478bd9Sstevel@tonic-gate 	fprintf(stderr, "\n");
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	if (oflg+pflg) {
1577c478bd9Sstevel@tonic-gate 		if (isatty(0))
1587c478bd9Sstevel@tonic-gate 			sprintf(ttyid, "-l %s", ttyname(0)+5);
1597c478bd9Sstevel@tonic-gate 		sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
1607c478bd9Sstevel@tonic-gate 		    stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
1617c478bd9Sstevel@tonic-gate 		pipin = popen(cmd, "r");
1627c478bd9Sstevel@tonic-gate 		while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
1637c478bd9Sstevel@tonic-gate 			if (pflg)
1647c478bd9Sstevel@tonic-gate 				fprintf(stderr, "%s\n", line);
1657c478bd9Sstevel@tonic-gate 			if (oflg)  {
1667c478bd9Sstevel@tonic-gate 				nfld = sscanf(line,
1677c478bd9Sstevel@tonic-gate 				    "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
1687c478bd9Sstevel@tonic-gate 				    fld[0], fld[1], fld[2], fld[3], fld[4],
1697c478bd9Sstevel@tonic-gate 				    fld[5], fld[6], fld[7], fld[8], fld[9],
1707c478bd9Sstevel@tonic-gate 				    fld[10], fld[11], fld[12], fld[13], fld[14],
171d2117003Sdp 				    fld[15], fld[16], fld[17], fld[18],
172d2117003Sdp 				    fld[19]);
1737c478bd9Sstevel@tonic-gate 				if (++iline == 3)
1747c478bd9Sstevel@tonic-gate 					for (i = 0; i < nfld; i++)  {
1757c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "CHARS")
1767c478bd9Sstevel@tonic-gate 						    == 0)
1777c478bd9Sstevel@tonic-gate 							ichar = i+2;
1787c478bd9Sstevel@tonic-gate 						if (strcmp(fld[i], "BLOCKS")
1797c478bd9Sstevel@tonic-gate 						    == 0)
1807c478bd9Sstevel@tonic-gate 							iblok = i+2;
1817c478bd9Sstevel@tonic-gate 					}
1827c478bd9Sstevel@tonic-gate 				if (iline > 4)  {
1837c478bd9Sstevel@tonic-gate 					chars += atol(fld[ichar]);
1847c478bd9Sstevel@tonic-gate 					bloks += atol(fld[iblok]);
1857c478bd9Sstevel@tonic-gate 				}
1867c478bd9Sstevel@tonic-gate 			}
1877c478bd9Sstevel@tonic-gate 		}
1887c478bd9Sstevel@tonic-gate 		pclose(pipin);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 		if (oflg)
1917c478bd9Sstevel@tonic-gate 			if (iline > 4)
1927c478bd9Sstevel@tonic-gate 				fprintf(stderr,
193d2117003Sdp 				    "\nCHARS TRNSFD = %ld\n"
194d2117003Sdp 				    "BLOCKS READ  = %ld\n", chars, bloks);
1957c478bd9Sstevel@tonic-gate 			else
1967c478bd9Sstevel@tonic-gate 				fprintf(stderr,
1977c478bd9Sstevel@tonic-gate 				    "\nNo process records found!\n");
1987c478bd9Sstevel@tonic-gate 	}
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	if (sflg)  {
201d2117003Sdp 		sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
2027c478bd9Sstevel@tonic-gate 		system(cmd);
2037c478bd9Sstevel@tonic-gate 		unlink(fname);
2047c478bd9Sstevel@tonic-gate 	}
205231ecbdfSas158974 	exit(WEXITSTATUS(status));
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
208d2117003Sdp void
209231ecbdfSas158974 printt(char *label, hrtime_t ticks)
2109ba7e32fSdduvall {
211231ecbdfSas158974 	long tk;	/* number of leftover ticks   */
212231ecbdfSas158974 	long ss;	/* number of seconds */
213231ecbdfSas158974 	long mm;	/* number of minutes */
214231ecbdfSas158974 	long hh;	/* number of hours   */
215231ecbdfSas158974 	longlong_t total = ticks;
2167c478bd9Sstevel@tonic-gate 
217231ecbdfSas158974 	tk	= total % hz;	/* ticks % hz		*/
218231ecbdfSas158974 	total	/= hz;
219231ecbdfSas158974 	ss	= total % 60;	/* ticks / hz % 60	*/
220231ecbdfSas158974 	total	/= 60;
221231ecbdfSas158974 	mm	= total % 60;	/* ticks / hz / 60 % 60 */
222231ecbdfSas158974 	hh	= total / 60;	/* ticks / hz / 60 / 60 */
223231ecbdfSas158974 
224231ecbdfSas158974 	(void) fprintf(stderr, "%s ", label);
225231ecbdfSas158974 
226231ecbdfSas158974 	/* Display either padding or the elapsed hours */
227231ecbdfSas158974 	if (hh == 0L) {
228231ecbdfSas158974 		(void) fprintf(stderr, "%6c", ' ');
229231ecbdfSas158974 	} else {
230231ecbdfSas158974 		(void) fprintf(stderr, "%5ld:", hh);
2317c478bd9Sstevel@tonic-gate 	}
232231ecbdfSas158974 
233231ecbdfSas158974 	/*
234231ecbdfSas158974 	 * Display either nothing or the elapsed minutes, zero
235231ecbdfSas158974 	 * padding (if hours > 0) or space padding (if not).
236231ecbdfSas158974 	 */
237231ecbdfSas158974 	if (mm == 0L && hh == 0L) {
238231ecbdfSas158974 		(void) fprintf(stderr, "%3c", ' ');
239231ecbdfSas158974 	} else if (mm != 0L && hh == 0L) {
240231ecbdfSas158974 		(void) fprintf(stderr, "%2ld:", mm);
241231ecbdfSas158974 	} else {
242231ecbdfSas158974 		(void) fprintf(stderr, "%02ld:", mm);
2437c478bd9Sstevel@tonic-gate 	}
244231ecbdfSas158974 
245231ecbdfSas158974 	/*
246231ecbdfSas158974 	 * Display the elapsed seconds; seconds are always
247231ecbdfSas158974 	 * zero padded.
248231ecbdfSas158974 	 */
249231ecbdfSas158974 	if (hh == 0L && mm == 0L) {
250231ecbdfSas158974 		(void) fprintf(stderr, "%2ld.", ss);
251231ecbdfSas158974 	} else {
252231ecbdfSas158974 		(void) fprintf(stderr, "%02ld.", ss);
253231ecbdfSas158974 	}
254231ecbdfSas158974 
255231ecbdfSas158974 	/* Display hundredths of a second. */
256231ecbdfSas158974 	(void) fprintf(stderr, "%02ld\n", tk * 100/hz);
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*
260d2117003Sdp  * hmstime() sets current time in hh:mm:ss string format in stime;
2617c478bd9Sstevel@tonic-gate  */
2627c478bd9Sstevel@tonic-gate 
263d2117003Sdp void
264d2117003Sdp hmstime(char stime[])
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	char	*ltime;
2677c478bd9Sstevel@tonic-gate 	time_t tme;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	tme = time((time_t *)0);
2707c478bd9Sstevel@tonic-gate 	ltime = ctime(&tme);
2717c478bd9Sstevel@tonic-gate 	strncpy(stime, ltime+11, 8);
2727c478bd9Sstevel@tonic-gate 	stime[8] = '\0';
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
275d2117003Sdp void
276*24292ef7Sesaxe usage()
2777c478bd9Sstevel@tonic-gate {
278*24292ef7Sesaxe 	fprintf(stderr, "Usage: timex [-o] [-p [-fhkmrt]] [-s] command\n");
2797c478bd9Sstevel@tonic-gate 	unlink(fname);
280231ecbdfSas158974 	exit(EXIT_FAILURE);
2817c478bd9Sstevel@tonic-gate }
282