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.
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.
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]
22 * Copyright (c) 2013 Gary Mills
24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
27 * Portions Copyright 2009 Chad Mynhier
28 * Copyright 2018 Joyent, Inc. All rights reserved.
31 #include <sys/types.h>
32 #include <sys/param.h>
33 #include <sys/resource.h>
34 #include <sys/priocntl.h>
35 #include <sys/rtpriocntl.h>
36 #include <sys/tspriocntl.h>
47 #include <stdio_ext.h>
57 static char PRG_FMT
[] = "%s: ";
58 static char ERR_FMT
[] = ": %s\n";
59 static char *progname
;
60 static char projbuf
[PROJECT_BUFSZ
];
62 #define RLIMIT_NOFILE_MAX 32767
66 Warn(char *format
, ...)
72 (void) fprintf(stderr
, PRG_FMT
, progname
);
73 va_start(alist
, format
);
74 (void) vfprintf(stderr
, format
, alist
);
76 if (strchr(format
, '\n') == NULL
)
77 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
82 Die(char *format
, ...)
88 (void) fprintf(stderr
, PRG_FMT
, progname
);
89 va_start(alist
, format
);
90 (void) vfprintf(stderr
, format
, alist
);
92 if (strchr(format
, '\n') == NULL
)
93 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
100 char *p
= strrchr(arg0
, '/');
111 (void) fprintf(stderr
, gettext(
112 "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
113 "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
114 "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
115 "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
116 "\t[interval [counter]]\n"));
126 i
= (int)strtol(p
, &q
, 10);
127 if (errno
!= 0 || q
== p
|| i
< 0 || *q
!= '\0')
128 Die(gettext("illegal argument -- %s\n"), p
);
132 return (0); /* keep gcc happy */
136 Format_size(char *str
, size_t size
, int length
)
140 size
= (size
+ 512) / 1024;
143 size
= (size
+ 512) / 1024;
147 (void) snprintf(str
, length
, "%4d%c", (int)size
, tag
);
151 Format_time(char *str
, ulong_t time
, int length
)
153 (void) snprintf(str
, length
, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
154 (int)time
/3600, (int)(time
% 3600)/60, (int)time
% 60);
158 Format_pct(char *str
, float val
, int length
)
160 if (val
> (float)100)
165 if (val
< (float)9.95)
166 (void) snprintf(str
, length
, "%1.1f", val
);
168 (void) snprintf(str
, length
, "%.0f", val
);
172 Format_num(char *str
, int num
, int length
)
175 (void) snprintf(str
, length
, ".%1dM", num
/100000);
178 (void) snprintf(str
, length
, "%2dK", num
/1000);
180 (void) snprintf(str
, length
, "%3d", num
);
185 Format_state(char *str
, char state
, processorid_t pr_id
, int length
)
189 (void) strncpy(str
, "sleep", length
);
192 (void) strncpy(str
, "run", length
);
195 (void) strncpy(str
, "zombie", length
);
198 (void) strncpy(str
, "stop", length
);
201 (void) strncpy(str
, "idle", length
);
204 (void) strncpy(str
, "wait", length
);
207 (void) snprintf(str
, length
, "cpu%-3d", (int)pr_id
);
210 (void) strncpy(str
, "?", length
);
216 Realloc(void *ptr
, size_t size
)
221 eagain
: if ((ptr
= realloc(ptr
, size
)))
224 if ((++cnt
<= 3) && (errno
== EAGAIN
)) {
225 Warn(gettext("realloc() failed, attempt %d"), cnt
);
226 (void) poll(NULL
, 0, 5000); /* wait for 5 seconds */
231 Die(gettext("not enough memory"));
233 return (NULL
); /* keep gcc happy */
239 return (Realloc(NULL
, size
));
245 return (memset(Realloc(NULL
, size
), 0, size
));
253 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
254 Die(gettext("getrlimit failed"));
255 fd_limit
= rlim
.rlim_cur
;
256 rlim
.rlim_max
= MIN(rlim
.rlim_max
, RLIMIT_NOFILE_MAX
);
257 rlim
.rlim_cur
= rlim
.rlim_max
;
258 (void) enable_extended_FILE_stdio(-1, -1);
259 if (setrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
262 return (rlim
.rlim_cur
);
266 Priocntl(char *class)
270 (void) strcpy(pcinfo
.pc_clname
, class);
271 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1) {
272 Warn(gettext("cannot get real time class parameters"));
275 pcparms
.pc_cid
= pcinfo
.pc_cid
;
276 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_pri
= 0;
277 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqsecs
= 0;
278 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqnsecs
= RT_NOCHANGE
;
279 if (priocntl(P_PID
, getpid(), PC_SETPARMS
, (caddr_t
)&pcparms
) == -1)
280 Warn(gettext("cannot enter the real time class"));
284 getprojname(projid_t projid
, char *str
, size_t len
, int noresolve
,
285 int trunc
, size_t width
)
290 if (noresolve
|| getprojbyid(projid
, &proj
, projbuf
, PROJECT_BUFSZ
) ==
292 (void) snprintf(str
, len
, "%-6d", (int)projid
);
294 n
= mbstowcs(NULL
, proj
.pj_name
, 0);
296 (void) snprintf(str
, len
, "%-28s", "ERROR");
297 else if (trunc
&& n
> width
)
298 (void) snprintf(str
, len
, "%.*s%c", width
- 1,
301 (void) snprintf(str
, len
, "%-28s", proj
.pj_name
);
306 getzonename(zoneid_t zoneid
, char *str
, size_t len
, int trunc
, size_t width
)
308 char zone_name
[ZONENAME_MAX
];
311 if (getzonenamebyid(zoneid
, zone_name
, sizeof (zone_name
)) < 0) {
312 (void) snprintf(str
, len
, "%-6d", (int)zoneid
);
314 n
= mbstowcs(NULL
, zone_name
, 0);
316 (void) snprintf(str
, len
, "%-28s", "ERROR");
317 else if (trunc
&& n
> width
)
318 (void) snprintf(str
, len
, "%.*s%c", width
- 1,
321 (void) snprintf(str
, len
, "%-28s", zone_name
);
326 * Remove all unprintable characters from process name
329 stripfname(char *buf
, size_t bufsize
, const char *pname
)
331 int bytesleft
= PRFNSZ
;
336 (void) strlcpy(buf
, pname
, bufsize
);
338 buf
[bytesleft
- 1] = '\0';
340 for (cp
= buf
; *cp
!= '\0'; cp
+= length
) {
341 length
= mbtowc(&wchar
, cp
, MB_LEN_MAX
);
346 if (!iswprint(wchar
)) {
347 if (bytesleft
<= length
) {
351 (void) memmove(cp
, cp
+ length
, bytesleft
- length
);
360 * prstat has always implicitly wanted a terminal width of at least 80 columns
361 * (when a TTY is present). If run in a terminal narrower than 80 columns,
362 * prstat output may wrap. For wider terminals, we allow the last column to use
363 * the additional space.
365 * We never truncate if using -c, or not outputting to a TTY.
368 format_namewidth(void)
372 if (opts
.o_cols
== 0 || !(opts
.o_outpmode
& (OPT_TERMCAP
| OPT_TRUNC
)))
375 if (opts
.o_outpmode
& OPT_PSINFO
) {
376 if (opts
.o_outpmode
& OPT_LGRP
)
380 } else if (opts
.o_outpmode
& OPT_MSACCT
) {
384 return (opts
.o_cols
- prefixlen
);
388 format_name(lwp_info_t
*lwp
, char *buf
, size_t buflen
)
390 int pname_width
= PRFNSZ
;
396 stripfname(pname
, sizeof (pname
), lwp
->li_info
.pr_fname
);
398 if (opts
.o_outpmode
& OPT_LWPS
) {
399 n
= snprintf(nr_suffix
, sizeof (nr_suffix
), "%d",
400 lwp
->li_info
.pr_lwp
.pr_lwpid
);
402 n
= snprintf(nr_suffix
, sizeof (nr_suffix
), "%d",
403 lwp
->li_info
.pr_nlwp
+ lwp
->li_info
.pr_nzomb
);
406 width
= format_namewidth();
408 /* If we're over budget, truncate the process name not the LWP part. */
409 if (strlen(pname
) > (width
- n
- 1)) {
410 pname_width
= width
- n
- 1;
411 pname
[pname_width
- 1] = '*';
414 if ((opts
.o_outpmode
& OPT_LWPS
) && lwp
->li_lwpname
[0] != '\0') {
415 n
= snprintf(buf
, buflen
, "%.*s/%s [%s]", pname_width
,
416 pname
, nr_suffix
, lwp
->li_lwpname
);
418 n
= snprintf(buf
, buflen
, "%.*s/%s", pname_width
,
422 if (width
> 0 && strlen(buf
) > width
)