1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 30*7c478bd9Sstevel@tonic-gate #include <sys/param.h> 31*7c478bd9Sstevel@tonic-gate #include <sys/resource.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/priocntl.h> 33*7c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h> 35*7c478bd9Sstevel@tonic-gate #include <zone.h> 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #include <libintl.h> 38*7c478bd9Sstevel@tonic-gate #include <limits.h> 39*7c478bd9Sstevel@tonic-gate #include <wchar.h> 40*7c478bd9Sstevel@tonic-gate #include <unistd.h> 41*7c478bd9Sstevel@tonic-gate #include <string.h> 42*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 43*7c478bd9Sstevel@tonic-gate #include <stdarg.h> 44*7c478bd9Sstevel@tonic-gate #include <stdio.h> 45*7c478bd9Sstevel@tonic-gate #include <errno.h> 46*7c478bd9Sstevel@tonic-gate #include <ctype.h> 47*7c478bd9Sstevel@tonic-gate #include <poll.h> 48*7c478bd9Sstevel@tonic-gate #include <project.h> 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate #include "prfile.h" 51*7c478bd9Sstevel@tonic-gate #include "prstat.h" 52*7c478bd9Sstevel@tonic-gate #include "prutil.h" 53*7c478bd9Sstevel@tonic-gate 54*7c478bd9Sstevel@tonic-gate static char PRG_FMT[] = "%s: "; 55*7c478bd9Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n"; 56*7c478bd9Sstevel@tonic-gate static char *progname; 57*7c478bd9Sstevel@tonic-gate static char projbuf[PROJECT_BUFSZ]; 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate #define RLIMIT_NOFILE_MAX 32767 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 62*7c478bd9Sstevel@tonic-gate void 63*7c478bd9Sstevel@tonic-gate Warn(char *format, ...) 64*7c478bd9Sstevel@tonic-gate { 65*7c478bd9Sstevel@tonic-gate int err = errno; 66*7c478bd9Sstevel@tonic-gate va_list alist; 67*7c478bd9Sstevel@tonic-gate 68*7c478bd9Sstevel@tonic-gate if (progname != NULL) 69*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PRG_FMT, progname); 70*7c478bd9Sstevel@tonic-gate va_start(alist, format); 71*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 72*7c478bd9Sstevel@tonic-gate va_end(alist); 73*7c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL) 74*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_FMT), strerror(err)); 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 78*7c478bd9Sstevel@tonic-gate void 79*7c478bd9Sstevel@tonic-gate Die(char *format, ...) 80*7c478bd9Sstevel@tonic-gate { 81*7c478bd9Sstevel@tonic-gate int err = errno; 82*7c478bd9Sstevel@tonic-gate va_list alist; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if (progname != NULL) 85*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, PRG_FMT, progname); 86*7c478bd9Sstevel@tonic-gate va_start(alist, format); 87*7c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 88*7c478bd9Sstevel@tonic-gate va_end(alist); 89*7c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL) 90*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERR_FMT), strerror(err)); 91*7c478bd9Sstevel@tonic-gate exit(1); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate void 95*7c478bd9Sstevel@tonic-gate Progname(char *arg0) 96*7c478bd9Sstevel@tonic-gate { 97*7c478bd9Sstevel@tonic-gate char *p = strrchr(arg0, '/'); 98*7c478bd9Sstevel@tonic-gate if (p == NULL) 99*7c478bd9Sstevel@tonic-gate p = arg0; 100*7c478bd9Sstevel@tonic-gate else 101*7c478bd9Sstevel@tonic-gate p++; 102*7c478bd9Sstevel@tonic-gate progname = p; 103*7c478bd9Sstevel@tonic-gate } 104*7c478bd9Sstevel@tonic-gate 105*7c478bd9Sstevel@tonic-gate void 106*7c478bd9Sstevel@tonic-gate Usage() 107*7c478bd9Sstevel@tonic-gate { 108*7c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext( 109*7c478bd9Sstevel@tonic-gate "Usage:\tprstat [-acJLmRtTvZ] [-u euidlist] [-U uidlist]\n" 110*7c478bd9Sstevel@tonic-gate "\t[-p pidlist] [-P cpulist] [-C psrsetlist]\n" 111*7c478bd9Sstevel@tonic-gate "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n" 112*7c478bd9Sstevel@tonic-gate "\t[-s key | -S key] [-n nprocs[,nusers]]\n" 113*7c478bd9Sstevel@tonic-gate "\t[interval [counter]]\n")); 114*7c478bd9Sstevel@tonic-gate exit(1); 115*7c478bd9Sstevel@tonic-gate } 116*7c478bd9Sstevel@tonic-gate 117*7c478bd9Sstevel@tonic-gate int 118*7c478bd9Sstevel@tonic-gate Atoi(char *p) 119*7c478bd9Sstevel@tonic-gate { 120*7c478bd9Sstevel@tonic-gate int i; 121*7c478bd9Sstevel@tonic-gate char *q; 122*7c478bd9Sstevel@tonic-gate errno = 0; 123*7c478bd9Sstevel@tonic-gate i = (int)strtol(p, &q, 10); 124*7c478bd9Sstevel@tonic-gate if (errno != 0 || q == p || i < 0 || *q != '\0') 125*7c478bd9Sstevel@tonic-gate Die(gettext("illegal argument -- %s\n"), p); 126*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 127*7c478bd9Sstevel@tonic-gate else 128*7c478bd9Sstevel@tonic-gate return (i); 129*7c478bd9Sstevel@tonic-gate return (0); /* keep gcc happy */ 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate void 133*7c478bd9Sstevel@tonic-gate Format_size(char *str, size_t size, int length) 134*7c478bd9Sstevel@tonic-gate { 135*7c478bd9Sstevel@tonic-gate char tag = 'K'; 136*7c478bd9Sstevel@tonic-gate if (size >= 10000) { 137*7c478bd9Sstevel@tonic-gate size = (size + 512) / 1024; 138*7c478bd9Sstevel@tonic-gate tag = 'M'; 139*7c478bd9Sstevel@tonic-gate if (size >= 10000) { 140*7c478bd9Sstevel@tonic-gate size = (size + 512) / 1024; 141*7c478bd9Sstevel@tonic-gate tag = 'G'; 142*7c478bd9Sstevel@tonic-gate } 143*7c478bd9Sstevel@tonic-gate } 144*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%4d%c", (int)size, tag); 145*7c478bd9Sstevel@tonic-gate } 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate void 148*7c478bd9Sstevel@tonic-gate Format_time(char *str, ulong_t time, int length) 149*7c478bd9Sstevel@tonic-gate { 150*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */ 151*7c478bd9Sstevel@tonic-gate (int)time/3600, (int)(time % 3600)/60, (int)time % 60); 152*7c478bd9Sstevel@tonic-gate } 153*7c478bd9Sstevel@tonic-gate 154*7c478bd9Sstevel@tonic-gate void 155*7c478bd9Sstevel@tonic-gate Format_pct(char *str, float val, int length) 156*7c478bd9Sstevel@tonic-gate { 157*7c478bd9Sstevel@tonic-gate if (val > (float)100) 158*7c478bd9Sstevel@tonic-gate val = 100; 159*7c478bd9Sstevel@tonic-gate if (val < 0) 160*7c478bd9Sstevel@tonic-gate val = 0; 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate if (val < (float)9.95) 163*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%1.1f", val); 164*7c478bd9Sstevel@tonic-gate else 165*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%.0f", val); 166*7c478bd9Sstevel@tonic-gate } 167*7c478bd9Sstevel@tonic-gate 168*7c478bd9Sstevel@tonic-gate void 169*7c478bd9Sstevel@tonic-gate Format_num(char *str, int num, int length) 170*7c478bd9Sstevel@tonic-gate { 171*7c478bd9Sstevel@tonic-gate if (num >= 100000) { 172*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, ".%1dM", num/100000); 173*7c478bd9Sstevel@tonic-gate } else { 174*7c478bd9Sstevel@tonic-gate if (num >= 1000) 175*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%2dK", num/1000); 176*7c478bd9Sstevel@tonic-gate else 177*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "%3d", num); 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate void 182*7c478bd9Sstevel@tonic-gate Format_state(char *str, char state, processorid_t pr_id, int length) 183*7c478bd9Sstevel@tonic-gate { 184*7c478bd9Sstevel@tonic-gate switch (state) { 185*7c478bd9Sstevel@tonic-gate case 'S': 186*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "sleep", length); 187*7c478bd9Sstevel@tonic-gate break; 188*7c478bd9Sstevel@tonic-gate case 'R': 189*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "run", length); 190*7c478bd9Sstevel@tonic-gate break; 191*7c478bd9Sstevel@tonic-gate case 'Z': 192*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "zombie", length); 193*7c478bd9Sstevel@tonic-gate break; 194*7c478bd9Sstevel@tonic-gate case 'T': 195*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "stop", length); 196*7c478bd9Sstevel@tonic-gate break; 197*7c478bd9Sstevel@tonic-gate case 'I': 198*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "idle", length); 199*7c478bd9Sstevel@tonic-gate break; 200*7c478bd9Sstevel@tonic-gate case 'X': 201*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "xbrk", length); 202*7c478bd9Sstevel@tonic-gate break; 203*7c478bd9Sstevel@tonic-gate case 'O': 204*7c478bd9Sstevel@tonic-gate (void) snprintf(str, length, "cpu%-3d", (int)pr_id); 205*7c478bd9Sstevel@tonic-gate break; 206*7c478bd9Sstevel@tonic-gate default: 207*7c478bd9Sstevel@tonic-gate (void) strncpy(str, "?", length); 208*7c478bd9Sstevel@tonic-gate break; 209*7c478bd9Sstevel@tonic-gate } 210*7c478bd9Sstevel@tonic-gate } 211*7c478bd9Sstevel@tonic-gate 212*7c478bd9Sstevel@tonic-gate void * 213*7c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size) 214*7c478bd9Sstevel@tonic-gate { 215*7c478bd9Sstevel@tonic-gate int cnt = 0; 216*7c478bd9Sstevel@tonic-gate void *sav = ptr; 217*7c478bd9Sstevel@tonic-gate 218*7c478bd9Sstevel@tonic-gate eagain: if ((ptr = realloc(ptr, size))) 219*7c478bd9Sstevel@tonic-gate return (ptr); 220*7c478bd9Sstevel@tonic-gate 221*7c478bd9Sstevel@tonic-gate if ((++cnt <= 3) && (errno == EAGAIN)) { 222*7c478bd9Sstevel@tonic-gate Warn(gettext("realloc() failed, attempt %d"), cnt); 223*7c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, 5000); /* wait for 5 seconds */ 224*7c478bd9Sstevel@tonic-gate ptr = sav; 225*7c478bd9Sstevel@tonic-gate goto eagain; 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate ptr = sav; 228*7c478bd9Sstevel@tonic-gate Die(gettext("not enough memory")); 229*7c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 230*7c478bd9Sstevel@tonic-gate return (NULL); /* keep gcc happy */ 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate void * 234*7c478bd9Sstevel@tonic-gate Malloc(size_t size) 235*7c478bd9Sstevel@tonic-gate { 236*7c478bd9Sstevel@tonic-gate return (Realloc(NULL, size)); 237*7c478bd9Sstevel@tonic-gate } 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate void * 240*7c478bd9Sstevel@tonic-gate Zalloc(size_t size) 241*7c478bd9Sstevel@tonic-gate { 242*7c478bd9Sstevel@tonic-gate return (memset(Realloc(NULL, size), 0, size)); 243*7c478bd9Sstevel@tonic-gate } 244*7c478bd9Sstevel@tonic-gate 245*7c478bd9Sstevel@tonic-gate int 246*7c478bd9Sstevel@tonic-gate Setrlimit() 247*7c478bd9Sstevel@tonic-gate { 248*7c478bd9Sstevel@tonic-gate struct rlimit rlim; 249*7c478bd9Sstevel@tonic-gate int fd_limit; 250*7c478bd9Sstevel@tonic-gate if (getrlimit(RLIMIT_NOFILE, &rlim) == -1) 251*7c478bd9Sstevel@tonic-gate Die(gettext("getrlimit failed")); 252*7c478bd9Sstevel@tonic-gate fd_limit = rlim.rlim_cur; 253*7c478bd9Sstevel@tonic-gate rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX); 254*7c478bd9Sstevel@tonic-gate rlim.rlim_cur = rlim.rlim_max; 255*7c478bd9Sstevel@tonic-gate if (setrlimit(RLIMIT_NOFILE, &rlim) == -1) 256*7c478bd9Sstevel@tonic-gate return (fd_limit); 257*7c478bd9Sstevel@tonic-gate else 258*7c478bd9Sstevel@tonic-gate return (rlim.rlim_cur); 259*7c478bd9Sstevel@tonic-gate } 260*7c478bd9Sstevel@tonic-gate 261*7c478bd9Sstevel@tonic-gate void 262*7c478bd9Sstevel@tonic-gate Priocntl(char *class) 263*7c478bd9Sstevel@tonic-gate { 264*7c478bd9Sstevel@tonic-gate pcinfo_t pcinfo; 265*7c478bd9Sstevel@tonic-gate pcparms_t pcparms; 266*7c478bd9Sstevel@tonic-gate (void) strcpy(pcinfo.pc_clname, class); 267*7c478bd9Sstevel@tonic-gate if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) { 268*7c478bd9Sstevel@tonic-gate Warn(gettext("cannot get real time class parameters")); 269*7c478bd9Sstevel@tonic-gate return; 270*7c478bd9Sstevel@tonic-gate } 271*7c478bd9Sstevel@tonic-gate pcparms.pc_cid = pcinfo.pc_cid; 272*7c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0; 273*7c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0; 274*7c478bd9Sstevel@tonic-gate ((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE; 275*7c478bd9Sstevel@tonic-gate if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1) 276*7c478bd9Sstevel@tonic-gate Warn(gettext("cannot enter the real time class")); 277*7c478bd9Sstevel@tonic-gate } 278*7c478bd9Sstevel@tonic-gate 279*7c478bd9Sstevel@tonic-gate void 280*7c478bd9Sstevel@tonic-gate getprojname(projid_t projid, char *str, int len) 281*7c478bd9Sstevel@tonic-gate { 282*7c478bd9Sstevel@tonic-gate struct project proj; 283*7c478bd9Sstevel@tonic-gate 284*7c478bd9Sstevel@tonic-gate if (getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) != NULL) 285*7c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-28s", proj.pj_name); 286*7c478bd9Sstevel@tonic-gate else 287*7c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-6d", (int)projid); 288*7c478bd9Sstevel@tonic-gate } 289*7c478bd9Sstevel@tonic-gate 290*7c478bd9Sstevel@tonic-gate void 291*7c478bd9Sstevel@tonic-gate getzonename(zoneid_t zoneid, char *str, int len) 292*7c478bd9Sstevel@tonic-gate { 293*7c478bd9Sstevel@tonic-gate char zone_name[ZONENAME_MAX]; 294*7c478bd9Sstevel@tonic-gate 295*7c478bd9Sstevel@tonic-gate if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0) 296*7c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-6d", (int)zoneid); 297*7c478bd9Sstevel@tonic-gate else 298*7c478bd9Sstevel@tonic-gate (void) snprintf(str, len, "%-28s", zone_name); 299*7c478bd9Sstevel@tonic-gate } 300*7c478bd9Sstevel@tonic-gate 301*7c478bd9Sstevel@tonic-gate /* 302*7c478bd9Sstevel@tonic-gate * Remove all unprintable characters from process name 303*7c478bd9Sstevel@tonic-gate */ 304*7c478bd9Sstevel@tonic-gate void 305*7c478bd9Sstevel@tonic-gate stripfname(char *buf) 306*7c478bd9Sstevel@tonic-gate { 307*7c478bd9Sstevel@tonic-gate int bytesleft = PRFNSZ; 308*7c478bd9Sstevel@tonic-gate wchar_t wchar; 309*7c478bd9Sstevel@tonic-gate int length; 310*7c478bd9Sstevel@tonic-gate char *cp; 311*7c478bd9Sstevel@tonic-gate 312*7c478bd9Sstevel@tonic-gate buf[bytesleft - 1] = '\0'; 313*7c478bd9Sstevel@tonic-gate 314*7c478bd9Sstevel@tonic-gate for (cp = buf; *cp != '\0'; cp += length) { 315*7c478bd9Sstevel@tonic-gate length = mbtowc(&wchar, cp, MB_LEN_MAX); 316*7c478bd9Sstevel@tonic-gate if (length <= 0) { 317*7c478bd9Sstevel@tonic-gate *cp = '\0'; 318*7c478bd9Sstevel@tonic-gate break; 319*7c478bd9Sstevel@tonic-gate } 320*7c478bd9Sstevel@tonic-gate if (!iswprint(wchar)) { 321*7c478bd9Sstevel@tonic-gate if (bytesleft <= length) { 322*7c478bd9Sstevel@tonic-gate *cp = '\0'; 323*7c478bd9Sstevel@tonic-gate break; 324*7c478bd9Sstevel@tonic-gate } 325*7c478bd9Sstevel@tonic-gate (void) memmove(cp, cp + length, bytesleft - length); 326*7c478bd9Sstevel@tonic-gate length = 0; 327*7c478bd9Sstevel@tonic-gate } 328*7c478bd9Sstevel@tonic-gate bytesleft -= length; 329*7c478bd9Sstevel@tonic-gate } 330*7c478bd9Sstevel@tonic-gate } 331