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