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

Annotation of src/usr.bin/systat/cpu.c, Revision 1.6

1.6     ! mpi         1: /*     $OpenBSD: cpu.c,v 1.5 2016/01/02 20:02:40 benno Exp $   */
1.1       reyk        2:
                      3: /*
                      4:  * Copyright (c) 2013 Reyk Floeter <reyk@openbsd.org>
                      5:  * Copyright (c) 2001, 2007 Can Erkin Acar <canacar@openbsd.org>
                      6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19:
                     20: /* CPU percentages() function from usr.bin/top/util.c:
                     21:  *
                     22:  *  Top users/processes display for Unix
                     23:  *  Version 3
                     24:  *
                     25:  * Copyright (c) 1984, 1989, William LeFebvre, Rice University
                     26:  * Copyright (c) 1989, 1990, 1992, William LeFebvre, Northwestern University
                     27:  *
                     28:  * Redistribution and use in source and binary forms, with or without
                     29:  * modification, are permitted provided that the following conditions
                     30:  * are met:
                     31:  * 1. Redistributions of source code must retain the above copyright
                     32:  *    notice, this list of conditions and the following disclaimer.
                     33:  * 2. Redistributions in binary form must reproduce the above copyright
                     34:  *    notice, this list of conditions and the following disclaimer in the
                     35:  *    documentation and/or other materials provided with the distribution.
                     36:  *
                     37:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     38:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     39:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     40:  * IN NO EVENT SHALL THE AUTHOR OR HIS EMPLOYER BE LIABLE FOR ANY DIRECT, INDIRECT,
                     41:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     42:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     43:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     44:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     45:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     46:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
                     47:  */
                     48:
