xref: /titanic_44/usr/src/cmd/sa/timex.c (revision b7f45089ccbe01bab3d7c7377b49d80d2ae18a69)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"       /* SVr4.0 1.19 */
27 /*	timex.c 1.19 of 7/21/89 	*/
28 #include <sys/types.h>
29 #include <sys/times.h>
30 #include <sys/param.h>
31 #include <stdio.h>
32 #include <signal.h>
33 #include <time.h>
34 #include <errno.h>
35 #include <pwd.h>
36 
37 long	atol();
38 extern	time_t time();
39 int	isatty();
40 char	*ttyname();
41 char	*strcat();
42 char	*strncpy();
43 struct	passwd *getpwuid();
44 
45 extern int errno;
46 extern char *sys_errlist[];
47 char	fname[20];
48 
49 /*
50  * Quant[0] will get set to HZ/10 later.
51  */
52 char quant[] = { 10, 10, 10, 6, 10, 6, 10, 10, 10 };
53 
54 main(argc, argv)
55 int argc;
56 char **argv;
57 {
58 	struct	tms buffer, obuffer;
59 	int	status;
60 	register pid_t	p;
61 	int	c;
62 	time_t	before, after;
63 	char	stime[9], etime[9];
64 	char	cmd[80];
65 	extern	char	*optarg;
66 	extern	int	optind;
67 	int	pflg = 0, sflg = 0, oflg = 0;
68 	char	aopt[25];
69 	FILE	*popen(), *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), sys_errlist[errno]);
125 		exit(1);
126 	}
127 	signal(SIGINT, SIG_IGN);
128 	signal(SIGQUIT, SIG_IGN);
129 	while (wait(&status) != p);
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], fld[19]);
160 				if (++iline == 3)
161 					for (i = 0; i < nfld; i++)  {
162 						if (strcmp(fld[i], "CHARS")
163 						    == 0)
164 							ichar = i+2;
165 						if (strcmp(fld[i], "BLOCKS")
166 						    == 0)
167 							iblok = i+2;
168 					}
169 				if (iline > 4)  {
170 					chars += atol(fld[ichar]);
171 					bloks += atol(fld[iblok]);
172 				}
173 			}
174 		}
175 		pclose(pipin);
176 
177 		if (oflg)
178 			if (iline > 4)
179 				fprintf(stderr,
180 				"\nCHARS TRNSFD = %ld\nBLOCKS READ  = %ld\n",
181 				chars, bloks);
182 			else
183 				fprintf(stderr,
184 					"\nNo process records found!\n");
185 	}
186 
187 	if (sflg)  {
188 		sprintf(cmd,
189 			"/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 printt(s, a)
201 char	*s;
202 time_t	a;
203 {
204 	char	digit[9];
205 	register	i;
206 	char	c;
207 	int	nonzero;
208 
209 	for (i = 0; i < 9; 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 hmstime(stime)
233 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 diag(s)
245 char *s;
246 {
247 	fprintf(stderr, "%s\n", s);
248 	unlink(fname);
249 	exit(1);
250 }
251