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
5509bee73SRafael Vanoni Polanczyk * Common Development and Distribution License (the "License").
6509bee73SRafael Vanoni Polanczyk * 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 */
217c478bd9Sstevel@tonic-gate /*
22509bee73SRafael Vanoni Polanczyk * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
24509bee73SRafael Vanoni Polanczyk *
25509bee73SRafael Vanoni Polanczyk * Portions Copyright 2008 Chad Mynhier
267c478bd9Sstevel@tonic-gate */
27*8551f527SRobert Mustacchi /*
28*8551f527SRobert Mustacchi * Copyright 2016 Joyent, Inc.
29*8551f527SRobert Mustacchi */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <unistd.h>
347c478bd9Sstevel@tonic-gate #include <fcntl.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <math.h>
387c478bd9Sstevel@tonic-gate #include <wait.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <sys/types.h>
417c478bd9Sstevel@tonic-gate #include <sys/time.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <libproc.h>
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate static int look(pid_t);
467c478bd9Sstevel@tonic-gate static void hr_min_sec(char *, long);
477c478bd9Sstevel@tonic-gate static void prtime(char *, timestruc_t *);
487c478bd9Sstevel@tonic-gate static int perr(const char *);
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate static void tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b);
517c478bd9Sstevel@tonic-gate static void tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b);
52509bee73SRafael Vanoni Polanczyk static void hrt2ts(hrtime_t hrt, timestruc_t *tsp);
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate static char *command;
55509bee73SRafael Vanoni Polanczyk static char *pidarg;
567c478bd9Sstevel@tonic-gate static char procname[64];
577c478bd9Sstevel@tonic-gate
58509bee73SRafael Vanoni Polanczyk static int Fflag;
59509bee73SRafael Vanoni Polanczyk static int mflag;
60509bee73SRafael Vanoni Polanczyk static int errflg;
61*8551f527SRobert Mustacchi static int pflag;
62*8551f527SRobert Mustacchi static int pfirst;
63*8551f527SRobert Mustacchi
64*8551f527SRobert Mustacchi static int
ptime_pid(const char * pidstr)65*8551f527SRobert Mustacchi ptime_pid(const char *pidstr)
66*8551f527SRobert Mustacchi {
67*8551f527SRobert Mustacchi struct ps_prochandle *Pr;
68*8551f527SRobert Mustacchi pid_t pid;
69*8551f527SRobert Mustacchi int gret;
70*8551f527SRobert Mustacchi
71*8551f527SRobert Mustacchi if ((Pr = proc_arg_grab(pidstr, PR_ARG_PIDS,
72*8551f527SRobert Mustacchi Fflag | PGRAB_RDONLY, &gret)) == NULL) {
73*8551f527SRobert Mustacchi (void) fprintf(stderr, "%s: cannot examine %s: %s\n",
74*8551f527SRobert Mustacchi command, pidstr, Pgrab_error(gret));
75*8551f527SRobert Mustacchi return (1);
76*8551f527SRobert Mustacchi }
77*8551f527SRobert Mustacchi
78*8551f527SRobert Mustacchi pid = Pstatus(Pr)->pr_pid;
79*8551f527SRobert Mustacchi (void) sprintf(procname, "%d", (int)pid); /* for perr() */
80*8551f527SRobert Mustacchi (void) look(pid);
81*8551f527SRobert Mustacchi Prelease(Pr, 0);
82*8551f527SRobert Mustacchi return (0);
83*8551f527SRobert Mustacchi }
84509bee73SRafael Vanoni Polanczyk
857c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)867c478bd9Sstevel@tonic-gate main(int argc, char **argv)
877c478bd9Sstevel@tonic-gate {
88*8551f527SRobert Mustacchi int opt, exit;
897c478bd9Sstevel@tonic-gate pid_t pid;
907c478bd9Sstevel@tonic-gate struct siginfo info;
917c478bd9Sstevel@tonic-gate int status;
92509bee73SRafael Vanoni Polanczyk int gret;
93509bee73SRafael Vanoni Polanczyk struct ps_prochandle *Pr;
947c478bd9Sstevel@tonic-gate
957c478bd9Sstevel@tonic-gate if ((command = strrchr(argv[0], '/')) != NULL)
967c478bd9Sstevel@tonic-gate command++;
977c478bd9Sstevel@tonic-gate else
987c478bd9Sstevel@tonic-gate command = argv[0];
997c478bd9Sstevel@tonic-gate
100509bee73SRafael Vanoni Polanczyk while ((opt = getopt(argc, argv, "Fhmp:")) != EOF) {
101509bee73SRafael Vanoni Polanczyk switch (opt) {
102509bee73SRafael Vanoni Polanczyk case 'F': /* force grabbing (no O_EXCL) */
103509bee73SRafael Vanoni Polanczyk Fflag = PGRAB_FORCE;
104509bee73SRafael Vanoni Polanczyk break;
105509bee73SRafael Vanoni Polanczyk case 'm': /* microstate accounting */
106509bee73SRafael Vanoni Polanczyk mflag = 1;
107509bee73SRafael Vanoni Polanczyk break;
108509bee73SRafael Vanoni Polanczyk case 'p':
109*8551f527SRobert Mustacchi pflag = 1;
110509bee73SRafael Vanoni Polanczyk pidarg = optarg;
111509bee73SRafael Vanoni Polanczyk break;
112509bee73SRafael Vanoni Polanczyk default:
113509bee73SRafael Vanoni Polanczyk errflg = 1;
114509bee73SRafael Vanoni Polanczyk break;
115509bee73SRafael Vanoni Polanczyk }
116509bee73SRafael Vanoni Polanczyk }
117509bee73SRafael Vanoni Polanczyk
118509bee73SRafael Vanoni Polanczyk argc -= optind;
119509bee73SRafael Vanoni Polanczyk argv += optind;
120509bee73SRafael Vanoni Polanczyk
121509bee73SRafael Vanoni Polanczyk if (((pidarg != NULL) ^ (argc < 1)) || errflg) {
1227c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
123*8551f527SRobert Mustacchi "usage:\t%s [-mh] [-p pidlist | command [ args ... ]]\n",
124509bee73SRafael Vanoni Polanczyk command);
1257c478bd9Sstevel@tonic-gate (void) fprintf(stderr,
126509bee73SRafael Vanoni Polanczyk " (time a command using microstate accounting)\n");
1277c478bd9Sstevel@tonic-gate return (1);
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate
130*8551f527SRobert Mustacchi if (pflag) {
131*8551f527SRobert Mustacchi char *pp;
132*8551f527SRobert Mustacchi
133*8551f527SRobert Mustacchi exit = 0;
134*8551f527SRobert Mustacchi (void) signal(SIGINT, SIG_IGN);
135*8551f527SRobert Mustacchi (void) signal(SIGQUIT, SIG_IGN);
136*8551f527SRobert Mustacchi
137*8551f527SRobert Mustacchi pp = strtok(pidarg, ", ");
138*8551f527SRobert Mustacchi if (pp == NULL) {
139*8551f527SRobert Mustacchi (void) fprintf(stderr, "%s: invalid argument for -p\n",
140*8551f527SRobert Mustacchi command);
141509bee73SRafael Vanoni Polanczyk return (1);
142509bee73SRafael Vanoni Polanczyk }
143*8551f527SRobert Mustacchi exit = ptime_pid(pp);
144*8551f527SRobert Mustacchi while ((pp = strtok(NULL, ", ")) != NULL) {
145*8551f527SRobert Mustacchi exit |= ptime_pid(pp);
146509bee73SRafael Vanoni Polanczyk }
147*8551f527SRobert Mustacchi return (exit);
148*8551f527SRobert Mustacchi }
149*8551f527SRobert Mustacchi
150*8551f527SRobert Mustacchi
151*8551f527SRobert Mustacchi if ((Pr = Pcreate(argv[0], &argv[0], &gret, NULL, 0)) == NULL) {
152*8551f527SRobert Mustacchi (void) fprintf(stderr, "%s: failed to exec %s: %s\n",
153*8551f527SRobert Mustacchi command, argv[0], Pcreate_error(gret));
154*8551f527SRobert Mustacchi return (1);
155*8551f527SRobert Mustacchi }
156*8551f527SRobert Mustacchi if (Psetrun(Pr, 0, 0) == -1) {
157*8551f527SRobert Mustacchi (void) fprintf(stderr, "%s: failed to set running %s: "
158*8551f527SRobert Mustacchi "%s\n", command, argv[0], strerror(errno));
159*8551f527SRobert Mustacchi return (1);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate
162509bee73SRafael Vanoni Polanczyk pid = Pstatus(Pr)->pr_pid;
163*8551f527SRobert Mustacchi
1647c478bd9Sstevel@tonic-gate (void) sprintf(procname, "%d", (int)pid); /* for perr() */
1657c478bd9Sstevel@tonic-gate (void) signal(SIGINT, SIG_IGN);
1667c478bd9Sstevel@tonic-gate (void) signal(SIGQUIT, SIG_IGN);
167509bee73SRafael Vanoni Polanczyk
168*8551f527SRobert Mustacchi (void) waitid(P_PID, pid, &info, WEXITED | WNOWAIT);
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate (void) look(pid);
1717c478bd9Sstevel@tonic-gate
172*8551f527SRobert Mustacchi (void) waitpid(pid, &status, 0);
1737c478bd9Sstevel@tonic-gate
174*8551f527SRobert Mustacchi if (WIFEXITED(status))
175*8551f527SRobert Mustacchi return (WEXITSTATUS(status));
1767c478bd9Sstevel@tonic-gate
177*8551f527SRobert Mustacchi if (WIFSIGNALED(status)) {
178*8551f527SRobert Mustacchi int sig = WTERMSIG(status);
179*8551f527SRobert Mustacchi char name[SIG2STR_MAX];
1807c478bd9Sstevel@tonic-gate
181*8551f527SRobert Mustacchi (void) fprintf(stderr, "%s: command terminated "
182*8551f527SRobert Mustacchi "abnormally by %s\n", command,
183*8551f527SRobert Mustacchi proc_signame(sig, name, sizeof (name)));
184509bee73SRafael Vanoni Polanczyk }
185*8551f527SRobert Mustacchi
186*8551f527SRobert Mustacchi return (status | WCOREFLG); /* see time(1) */
1877c478bd9Sstevel@tonic-gate }
1887c478bd9Sstevel@tonic-gate
1897c478bd9Sstevel@tonic-gate static int
look(pid_t pid)1907c478bd9Sstevel@tonic-gate look(pid_t pid)
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate char pathname[100];
1937c478bd9Sstevel@tonic-gate int rval = 0;
1947c478bd9Sstevel@tonic-gate int fd;
1957c478bd9Sstevel@tonic-gate psinfo_t psinfo;
1967c478bd9Sstevel@tonic-gate prusage_t prusage;
1977c478bd9Sstevel@tonic-gate timestruc_t real, user, sys;
198509bee73SRafael Vanoni Polanczyk hrtime_t hrtime;
1997c478bd9Sstevel@tonic-gate prusage_t *pup = &prusage;
2007c478bd9Sstevel@tonic-gate
201*8551f527SRobert Mustacchi pfirst++;
202*8551f527SRobert Mustacchi
2037c478bd9Sstevel@tonic-gate if (proc_get_psinfo(pid, &psinfo) < 0)
2047c478bd9Sstevel@tonic-gate return (perr("read psinfo"));
2057c478bd9Sstevel@tonic-gate
2067c478bd9Sstevel@tonic-gate (void) sprintf(pathname, "/proc/%d/usage", (int)pid);
2077c478bd9Sstevel@tonic-gate if ((fd = open(pathname, O_RDONLY)) < 0)
2087c478bd9Sstevel@tonic-gate return (perr("open usage"));
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate if (read(fd, &prusage, sizeof (prusage)) != sizeof (prusage))
2117c478bd9Sstevel@tonic-gate rval = perr("read usage");
2127c478bd9Sstevel@tonic-gate else {
213509bee73SRafael Vanoni Polanczyk if (pidarg) {
214509bee73SRafael Vanoni Polanczyk hrtime = gethrtime();
215509bee73SRafael Vanoni Polanczyk hrt2ts(hrtime, &real);
216509bee73SRafael Vanoni Polanczyk } else {
217509bee73SRafael Vanoni Polanczyk real = pup->pr_term;
218509bee73SRafael Vanoni Polanczyk }
2197c478bd9Sstevel@tonic-gate tssub(&real, &real, &pup->pr_create);
2207c478bd9Sstevel@tonic-gate user = pup->pr_utime;
2217c478bd9Sstevel@tonic-gate sys = pup->pr_stime;
222509bee73SRafael Vanoni Polanczyk if (!mflag)
223509bee73SRafael Vanoni Polanczyk tsadd(&sys, &sys, &pup->pr_ttime);
224509bee73SRafael Vanoni Polanczyk
225*8551f527SRobert Mustacchi if (!pflag || pfirst > 1)
226*8551f527SRobert Mustacchi (void) fprintf(stderr, "\n");
227*8551f527SRobert Mustacchi if (pflag)
228*8551f527SRobert Mustacchi (void) fprintf(stderr, "%d:\t%.70s\n",
229*8551f527SRobert Mustacchi (int)psinfo.pr_pid, psinfo.pr_psargs);
2307c478bd9Sstevel@tonic-gate prtime("real", &real);
2317c478bd9Sstevel@tonic-gate prtime("user", &user);
2327c478bd9Sstevel@tonic-gate prtime("sys", &sys);
233509bee73SRafael Vanoni Polanczyk
234509bee73SRafael Vanoni Polanczyk if (mflag) {
235509bee73SRafael Vanoni Polanczyk prtime("trap", &pup->pr_ttime);
236509bee73SRafael Vanoni Polanczyk prtime("tflt", &pup->pr_tftime);
237509bee73SRafael Vanoni Polanczyk prtime("dflt", &pup->pr_dftime);
238509bee73SRafael Vanoni Polanczyk prtime("kflt", &pup->pr_kftime);
239509bee73SRafael Vanoni Polanczyk prtime("lock", &pup->pr_ltime);
240509bee73SRafael Vanoni Polanczyk prtime("slp", &pup->pr_slptime);
241509bee73SRafael Vanoni Polanczyk prtime("lat", &pup->pr_wtime);
242509bee73SRafael Vanoni Polanczyk prtime("stop", &pup->pr_stoptime);
243509bee73SRafael Vanoni Polanczyk }
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate (void) close(fd);
2477c478bd9Sstevel@tonic-gate return (rval);
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate static void
hr_min_sec(char * buf,long sec)2517c478bd9Sstevel@tonic-gate hr_min_sec(char *buf, long sec)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate if (sec >= 3600)
2547c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%ld:%.2ld:%.2ld",
255509bee73SRafael Vanoni Polanczyk sec / 3600, (sec % 3600) / 60, sec % 60);
2567c478bd9Sstevel@tonic-gate else if (sec >= 60)
2577c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%ld:%.2ld",
258509bee73SRafael Vanoni Polanczyk sec / 60, sec % 60);
2597c478bd9Sstevel@tonic-gate else
2607c478bd9Sstevel@tonic-gate (void) sprintf(buf, "%ld", sec);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate static void
prtime(char * name,timestruc_t * ts)2647c478bd9Sstevel@tonic-gate prtime(char *name, timestruc_t *ts)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate char buf[32];
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate hr_min_sec(buf, ts->tv_sec);
269509bee73SRafael Vanoni Polanczyk
270509bee73SRafael Vanoni Polanczyk (void) fprintf(stderr, "%-4s %8s.%.9u\n",
271509bee73SRafael Vanoni Polanczyk name, buf, (uint_t)ts->tv_nsec);
2727c478bd9Sstevel@tonic-gate }
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate static int
perr(const char * s)2757c478bd9Sstevel@tonic-gate perr(const char *s)
2767c478bd9Sstevel@tonic-gate {
2777c478bd9Sstevel@tonic-gate if (s)
2787c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", procname);
2797c478bd9Sstevel@tonic-gate else
2807c478bd9Sstevel@tonic-gate s = procname;
2817c478bd9Sstevel@tonic-gate perror(s);
2827c478bd9Sstevel@tonic-gate return (1);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate static void
tsadd(timestruc_t * result,timestruc_t * a,timestruc_t * b)2867c478bd9Sstevel@tonic-gate tsadd(timestruc_t *result, timestruc_t *a, timestruc_t *b)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate result->tv_sec = a->tv_sec + b->tv_sec;
2897c478bd9Sstevel@tonic-gate if ((result->tv_nsec = a->tv_nsec + b->tv_nsec) >= 1000000000) {
2907c478bd9Sstevel@tonic-gate result->tv_nsec -= 1000000000;
2917c478bd9Sstevel@tonic-gate result->tv_sec += 1;
2927c478bd9Sstevel@tonic-gate }
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate static void
tssub(timestruc_t * result,timestruc_t * a,timestruc_t * b)2967c478bd9Sstevel@tonic-gate tssub(timestruc_t *result, timestruc_t *a, timestruc_t *b)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate result->tv_sec = a->tv_sec - b->tv_sec;
2997c478bd9Sstevel@tonic-gate if ((result->tv_nsec = a->tv_nsec - b->tv_nsec) < 0) {
3007c478bd9Sstevel@tonic-gate result->tv_nsec += 1000000000;
3017c478bd9Sstevel@tonic-gate result->tv_sec -= 1;
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate }
304509bee73SRafael Vanoni Polanczyk
305509bee73SRafael Vanoni Polanczyk static void
hrt2ts(hrtime_t hrt,timestruc_t * tsp)306509bee73SRafael Vanoni Polanczyk hrt2ts(hrtime_t hrt, timestruc_t *tsp)
307509bee73SRafael Vanoni Polanczyk {
308509bee73SRafael Vanoni Polanczyk tsp->tv_sec = hrt / NANOSEC;
309509bee73SRafael Vanoni Polanczyk tsp->tv_nsec = hrt % NANOSEC;
310509bee73SRafael Vanoni Polanczyk }
311