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