xref: /titanic_51/usr/src/cmd/sa/timex.c (revision 2a9459bdd821c1cf59590a7a9069ac9c591e8a6b)
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 2007 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 
49 char	fname[20];
50 static int hz;
51 static int nsec_per_tick;
52 
53 void printt(char *, hrtime_t);
54 void hmstime(char[]);
55 void diag(char *);
56 
57 int
58 main(int argc, char **argv)
59 {
60 	struct	tms buffer, obuffer;
61 	int	status;
62 	register pid_t	p;
63 	int	c;
64 	hrtime_t before, after, timediff;
65 	char	stime[9], etime[9];
66 	char	cmd[80];
67 	int	pflg = 0, sflg = 0, oflg = 0;
68 	char	aopt[25];
69 	FILE	*pipin;
70 	char	ttyid[12], line[150];
71 	char	eol;
72 	char	fld[20][12];
73 	int	iline = 0, i, nfld;
74 	int	ichar, iblok;
75 	long	chars = 0, bloks = 0;
76 
77 	aopt[0] = '\0';
78 
79 	hz = sysconf(_SC_CLK_TCK);
80 	nsec_per_tick = NANOSEC / hz;
81 
82 	/* check options; */
83 	while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
84 		switch (c)  {
85 		case 's':  sflg++;  break;
86 		case 'o':  oflg++;  break;
87 		case 'p':  pflg++;  break;
88 
89 		case 'f':  strcat(aopt, "-f ");  break;
90 		case 'h':  strcat(aopt, "-h ");  break;
91 		case 'k':  strcat(aopt, "-k ");  break;
92 		case 'm':  strcat(aopt, "-m ");  break;
93 		case 'r':  strcat(aopt, "-r ");  break;
94 		case 't':  strcat(aopt, "-t ");  break;
95 
96 		case '?':  diag("Usage: timex [-s][-o][-p[-fhkmrt]] cmd");
97 				break;
98 		}
99 	if (optind >= argc)	diag("Missing command");
100 
101 	/*
102 	 * Check to see if accounting is installed and print a somewhat
103 	 * meaninful message if not.
104 	 */
105 	if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
106 		oflg = 0;
107 		pflg = 0;
108 		fprintf(stderr,
109 		    "Information from -p and -o options not available\n");
110 		fprintf(stderr,
111 		    " because process accounting is not operational.\n");
112 	}
113 
114 	if (sflg) {
115 		sprintf(fname, "/tmp/tmx%ld", getpid());
116 		sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
117 		system(cmd);
118 	}
119 	if (pflg + oflg) hmstime(stime);
120 	before = gethrtime();
121 	(void) times(&obuffer);
122 	if ((p = fork()) == (pid_t)-1) {
123 		perror("Fork Failed");
124 		(void) unlink(fname);
125 		exit(EXIT_FAILURE);
126 	}
127 	if (p == 0) {
128 		setgid(getgid());
129 		execvp(*(argv+optind), (argv+optind));
130 		fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
131 		exit(EXIT_FAILURE);
132 	}
133 	signal(SIGINT, SIG_IGN);
134 	signal(SIGQUIT, SIG_IGN);
135 	while (wait(&status) != p)
136 		;
137 	if ((status&0377) != 0)
138 		fprintf(stderr, "Command terminated abnormally.\n");
139 	signal(SIGINT, SIG_DFL);
140 	signal(SIGQUIT, SIG_DFL);
141 	(void) times(&buffer);
142 	after = gethrtime();
143 	timediff = after - before;
144 	if (pflg + oflg) hmstime(etime);
145 	if (sflg) system(cmd);
146 
147 	fprintf(stderr, "\n");
148 	printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
149 	printt("user", buffer.tms_cutime - obuffer.tms_cutime);
150 	printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
151 	fprintf(stderr, "\n");
152 
153 	if (oflg+pflg) {
154 		if (isatty(0))
155 			sprintf(ttyid, "-l %s", ttyname(0)+5);
156 		sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
157 		    stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
158 		pipin = popen(cmd, "r");
159 		while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
160 			if (pflg)
161 				fprintf(stderr, "%s\n", line);
162 			if (oflg)  {
163 				nfld = sscanf(line,
164 				    "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
165 				    fld[0], fld[1], fld[2], fld[3], fld[4],
166 				    fld[5], fld[6], fld[7], fld[8], fld[9],
167 				    fld[10], fld[11], fld[12], fld[13], fld[14],
168 				    fld[15], fld[16], fld[17], fld[18],
169 				    fld[19]);
170 				if (++iline == 3)
171 					for (i = 0; i < nfld; i++)  {
172 						if (strcmp(fld[i], "CHARS")
173 						    == 0)
174 							ichar = i+2;
175 						if (strcmp(fld[i], "BLOCKS")
176 						    == 0)
177 							iblok = i+2;
178 					}
179 				if (iline > 4)  {
180 					chars += atol(fld[ichar]);
181 					bloks += atol(fld[iblok]);
182 				}
183 			}
184 		}
185 		pclose(pipin);
186 
187 		if (oflg)
188 			if (iline > 4)
189 				fprintf(stderr,
190 				    "\nCHARS TRNSFD = %ld\n"
191 				    "BLOCKS READ  = %ld\n", chars, bloks);
192 			else
193 				fprintf(stderr,
194 				    "\nNo process records found!\n");
195 	}
196 
197 	if (sflg)  {
198 		sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
199 		system(cmd);
200 		unlink(fname);
201 	}
202 	exit(WEXITSTATUS(status));
203 }
204 
205 void
206 printt(char *label, hrtime_t ticks)
207 {
208 	long tk;	/* number of leftover ticks   */
209 	long ss;	/* number of seconds */
210 	long mm;	/* number of minutes */
211 	long hh;	/* number of hours   */
212 	longlong_t total = ticks;
213 
214 	tk	= total % hz;	/* ticks % hz		*/
215 	total	/= hz;
216 	ss	= total % 60;	/* ticks / hz % 60	*/
217 	total	/= 60;
218 	mm	= total % 60;	/* ticks / hz / 60 % 60 */
219 	hh	= total / 60;	/* ticks / hz / 60 / 60 */
220 
221 	(void) fprintf(stderr, "%s ", label);
222 
223 	/* Display either padding or the elapsed hours */
224 	if (hh == 0L) {
225 		(void) fprintf(stderr, "%6c", ' ');
226 	} else {
227 		(void) fprintf(stderr, "%5ld:", hh);
228 	}
229 
230 	/*
231 	 * Display either nothing or the elapsed minutes, zero
232 	 * padding (if hours > 0) or space padding (if not).
233 	 */
234 	if (mm == 0L && hh == 0L) {
235 		(void) fprintf(stderr, "%3c", ' ');
236 	} else if (mm != 0L && hh == 0L) {
237 		(void) fprintf(stderr, "%2ld:", mm);
238 	} else {
239 		(void) fprintf(stderr, "%02ld:", mm);
240 	}
241 
242 	/*
243 	 * Display the elapsed seconds; seconds are always
244 	 * zero padded.
245 	 */
246 	if (hh == 0L && mm == 0L) {
247 		(void) fprintf(stderr, "%2ld.", ss);
248 	} else {
249 		(void) fprintf(stderr, "%02ld.", ss);
250 	}
251 
252 	/* Display hundredths of a second. */
253 	(void) fprintf(stderr, "%02ld\n", tk * 100/hz);
254 }
255 
256 /*
257  * hmstime() sets current time in hh:mm:ss string format in stime;
258  */
259 
260 void
261 hmstime(char stime[])
262 {
263 	char	*ltime;
264 	time_t tme;
265 
266 	tme = time((time_t *)0);
267 	ltime = ctime(&tme);
268 	strncpy(stime, ltime+11, 8);
269 	stime[8] = '\0';
270 }
271 
272 void
273 diag(char *s)
274 {
275 	fprintf(stderr, "%s\n", s);
276 	unlink(fname);
277 	exit(EXIT_FAILURE);
278 }
279