[BACK]Return to utils.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / top

Annotation of src/usr.bin/top/utils.c, Revision 1.26

1.26    ! deraadt     1: /* $OpenBSD: utils.c,v 1.25 2015/01/16 06:40:13 deraadt Exp $   */
1.1       downsj      2:
                      3: /*
                      4:  *  Top users/processes display for Unix
                      5:  *  Version 3
                      6:  *
1.6       deraadt     7:  * Copyright (c) 1984, 1989, William LeFebvre, Rice University
                      8:  * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
1.1       downsj      9:  *
1.6       deraadt    10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     20:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     21:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     22:  * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
                     23:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     24:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     25:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     26:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     27:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     28:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1       downsj     29:  */
                     30:
                     31: /*
                     32:  *  This file contains various handy utilities used by top.
                     33:  */
                     34:
1.25      deraadt    35: #include <sys/types.h>
1.14      otto       36: #include <sys/sysctl.h>
1.16      millert    37: #include <err.h>
1.2       downsj     38: #include <stdio.h>
                     39: #include <string.h>
                     40: #include <stdlib.h>
1.19      otto       41: #include <stdint.h>
1.25      deraadt    42: #include <limits.h>
1.2       downsj     43:
1.1       downsj     44: #include "top.h"
1.14      otto       45: #include "machine.h"
1.13      deraadt    46: #include "utils.h"
1.1       downsj     47:
1.9       pvalchev   48: int
                     49: atoiwi(char *str)
1.1       downsj     50: {
1.12      deraadt    51:        size_t len;
1.17      otto       52:        const char *errstr;
                     53:        int i;
1.1       downsj     54:
1.10      deraadt    55:        len = strlen(str);
                     56:        if (len != 0) {
                     57:                if (strncmp(str, "infinity", len) == 0 ||
                     58:                    strncmp(str, "all", len) == 0 ||
                     59:                    strncmp(str, "maximum", len) == 0) {
                     60:                        return (Infinity);
1.26    ! deraadt    61:                }
1.17      otto       62:                i = (int)strtonum(str, 0, INT_MAX, &errstr);
                     63:                if (errstr) {
1.10      deraadt    64:                        return (Invalid);
1.17      otto       65:                } else
                     66:                        return (i);
1.1       downsj     67:        }
1.10      deraadt    68:        return (0);
1.1       downsj     69: }
                     70:
                     71: /*
1.11      millert    72:  * itoa - convert integer (decimal) to ascii string.
1.1       downsj     73:  */
1.9       pvalchev   74: char *
                     75: itoa(int val)
1.1       downsj     76: {
1.10      deraadt    77:        static char buffer[16]; /* result is built here */
                     78:
                     79:        /*
                     80:         * 16 is sufficient since the largest number we will ever convert
                     81:         * will be 2^32-1, which is 10 digits.
                     82:         */
1.11      millert    83:        (void)snprintf(buffer, sizeof(buffer), "%d", val);
                     84:        return (buffer);
1.1       downsj     85: }
                     86:
                     87: /*
1.11      millert    88:  * format_uid(uid) - like itoa, except for uid_t and the number is right
                     89:  * justified in a 6 character field to match uname_field in top.c.
1.1       downsj     90:  */
1.9       pvalchev   91: char *
1.11      millert    92: format_uid(uid_t uid)
1.1       downsj     93: {
1.11      millert    94:        static char buffer[16]; /* result is built here */
1.1       downsj     95:
1.11      millert    96:        /*
                     97:         * 16 is sufficient since the largest uid we will ever convert
                     98:         * will be 2^32-1, which is 10 digits.
                     99:         */
                    100:        (void)snprintf(buffer, sizeof(buffer), "%6u", uid);
                    101:        return (buffer);
1.1       downsj    102: }
                    103:
                    104: /*
1.10      deraadt   105:  * digits(val) - return number of decimal digits in val.  Only works for
                    106:  * positive numbers.  If val <= 0 then digits(val) == 0.
1.1       downsj    107:  */
1.9       pvalchev  108: int
                    109: digits(int val)
1.1       downsj    110: {
1.10      deraadt   111:        int cnt = 0;
1.1       downsj    112:
1.10      deraadt   113:        while (val > 0) {
                    114:                cnt++;
                    115:                val /= 10;
                    116:        }
                    117:        return (cnt);
1.1       downsj    118: }
                    119:
                    120: /*
                    121:  * string_index(string, array) - find string in array and return index
                    122:  */
1.9       pvalchev  123: int
                    124: string_index(char *string, char **array)
