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 2009 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
25 * Portions Copyright 2009 Chad Mynhier
28 #include <sys/types.h>
29 #include <sys/param.h>
30 #include <sys/resource.h>
31 #include <sys/priocntl.h>
32 #include <sys/rtpriocntl.h>
33 #include <sys/tspriocntl.h>
44 #include <stdio_ext.h>
54 static char PRG_FMT
[] = "%s: ";
55 static char ERR_FMT
[] = ": %s\n";
56 static char *progname
;
57 static char projbuf
[PROJECT_BUFSZ
];
59 #define RLIMIT_NOFILE_MAX 32767
63 Warn(char *format
, ...)
69 (void) fprintf(stderr
, PRG_FMT
, progname
);
70 va_start(alist
, format
);
71 (void) vfprintf(stderr
, format
, alist
);
73 if (strchr(format
, '\n') == NULL
)
74 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
79 Die(char *format
, ...)
85 (void) fprintf(stderr
, PRG_FMT
, progname
);
86 va_start(alist
, format
);
87 (void) vfprintf(stderr
, format
, alist
);
89 if (strchr(format
, '\n') == NULL
)
90 (void) fprintf(stderr
, gettext(ERR_FMT
), strerror(err
));
97 char *p
= strrchr(arg0
, '/');
108 (void) fprintf(stderr
, gettext(
109 "Usage:\tprstat [-acHJLmrRtTvZ] [-u euidlist] [-U uidlist]\n"
110 "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
111 "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
112 "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
113 "\t[interval [counter]]\n"));
123 i
= (int)strtol(p
, &q
, 10);
124 if (errno
!= 0 || q
== p
|| i
< 0 || *q
!= '\0')
125 Die(gettext("illegal argument -- %s\n"), p
);
129 return (0); /* keep gcc happy */
133 Format_size(char *str
, size_t size
, int length
)
137 size
= (size
+ 512) / 1024;
140 size
= (size
+ 512) / 1024;
144 (void) snprintf(str
, length
, "%4d%c", (int)size
, tag
);
148 Format_time(char *str
, ulong_t time
, int length
)
150 (void) snprintf(str
, length
, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
151 (int)time
/3600, (int)(time
% 3600)/60, (int)time
% 60);
155 Format_pct(char *str
, float val
, int length
)
157 if (val
> (float)100)
162 if (val
< (float)9.95)
163 (void) snprintf(str
, length
, "%1.1f", val
);
165 (void) snprintf(str
, length
, "%.0f", val
);
169 Format_num(char *str
, int num
, int length
)
172 (void) snprintf(str
, length
, ".%1dM", num
/100000);
175 (void) snprintf(str
, length
, "%2dK", num
/1000);
177 (void) snprintf(str
, length
, "%3d", num
);
182 Format_state(char *str
, char state
, processorid_t pr_id
, int length
)
186 (void) strncpy(str
, "sleep", length
);
189 (void) strncpy(str
, "run", length
);
192 (void) strncpy(str
, "zombie", length
);
195 (void) strncpy(str
, "stop", length
);
198 (void) strncpy(str
, "idle", length
);
201 (void) strncpy(str
, "wait", length
);
204 (void) snprintf(str
, length
, "cpu%-3d", (int)pr_id
);
207 (void) strncpy(str
, "?", length
);
213 Realloc(void *ptr
, size_t size
)
218 eagain
: if ((ptr
= realloc(ptr
, size
)))
221 if ((++cnt
<= 3) && (errno
== EAGAIN
)) {
222 Warn(gettext("realloc() failed, attempt %d"), cnt
);
223 (void) poll(NULL
, 0, 5000); /* wait for 5 seconds */
228 Die(gettext("not enough memory"));
230 return (NULL
); /* keep gcc happy */
236 return (Realloc(NULL
, size
));
242 return (memset(Realloc(NULL
, size
), 0, size
));
250 if (getrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
251 Die(gettext("getrlimit failed"));
252 fd_limit
= rlim
.rlim_cur
;
253 rlim
.rlim_max
= MIN(rlim
.rlim_max
, RLIMIT_NOFILE_MAX
);
254 rlim
.rlim_cur
= rlim
.rlim_max
;
255 (void) enable_extended_FILE_stdio(-1, -1);
256 if (setrlimit(RLIMIT_NOFILE
, &rlim
) == -1)
259 return (rlim
.rlim_cur
);
263 Priocntl(char *class)
267 (void) strcpy(pcinfo
.pc_clname
, class);
268 if (priocntl(0, 0, PC_GETCID
, (caddr_t
)&pcinfo
) == -1) {
269 Warn(gettext("cannot get real time class parameters"));
272 pcparms
.pc_cid
= pcinfo
.pc_cid
;
273 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_pri
= 0;
274 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqsecs
= 0;
275 ((rtparms_t
*)pcparms
.pc_clparms
)->rt_tqnsecs
= RT_NOCHANGE
;
276 if (priocntl(P_PID
, getpid(), PC_SETPARMS
, (caddr_t
)&pcparms
) == -1)
277 Warn(gettext("cannot enter the real time class"));
281 getprojname(projid_t projid
, char *str
, int len
, int noresolve
)
285 if (noresolve
|| getprojbyid(projid
, &proj
, projbuf
, PROJECT_BUFSZ
) ==
287 (void) snprintf(str
, len
, "%-6d", (int)projid
);
289 (void) snprintf(str
, len
, "%-28s", proj
.pj_name
);
293 getzonename(zoneid_t zoneid
, char *str
, int len
)
295 char zone_name
[ZONENAME_MAX
];
297 if (getzonenamebyid(zoneid
, zone_name
, sizeof (zone_name
)) < 0)
298 (void) snprintf(str
, len
, "%-6d", (int)zoneid
);
300 (void) snprintf(str
, len
, "%-28s", zone_name
);
304 * Remove all unprintable characters from process name
307 stripfname(char *buf
)
309 int bytesleft
= PRFNSZ
;
314 buf
[bytesleft
- 1] = '\0';
316 for (cp
= buf
; *cp
!= '\0'; cp
+= length
) {
317 length
= mbtowc(&wchar
, cp
, MB_LEN_MAX
);
322 if (!iswprint(wchar
)) {
323 if (bytesleft
<= length
) {
327 (void) memmove(cp
, cp
+ length
, bytesleft
- length
);