diff options
Diffstat (limited to 'usr.bin/top/machine.c')
-rw-r--r-- | usr.bin/top/machine.c | 59 |
1 files changed, 35 insertions, 24 deletions
diff --git a/usr.bin/top/machine.c b/usr.bin/top/machine.c index 10cf0b4a2bb..04c1d1b0443 100644 --- a/usr.bin/top/machine.c +++ b/usr.bin/top/machine.c @@ -1,4 +1,4 @@ -/* $OpenBSD: machine.c,v 1.92 2018/09/22 16:50:35 millert Exp $ */ +/* $OpenBSD: machine.c,v 1.93 2018/09/26 17:23:13 cheloha Exp $ */ /*- * Copyright (c) 1994 Thorsten Lockert <tholo@sigmasoft.com> @@ -111,6 +111,9 @@ char *cpustatenames[] = { "user", "nice", "sys", "spin", "intr", "idle", NULL }; +/* this tracks which cpus are online */ +int *cpu_online; + /* these are for detailing the memory statistics */ int memory_stats[10]; char *memorynames[] = { @@ -170,6 +173,20 @@ getncpu(void) } int +getncpuonline(void) +{ + int mib[] = { CTL_HW, HW_NCPUONLINE }; + int numcpu; + size_t size = sizeof(numcpu); + + if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), + &numcpu, &size, NULL, 0) == -1) + return (-1); + + return (numcpu); +} + +int machine_init(struct statics *statics) { int pagesize, cpu; @@ -182,6 +199,9 @@ machine_init(struct statics *statics) cpu_states = calloc(ncpu, CPUSTATES * sizeof(int64_t)); if (cpu_states == NULL) err(1, NULL); + cpu_online = calloc(ncpu, sizeof(*cpu_online)); + if (cpu_online == NULL) + err(1, NULL); cp_time = calloc(ncpu, sizeof(int64_t *)); cp_old = calloc(ncpu, sizeof(int64_t *)); cp_diff = calloc(ncpu, sizeof(int64_t *)); @@ -243,6 +263,7 @@ format_header(char *second_field, int show_threads) void get_system_info(struct system_info *si) { + static int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, /*fillme*/0}; static int sysload_mib[] = {CTL_VM, VM_LOADAVG}; static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP}; static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT}; @@ -254,31 +275,20 @@ get_system_info(struct system_info *si) int i; int64_t *tmpstate; - if (ncpu > 1) { - int cp_time_mib[] = {CTL_KERN, KERN_CPTIME2, /*fillme*/0}; - - size = CPUSTATES * sizeof(int64_t); - for (i = 0; i < ncpu; i++) { - cp_time_mib[2] = i; - tmpstate = cpu_states + (CPUSTATES * i); - if (sysctl(cp_time_mib, 3, cp_time[i], &size, NULL, 0) < 0) + size = CPUSTATES * sizeof(int64_t); + for (i = 0; i < ncpu; i++) { + cp_time_mib[2] = i; + tmpstate = cpu_states + (CPUSTATES * i); + if (sysctl(cp_time_mib, 3, cp_time[i], &size, NULL, 0) < 0) { + if (errno != ENODEV) warn("sysctl kern.cp_time2 failed"); - /* convert cp_time2 counts to percentages */ - (void) percentages(CPUSTATES, tmpstate, cp_time[i], - cp_old[i], cp_diff[i]); + cpu_online[i] = 0; + continue; } - } else { - int cp_time_mib[] = {CTL_KERN, KERN_CPTIME}; - long cp_time_tmp[CPUSTATES]; - - size = sizeof(cp_time_tmp); - if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) - warn("sysctl kern.cp_time failed"); - for (i = 0; i < CPUSTATES; i++) - cp_time[0][i] = cp_time_tmp[i]; - /* convert cp_time counts to percentages */ - (void) percentages(CPUSTATES, cpu_states, cp_time[0], - cp_old[0], cp_diff[0]); + cpu_online[i] = 1; + /* convert cp_time2 counts to percentages */ + (void) percentages(CPUSTATES, tmpstate, cp_time[i], + cp_old[i], cp_diff[i]); } size = sizeof(sysload); @@ -317,6 +327,7 @@ get_system_info(struct system_info *si) /* set arrays and strings */ si->cpustates = cpu_states; + si->cpuonline = cpu_online; si->memory = memory_stats; si->last_pid = -1; } |