1.1       downsj    125: {
1.10      deraadt   126:        int i = 0;
1.1       downsj    127:
1.10      deraadt   128:        while (*array != NULL) {
1.22      tedu      129:                if (strncmp(string, *array, strlen(string)) == 0)
1.10      deraadt   130:                        return (i);
                    131:                array++;
                    132:                i++;
1.1       downsj    133:        }
1.10      deraadt   134:        return (-1);
1.1       downsj    135: }
                    136:
                    137: /*
                    138:  * argparse(line, cntp) - parse arguments in string "line", separating them
1.10      deraadt   139:  * out into an argv-like array, and setting *cntp to the number of
                    140:  * arguments encountered.  This is a simple parser that doesn't understand
                    141:  * squat about quotes.
1.1       downsj    142:  */
1.9       pvalchev  143: char **
                    144: argparse(char *line, int *cntp)
1.1       downsj    145: {
1.10      deraadt   146:        char **argv, **argarray, *args, *from, *to;
                    147:        int cnt, ch, length, lastch;
                    148:
                    149:        /*
                    150:         * unfortunately, the only real way to do this is to go thru the
                    151:         * input string twice.
                    152:         */
                    153:
                    154:        /* step thru the string counting the white space sections */
                    155:        from = line;
                    156:        lastch = cnt = length = 0;
                    157:        while ((ch = *from++) != '\0') {
                    158:                length++;
                    159:                if (ch == ' ' && lastch != ' ')
                    160:                        cnt++;
                    161:                lastch = ch;
1.1       downsj    162:        }
                    163:
1.10      deraadt   164:        /*
                    165:         * add three to the count:  one for the initial "dummy" argument, one
                    166:         * for the last argument and one for NULL
                    167:         */
                    168:        cnt += 3;
                    169:
                    170:        /* allocate a char * array to hold the pointers */
1.20      deraadt   171:        if ((argarray = calloc(cnt, sizeof(char *))) == NULL)
1.16      millert   172:                err(1, NULL);
1.10      deraadt   173:
                    174:        /* allocate another array to hold the strings themselves */
1.16      millert   175:        if ((args = malloc(length + 2)) == NULL)
                    176:                err(1, NULL);
1.10      deraadt   177:
                    178:        /* initialization for main loop */
                    179:        from = line;
                    180:        to = args;
                    181:        argv = argarray;
                    182:        lastch = '\0';
                    183:
                    184:        /* create a dummy argument to keep getopt happy */
                    185:        *argv++ = to;
                    186:        *to++ = '\0';
                    187:        cnt = 2;
                    188:
                    189:        /* now build argv while copying characters */
                    190:        *argv++ = to;
                    191:        while ((ch = *from++) != '\0') {
                    192:                if (ch != ' ') {
                    193:                        if (lastch == ' ') {
                    194:                                *to++ = '\0';
                    195:                                *argv++ = to;
                    196:                                cnt++;
                    197:                        }
                    198:                        *to++ = ch;
                    199:                }
                    200:                lastch = ch;
1.1       downsj    201:        }
1.10      deraadt   202:        *to++ = '\0';
                    203:
                    204:        /* set cntp and return the allocated array */
                    205:        *cntp = cnt;
                    206:        return (argarray);
1.1       downsj    207: }
                    208:
                    209: /*
1.10      deraadt   210:  * percentages(cnt, out, new, old, diffs) - calculate percentage change
1.21      otto      211:  * between array "old" and "new", putting the percentages in "out".
1.10      deraadt   212:  * "cnt" is size of each array and "diffs" is used for scratch space.
                    213:  * The array "old" is updated on each call.
                    214:  * The routine assumes modulo arithmetic.  This function is especially
1.21      otto      215:  * useful on BSD machines for calculating cpu state percentages.
1.1       downsj    216:  */
1.9       pvalchev  217: int
1.16      millert   218: percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
1.1       downsj    219: {
1.16      millert   220:        int64_t change, total_change, *dp, half_total;
1.10      deraadt   221:        int i;
                    222:
                    223:        /* initialization */
                    224:        total_change = 0;
                    225:        dp = diffs;
                    226:
                    227:        /* calculate changes for each state and the overall change */
                    228:        for (i = 0; i < cnt; i++) {
                    229:                if ((change = *new - *old) < 0) {
                    230:                        /* this only happens when the counter wraps */
1.19      otto      231:                        change = INT64_MAX - *old + *new;
1.10      deraadt   232:                }
                    233:                total_change += (*dp++ = change);
                    234:                *old++ = *new++;
1.1       downsj    235:        }
                    236:
1.10      deraadt   237:        /* avoid divide by zero potential */
                    238:        if (total_change == 0)
                    239:                total_change = 1;
                    240:
                    241:        /* calculate percentages based on overall change, rounding up */
                    242:        half_total = total_change / 2l;
                    243:        for (i = 0; i < cnt; i++)
                    244:                *out++ = ((*diffs++ * 1000 + half_total) / total_change);
                    245:
                    246:        /* return the total in case the caller wants to use it */
                    247:        return (total_change);
1.1       downsj    248: }
                    249:
1.10      deraadt   250: /*
                    251:  * format_time(seconds) - format number of seconds into a suitable display
                    252:  * that will fit within 6 characters.  Note that this routine builds its
                    253:  * string in a static area.  If it needs to be called more than once without
                    254:  * overwriting previous data, then we will need to adopt a technique similar
                    255:  * to the one used for format_k.
1.1       downsj    256:  */
                    257:
