Print this page
8158 Want named threads API
9857 proc manpages should have LIBRARY section
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/prstat/prutil.c
+++ new/usr/src/cmd/prstat/prutil.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 2013 Gary Mills
23 23 *
24 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
25 25 * Use is subject to license terms.
26 26 *
27 27 * Portions Copyright 2009 Chad Mynhier
28 + * Copyright 2018 Joyent, Inc. All rights reserved.
28 29 */
29 30
30 31 #include <sys/types.h>
31 32 #include <sys/param.h>
32 33 #include <sys/resource.h>
33 34 #include <sys/priocntl.h>
34 35 #include <sys/rtpriocntl.h>
35 36 #include <sys/tspriocntl.h>
36 37 #include <zone.h>
37 38
38 39 #include <libintl.h>
39 40 #include <limits.h>
40 41 #include <wchar.h>
41 42 #include <unistd.h>
42 43 #include <string.h>
43 44 #include <stdlib.h>
44 45 #include <stdarg.h>
45 46 #include <stdio.h>
46 47 #include <stdio_ext.h>
47 48 #include <errno.h>
48 49 #include <ctype.h>
49 50 #include <poll.h>
50 51 #include <project.h>
51 52
52 53 #include "prfile.h"
53 54 #include "prstat.h"
54 55 #include "prutil.h"
55 56
56 57 static char PRG_FMT[] = "%s: ";
57 58 static char ERR_FMT[] = ": %s\n";
58 59 static char *progname;
59 60 static char projbuf[PROJECT_BUFSZ];
60 61
61 62 #define RLIMIT_NOFILE_MAX 32767
62 63
63 64 /*PRINTFLIKE1*/
64 65 void
65 66 Warn(char *format, ...)
66 67 {
67 68 int err = errno;
68 69 va_list alist;
69 70
70 71 if (progname != NULL)
71 72 (void) fprintf(stderr, PRG_FMT, progname);
72 73 va_start(alist, format);
73 74 (void) vfprintf(stderr, format, alist);
74 75 va_end(alist);
75 76 if (strchr(format, '\n') == NULL)
76 77 (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
77 78 }
78 79
79 80 /*PRINTFLIKE1*/
80 81 void
81 82 Die(char *format, ...)
82 83 {
83 84 int err = errno;
84 85 va_list alist;
85 86
86 87 if (progname != NULL)
87 88 (void) fprintf(stderr, PRG_FMT, progname);
88 89 va_start(alist, format);
89 90 (void) vfprintf(stderr, format, alist);
90 91 va_end(alist);
91 92 if (strchr(format, '\n') == NULL)
92 93 (void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
93 94 exit(1);
94 95 }
95 96
96 97 void
97 98 Progname(char *arg0)
98 99 {
99 100 char *p = strrchr(arg0, '/');
100 101 if (p == NULL)
101 102 p = arg0;
102 103 else
103 104 p++;
104 105 progname = p;
105 106 }
106 107
107 108 void
108 109 Usage()
109 110 {
110 111 (void) fprintf(stderr, gettext(
111 112 "Usage:\tprstat [-acHJLmrRtTvWZ] [-u euidlist] [-U uidlist]\n"
112 113 "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
113 114 "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
114 115 "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
115 116 "\t[interval [counter]]\n"));
116 117 exit(1);
117 118 }
118 119
119 120 int
120 121 Atoi(char *p)
121 122 {
122 123 int i;
123 124 char *q;
124 125 errno = 0;
125 126 i = (int)strtol(p, &q, 10);
126 127 if (errno != 0 || q == p || i < 0 || *q != '\0')
127 128 Die(gettext("illegal argument -- %s\n"), p);
128 129 /*NOTREACHED*/
129 130 else
130 131 return (i);
131 132 return (0); /* keep gcc happy */
132 133 }
133 134
134 135 void
135 136 Format_size(char *str, size_t size, int length)
136 137 {
137 138 char tag = 'K';
138 139 if (size >= 10000) {
139 140 size = (size + 512) / 1024;
140 141 tag = 'M';
141 142 if (size >= 10000) {
142 143 size = (size + 512) / 1024;
143 144 tag = 'G';
144 145 }
145 146 }
146 147 (void) snprintf(str, length, "%4d%c", (int)size, tag);
147 148 }
148 149
149 150 void
150 151 Format_time(char *str, ulong_t time, int length)
151 152 {
152 153 (void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
153 154 (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
154 155 }
155 156
156 157 void
157 158 Format_pct(char *str, float val, int length)
158 159 {
159 160 if (val > (float)100)
160 161 val = 100;
161 162 if (val < 0)
162 163 val = 0;
163 164
164 165 if (val < (float)9.95)
165 166 (void) snprintf(str, length, "%1.1f", val);
166 167 else
167 168 (void) snprintf(str, length, "%.0f", val);
168 169 }
169 170
170 171 void
171 172 Format_num(char *str, int num, int length)
172 173 {
173 174 if (num >= 100000) {
174 175 (void) snprintf(str, length, ".%1dM", num/100000);
175 176 } else {
176 177 if (num >= 1000)
177 178 (void) snprintf(str, length, "%2dK", num/1000);
178 179 else
179 180 (void) snprintf(str, length, "%3d", num);
180 181 }
181 182 }
182 183
183 184 void
184 185 Format_state(char *str, char state, processorid_t pr_id, int length)
185 186 {
186 187 switch (state) {
187 188 case 'S':
188 189 (void) strncpy(str, "sleep", length);
189 190 break;
190 191 case 'R':
191 192 (void) strncpy(str, "run", length);
192 193 break;
193 194 case 'Z':
194 195 (void) strncpy(str, "zombie", length);
195 196 break;
196 197 case 'T':
197 198 (void) strncpy(str, "stop", length);
198 199 break;
199 200 case 'I':
200 201 (void) strncpy(str, "idle", length);
201 202 break;
202 203 case 'W':
203 204 (void) strncpy(str, "wait", length);
204 205 break;
205 206 case 'O':
206 207 (void) snprintf(str, length, "cpu%-3d", (int)pr_id);
207 208 break;
208 209 default:
209 210 (void) strncpy(str, "?", length);
210 211 break;
211 212 }
212 213 }
213 214
214 215 void *
215 216 Realloc(void *ptr, size_t size)
216 217 {
217 218 int cnt = 0;
218 219 void *sav = ptr;
219 220
220 221 eagain: if ((ptr = realloc(ptr, size)))
221 222 return (ptr);
222 223
223 224 if ((++cnt <= 3) && (errno == EAGAIN)) {
224 225 Warn(gettext("realloc() failed, attempt %d"), cnt);
225 226 (void) poll(NULL, 0, 5000); /* wait for 5 seconds */
226 227 ptr = sav;
227 228 goto eagain;
228 229 }
229 230 ptr = sav;
230 231 Die(gettext("not enough memory"));
231 232 /*NOTREACHED*/
232 233 return (NULL); /* keep gcc happy */
233 234 }
234 235
235 236 void *
236 237 Malloc(size_t size)
237 238 {
238 239 return (Realloc(NULL, size));
239 240 }
240 241
241 242 void *
242 243 Zalloc(size_t size)
243 244 {
244 245 return (memset(Realloc(NULL, size), 0, size));
245 246 }
246 247
247 248 int
248 249 Setrlimit()
249 250 {
250 251 struct rlimit rlim;
251 252 int fd_limit;
252 253 if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
253 254 Die(gettext("getrlimit failed"));
254 255 fd_limit = rlim.rlim_cur;
255 256 rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
256 257 rlim.rlim_cur = rlim.rlim_max;
257 258 (void) enable_extended_FILE_stdio(-1, -1);
258 259 if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
259 260 return (fd_limit);
260 261 else
261 262 return (rlim.rlim_cur);
262 263 }
263 264
264 265 void
265 266 Priocntl(char *class)
266 267 {
267 268 pcinfo_t pcinfo;
268 269 pcparms_t pcparms;
269 270 (void) strcpy(pcinfo.pc_clname, class);
270 271 if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
271 272 Warn(gettext("cannot get real time class parameters"));
272 273 return;
273 274 }
274 275 pcparms.pc_cid = pcinfo.pc_cid;
275 276 ((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
276 277 ((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
277 278 ((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
278 279 if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
279 280 Warn(gettext("cannot enter the real time class"));
280 281 }
281 282
282 283 void
283 284 getprojname(projid_t projid, char *str, size_t len, int noresolve,
284 285 int trunc, size_t width)
285 286 {
286 287 struct project proj;
287 288 size_t n;
288 289
289 290 if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) ==
290 291 NULL) {
291 292 (void) snprintf(str, len, "%-6d", (int)projid);
292 293 } else {
293 294 n = mbstowcs(NULL, proj.pj_name, 0);
294 295 if (n == (size_t)-1)
295 296 (void) snprintf(str, len, "%-28s", "ERROR");
296 297 else if (trunc && n > width)
297 298 (void) snprintf(str, len, "%.*s%c", width - 1,
298 299 proj.pj_name, '*');
299 300 else
300 301 (void) snprintf(str, len, "%-28s", proj.pj_name);
301 302 }
302 303 }
303 304
304 305 void
305 306 getzonename(zoneid_t zoneid, char *str, size_t len, int trunc, size_t width)
306 307 {
307 308 char zone_name[ZONENAME_MAX];
308 309 size_t n;
309 310
310 311 if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0) {
311 312 (void) snprintf(str, len, "%-6d", (int)zoneid);
312 313 } else {
313 314 n = mbstowcs(NULL, zone_name, 0);
314 315 if (n == (size_t)-1)
315 316 (void) snprintf(str, len, "%-28s", "ERROR");
316 317 else if (trunc && n > width)
↓ open down ↓ |
279 lines elided |
↑ open up ↑ |
317 318 (void) snprintf(str, len, "%.*s%c", width - 1,
318 319 zone_name, '*');
319 320 else
320 321 (void) snprintf(str, len, "%-28s", zone_name);
321 322 }
322 323 }
323 324
324 325 /*
325 326 * Remove all unprintable characters from process name
326 327 */
327 -void
328 -stripfname(char *buf)
328 +static void
329 +stripfname(char *buf, size_t bufsize, const char *pname)
329 330 {
330 331 int bytesleft = PRFNSZ;
331 332 wchar_t wchar;
332 333 int length;
333 334 char *cp;
334 335
336 + (void) strlcpy(buf, pname, bufsize);
337 +
335 338 buf[bytesleft - 1] = '\0';
336 339
337 340 for (cp = buf; *cp != '\0'; cp += length) {
338 341 length = mbtowc(&wchar, cp, MB_LEN_MAX);
339 342 if (length <= 0) {
340 343 *cp = '\0';
341 344 break;
342 345 }
343 346 if (!iswprint(wchar)) {
344 347 if (bytesleft <= length) {
345 348 *cp = '\0';
346 349 break;
347 350 }
348 351 (void) memmove(cp, cp + length, bytesleft - length);
349 352 length = 0;
350 353 }
351 354 bytesleft -= length;
352 355 }
356 +}
357 +
358 +
359 +/*
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.
364 + *
365 + * We never truncate if using -c, or not outputting to a TTY.
366 + */
367 +static int
368 +format_namewidth(void)
369 +{
370 + int prefixlen = 0;
371 +
372 + if (opts.o_cols == 0 || !(opts.o_outpmode & (OPT_TERMCAP | OPT_TRUNC)))
373 + return (0);
374 +
375 + if (opts.o_outpmode & OPT_PSINFO) {
376 + if (opts.o_outpmode & OPT_LGRP)
377 + prefixlen = 64;
378 + else
379 + prefixlen = 59;
380 + } else if (opts.o_outpmode & OPT_MSACCT) {
381 + prefixlen = 64;
382 + }
383 +
384 + return (opts.o_cols - prefixlen);
385 +}
386 +
387 +void
388 +format_name(lwp_info_t *lwp, char *buf, size_t buflen)
389 +{
390 + int pname_width = PRFNSZ;
391 + char nr_suffix[20];
392 + char pname[PRFNSZ];
393 + int width;
394 + int n;
395 +
396 + stripfname(pname, sizeof (pname), lwp->li_info.pr_fname);
397 +
398 + if (opts.o_outpmode & OPT_LWPS) {
399 + n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
400 + lwp->li_info.pr_lwp.pr_lwpid);
401 + } else {
402 + n = snprintf(nr_suffix, sizeof (nr_suffix), "%d",
403 + lwp->li_info.pr_nlwp + lwp->li_info.pr_nzomb);
404 + }
405 +
406 + width = format_namewidth();
407 +
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] = '*';
412 + }
413 +
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);
417 + } else {
418 + n = snprintf(buf, buflen, "%.*s/%s", pname_width,
419 + pname, nr_suffix);
420 + }
421 +
422 + if (width > 0 && strlen(buf) > width)
423 + buf[width] = '\0';
353 424 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX