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