1.4       deraadt    49: #include <sys/signal.h>
1.3       miod       50: #include <sys/sched.h>
1.1       reyk       51: #include <sys/sysctl.h>
                     52:
                     53: #include <stdlib.h>
                     54: #include <stdint.h>
                     55: #include <string.h>
                     56: #include <unistd.h>
                     57: #include "systat.h"
                     58:
                     59: void            print_cpu(void);
                     60: int             read_cpu(void);
                     61: int             select_cpu(void);
                     62: static void     cpu_info(void);
                     63: static void     print_fld_percentage(field_def *, double);
                     64: static int      percentages(int, int64_t *, int64_t *, int64_t *, int64_t *);
                     65:
                     66: field_def fields_cpu[] = {
                     67:        { "CPU", 4, 8, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0 },
                     68:        { "User", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
                     69:        { "Nice", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
                     70:        { "System", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
1.6     ! mpi        71:        { "Spin", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
1.1       reyk       72:        { "Interrupt", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
                     73:        { "Idle", 10, 20, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0 },
                     74: };
                     75:
                     76: #define FLD_CPU_CPU    FIELD_ADDR(fields_cpu, 0)
1.6     ! mpi        77: #define FLD_CPU_USR    FIELD_ADDR(fields_cpu, 1)
        !            78: #define FLD_CPU_NIC    FIELD_ADDR(fields_cpu, 2)
        !            79: #define FLD_CPU_SYS    FIELD_ADDR(fields_cpu, 3)
        !            80: #define FLD_CPU_SPIN   FIELD_ADDR(fields_cpu, 4)
        !            81: #define FLD_CPU_INT    FIELD_ADDR(fields_cpu, 5)
        !            82: #define FLD_CPU_IDLE   FIELD_ADDR(fields_cpu, 6)
1.1       reyk       83:
                     84: /* Define views */
                     85: field_def *view_cpu_0[] = {
1.6     ! mpi        86:        FLD_CPU_CPU, FLD_CPU_USR, FLD_CPU_NIC, FLD_CPU_SYS, FLD_CPU_SPIN,
        !            87:        FLD_CPU_INT, FLD_CPU_IDLE, NULL
1.1       reyk       88: };
                     89:
                     90: /* Define view managers */
                     91: struct view_manager cpu_mgr = {
                     92:        "cpu", select_cpu, read_cpu, NULL, print_header,
                     93:        print_cpu, keyboard_callback, NULL, NULL
                     94: };
                     95:
                     96: field_view views_cpu[] = {
                     97:        { view_cpu_0, "cpu", 'C', &cpu_mgr },
                     98:        { NULL, NULL, 0, NULL }
                     99: };
                    100:
                    101: int      cpu_count;
                    102: int64_t         *cpu_states;
                    103: int64_t        **cpu_tm;
                    104: int64_t        **cpu_old;
                    105: int64_t        **cpu_diff;
                    106:
                    107: /*
                    108:  * percentages(cnt, out, new, old, diffs) - calculate percentage change
                    109:  * between array "old" and "new", putting the percentages in "out".
                    110:  * "cnt" is size of each array and "diffs" is used for scratch space.
                    111:  * The array "old" is updated on each call.
                    112:  * The routine assumes modulo arithmetic.  This function is especially
                    113:  * useful on BSD machines for calculating cpu state percentages.
                    114:  */
                    115: static int
                    116: percentages(int cnt, int64_t *out, int64_t *new, int64_t *old, int64_t *diffs)
                    117: {
                    118:        int64_t change, total_change, *dp, half_total;
                    119:        int i;
                    120:
                    121:        /* initialization */
                    122:        total_change = 0;
                    123:        dp = diffs;
                    124:
                    125:        /* calculate changes for each state and the overall change */
                    126:        for (i = 0; i < cnt; i++) {
                    127:                if ((change = *new - *old) < 0) {
                    128:                        /* this only happens when the counter wraps */
                    129:                        change = INT64_MAX - *old + *new;
                    130:                }
                    131:                total_change += (*dp++ = change);
                    132:                *old++ = *new++;
                    133:        }
                    134:
                    135:        /* avoid divide by zero potential */
                    136:        if (total_change == 0)
                    137:                total_change = 1;
                    138:
                    139:        /* calculate percentages based on overall change, rounding up */
                    140:        half_total = total_change / 2l;
                    141:        for (i = 0; i < cnt; i++)
                    142:                *out++ = ((*diffs++ * 1000 + half_total) / total_change);
                    143:
                    144:        /* return the total in case the caller wants to use it */
                    145:        return (total_change);
                    146: }
                    147:
                    148: static void
                    149: cpu_info(void)
                    150: {
                    151:        int      cpu_time_mib[] = { CTL_KERN, KERN_CPTIME2, 0 }, i;
                    152:        int64_t *tmpstate;
                    153:        size_t   size;
                    154:
                    155:        size = CPUSTATES * sizeof(int64_t);
                    156:        for (i = 0; i < cpu_count; i++) {
                    157:                cpu_time_mib[2] = i;
                    158:                tmpstate = cpu_states + (CPUSTATES * i);
                    159:                if (sysctl(cpu_time_mib, 3, cpu_tm[i], &size, NULL, 0) < 0)
                    160:                        error("sysctl KERN_CPTIME2");
                    161:                percentages(CPUSTATES, tmpstate, cpu_tm[i],
                    162:                    cpu_old[i], cpu_diff[i]);
                    163:        }
                    164: }
                    165:
                    166: static void
                    167: print_fld_percentage(field_def *fld, double val)
                    168: {
                    169:        if (fld == NULL)
                    170:                return;
                    171:
                    172:        tb_start();
                    173:        tbprintf(val >= 1000 ? "%4.0f%%" : "%4.1f%%", val / 10.);
                    174:        print_fld_tb(fld);
                    175:        tb_end();
                    176: }
                    177:
                    178: int
                    179: select_cpu(void)
                    180: {
                    181:        return (0);
                    182: }
                    183:
                    184: int
                    185: read_cpu(void)
                    186: {
                    187:        cpu_info();
                    188:        num_disp = cpu_count;
                    189:        return (0);
                    190: }
                    191:
                    192: int
                    193: initcpu(void)
                    194: {
                    195:        field_view      *v;
                    196:        size_t           size = sizeof(cpu_count);
                    197:        int              mib[2], i;
                    198:
                    199:        mib[0] = CTL_HW;
                    200:        mib[1] = HW_NCPU;
                    201:        if (sysctl(mib, 2, &cpu_count, &size, NULL, 0) == -1)
                    202:                return (-1);
                    203:        if ((cpu_states = calloc(cpu_count,
                    204:            CPUSTATES * sizeof(int64_t))) == NULL)
                    205:                return (-1);
                    206:        if ((cpu_tm = calloc(cpu_count, sizeof(int64_t *))) == NULL ||
                    207:            (cpu_old = calloc(cpu_count, sizeof(int64_t *))) == NULL ||
                    208:            (cpu_diff = calloc(cpu_count, sizeof(int64_t *))) == NULL)
                    209:                return (-1);
                    210:        for (i = 0; i < cpu_count; i++) {
                    211:                if ((cpu_tm[i] = calloc(CPUSTATES, sizeof(int64_t))) == NULL ||
                    212:                    (cpu_old[i] = calloc(CPUSTATES, sizeof(int64_t))) == NULL ||
                    213:                    (cpu_diff[i] = calloc(CPUSTATES, sizeof(int64_t))) == NULL)
                    214:                        return (-1);
                    215:        }
                    216:
                    217:        for (v = views_cpu; v->name != NULL; v++)
                    218:                add_view(v);
                    219:
                    220:        read_cpu();
                    221:
                    222:        return(1);
                    223: }
                    224:
                    225: #define ADD_EMPTY_LINE \
                    226:        do {                                                            \
                    227:                if (cur >= dispstart && cur < end)                      \
                    228:                        end_line();                                     \
                    229:                if (++cur >= end)                                       \
                    230:                        return;                                         \
                    231:        } while (0)
                    232:
                    233: #define ADD_LINE_CPU(v, cs) \
                    234:        do {                                                            \
                    235:                if (cur >= dispstart && cur < end) {                    \
                    236:                        print_fld_size(FLD_CPU_CPU, (v));               \
1.6     ! mpi       237:                        print_fld_percentage(FLD_CPU_USR, (cs[CP_USER]));\
        !           238:                        print_fld_percentage(FLD_CPU_NIC, (cs[CP_NICE]));\
        !           239:                        print_fld_percentage(FLD_CPU_SYS, (cs[CP_SYS]));\
        !           240:                        print_fld_percentage(FLD_CPU_SPIN, (cs[CP_SPIN]));\
        !           241:                        print_fld_percentage(FLD_CPU_INT, (cs[CP_INTR]));\
        !           242:                        print_fld_percentage(FLD_CPU_IDLE, (cs[CP_IDLE]));      \
1.1       reyk      243:                        end_line();                                     \
                    244:                }                                                       \
                    245:                if (++cur >= end)                                       \
                    246:                        return;                                         \
                    247:        } while (0)
                    248:
                    249: void
                    250: print_cpu(void)
                    251: {
                    252:        int             cur = 0, c, i;
                    253:        int             end = dispstart + maxprint;
                    254:        int64_t         *states;
                    255:        double          value[CPUSTATES];
                    256:
                    257:        if (end > num_disp)
                    258:                end = num_disp;
                    259:
                    260:        for (c = 0; c < cpu_count; c++) {
                    261:                states = cpu_states + (CPUSTATES * c);
                    262:
                    263:                for (i = 0; i < CPUSTATES; i++)
                    264:                        value[i] = *states++;
                    265:
                    266:                ADD_LINE_CPU(c, value);
                    267:        }
                    268:
                    269:        ADD_EMPTY_LINE;
                    270: }