xref: /freebsd/usr.bin/top/utils.c (revision 8e4b205ea5c6a58189c6e82c27c61a694b1d7182)
13be6ef06SEitan Adler /*
23be6ef06SEitan Adler  *  This program may be freely redistributed,
33be6ef06SEitan Adler  *  but this entire comment MUST remain intact.
43be6ef06SEitan Adler  *
53be6ef06SEitan Adler  *  Copyright (c) 1984, 1989, William LeFebvre, Rice University
63be6ef06SEitan Adler  *  Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
73be6ef06SEitan Adler  *
83be6ef06SEitan Adler  * $FreeBSD$
93be6ef06SEitan Adler  */
103be6ef06SEitan Adler 
113be6ef06SEitan Adler /*
123be6ef06SEitan Adler  *  This file contains various handy utilities used by top.
133be6ef06SEitan Adler  */
143be6ef06SEitan Adler 
153be6ef06SEitan Adler #include "top.h"
16a5ca08edSEitan Adler #include "utils.h"
17caee4883SEitan Adler 
1851b29cb7SRoman Bogorodskiy #include <sys/param.h>
1951b29cb7SRoman Bogorodskiy #include <sys/sysctl.h>
2051b29cb7SRoman Bogorodskiy #include <sys/user.h>
2151b29cb7SRoman Bogorodskiy 
22*8e4b205eSEitan Adler #include <libutil.h>
23caee4883SEitan Adler #include <stdlib.h>
24caee4883SEitan Adler #include <stdio.h>
25caee4883SEitan Adler #include <string.h>
2651b29cb7SRoman Bogorodskiy #include <fcntl.h>
2751b29cb7SRoman Bogorodskiy #include <paths.h>
2851b29cb7SRoman Bogorodskiy #include <kvm.h>
2951b29cb7SRoman Bogorodskiy 
30b3c88c28SEitan Adler int
3166b3f031SEitan Adler atoiwi(const char *str)
323be6ef06SEitan Adler {
33d0bb69dcSEitan Adler     size_t len;
343be6ef06SEitan Adler 
353be6ef06SEitan Adler     len = strlen(str);
363be6ef06SEitan Adler     if (len != 0)
373be6ef06SEitan Adler     {
383be6ef06SEitan Adler 	if (strncmp(str, "infinity", len) == 0 ||
393be6ef06SEitan Adler 	    strncmp(str, "all",      len) == 0 ||
403be6ef06SEitan Adler 	    strncmp(str, "maximum",  len) == 0)
413be6ef06SEitan Adler 	{
423be6ef06SEitan Adler 	    return(Infinity);
433be6ef06SEitan Adler 	}
443be6ef06SEitan Adler 	else if (str[0] == '-')
453be6ef06SEitan Adler 	{
463be6ef06SEitan Adler 	    return(Invalid);
473be6ef06SEitan Adler 	}
483be6ef06SEitan Adler 	else
493be6ef06SEitan Adler 	{
503be6ef06SEitan Adler 	    return(atoi(str));
513be6ef06SEitan Adler 	}
523be6ef06SEitan Adler     }
533be6ef06SEitan Adler     return(0);
543be6ef06SEitan Adler }
553be6ef06SEitan Adler 
563be6ef06SEitan Adler /*
573be6ef06SEitan Adler  *  itoa - convert integer (decimal) to ascii string for positive numbers
583be6ef06SEitan Adler  *  	   only (we don't bother with negative numbers since we know we
593be6ef06SEitan Adler  *	   don't use them).
603be6ef06SEitan Adler  */
613be6ef06SEitan Adler 
623be6ef06SEitan Adler 				/*
633be6ef06SEitan Adler 				 * How do we know that 16 will suffice?
643be6ef06SEitan Adler 				 * Because the biggest number that we will
653be6ef06SEitan Adler 				 * ever convert will be 2^32-1, which is 10
663be6ef06SEitan Adler 				 * digits.
673be6ef06SEitan Adler 				 */
683be6ef06SEitan Adler _Static_assert(sizeof(int) <= 4, "buffer too small for this sized int");
693be6ef06SEitan Adler 
70d408c8f7SEitan Adler char *
71d408c8f7SEitan Adler itoa(unsigned int val)
723be6ef06SEitan Adler {
7398c299e0SEitan Adler     char *ptr;
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 
793be6ef06SEitan Adler     ptr = buffer + sizeof(buffer);
803be6ef06SEitan Adler     *--ptr = '\0';
813be6ef06SEitan Adler     if (val == 0)
823be6ef06SEitan Adler     {
833be6ef06SEitan Adler 	*--ptr = '0';
843be6ef06SEitan Adler     }
853be6ef06SEitan Adler     else while (val != 0)
863be6ef06SEitan Adler     {
873be6ef06SEitan Adler 	*--ptr = (val % 10) + '0';
883be6ef06SEitan Adler 	val /= 10;
893be6ef06SEitan Adler     }
903be6ef06SEitan Adler     return(ptr);
913be6ef06SEitan Adler }
923be6ef06SEitan Adler 
933be6ef06SEitan Adler /*
949f8096e3SEitan Adler  *  itoa7(val) - like itoa, except the number is right justified in a 7
953be6ef06SEitan Adler  *	character field.  This code is a duplication of itoa instead of
963be6ef06SEitan Adler  *	a front end to a more general routine for efficiency.
973be6ef06SEitan Adler  */
983be6ef06SEitan Adler 
99d408c8f7SEitan Adler char *
100d408c8f7SEitan Adler itoa7(int val)
1013be6ef06SEitan Adler {
10298c299e0SEitan Adler     char *ptr;
1033be6ef06SEitan Adler     static char buffer[16];	/* result is built here */
1043be6ef06SEitan Adler     				/* 16 is sufficient since the largest number
1053be6ef06SEitan Adler 				   we will ever convert will be 2^32-1,
1063be6ef06SEitan Adler 				   which is 10 digits. */
1073be6ef06SEitan Adler 
1083be6ef06SEitan Adler     ptr = buffer + sizeof(buffer);
1093be6ef06SEitan Adler     *--ptr = '\0';
1103be6ef06SEitan Adler     if (val == 0)
1113be6ef06SEitan Adler     {
1123be6ef06SEitan Adler 	*--ptr = '0';
1133be6ef06SEitan Adler     }
1143be6ef06SEitan Adler     else while (val != 0)
1153be6ef06SEitan Adler     {
1163be6ef06SEitan Adler 	*--ptr = (val % 10) + '0';
1173be6ef06SEitan Adler 	val /= 10;
1183be6ef06SEitan Adler     }
1193be6ef06SEitan Adler     while (ptr > buffer + sizeof(buffer) - 7)
1203be6ef06SEitan Adler     {
1213be6ef06SEitan Adler 	*--ptr = ' ';
1223be6ef06SEitan Adler     }
1233be6ef06SEitan Adler     return(ptr);
1243be6ef06SEitan Adler }
1253be6ef06SEitan Adler 
1263be6ef06SEitan Adler /*
1273be6ef06SEitan Adler  *  digits(val) - return number of decimal digits in val.  Only works for
128ccf22059SEitan Adler  *	non-negative numbers.
1293be6ef06SEitan Adler  */
1303be6ef06SEitan Adler 
131fb7b896cSEitan Adler int __pure2
132d408c8f7SEitan Adler digits(int val)
1333be6ef06SEitan Adler {
13498c299e0SEitan Adler     int cnt = 0;
135fb7b896cSEitan Adler 	if (val == 0) {
136fb7b896cSEitan Adler 		return 1;
137fb7b896cSEitan Adler 	}
1383be6ef06SEitan Adler 
139fb7b896cSEitan Adler     while (val > 0) {
1403be6ef06SEitan Adler 		cnt++;
1413be6ef06SEitan Adler 		val /= 10;
1423be6ef06SEitan Adler     }
1433be6ef06SEitan Adler     return(cnt);
1443be6ef06SEitan Adler }
1453be6ef06SEitan Adler 
1463be6ef06SEitan Adler /*
1473be6ef06SEitan Adler  * string_index(string, array) - find string in array and return index
1483be6ef06SEitan Adler  */
1493be6ef06SEitan Adler 
150f6234b51SEitan Adler int
151eae589f1SEitan Adler string_index(const char *string, const char * const *array)
1523be6ef06SEitan Adler {
153f6234b51SEitan Adler     size_t i = 0;
1543be6ef06SEitan Adler 
1553be6ef06SEitan Adler     while (*array != NULL)
1563be6ef06SEitan Adler     {
1573be6ef06SEitan Adler 	if (strcmp(string, *array) == 0)
1583be6ef06SEitan Adler 	{
1593be6ef06SEitan Adler 	    return(i);
1603be6ef06SEitan Adler 	}
1613be6ef06SEitan Adler 	array++;
1623be6ef06SEitan Adler 	i++;
1633be6ef06SEitan Adler     }
1643be6ef06SEitan Adler     return(-1);
1653be6ef06SEitan Adler }
1663be6ef06SEitan Adler 
1673be6ef06SEitan Adler /*
1683be6ef06SEitan Adler  * argparse(line, cntp) - parse arguments in string "line", separating them
1693be6ef06SEitan Adler  *	out into an argv-like array, and setting *cntp to the number of
1703be6ef06SEitan Adler  *	arguments encountered.  This is a simple parser that doesn't understand
1713be6ef06SEitan Adler  *	squat about quotes.
1723be6ef06SEitan Adler  */
1733be6ef06SEitan Adler 
174eae589f1SEitan Adler const char * const *
175a9a99372SEitan Adler argparse(char *line, int *cntp)
1763be6ef06SEitan Adler {
177a9a99372SEitan Adler     const char **ap;
178a9a99372SEitan Adler     static const char *argv[1024] = {0};
1793be6ef06SEitan Adler 
180a9a99372SEitan Adler     *cntp = 1;
181a9a99372SEitan Adler     ap = &argv[1];
182a9a99372SEitan Adler     while ((*ap = strsep(&line, " ")) != NULL) {
183a9a99372SEitan Adler         if (**ap != '\0') {
184a9a99372SEitan Adler             (*cntp)++;
185a9a99372SEitan Adler             if (*cntp >= (int)nitems(argv)) {
186a9a99372SEitan Adler                 break;
1873be6ef06SEitan Adler             }
188a9a99372SEitan Adler 	    ap++;
1893be6ef06SEitan Adler         }
1903be6ef06SEitan Adler     }
191d408c8f7SEitan Adler     return (argv);
1923be6ef06SEitan Adler }
1933be6ef06SEitan Adler 
1943be6ef06SEitan Adler /*
1953be6ef06SEitan Adler  *  percentages(cnt, out, new, old, diffs) - calculate percentage change
1963be6ef06SEitan Adler  *	between array "old" and "new", putting the percentages i "out".
1973be6ef06SEitan Adler  *	"cnt" is size of each array and "diffs" is used for scratch space.
1983be6ef06SEitan Adler  *	The array "old" is updated on each call.
1993be6ef06SEitan Adler  *	The routine assumes modulo arithmetic.  This function is especially
200f6234b51SEitan Adler  *	useful on for calculating cpu state percentages.
2013be6ef06SEitan Adler  */
2023be6ef06SEitan Adler 
203f6234b51SEitan Adler long
204f6234b51SEitan Adler percentages(int cnt, int *out, long *new, long *old, long *diffs)
2053be6ef06SEitan Adler {
20698c299e0SEitan Adler     int i;
20798c299e0SEitan Adler     long change;
20898c299e0SEitan Adler     long total_change;
20998c299e0SEitan Adler     long *dp;
2103be6ef06SEitan Adler     long half_total;
2113be6ef06SEitan Adler 
2123be6ef06SEitan Adler     /* initialization */
2133be6ef06SEitan Adler     total_change = 0;
2143be6ef06SEitan Adler     dp = diffs;
2153be6ef06SEitan Adler 
2163be6ef06SEitan Adler     /* calculate changes for each state and the overall change */
2173be6ef06SEitan Adler     for (i = 0; i < cnt; i++)
2183be6ef06SEitan Adler     {
2193be6ef06SEitan Adler 	if ((change = *new - *old) < 0)
2203be6ef06SEitan Adler 	{
2213be6ef06SEitan Adler 	    /* this only happens when the counter wraps */
2223be6ef06SEitan Adler 	    change = (int)
2233be6ef06SEitan Adler 		((unsigned long)*new-(unsigned long)*old);
2243be6ef06SEitan Adler 	}
2253be6ef06SEitan Adler 	total_change += (*dp++ = change);
2263be6ef06SEitan Adler 	*old++ = *new++;
2273be6ef06SEitan Adler     }
2283be6ef06SEitan Adler 
2293be6ef06SEitan Adler     /* avoid divide by zero potential */
2303be6ef06SEitan Adler     if (total_change == 0)
2313be6ef06SEitan Adler     {
2323be6ef06SEitan Adler 	total_change = 1;
2333be6ef06SEitan Adler     }
2343be6ef06SEitan Adler 
2353be6ef06SEitan Adler     /* calculate percentages based on overall change, rounding up */
2363be6ef06SEitan Adler     half_total = total_change / 2l;
2373be6ef06SEitan Adler 
2383be6ef06SEitan Adler     /* Do not divide by 0. Causes Floating point exception */
2393be6ef06SEitan Adler     if(total_change) {
2403be6ef06SEitan Adler         for (i = 0; i < cnt; i++)
2413be6ef06SEitan Adler         {
2423be6ef06SEitan Adler           *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
2433be6ef06SEitan Adler         }
2443be6ef06SEitan Adler     }
2453be6ef06SEitan Adler 
2463be6ef06SEitan Adler     /* return the total in case the caller wants to use it */
2473be6ef06SEitan Adler     return(total_change);
2483be6ef06SEitan Adler }
2493be6ef06SEitan Adler 
2503be6ef06SEitan Adler /* format_time(seconds) - format number of seconds into a suitable
2513be6ef06SEitan Adler  *		display that will fit within 6 characters.  Note that this
2523be6ef06SEitan Adler  *		routine builds its string in a static area.  If it needs
2533be6ef06SEitan Adler  *		to be called more than once without overwriting previous data,
2543be6ef06SEitan Adler  *		then we will need to adopt a technique similar to the
2553be6ef06SEitan Adler  *		one used for format_k.
2563be6ef06SEitan Adler  */
2573be6ef06SEitan Adler 
2583be6ef06SEitan Adler /* Explanation:
2593be6ef06SEitan Adler    We want to keep the output within 6 characters.  For low values we use
2603be6ef06SEitan Adler    the format mm:ss.  For values that exceed 999:59, we switch to a format
2613be6ef06SEitan Adler    that displays hours and fractions:  hhh.tH.  For values that exceed
2623be6ef06SEitan Adler    999.9, we use hhhh.t and drop the "H" designator.  For values that
2633be6ef06SEitan Adler    exceed 9999.9, we use "???".
2643be6ef06SEitan Adler  */
2653be6ef06SEitan Adler 
266*8e4b205eSEitan Adler const char *
267f6234b51SEitan Adler format_time(long seconds)
2683be6ef06SEitan Adler {
2693be6ef06SEitan Adler 	static char result[10];
2703be6ef06SEitan Adler 
2713be6ef06SEitan Adler 	/* sanity protection */
2723be6ef06SEitan Adler 	if (seconds < 0 || seconds > (99999l * 360l))
2733be6ef06SEitan Adler 	{
2743be6ef06SEitan Adler 		strcpy(result, "   ???");
2753be6ef06SEitan Adler 	}
2763be6ef06SEitan Adler 	else if (seconds >= (1000l * 60l))
2773be6ef06SEitan Adler 	{
2783be6ef06SEitan Adler 		/* alternate (slow) method displaying hours and tenths */
2793be6ef06SEitan Adler 		sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l));
2803be6ef06SEitan Adler 
2813be6ef06SEitan Adler 		/* It is possible that the sprintf took more than 6 characters.
2823be6ef06SEitan Adler 		   If so, then the "H" appears as result[6].  If not, then there
2833be6ef06SEitan Adler 		   is a \0 in result[6].  Either way, it is safe to step on.
2843be6ef06SEitan Adler 		   */
2853be6ef06SEitan Adler 		result[6] = '\0';
2863be6ef06SEitan Adler 	}
2873be6ef06SEitan Adler 	else
2883be6ef06SEitan Adler 	{
2893be6ef06SEitan Adler 		/* standard method produces MMM:SS */
2903be6ef06SEitan Adler 		sprintf(result, "%3ld:%02ld",
291*8e4b205eSEitan Adler 				seconds / 60l, seconds % 60l);
2923be6ef06SEitan Adler 	}
2933be6ef06SEitan Adler 	return(result);
2943be6ef06SEitan Adler }
2953be6ef06SEitan Adler 
2963be6ef06SEitan Adler /*
2973be6ef06SEitan Adler  * format_k(amt) - format a kilobyte memory value, returning a string
2983be6ef06SEitan Adler  *		suitable for display.  Returns a pointer to a static
2993be6ef06SEitan Adler  *		area that changes each call.  "amt" is converted to a
3003be6ef06SEitan Adler  *		string with a trailing "K".  If "amt" is 10000 or greater,
3013be6ef06SEitan Adler  *		then it is formatted as megabytes (rounded) with a
3023be6ef06SEitan Adler  *		trailing "M".
3033be6ef06SEitan Adler  */
3043be6ef06SEitan Adler 
3053be6ef06SEitan Adler /*
3063be6ef06SEitan Adler  * Compromise time.  We need to return a string, but we don't want the
3073be6ef06SEitan Adler  * caller to have to worry about freeing a dynamically allocated string.
3083be6ef06SEitan Adler  * Unfortunately, we can't just return a pointer to a static area as one
3093be6ef06SEitan Adler  * of the common uses of this function is in a large call to sprintf where
3103be6ef06SEitan Adler  * it might get invoked several times.  Our compromise is to maintain an
3113be6ef06SEitan Adler  * array of strings and cycle thru them with each invocation.  We make the
3123be6ef06SEitan Adler  * array large enough to handle the above mentioned case.  The constant
3133be6ef06SEitan Adler  * NUM_STRINGS defines the number of strings in this array:  we can tolerate
3143be6ef06SEitan Adler  * up to NUM_STRINGS calls before we start overwriting old information.
3153be6ef06SEitan Adler  * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
3163be6ef06SEitan Adler  * to convert the modulo operation into something quicker.  What a hack!
3173be6ef06SEitan Adler  */
3183be6ef06SEitan Adler 
3193be6ef06SEitan Adler #define NUM_STRINGS 8
3203be6ef06SEitan Adler 
321d408c8f7SEitan Adler char *
322*8e4b205eSEitan Adler format_k(int64_t amt)
3233be6ef06SEitan Adler {
3243be6ef06SEitan Adler     static char retarray[NUM_STRINGS][16];
3253be6ef06SEitan Adler     static int index = 0;
32698c299e0SEitan Adler     char *ret;
3273be6ef06SEitan Adler 
328*8e4b205eSEitan Adler     ret = retarray[index];
3293be6ef06SEitan Adler 	index = (index + 1) % NUM_STRINGS;
330*8e4b205eSEitan Adler 	humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE);
3313be6ef06SEitan Adler 	return (ret);
3323be6ef06SEitan Adler }
33351b29cb7SRoman Bogorodskiy 
33451b29cb7SRoman Bogorodskiy int
33551b29cb7SRoman Bogorodskiy find_pid(pid_t pid)
33651b29cb7SRoman Bogorodskiy {
33751b29cb7SRoman Bogorodskiy 	kvm_t *kd = NULL;
33851b29cb7SRoman Bogorodskiy 	struct kinfo_proc *pbase = NULL;
33951b29cb7SRoman Bogorodskiy 	int nproc;
34051b29cb7SRoman Bogorodskiy 	int ret = 0;
34151b29cb7SRoman Bogorodskiy 
34251b29cb7SRoman Bogorodskiy 	kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
34351b29cb7SRoman Bogorodskiy 	if (kd == NULL) {
34451b29cb7SRoman Bogorodskiy 		fprintf(stderr, "top: kvm_open() failed.\n");
34551b29cb7SRoman Bogorodskiy 		quit(TOP_EX_SYS_ERROR);
34651b29cb7SRoman Bogorodskiy 	}
34751b29cb7SRoman Bogorodskiy 
34851b29cb7SRoman Bogorodskiy 	pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc);
34951b29cb7SRoman Bogorodskiy 	if (pbase == NULL) {
35051b29cb7SRoman Bogorodskiy 		goto done;
35151b29cb7SRoman Bogorodskiy 	}
35251b29cb7SRoman Bogorodskiy 
35351b29cb7SRoman Bogorodskiy 	if ((nproc == 1) && (pbase->ki_pid == pid)) {
35451b29cb7SRoman Bogorodskiy 		ret = 1;
35551b29cb7SRoman Bogorodskiy 	}
35651b29cb7SRoman Bogorodskiy 
35751b29cb7SRoman Bogorodskiy done:
35851b29cb7SRoman Bogorodskiy 	kvm_close(kd);
35951b29cb7SRoman Bogorodskiy 	return ret;
36051b29cb7SRoman Bogorodskiy }
361