diff --git a/include/sigar_util.h b/include/sigar_util.h index 2698bdf0..b81196c2 100644 --- a/include/sigar_util.h +++ b/include/sigar_util.h @@ -139,6 +139,11 @@ sigar_iodev_t *sigar_iodev_get(sigar_t *sigar, int sigar_cpu_core_count(sigar_t *sigar); +/* e.g. VM guest may have 1 virtual ncpu on multicore hosts */ +#define sigar_cpu_socket_count(sigar) \ + (sigar->ncpu < sigar->lcpu) ? sigar->ncpu : \ + (sigar->ncpu / sigar->lcpu) + int sigar_cpu_core_rollup(sigar_t *sigar); void sigar_cpu_model_adjust(sigar_t *sigar, sigar_cpu_info_t *info); diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index fadceead..e06c8799 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -2133,7 +2133,7 @@ int sigar_cpu_info_list_get(sigar_t *sigar, info->mhz = mhz; info->cache_size = cache_size; info->total_cores = sigar->ncpu; - info->total_sockets = sigar->ncpu / lcpu; + info->total_sockets = sigar_cpu_socket_count(sigar); } return SIGAR_OK; diff --git a/src/os/linux/linux_sigar.c b/src/os/linux/linux_sigar.c index 355701bc..cf778f4c 100644 --- a/src/os/linux/linux_sigar.c +++ b/src/os/linux/linux_sigar.c @@ -212,7 +212,8 @@ char *sigar_os_error_string(sigar_t *sigar, int err) static int sigar_cpu_total_count(sigar_t *sigar) { - return (int)sysconf(_SC_NPROCESSORS_CONF); + sigar->ncpu = (int)sysconf(_SC_NPROCESSORS_CONF); + return sigar->ncpu; } static int get_ram(sigar_t *sigar, sigar_mem_t *mem) @@ -1569,12 +1570,12 @@ int sigar_cpu_info_list_get(sigar_t *sigar, { FILE *fp; int core_rollup = sigar_cpu_core_rollup(sigar), i=0; - int ncpu = sigar_cpu_total_count(sigar); if (!(fp = fopen(PROC_FS_ROOT "cpuinfo", "r"))) { return errno; } + (void)sigar_cpu_total_count(sigar); sigar_cpu_info_list_create(cpu_infos); while (get_cpu_info(sigar, &cpu_infos->data[cpu_infos->number], fp)) { @@ -1588,8 +1589,8 @@ int sigar_cpu_info_list_get(sigar_t *sigar, cpu_info = &cpu_infos->data[cpu_infos->number]; get_cpuinfo_max_freq(cpu_info, cpu_infos->number); - cpu_info->total_sockets = ncpu / sigar->lcpu; - cpu_info->total_cores = ncpu; + cpu_info->total_sockets = sigar_cpu_socket_count(sigar); + cpu_info->total_cores = sigar->ncpu; ++cpu_infos->number; } diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index c12fd7e0..151e51aa 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -2005,7 +2005,7 @@ static int sigar_cpu_info_get(sigar_t *sigar, sigar_cpu_info_t *info) RegCloseKey(key); RegCloseKey(cpu); - info->total_sockets = sigar->ncpu / sigar->lcpu; + info->total_sockets = sigar_cpu_socket_count(sigar); info->total_cores = sigar->ncpu; return SIGAR_OK;