From: Florian Forster Date: Tue, 22 Sep 2020 09:01:50 +0000 (+0200) Subject: Merge branch 'main' into collectd-6.0 X-Git-Tag: 6.0.0-rc0~140^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=bc02a12bb330fdf6f87ae0e5a6bbecd9d5bac6ab;p=thirdparty%2Fcollectd.git Merge branch 'main' into collectd-6.0 --- bc02a12bb330fdf6f87ae0e5a6bbecd9d5bac6ab diff --cc src/memory.c index 6f86a685d,440a5f9cb..a099ae6a3 --- a/src/memory.c +++ b/src/memory.c @@@ -235,6 -238,41 +248,42 @@@ static int memory_read_internal(value_l /* #endif HAVE_HOST_STATISTICS */ #elif HAVE_SYSCTLBYNAME + + #if HAVE_SYSCTL && defined(KERNEL_NETBSD) + int mib[] = {CTL_VM, VM_UVMEXP2}; + struct uvmexp_sysctl uvmexp; + gauge_t mem_active; + gauge_t mem_inactive; + gauge_t mem_free; + gauge_t mem_wired; + gauge_t mem_kernel; + size_t size; + + memset(&uvmexp, 0, sizeof(uvmexp)); + size = sizeof(uvmexp); + + if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) { + char errbuf[1024]; + WARNING("memory plugin: sysctl failed: %s", + sstrerror(errno, errbuf, sizeof(errbuf))); + return (-1); + } + + assert(pagesize > 0); + mem_active = (gauge_t)(uvmexp.active * pagesize); + mem_inactive = (gauge_t)(uvmexp.inactive * pagesize); + mem_free = (gauge_t)(uvmexp.free * pagesize); + mem_wired = (gauge_t)(uvmexp.wired * pagesize); + mem_kernel = (gauge_t)((uvmexp.npages - (uvmexp.active + uvmexp.inactive + + uvmexp.free + uvmexp.wired)) * + pagesize); + ++ /* TODO(octo): migrate to metric_family_t. */ + MEMORY_SUBMIT("active", mem_active, "inactive", mem_inactive, "free", + mem_free, "wired", mem_wired, "kernel", mem_kernel); + /* #endif HAVE_SYSCTL && defined(KERNEL_NETBSD) */ + + #else /* Other HAVE_SYSCTLBYNAME providers */ /* * vm.stats.vm.v_page_size: 4096 * vm.stats.vm.v_page_count: 246178 @@@ -244,43 -282,37 +293,45 @@@ * vm.stats.vm.v_inactive_count: 113730 * vm.stats.vm.v_cache_count: 10809 */ - const char *sysctl_keys[8] = { - "vm.stats.vm.v_page_size", "vm.stats.vm.v_page_count", - "vm.stats.vm.v_free_count", "vm.stats.vm.v_wire_count", - "vm.stats.vm.v_active_count", "vm.stats.vm.v_inactive_count", - "vm.stats.vm.v_cache_count", NULL}; - double sysctl_vals[8]; - - for (int i = 0; sysctl_keys[i] != NULL; i++) { - int value; + struct { + char const *sysctl_key; + char const *label_value; + } metrics[] = { + {"vm.stats.vm.v_page_size", NULL}, + {"vm.stats.vm.v_free_count", "free"}, + {"vm.stats.vm.v_wire_count", "wired"}, + {"vm.stats.vm.v_active_count", "active"}, + {"vm.stats.vm.v_inactive_count", "inactive"}, + {"vm.stats.vm.v_cache_count", "cache"}, + }; + + gauge_t page_size = 0; + + for (size_t i = 0; i < STATIC_ARRAY_SIZE(metrics); i++) { + int value = 0; size_t value_len = sizeof(value); - if (sysctlbyname(sysctl_keys[i], (void *)&value, &value_len, NULL, 0) == - 0) { - sysctl_vals[i] = value; - DEBUG("memory plugin: %26s: %g", sysctl_keys[i], sysctl_vals[i]); - } else { - sysctl_vals[i] = NAN; + int status = sysctlbyname(metrics[i].sysctl_key, (void *)&value, &value_len, + NULL, 0); + if (status != 0) { + WARNING("sysctlbyname(\"%s\") failed: %s", metrics[i].sysctl_key, + STRERROR(status)); + continue; } - } /* for (sysctl_keys) */ - /* multiply all all page counts with the pagesize */ - for (int i = 1; sysctl_keys[i] != NULL; i++) - if (!isnan(sysctl_vals[i])) - sysctl_vals[i] *= sysctl_vals[0]; + if (i == 0) { + page_size = (gauge_t)value; + continue; + } + + value_t v = {.gauge = page_size * (gauge_t)value}; + metric_family_append(&fam, "type", metrics[i].label_value, v, NULL); + } /* for (sysctl_keys) */ - MEMORY_SUBMIT("free", (gauge_t)sysctl_vals[2], "wired", - (gauge_t)sysctl_vals[3], "active", (gauge_t)sysctl_vals[4], - "inactive", (gauge_t)sysctl_vals[5], "cache", - (gauge_t)sysctl_vals[6]); + plugin_dispatch_metric_family(&fam); + metric_family_metric_reset(&fam); + + #endif /* HAVE_SYSCTL && KERNEL_NETBSD */ /* #endif HAVE_SYSCTLBYNAME */ #elif KERNEL_LINUX