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
103be6ef06SEitan Adler /*
113be6ef06SEitan Adler * This file contains various handy utilities used by top.
123be6ef06SEitan Adler */
133be6ef06SEitan Adler
143be6ef06SEitan Adler #include "top.h"
15a5ca08edSEitan Adler #include "utils.h"
16caee4883SEitan Adler
1751b29cb7SRoman Bogorodskiy #include <sys/param.h>
1851b29cb7SRoman Bogorodskiy #include <sys/sysctl.h>
1951b29cb7SRoman Bogorodskiy #include <sys/user.h>
2051b29cb7SRoman Bogorodskiy
218e4b205eSEitan Adler #include <libutil.h>
22caee4883SEitan Adler #include <stdlib.h>
23caee4883SEitan Adler #include <stdio.h>
24caee4883SEitan Adler #include <string.h>
2551b29cb7SRoman Bogorodskiy #include <fcntl.h>
2651b29cb7SRoman Bogorodskiy #include <paths.h>
2751b29cb7SRoman Bogorodskiy #include <kvm.h>
2851b29cb7SRoman Bogorodskiy
29b3c88c28SEitan Adler int
atoiwi(const char * str)3066b3f031SEitan Adler atoiwi(const char *str)
313be6ef06SEitan Adler {
32d0bb69dcSEitan Adler size_t len;
333be6ef06SEitan Adler
343be6ef06SEitan Adler len = strlen(str);
353be6ef06SEitan Adler if (len != 0)
363be6ef06SEitan Adler {
373be6ef06SEitan Adler if (strncmp(str, "infinity", len) == 0 ||
383be6ef06SEitan Adler strncmp(str, "all", len) == 0 ||
393be6ef06SEitan Adler strncmp(str, "maximum", len) == 0)
403be6ef06SEitan Adler {
413be6ef06SEitan Adler return(Infinity);
423be6ef06SEitan Adler }
433be6ef06SEitan Adler else if (str[0] == '-')
443be6ef06SEitan Adler {
453be6ef06SEitan Adler return(Invalid);
463be6ef06SEitan Adler }
473be6ef06SEitan Adler else
483be6ef06SEitan Adler {
49c655e639SEitan Adler return((int)strtol(str, NULL, 10));
503be6ef06SEitan Adler }
513be6ef06SEitan Adler }
523be6ef06SEitan Adler return(0);
533be6ef06SEitan Adler }
543be6ef06SEitan Adler
553be6ef06SEitan Adler /*
563be6ef06SEitan Adler * itoa - convert integer (decimal) to ascii string for positive numbers
573be6ef06SEitan Adler * only (we don't bother with negative numbers since we know we
583be6ef06SEitan Adler * don't use them).
593be6ef06SEitan Adler */
603be6ef06SEitan Adler
613be6ef06SEitan Adler /*
623be6ef06SEitan Adler * How do we know that 16 will suffice?
633be6ef06SEitan Adler * Because the biggest number that we will
643be6ef06SEitan Adler * ever convert will be 2^32-1, which is 10
653be6ef06SEitan Adler * digits.
663be6ef06SEitan Adler */
673be6ef06SEitan Adler _Static_assert(sizeof(int) <= 4, "buffer too small for this sized int");
683be6ef06SEitan Adler
69d408c8f7SEitan Adler char *
itoa(unsigned int val)70d408c8f7SEitan Adler itoa(unsigned int val)
713be6ef06SEitan Adler {
723be6ef06SEitan Adler static char buffer[16]; /* result is built here */
733be6ef06SEitan Adler /* 16 is sufficient since the largest number
743be6ef06SEitan Adler we will ever convert will be 2^32-1,
753be6ef06SEitan Adler which is 10 digits. */
763be6ef06SEitan Adler
77704daa9bSEitan Adler sprintf(buffer, "%u", val);
78704daa9bSEitan Adler return (buffer);
793be6ef06SEitan Adler }
803be6ef06SEitan Adler
813be6ef06SEitan Adler /*
829f8096e3SEitan Adler * itoa7(val) - like itoa, except the number is right justified in a 7
833be6ef06SEitan Adler * character field. This code is a duplication of itoa instead of
843be6ef06SEitan Adler * a front end to a more general routine for efficiency.
853be6ef06SEitan Adler */
863be6ef06SEitan Adler
87d408c8f7SEitan Adler char *
itoa7(int val)88d408c8f7SEitan Adler itoa7(int val)
893be6ef06SEitan Adler {
903be6ef06SEitan Adler static char buffer[16]; /* result is built here */
913be6ef06SEitan Adler /* 16 is sufficient since the largest number
923be6ef06SEitan Adler we will ever convert will be 2^32-1,
933be6ef06SEitan Adler which is 10 digits. */
943be6ef06SEitan Adler
95704daa9bSEitan Adler sprintf(buffer, "%6u", val);
96704daa9bSEitan Adler return (buffer);
973be6ef06SEitan Adler }
983be6ef06SEitan Adler
993be6ef06SEitan Adler /*
1003be6ef06SEitan Adler * digits(val) - return number of decimal digits in val. Only works for
101ccf22059SEitan Adler * non-negative numbers.
1023be6ef06SEitan Adler */
1033be6ef06SEitan Adler
104fb7b896cSEitan Adler int __pure2
digits(int val)105d408c8f7SEitan Adler digits(int val)
1063be6ef06SEitan Adler {
10798c299e0SEitan Adler int cnt = 0;
108fb7b896cSEitan Adler if (val == 0) {
109fb7b896cSEitan Adler return 1;
110fb7b896cSEitan Adler }
1113be6ef06SEitan Adler
112fb7b896cSEitan Adler while (val > 0) {
1133be6ef06SEitan Adler cnt++;
1143be6ef06SEitan Adler val /= 10;
1153be6ef06SEitan Adler }
1163be6ef06SEitan Adler return(cnt);
1173be6ef06SEitan Adler }
1183be6ef06SEitan Adler
1193be6ef06SEitan Adler /*
1203be6ef06SEitan Adler * string_index(string, array) - find string in array and return index
1213be6ef06SEitan Adler */
1223be6ef06SEitan Adler
123f7642433SEitan Adler int
string_index(const char * string,const char * const * array)124eae589f1SEitan Adler string_index(const char *string, const char * const *array)
1253be6ef06SEitan Adler {
126f6234b51SEitan Adler size_t i = 0;
1273be6ef06SEitan Adler
1283be6ef06SEitan Adler while (*array != NULL)
1293be6ef06SEitan Adler {
1303be6ef06SEitan Adler if (strcmp(string, *array) == 0)
1313be6ef06SEitan Adler {
1323be6ef06SEitan Adler return(i);
1333be6ef06SEitan Adler }
1343be6ef06SEitan Adler array++;
1353be6ef06SEitan Adler i++;
1363be6ef06SEitan Adler }
1373be6ef06SEitan Adler return(-1);
1383be6ef06SEitan Adler }
1393be6ef06SEitan Adler
1403be6ef06SEitan Adler /*
1413be6ef06SEitan Adler * argparse(line, cntp) - parse arguments in string "line", separating them
1423be6ef06SEitan Adler * out into an argv-like array, and setting *cntp to the number of
1433be6ef06SEitan Adler * arguments encountered. This is a simple parser that doesn't understand
1443be6ef06SEitan Adler * squat about quotes.
1453be6ef06SEitan Adler */
1463be6ef06SEitan Adler
147d0f687d3SDimitry Andric const char **
argparse(char * line,int * cntp)148a9a99372SEitan Adler argparse(char *line, int *cntp)
1493be6ef06SEitan Adler {
150a9a99372SEitan Adler const char **ap;
151a9a99372SEitan Adler static const char *argv[1024] = {0};
1523be6ef06SEitan Adler
153a9a99372SEitan Adler *cntp = 1;
154a9a99372SEitan Adler ap = &argv[1];
155a9a99372SEitan Adler while ((*ap = strsep(&line, " ")) != NULL) {
156a9a99372SEitan Adler if (**ap != '\0') {
157a9a99372SEitan Adler (*cntp)++;
158a9a99372SEitan Adler if (*cntp >= (int)nitems(argv)) {
159a9a99372SEitan Adler break;
1603be6ef06SEitan Adler }
161a9a99372SEitan Adler ap++;
1623be6ef06SEitan Adler }
1633be6ef06SEitan Adler }
164d408c8f7SEitan Adler return (argv);
1653be6ef06SEitan Adler }
1663be6ef06SEitan Adler
1673be6ef06SEitan Adler /*
1683be6ef06SEitan Adler * percentages(cnt, out, new, old, diffs) - calculate percentage change
1693be6ef06SEitan Adler * between array "old" and "new", putting the percentages i "out".
1703be6ef06SEitan Adler * "cnt" is size of each array and "diffs" is used for scratch space.
1713be6ef06SEitan Adler * The array "old" is updated on each call.
1723be6ef06SEitan Adler * The routine assumes modulo arithmetic. This function is especially
173f6234b51SEitan Adler * useful on for calculating cpu state percentages.
1743be6ef06SEitan Adler */
1753be6ef06SEitan Adler
176f7642433SEitan Adler long
percentages(int cnt,int * out,long * new,long * old,long * diffs)177f6234b51SEitan Adler percentages(int cnt, int *out, long *new, long *old, long *diffs)
1783be6ef06SEitan Adler {
17998c299e0SEitan Adler int i;
18098c299e0SEitan Adler long change;
18198c299e0SEitan Adler long total_change;
18298c299e0SEitan Adler long *dp;
1833be6ef06SEitan Adler long half_total;
1843be6ef06SEitan Adler
1853be6ef06SEitan Adler /* initialization */
1863be6ef06SEitan Adler total_change = 0;
1873be6ef06SEitan Adler dp = diffs;
1883be6ef06SEitan Adler
1893be6ef06SEitan Adler /* calculate changes for each state and the overall change */
1903be6ef06SEitan Adler for (i = 0; i < cnt; i++)
1913be6ef06SEitan Adler {
1923be6ef06SEitan Adler if ((change = *new - *old) < 0)
1933be6ef06SEitan Adler {
1943be6ef06SEitan Adler /* this only happens when the counter wraps */
1953be6ef06SEitan Adler change = (int)
1963be6ef06SEitan Adler ((unsigned long)*new-(unsigned long)*old);
1973be6ef06SEitan Adler }
1983be6ef06SEitan Adler total_change += (*dp++ = change);
1993be6ef06SEitan Adler *old++ = *new++;
2003be6ef06SEitan Adler }
2013be6ef06SEitan Adler
2023be6ef06SEitan Adler /* avoid divide by zero potential */
2033be6ef06SEitan Adler if (total_change == 0)
2043be6ef06SEitan Adler {
2053be6ef06SEitan Adler total_change = 1;
2063be6ef06SEitan Adler }
2073be6ef06SEitan Adler
2083be6ef06SEitan Adler /* calculate percentages based on overall change, rounding up */
2093be6ef06SEitan Adler half_total = total_change / 2l;
2103be6ef06SEitan Adler
2113be6ef06SEitan Adler for (i = 0; i < cnt; i++)
2123be6ef06SEitan Adler {
2133be6ef06SEitan Adler *out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
2143be6ef06SEitan Adler }
2153be6ef06SEitan Adler
2163be6ef06SEitan Adler /* return the total in case the caller wants to use it */
2173be6ef06SEitan Adler return(total_change);
2183be6ef06SEitan Adler }
2193be6ef06SEitan Adler
2203be6ef06SEitan Adler /* format_time(seconds) - format number of seconds into a suitable
2213be6ef06SEitan Adler * display that will fit within 6 characters. Note that this
2223be6ef06SEitan Adler * routine builds its string in a static area. If it needs
2233be6ef06SEitan Adler * to be called more than once without overwriting previous data,
2243be6ef06SEitan Adler * then we will need to adopt a technique similar to the
2253be6ef06SEitan Adler * one used for format_k.
2263be6ef06SEitan Adler */
2273be6ef06SEitan Adler
2283be6ef06SEitan Adler /* Explanation:
2293be6ef06SEitan Adler We want to keep the output within 6 characters. For low values we use
2303be6ef06SEitan Adler the format mm:ss. For values that exceed 999:59, we switch to a format
2313be6ef06SEitan Adler that displays hours and fractions: hhh.tH. For values that exceed
2323be6ef06SEitan Adler 999.9, we use hhhh.t and drop the "H" designator. For values that
2333be6ef06SEitan Adler exceed 9999.9, we use "???".
2343be6ef06SEitan Adler */
2353be6ef06SEitan Adler
2368e4b205eSEitan Adler const char *
format_time(long seconds)237f6234b51SEitan Adler format_time(long seconds)
2383be6ef06SEitan Adler {
2393be6ef06SEitan Adler static char result[10];
2403be6ef06SEitan Adler
2413be6ef06SEitan Adler /* sanity protection */
2423be6ef06SEitan Adler if (seconds < 0 || seconds > (99999l * 360l))
2433be6ef06SEitan Adler {
2443be6ef06SEitan Adler strcpy(result, " ???");
2453be6ef06SEitan Adler }
2463be6ef06SEitan Adler else if (seconds >= (1000l * 60l))
2473be6ef06SEitan Adler {
2483be6ef06SEitan Adler /* alternate (slow) method displaying hours and tenths */
2493be6ef06SEitan Adler sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l));
2503be6ef06SEitan Adler
2513be6ef06SEitan Adler /* It is possible that the sprintf took more than 6 characters.
2523be6ef06SEitan Adler If so, then the "H" appears as result[6]. If not, then there
2533be6ef06SEitan Adler is a \0 in result[6]. Either way, it is safe to step on.
2543be6ef06SEitan Adler */
2553be6ef06SEitan Adler result[6] = '\0';
2563be6ef06SEitan Adler }
2573be6ef06SEitan Adler else
2583be6ef06SEitan Adler {
2593be6ef06SEitan Adler /* standard method produces MMM:SS */
2603be6ef06SEitan Adler sprintf(result, "%3ld:%02ld",
2618e4b205eSEitan Adler seconds / 60l, seconds % 60l);
2623be6ef06SEitan Adler }
2633be6ef06SEitan Adler return(result);
2643be6ef06SEitan Adler }
2653be6ef06SEitan Adler
2663be6ef06SEitan Adler /*
2673be6ef06SEitan Adler * format_k(amt) - format a kilobyte memory value, returning a string
2683be6ef06SEitan Adler * suitable for display. Returns a pointer to a static
26951c834c4SEitan Adler * area that changes each call. "amt" is converted to a fixed
27051c834c4SEitan Adler * size humanize_number call
2713be6ef06SEitan Adler */
2723be6ef06SEitan Adler
2733be6ef06SEitan Adler /*
2743be6ef06SEitan Adler * Compromise time. We need to return a string, but we don't want the
2753be6ef06SEitan Adler * caller to have to worry about freeing a dynamically allocated string.
2763be6ef06SEitan Adler * Unfortunately, we can't just return a pointer to a static area as one
2773be6ef06SEitan Adler * of the common uses of this function is in a large call to sprintf where
2783be6ef06SEitan Adler * it might get invoked several times. Our compromise is to maintain an
2793be6ef06SEitan Adler * array of strings and cycle thru them with each invocation. We make the
2803be6ef06SEitan Adler * array large enough to handle the above mentioned case. The constant
2813be6ef06SEitan Adler * NUM_STRINGS defines the number of strings in this array: we can tolerate
2823be6ef06SEitan Adler * up to NUM_STRINGS calls before we start overwriting old information.
2833be6ef06SEitan Adler * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
2843be6ef06SEitan Adler * to convert the modulo operation into something quicker. What a hack!
2853be6ef06SEitan Adler */
2863be6ef06SEitan Adler
2873be6ef06SEitan Adler #define NUM_STRINGS 8
2883be6ef06SEitan Adler
289d408c8f7SEitan Adler char *
format_k(int64_t amt)2908e4b205eSEitan Adler format_k(int64_t amt)
2913be6ef06SEitan Adler {
2923be6ef06SEitan Adler static char retarray[NUM_STRINGS][16];
2932f301637SDimitry Andric static int index_ = 0;
29498c299e0SEitan Adler char *ret;
2953be6ef06SEitan Adler
2962f301637SDimitry Andric ret = retarray[index_];
2972f301637SDimitry Andric index_ = (index_ + 1) % NUM_STRINGS;
298*738b2d5cSMark Johnston humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE |
299*738b2d5cSMark Johnston HN_B);
3003be6ef06SEitan Adler return (ret);
3013be6ef06SEitan Adler }
30251b29cb7SRoman Bogorodskiy
30351b29cb7SRoman Bogorodskiy int
find_pid(pid_t pid)30451b29cb7SRoman Bogorodskiy find_pid(pid_t pid)
30551b29cb7SRoman Bogorodskiy {
30651b29cb7SRoman Bogorodskiy kvm_t *kd = NULL;
30751b29cb7SRoman Bogorodskiy struct kinfo_proc *pbase = NULL;
30851b29cb7SRoman Bogorodskiy int nproc;
30951b29cb7SRoman Bogorodskiy int ret = 0;
31051b29cb7SRoman Bogorodskiy
31151b29cb7SRoman Bogorodskiy kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
31251b29cb7SRoman Bogorodskiy if (kd == NULL) {
31351b29cb7SRoman Bogorodskiy fprintf(stderr, "top: kvm_open() failed.\n");
31451b29cb7SRoman Bogorodskiy quit(TOP_EX_SYS_ERROR);
31551b29cb7SRoman Bogorodskiy }
31651b29cb7SRoman Bogorodskiy
31751b29cb7SRoman Bogorodskiy pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc);
31851b29cb7SRoman Bogorodskiy if (pbase == NULL) {
31951b29cb7SRoman Bogorodskiy goto done;
32051b29cb7SRoman Bogorodskiy }
32151b29cb7SRoman Bogorodskiy
32251b29cb7SRoman Bogorodskiy if ((nproc == 1) && (pbase->ki_pid == pid)) {
32351b29cb7SRoman Bogorodskiy ret = 1;
32451b29cb7SRoman Bogorodskiy }
32551b29cb7SRoman Bogorodskiy
32651b29cb7SRoman Bogorodskiy done:
32751b29cb7SRoman Bogorodskiy kvm_close(kd);
32851b29cb7SRoman Bogorodskiy return ret;
32951b29cb7SRoman Bogorodskiy }
330