xref: /onnv-gate/usr/src/cmd/prstat/prutil.c (revision 9966:d4ba9662e3c8)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51914Scasper  * Common Development and Distribution License (the "License").
61914Scasper  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
229123Sjohn.levon@sun.com  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
24*9966SMenno.Lageman@Sun.COM  *
25*9966SMenno.Lageman@Sun.COM  * Portions Copyright 2009 Chad Mynhier
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/resource.h>
310Sstevel@tonic-gate #include <sys/priocntl.h>
320Sstevel@tonic-gate #include <sys/rtpriocntl.h>
330Sstevel@tonic-gate #include <sys/tspriocntl.h>
340Sstevel@tonic-gate #include <zone.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <libintl.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <wchar.h>
390Sstevel@tonic-gate #include <unistd.h>
400Sstevel@tonic-gate #include <string.h>
410Sstevel@tonic-gate #include <stdlib.h>
420Sstevel@tonic-gate #include <stdarg.h>
430Sstevel@tonic-gate #include <stdio.h>
441914Scasper #include <stdio_ext.h>
450Sstevel@tonic-gate #include <errno.h>
460Sstevel@tonic-gate #include <ctype.h>
470Sstevel@tonic-gate #include <poll.h>
480Sstevel@tonic-gate #include <project.h>
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #include "prfile.h"
510Sstevel@tonic-gate #include "prstat.h"
520Sstevel@tonic-gate #include "prutil.h"
530Sstevel@tonic-gate 
540Sstevel@tonic-gate static char PRG_FMT[] = "%s: ";
550Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n";
560Sstevel@tonic-gate static char *progname;
570Sstevel@tonic-gate static char projbuf[PROJECT_BUFSZ];
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	RLIMIT_NOFILE_MAX	32767
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*PRINTFLIKE1*/
620Sstevel@tonic-gate void
Warn(char * format,...)630Sstevel@tonic-gate Warn(char *format, ...)
640Sstevel@tonic-gate {
650Sstevel@tonic-gate 	int err = errno;
660Sstevel@tonic-gate 	va_list alist;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	if (progname != NULL)
690Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
700Sstevel@tonic-gate 	va_start(alist, format);
710Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
720Sstevel@tonic-gate 	va_end(alist);
730Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
740Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*PRINTFLIKE1*/
780Sstevel@tonic-gate void
Die(char * format,...)790Sstevel@tonic-gate Die(char *format, ...)
800Sstevel@tonic-gate {
810Sstevel@tonic-gate 	int err = errno;
820Sstevel@tonic-gate 	va_list alist;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 	if (progname != NULL)
850Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
860Sstevel@tonic-gate 	va_start(alist, format);
870Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
880Sstevel@tonic-gate 	va_end(alist);
890Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
900Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
910Sstevel@tonic-gate 	exit(1);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate 
940Sstevel@tonic-gate void
Progname(char * arg0)950Sstevel@tonic-gate Progname(char *arg0)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	char *p = strrchr(arg0, '/');
980Sstevel@tonic-gate 	if (p == NULL)
990Sstevel@tonic-gate 		p = arg0;
1000Sstevel@tonic-gate 	else
1010Sstevel@tonic-gate 		p++;
1020Sstevel@tonic-gate 	progname = p;
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate void
Usage()1060Sstevel@tonic-gate Usage()
1070Sstevel@tonic-gate {
1080Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
109*9966SMenno.Lageman@Sun.COM 	    "Usage:\tprstat [-acHJLmrRtTvZ] [-u euidlist] [-U uidlist]\n"
1102685Sakolb 	    "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
1110Sstevel@tonic-gate 	    "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
1129123Sjohn.levon@sun.com 	    "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
1130Sstevel@tonic-gate 	    "\t[interval [counter]]\n"));
1140Sstevel@tonic-gate 	exit(1);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate int
Atoi(char * p)1180Sstevel@tonic-gate Atoi(char *p)
1190Sstevel@tonic-gate {
1200Sstevel@tonic-gate 	int i;
1210Sstevel@tonic-gate 	char *q;
1220Sstevel@tonic-gate 	errno = 0;
1230Sstevel@tonic-gate 	i = (int)strtol(p, &q, 10);
1240Sstevel@tonic-gate 	if (errno != 0 || q == p || i < 0 || *q != '\0')
1250Sstevel@tonic-gate 		Die(gettext("illegal argument -- %s\n"), p);
1260Sstevel@tonic-gate 		/*NOTREACHED*/
1270Sstevel@tonic-gate 	else
1280Sstevel@tonic-gate 		return (i);
1290Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate void
Format_size(char * str,size_t size,int length)1330Sstevel@tonic-gate Format_size(char *str, size_t size, int length)
1340Sstevel@tonic-gate {
1350Sstevel@tonic-gate 	char tag = 'K';
1360Sstevel@tonic-gate 	if (size >= 10000) {
1370Sstevel@tonic-gate 		size = (size + 512) / 1024;
1380Sstevel@tonic-gate 		tag = 'M';
1390Sstevel@tonic-gate 		if (size >= 10000) {
1400Sstevel@tonic-gate 			size = (size + 512) / 1024;
1410Sstevel@tonic-gate 			tag = 'G';
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	(void) snprintf(str, length, "%4d%c", (int)size, tag);
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate void
Format_time(char * str,ulong_t time,int length)1480Sstevel@tonic-gate Format_time(char *str, ulong_t time, int length)
1490Sstevel@tonic-gate {
1500Sstevel@tonic-gate 	(void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
1510Sstevel@tonic-gate 	    (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate void
Format_pct(char * str,float val,int length)1550Sstevel@tonic-gate Format_pct(char *str, float val, int length)
1560Sstevel@tonic-gate {
1570Sstevel@tonic-gate 	if (val > (float)100)
1580Sstevel@tonic-gate 		val = 100;
1590Sstevel@tonic-gate 	if (val < 0)
1600Sstevel@tonic-gate 		val = 0;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 	if (val < (float)9.95)
1630Sstevel@tonic-gate 		(void) snprintf(str, length, "%1.1f", val);
1640Sstevel@tonic-gate 	else
1650Sstevel@tonic-gate 		(void) snprintf(str, length, "%.0f", val);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate void
Format_num(char * str,int num,int length)1690Sstevel@tonic-gate Format_num(char *str, int num, int length)
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	if (num >= 100000) {
1720Sstevel@tonic-gate 		(void) snprintf(str, length, ".%1dM", num/100000);
1730Sstevel@tonic-gate 	} else {
1740Sstevel@tonic-gate 		if (num >= 1000)
1750Sstevel@tonic-gate 			(void) snprintf(str, length, "%2dK", num/1000);
1760Sstevel@tonic-gate 		else
1770Sstevel@tonic-gate 			(void) snprintf(str, length, "%3d", num);
1780Sstevel@tonic-gate 	}
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate void
Format_state(char * str,char state,processorid_t pr_id,int length)1820Sstevel@tonic-gate Format_state(char *str, char state, processorid_t pr_id, int length)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate 	switch (state) {
1850Sstevel@tonic-gate 	case 'S':
1860Sstevel@tonic-gate 		(void) strncpy(str, "sleep", length);
1870Sstevel@tonic-gate 		break;
1880Sstevel@tonic-gate 	case 'R':
1890Sstevel@tonic-gate 		(void) strncpy(str, "run", length);
1900Sstevel@tonic-gate 		break;
1910Sstevel@tonic-gate 	case 'Z':
1920Sstevel@tonic-gate 		(void) strncpy(str, "zombie", length);
1930Sstevel@tonic-gate 		break;
1940Sstevel@tonic-gate 	case 'T':
1950Sstevel@tonic-gate 		(void) strncpy(str, "stop", length);
1960Sstevel@tonic-gate 		break;
1970Sstevel@tonic-gate 	case 'I':
1980Sstevel@tonic-gate 		(void) strncpy(str, "idle", length);
1990Sstevel@tonic-gate 		break;
2003792Sakolb 	case 'W':
2013792Sakolb 		(void) strncpy(str, "wait", length);
2020Sstevel@tonic-gate 		break;
2030Sstevel@tonic-gate 	case 'O':
2040Sstevel@tonic-gate 		(void) snprintf(str, length, "cpu%-3d", (int)pr_id);
2050Sstevel@tonic-gate 		break;
2060Sstevel@tonic-gate 	default:
2070Sstevel@tonic-gate 		(void) strncpy(str, "?", length);
2080Sstevel@tonic-gate 		break;
2090Sstevel@tonic-gate 	}
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate void *
Realloc(void * ptr,size_t size)2130Sstevel@tonic-gate Realloc(void *ptr, size_t size)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	int	cnt = 0;
2160Sstevel@tonic-gate 	void	*sav = ptr;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate eagain:	if ((ptr = realloc(ptr, size)))
2190Sstevel@tonic-gate 		return (ptr);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if ((++cnt <= 3) && (errno == EAGAIN)) {
2220Sstevel@tonic-gate 		Warn(gettext("realloc() failed, attempt %d"), cnt);
2230Sstevel@tonic-gate 		(void) poll(NULL, 0, 5000); /* wait for 5 seconds */
2240Sstevel@tonic-gate 		ptr = sav;
2250Sstevel@tonic-gate 		goto eagain;
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate 	ptr = sav;
2280Sstevel@tonic-gate 	Die(gettext("not enough memory"));
2290Sstevel@tonic-gate 	/*NOTREACHED*/
2300Sstevel@tonic-gate 	return (NULL);	/* keep gcc happy */
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate void *
Malloc(size_t size)2340Sstevel@tonic-gate Malloc(size_t size)
2350Sstevel@tonic-gate {
2360Sstevel@tonic-gate 	return (Realloc(NULL, size));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate void *
Zalloc(size_t size)2400Sstevel@tonic-gate Zalloc(size_t size)
2410Sstevel@tonic-gate {
2420Sstevel@tonic-gate 	return (memset(Realloc(NULL, size), 0, size));
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate int
Setrlimit()2460Sstevel@tonic-gate Setrlimit()
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	struct rlimit rlim;
2490Sstevel@tonic-gate 	int fd_limit;
2500Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
2510Sstevel@tonic-gate 		Die(gettext("getrlimit failed"));
2520Sstevel@tonic-gate 	fd_limit = rlim.rlim_cur;
2530Sstevel@tonic-gate 	rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
2540Sstevel@tonic-gate 	rlim.rlim_cur = rlim.rlim_max;
2551914Scasper 	(void) enable_extended_FILE_stdio(-1, -1);
2560Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
2570Sstevel@tonic-gate 		return (fd_limit);
2580Sstevel@tonic-gate 	else
2590Sstevel@tonic-gate 		return (rlim.rlim_cur);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate void
Priocntl(char * class)2630Sstevel@tonic-gate Priocntl(char *class)
2640Sstevel@tonic-gate {
2650Sstevel@tonic-gate 	pcinfo_t pcinfo;
2660Sstevel@tonic-gate 	pcparms_t pcparms;
2670Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, class);
2680Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
2690Sstevel@tonic-gate 		Warn(gettext("cannot get real time class parameters"));
2700Sstevel@tonic-gate 		return;
2710Sstevel@tonic-gate 	}
2720Sstevel@tonic-gate 	pcparms.pc_cid = pcinfo.pc_cid;
2730Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
2740Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
2750Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
2760Sstevel@tonic-gate 	if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
2770Sstevel@tonic-gate 		Warn(gettext("cannot enter the real time class"));
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate void
getprojname(projid_t projid,char * str,int len,int noresolve)281*9966SMenno.Lageman@Sun.COM getprojname(projid_t projid, char *str, int len, int noresolve)
2820Sstevel@tonic-gate {
2830Sstevel@tonic-gate 	struct project proj;
2840Sstevel@tonic-gate 
285*9966SMenno.Lageman@Sun.COM 	if (noresolve || getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) ==
286*9966SMenno.Lageman@Sun.COM 	    NULL)
287*9966SMenno.Lageman@Sun.COM 		(void) snprintf(str, len, "%-6d", (int)projid);
288*9966SMenno.Lageman@Sun.COM 	else
2890Sstevel@tonic-gate 		(void) snprintf(str, len, "%-28s", proj.pj_name);
2900Sstevel@tonic-gate }
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate void
getzonename(zoneid_t zoneid,char * str,int len)2930Sstevel@tonic-gate getzonename(zoneid_t zoneid, char *str, int len)
2940Sstevel@tonic-gate {
2950Sstevel@tonic-gate 	char zone_name[ZONENAME_MAX];
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0)
2980Sstevel@tonic-gate 		(void) snprintf(str, len, "%-6d", (int)zoneid);
2990Sstevel@tonic-gate 	else
3000Sstevel@tonic-gate 		(void) snprintf(str, len, "%-28s", zone_name);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Remove all unprintable characters from process name
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate void
stripfname(char * buf)3070Sstevel@tonic-gate stripfname(char *buf)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	int bytesleft = PRFNSZ;
3100Sstevel@tonic-gate 	wchar_t wchar;
3110Sstevel@tonic-gate 	int length;
3120Sstevel@tonic-gate 	char *cp;
3130Sstevel@tonic-gate 
3140Sstevel@tonic-gate 	buf[bytesleft - 1] = '\0';
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate 	for (cp = buf; *cp != '\0'; cp += length) {
3170Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
3180Sstevel@tonic-gate 		if (length <= 0) {
3190Sstevel@tonic-gate 			*cp = '\0';
3200Sstevel@tonic-gate 			break;
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 		if (!iswprint(wchar)) {
3230Sstevel@tonic-gate 			if (bytesleft <= length) {
3240Sstevel@tonic-gate 				*cp = '\0';
3250Sstevel@tonic-gate 				break;
3260Sstevel@tonic-gate 			}
3270Sstevel@tonic-gate 			(void) memmove(cp, cp + length, bytesleft - length);
3280Sstevel@tonic-gate 			length = 0;
3290Sstevel@tonic-gate 		}
3300Sstevel@tonic-gate 		bytesleft -= length;
3310Sstevel@tonic-gate 	}
3320Sstevel@tonic-gate }
333