xref: /freebsd/usr.bin/top/utils.c (revision 2f301637)
13be6ef06SEitan Adler /*
23be6ef06SEitan Adler  *  This program may be freely redistributed,
33be6ef06SEitan Adler  *  but this entire comment MUST remain intact.
43be6ef06SEitan Adler  *
5d1862666SEitan Adler  *  Copyright (c) 2018, Eitan Adler
63be6ef06SEitan Adler  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
73be6ef06SEitan Adler  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
83be6ef06SEitan Adler  *
93be6ef06SEitan Adler  * $FreeBSD$
103be6ef06SEitan Adler  */
113be6ef06SEitan Adler 
123be6ef06SEitan Adler /*
133be6ef06SEitan Adler  *  This file contains various handy utilities used by top.
143be6ef06SEitan Adler  */
153be6ef06SEitan Adler 
163be6ef06SEitan Adler #include "top.h"
17a5ca08edSEitan Adler #include "utils.h"
18caee4883SEitan Adler 
1951b29cb7SRoman Bogorodskiy #include <sys/param.h>
2051b29cb7SRoman Bogorodskiy #include <sys/sysctl.h>
2151b29cb7SRoman Bogorodskiy #include <sys/user.h>
2251b29cb7SRoman Bogorodskiy 
238e4b205eSEitan Adler #include <libutil.h>
24caee4883SEitan Adler #include <stdlib.h>
25caee4883SEitan Adler #include <stdio.h>
26caee4883SEitan Adler #include <string.h>
2751b29cb7SRoman Bogorodskiy #include <fcntl.h>
2851b29cb7SRoman Bogorodskiy #include <paths.h>
2951b29cb7SRoman Bogorodskiy #include <kvm.h>
3051b29cb7SRoman Bogorodskiy 
31b3c88c28SEitan Adler int
3266b3f031SEitan Adler atoiwi(const char *str)
333be6ef06SEitan Adler {
34d0bb69dcSEitan Adler     size_t len;
353be6ef06SEitan Adler 
363be6ef06SEitan Adler     len = strlen(str);
373be6ef06SEitan Adler     if (len != 0)
383be6ef06SEitan Adler     {
393be6ef06SEitan Adler 	if (strncmp(str, "infinity", len) == 0 ||
403be6ef06SEitan Adler 	    strncmp(str, "all",      len) == 0 ||
413be6ef06SEitan Adler 	    strncmp(str, "maximum",  len) == 0)
423be6ef06SEitan Adler 	{
433be6ef06SEitan Adler 	    return(Infinity);
443be6ef06SEitan Adler 	}
453be6ef06SEitan Adler 	else if (str[0] == '-')
463be6ef06SEitan Adler 	{
473be6ef06SEitan Adler 	    return(Invalid);
483be6ef06SEitan Adler 	}
493be6ef06SEitan Adler 	else
503be6ef06SEitan Adler 	{
51c655e639SEitan Adler 		return((int)strtol(str, NULL, 10));
523be6ef06SEitan Adler 	}
533be6ef06SEitan Adler     }
543be6ef06SEitan Adler     return(0);
553be6ef06SEitan Adler }
563be6ef06SEitan Adler 
573be6ef06SEitan Adler /*
583be6ef06SEitan Adler  *  itoa - convert integer (decimal) to ascii string for positive numbers
593be6ef06SEitan Adler  *  	   only (we don't bother with negative numbers since we know we
603be6ef06SEitan Adler  *	   don't use them).
613be6ef06SEitan Adler  */
623be6ef06SEitan Adler 
633be6ef06SEitan Adler 				/*
643be6ef06SEitan Adler 				 * How do we know that 16 will suffice?
653be6ef06SEitan Adler 				 * Because the biggest number that we will
663be6ef06SEitan Adler 				 * ever convert will be 2^32-1, which is 10
673be6ef06SEitan Adler 				 * digits.
683be6ef06SEitan Adler 				 */
693be6ef06SEitan Adler _Static_assert(sizeof(int) <= 4, "buffer too small for this sized int");
703be6ef06SEitan Adler 
71d408c8f7SEitan Adler char *
72d408c8f7SEitan Adler itoa(unsigned int val)
733be6ef06SEitan Adler {
743be6ef06SEitan Adler     static char buffer[16];	/* result is built here */
753be6ef06SEitan Adler     				/* 16 is sufficient since the largest number
763be6ef06SEitan Adler 				   we will ever convert will be 2^32-1,
773be6ef06SEitan Adler 				   which is 10 digits. */
783be6ef06SEitan Adler 
79704daa9bSEitan Adler 	sprintf(buffer, "%u", val);
80704daa9bSEitan Adler     return (buffer);
813be6ef06SEitan Adler }
823be6ef06SEitan Adler 
833be6ef06SEitan Adler /*
849f8096e3SEitan Adler  *  itoa7(val) - like itoa, except the number is right justified in a 7
853be6ef06SEitan Adler  *	character field.  This code is a duplication of itoa instead of
863be6ef06SEitan Adler  *	a front end to a more general routine for efficiency.
873be6ef06SEitan Adler  */
883be6ef06SEitan Adler 
89d408c8f7SEitan Adler char *
90d408c8f7SEitan Adler itoa7(int val)
913be6ef06SEitan Adler {
923be6ef06SEitan Adler     static char buffer[16];	/* result is built here */
933be6ef06SEitan Adler     				/* 16 is sufficient since the largest number
943be6ef06SEitan Adler 				   we will ever convert will be 2^32-1,
953be6ef06SEitan Adler 				   which is 10 digits. */
963be6ef06SEitan Adler 
97704daa9bSEitan Adler 	sprintf(buffer, "%6u", val);
98704daa9bSEitan Adler     return (buffer);
993be6ef06SEitan Adler }
1003be6ef06SEitan Adler 
1013be6ef06SEitan Adler /*
1023be6ef06SEitan Adler  *  digits(val) - return number of decimal digits in val.  Only works for
103ccf22059SEitan Adler  *	non-negative numbers.
1043be6ef06SEitan Adler  */
1053be6ef06SEitan Adler 
106fb7b896cSEitan Adler int __pure2
107d408c8f7SEitan Adler digits(int val)
1083be6ef06SEitan Adler {
10998c299e0SEitan Adler     int cnt = 0;
110fb7b896cSEitan Adler 	if (val == 0) {
111fb7b896cSEitan Adler 		return 1;
112fb7b896cSEitan Adler 	}
1133be6ef06SEitan Adler 
114fb7b896cSEitan Adler     while (val > 0) {
1153be6ef06SEitan Adler 		cnt++;
1163be6ef06SEitan Adler 		val /= 10;
1173be6ef06SEitan Adler     }
1183be6ef06SEitan Adler     return(cnt);
1193be6ef06SEitan Adler }
1203be6ef06SEitan Adler 
1213be6ef06SEitan Adler /*
1223be6ef06SEitan Adler  * string_index(string, array) - find string in array and return index
1233be6ef06SEitan Adler  */
1243be6ef06SEitan Adler 
125f7642433SEitan Adler int
126eae589f1SEitan Adler string_index(const char *string, const char * const *array)
1273be6ef06SEitan Adler {
128f6234b51SEitan Adler     size_t i = 0;
1293be6ef06SEitan Adler 
1303be6ef06SEitan Adler     while (*array != NULL)
1313be6ef06SEitan Adler     {
1323be6ef06SEitan Adler 	if (strcmp(string, *array) == 0)
1333be6ef06SEitan Adler 	{
1343be6ef06SEitan Adler 	    return(i);
1353be6ef06SEitan Adler 	}
1363be6ef06SEitan Adler 	array++;
1373be6ef06SEitan Adler 	i++;
1383be6ef06SEitan Adler     }
1393be6ef06SEitan Adler     return(-1);
1403be6ef06SEitan Adler }
1413be6ef06SEitan Adler 
1423be6ef06SEitan Adler /*
1433be6ef06SEitan Adler  * argparse(line, cntp) - parse arguments in string "line", separating them
1443be6ef06SEitan Adler  *	out into an argv-like array, and setting *cntp to the number of
1453be6ef06SEitan Adler  *	arguments encountered.  This is a simple parser that doesn't understand
1463be6ef06SEitan Adler  *	squat about quotes.
1473be6ef06SEitan Adler  */
1483be6ef06SEitan Adler 
149d0f687d3SDimitry Andric const char **
150a9a99372SEitan Adler argparse(char *line, int *cntp)
1513be6ef06SEitan Adler {
152a9a99372SEitan Adler     const char **ap;
153a9a99372SEitan Adler     static const char *argv[1024] = {0};
1543be6ef06SEitan Adler 
155a9a99372SEitan Adler     *cntp = 1;
156a9a99372SEitan Adler     ap = &argv[1];
157a9a99372SEitan Adler     while ((*ap = strsep(&line, " ")) != NULL) {
158a9a99372SEitan Adler         if (**ap != '\0') {
159a9a99372SEitan Adler             (*cntp)++;
160a9a99372SEitan Adler             if (*cntp >= (int)nitems(argv)) {
161a9a99372SEitan Adler                 break;
1623be6ef06SEitan Adler             }
163a9a99372SEitan Adler 	    ap++;
1643be6ef06SEitan Adler         }
1653be6ef06SEitan Adler     }
166d408c8f7SEitan Adler     return (argv);
1673be6ef06SEitan Adler }
1683be6ef06SEitan Adler 
1693be6ef06SEitan Adler /*
1703be6ef06SEitan Adler  *  percentages(cnt, out, new, old, diffs) - calculate percentage change
1713be6ef06SEitan Adler  *	between array "old" and "new", putting the percentages i "out".
1723be6ef06SEitan Adler  *	"cnt" is size of each array and "diffs" is used for scratch space.
1733be6ef06SEitan Adler  *	The array "old" is updated on each call.
1743be6ef06SEitan Adler  *	The routine assumes modulo arithmetic.  This function is especially
175f6234b51SEitan Adler  *	useful on for calculating cpu state percentages.
1763be6ef06SEitan Adler  */
1773be6ef06SEitan Adler 
178f7642433SEitan Adler long
179f6234b51SEitan Adler percentages(int cnt, int *out, long *new, long *old, long *diffs)
1803be6ef06SEitan Adler {
18198c299e0SEitan Adler     int i;
18298c299e0SEitan Adler     long change;
18398c299e0SEitan Adler     long total_change;
18498c299e0SEitan Adler     long *dp;
1853be6ef06SEitan Adler     long half_total;
1863be6ef06SEitan Adler 
1873be6ef06SEitan Adler     /* initialization */
1883be6ef06SEitan Adler     total_change = 0;
1893be6ef06SEitan Adler     dp = diffs;
1903be6ef06SEitan Adler 
1913be6ef06SEitan Adler     /* calculate changes for each state and the overall change */
1923be6ef06SEitan Adler     for (i = 0; i < cnt; i++)
1933be6ef06SEitan Adler     {
1943be6ef06SEitan Adler 	if ((change = *new - *old) < 0)
1953be6ef06SEitan Adler 	{
1963be6ef06SEitan Adler 	    /* this only happens when the counter wraps */
1973be6ef06SEitan Adler 	    change = (int)
1983be6ef06SEitan Adler 		((unsigned long)*new-(unsigned long)*old);
1993be6ef06SEitan Adler 	}
2003be6ef06SEitan Adler 	total_change += (*dp++ = change);
2013be6ef06SEitan Adler 	*old++ = *new++;
2023be6ef06SEitan Adler     }
2033be6ef06SEitan Adler 
2043be6ef06SEitan Adler     /* avoid divide by zero potential */
2053be6ef06SEitan Adler     if (total_change == 0)
2063be6ef06SEitan Adler     {
2073be6ef06SEitan Adler 	total_change = 1;
2083be6ef06SEitan Adler     }
2093be6ef06SEitan Adler 
2103be6ef06SEitan Adler     /* calculate percentages based on overall change, rounding up */
2113be6ef06SEitan Adler     half_total = total_change / 2l;
2123be6ef06SEitan Adler 
2133be6ef06SEitan Adler 	for (i = 0; i < cnt; i++)
2143be6ef06SEitan Adler 	{
2153be6ef06SEitan Adler 		*out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
2163be6ef06SEitan Adler 	}
2173be6ef06SEitan Adler 
2183be6ef06SEitan Adler     /* return the total in case the caller wants to use it */
2193be6ef06SEitan Adler     return(total_change);
2203be6ef06SEitan Adler }
2213be6ef06SEitan Adler 
2223be6ef06SEitan Adler /* format_time(seconds) - format number of seconds into a suitable
2233be6ef06SEitan Adler  *		display that will fit within 6 characters.  Note that this
2243be6ef06SEitan Adler  *		routine builds its string in a static area.  If it needs
2253be6ef06SEitan Adler  *		to be called more than once without overwriting previous data,
2263be6ef06SEitan Adler  *		then we will need to adopt a technique similar to the
2273be6ef06SEitan Adler  *		one used for format_k.
2283be6ef06SEitan Adler  */
2293be6ef06SEitan Adler 
2303be6ef06SEitan Adler /* Explanation:
2313be6ef06SEitan Adler    We want to keep the output within 6 characters.  For low values we use
2323be6ef06SEitan Adler    the format mm:ss.  For values that exceed 999:59, we switch to a format
2333be6ef06SEitan Adler    that displays hours and fractions:  hhh.tH.  For values that exceed
2343be6ef06SEitan Adler    999.9, we use hhhh.t and drop the "H" designator.  For values that
2353be6ef06SEitan Adler    exceed 9999.9, we use "???".
2363be6ef06SEitan Adler  */
2373be6ef06SEitan Adler 
2388e4b205eSEitan Adler const char *
239f6234b51SEitan Adler format_time(long seconds)
2403be6ef06SEitan Adler {
2413be6ef06SEitan Adler 	static char result[10];
2423be6ef06SEitan Adler 
2433be6ef06SEitan Adler 	/* sanity protection */
2443be6ef06SEitan Adler 	if (seconds < 0 || seconds > (99999l * 360l))
2453be6ef06SEitan Adler 	{
2463be6ef06SEitan Adler 		strcpy(result, "   ???");
2473be6ef06SEitan Adler 	}
2483be6ef06SEitan Adler 	else if (seconds >= (1000l * 60l))
2493be6ef06SEitan Adler 	{
2503be6ef06SEitan Adler 		/* alternate (slow) method displaying hours and tenths */
2513be6ef06SEitan Adler 		sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l));
2523be6ef06SEitan Adler 
2533be6ef06SEitan Adler 		/* It is possible that the sprintf took more than 6 characters.
2543be6ef06SEitan Adler 		   If so, then the "H" appears as result[6].  If not, then there
2553be6ef06SEitan Adler 		   is a \0 in result[6].  Either way, it is safe to step on.
2563be6ef06SEitan Adler 		   */
2573be6ef06SEitan Adler 		result[6] = '\0';
2583be6ef06SEitan Adler 	}
2593be6ef06SEitan Adler 	else
2603be6ef06SEitan Adler 	{
2613be6ef06SEitan Adler 		/* standard method produces MMM:SS */
2623be6ef06SEitan Adler 		sprintf(result, "%3ld:%02ld",
2638e4b205eSEitan Adler 				seconds / 60l, seconds % 60l);
2643be6ef06SEitan Adler 	}
2653be6ef06SEitan Adler 	return(result);
2663be6ef06SEitan Adler }
2673be6ef06SEitan Adler 
2683be6ef06SEitan Adler /*
2693be6ef06SEitan Adler  * format_k(amt) - format a kilobyte memory value, returning a string
2703be6ef06SEitan Adler  *		suitable for display.  Returns a pointer to a static
27151c834c4SEitan Adler  *		area that changes each call.  "amt" is converted to a fixed
27251c834c4SEitan Adler  *		size humanize_number call
2733be6ef06SEitan Adler  */
2743be6ef06SEitan Adler 
2753be6ef06SEitan Adler /*
2763be6ef06SEitan Adler  * Compromise time.  We need to return a string, but we don't want the
2773be6ef06SEitan Adler  * caller to have to worry about freeing a dynamically allocated string.
2783be6ef06SEitan Adler  * Unfortunately, we can't just return a pointer to a static area as one
2793be6ef06SEitan Adler  * of the common uses of this function is in a large call to sprintf where
2803be6ef06SEitan Adler  * it might get invoked several times.  Our compromise is to maintain an
2813be6ef06SEitan Adler  * array of strings and cycle thru them with each invocation.  We make the
2823be6ef06SEitan Adler  * array large enough to handle the above mentioned case.  The constant
2833be6ef06SEitan Adler  * NUM_STRINGS defines the number of strings in this array:  we can tolerate
2843be6ef06SEitan Adler  * up to NUM_STRINGS calls before we start overwriting old information.
2853be6ef06SEitan Adler  * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
2863be6ef06SEitan Adler  * to convert the modulo operation into something quicker.  What a hack!
2873be6ef06SEitan Adler  */
2883be6ef06SEitan Adler 
2893be6ef06SEitan Adler #define NUM_STRINGS 8
2903be6ef06SEitan Adler 
291d408c8f7SEitan Adler char *
2928e4b205eSEitan Adler format_k(int64_t amt)
2933be6ef06SEitan Adler {
2943be6ef06SEitan Adler     static char retarray[NUM_STRINGS][16];
2952f301637SDimitry Andric     static int index_ = 0;
29698c299e0SEitan Adler     char *ret;
2973be6ef06SEitan Adler 
2982f301637SDimitry Andric     ret = retarray[index_];
2992f301637SDimitry Andric 	index_ = (index_ + 1) % NUM_STRINGS;
30010550f0bSEitan Adler 	humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE);
3013be6ef06SEitan Adler 	return (ret);
3023be6ef06SEitan Adler }
30351b29cb7SRoman Bogorodskiy 
30451b29cb7SRoman Bogorodskiy int
30551b29cb7SRoman Bogorodskiy find_pid(pid_t pid)
30651b29cb7SRoman Bogorodskiy {
30751b29cb7SRoman Bogorodskiy 	kvm_t *kd = NULL;
30851b29cb7SRoman Bogorodskiy 	struct kinfo_proc *pbase = NULL;
30951b29cb7SRoman Bogorodskiy 	int nproc;
31051b29cb7SRoman Bogorodskiy 	int ret = 0;
31151b29cb7SRoman Bogorodskiy 
31251b29cb7SRoman Bogorodskiy 	kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
31351b29cb7SRoman Bogorodskiy 	if (kd == NULL) {
31451b29cb7SRoman Bogorodskiy 		fprintf(stderr, "top: kvm_open() failed.\n");
31551b29cb7SRoman Bogorodskiy 		quit(TOP_EX_SYS_ERROR);
31651b29cb7SRoman Bogorodskiy 	}
31751b29cb7SRoman Bogorodskiy 
31851b29cb7SRoman Bogorodskiy 	pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc);
31951b29cb7SRoman Bogorodskiy 	if (pbase == NULL) {
32051b29cb7SRoman Bogorodskiy 		goto done;
32151b29cb7SRoman Bogorodskiy 	}
32251b29cb7SRoman Bogorodskiy 
32351b29cb7SRoman Bogorodskiy 	if ((nproc == 1) && (pbase->ki_pid == pid)) {
32451b29cb7SRoman Bogorodskiy 		ret = 1;
32551b29cb7SRoman Bogorodskiy 	}
32651b29cb7SRoman Bogorodskiy 
32751b29cb7SRoman Bogorodskiy done:
32851b29cb7SRoman Bogorodskiy 	kvm_close(kd);
32951b29cb7SRoman Bogorodskiy 	return ret;
33051b29cb7SRoman Bogorodskiy }
331