10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*8131SRafael.Vanoni@Sun.COM * Common Development and Distribution License (the "License").
6*8131SRafael.Vanoni@Sun.COM * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
210Sstevel@tonic-gate /*
22*8131SRafael.Vanoni@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
230Sstevel@tonic-gate * Use is subject to license terms.
24*8131SRafael.Vanoni@Sun.COM *
25*8131SRafael.Vanoni@Sun.COM * Portions Copyright 2008 Chad Mynhier
260Sstevel@tonic-gate */
270Sstevel@tonic-gate
280Sstevel@tonic-gate #include <stdio.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <unistd.h>
310Sstevel@tonic-gate #include <fcntl.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <errno.h>
340Sstevel@tonic-gate #include <math.h>
350Sstevel@tonic-gate #include <wait.h>
360Sstevel@tonic-gate #include <signal.h>
370Sstevel@tonic-gate #include <sys/types.h>
380Sstevel@tonic-gate #include <sys/time.h>
390Sstevel@tonic-gate #include <signal.h>
400Sstevel@tonic-gate #include <libproc.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate static int look(pid_t);
430Sstevel@tonic-gate static void hr_min_sec(char *, long);
440Sstevel@tonic-gate static void prtime(char *, timestruc_t *);
450Sstevel@tonic-gate static int perr(const char *);
460Sstevel@tonic-gate
470Sstevel@tonic-gate static void tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b);
480Sstevel@tonic-gate static void tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b);
49*8131SRafael.Vanoni@Sun.COM static void hrt2ts(hrtime_t hrt, timestruc_t *tsp);
500Sstevel@tonic-gate
510Sstevel@tonic-gate static char *command;
52*8131SRafael.Vanoni@Sun.COM static char *pidarg;
530Sstevel@tonic-gate static char procname[64];
540Sstevel@tonic-gate
55*8131SRafael.Vanoni@Sun.COM static int Fflag;
56*8131SRafael.Vanoni@Sun.COM static int mflag;
57*8131SRafael.Vanoni@Sun.COM static int errflg;
58*8131SRafael.Vanoni@Sun.COM
590Sstevel@tonic-gate int
main(int argc,char ** argv)600Sstevel@tonic-gate main(int argc, char **argv)
610Sstevel@tonic-gate {
62*8131SRafael.Vanoni@Sun.COM int opt;
630Sstevel@tonic-gate pid_t pid;
640Sstevel@tonic-gate struct siginfo info;
650Sstevel@tonic-gate int status;
66*8131SRafael.Vanoni@Sun.COM int gret;
67*8131SRafael.Vanoni@Sun.COM struct ps_prochandle *Pr;
680Sstevel@tonic-gate
690Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
700Sstevel@tonic-gate command++;
710Sstevel@tonic-gate else
720Sstevel@tonic-gate command = argv[0];
730Sstevel@tonic-gate
74*8131SRafael.Vanoni@Sun.COM while ((opt = getopt(argc, argv, "Fhmp:")) != EOF) {
75*8131SRafael.Vanoni@Sun.COM switch (opt) {
76*8131SRafael.Vanoni@Sun.COM case 'F': /* force grabbing (no O_EXCL) */
77*8131SRafael.Vanoni@Sun.COM Fflag = PGRAB_FORCE;
78*8131SRafael.Vanoni@Sun.COM break;
79*8131SRafael.Vanoni@Sun.COM case 'm': /* microstate accounting */
80*8131SRafael.Vanoni@Sun.COM mflag = 1;
81*8131SRafael.Vanoni@Sun.COM break;
82*8131SRafael.Vanoni@Sun.COM case 'p':
83*8131SRafael.Vanoni@Sun.COM pidarg = optarg;
84*8131SRafael.Vanoni@Sun.COM break;
85*8131SRafael.Vanoni@Sun.COM default:
86*8131SRafael.Vanoni@Sun.COM errflg = 1;
87*8131SRafael.Vanoni@Sun.COM break;
88*8131SRafael.Vanoni@Sun.COM }
89*8131SRafael.Vanoni@Sun.COM }
90*8131SRafael.Vanoni@Sun.COM
91*8131SRafael.Vanoni@Sun.COM argc -= optind;
92*8131SRafael.Vanoni@Sun.COM argv += optind;
93*8131SRafael.Vanoni@Sun.COM
94*8131SRafael.Vanoni@Sun.COM if (((pidarg != NULL) ^ (argc < 1)) || errflg) {
950Sstevel@tonic-gate (void) fprintf(stderr,
96*8131SRafael.Vanoni@Sun.COM "usage:\t%s [-mh] [-p pid | command [ args ... ]]\n",
97*8131SRafael.Vanoni@Sun.COM command);
980Sstevel@tonic-gate (void) fprintf(stderr,
99*8131SRafael.Vanoni@Sun.COM " (time a command using microstate accounting)\n");
1000Sstevel@tonic-gate return (1);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
103*8131SRafael.Vanoni@Sun.COM if (pidarg != NULL) {
104*8131SRafael.Vanoni@Sun.COM if ((Pr = proc_arg_grab(pidarg, PR_ARG_PIDS,
105*8131SRafael.Vanoni@Sun.COM Fflag | PGRAB_RDONLY, &gret)) == NULL) {
106*8131SRafael.Vanoni@Sun.COM (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
107*8131SRafael.Vanoni@Sun.COM command, pidarg, Pgrab_error(gret));
108*8131SRafael.Vanoni@Sun.COM return (1);
109*8131SRafael.Vanoni@Sun.COM }
110*8131SRafael.Vanoni@Sun.COM } else {
111*8131SRafael.Vanoni@Sun.COM if ((Pr = Pcreate(argv[0], &argv[0], &gret, NULL, 0)) == NULL) {
112*8131SRafael.Vanoni@Sun.COM (void) fprintf(stderr, "%s: failed to exec %s: %s\n",
113*8131SRafael.Vanoni@Sun.COM command, argv[0], Pcreate_error(gret));
114*8131SRafael.Vanoni@Sun.COM return (1);
115*8131SRafael.Vanoni@Sun.COM }
116*8131SRafael.Vanoni@Sun.COM if (Psetrun(Pr, 0, 0) == -1) {
117*8131SRafael.Vanoni@Sun.COM (void) fprintf(stderr, "%s: failed to set running %s: "
118*8131SRafael.Vanoni@Sun.COM "%s\n", command, argv[0], strerror(errno));
119*8131SRafael.Vanoni@Sun.COM return (1);
120*8131SRafael.Vanoni@Sun.COM }
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate
123*8131SRafael.Vanoni@Sun.COM pid = Pstatus(Pr)->pr_pid;
1240Sstevel@tonic-gate (void) sprintf(procname, "%d", (int)pid); /* for perr() */
1250Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
1260Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN);
127*8131SRafael.Vanoni@Sun.COM
128*8131SRafael.Vanoni@Sun.COM if (pidarg == NULL)
129*8131SRafael.Vanoni@Sun.COM (void) waitid(P_PID, pid, &info, WEXITED | WNOWAIT);
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate (void) look(pid);
1320Sstevel@tonic-gate
133*8131SRafael.Vanoni@Sun.COM if (pidarg != NULL) {
134*8131SRafael.Vanoni@Sun.COM Prelease(Pr, 0);
135*8131SRafael.Vanoni@Sun.COM return (0);
136*8131SRafael.Vanoni@Sun.COM } else {
137*8131SRafael.Vanoni@Sun.COM (void) waitpid(pid, &status, 0);
1380Sstevel@tonic-gate
139*8131SRafael.Vanoni@Sun.COM if (WIFEXITED(status))
140*8131SRafael.Vanoni@Sun.COM return (WEXITSTATUS(status));
1410Sstevel@tonic-gate
142*8131SRafael.Vanoni@Sun.COM if (WIFSIGNALED(status)) {
143*8131SRafael.Vanoni@Sun.COM int sig = WTERMSIG(status);
144*8131SRafael.Vanoni@Sun.COM char name[SIG2STR_MAX];
1450Sstevel@tonic-gate
146*8131SRafael.Vanoni@Sun.COM (void) fprintf(stderr, "%s: command terminated "
147*8131SRafael.Vanoni@Sun.COM "abnormally by %s\n", command,
148*8131SRafael.Vanoni@Sun.COM proc_signame(sig, name, sizeof (name)));
149*8131SRafael.Vanoni@Sun.COM }
150*8131SRafael.Vanoni@Sun.COM
151*8131SRafael.Vanoni@Sun.COM return (status | WCOREFLG); /* see time(1) */
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate }
1540Sstevel@tonic-gate
1550Sstevel@tonic-gate static int
look(pid_t pid)1560Sstevel@tonic-gate look(pid_t pid)
1570Sstevel@tonic-gate {
1580Sstevel@tonic-gate char pathname[100];
1590Sstevel@tonic-gate int rval = 0;
1600Sstevel@tonic-gate int fd;
1610Sstevel@tonic-gate psinfo_t psinfo;
1620Sstevel@tonic-gate prusage_t prusage;
1630Sstevel@tonic-gate timestruc_t real, user, sys;
164*8131SRafael.Vanoni@Sun.COM hrtime_t hrtime;
1650Sstevel@tonic-gate prusage_t *pup = &prusage;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (proc_get_psinfo(pid, &psinfo) < 0)
1680Sstevel@tonic-gate return (perr("read psinfo"));
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate (void) sprintf(pathname, "/proc/%d/usage", (int)pid);
1710Sstevel@tonic-gate if ((fd = open(pathname, O_RDONLY)) < 0)
1720Sstevel@tonic-gate return (perr("open usage"));
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate if (read(fd, &prusage, sizeof (prusage)) != sizeof (prusage))
1750Sstevel@tonic-gate rval = perr("read usage");
1760Sstevel@tonic-gate else {
177*8131SRafael.Vanoni@Sun.COM if (pidarg) {
178*8131SRafael.Vanoni@Sun.COM hrtime = gethrtime();
179*8131SRafael.Vanoni@Sun.COM hrt2ts(hrtime, &real);
180*8131SRafael.Vanoni@Sun.COM } else {
181*8131SRafael.Vanoni@Sun.COM real = pup->pr_term;
182*8131SRafael.Vanoni@Sun.COM }
1830Sstevel@tonic-gate tssub(&real, &real, &pup->pr_create);
1840Sstevel@tonic-gate user = pup->pr_utime;
1850Sstevel@tonic-gate sys = pup->pr_stime;
186*8131SRafael.Vanoni@Sun.COM if (!mflag)
187*8131SRafael.Vanoni@Sun.COM tsadd(&sys, &sys, &pup->pr_ttime);
188*8131SRafael.Vanoni@Sun.COM
1890Sstevel@tonic-gate (void) fprintf(stderr, "\n");
1900Sstevel@tonic-gate prtime("real", &real);
1910Sstevel@tonic-gate prtime("user", &user);
1920Sstevel@tonic-gate prtime("sys", &sys);
193*8131SRafael.Vanoni@Sun.COM
194*8131SRafael.Vanoni@Sun.COM if (mflag) {
195*8131SRafael.Vanoni@Sun.COM prtime("trap", &pup->pr_ttime);
196*8131SRafael.Vanoni@Sun.COM prtime("tflt", &pup->pr_tftime);
197*8131SRafael.Vanoni@Sun.COM prtime("dflt", &pup->pr_dftime);
198*8131SRafael.Vanoni@Sun.COM prtime("kflt", &pup->pr_kftime);
199*8131SRafael.Vanoni@Sun.COM prtime("lock", &pup->pr_ltime);
200*8131SRafael.Vanoni@Sun.COM prtime("slp", &pup->pr_slptime);
201*8131SRafael.Vanoni@Sun.COM prtime("lat", &pup->pr_wtime);
202*8131SRafael.Vanoni@Sun.COM prtime("stop", &pup->pr_stoptime);
203*8131SRafael.Vanoni@Sun.COM }
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate (void) close(fd);
2070Sstevel@tonic-gate return (rval);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate static void
hr_min_sec(char * buf,long sec)2110Sstevel@tonic-gate hr_min_sec(char *buf, long sec)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate if (sec >= 3600)
2140Sstevel@tonic-gate (void) sprintf(buf, "%ld:%.2ld:%.2ld",
215*8131SRafael.Vanoni@Sun.COM sec / 3600, (sec % 3600) / 60, sec % 60);
2160Sstevel@tonic-gate else if (sec >= 60)
2170Sstevel@tonic-gate (void) sprintf(buf, "%ld:%.2ld",
218*8131SRafael.Vanoni@Sun.COM sec / 60, sec % 60);
2190Sstevel@tonic-gate else
2200Sstevel@tonic-gate (void) sprintf(buf, "%ld", sec);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate static void
prtime(char * name,timestruc_t * ts)2240Sstevel@tonic-gate prtime(char *name, timestruc_t *ts)
2250Sstevel@tonic-gate {
2260Sstevel@tonic-gate char buf[32];
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate hr_min_sec(buf, ts->tv_sec);
229*8131SRafael.Vanoni@Sun.COM
230*8131SRafael.Vanoni@Sun.COM (void) fprintf(stderr, "%-4s %8s.%.9u\n",
231*8131SRafael.Vanoni@Sun.COM name, buf, (uint_t)ts->tv_nsec);
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate static int
perr(const char * s)2350Sstevel@tonic-gate perr(const char *s)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate if (s)
2380Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", procname);
2390Sstevel@tonic-gate else
2400Sstevel@tonic-gate s = procname;
2410Sstevel@tonic-gate perror(s);
2420Sstevel@tonic-gate return (1);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate
2450Sstevel@tonic-gate static void
tsadd(timestruc_t * result,timestruc_t * a,timestruc_t * b)2460Sstevel@tonic-gate tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate result->tv_sec = a->tv_sec + b->tv_sec;
2490Sstevel@tonic-gate if ((result->tv_nsec = a->tv_nsec + b->tv_nsec) >= 1000000000) {
2500Sstevel@tonic-gate result->tv_nsec -= 1000000000;
2510Sstevel@tonic-gate result->tv_sec += 1;
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate static void
tssub(timestruc_t * result,timestruc_t * a,timestruc_t * b)2560Sstevel@tonic-gate tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b)
2570Sstevel@tonic-gate {
2580Sstevel@tonic-gate result->tv_sec = a->tv_sec - b->tv_sec;
2590Sstevel@tonic-gate if ((result->tv_nsec = a->tv_nsec - b->tv_nsec) < 0) {
2600Sstevel@tonic-gate result->tv_nsec += 1000000000;
2610Sstevel@tonic-gate result->tv_sec -= 1;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate }
264*8131SRafael.Vanoni@Sun.COM
265*8131SRafael.Vanoni@Sun.COM static void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)266*8131SRafael.Vanoni@Sun.COM hrt2ts(hrtime_t hrt, timestruc_t *tsp)
267*8131SRafael.Vanoni@Sun.COM {
268*8131SRafael.Vanoni@Sun.COM tsp->tv_sec = hrt / NANOSEC;
269*8131SRafael.Vanoni@Sun.COM tsp->tv_nsec = hrt % NANOSEC;
270*8131SRafael.Vanoni@Sun.COM }
271