xref: /titanic_50/usr/src/cmd/sa/timex.c (revision 657b1f3d64bcf8eaa2385dba72a6047f089433b2)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /*	All Rights Reserved	*/
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/types.h>
32 #include <sys/times.h>
33 #include <sys/time.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 #define	NSEC_TO_TICK(nsec)	((nsec) / nsec_per_tick)
46 #define	NSEC_TO_TICK_ROUNDUP(nsec) NSEC_TO_TICK((nsec) + \
47 	nsec_per_tick/2)
48 #define	NANOSEC	1000000000
49 
50 char	fname[20];
51 static int hz;
52 int nsec_per_tick;
53 
54 void printt(char *, hrtime_t);
55 void hmstime(char[]);
56 void diag(char *);
57 
58 int
59 main(int argc, char **argv)
60 {
61 	struct	tms buffer, obuffer;
62 	int	status;
63 	register pid_t	p;
64 	int	c;
65 	hrtime_t before, after, timediff;
66 	char	stime[9], etime[9];
67 	char	cmd[80];
68 	int	pflg = 0, sflg = 0, oflg = 0;
69 	char	aopt[25];
70 	FILE	*pipin;
71 	char	ttyid[12], line[150];
72 	char	eol;
73 	char	fld[20][12];
74 	int	iline = 0, i, nfld;
75 	int	ichar, iblok;
76 	long	chars = 0, bloks = 0;
77 
78 	aopt[0] = '\0';			/* terminate the string #1245107 */
79 
80 	hz = sysconf(_SC_CLK_TCK);
81 	nsec_per_tick = NANOSEC / hz;
82 
83 	/* check options; */
84 	while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
85 		switch (c)  {
86 		case 's':  sflg++;  break;
87 		case 'o':  oflg++;  break;
88 		case 'p':  pflg++;  break;
89 
90 		case 'f':  strcat(aopt, "-f ");  break;
91 		case 'h':  strcat(aopt, "-h ");  break;
92 		case 'k':  strcat(aopt, "-k ");  break;
93 		case 'm':  strcat(aopt, "-m ");  break;
94 		case 'r':  strcat(aopt, "-r ");  break;
95 		case 't':  strcat(aopt, "-t ");  break;
96 
97 		case '?':  diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd");
98 				break;
99 		}
100 	if (optind >= argc)	diag("Missing command");
101 
102 	/*
103 	 * Check to see if accounting is installed and print a somewhat
104 	 * meaninful message if not.
105 	 */
106 	if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
107 		oflg = 0;
108 		pflg = 0;
109 		fprintf(stderr,
110 		    "Information from -p and -o options not available\n");
111 		fprintf(stderr,
112 		    " because process accounting is not operational.\n");
113 	}
114 
115 	if (sflg) {
116 		sprintf(fname, "/tmp/tmx%ld", getpid());
117 		sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
118 		system(cmd);
119 	}
120 	if (pflg + oflg) hmstime(stime);
121 	before = gethrtime();
122 	(void) times(&obuffer);
123 	if ((p = fork()) == (pid_t)-1) diag("Try again.\n");
124 	if (p == 0) {
125 		setgid(getgid());
126 		execvp(*(argv+optind), (argv+optind));
127 		fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
128 		exit(1);
129 	}
130 	signal(SIGINT, SIG_IGN);
131 	signal(SIGQUIT, SIG_IGN);
132 	while (wait(&status) != p)
133 		;
134 	if ((status&0377) != 0)
135 		fprintf(stderr, "Command terminated abnormally.\n");
136 	signal(SIGINT, SIG_DFL);
137 	signal(SIGQUIT, SIG_DFL);
138 	(void) times(&buffer);
139 	after = gethrtime();
140 	timediff = after - before;
141 	if (pflg + oflg) hmstime(etime);
142 	if (sflg) system(cmd);
143 
144 	fprintf(stderr, "\n");
145 	printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
146 	printt("user", (hrtime_t)buffer.tms_cutime - (hrtime_t)
147 	    obuffer.tms_cutime);
148 	printt("sys ", (hrtime_t)buffer.tms_cstime - (hrtime_t)
149 	    obuffer.tms_cstime);
150 	fprintf(stderr, "\n");
151 
152 	if (oflg+pflg) {
153 		if (isatty(0))
154 			sprintf(ttyid, "-l %s", ttyname(0)+5);
155 		sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
156 		    stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
157 		pipin = popen(cmd, "r");
158 		while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
159 			if (pflg)
160 				fprintf(stderr, "%s\n", line);
161 			if (oflg)  {
162 				nfld = sscanf(line,
163 				    "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
164 				    fld[0], fld[1], fld[2], fld[3], fld[4],
165 				    fld[5], fld[6], fld[7], fld[8], fld[9],
166 				    fld[10], fld[11], fld[12], fld[13], fld[14],
167 				    fld[15], fld[16], fld[17], fld[18],
168 				    fld[19]);
169 				if (++iline == 3)
170 					for (i = 0; i < nfld; i++)  {
171 						if (strcmp(fld[i], "CHARS")
172 						    == 0)
173 							ichar = i+2;
174 						if (strcmp(fld[i], "BLOCKS")
175 						    == 0)
176 							iblok = i+2;
177 					}
178 				if (iline > 4)  {
179 					chars += atol(fld[ichar]);
180 					bloks += atol(fld[iblok]);
181 				}
182 			}
183 		}
184 		pclose(pipin);
185 
186 		if (oflg)
187 			if (iline > 4)
188 				fprintf(stderr,
189 				    "\nCHARS TRNSFD = %ld\n"
190 				    "BLOCKS READ  = %ld\n", chars, bloks);
191 			else
192 				fprintf(stderr,
193 				    "\nNo process records found!\n");
194 	}
195 
196 	if (sflg)  {
197 		sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
198 		system(cmd);
199 		unlink(fname);
200 	}
201 	exit(status>>8);
202 }
203 
204 void
205 printt(char *label, hrtime_t ticks) {
206 	long tk;		/* number of ticks   */
207 	long ss;		/* number of seconds */
208 	long mm;		/* number of minutes */
209 	long hh;		/* number of hours   */
210 	longlong_t total = ticks;
211 
212 	tk	= total % HZ;	/* ticks % HZ		*/
213 	total /= HZ;
214 	ss	= total % 60;	/* ticks / HZ % 60	*/
215 	total /= 60;
216 	mm	= total % 60;	/* ticks / HZ / 60 % 60 */
217 	hh	= total / 60;	/* ticks / HZ / 60 / 60 */
218 
219 	fprintf(stderr, "%s", label);
220 
221 	/*
222 	 * A negative sign indicates either time travelling backward
223 	 * or an overflow in time travelling forward.
224 	 * A positive sign indicates either time travelling forward
225 	 * or an overflow in time travelling backward.
226 	 */
227 	if (ticks < 0) {
228 		fprintf(stderr, "%1c", '-');
229 	} else {
230 		fprintf(stderr, "%1c", ' ');
231 	}
232 
233 	/*
234 	 * We display either nothing or the absolute value of the
235 	 * number of calculated hours.
236 	 */
237 	if (hh == 0) {
238 		fprintf(stderr, "%7c",   ' ');
239 	} else {
240 		fprintf(stderr, "%7ld:", (hh > 0) ? hh : hh * -1);
241 	}
242 
243 	/*
244 	 * We display either nothing or the absolute value of the
245 	 * number of calculated minutes.  If the value is a single-
246 	 * digit value, we would pad a '0' before it.
247 	 */
248 	if (mm == 0) {
249 		if (hh == 0) {
250 			fprintf(stderr, "%1c", ' ');
251 		} else {
252 			fprintf(stderr, "0:");
253 		}
254 	} else if (mm > -10 && mm < 10) {
255 			fprintf(stderr, "0%ld:", (mm > 0) ? mm : mm * -1);
256 	} else {
257 			fprintf(stderr, "%2ld:", (mm > 0) ? mm : mm * -1);
258 	}
259 
260 	/*
261 	 * We display the absolute value of the number of
262 	 * calculated seconds.  If the value is a single-
263 	 * digit value, we would pad a '0' before it.
264 	 */
265 	if (ss > -10 && ss < 10) {
266 		fprintf(stderr, "%ld.0", (ss > 0) ? ss : ss * -1);
267 	} else {
268 		fprintf(stderr, "%2ld.", (ss > 0) ? ss : ss * -1);
269 	}
270 
271 	/*
272 	 * We display the absolute value of the number of calculated ticks.
273 	 */
274 	fprintf(stderr, "%ld", (tk > 0) ? tk : tk * -1);
275 
276 	fprintf(stderr, "\n");
277 }
278 
279 /*
280  * hmstime() sets current time in hh:mm:ss string format in stime;
281  */
282 
283 void
284 hmstime(char stime[])
285 {
286 	char	*ltime;
287 	time_t tme;
288 
289 	tme = time((time_t *)0);
290 	ltime = ctime(&tme);
291 	strncpy(stime, ltime+11, 8);
292 	stime[8] = '\0';
293 }
294 
295 void
296 diag(char *s)
297 {
298 	fprintf(stderr, "%s\n", s);
299 	unlink(fname);
300 	exit(1);
301 }
302