1.10      deraadt   258: /*
                    259:  * Explanation: We want to keep the output within 6 characters.  For low
                    260:  * values we use the format mm:ss.  For values that exceed 999:59, we switch
                    261:  * to a format that displays hours and fractions:  hhh.tH.  For values that
                    262:  * exceed 999.9, we use hhhh.t and drop the "H" designator.  For values that
                    263:  * exceed 9999.9, we use "???".
1.1       downsj    264:  */
                    265:
1.9       pvalchev  266: char *
                    267: format_time(time_t seconds)
1.1       downsj    268: {
1.10      deraadt   269:        static char result[10];
1.1       downsj    270:
1.10      deraadt   271:        /* sanity protection */
                    272:        if (seconds < 0 || seconds > (99999l * 360l)) {
                    273:                strlcpy(result, "   ???", sizeof result);
                    274:        } else if (seconds >= (1000l * 60l)) {
                    275:                /* alternate (slow) method displaying hours and tenths */
                    276:                snprintf(result, sizeof(result), "%5.1fH",
                    277:                    (double) seconds / (double) (60l * 60l));
                    278:
                    279:                /*
                    280:                 * It is possible that the snprintf took more than 6
                    281:                 * characters. If so, then the "H" appears as result[6].  If
                    282:                 * not, then there is a \0 in result[6].  Either way, it is
                    283:                 * safe to step on.
                    284:                 */
                    285:                result[6] = '\0';
                    286:        } else {
                    287:                /* standard method produces MMM:SS */
                    288:                /* we avoid printf as must as possible to make this quick */
1.24      guenther  289:                snprintf(result, sizeof(result), "%3d:%02d", (int)seconds / 60,
                    290:                    (int)seconds % 60);
1.10      deraadt   291:        }
                    292:        return (result);
1.1       downsj    293: }
                    294:
                    295: /*
                    296:  * format_k(amt) - format a kilobyte memory value, returning a string
1.10      deraadt   297:  * suitable for display.  Returns a pointer to a static
                    298:  * area that changes each call.  "amt" is converted to a
                    299:  * string with a trailing "K".  If "amt" is 10000 or greater,
                    300:  * then it is formatted as megabytes (rounded) with a
                    301:  * trailing "M".
1.1       downsj    302:  */
                    303:
                    304: /*
                    305:  * Compromise time.  We need to return a string, but we don't want the
                    306:  * caller to have to worry about freeing a dynamically allocated string.
                    307:  * Unfortunately, we can't just return a pointer to a static area as one
1.8       deraadt   308:  * of the common uses of this function is in a large call to snprintf where
1.1       downsj    309:  * it might get invoked several times.  Our compromise is to maintain an
                    310:  * array of strings and cycle thru them with each invocation.  We make the
                    311:  * array large enough to handle the above mentioned case.  The constant
                    312:  * NUM_STRINGS defines the number of strings in this array:  we can tolerate
                    313:  * up to NUM_STRINGS calls before we start overwriting old information.
                    314:  * Keeping NUM_STRINGS a power of two will allow an intelligent optimizer
                    315:  * to convert the modulo operation into something quicker.  What a hack!
                    316:  */
                    317:
                    318: #define NUM_STRINGS 8
                    319:
1.9       pvalchev  320: char *
                    321: format_k(int amt)
1.1       downsj    322: {
1.10      deraadt   323:        static char retarray[NUM_STRINGS][16];
1.12      deraadt   324:        static int  idx = 0;
1.11      millert   325:        char *ret, tag = 'K';
1.10      deraadt   326:
1.12      deraadt   327:        ret = retarray[idx];
                    328:        idx = (idx + 1) % NUM_STRINGS;
1.10      deraadt   329:
                    330:        if (amt >= 10000) {
                    331:                amt = (amt + 512) / 1024;
                    332:                tag = 'M';
                    333:                if (amt >= 10000) {
                    334:                        amt = (amt + 512) / 1024;
                    335:                        tag = 'G';
                    336:                }
1.1       downsj    337:        }
1.11      millert   338:        snprintf(ret, sizeof(retarray[0]), "%d%c", amt, tag);
1.10      deraadt   339:        return (ret);
1.14      otto      340: }
                    341:
                    342: int
                    343: find_pid(pid_t pid)
                    344: {
1.23      guenther  345:        struct kinfo_proc *pbase, *cur;
1.15      pat       346:        int nproc;
1.14      otto      347:
                    348:        if ((pbase = getprocs(KERN_PROC_KTHREAD, 0, &nproc)) == NULL)
                    349:                quit(23);
                    350:
1.15      pat       351:        for (cur = pbase; cur < &pbase[nproc]; cur++)
1.14      otto      352:                if (cur->p_pid == pid)
                    353:                        return 1;
                    354:        return 0;
1.1       downsj    355: }