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