From e5e098c6751aac641d231aa146be320a8a389d3a Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Sat, 5 Oct 2024 10:23:06 +0200 Subject: [PATCH 1/3] Fix: metric providers for IO where accumulating instead of echoing --- metric_providers/disk/io/cgroup/container/source.c | 10 +++------- metric_providers/network/io/cgroup/container/source.c | 2 +- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/metric_providers/disk/io/cgroup/container/source.c b/metric_providers/disk/io/cgroup/container/source.c index fbf5e5452..22c255b22 100644 --- a/metric_providers/disk/io/cgroup/container/source.c +++ b/metric_providers/disk/io/cgroup/container/source.c @@ -30,9 +30,8 @@ static unsigned int msleep_time=1000; static disk_io_t get_disk_cgroup(char* filename) { long long int rbytes = -1; - unsigned long long int rbytes_sum = 0; long long int wbytes = -1; - unsigned long long int wbytes_sum = 0; + disk_io_t disk_io = {0}; FILE * fd = fopen(filename, "r"); if ( fd == NULL) { @@ -41,8 +40,8 @@ static disk_io_t get_disk_cgroup(char* filename) { } while (fscanf(fd, "%*u:%*u rbytes=%lld wbytes=%lld rios=%*u wios=%*u dbytes=%*u dios=%*u", &rbytes, &wbytes) == 2) { - rbytes_sum += rbytes; - wbytes_sum += wbytes; + disk_io.rbytes += rbytes; + disk_io.wbytes += wbytes; } fclose(fd); @@ -52,9 +51,6 @@ static disk_io_t get_disk_cgroup(char* filename) { exit(1); } - disk_io_t disk_io; - disk_io.rbytes = rbytes_sum; - disk_io.wbytes = wbytes_sum; return disk_io; } diff --git a/metric_providers/network/io/cgroup/container/source.c b/metric_providers/network/io/cgroup/container/source.c index 4c3e2a42c..c647c5f0e 100644 --- a/metric_providers/network/io/cgroup/container/source.c +++ b/metric_providers/network/io/cgroup/container/source.c @@ -54,7 +54,7 @@ static char *trimwhitespace(char *str) { static net_io_t get_network_cgroup(unsigned int pid) { char buf[200], ifname[20]; unsigned long long int r_bytes, t_bytes, r_packets, t_packets; - net_io_t net_io; + net_io_t net_io = {0}; char ns_path[PATH_MAX]; snprintf(ns_path, PATH_MAX, "/proc/%u/ns/net", pid); From 6aff1698b7f3ea9cd4d3d6f1ff3a3378b4e32f56 Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Sat, 5 Oct 2024 10:39:13 +0200 Subject: [PATCH 2/3] Optimization: gettimeofday removed from loop to reduce overhead --- metric_providers/disk/io/procfs/system/source.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/metric_providers/disk/io/procfs/system/source.c b/metric_providers/disk/io/procfs/system/source.c index 8357110d4..4b29062ae 100644 --- a/metric_providers/disk/io/procfs/system/source.c +++ b/metric_providers/disk/io/procfs/system/source.c @@ -31,6 +31,8 @@ static void output_get_disk_procfs() { exit(1); } + gettimeofday(&now, NULL); // one call for get time of day for all interfaces is fine. The overhead would be more than the gain in granularity + while (fgets(buf, 1024, fd)) { // We are not counting dropped packets, as we believe they will at least show up in the // sender side as not dropped. @@ -47,7 +49,6 @@ static void output_get_disk_procfs() { continue; // we skip loop devices } - gettimeofday(&now, NULL); printf("%ld%06ld %llu %llu %s\n", now.tv_sec, now.tv_usec, sectors_read, sectors_written, device_name); usleep(msleep_time*1000); From 8c07d5e3542c5c9ac9eca10b3120182a7211a41f Mon Sep 17 00:00:00 2001 From: Arne Tarara Date: Sat, 5 Oct 2024 10:39:46 +0200 Subject: [PATCH 3/3] Fix: Sleep was in loop of different devices. This makes reporting with wrong interval when more than one device present --- metric_providers/disk/io/procfs/system/source.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/metric_providers/disk/io/procfs/system/source.c b/metric_providers/disk/io/procfs/system/source.c index 4b29062ae..3c72bba76 100644 --- a/metric_providers/disk/io/procfs/system/source.c +++ b/metric_providers/disk/io/procfs/system/source.c @@ -50,10 +50,10 @@ static void output_get_disk_procfs() { } printf("%ld%06ld %llu %llu %s\n", now.tv_sec, now.tv_usec, sectors_read, sectors_written, device_name); - usleep(msleep_time*1000); - } + usleep(msleep_time*1000); // after we have looked at all interfaces + fclose(fd); }