From 404bdb343d464d4cfeb2e868be87fed2ba86991b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Luis=20Sanmart=C3=ADn=20Rozada?= Date: Thu, 21 Jan 2021 00:17:18 +0100 Subject: [PATCH 01/15] ChangeLog: swap: plugin: migration to v6.0 --- src/swap.c | 309 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 216 insertions(+), 93 deletions(-) diff --git a/src/swap.c b/src/swap.c index 979d265ed9..2213da2b7a 100644 --- a/src/swap.c +++ b/src/swap.c @@ -118,6 +118,20 @@ static bool values_absolute = true; static bool values_percentage; static bool report_io = true; +enum { + FAM_SWAP_USED = 0, + FAM_SWAP_FREE, + FAM_SWAP_CACHED, + FAM_SWAP_RESERVED, + FAM_SWAP_USED_PCT, + FAM_SWAP_FREE_PCT, + FAM_SWAP_CACHED_PCT, + FAM_SWAP_RESERVED_PCT, + FAM_SWAP_IN, + FAM_SWAP_OUT, + FAM_SWAP_MAX, +}; + static int swap_config(oconfig_item_t *ci) /* {{{ */ { for (int i = 0; i < ci->children_num; i++) { @@ -195,44 +209,65 @@ static int swap_init(void) /* {{{ */ return 0; } /* }}} int swap_init */ -static void swap_submit_usage(char const *plugin_instance, /* {{{ */ - gauge_t used, gauge_t free, - char const *other_name, gauge_t other_value) { - value_list_t vl = VALUE_LIST_INIT; - - vl.values = &(value_t){.gauge = NAN}; - vl.values_len = 1; - sstrncpy(vl.plugin, "swap", sizeof(vl.plugin)); - if (plugin_instance != NULL) - sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance)); - sstrncpy(vl.type, "swap", sizeof(vl.type)); - - if (values_absolute) - plugin_dispatch_multivalue(&vl, false, DS_TYPE_GAUGE, "used", used, "free", - free, other_name, other_value, NULL); - if (values_percentage) - plugin_dispatch_multivalue(&vl, true, DS_TYPE_GAUGE, "used", used, "free", - free, other_name, other_value, NULL); +static void swap_submit_usage(char *device, /* {{{ */ + metric_family_t *fam_used, + metric_family_t *fam_used_pct, gauge_t used, + metric_family_t *fam_free, + metric_family_t *fam_free_pct, gauge_t free, + metric_family_t *fam_other, + metric_family_t *fam_other_pct, gauge_t other) { + metric_t m = {0}; + + if (device != NULL) { + metric_label_set(&m, "device", device); + } + + if (values_absolute) { + if (fam_other != NULL) { + m.value.gauge = other; + metric_family_metric_append(fam_other, m); + } + + m.value.gauge = used; + metric_family_metric_append(fam_used, m); + + m.value.gauge = free; + metric_family_metric_append(fam_free, m); + } + + if (values_percentage) { + gauge_t total = used + free; + if (fam_other_pct != NULL) { + total += other; + m.value.gauge = 100.0 * other / total; + metric_family_metric_append(fam_other_pct, m); + } + + m.value.gauge = 100.0 * used / total; + metric_family_metric_append(fam_used_pct, m); + + m.value.gauge = 100.0 * free / total; + metric_family_metric_append(fam_free_pct, m); + } + + metric_reset(&m); } /* }}} void swap_submit_usage */ #if KERNEL_LINUX || HAVE_PERFSTAT || KERNEL_NETBSD -__attribute__((nonnull(1))) static void -swap_submit_derive(char const *type_instance, /* {{{ */ - derive_t value) { - value_list_t vl = VALUE_LIST_INIT; - - vl.values = &(value_t){.derive = value}; - vl.values_len = 1; - sstrncpy(vl.plugin, "swap", sizeof(vl.plugin)); - sstrncpy(vl.type, "swap_io", sizeof(vl.type)); - sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); - - plugin_dispatch_values(&vl); -} /* }}} void swap_submit_derive */ +static void swap_submit_io(metric_family_t *fam_in, counter_t in, /* {{{ */ + metric_family_t *fam_out, counter_t out) { + + metric_family_metric_append(fam_in, (metric_t){ + .value.counter = in, + }); + metric_family_metric_append(fam_out, (metric_t){ + .value.counter = out, + }); +} /* }}} void swap_submit_io */ #endif #if KERNEL_LINUX -static int swap_read_separate(void) /* {{{ */ +static int swap_read_separate(metric_family_t *fams[]) /* {{{ */ { FILE *fh; char buffer[1024]; @@ -257,7 +292,6 @@ static int swap_read_separate(void) /* {{{ */ continue; sstrncpy(path, fields[0], sizeof(path)); - escape_slashes(path, sizeof(path)); errno = 0; endptr = NULL; @@ -274,7 +308,10 @@ static int swap_read_separate(void) /* {{{ */ if (total < used) continue; - swap_submit_usage(path, used * 1024.0, (total - used) * 1024.0, NULL, NAN); + swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + used * 1024.0, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], (total - used) * 1024.0, NULL, + NULL, NAN); } fclose(fh); @@ -282,7 +319,7 @@ static int swap_read_separate(void) /* {{{ */ return 0; } /* }}} int swap_read_separate */ -static int swap_read_combined(void) /* {{{ */ +static int swap_read_combined(metric_family_t *fams[]) /* {{{ */ { FILE *fh; char buffer[1024]; @@ -329,13 +366,16 @@ static int swap_read_combined(void) /* {{{ */ if (swap_used < 0.0) return EINVAL; - swap_submit_usage(NULL, swap_used * 1024.0, swap_free * 1024.0, - isnan(swap_cached) ? NULL : "cached", + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + swap_used * 1024.0, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], swap_free * 1024.0, + isnan(swap_cached) ? NULL : fams[FAM_SWAP_CACHED], + isnan(swap_cached) ? NULL : fams[FAM_SWAP_CACHED_PCT], isnan(swap_cached) ? NAN : swap_cached * 1024.0); return 0; } /* }}} int swap_read_combined */ -static int swap_read_io(void) /* {{{ */ +static int swap_read_io(metric_family_t *fams[]) /* {{{ */ { char buffer[1024]; @@ -375,24 +415,23 @@ static int swap_read_io(void) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_derive("in", swap_in); - swap_submit_derive("out", swap_out); + swap_submit_io(fams[FAM_SWAP_IN], swap_in, fams[FAM_SWAP_OUT], swap_out); return 0; } /* }}} int swap_read_io */ -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { if (report_by_device) - swap_read_separate(); + swap_read_separate(fams); else - swap_read_combined(); + swap_read_combined(fams); if (report_io) - swap_read_io(); + swap_read_io(fams); return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif KERNEL_LINUX */ /* @@ -406,7 +445,7 @@ static int swap_read(void) /* {{{ */ */ #elif 0 && HAVE_LIBKSTAT /* kstat-based read function */ -static int swap_read_kstat(void) /* {{{ */ +static int swap_read_kstat(metric_family_t *fams[]) /* {{{ */ { gauge_t swap_alloc; gauge_t swap_resv; @@ -444,14 +483,17 @@ static int swap_read_kstat(void) /* {{{ */ swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); - swap_submit_usage(NULL, swap_alloc, swap_avail, "reserved", swap_resv); + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + swap_alloc, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + swap_avail, fams[FAM_SWAP_RESERVED], + fams[FAM_SWAP_RESERVED_PCT], swap_resv); return 0; } /* }}} int swap_read_kstat */ /* #endif 0 && HAVE_LIBKSTAT */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS /* swapctl-based read function */ -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { swaptbl_t *s; char *s_paths; @@ -526,9 +568,10 @@ static int swap_read(void) /* {{{ */ } sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path)); - escape_slashes(path, sizeof(path)); - swap_submit_usage(path, this_total - this_avail, this_avail, NULL, NAN); + swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + this_total - this_avail, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], this_avail, NULL, NULL, NAN); } /* for (swap_num) */ if (total < avail) { @@ -542,25 +585,28 @@ static int swap_read(void) /* {{{ */ /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ - if (!report_by_device) - swap_submit_usage(NULL, total - avail, avail, NULL, NAN); + if (!report_by_device) { + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + total - avail, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], avail, NULL, NULL, NAN); + } sfree(s_paths); sfree(s); return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS #if KERNEL_NETBSD #include -static int swap_read_io(void) /* {{{ */ +static int swap_read_io(metric_family_t *fams[]) /* {{{ */ { static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2}; struct uvmexp_sysctl uvmexp; size_t ssize; - derive_t swap_in, swap_out; + counter_t swap_in, swap_out; ssize = sizeof(uvmexp); memset(&uvmexp, 0, ssize); @@ -580,14 +626,13 @@ static int swap_read_io(void) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_derive("in", swap_in); - swap_submit_derive("out", swap_out); + swap_submit_io(fams[FAM_SWAP_IN], swap_in, fams[FAM_SWAP_OUT], swap_out); return (0); } /* }}} */ #endif -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { struct swapent *swap_entries; int swap_num; @@ -643,9 +688,10 @@ static int swap_read(void) /* {{{ */ } sstrncpy(path, swap_entries[i].se_path, sizeof(path)); - escape_slashes(path, sizeof(path)); - swap_submit_usage(path, this_used, this_total - this_used, NULL, NAN); + swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + this_used, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + this_total - this_used, NULL, NULL, NAN); } /* for (swap_num) */ if (total < used) { @@ -658,19 +704,22 @@ static int swap_read(void) /* {{{ */ /* If the "separate" option was specified (report_by_device == 1), all * values have already been dispatched from within the loop. */ - if (!report_by_device) - swap_submit_usage(NULL, used, total - used, NULL, NAN); + if (!report_by_device) { + swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], used, + fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + total - used, NULL, NULL, NAN); + } sfree(swap_entries); #if KERNEL_NETBSD - swap_read_io(); + swap_read_io(fams); #endif return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */ #elif defined(VM_SWAPUSAGE) -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { int mib[3]; size_t mib_len; @@ -687,22 +736,21 @@ static int swap_read(void) /* {{{ */ return -1; /* The returned values are bytes. */ - swap_submit_usage(NULL, (gauge_t)sw_usage.xsu_used, - (gauge_t)sw_usage.xsu_avail, NULL, NAN); + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + (gauge_t)sw_usage.xsu_used, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], (gauge_t)sw_usage.xsu_avail, NULL, + NULL, NAN); return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif VM_SWAPUSAGE */ #elif HAVE_LIBKVM_GETSWAPINFO -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { struct kvm_swap data_s; int status; - gauge_t used; - gauge_t total; - if (kvm_obj == NULL) return -1; @@ -711,20 +759,22 @@ static int swap_read(void) /* {{{ */ if (status == -1) return -1; - total = (gauge_t)data_s.ksw_total; - used = (gauge_t)data_s.ksw_used; + gauge_t total = (gauge_t)data_s.ksw_total; + gauge_t used = (gauge_t)data_s.ksw_used; total *= (gauge_t)kvm_pagesize; used *= (gauge_t)kvm_pagesize; - swap_submit_usage(NULL, used, total - used, NULL, NAN); + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], used, + fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], total - used, + NULL, NULL, NAN); return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { sg_swap_stats *swap; @@ -732,44 +782,117 @@ static int swap_read(void) /* {{{ */ if (swap == NULL) return -1; - swap_submit_usage(NULL, (gauge_t)swap->used, (gauge_t)swap->free, NULL, NAN); + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + (gauge_t)swap->used, fams[FAM_SWAP_FREE], + fams[FAM_SWAP_FREE_PCT], (gauge_t)swap->free, NULL, NULL, + NAN); return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ /* #endif HAVE_LIBSTATGRAB */ #elif HAVE_PERFSTAT -static int swap_read(void) /* {{{ */ +static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ { perfstat_memory_total_t pmemory = {0}; - int status; - - gauge_t total; - gauge_t free; - gauge_t reserved; - status = + int status = perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1); if (status < 0) { WARNING("swap plugin: perfstat_memory_total failed: %s", STRERRNO); return -1; } - total = (gauge_t)(pmemory.pgsp_total * pagesize); - free = (gauge_t)(pmemory.pgsp_free * pagesize); - reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); + gauge_t total = (gauge_t)(pmemory.pgsp_total * pagesize); + gauge_t free = (gauge_t)(pmemory.pgsp_free * pagesize); + gauge_t reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); - swap_submit_usage(NULL, total - free, free, "reserved", reserved); + swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], + total - free, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + free, fams[FAM_SWAP_RESERVED], fams[FAM_SWAP_RESERVED_PCT], + reserved); if (report_io) { - swap_submit_derive("in", (derive_t)pmemory.pgspins * pagesize); - swap_submit_derive("out", (derive_t)pmemory.pgspouts * pagesize); + swap_submit_io(fams[FAM_SWAP_IN], (counter_t)(pmemory.pgspins * pagesize), + fams[FAM_SWAP_OUT], + (counter_t)(pmemory.pgspouts * pagesize)); } return 0; -} /* }}} int swap_read */ +} /* }}} int swap_read_fam */ #endif /* HAVE_PERFSTAT */ +static int swap_read(void) { + metric_family_t fam_swap_used = { + .name = "swap_used_bytes", + .type = METRIC_TYPE_COUNTER, + }; + metric_family_t fam_swap_free = { + .name = "swap_free_bytes", + .type = METRIC_TYPE_COUNTER, + }; + metric_family_t fam_swap_cached = { + .name = "swap_cached_bytes", + .type = METRIC_TYPE_COUNTER, + }; + metric_family_t fam_swap_reserved = { + .name = "swap_reserved_bytes", + .type = METRIC_TYPE_COUNTER, + }; + metric_family_t fam_swap_used_pct = { + .name = "swap_used_percent", + .type = METRIC_TYPE_GAUGE, + }; + metric_family_t fam_swap_free_pct = { + .name = "swap_free_percent", + .type = METRIC_TYPE_GAUGE, + }; + metric_family_t fam_swap_cached_pct = { + .name = "swap_cached_percent", + .type = METRIC_TYPE_GAUGE, + }; + metric_family_t fam_swap_reserved_pct = { + .name = "swap_reserved_percent", + .type = METRIC_TYPE_GAUGE, + }; + metric_family_t fam_swap_in = { + .name = "swap_in", + .type = METRIC_TYPE_COUNTER, + }; + metric_family_t fam_swap_out = { + .name = "swap_out", + .type = METRIC_TYPE_COUNTER, + }; + + metric_family_t *fams_swap[FAM_SWAP_MAX]; + + fams_swap[FAM_SWAP_USED] = &fam_swap_used; + fams_swap[FAM_SWAP_FREE] = &fam_swap_free; + fams_swap[FAM_SWAP_CACHED] = &fam_swap_cached; + fams_swap[FAM_SWAP_RESERVED] = &fam_swap_reserved; + fams_swap[FAM_SWAP_USED_PCT] = &fam_swap_used_pct; + fams_swap[FAM_SWAP_FREE_PCT] = &fam_swap_free_pct; + fams_swap[FAM_SWAP_CACHED_PCT] = &fam_swap_cached_pct; + fams_swap[FAM_SWAP_RESERVED_PCT] = &fam_swap_reserved_pct; + fams_swap[FAM_SWAP_IN] = &fam_swap_in; + fams_swap[FAM_SWAP_OUT] = &fam_swap_out; + + swap_read_fam(fams_swap); + + for (size_t i = 0; i < FAM_SWAP_MAX; i++) { + if (fams_swap[i]->metric.num > 0) { + int status = plugin_dispatch_metric_family(fams_swap[i]); + if (status != 0) { + ERROR("serial plugin: plugin_dispatch_metric_family failed: %s", + STRERROR(status)); + } + metric_family_metric_reset(fams_swap[i]); + } + } + + return 0; +} + void module_register(void) { plugin_register_complex_config("swap", swap_config); plugin_register_init("swap", swap_init); From 5029473af6069764de27e48ea22e4419ac3af3cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Luis=20Sanmart=C3=ADn=20Rozada?= Date: Sun, 24 Jan 2021 00:12:27 +0100 Subject: [PATCH 02/15] Fix error message --- src/swap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/swap.c b/src/swap.c index 2213da2b7a..53055aa299 100644 --- a/src/swap.c +++ b/src/swap.c @@ -883,7 +883,7 @@ static int swap_read(void) { if (fams_swap[i]->metric.num > 0) { int status = plugin_dispatch_metric_family(fams_swap[i]); if (status != 0) { - ERROR("serial plugin: plugin_dispatch_metric_family failed: %s", + ERROR("swap plugin: plugin_dispatch_metric_family failed: %s", STRERROR(status)); } metric_family_metric_reset(fams_swap[i]); From f6a90783ea8a3445a5fbe02ba8d5a6886b6701d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20Luis=20Sanmart=C3=ADn=20Rozada?= Date: Sun, 24 Jan 2021 00:41:12 +0100 Subject: [PATCH 03/15] Declare metric families as an array --- src/swap.c | 213 ++++++++++++++++++++++++++--------------------------- 1 file changed, 106 insertions(+), 107 deletions(-) diff --git a/src/swap.c b/src/swap.c index 53055aa299..58b2e633b4 100644 --- a/src/swap.c +++ b/src/swap.c @@ -267,7 +267,7 @@ static void swap_submit_io(metric_family_t *fam_in, counter_t in, /* {{{ */ #endif #if KERNEL_LINUX -static int swap_read_separate(metric_family_t *fams[]) /* {{{ */ +static int swap_read_separate(metric_family_t *fams) /* {{{ */ { FILE *fh; char buffer[1024]; @@ -308,9 +308,9 @@ static int swap_read_separate(metric_family_t *fams[]) /* {{{ */ if (total < used) continue; - swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - used * 1024.0, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], (total - used) * 1024.0, NULL, + swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + used * 1024.0, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], (total - used) * 1024.0, NULL, NULL, NAN); } @@ -319,7 +319,7 @@ static int swap_read_separate(metric_family_t *fams[]) /* {{{ */ return 0; } /* }}} int swap_read_separate */ -static int swap_read_combined(metric_family_t *fams[]) /* {{{ */ +static int swap_read_combined(metric_family_t *fams) /* {{{ */ { FILE *fh; char buffer[1024]; @@ -366,16 +366,16 @@ static int swap_read_combined(metric_family_t *fams[]) /* {{{ */ if (swap_used < 0.0) return EINVAL; - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - swap_used * 1024.0, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], swap_free * 1024.0, - isnan(swap_cached) ? NULL : fams[FAM_SWAP_CACHED], - isnan(swap_cached) ? NULL : fams[FAM_SWAP_CACHED_PCT], + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + swap_used * 1024.0, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], swap_free * 1024.0, + isnan(swap_cached) ? NULL : &fams[FAM_SWAP_CACHED], + isnan(swap_cached) ? NULL : &fams[FAM_SWAP_CACHED_PCT], isnan(swap_cached) ? NAN : swap_cached * 1024.0); return 0; } /* }}} int swap_read_combined */ -static int swap_read_io(metric_family_t *fams[]) /* {{{ */ +static int swap_read_io(metric_family_t *fams) /* {{{ */ { char buffer[1024]; @@ -415,12 +415,12 @@ static int swap_read_io(metric_family_t *fams[]) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_io(fams[FAM_SWAP_IN], swap_in, fams[FAM_SWAP_OUT], swap_out); + swap_submit_io(&fams[FAM_SWAP_IN], swap_in, &fams[FAM_SWAP_OUT], swap_out); return 0; } /* }}} int swap_read_io */ -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { if (report_by_device) swap_read_separate(fams); @@ -445,7 +445,7 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ */ #elif 0 && HAVE_LIBKSTAT /* kstat-based read function */ -static int swap_read_kstat(metric_family_t *fams[]) /* {{{ */ +static int swap_read_kstat(metric_family_t *fams) /* {{{ */ { gauge_t swap_alloc; gauge_t swap_resv; @@ -483,17 +483,17 @@ static int swap_read_kstat(metric_family_t *fams[]) /* {{{ */ swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - swap_alloc, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], - swap_avail, fams[FAM_SWAP_RESERVED], - fams[FAM_SWAP_RESERVED_PCT], swap_resv); + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + swap_alloc, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], + swap_avail, &fams[FAM_SWAP_RESERVED], + &fams[FAM_SWAP_RESERVED_PCT], swap_resv); return 0; } /* }}} int swap_read_kstat */ /* #endif 0 && HAVE_LIBKSTAT */ #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS /* swapctl-based read function */ -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { swaptbl_t *s; char *s_paths; @@ -569,9 +569,9 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path)); - swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - this_total - this_avail, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], this_avail, NULL, NULL, NAN); + swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + this_total - this_avail, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], this_avail, NULL, NULL, NAN); } /* for (swap_num) */ if (total < avail) { @@ -586,9 +586,9 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - total - avail, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], avail, NULL, NULL, NAN); + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + total - avail, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], avail, NULL, NULL, NAN); } sfree(s_paths); @@ -601,7 +601,7 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ #if KERNEL_NETBSD #include -static int swap_read_io(metric_family_t *fams[]) /* {{{ */ +static int swap_read_io(metric_family_t *fams) /* {{{ */ { static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2}; struct uvmexp_sysctl uvmexp; @@ -626,13 +626,13 @@ static int swap_read_io(metric_family_t *fams[]) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_io(fams[FAM_SWAP_IN], swap_in, fams[FAM_SWAP_OUT], swap_out); + swap_submit_io(&fams[FAM_SWAP_IN], swap_in, &fams[FAM_SWAP_OUT], swap_out); return (0); } /* }}} */ #endif -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { struct swapent *swap_entries; int swap_num; @@ -689,8 +689,8 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ sstrncpy(path, swap_entries[i].se_path, sizeof(path)); - swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - this_used, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + this_used, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], this_total - this_used, NULL, NULL, NAN); } /* for (swap_num) */ @@ -705,8 +705,8 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ /* If the "separate" option was specified (report_by_device == 1), all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(path, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], used, - fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], + swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + used, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], total - used, NULL, NULL, NAN); } @@ -719,7 +719,7 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */ #elif defined(VM_SWAPUSAGE) -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { int mib[3]; size_t mib_len; @@ -736,9 +736,9 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ return -1; /* The returned values are bytes. */ - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - (gauge_t)sw_usage.xsu_used, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], (gauge_t)sw_usage.xsu_avail, NULL, + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + (gauge_t)sw_usage.xsu_used, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], (gauge_t)sw_usage.xsu_avail, NULL, NULL, NAN); return 0; @@ -746,7 +746,7 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ /* #endif VM_SWAPUSAGE */ #elif HAVE_LIBKVM_GETSWAPINFO -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { struct kvm_swap data_s; int status; @@ -765,16 +765,16 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ total *= (gauge_t)kvm_pagesize; used *= (gauge_t)kvm_pagesize; - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], used, - fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], total - used, - NULL, NULL, NAN); + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], used, + &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], + total - used, NULL, NULL, NAN); return 0; } /* }}} int swap_read_fam */ /* #endif HAVE_LIBKVM_GETSWAPINFO */ #elif HAVE_LIBSTATGRAB -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { sg_swap_stats *swap; @@ -782,9 +782,9 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ if (swap == NULL) return -1; - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - (gauge_t)swap->used, fams[FAM_SWAP_FREE], - fams[FAM_SWAP_FREE_PCT], (gauge_t)swap->free, NULL, NULL, + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + (gauge_t)swap->used, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], (gauge_t)swap->free, NULL, NULL, NAN); return 0; @@ -792,7 +792,7 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ /* #endif HAVE_LIBSTATGRAB */ #elif HAVE_PERFSTAT -static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ +static int swap_read_fam(metric_family_t *fams) /* {{{ */ { perfstat_memory_total_t pmemory = {0}; @@ -807,14 +807,14 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ gauge_t free = (gauge_t)(pmemory.pgsp_free * pagesize); gauge_t reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); - swap_submit_usage(NULL, fams[FAM_SWAP_USED], fams[FAM_SWAP_USED_PCT], - total - free, fams[FAM_SWAP_FREE], fams[FAM_SWAP_FREE_PCT], - free, fams[FAM_SWAP_RESERVED], fams[FAM_SWAP_RESERVED_PCT], - reserved); + swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], + total - free, &fams[FAM_SWAP_FREE], + &fams[FAM_SWAP_FREE_PCT], free, &fams[FAM_SWAP_RESERVED], + &fams[FAM_SWAP_RESERVED_PCT], reserved); if (report_io) { - swap_submit_io(fams[FAM_SWAP_IN], (counter_t)(pmemory.pgspins * pagesize), - fams[FAM_SWAP_OUT], + swap_submit_io(&fams[FAM_SWAP_IN], (counter_t)(pmemory.pgspins * pagesize), + &fams[FAM_SWAP_OUT], (counter_t)(pmemory.pgspouts * pagesize)); } @@ -823,70 +823,69 @@ static int swap_read_fam(metric_family_t *fams[]) /* {{{ */ #endif /* HAVE_PERFSTAT */ static int swap_read(void) { - metric_family_t fam_swap_used = { - .name = "swap_used_bytes", - .type = METRIC_TYPE_COUNTER, + metric_family_t fams[FAM_SWAP_MAX] = { + [FAM_SWAP_USED] = + { + .name = "swap_used_bytes", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_FREE] = + { + .name = "swap_free_bytes", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_CACHED] = + { + .name = "swap_cached_bytes", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_RESERVED] = + { + .name = "swap_reserved_bytes", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_USED_PCT] = + { + .name = "swap_used_percent", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_FREE_PCT] = + { + .name = "swap_free_percent", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_CACHED_PCT] = + { + .name = "swap_cached_percent", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_RESERVED_PCT] = + { + .name = "swap_reserved_percent", + .type = METRIC_TYPE_GAUGE, + }, + [FAM_SWAP_IN] = + { + .name = "swap_in", + .type = METRIC_TYPE_COUNTER, + }, + [FAM_SWAP_OUT] = + { + .name = "swap_out", + .type = METRIC_TYPE_COUNTER, + }, }; - metric_family_t fam_swap_free = { - .name = "swap_free_bytes", - .type = METRIC_TYPE_COUNTER, - }; - metric_family_t fam_swap_cached = { - .name = "swap_cached_bytes", - .type = METRIC_TYPE_COUNTER, - }; - metric_family_t fam_swap_reserved = { - .name = "swap_reserved_bytes", - .type = METRIC_TYPE_COUNTER, - }; - metric_family_t fam_swap_used_pct = { - .name = "swap_used_percent", - .type = METRIC_TYPE_GAUGE, - }; - metric_family_t fam_swap_free_pct = { - .name = "swap_free_percent", - .type = METRIC_TYPE_GAUGE, - }; - metric_family_t fam_swap_cached_pct = { - .name = "swap_cached_percent", - .type = METRIC_TYPE_GAUGE, - }; - metric_family_t fam_swap_reserved_pct = { - .name = "swap_reserved_percent", - .type = METRIC_TYPE_GAUGE, - }; - metric_family_t fam_swap_in = { - .name = "swap_in", - .type = METRIC_TYPE_COUNTER, - }; - metric_family_t fam_swap_out = { - .name = "swap_out", - .type = METRIC_TYPE_COUNTER, - }; - - metric_family_t *fams_swap[FAM_SWAP_MAX]; - - fams_swap[FAM_SWAP_USED] = &fam_swap_used; - fams_swap[FAM_SWAP_FREE] = &fam_swap_free; - fams_swap[FAM_SWAP_CACHED] = &fam_swap_cached; - fams_swap[FAM_SWAP_RESERVED] = &fam_swap_reserved; - fams_swap[FAM_SWAP_USED_PCT] = &fam_swap_used_pct; - fams_swap[FAM_SWAP_FREE_PCT] = &fam_swap_free_pct; - fams_swap[FAM_SWAP_CACHED_PCT] = &fam_swap_cached_pct; - fams_swap[FAM_SWAP_RESERVED_PCT] = &fam_swap_reserved_pct; - fams_swap[FAM_SWAP_IN] = &fam_swap_in; - fams_swap[FAM_SWAP_OUT] = &fam_swap_out; - swap_read_fam(fams_swap); + swap_read_fam(fams); for (size_t i = 0; i < FAM_SWAP_MAX; i++) { - if (fams_swap[i]->metric.num > 0) { - int status = plugin_dispatch_metric_family(fams_swap[i]); + if (fams[i].metric.num > 0) { + int status = plugin_dispatch_metric_family(&fams[i]); if (status != 0) { ERROR("swap plugin: plugin_dispatch_metric_family failed: %s", STRERROR(status)); } - metric_family_metric_reset(fams_swap[i]); + metric_family_metric_reset(&fams[i]); } } From 9f90a3412379950f0d24640de6a9acb16cc59923 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Thu, 14 Dec 2023 17:54:09 +0100 Subject: [PATCH 04/15] swap plugin: Group metrics into fewer metric families. This should align this plugin with the OpenTelemetry semantic conventions: https://opentelemetry.io/docs/specs/semconv/system/system-metrics/#pagingswap-metrics --- src/swap.c | 212 +++++++++++++++++++---------------------------------- 1 file changed, 75 insertions(+), 137 deletions(-) diff --git a/src/swap.c b/src/swap.c index 58b2e633b4..3631c152a6 100644 --- a/src/swap.c +++ b/src/swap.c @@ -119,16 +119,9 @@ static bool values_percentage; static bool report_io = true; enum { - FAM_SWAP_USED = 0, - FAM_SWAP_FREE, - FAM_SWAP_CACHED, - FAM_SWAP_RESERVED, - FAM_SWAP_USED_PCT, - FAM_SWAP_FREE_PCT, - FAM_SWAP_CACHED_PCT, - FAM_SWAP_RESERVED_PCT, - FAM_SWAP_IN, - FAM_SWAP_OUT, + FAM_SWAP_USAGE = 0, + FAM_SWAP_UTILIZATION, + FAM_SWAP_IO, FAM_SWAP_MAX, }; @@ -141,7 +134,7 @@ static int swap_config(oconfig_item_t *ci) /* {{{ */ cf_util_get_boolean(child, &report_bytes); #else WARNING("swap plugin: The \"ReportBytes\" option " - "is only valid under Linux. " + "is only valid under Linux and NetBSD. " "The option is going to be ignored."); #endif else if (strcasecmp("ReportByDevice", child->key) == 0) @@ -209,61 +202,64 @@ static int swap_init(void) /* {{{ */ return 0; } /* }}} int swap_init */ -static void swap_submit_usage(char *device, /* {{{ */ - metric_family_t *fam_used, - metric_family_t *fam_used_pct, gauge_t used, - metric_family_t *fam_free, - metric_family_t *fam_free_pct, gauge_t free, - metric_family_t *fam_other, - metric_family_t *fam_other_pct, gauge_t other) { - metric_t m = {0}; +static void swap_submit_usage3(metric_family_t *fams, char const *device, + gauge_t used, gauge_t free, + char const *other_name, gauge_t other) { + metric_family_t *fam_usage = &fams[FAM_SWAP_USAGE]; + metric_family_t *fam_utilization = &fams[FAM_SWAP_UTILIZATION]; + metric_t m = {0}; if (device != NULL) { metric_label_set(&m, "device", device); } + bool have_other = (other_name != NULL) && !isnan(other); + if (values_absolute) { - if (fam_other != NULL) { - m.value.gauge = other; - metric_family_metric_append(fam_other, m); + if (have_other) { + metric_family_append(fam_usage, "system.paging.state", other_name, + (value_t){.gauge = other}, &m); } - m.value.gauge = used; - metric_family_metric_append(fam_used, m); - - m.value.gauge = free; - metric_family_metric_append(fam_free, m); + metric_family_append(fam_usage, "system.paging.state", "used", + (value_t){.gauge = used}, &m); + metric_family_append(fam_usage, "system.paging.state", "free", + (value_t){.gauge = free}, &m); } if (values_percentage) { gauge_t total = used + free; - if (fam_other_pct != NULL) { + if (have_other) { total += other; - m.value.gauge = 100.0 * other / total; - metric_family_metric_append(fam_other_pct, m); - } - m.value.gauge = 100.0 * used / total; - metric_family_metric_append(fam_used_pct, m); + metric_family_append(fam_utilization, "system.paging.state", other_name, + (value_t){.gauge = 100.0 * other / total}, &m); + } - m.value.gauge = 100.0 * free / total; - metric_family_metric_append(fam_free_pct, m); + metric_family_append(fam_utilization, "system.paging.state", "used", + (value_t){.gauge = 100.0 * used / total}, &m); + metric_family_append(fam_utilization, "system.paging.state", "free", + (value_t){.gauge = 100.0 * free / total}, &m); } metric_reset(&m); -} /* }}} void swap_submit_usage */ +} /* void swap_submit_usage3 */ + +static void swap_submit_usage(metric_family_t *fams, char *device, gauge_t used, + gauge_t free) { + swap_submit_usage3(fams, device, used, free, NULL, NAN); +} #if KERNEL_LINUX || HAVE_PERFSTAT || KERNEL_NETBSD -static void swap_submit_io(metric_family_t *fam_in, counter_t in, /* {{{ */ - metric_family_t *fam_out, counter_t out) { - - metric_family_metric_append(fam_in, (metric_t){ - .value.counter = in, - }); - metric_family_metric_append(fam_out, (metric_t){ - .value.counter = out, - }); -} /* }}} void swap_submit_io */ +static void swap_submit_io(metric_family_t *fams, counter_t in, counter_t out) { + metric_family_t *fam_io = &fams[FAM_SWAP_IO]; + metric_t m = {0}; + + metric_family_append(fam_io, "system.paging.direction", "in", + (value_t){.counter = in}, &m); + metric_family_append(fam_io, "system.paging.direction", "out", + (value_t){.counter = out}, &m); +} /* void swap_submit_io */ #endif #if KERNEL_LINUX @@ -308,10 +304,7 @@ static int swap_read_separate(metric_family_t *fams) /* {{{ */ if (total < used) continue; - swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - used * 1024.0, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], (total - used) * 1024.0, NULL, - NULL, NAN); + swap_submit_usage(fams, path, used * 1024.0, (total - used) * 1024.0); } fclose(fh); @@ -366,12 +359,9 @@ static int swap_read_combined(metric_family_t *fams) /* {{{ */ if (swap_used < 0.0) return EINVAL; - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - swap_used * 1024.0, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], swap_free * 1024.0, - isnan(swap_cached) ? NULL : &fams[FAM_SWAP_CACHED], - isnan(swap_cached) ? NULL : &fams[FAM_SWAP_CACHED_PCT], - isnan(swap_cached) ? NAN : swap_cached * 1024.0); + swap_submit_usage3(fams, NULL, swap_used * 1024.0, swap_free * 1024.0, + "cached", swap_cached * 1024.0); + return 0; } /* }}} int swap_read_combined */ @@ -415,7 +405,7 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_io(&fams[FAM_SWAP_IN], swap_in, &fams[FAM_SWAP_OUT], swap_out); + swap_submit_io(fams, swap_in, swap_out); return 0; } /* }}} int swap_read_io */ @@ -483,10 +473,8 @@ static int swap_read_kstat(metric_family_t *fams) /* {{{ */ swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - swap_alloc, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], - swap_avail, &fams[FAM_SWAP_RESERVED], - &fams[FAM_SWAP_RESERVED_PCT], swap_resv); + swap_submit_usage3(fams, NULL, swap_alloc, swap_avail, "reserved", swap_resv); + return 0; } /* }}} int swap_read_kstat */ /* #endif 0 && HAVE_LIBKSTAT */ @@ -569,9 +557,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path)); - swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - this_total - this_avail, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], this_avail, NULL, NULL, NAN); + swap_submit_usage(fams, path, this_total - this_avail, this_avail); } /* for (swap_num) */ if (total < avail) { @@ -586,9 +572,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - total - avail, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], avail, NULL, NULL, NAN); + swap_submit_usage(fams, NULL, total - avail, avail); } sfree(s_paths); @@ -626,7 +610,7 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ swap_out = swap_out * pagesize; } - swap_submit_io(&fams[FAM_SWAP_IN], swap_in, &fams[FAM_SWAP_OUT], swap_out); + swap_submit_io(fams, swap_in, swap_out); return (0); } /* }}} */ @@ -689,9 +673,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, swap_entries[i].se_path, sizeof(path)); - swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - this_used, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], - this_total - this_used, NULL, NULL, NAN); + swap_submit_usage(fams, path, this_used, this_total - this_used); } /* for (swap_num) */ if (total < used) { @@ -705,9 +687,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == 1), all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(path, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - used, &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], - total - used, NULL, NULL, NAN); + swap_submit_usage(fams, NULL, used, total - used); } sfree(swap_entries); @@ -736,10 +716,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ return -1; /* The returned values are bytes. */ - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - (gauge_t)sw_usage.xsu_used, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], (gauge_t)sw_usage.xsu_avail, NULL, - NULL, NAN); + swap_submit_usage(fams, NULL, (gauge_t)sw_usage.xsu_used, + (gauge_t)sw_usage.xsu_avail); return 0; } /* }}} int swap_read_fam */ @@ -765,9 +743,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ total *= (gauge_t)kvm_pagesize; used *= (gauge_t)kvm_pagesize; - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], used, - &fams[FAM_SWAP_FREE], &fams[FAM_SWAP_FREE_PCT], - total - used, NULL, NULL, NAN); + swap_submit_usage(fams, NULL, used, total - used); return 0; } /* }}} int swap_read_fam */ @@ -782,10 +758,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ if (swap == NULL) return -1; - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - (gauge_t)swap->used, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], (gauge_t)swap->free, NULL, NULL, - NAN); + swap_submit_usage(fams, NULL, (gauge_t)swap->used, (gauge_t)swap->free); return 0; } /* }}} int swap_read_fam */ @@ -807,14 +780,10 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ gauge_t free = (gauge_t)(pmemory.pgsp_free * pagesize); gauge_t reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); - swap_submit_usage(NULL, &fams[FAM_SWAP_USED], &fams[FAM_SWAP_USED_PCT], - total - free, &fams[FAM_SWAP_FREE], - &fams[FAM_SWAP_FREE_PCT], free, &fams[FAM_SWAP_RESERVED], - &fams[FAM_SWAP_RESERVED_PCT], reserved); + swap_submit_usage3(fams, NULL, total - free, free, "reserved", reserved); if (report_io) { - swap_submit_io(&fams[FAM_SWAP_IN], (counter_t)(pmemory.pgspins * pagesize), - &fams[FAM_SWAP_OUT], + swap_submit_io(fams, (counter_t)(pmemory.pgspins * pagesize), (counter_t)(pmemory.pgspouts * pagesize)); } @@ -823,69 +792,38 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ #endif /* HAVE_PERFSTAT */ static int swap_read(void) { - metric_family_t fams[FAM_SWAP_MAX] = { - [FAM_SWAP_USED] = - { - .name = "swap_used_bytes", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_FREE] = - { - .name = "swap_free_bytes", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_CACHED] = + metric_family_t fams[] = { + [FAM_SWAP_USAGE] = { - .name = "swap_cached_bytes", + .name = "system.paging.usage", .type = METRIC_TYPE_GAUGE, }, - [FAM_SWAP_RESERVED] = + [FAM_SWAP_UTILIZATION] = { - .name = "swap_reserved_bytes", + .name = "system.paging.utilization", .type = METRIC_TYPE_GAUGE, }, - [FAM_SWAP_USED_PCT] = + [FAM_SWAP_IO] = { - .name = "swap_used_percent", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_FREE_PCT] = - { - .name = "swap_free_percent", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_CACHED_PCT] = - { - .name = "swap_cached_percent", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_RESERVED_PCT] = - { - .name = "swap_reserved_percent", - .type = METRIC_TYPE_GAUGE, - }, - [FAM_SWAP_IN] = - { - .name = "swap_in", - .type = METRIC_TYPE_COUNTER, - }, - [FAM_SWAP_OUT] = - { - .name = "swap_out", + .name = "system.paging.io", .type = METRIC_TYPE_COUNTER, }, }; - swap_read_fam(fams); + int status = swap_read_fam(fams); + if (status != 0) { + return status; + } for (size_t i = 0; i < FAM_SWAP_MAX; i++) { - if (fams[i].metric.num > 0) { - int status = plugin_dispatch_metric_family(&fams[i]); + metric_family_t *fam = &fams[i]; + if (fam->metric.num > 0) { + int status = plugin_dispatch_metric_family(fam); if (status != 0) { ERROR("swap plugin: plugin_dispatch_metric_family failed: %s", STRERROR(status)); } - metric_family_metric_reset(&fams[i]); + metric_family_metric_reset(fam); } } From 555ef570f35deb72e2898eb274c4557ec2e438f6 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 27 Dec 2023 10:16:26 +0100 Subject: [PATCH 05/15] swap plugin: Use different metric families for I/O reporting in bytes. Depending on the "ReportIO" setting, the plugin may emit paging operations or bytes. Use a different metric family for these two metrics. This also moves the decision whether or not to report I/O to the `swap_submit_io`. This fixes a bug on NetBSD where I/O was reported even if "ReportIO" was set to false. --- src/swap.c | 67 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/src/swap.c b/src/swap.c index 3631c152a6..1d0935eaaf 100644 --- a/src/swap.c +++ b/src/swap.c @@ -121,6 +121,7 @@ static bool report_io = true; enum { FAM_SWAP_USAGE = 0, FAM_SWAP_UTILIZATION, + FAM_SWAP_OPS, FAM_SWAP_IO, FAM_SWAP_MAX, }; @@ -251,14 +252,28 @@ static void swap_submit_usage(metric_family_t *fams, char *device, gauge_t used, } #if KERNEL_LINUX || HAVE_PERFSTAT || KERNEL_NETBSD -static void swap_submit_io(metric_family_t *fams, counter_t in, counter_t out) { - metric_family_t *fam_io = &fams[FAM_SWAP_IO]; - metric_t m = {0}; +static char const *const label_direction = "system.paging.direction"; + +static char const *const direction_in = "in"; +static char const *const direction_out = "out"; - metric_family_append(fam_io, "system.paging.direction", "in", - (value_t){.counter = in}, &m); - metric_family_append(fam_io, "system.paging.direction", "out", - (value_t){.counter = out}, &m); +static void swap_submit_io(metric_family_t *fams, counter_t in, counter_t out, + derive_t pagesize) { + if (!report_io) { + return; + } + + metric_family_t *fam = &fams[FAM_SWAP_OPS]; + if (report_bytes) { + fam = &fams[FAM_SWAP_IO]; + in = in * (counter_t)pagesize; + out = out * (counter_t)pagesize; + } + + metric_family_append(fam, label_direction, direction_in, + (value_t){.counter = in}, NULL); + metric_family_append(fam, label_direction, direction_out, + (value_t){.counter = out}, NULL); } /* void swap_submit_io */ #endif @@ -397,15 +412,11 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ fclose(fh); - if (have_data != 0x03) + if (have_data != 0x03) { return ENOENT; - - if (report_bytes) { - swap_in = swap_in * pagesize; - swap_out = swap_out * pagesize; } - swap_submit_io(fams, swap_in, swap_out); + swap_submit_io(fams, swap_in, swap_out, pagesize); return 0; } /* }}} int swap_read_io */ @@ -589,10 +600,8 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ { static int uvmexp_mib[] = {CTL_VM, VM_UVMEXP2}; struct uvmexp_sysctl uvmexp; - size_t ssize; - counter_t swap_in, swap_out; - ssize = sizeof(uvmexp); + size_t ssize = sizeof(uvmexp); memset(&uvmexp, 0, ssize); if (sysctl(uvmexp_mib, __arraycount(uvmexp_mib), &uvmexp, &ssize, NULL, 0) == -1) { @@ -602,15 +611,10 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ return (-1); } - swap_in = uvmexp.pgswapin; - swap_out = uvmexp.pgswapout; + counter_t swap_in = uvmexp.pgswapin; + counter_t swap_out = uvmexp.pgswapout; - if (report_bytes) { - swap_in = swap_in * pagesize; - swap_out = swap_out * pagesize; - } - - swap_submit_io(fams, swap_in, swap_out); + swap_submit_io(fams, swap_in, swap_out, pagesize); return (0); } /* }}} */ @@ -782,10 +786,10 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ swap_submit_usage3(fams, NULL, total - free, free, "reserved", reserved); - if (report_io) { - swap_submit_io(fams, (counter_t)(pmemory.pgspins * pagesize), - (counter_t)(pmemory.pgspouts * pagesize)); - } + counter_t swap_in = pmemory.pgspins; + counter_t swap_out = pmemory.pgspouts; + + swap_submit_io(fams, swap_in, swap_out, pagesize); return 0; } /* }}} int swap_read_fam */ @@ -803,8 +807,15 @@ static int swap_read(void) { .name = "system.paging.utilization", .type = METRIC_TYPE_GAUGE, }, + [FAM_SWAP_OPS] = + { + /* used when report_io && !report_bytes */ + .name = "system.paging.operations", + .type = METRIC_TYPE_COUNTER, + }, [FAM_SWAP_IO] = { + /* used when report_io && report_bytes */ .name = "system.paging.io", .type = METRIC_TYPE_COUNTER, }, From c4454f3e5efa91436c90b82f072394160368436c Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 27 Dec 2023 10:20:10 +0100 Subject: [PATCH 06/15] swap plugin: Use constants for label names and well-known label values. --- src/swap.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/swap.c b/src/swap.c index 1d0935eaaf..c621c23dbe 100644 --- a/src/swap.c +++ b/src/swap.c @@ -118,6 +118,12 @@ static bool values_absolute = true; static bool values_percentage; static bool report_io = true; +static char const *const label_device = "system.device"; +static char const *const label_state = "system.paging.state"; + +static char const *const state_free = "free"; +static char const *const state_used = "used"; + enum { FAM_SWAP_USAGE = 0, FAM_SWAP_UTILIZATION, @@ -211,20 +217,20 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, metric_t m = {0}; if (device != NULL) { - metric_label_set(&m, "device", device); + metric_label_set(&m, label_device, device); } bool have_other = (other_name != NULL) && !isnan(other); if (values_absolute) { if (have_other) { - metric_family_append(fam_usage, "system.paging.state", other_name, + metric_family_append(fam_usage, label_state, other_name, (value_t){.gauge = other}, &m); } - metric_family_append(fam_usage, "system.paging.state", "used", + metric_family_append(fam_usage, label_state, state_used, (value_t){.gauge = used}, &m); - metric_family_append(fam_usage, "system.paging.state", "free", + metric_family_append(fam_usage, label_state, state_free, (value_t){.gauge = free}, &m); } @@ -233,13 +239,13 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, if (have_other) { total += other; - metric_family_append(fam_utilization, "system.paging.state", other_name, + metric_family_append(fam_utilization, label_state, other_name, (value_t){.gauge = 100.0 * other / total}, &m); } - metric_family_append(fam_utilization, "system.paging.state", "used", + metric_family_append(fam_utilization, label_state, state_used, (value_t){.gauge = 100.0 * used / total}, &m); - metric_family_append(fam_utilization, "system.paging.state", "free", + metric_family_append(fam_utilization, label_state, state_free, (value_t){.gauge = 100.0 * free / total}, &m); } From 886ea28f7d2d553162d1c2598d7d5d5643dbba30 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 27 Dec 2023 10:20:31 +0100 Subject: [PATCH 07/15] swap plugin: Add descriptions to metric families. --- src/swap.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/swap.c b/src/swap.c index c621c23dbe..1ac0692044 100644 --- a/src/swap.c +++ b/src/swap.c @@ -806,11 +806,13 @@ static int swap_read(void) { [FAM_SWAP_USAGE] = { .name = "system.paging.usage", + .help = "Unix swap usage", .type = METRIC_TYPE_GAUGE, }, [FAM_SWAP_UTILIZATION] = { .name = "system.paging.utilization", + .help = "Unix swap utilization", .type = METRIC_TYPE_GAUGE, }, [FAM_SWAP_OPS] = From 1c1449992bb7b3b79120dc2a5714b0557c3666da Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 10 Jan 2024 22:00:03 +0100 Subject: [PATCH 08/15] swap plugin: Add the `ReportUsage` and `ReportUtilization` config options. --- src/collectd.conf.in | 6 +++--- src/collectd.conf.pod | 11 ++++++----- src/swap.c | 19 +++++++++++-------- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index 18dd82ef61..a269306ab9 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1748,10 +1748,10 @@ # # ReportByDevice false -# ReportBytes true -# ValuesAbsolute true -# ValuesPercentage false +# ReportUsage true +# ReportUtilization false # ReportIO true +# ReportBytes true # # diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index b8b9457a99..d378928506 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -9334,17 +9334,18 @@ This option is only available if the I can read C =item B B|B When enabled, the I is reported in bytes. When disabled, the default, -I is reported in pages. This option is available under Linux only. +I is reported in pages. This option is available under Linux and +NetBSD only. -=item B B|B +=item B B|B Enables or disables reporting of absolute swap metrics, i.e. number of I available and used. Defaults to B. -=item B B|B +=item B B|B -Enables or disables reporting of relative swap metrics, i.e. I -available and free. Defaults to B. +Enables or disables reporting of relative swap metrics, i.e. the ratio of used +and free swap space. Defaults to B. This is useful for deploying I in a heterogeneous environment, where swap sizes differ and you want to specify generic thresholds or similar. diff --git a/src/swap.c b/src/swap.c index 1ac0692044..e79f4fbc09 100644 --- a/src/swap.c +++ b/src/swap.c @@ -114,8 +114,8 @@ static int pagesize; #error "No applicable input method." #endif /* HAVE_LIBSTATGRAB */ -static bool values_absolute = true; -static bool values_percentage; +static bool report_usage = true; +static bool report_utilization; static bool report_io = true; static char const *const label_device = "system.device"; @@ -152,10 +152,13 @@ static int swap_config(oconfig_item_t *ci) /* {{{ */ "is not supported on this platform. " "The option is going to be ignored."); #endif /* SWAP_HAVE_REPORT_BY_DEVICE */ - else if (strcasecmp("ValuesAbsolute", child->key) == 0) - cf_util_get_boolean(child, &values_absolute); - else if (strcasecmp("ValuesPercentage", child->key) == 0) - cf_util_get_boolean(child, &values_percentage); + /* ValuesAbsolute and ValuesPercentage are for collectd 5 compatibility. */ + else if (strcasecmp("ReportUsage", child->key) == 0 || + strcasecmp("ValuesAbsolute", child->key) == 0) + cf_util_get_boolean(child, &report_usage); + else if (strcasecmp("ReportUtilization", child->key) == 0 || + strcasecmp("ValuesPercentage", child->key) == 0) + cf_util_get_boolean(child, &report_utilization); else if (strcasecmp("ReportIO", child->key) == 0) cf_util_get_boolean(child, &report_io); else @@ -222,7 +225,7 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, bool have_other = (other_name != NULL) && !isnan(other); - if (values_absolute) { + if (report_usage) { if (have_other) { metric_family_append(fam_usage, label_state, other_name, (value_t){.gauge = other}, &m); @@ -234,7 +237,7 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, (value_t){.gauge = free}, &m); } - if (values_percentage) { + if (report_utilization) { gauge_t total = used + free; if (have_other) { total += other; From 8b831b3beef660fc5aeeef32ce1afd37e20c80a5 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 10 Jan 2024 22:19:58 +0100 Subject: [PATCH 09/15] swap plugin: Populate the `unit` field. --- src/swap.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/swap.c b/src/swap.c index e79f4fbc09..93d68618da 100644 --- a/src/swap.c +++ b/src/swap.c @@ -810,24 +810,28 @@ static int swap_read(void) { { .name = "system.paging.usage", .help = "Unix swap usage", + .unit = "By", .type = METRIC_TYPE_GAUGE, }, [FAM_SWAP_UTILIZATION] = { .name = "system.paging.utilization", .help = "Unix swap utilization", + .unit = "1", .type = METRIC_TYPE_GAUGE, }, [FAM_SWAP_OPS] = { /* used when report_io && !report_bytes */ .name = "system.paging.operations", + .unit = "{operation}", .type = METRIC_TYPE_COUNTER, }, [FAM_SWAP_IO] = { /* used when report_io && report_bytes */ .name = "system.paging.io", + .unit = "By", .type = METRIC_TYPE_COUNTER, }, }; From 72fa4c9ca71aa809b6f8f24db22dd2c99da1c8fe Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Mon, 15 Jan 2024 20:47:17 +0100 Subject: [PATCH 10/15] swap plugin: Report utilization as fraction of one. --- src/swap.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/swap.c b/src/swap.c index 93d68618da..d3ee3781b8 100644 --- a/src/swap.c +++ b/src/swap.c @@ -243,13 +243,13 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, total += other; metric_family_append(fam_utilization, label_state, other_name, - (value_t){.gauge = 100.0 * other / total}, &m); + (value_t){.gauge = other / total}, &m); } metric_family_append(fam_utilization, label_state, state_used, - (value_t){.gauge = 100.0 * used / total}, &m); + (value_t){.gauge = used / total}, &m); metric_family_append(fam_utilization, label_state, state_free, - (value_t){.gauge = 100.0 * free / total}, &m); + (value_t){.gauge = free / total}, &m); } metric_reset(&m); From 8c14e4780f6b10702599d899ed77c5bb945be13f Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 26 Jan 2024 13:58:20 +0100 Subject: [PATCH 11/15] swap plugin: Rename `swap_submit_*` to `swap_append_*`. --- src/swap.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/swap.c b/src/swap.c index d3ee3781b8..7018612598 100644 --- a/src/swap.c +++ b/src/swap.c @@ -212,7 +212,7 @@ static int swap_init(void) /* {{{ */ return 0; } /* }}} int swap_init */ -static void swap_submit_usage3(metric_family_t *fams, char const *device, +static void swap_append_usage3(metric_family_t *fams, char const *device, gauge_t used, gauge_t free, char const *other_name, gauge_t other) { metric_family_t *fam_usage = &fams[FAM_SWAP_USAGE]; @@ -253,11 +253,11 @@ static void swap_submit_usage3(metric_family_t *fams, char const *device, } metric_reset(&m); -} /* void swap_submit_usage3 */ +} /* void swap_append_usage3 */ -static void swap_submit_usage(metric_family_t *fams, char *device, gauge_t used, +static void swap_append_usage(metric_family_t *fams, char *device, gauge_t used, gauge_t free) { - swap_submit_usage3(fams, device, used, free, NULL, NAN); + swap_append_usage3(fams, device, used, free, NULL, NAN); } #if KERNEL_LINUX || HAVE_PERFSTAT || KERNEL_NETBSD @@ -266,7 +266,7 @@ static char const *const label_direction = "system.paging.direction"; static char const *const direction_in = "in"; static char const *const direction_out = "out"; -static void swap_submit_io(metric_family_t *fams, counter_t in, counter_t out, +static void swap_append_io(metric_family_t *fams, counter_t in, counter_t out, derive_t pagesize) { if (!report_io) { return; @@ -283,7 +283,7 @@ static void swap_submit_io(metric_family_t *fams, counter_t in, counter_t out, (value_t){.counter = in}, NULL); metric_family_append(fam, label_direction, direction_out, (value_t){.counter = out}, NULL); -} /* void swap_submit_io */ +} /* void swap_append_io */ #endif #if KERNEL_LINUX @@ -328,7 +328,7 @@ static int swap_read_separate(metric_family_t *fams) /* {{{ */ if (total < used) continue; - swap_submit_usage(fams, path, used * 1024.0, (total - used) * 1024.0); + swap_append_usage(fams, path, used * 1024.0, (total - used) * 1024.0); } fclose(fh); @@ -383,7 +383,7 @@ static int swap_read_combined(metric_family_t *fams) /* {{{ */ if (swap_used < 0.0) return EINVAL; - swap_submit_usage3(fams, NULL, swap_used * 1024.0, swap_free * 1024.0, + swap_append_usage3(fams, NULL, swap_used * 1024.0, swap_free * 1024.0, "cached", swap_cached * 1024.0); return 0; @@ -425,7 +425,7 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ return ENOENT; } - swap_submit_io(fams, swap_in, swap_out, pagesize); + swap_append_io(fams, swap_in, swap_out, pagesize); return 0; } /* }}} int swap_read_io */ @@ -493,7 +493,7 @@ static int swap_read_kstat(metric_family_t *fams) /* {{{ */ swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); - swap_submit_usage3(fams, NULL, swap_alloc, swap_avail, "reserved", swap_resv); + swap_append_usage3(fams, NULL, swap_alloc, swap_avail, "reserved", swap_resv); return 0; } /* }}} int swap_read_kstat */ @@ -577,7 +577,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path)); - swap_submit_usage(fams, path, this_total - this_avail, this_avail); + swap_append_usage(fams, path, this_total - this_avail, this_avail); } /* for (swap_num) */ if (total < avail) { @@ -592,7 +592,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(fams, NULL, total - avail, avail); + swap_append_usage(fams, NULL, total - avail, avail); } sfree(s_paths); @@ -623,7 +623,7 @@ static int swap_read_io(metric_family_t *fams) /* {{{ */ counter_t swap_in = uvmexp.pgswapin; counter_t swap_out = uvmexp.pgswapout; - swap_submit_io(fams, swap_in, swap_out, pagesize); + swap_append_io(fams, swap_in, swap_out, pagesize); return (0); } /* }}} */ @@ -686,7 +686,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, swap_entries[i].se_path, sizeof(path)); - swap_submit_usage(fams, path, this_used, this_total - this_used); + swap_append_usage(fams, path, this_used, this_total - this_used); } /* for (swap_num) */ if (total < used) { @@ -700,7 +700,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == 1), all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_submit_usage(fams, NULL, used, total - used); + swap_append_usage(fams, NULL, used, total - used); } sfree(swap_entries); @@ -729,7 +729,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ return -1; /* The returned values are bytes. */ - swap_submit_usage(fams, NULL, (gauge_t)sw_usage.xsu_used, + swap_append_usage(fams, NULL, (gauge_t)sw_usage.xsu_used, (gauge_t)sw_usage.xsu_avail); return 0; @@ -756,7 +756,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ total *= (gauge_t)kvm_pagesize; used *= (gauge_t)kvm_pagesize; - swap_submit_usage(fams, NULL, used, total - used); + swap_append_usage(fams, NULL, used, total - used); return 0; } /* }}} int swap_read_fam */ @@ -771,7 +771,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ if (swap == NULL) return -1; - swap_submit_usage(fams, NULL, (gauge_t)swap->used, (gauge_t)swap->free); + swap_append_usage(fams, NULL, (gauge_t)swap->used, (gauge_t)swap->free); return 0; } /* }}} int swap_read_fam */ @@ -793,12 +793,12 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ gauge_t free = (gauge_t)(pmemory.pgsp_free * pagesize); gauge_t reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); - swap_submit_usage3(fams, NULL, total - free, free, "reserved", reserved); + swap_append_usage3(fams, NULL, total - free, free, "reserved", reserved); counter_t swap_in = pmemory.pgspins; counter_t swap_out = pmemory.pgspouts; - swap_submit_io(fams, swap_in, swap_out, pagesize); + swap_append_io(fams, swap_in, swap_out, pagesize); return 0; } /* }}} int swap_read_fam */ From b70160caa6b9dc663bd685ddf3dc52a7144f9ebc Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 26 Jan 2024 13:59:04 +0100 Subject: [PATCH 12/15] swap plugin: Call `plugin_dispatch_metric_family` unconditionally. --- src/swap.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/swap.c b/src/swap.c index 7018612598..e4cfe9c3ff 100644 --- a/src/swap.c +++ b/src/swap.c @@ -843,14 +843,12 @@ static int swap_read(void) { for (size_t i = 0; i < FAM_SWAP_MAX; i++) { metric_family_t *fam = &fams[i]; - if (fam->metric.num > 0) { - int status = plugin_dispatch_metric_family(fam); - if (status != 0) { - ERROR("swap plugin: plugin_dispatch_metric_family failed: %s", - STRERROR(status)); - } - metric_family_metric_reset(fam); + int status = plugin_dispatch_metric_family(fam); + if (status != 0) { + ERROR("swap plugin: plugin_dispatch_metric_family failed: %s", + STRERROR(status)); } + metric_family_metric_reset(fam); } return 0; From 35331758ef6453e059bfc13609419f70c66ec9ab Mon Sep 17 00:00:00 2001 From: Eero Tamminen Date: Fri, 26 Jan 2024 14:00:57 +0100 Subject: [PATCH 13/15] collectd.conf(5): Improve wording for `ReportBytes`. --- src/collectd.conf.pod | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index d378928506..0dc7ba4291 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -9333,9 +9333,10 @@ This option is only available if the I can read C =item B B|B -When enabled, the I is reported in bytes. When disabled, the default, -I is reported in pages. This option is available under Linux and -NetBSD only. +When enabled, the I is reported in bytes. When disabled, I +is reported in pages. Defaults to B. This option is available under +Linux and NetBSD only. + =item B B|B From c18af0c6f811fe3aafce762a02110f59d8f813e6 Mon Sep 17 00:00:00 2001 From: Eero Tamminen Date: Fri, 26 Jan 2024 14:05:44 +0100 Subject: [PATCH 14/15] src/collectd.conf.in: Fix default of `ReportBytes`. --- src/collectd.conf.in | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/collectd.conf.in b/src/collectd.conf.in index a269306ab9..283b065cae 100644 --- a/src/collectd.conf.in +++ b/src/collectd.conf.in @@ -1751,7 +1751,7 @@ # ReportUsage true # ReportUtilization false # ReportIO true -# ReportBytes true +# ReportBytes false # # From b26bf158e16e12525119f30fbfb56ae465fa01a3 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Fri, 26 Jan 2024 15:04:32 +0100 Subject: [PATCH 15/15] swap plugin: Patch the "total" value through to `swap_append_usage3`. --- src/swap.c | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/swap.c b/src/swap.c index e4cfe9c3ff..a358251977 100644 --- a/src/swap.c +++ b/src/swap.c @@ -213,7 +213,7 @@ static int swap_init(void) /* {{{ */ } /* }}} int swap_init */ static void swap_append_usage3(metric_family_t *fams, char const *device, - gauge_t used, gauge_t free, + gauge_t total, gauge_t used, gauge_t free, char const *other_name, gauge_t other) { metric_family_t *fam_usage = &fams[FAM_SWAP_USAGE]; metric_family_t *fam_utilization = &fams[FAM_SWAP_UTILIZATION]; @@ -238,10 +238,7 @@ static void swap_append_usage3(metric_family_t *fams, char const *device, } if (report_utilization) { - gauge_t total = used + free; if (have_other) { - total += other; - metric_family_append(fam_utilization, label_state, other_name, (value_t){.gauge = other / total}, &m); } @@ -255,9 +252,9 @@ static void swap_append_usage3(metric_family_t *fams, char const *device, metric_reset(&m); } /* void swap_append_usage3 */ -static void swap_append_usage(metric_family_t *fams, char *device, gauge_t used, - gauge_t free) { - swap_append_usage3(fams, device, used, free, NULL, NAN); +static void swap_append_usage(metric_family_t *fams, char *device, + gauge_t total, gauge_t used, gauge_t free) { + swap_append_usage3(fams, device, total, used, free, NULL, NAN); } #if KERNEL_LINUX || HAVE_PERFSTAT || KERNEL_NETBSD @@ -328,7 +325,8 @@ static int swap_read_separate(metric_family_t *fams) /* {{{ */ if (total < used) continue; - swap_append_usage(fams, path, used * 1024.0, (total - used) * 1024.0); + swap_append_usage(fams, path, total * 1024.0, used * 1024.0, + (total - used) * 1024.0); } fclose(fh); @@ -383,8 +381,8 @@ static int swap_read_combined(metric_family_t *fams) /* {{{ */ if (swap_used < 0.0) return EINVAL; - swap_append_usage3(fams, NULL, swap_used * 1024.0, swap_free * 1024.0, - "cached", swap_cached * 1024.0); + swap_append_usage3(fams, NULL, swap_total * 1024.0, swap_used * 1024.0, + swap_free * 1024.0, "cached", swap_cached * 1024.0); return 0; } /* }}} int swap_read_combined */ @@ -493,7 +491,8 @@ static int swap_read_kstat(metric_family_t *fams) /* {{{ */ swap_resv = (gauge_t)((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize); swap_avail = (gauge_t)((ai.ani_max - ai.ani_resv) * pagesize); - swap_append_usage3(fams, NULL, swap_alloc, swap_avail, "reserved", swap_resv); + swap_append_usage3(fams, NULL, (swap_alloc + swap_resv + swap_avail), + swap_alloc, swap_avail, "reserved", swap_resv); return 0; } /* }}} int swap_read_kstat */ @@ -577,7 +576,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, s->swt_ent[i].ste_path, sizeof(path)); - swap_append_usage(fams, path, this_total - this_avail, this_avail); + swap_append_usage(fams, path, this_total, this_total - this_avail, + this_avail); } /* for (swap_num) */ if (total < avail) { @@ -592,7 +592,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == true) all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_append_usage(fams, NULL, total - avail, avail); + swap_append_usage(fams, NULL, total, total - avail, avail); } sfree(s_paths); @@ -686,7 +686,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ sstrncpy(path, swap_entries[i].se_path, sizeof(path)); - swap_append_usage(fams, path, this_used, this_total - this_used); + swap_append_usage(fams, path, this_total, this_used, + this_total - this_used); } /* for (swap_num) */ if (total < used) { @@ -700,7 +701,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ /* If the "separate" option was specified (report_by_device == 1), all * values have already been dispatched from within the loop. */ if (!report_by_device) { - swap_append_usage(fams, NULL, used, total - used); + swap_append_usage(fams, NULL, total, used, total - used); } sfree(swap_entries); @@ -729,8 +730,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ return -1; /* The returned values are bytes. */ - swap_append_usage(fams, NULL, (gauge_t)sw_usage.xsu_used, - (gauge_t)sw_usage.xsu_avail); + swap_append_usage(fams, NULL, (gauge_t)sw_usage.xsu_total, + (gauge_t)sw_usage.xsu_used, (gauge_t)sw_usage.xsu_avail); return 0; } /* }}} int swap_read_fam */ @@ -756,7 +757,7 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ total *= (gauge_t)kvm_pagesize; used *= (gauge_t)kvm_pagesize; - swap_append_usage(fams, NULL, used, total - used); + swap_append_usage(fams, NULL, total, used, total - used); return 0; } /* }}} int swap_read_fam */ @@ -771,7 +772,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ if (swap == NULL) return -1; - swap_append_usage(fams, NULL, (gauge_t)swap->used, (gauge_t)swap->free); + swap_append_usage(fams, NULL, (gauge_t)swap->total, (gauge_t)swap->used, + (gauge_t)swap->free); return 0; } /* }}} int swap_read_fam */ @@ -793,7 +795,8 @@ static int swap_read_fam(metric_family_t *fams) /* {{{ */ gauge_t free = (gauge_t)(pmemory.pgsp_free * pagesize); gauge_t reserved = (gauge_t)(pmemory.pgsp_rsvd * pagesize); - swap_append_usage3(fams, NULL, total - free, free, "reserved", reserved); + swap_append_usage3(fams, NULL, total, total - free, free, "reserved", + reserved); counter_t swap_in = pmemory.pgspins; counter_t swap_out = pmemory.pgspouts;