17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51337fffaSsr * Common Development and Distribution License (the "License").
61337fffaSsr * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21d2117003Sdp /*
22*24292ef7Sesaxe * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23d2117003Sdp * Use is subject to license terms.
24d2117003Sdp */
251337fffaSsr
26231ecbdfSas /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27231ecbdfSas /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/times.h>
31231ecbdfSas #include <sys/time.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
33d2117003Sdp #include <sys/wait.h>
34d2117003Sdp #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
36d2117003Sdp #include <stdlib.h>
377c478bd9Sstevel@tonic-gate #include <signal.h>
38d2117003Sdp #include <strings.h>
397c478bd9Sstevel@tonic-gate #include <time.h>
407c478bd9Sstevel@tonic-gate #include <errno.h>
417c478bd9Sstevel@tonic-gate #include <pwd.h>
427c478bd9Sstevel@tonic-gate
43231ecbdfSas #define NSEC_TO_TICK(nsec) ((nsec) / nsec_per_tick)
44231ecbdfSas #define NSEC_TO_TICK_ROUNDUP(nsec) NSEC_TO_TICK((nsec) + \
45231ecbdfSas nsec_per_tick/2)
467c478bd9Sstevel@tonic-gate
47231ecbdfSas char fname[20];
48231ecbdfSas static int hz;
49231ecbdfSas static int nsec_per_tick;
509ba7e32fSdduvall
51231ecbdfSas void printt(char *, hrtime_t);
52d2117003Sdp void hmstime(char[]);
53*24292ef7Sesaxe void usage();
54d2117003Sdp
55d2117003Sdp int
main(int argc,char ** argv)56d2117003Sdp main(int argc, char **argv)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate struct tms buffer, obuffer;
597c478bd9Sstevel@tonic-gate int status;
607c478bd9Sstevel@tonic-gate register pid_t p;
617c478bd9Sstevel@tonic-gate int c;
62231ecbdfSas hrtime_t before, after, timediff;
637c478bd9Sstevel@tonic-gate char stime[9], etime[9];
647c478bd9Sstevel@tonic-gate char cmd[80];
657c478bd9Sstevel@tonic-gate int pflg = 0, sflg = 0, oflg = 0;
667c478bd9Sstevel@tonic-gate char aopt[25];
67d2117003Sdp FILE *pipin;
687c478bd9Sstevel@tonic-gate char ttyid[12], line[150];
697c478bd9Sstevel@tonic-gate char eol;
707c478bd9Sstevel@tonic-gate char fld[20][12];
717c478bd9Sstevel@tonic-gate int iline = 0, i, nfld;
727c478bd9Sstevel@tonic-gate int ichar, iblok;
737c478bd9Sstevel@tonic-gate long chars = 0, bloks = 0;
747c478bd9Sstevel@tonic-gate
75231ecbdfSas aopt[0] = '\0';
76231ecbdfSas
77231ecbdfSas hz = sysconf(_SC_CLK_TCK);
78231ecbdfSas nsec_per_tick = NANOSEC / hz;
7903f18836Sas
807c478bd9Sstevel@tonic-gate /* check options; */
817c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "sopfhkmrt")) != EOF)
827c478bd9Sstevel@tonic-gate switch (c) {
837c478bd9Sstevel@tonic-gate case 's': sflg++; break;
847c478bd9Sstevel@tonic-gate case 'o': oflg++; break;
857c478bd9Sstevel@tonic-gate case 'p': pflg++; break;
867c478bd9Sstevel@tonic-gate
877c478bd9Sstevel@tonic-gate case 'f': strcat(aopt, "-f "); break;
887c478bd9Sstevel@tonic-gate case 'h': strcat(aopt, "-h "); break;
897c478bd9Sstevel@tonic-gate case 'k': strcat(aopt, "-k "); break;
907c478bd9Sstevel@tonic-gate case 'm': strcat(aopt, "-m "); break;
917c478bd9Sstevel@tonic-gate case 'r': strcat(aopt, "-r "); break;
927c478bd9Sstevel@tonic-gate case 't': strcat(aopt, "-t "); break;
937c478bd9Sstevel@tonic-gate
94*24292ef7Sesaxe case '?': usage();
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate }
97*24292ef7Sesaxe if (optind >= argc) {
98*24292ef7Sesaxe fprintf(stderr, "timex: Missing command\n");
99*24292ef7Sesaxe usage();
100*24292ef7Sesaxe }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * Check to see if accounting is installed and print a somewhat
1047c478bd9Sstevel@tonic-gate * meaninful message if not.
1057c478bd9Sstevel@tonic-gate */
1067c478bd9Sstevel@tonic-gate if (((oflg+pflg) != 0) && (access("/usr/bin/acctcom", 01) == -1)) {
1077c478bd9Sstevel@tonic-gate oflg = 0;
1087c478bd9Sstevel@tonic-gate pflg = 0;
1097c478bd9Sstevel@tonic-gate fprintf(stderr,
110d2117003Sdp "Information from -p and -o options not available\n");
1117c478bd9Sstevel@tonic-gate fprintf(stderr,
112d2117003Sdp " because process accounting is not operational.\n");
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate
1157c478bd9Sstevel@tonic-gate if (sflg) {
1167c478bd9Sstevel@tonic-gate sprintf(fname, "/tmp/tmx%ld", getpid());
1177c478bd9Sstevel@tonic-gate sprintf(cmd, "/usr/lib/sa/sadc 1 1 %s", fname);
1187c478bd9Sstevel@tonic-gate system(cmd);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(stime);
121231ecbdfSas before = gethrtime();
122231ecbdfSas (void) times(&obuffer);
123231ecbdfSas if ((p = fork()) == (pid_t)-1) {
124231ecbdfSas perror("Fork Failed");
125231ecbdfSas (void) unlink(fname);
126231ecbdfSas exit(EXIT_FAILURE);
127231ecbdfSas }
1287c478bd9Sstevel@tonic-gate if (p == 0) {
1297c478bd9Sstevel@tonic-gate setgid(getgid());
1307c478bd9Sstevel@tonic-gate execvp(*(argv+optind), (argv+optind));
131d2117003Sdp fprintf(stderr, "%s: %s\n", *(argv+optind), strerror(errno));
132231ecbdfSas exit(EXIT_FAILURE);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_IGN);
1357c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_IGN);
136d2117003Sdp while (wait(&status) != p)
137d2117003Sdp ;
1387c478bd9Sstevel@tonic-gate if ((status&0377) != 0)
1397c478bd9Sstevel@tonic-gate fprintf(stderr, "Command terminated abnormally.\n");
1407c478bd9Sstevel@tonic-gate signal(SIGINT, SIG_DFL);
1417c478bd9Sstevel@tonic-gate signal(SIGQUIT, SIG_DFL);
142231ecbdfSas (void) times(&buffer);
143231ecbdfSas after = gethrtime();
144231ecbdfSas timediff = after - before;
1457c478bd9Sstevel@tonic-gate if (pflg + oflg) hmstime(etime);
1467c478bd9Sstevel@tonic-gate if (sflg) system(cmd);
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
149231ecbdfSas printt("real", NSEC_TO_TICK_ROUNDUP(timediff));
1509ba7e32fSdduvall printt("user", buffer.tms_cutime - obuffer.tms_cutime);
1519ba7e32fSdduvall printt("sys ", buffer.tms_cstime - obuffer.tms_cstime);
1527c478bd9Sstevel@tonic-gate fprintf(stderr, "\n");
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate if (oflg+pflg) {
1557c478bd9Sstevel@tonic-gate if (isatty(0))
1567c478bd9Sstevel@tonic-gate sprintf(ttyid, "-l %s", ttyname(0)+5);
1577c478bd9Sstevel@tonic-gate sprintf(cmd, "acctcom -S %s -E %s -u %s %s -i %s",
158d2117003Sdp stime, etime, getpwuid(getuid())->pw_name, ttyid, aopt);
1597c478bd9Sstevel@tonic-gate pipin = popen(cmd, "r");
1607c478bd9Sstevel@tonic-gate while (fscanf(pipin, "%[^\n]%1c", line, &eol) > 1) {
1617c478bd9Sstevel@tonic-gate if (pflg)
1627c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", line);
1637c478bd9Sstevel@tonic-gate if (oflg) {
1647c478bd9Sstevel@tonic-gate nfld = sscanf(line,
165d2117003Sdp "%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
166d2117003Sdp fld[0], fld[1], fld[2], fld[3], fld[4],
167d2117003Sdp fld[5], fld[6], fld[7], fld[8], fld[9],
168d2117003Sdp fld[10], fld[11], fld[12], fld[13], fld[14],
169d2117003Sdp fld[15], fld[16], fld[17], fld[18],
170d2117003Sdp fld[19]);
1717c478bd9Sstevel@tonic-gate if (++iline == 3)
1727c478bd9Sstevel@tonic-gate for (i = 0; i < nfld; i++) {
1737c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "CHARS")
1747c478bd9Sstevel@tonic-gate == 0)
1757c478bd9Sstevel@tonic-gate ichar = i+2;
1767c478bd9Sstevel@tonic-gate if (strcmp(fld[i], "BLOCKS")
1777c478bd9Sstevel@tonic-gate == 0)
1787c478bd9Sstevel@tonic-gate iblok = i+2;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate if (iline > 4) {
1817c478bd9Sstevel@tonic-gate chars += atol(fld[ichar]);
1827c478bd9Sstevel@tonic-gate bloks += atol(fld[iblok]);
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate }
1867c478bd9Sstevel@tonic-gate pclose(pipin);
1877c478bd9Sstevel@tonic-gate
1887c478bd9Sstevel@tonic-gate if (oflg)
1897c478bd9Sstevel@tonic-gate if (iline > 4)
1907c478bd9Sstevel@tonic-gate fprintf(stderr,
191d2117003Sdp "\nCHARS TRNSFD = %ld\n"
192d2117003Sdp "BLOCKS READ = %ld\n", chars, bloks);
1937c478bd9Sstevel@tonic-gate else
1947c478bd9Sstevel@tonic-gate fprintf(stderr,
195d2117003Sdp "\nNo process records found!\n");
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate
1987c478bd9Sstevel@tonic-gate if (sflg) {
199d2117003Sdp sprintf(cmd, "/usr/bin/sar -ubdycwaqvmpgrk -f %s 1>&2", fname);
2007c478bd9Sstevel@tonic-gate system(cmd);
2017c478bd9Sstevel@tonic-gate unlink(fname);
2027c478bd9Sstevel@tonic-gate }
203231ecbdfSas exit(WEXITSTATUS(status));
2047c478bd9Sstevel@tonic-gate }
2057c478bd9Sstevel@tonic-gate
2069ba7e32fSdduvall void
printt(char * label,hrtime_t ticks)207231ecbdfSas printt(char *label, hrtime_t ticks)
2089ba7e32fSdduvall {
209231ecbdfSas long tk; /* number of leftover ticks */
210231ecbdfSas long ss; /* number of seconds */
211231ecbdfSas long mm; /* number of minutes */
212231ecbdfSas long hh; /* number of hours */
213231ecbdfSas longlong_t total = ticks;
214231ecbdfSas
215231ecbdfSas tk = total % hz; /* ticks % hz */
216231ecbdfSas total /= hz;
217231ecbdfSas ss = total % 60; /* ticks / hz % 60 */
218231ecbdfSas total /= 60;
219231ecbdfSas mm = total % 60; /* ticks / hz / 60 % 60 */
220231ecbdfSas hh = total / 60; /* ticks / hz / 60 / 60 */
221231ecbdfSas
222231ecbdfSas (void) fprintf(stderr, "%s ", label);
223231ecbdfSas
224231ecbdfSas /* Display either padding or the elapsed hours */
225231ecbdfSas if (hh == 0L) {
226231ecbdfSas (void) fprintf(stderr, "%6c", ' ');
227231ecbdfSas } else {
228231ecbdfSas (void) fprintf(stderr, "%5ld:", hh);
2297c478bd9Sstevel@tonic-gate }
230231ecbdfSas
231231ecbdfSas /*
232231ecbdfSas * Display either nothing or the elapsed minutes, zero
233231ecbdfSas * padding (if hours > 0) or space padding (if not).
234231ecbdfSas */
235231ecbdfSas if (mm == 0L && hh == 0L) {
236231ecbdfSas (void) fprintf(stderr, "%3c", ' ');
237231ecbdfSas } else if (mm != 0L && hh == 0L) {
238231ecbdfSas (void) fprintf(stderr, "%2ld:", mm);
239231ecbdfSas } else {
240231ecbdfSas (void) fprintf(stderr, "%02ld:", mm);
24103f18836Sas }
242231ecbdfSas
243231ecbdfSas /*
244231ecbdfSas * Display the elapsed seconds; seconds are always
245231ecbdfSas * zero padded.
246231ecbdfSas */
247231ecbdfSas if (hh == 0L && mm == 0L) {
248231ecbdfSas (void) fprintf(stderr, "%2ld.", ss);
249231ecbdfSas } else {
250231ecbdfSas (void) fprintf(stderr, "%02ld.", ss);
251231ecbdfSas }
252231ecbdfSas
253231ecbdfSas /* Display hundredths of a second. */
254231ecbdfSas (void) fprintf(stderr, "%02ld\n", tk * 100/hz);
2557c478bd9Sstevel@tonic-gate }
2567c478bd9Sstevel@tonic-gate
2577c478bd9Sstevel@tonic-gate /*
258d2117003Sdp * hmstime() sets current time in hh:mm:ss string format in stime;
259d2117003Sdp */
2607c478bd9Sstevel@tonic-gate
261d2117003Sdp void
hmstime(char stime[])262d2117003Sdp hmstime(char stime[])
2637c478bd9Sstevel@tonic-gate {
2647c478bd9Sstevel@tonic-gate char *ltime;
2657c478bd9Sstevel@tonic-gate time_t tme;
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate tme = time((time_t *)0);
2687c478bd9Sstevel@tonic-gate ltime = ctime(&tme);
2697c478bd9Sstevel@tonic-gate strncpy(stime, ltime+11, 8);
2707c478bd9Sstevel@tonic-gate stime[8] = '\0';
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
273d2117003Sdp void
usage()274*24292ef7Sesaxe usage()
2757c478bd9Sstevel@tonic-gate {
276*24292ef7Sesaxe fprintf(stderr, "Usage: timex [-o] [-p [-fhkmrt]] [-s] command\n");
2777c478bd9Sstevel@tonic-gate unlink(fname);
278231ecbdfSas exit(EXIT_FAILURE);
2797c478bd9Sstevel@tonic-gate }
280