1997-03-23 18:51:21 +00:00
|
|
|
/*
|
|
|
|
* This program may be freely redistributed,
|
|
|
|
* but this entire comment MUST remain intact.
|
|
|
|
*
|
2018-06-13 08:52:19 +00:00
|
|
|
* Copyright (c) 2018, Eitan Adler
|
1997-03-23 18:51:21 +00:00
|
|
|
* Copyright (c) 1984, 1989, William LeFebvre, Rice University
|
|
|
|
* Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
|
2002-01-24 17:58:42 +00:00
|
|
|
*
|
|
|
|
* $FreeBSD$
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file contains various handy utilities used by top.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "top.h"
|
2018-05-21 03:36:16 +00:00
|
|
|
#include "utils.h"
|
2018-05-19 23:19:24 +00:00
|
|
|
|
2018-06-02 15:52:18 +00:00
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
|
2018-06-13 08:52:09 +00:00
|
|
|
#include <libutil.h>
|
2018-05-19 23:19:24 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2018-06-02 15:52:18 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <paths.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
top(1): Quiesce several warnings
This is all warnings at level six (6) that are not
char-subscripts, incompatible-pointer-types,
sign-compare, switch, int-conversion,
missing-variable-declarations, cast-qual, cast-align
Some warnings that are fixed by this commit are:
shadow, strict-prototypes, missing-prototypes, pointer-arith,
unused-parameter, unused-const-variable, and several others
2018-05-20 02:14:27 +00:00
|
|
|
int
|
2018-06-02 04:37:37 +00:00
|
|
|
atoiwi(const char *str)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-03 02:58:53 +00:00
|
|
|
size_t len;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
if (len != 0)
|
|
|
|
{
|
|
|
|
if (strncmp(str, "infinity", len) == 0 ||
|
|
|
|
strncmp(str, "all", len) == 0 ||
|
|
|
|
strncmp(str, "maximum", len) == 0)
|
|
|
|
{
|
|
|
|
return(Infinity);
|
|
|
|
}
|
|
|
|
else if (str[0] == '-')
|
|
|
|
{
|
|
|
|
return(Invalid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-06-13 08:52:14 +00:00
|
|
|
return((int)strtol(str, NULL, 10));
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* itoa - convert integer (decimal) to ascii string for positive numbers
|
|
|
|
* only (we don't bother with negative numbers since we know we
|
|
|
|
* don't use them).
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* How do we know that 16 will suffice?
|
|
|
|
* Because the biggest number that we will
|
|
|
|
* ever convert will be 2^32-1, which is 10
|
|
|
|
* digits.
|
|
|
|
*/
|
2015-02-11 07:44:53 +00:00
|
|
|
_Static_assert(sizeof(int) <= 4, "buffer too small for this sized int");
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-06-08 01:55:47 +00:00
|
|
|
char *
|
|
|
|
itoa(unsigned int val)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
static char buffer[16]; /* result is built here */
|
|
|
|
/* 16 is sufficient since the largest number
|
|
|
|
we will ever convert will be 2^32-1,
|
|
|
|
which is 10 digits. */
|
|
|
|
|
2018-06-13 08:52:12 +00:00
|
|
|
sprintf(buffer, "%u", val);
|
|
|
|
return (buffer);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-06-03 22:42:54 +00:00
|
|
|
* itoa7(val) - like itoa, except the number is right justified in a 7
|
1997-03-23 18:51:21 +00:00
|
|
|
* character field. This code is a duplication of itoa instead of
|
|
|
|
* a front end to a more general routine for efficiency.
|
|
|
|
*/
|
|
|
|
|
2018-06-08 01:55:47 +00:00
|
|
|
char *
|
|
|
|
itoa7(int val)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
|
|
|
static char buffer[16]; /* result is built here */
|
|
|
|
/* 16 is sufficient since the largest number
|
|
|
|
we will ever convert will be 2^32-1,
|
|
|
|
which is 10 digits. */
|
|
|
|
|
2018-06-13 08:52:12 +00:00
|
|
|
sprintf(buffer, "%6u", val);
|
|
|
|
return (buffer);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* digits(val) - return number of decimal digits in val. Only works for
|
2018-06-12 07:14:12 +00:00
|
|
|
* non-negative numbers.
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-11 05:05:20 +00:00
|
|
|
int __pure2
|
2018-06-08 01:55:47 +00:00
|
|
|
digits(int val)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int cnt = 0;
|
2018-06-11 05:05:20 +00:00
|
|
|
if (val == 0) {
|
|
|
|
return 1;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-06-11 05:05:20 +00:00
|
|
|
while (val > 0) {
|
2018-06-09 23:24:08 +00:00
|
|
|
cnt++;
|
|
|
|
val /= 10;
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
return(cnt);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* string_index(string, array) - find string in array and return index
|
|
|
|
*/
|
|
|
|
|
2018-06-13 11:12:52 +00:00
|
|
|
int
|
2018-06-03 23:40:54 +00:00
|
|
|
string_index(const char *string, const char * const *array)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-02 00:02:15 +00:00
|
|
|
size_t i = 0;
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
while (*array != NULL)
|
|
|
|
{
|
|
|
|
if (strcmp(string, *array) == 0)
|
|
|
|
{
|
|
|
|
return(i);
|
|
|
|
}
|
|
|
|
array++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* argparse(line, cntp) - parse arguments in string "line", separating them
|
|
|
|
* out into an argv-like array, and setting *cntp to the number of
|
|
|
|
* arguments encountered. This is a simple parser that doesn't understand
|
|
|
|
* squat about quotes.
|
|
|
|
*/
|
|
|
|
|
2019-02-10 13:31:08 +00:00
|
|
|
const char **
|
2018-06-04 03:16:24 +00:00
|
|
|
argparse(char *line, int *cntp)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-04 03:16:24 +00:00
|
|
|
const char **ap;
|
|
|
|
static const char *argv[1024] = {0};
|
|
|
|
|
|
|
|
*cntp = 1;
|
|
|
|
ap = &argv[1];
|
|
|
|
while ((*ap = strsep(&line, " ")) != NULL) {
|
|
|
|
if (**ap != '\0') {
|
|
|
|
(*cntp)++;
|
|
|
|
if (*cntp >= (int)nitems(argv)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ap++;
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
2018-06-08 01:55:47 +00:00
|
|
|
return (argv);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* percentages(cnt, out, new, old, diffs) - calculate percentage change
|
|
|
|
* between array "old" and "new", putting the percentages i "out".
|
|
|
|
* "cnt" is size of each array and "diffs" is used for scratch space.
|
|
|
|
* The array "old" is updated on each call.
|
|
|
|
* The routine assumes modulo arithmetic. This function is especially
|
2018-06-02 00:02:15 +00:00
|
|
|
* useful on for calculating cpu state percentages.
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-13 11:12:52 +00:00
|
|
|
long
|
2018-06-02 00:02:15 +00:00
|
|
|
percentages(int cnt, int *out, long *new, long *old, long *diffs)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-05-20 01:32:27 +00:00
|
|
|
int i;
|
|
|
|
long change;
|
|
|
|
long total_change;
|
|
|
|
long *dp;
|
1997-03-23 18:51:21 +00:00
|
|
|
long half_total;
|
|
|
|
|
|
|
|
/* initialization */
|
|
|
|
total_change = 0;
|
|
|
|
dp = diffs;
|
|
|
|
|
|
|
|
/* calculate changes for each state and the overall change */
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
{
|
|
|
|
if ((change = *new - *old) < 0)
|
|
|
|
{
|
|
|
|
/* this only happens when the counter wraps */
|
|
|
|
change = (int)
|
|
|
|
((unsigned long)*new-(unsigned long)*old);
|
|
|
|
}
|
|
|
|
total_change += (*dp++ = change);
|
|
|
|
*old++ = *new++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* avoid divide by zero potential */
|
|
|
|
if (total_change == 0)
|
|
|
|
{
|
|
|
|
total_change = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* calculate percentages based on overall change, rounding up */
|
|
|
|
half_total = total_change / 2l;
|
1997-03-23 18:53:01 +00:00
|
|
|
|
2018-06-13 11:12:52 +00:00
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
{
|
|
|
|
*out++ = (int)((*diffs++ * 1000 + half_total) / total_change);
|
|
|
|
}
|
1997-03-23 18:51:21 +00:00
|
|
|
|
|
|
|
/* return the total in case the caller wants to use it */
|
|
|
|
return(total_change);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* format_time(seconds) - format number of seconds into a suitable
|
|
|
|
* display that will fit within 6 characters. Note that this
|
|
|
|
* routine builds its string in a static area. If it needs
|
|
|
|
* to be called more than once without overwriting previous data,
|
|
|
|
* then we will need to adopt a technique similar to the
|
|
|
|
* one used for format_k.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Explanation:
|
|
|
|
We want to keep the output within 6 characters. For low values we use
|
|
|
|
the format mm:ss. For values that exceed 999:59, we switch to a format
|
|
|
|
that displays hours and fractions: hhh.tH. For values that exceed
|
|
|
|
999.9, we use hhhh.t and drop the "H" designator. For values that
|
|
|
|
exceed 9999.9, we use "???".
|
|
|
|
*/
|
|
|
|
|
2018-06-13 08:52:09 +00:00
|
|
|
const char *
|
2018-06-02 00:02:15 +00:00
|
|
|
format_time(long seconds)
|
1997-03-23 18:51:21 +00:00
|
|
|
{
|
2018-06-13 08:52:09 +00:00
|
|
|
static char result[10];
|
1997-03-23 18:51:21 +00:00
|
|
|
|
2018-06-13 08:52:09 +00:00
|
|
|
/* sanity protection */
|
|
|
|
if (seconds < 0 || seconds > (99999l * 360l))
|
|
|
|
{
|
|
|
|
strcpy(result, " ???");
|
|
|
|
}
|
|
|
|
else if (seconds >= (1000l * 60l))
|
|
|
|
{
|
|
|
|
/* alternate (slow) method displaying hours and tenths */
|
|
|
|
sprintf(result, "%5.1fH", (double)seconds / (double)(60l * 60l));
|
|
|
|
|
|
|
|
/* It is possible that the sprintf took more than 6 characters.
|
|
|
|
If so, then the "H" appears as result[6]. If not, then there
|
|
|
|
is a \0 in result[6]. Either way, it is safe to step on.
|
|
|
|
*/
|
|
|
|
result[6] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* standard method produces MMM:SS */
|
|
|
|
sprintf(result, "%3ld:%02ld",
|
|
|
|
seconds / 60l, seconds % 60l);
|
|
|
|
}
|
|
|
|
return(result);
|
1997-03-23 18:51:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* format_k(amt) - format a kilobyte memory value, returning a string
|
|
|
|
* suitable for display. Returns a pointer to a static
|
2018-06-22 09:21:01 +00:00
|
|
|
* area that changes each call. "amt" is converted to a fixed
|
|
|
|
* size humanize_number call
|
1997-03-23 18:51:21 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compromise time. We need to return a string, but we don't want the
|
|
|
|
* caller to have to worry about freeing a dynamically allocated string.
|
|
|
|
* Unfortunately, we can't just return a pointer to a static area as one
|
|
|
|
* of the common uses of this function is in a large call to sprintf where
|
|
|
|
* it might get invoked several times. Our compromise is to maintain an
|
|
|
|
* array of strings and cycle thru them with each invocation. We make the
|
|
|
|
* array large enough to handle the above mentioned case. The constant
|
|
|
|
* NUM_STRINGS defines the number of strings in this array: we can tolerate
|
|
|
|
* up to NUM_STRINGS calls before we start overwriting old information.
|
|
|
|
* Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
|
|
|
|
* to convert the modulo operation into something quicker. What a hack!
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NUM_STRINGS 8
|
|
|
|
|
2018-06-08 01:55:47 +00:00
|
|
|
char *
|
2018-06-13 08:52:09 +00:00
|
|
|
format_k(int64_t amt)
|
1997-03-23 18:53:01 +00:00
|
|
|
{
|
|
|
|
static char retarray[NUM_STRINGS][16];
|
2019-02-10 13:34:21 +00:00
|
|
|
static int index_ = 0;
|
2018-05-20 01:32:27 +00:00
|
|
|
char *ret;
|
1997-03-23 18:53:01 +00:00
|
|
|
|
2019-02-10 13:34:21 +00:00
|
|
|
ret = retarray[index_];
|
|
|
|
index_ = (index_ + 1) % NUM_STRINGS;
|
2018-07-25 14:05:17 +00:00
|
|
|
humanize_number(ret, 6, amt * 1024, "", HN_AUTOSCALE, HN_NOSPACE);
|
2018-06-13 08:52:09 +00:00
|
|
|
return (ret);
|
1997-03-23 18:53:01 +00:00
|
|
|
}
|
2018-06-02 15:52:18 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
find_pid(pid_t pid)
|
|
|
|
{
|
|
|
|
kvm_t *kd = NULL;
|
|
|
|
struct kinfo_proc *pbase = NULL;
|
|
|
|
int nproc;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
kd = kvm_open(NULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL);
|
|
|
|
if (kd == NULL) {
|
|
|
|
fprintf(stderr, "top: kvm_open() failed.\n");
|
|
|
|
quit(TOP_EX_SYS_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
pbase = kvm_getprocs(kd, KERN_PROC_PID, pid, &nproc);
|
|
|
|
if (pbase == NULL) {
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((nproc == 1) && (pbase->ki_pid == pid)) {
|
|
|
|
ret = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
done:
|
2018-06-12 07:31:59 +00:00
|
|
|
kvm_close(kd);
|
2018-06-02 15:52:18 +00:00
|
|
|
return ret;
|
|
|
|
}
|