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
30 #include <sys/types.h>
31 #include <sys/param.h>
32 #include <sys/resource.h>
33 #include <sys/priocntl.h>
34 #include <sys/rtpriocntl.h>
35 #include <sys/tspriocntl.h>
46 #include <stdio_ext.h>
56 static char PRG_FMT
[] = "%s: ";
57 static char ERR_FMT
[] = ": %s\n";
58 static char *progname
;
59 static char projbuf
[PROJECT_BUFSZ
];
61 #define RLIMIT_NOFILE_MAX 32767
65 Warn(char *format
, ...)
71 (void) fprintf(stderr
, PRG_FMT
, progname
);
72 va_start(alist
, format
);
73 (void) vfprintf(stderr
, format
, alist
);
75 if (strchr(format
, '\n') == NULL
)
76 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
81 Die(char *format
, ...)
87 (void) fprintf(stderr
, PRG_FMT
, progname
);
88 va_start(alist
, format
);
89 (void) vfprintf(stderr
, format
, alist
);
91 if (strchr(format
, '\n') == NULL
)
92 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
99 char *p
= strrchr(arg0
, '/');
110 (void) fprintf(stderr
, gettext(
111 "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
112 "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
113 "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
114 "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
115 "\t[interval [counter]]\n"));
125 i
= (int)strtol(p
, &q
, 10);
126 if (errno
!= 0 || q
== p
|| i
< 0 || *q
!= '\0')
127 Die(gettext("illegal argument -- %s\n"), p
);
131 return (0); /* keep gcc happy */
135 Format_size(char *str
, size_t size
, int length
)
139 size
= (size
+ 512) / 1024;
142 size
= (size
+ 512) / 1024;
146 (void) snprintf(str
, length
, "%4d%c", (int)size
, tag
);
150 Format_time(char *str
, ulong_t time
, int length
)
152 (void) snprintf(str
, length
, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
153 (int)time
/3600, (int)(time
% 3600)/60, (int)time
% 60);
157 Format_pct(char *str
, float val
, int length
)
159 if (val
> (float)100)
164 if (val
< (float)9.95)
165 (void) snprintf(str
, length
, "%1.1f", val
);
167 (void) snprintf(str
, length
, "%.0f", val
);
171 Format_num(char *str
, int num
, int length
)
174 (void) snprintf(str
, length
, ".%1dM", num
/100000);
177 (void) snprintf(str
, length
, "%2dK", num
/1000);
179 (void) snprintf(str
, length
, "%3d", num
);
184 Format_state(char *str
, char state
, processorid_t pr_id
, int length
)
188 (void) strncpy(str
, "sleep", length
);
191 (void) strncpy(str
, "run", length
);
194 (void) strncpy(str
, "zombie", length
);
197 (void) strncpy(str
, "stop", length
);
200 (void) strncpy(str
, "idle", length
);
203 (void) strncpy(str
, "wait", length
);
206 (void) snprintf(str
, length
, "cpu%-3d", (int)pr_id
);
209 (void) strncpy(str
, "?", length
);
215 Realloc(void *ptr
, size_t size
)
220 eagain
: if ((ptr
= realloc(ptr
, size
)))
223 if ((++cnt
<= 3) && (errno
== EAGAIN
)) {
224 Warn(gettext("realloc() failed, attempt %d"), cnt
);
225 (void) poll(NULL
, 0, 5000); /* wait for 5 seconds */
230 Die(gettext("not enough memory"));
232 return (NULL
); /* keep gcc happy */
238 return (Realloc(NULL
, size
));
244 return (memset(Realloc(NULL
, size
), 0, size
));
252 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
253 Die(gettext("getrlimit failed"));
254 fd_limit
= rlim
.rlim_cur
;
255 rlim
.rlim_max
= MIN(rlim
.rlim_max
, RLIMIT_NOFILE_MAX
);
256 rlim
.rlim_cur
= rlim
.rlim_max
;
257 (void) enable_extended_FILE_stdio(-1, -1);
258 if (setrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
261 return (rlim
.rlim_cur
);
265 Priocntl(char *class)
269 (void) strcpy(pcinfo
.pc_clname
, class);
270 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1) {
271 Warn(gettext("cannot get real time class parameters"));
274 pcparms
.pc_cid
= pcinfo
.pc_cid
;
275 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_pri
= 0;
276 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqsecs
= 0;
277 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqnsecs
= RT_NOCHANGE
;
278 if (priocntl(P_PID
, getpid(), PC_SETPARMS
, (caddr_t
)&pcparms
) == -1)
279 Warn(gettext("cannot enter the real time class"));
283 getprojname(projid_t projid
, char *str
, size_t len
, int noresolve
,
284 int trunc
, size_t width
)
289 if (noresolve
|| getprojbyid(projid
, &proj
, projbuf
, PROJECT_BUFSZ
) ==
291 (void) snprintf(str
, len
, "%-6d", (int)projid
);
293 n
= mbstowcs(NULL
, proj
.pj_name
, 0);
295 (void) snprintf(str
, len
, "%-28s", "ERROR");
296 else if (trunc
&& n
> width
)
297 (void) snprintf(str
, len
, "%.*s%c", width
- 1,
300 (void) snprintf(str
, len
, "%-28s", proj
.pj_name
);
305 getzonename(zoneid_t zoneid
, char *str
, size_t len
, int trunc
, size_t width
)
307 char zone_name
[ZONENAME_MAX
];
310 if (getzonenamebyid(zoneid
, zone_name
, sizeof (zone_name
)) < 0) {
311 (void) snprintf(str
, len
, "%-6d", (int)zoneid
);
313 n
= mbstowcs(NULL
, zone_name
, 0);
315 (void) snprintf(str
, len
, "%-28s", "ERROR");
316 else if (trunc
&& n
> width
)
317 (void) snprintf(str
, len
, "%.*s%c", width
- 1,
320 (void) snprintf(str
, len
, "%-28s", zone_name
);
325 * Remove all unprintable characters from process name
328 stripfname(char *buf
)
330 int bytesleft
= PRFNSZ
;
335 buf
[bytesleft
- 1] = '\0';
337 for (cp
= buf
; *cp
!= '\0'; cp
+= length
) {
338 length
= mbtowc(&wchar
, cp
, MB_LEN_MAX
);
343 if (!iswprint(wchar
)) {
344 if (bytesleft
<= length
) {
348 (void) memmove(cp
, cp
+ length
, bytesleft
- length
);