xref: /titanic_50/usr/src/cmd/prstat/prutil.c (revision 4944376cd5de3dcd3b4feeaad9cbedbc024d1474)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5004388ebScasper  * Common Development and Distribution License (the "License").
6004388ebScasper  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*4944376cSJohn Levon  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/resource.h>
297c478bd9Sstevel@tonic-gate #include <sys/priocntl.h>
307c478bd9Sstevel@tonic-gate #include <sys/rtpriocntl.h>
317c478bd9Sstevel@tonic-gate #include <sys/tspriocntl.h>
327c478bd9Sstevel@tonic-gate #include <zone.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <limits.h>
367c478bd9Sstevel@tonic-gate #include <wchar.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <stdlib.h>
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #include <stdio.h>
42004388ebScasper #include <stdio_ext.h>
437c478bd9Sstevel@tonic-gate #include <errno.h>
447c478bd9Sstevel@tonic-gate #include <ctype.h>
457c478bd9Sstevel@tonic-gate #include <poll.h>
467c478bd9Sstevel@tonic-gate #include <project.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #include "prfile.h"
497c478bd9Sstevel@tonic-gate #include "prstat.h"
507c478bd9Sstevel@tonic-gate #include "prutil.h"
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate static char PRG_FMT[] = "%s: ";
537c478bd9Sstevel@tonic-gate static char ERR_FMT[] = ": %s\n";
547c478bd9Sstevel@tonic-gate static char *progname;
557c478bd9Sstevel@tonic-gate static char projbuf[PROJECT_BUFSZ];
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #define	RLIMIT_NOFILE_MAX	32767
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
607c478bd9Sstevel@tonic-gate void
617c478bd9Sstevel@tonic-gate Warn(char *format, ...)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	int err = errno;
647c478bd9Sstevel@tonic-gate 	va_list alist;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	if (progname != NULL)
677c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
687c478bd9Sstevel@tonic-gate 	va_start(alist, format);
697c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
707c478bd9Sstevel@tonic-gate 	va_end(alist);
717c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
727c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
737c478bd9Sstevel@tonic-gate }
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/
767c478bd9Sstevel@tonic-gate void
777c478bd9Sstevel@tonic-gate Die(char *format, ...)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	int err = errno;
807c478bd9Sstevel@tonic-gate 	va_list alist;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	if (progname != NULL)
837c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, PRG_FMT, progname);
847c478bd9Sstevel@tonic-gate 	va_start(alist, format);
857c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
867c478bd9Sstevel@tonic-gate 	va_end(alist);
877c478bd9Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
887c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERR_FMT), strerror(err));
897c478bd9Sstevel@tonic-gate 	exit(1);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate void
937c478bd9Sstevel@tonic-gate Progname(char *arg0)
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate 	char *p = strrchr(arg0, '/');
967c478bd9Sstevel@tonic-gate 	if (p == NULL)
977c478bd9Sstevel@tonic-gate 		p = arg0;
987c478bd9Sstevel@tonic-gate 	else
997c478bd9Sstevel@tonic-gate 		p++;
1007c478bd9Sstevel@tonic-gate 	progname = p;
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate void
1047c478bd9Sstevel@tonic-gate Usage()
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
107c6402783Sakolb 	    "Usage:\tprstat [-acHJLmRtTvZ] [-u euidlist] [-U uidlist]\n"
108c6402783Sakolb 	    "\t[-p pidlist] [-P cpulist] [-C psrsetlist] [-h lgrouplist]\n"
1097c478bd9Sstevel@tonic-gate 	    "\t[-j projidlist] [-k taskidlist] [-z zoneidlist]\n"
110*4944376cSJohn Levon 	    "\t[-s key | -S key] [-n nprocs[,nusers]] [-d d|u]\n"
1117c478bd9Sstevel@tonic-gate 	    "\t[interval [counter]]\n"));
1127c478bd9Sstevel@tonic-gate 	exit(1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate int
1167c478bd9Sstevel@tonic-gate Atoi(char *p)
1177c478bd9Sstevel@tonic-gate {
1187c478bd9Sstevel@tonic-gate 	int i;
1197c478bd9Sstevel@tonic-gate 	char *q;
1207c478bd9Sstevel@tonic-gate 	errno = 0;
1217c478bd9Sstevel@tonic-gate 	i = (int)strtol(p, &q, 10);
1227c478bd9Sstevel@tonic-gate 	if (errno != 0 || q == p || i < 0 || *q != '\0')
1237c478bd9Sstevel@tonic-gate 		Die(gettext("illegal argument -- %s\n"), p);
1247c478bd9Sstevel@tonic-gate 		/*NOTREACHED*/
1257c478bd9Sstevel@tonic-gate 	else
1267c478bd9Sstevel@tonic-gate 		return (i);
1277c478bd9Sstevel@tonic-gate 	return (0);	/* keep gcc happy */
1287c478bd9Sstevel@tonic-gate }
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate void
1317c478bd9Sstevel@tonic-gate Format_size(char *str, size_t size, int length)
1327c478bd9Sstevel@tonic-gate {
1337c478bd9Sstevel@tonic-gate 	char tag = 'K';
1347c478bd9Sstevel@tonic-gate 	if (size >= 10000) {
1357c478bd9Sstevel@tonic-gate 		size = (size + 512) / 1024;
1367c478bd9Sstevel@tonic-gate 		tag = 'M';
1377c478bd9Sstevel@tonic-gate 		if (size >= 10000) {
1387c478bd9Sstevel@tonic-gate 			size = (size + 512) / 1024;
1397c478bd9Sstevel@tonic-gate 			tag = 'G';
1407c478bd9Sstevel@tonic-gate 		}
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 	(void) snprintf(str, length, "%4d%c", (int)size, tag);
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate void
1467c478bd9Sstevel@tonic-gate Format_time(char *str, ulong_t time, int length)
1477c478bd9Sstevel@tonic-gate {
1487c478bd9Sstevel@tonic-gate 	(void) snprintf(str, length, gettext("%3d:%2.2d:%2.2d"), /* hr:mm:ss */
1497c478bd9Sstevel@tonic-gate 	    (int)time/3600, (int)(time % 3600)/60, (int)time % 60);
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate void
1537c478bd9Sstevel@tonic-gate Format_pct(char *str, float val, int length)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	if (val > (float)100)
1567c478bd9Sstevel@tonic-gate 		val = 100;
1577c478bd9Sstevel@tonic-gate 	if (val < 0)
1587c478bd9Sstevel@tonic-gate 		val = 0;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (val < (float)9.95)
1617c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "%1.1f", val);
1627c478bd9Sstevel@tonic-gate 	else
1637c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "%.0f", val);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate void
1677c478bd9Sstevel@tonic-gate Format_num(char *str, int num, int length)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	if (num >= 100000) {
1707c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, ".%1dM", num/100000);
1717c478bd9Sstevel@tonic-gate 	} else {
1727c478bd9Sstevel@tonic-gate 		if (num >= 1000)
1737c478bd9Sstevel@tonic-gate 			(void) snprintf(str, length, "%2dK", num/1000);
1747c478bd9Sstevel@tonic-gate 		else
1757c478bd9Sstevel@tonic-gate 			(void) snprintf(str, length, "%3d", num);
1767c478bd9Sstevel@tonic-gate 	}
1777c478bd9Sstevel@tonic-gate }
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate void
1807c478bd9Sstevel@tonic-gate Format_state(char *str, char state, processorid_t pr_id, int length)
1817c478bd9Sstevel@tonic-gate {
1827c478bd9Sstevel@tonic-gate 	switch (state) {
1837c478bd9Sstevel@tonic-gate 	case 'S':
1847c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "sleep", length);
1857c478bd9Sstevel@tonic-gate 		break;
1867c478bd9Sstevel@tonic-gate 	case 'R':
1877c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "run", length);
1887c478bd9Sstevel@tonic-gate 		break;
1897c478bd9Sstevel@tonic-gate 	case 'Z':
1907c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "zombie", length);
1917c478bd9Sstevel@tonic-gate 		break;
1927c478bd9Sstevel@tonic-gate 	case 'T':
1937c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "stop", length);
1947c478bd9Sstevel@tonic-gate 		break;
1957c478bd9Sstevel@tonic-gate 	case 'I':
1967c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "idle", length);
1977c478bd9Sstevel@tonic-gate 		break;
198c97ad5cdSakolb 	case 'W':
199c97ad5cdSakolb 		(void) strncpy(str, "wait", length);
2007c478bd9Sstevel@tonic-gate 		break;
2017c478bd9Sstevel@tonic-gate 	case 'O':
2027c478bd9Sstevel@tonic-gate 		(void) snprintf(str, length, "cpu%-3d", (int)pr_id);
2037c478bd9Sstevel@tonic-gate 		break;
2047c478bd9Sstevel@tonic-gate 	default:
2057c478bd9Sstevel@tonic-gate 		(void) strncpy(str, "?", length);
2067c478bd9Sstevel@tonic-gate 		break;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate void *
2117c478bd9Sstevel@tonic-gate Realloc(void *ptr, size_t size)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	int	cnt = 0;
2147c478bd9Sstevel@tonic-gate 	void	*sav = ptr;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate eagain:	if ((ptr = realloc(ptr, size)))
2177c478bd9Sstevel@tonic-gate 		return (ptr);
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if ((++cnt <= 3) && (errno == EAGAIN)) {
2207c478bd9Sstevel@tonic-gate 		Warn(gettext("realloc() failed, attempt %d"), cnt);
2217c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, 5000); /* wait for 5 seconds */
2227c478bd9Sstevel@tonic-gate 		ptr = sav;
2237c478bd9Sstevel@tonic-gate 		goto eagain;
2247c478bd9Sstevel@tonic-gate 	}
2257c478bd9Sstevel@tonic-gate 	ptr = sav;
2267c478bd9Sstevel@tonic-gate 	Die(gettext("not enough memory"));
2277c478bd9Sstevel@tonic-gate 	/*NOTREACHED*/
2287c478bd9Sstevel@tonic-gate 	return (NULL);	/* keep gcc happy */
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate void *
2327c478bd9Sstevel@tonic-gate Malloc(size_t size)
2337c478bd9Sstevel@tonic-gate {
2347c478bd9Sstevel@tonic-gate 	return (Realloc(NULL, size));
2357c478bd9Sstevel@tonic-gate }
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate void *
2387c478bd9Sstevel@tonic-gate Zalloc(size_t size)
2397c478bd9Sstevel@tonic-gate {
2407c478bd9Sstevel@tonic-gate 	return (memset(Realloc(NULL, size), 0, size));
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate int
2447c478bd9Sstevel@tonic-gate Setrlimit()
2457c478bd9Sstevel@tonic-gate {
2467c478bd9Sstevel@tonic-gate 	struct rlimit rlim;
2477c478bd9Sstevel@tonic-gate 	int fd_limit;
2487c478bd9Sstevel@tonic-gate 	if (getrlimit(RLIMIT_NOFILE, &rlim) == -1)
2497c478bd9Sstevel@tonic-gate 		Die(gettext("getrlimit failed"));
2507c478bd9Sstevel@tonic-gate 	fd_limit = rlim.rlim_cur;
2517c478bd9Sstevel@tonic-gate 	rlim.rlim_max = MIN(rlim.rlim_max, RLIMIT_NOFILE_MAX);
2527c478bd9Sstevel@tonic-gate 	rlim.rlim_cur = rlim.rlim_max;
253004388ebScasper 	(void) enable_extended_FILE_stdio(-1, -1);
2547c478bd9Sstevel@tonic-gate 	if (setrlimit(RLIMIT_NOFILE, &rlim) == -1)
2557c478bd9Sstevel@tonic-gate 		return (fd_limit);
2567c478bd9Sstevel@tonic-gate 	else
2577c478bd9Sstevel@tonic-gate 		return (rlim.rlim_cur);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate void
2617c478bd9Sstevel@tonic-gate Priocntl(char *class)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	pcinfo_t pcinfo;
2647c478bd9Sstevel@tonic-gate 	pcparms_t pcparms;
2657c478bd9Sstevel@tonic-gate 	(void) strcpy(pcinfo.pc_clname, class);
2667c478bd9Sstevel@tonic-gate 	if (priocntl(0, 0, PC_GETCID, (caddr_t)&pcinfo) == -1) {
2677c478bd9Sstevel@tonic-gate 		Warn(gettext("cannot get real time class parameters"));
2687c478bd9Sstevel@tonic-gate 		return;
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate 	pcparms.pc_cid = pcinfo.pc_cid;
2717c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_pri = 0;
2727c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqsecs = 0;
2737c478bd9Sstevel@tonic-gate 	((rtparms_t *)pcparms.pc_clparms)->rt_tqnsecs = RT_NOCHANGE;
2747c478bd9Sstevel@tonic-gate 	if (priocntl(P_PID, getpid(), PC_SETPARMS, (caddr_t)&pcparms) == -1)
2757c478bd9Sstevel@tonic-gate 		Warn(gettext("cannot enter the real time class"));
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate void
2797c478bd9Sstevel@tonic-gate getprojname(projid_t projid, char *str, int len)
2807c478bd9Sstevel@tonic-gate {
2817c478bd9Sstevel@tonic-gate 	struct project proj;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 	if (getprojbyid(projid, &proj, projbuf, PROJECT_BUFSZ) != NULL)
2847c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-28s", proj.pj_name);
2857c478bd9Sstevel@tonic-gate 	else
2867c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-6d", (int)projid);
2877c478bd9Sstevel@tonic-gate }
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate void
2907c478bd9Sstevel@tonic-gate getzonename(zoneid_t zoneid, char *str, int len)
2917c478bd9Sstevel@tonic-gate {
2927c478bd9Sstevel@tonic-gate 	char zone_name[ZONENAME_MAX];
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (getzonenamebyid(zoneid, zone_name, sizeof (zone_name)) < 0)
2957c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-6d", (int)zoneid);
2967c478bd9Sstevel@tonic-gate 	else
2977c478bd9Sstevel@tonic-gate 		(void) snprintf(str, len, "%-28s", zone_name);
2987c478bd9Sstevel@tonic-gate }
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate /*
3017c478bd9Sstevel@tonic-gate  * Remove all unprintable characters from process name
3027c478bd9Sstevel@tonic-gate  */
3037c478bd9Sstevel@tonic-gate void
3047c478bd9Sstevel@tonic-gate stripfname(char *buf)
3057c478bd9Sstevel@tonic-gate {
3067c478bd9Sstevel@tonic-gate 	int bytesleft = PRFNSZ;
3077c478bd9Sstevel@tonic-gate 	wchar_t wchar;
3087c478bd9Sstevel@tonic-gate 	int length;
3097c478bd9Sstevel@tonic-gate 	char *cp;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	buf[bytesleft - 1] = '\0';
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate 	for (cp = buf; *cp != '\0'; cp += length) {
3147c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, cp, MB_LEN_MAX);
3157c478bd9Sstevel@tonic-gate 		if (length <= 0) {
3167c478bd9Sstevel@tonic-gate 			*cp = '\0';
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 		if (!iswprint(wchar)) {
3207c478bd9Sstevel@tonic-gate 			if (bytesleft <= length) {
3217c478bd9Sstevel@tonic-gate 				*cp = '\0';
3227c478bd9Sstevel@tonic-gate 				break;
3237c478bd9Sstevel@tonic-gate 			}
3247c478bd9Sstevel@tonic-gate 			(void) memmove(cp, cp + length, bytesleft - length);
3257c478bd9Sstevel@tonic-gate 			length = 0;
3267c478bd9Sstevel@tonic-gate 		}
3277c478bd9Sstevel@tonic-gate 		bytesleft -= length;
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate }
330