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 228e4b205eSEitan 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 { 733be6ef06SEitan Adler static char buffer[16]; /* result is built here */ 743be6ef06SEitan Adler /* 16 is sufficient since the largest number 753be6ef06SEitan Adler we will ever convert will be 2^32-1, 763be6ef06SEitan Adler which is 10 digits. */ 773be6ef06SEitan Adler 78704daa9bSEitan Adler sprintf(buffer, "%u", val); 79704daa9bSEitan Adler return (buffer); 803be6ef06SEitan Adler } 813be6ef06SEitan Adler 823be6ef06SEitan Adler /* 839f8096e3SEitan Adler * itoa7(val) - like itoa, except the number is right justified in a 7 843be6ef06SEitan Adler * character field. This code is a duplication of itoa instead of 853be6ef06SEitan Adler * a front end to a more general routine for efficiency. 863be6ef06SEitan Adler */ 873be6ef06SEitan Adler 88d408c8f7SEitan Adler char * 89d408c8f7SEitan Adler itoa7(int val) 903be6ef06SEitan Adler { 913be6ef06SEitan Adler static char buffer[16]; /* result is built here */ 923be6ef06SEitan Adler /* 16 is sufficient since the largest number 933be6ef06SEitan Adler we will ever convert will be 2^32-1, 943be6ef06SEitan Adler which is 10 digits. */ 953be6ef06SEitan Adler 96704daa9bSEitan Adler sprintf(buffer, "%6u", val); 97704daa9bSEitan Adler return (buffer); 983be6ef06SEitan Adler } 993be6ef06SEitan Adler 1003be6ef06SEitan Adler /* 1013be6ef06SEitan Adler * digits(val) - return number of decimal digits in val. Only works for 102ccf22059SEitan Adler * non-negative numbers. 1033be6ef06SEitan Adler */ 1043be6ef06SEitan Adler 105fb7b896cSEitan Adler int __pure2 106d408c8f7SEitan Adler digits(int val) 1073be6ef06SEitan Adler { 10898c299e0SEitan Adler int cnt = 0; 109fb7b896cSEitan Adler if (val == 0) { 110fb7b896cSEitan Adler return 1; 111fb7b896cSEitan Adler } 1123be6ef06SEitan Adler 113fb7b896cSEitan Adler while (val > 0) { 1143be6ef06SEitan Adler cnt++; 1153be6ef06SEitan Adler val /= 10; 1163be6ef06SEitan Adler } 1173be6ef06SEitan Adler return(cnt); 1183be6ef06SEitan Adler } 1193be6ef06SEitan Adler 1203be6ef06SEitan Adler /* 1213be6ef06SEitan Adler * string_index(string, array) - find string in array and return index 1223be6ef06SEitan Adler */ 1233be6ef06SEitan Adler 124f6234b51SEitan Adler int 125eae589f1SEitan Adler string_index(const char *string, const char * const *array) 1263be6ef06SEitan Adler { 127f6234b51SEitan Adler size_t i = 0; 1283be6ef06SEitan Adler 1293be6ef06SEitan Adler while (*array != NULL) 1303be6ef06SEitan Adler { 1313be6ef06SEitan Adler if (strcmp(string, *array) == 0) 1323be6ef06SEitan Adler { 1333be6ef06SEitan Adler return(i); 1343be6ef06SEitan Adler } 1353be6ef06SEitan Adler array++; 1363be6ef06SEitan Adler i++; 1373be6ef06SEitan Adler } 1383be6ef06SEitan Adler return(-1); 1393be6ef06SEitan Adler } 1403be6ef06SEitan Adler 1413be6ef06SEitan Adler /* 1423be6ef06SEitan Adler * argparse(line, cntp) - parse arguments in string "line", separating them 1433be6ef06SEitan Adler * out into an argv-like array, and setting *cntp to the number of 1443be6ef06SEitan Adler * arguments encountered. This is a simple parser that doesn't understand 1453be6ef06SEitan Adler * squat about quotes. 1463be6ef06SEitan Adler */ 1473be6ef06SEitan Adler 148eae589f1SEitan Adler const char * const * 149a9a99372SEitan Adler argparse(char *line, int *cntp) 1503be6ef06SEitan Adler { 151a9a99372SEitan Adler const char **ap; 152a9a99372SEitan Adler static const char *argv[1024] = {0}; 1533be6ef06SEitan Adler 154a9a99372SEitan Adler *cntp = 1; 155a9a99372SEitan Adler ap = &argv[1]; 156a9a99372SEitan Adler while ((*ap = strsep(&line, " ")) != NULL) { 157a9a99372SEitan Adler if (**ap != '\0') { 158a9a99372SEitan Adler (*cntp)++; 159a9a99372SEitan Adler if (*cntp >= (int)nitems(argv)) { 160a9a99372SEitan Adler break; 1613be6ef06SEitan Adler } 162a9a99372SEitan Adler ap++; 1633be6ef06SEitan Adler } 1643be6ef06SEitan Adler } 165d408c8f7SEitan Adler return (argv); 1663be6ef06SEitan Adler } 1673be6ef06SEitan Adler 1683be6ef06SEitan Adler /* 1693be6ef06SEitan Adler * percentages(cnt, out, new, old, diffs) - calculate percentage change 1703be6ef06SEitan Adler * between array "old" and "new", putting the percentages i "out". 1713be6ef06SEitan Adler * "cnt" is size of each array and "diffs" is used for scratch space. 1723be6ef06SEitan Adler * The array "old" is updated on each call. 1733be6ef06SEitan Adler * The routine assumes modulo arithmetic. This function is especially 174f6234b51SEitan Adler * useful on for calculating cpu state percentages. 1753be6ef06SEitan Adler */ 1763be6ef06SEitan Adler 177f6234b51SEitan Adler long 178f6234b51SEitan Adler percentages(int cnt, int *out, long *new, long *old, long *diffs) 1793be6ef06SEitan Adler { 18098c299e0SEitan Adler int i; 18198c299e0SEitan Adler long change; 18298c299e0SEitan Adler long total_change; 18398c299e0SEitan Adler long *dp; 1843be6ef06SEitan Adler long half_total; 1853be6ef06SEitan Adler 1863be6ef06SEitan Adler /* initialization */ 1873be6ef06SEitan Adler total_change = 0; 1883be6ef06SEitan Adler dp = diffs; 1893be6ef06SEitan Adler 1903be6ef06SEitan Adler /* calculate changes for each state and the overall change */ 1913be6ef06SEitan Adler for (i = 0; i < cnt; i++) 1923be6ef06SEitan Adler { 1933be6ef06SEitan Adler if ((change = *new - *old) < 0) 1943be6ef06SEitan Adler { 1953be6ef06SEitan Adler /* this only happens when the counter wraps */ 1963be6ef06SEitan Adler change = (int) 1973be6ef06SEitan Adler ((unsigned long)*new-(unsigned long)*old); 1983be6ef06SEitan Adler } 1993be6ef06SEitan Adler total_change += (*dp++ = change); 2003be6ef06SEitan Adler *old++ = *new++; 2013be6ef06SEitan Adler } 2023be6ef06SEitan Adler 2033be6ef06SEitan Adler /* avoid divide by zero potential */ 2043be6ef06SEitan Adler if (total_change == 0) 2053be6ef06SEitan Adler { 2063be6ef06SEitan Adler total_change = 1; 2073be6ef06SEitan Adler } 2083be6ef06SEitan Adler 2093be6ef06SEitan Adler /* calculate percentages based on overall change, rounding up */ 2103be6ef06SEitan Adler half_total = total_change / 2l; 2113be6ef06SEitan Adler 2123be6ef06SEitan Adler /* Do not divide by 0. Causes Floating point exception */ 2133be6ef06SEitan Adler if(total_change) { 2143be6ef06SEitan Adler for (i = 0; i < cnt; i++) 2153be6ef06SEitan Adler { 2163be6ef06SEitan Adler *out++ = (int)((*diffs++ * 1000 + half_total) / total_change); 2173be6ef06SEitan Adler } 2183be6ef06SEitan Adler } 2193be6ef06SEitan Adler 2203be6ef06SEitan Adler /* return the total in case the caller wants to use it */ 2213be6ef06SEitan Adler return(total_change); 2223be6ef06SEitan Adler } 2233be6ef06SEitan Adler 2243be6ef06SEitan Adler /* format_time(seconds) - format number of seconds into a suitable 2253be6ef06SEitan Adler * display that will fit within 6 characters. Note that this 2263be6ef06SEitan Adler * routine builds its string in a static area. If it needs 2273be6ef06SEitan Adler * to be called more than once without overwriting previous data, 2283be6ef06SEitan Adler * then we will need to adopt a technique similar to the 2293be6ef06SEitan Adler * one used for format_k. 2303be6ef06SEitan Adler */ 2313be6ef06SEitan Adler 2323be6ef06SEitan Adler /* Explanation: 2333be6ef06SEitan Adler We want to keep the output within 6 characters. For low values we use 2343be6ef06SEitan Adler the format mm:ss. For values that exceed 999:59, we switch to a format 2353be6ef06SEitan Adler that displays hours and fractions: hhh.tH. For values that exceed 2363be6ef06SEitan Adler 999.9, we use hhhh.t and drop the "H" designator. For values that 2373be6ef06SEitan Adler exceed 9999.9, we use "???". 2383be6ef06SEitan Adler */ 2393be6ef06SEitan Adler 2408e4b205eSEitan Adler const char * 241f6234b51SEitan Adler format_time(long seconds) 2423be6ef06SEitan Adler { 2433be6ef06SEitan Adler static char result[10]; 2443be6ef06SEitan Adler 2453be6ef06SEitan Adler /* sanity protection */ 2463be6ef06SEitan Adler if (seconds < 0 || seconds > (99999l * 360l)) 2473be6ef06SEitan Adler { 2483be6ef06SEitan Adler strcpy(result, " ???"); 2493be6ef06SEitan Adler } 2503be6ef06SEitan Adler else if (seconds >= (1000l * 60l)) 2513be6ef06SEitan Adler { 2523be6ef06SEitan Adler /* alternate (slow) method displaying hours and tenths */ 2533be6ef06SEitan Adler sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l)); 2543be6ef06SEitan Adler 2553be6ef06SEitan Adler /* It is possible that the sprintf took more than 6 characters. 2563be6ef06SEitan Adler If so, then the "H" appears as result[6]. If not, then there 2573be6ef06SEitan Adler is a \0 in result[6]. Either way, it is safe to step on. 2583be6ef06SEitan Adler */ 2593be6ef06SEitan Adler result[6] = '\0'; 2603be6ef06SEitan Adler } 2613be6ef06SEitan Adler else 2623be6ef06SEitan Adler { 2633be6ef06SEitan Adler /* standard method produces MMM:SS */ 2643be6ef06SEitan Adler sprintf(result, "%3ld:%02ld", 2658e4b205eSEitan Adler seconds / 60l, seconds % 60l); 2663be6ef06SEitan Adler } 2673be6ef06SEitan Adler return(result); 2683be6ef06SEitan Adler } 2693be6ef06SEitan Adler 2703be6ef06SEitan Adler /* 2713be6ef06SEitan Adler * format_k(amt) - format a kilobyte memory value, returning a string 2723be6ef06SEitan Adler * suitable for display. Returns a pointer to a static 2733be6ef06SEitan Adler * area that changes each call. "amt" is converted to a 2743be6ef06SEitan Adler * string with a trailing "K". If "amt" is 10000 or greater, 2753be6ef06SEitan Adler * then it is formatted as megabytes (rounded) with a 2763be6ef06SEitan Adler * trailing "M". 2773be6ef06SEitan Adler */ 2783be6ef06SEitan Adler 2793be6ef06SEitan Adler /* 2803be6ef06SEitan Adler * Compromise time. We need to return a string, but we don't want the 2813be6ef06SEitan Adler * caller to have to worry about freeing a dynamically allocated string. 2823be6ef06SEitan Adler * Unfortunately, we can't just return a pointer to a static area as one 2833be6ef06SEitan Adler * of the common uses of this function is in a large call to sprintf where 2843be6ef06SEitan Adler * it might get invoked several times. Our compromise is to maintain an 2853be6ef06SEitan Adler * array of strings and cycle thru them with each invocation. We make the 2863be6ef06SEitan Adler * array large enough to handle the above mentioned case. The constant 2873be6ef06SEitan Adler * NUM_STRINGS defines the number of strings in this array: we can tolerate 2883be6ef06SEitan Adler * up to NUM_STRINGS calls before we start overwriting old information. 2893be6ef06SEitan Adler * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer 2903be6ef06SEitan Adler * to convert the modulo operation into something quicker. What a hack! 2913be6ef06SEitan Adler */ 2923be6ef06SEitan Adler 2933be6ef06SEitan Adler #define NUM_STRINGS 8 2943be6ef06SEitan Adler 295d408c8f7SEitan Adler char * 2968e4b205eSEitan Adler format_k(int64_t amt) 2973be6ef06SEitan Adler { 2983be6ef06SEitan Adler static char retarray[NUM_STRINGS][16]; 2993be6ef06SEitan Adler static int index = 0; 30098c299e0SEitan Adler char *ret; 3013be6ef06SEitan Adler 3028e4b205eSEitan Adler ret = retarray[index]; 3033be6ef06SEitan Adler index = (index + 1) % NUM_STRINGS; 3048e4b205eSEitan Adler humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE); 3053be6ef06SEitan Adler return (ret); 3063be6ef06SEitan Adler } 30751b29cb7SRoman Bogorodskiy 30851b29cb7SRoman Bogorodskiy int 30951b29cb7SRoman Bogorodskiy find_pid(pid_t pid) 31051b29cb7SRoman Bogorodskiy { 31151b29cb7SRoman Bogorodskiy kvm_t *kd = NULL; 31251b29cb7SRoman Bogorodskiy struct kinfo_proc *pbase = NULL; 31351b29cb7SRoman Bogorodskiy int nproc; 31451b29cb7SRoman Bogorodskiy int ret = 0; 31551b29cb7SRoman Bogorodskiy 31651b29cb7SRoman Bogorodskiy kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); 31751b29cb7SRoman Bogorodskiy if (kd == NULL) { 31851b29cb7SRoman Bogorodskiy fprintf(stderr, "top: kvm_open() failed.\n"); 31951b29cb7SRoman Bogorodskiy quit(TOP_EX_SYS_ERROR); 32051b29cb7SRoman Bogorodskiy } 32151b29cb7SRoman Bogorodskiy 32251b29cb7SRoman Bogorodskiy pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc); 32351b29cb7SRoman Bogorodskiy if (pbase == NULL) { 32451b29cb7SRoman Bogorodskiy goto done; 32551b29cb7SRoman Bogorodskiy } 32651b29cb7SRoman Bogorodskiy 32751b29cb7SRoman Bogorodskiy if ((nproc == 1) && (pbase->ki_pid == pid)) { 32851b29cb7SRoman Bogorodskiy ret = 1; 32951b29cb7SRoman Bogorodskiy } 33051b29cb7SRoman Bogorodskiy 33151b29cb7SRoman Bogorodskiy done: 33251b29cb7SRoman Bogorodskiy kvm_close(kd); 33351b29cb7SRoman Bogorodskiy return ret; 33451b29cb7SRoman Bogorodskiy } 335