From f94934472f6325db33a45ca5a3a29e371085e507 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20G=C3=B6ttsche?= Date: Sat, 21 Aug 2021 21:47:19 +0200 Subject: [PATCH] Linux: rework disk-io parsing Generalize sub-diskname handling, like sdb1/sdb2, to not count the usage twice with the aggregate top-diskname, like sdb. Rely on /proc/diskstats being ordered, e.g. no sub-diskname precedes its top-diskname. Closes: #675 --- linux/Platform.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/linux/Platform.c b/linux/Platform.c index 7d1c190a..05023d50 100644 --- a/linux/Platform.c +++ b/linux/Platform.c @@ -543,6 +543,8 @@ bool Platform_getDiskIO(DiskIOData* data) { if (!fd) return false; + char lastTopDisk[32] = { '\0' }; + unsigned long long int read_sum = 0, write_sum = 0, timeSpend_sum = 0; char lineBuffer[256]; while (fgets(lineBuffer, sizeof(lineBuffer), fd)) { @@ -556,22 +558,11 @@ bool Platform_getDiskIO(DiskIOData* data) { continue; /* only count root disks, e.g. do not count IO from sda and sda1 twice */ - if ((diskname[0] == 's' || diskname[0] == 'h') - && diskname[1] == 'd' - && isalpha((unsigned char)diskname[2]) - && isdigit((unsigned char)diskname[3])) + if (lastTopDisk[0] && String_startsWith(diskname, lastTopDisk)) continue; - /* only count root disks, e.g. do not count IO from mmcblk0 and mmcblk0p1 twice */ - if (diskname[0] == 'm' - && diskname[1] == 'm' - && diskname[2] == 'c' - && diskname[3] == 'b' - && diskname[4] == 'l' - && diskname[5] == 'k' - && isdigit((unsigned char)diskname[6]) - && diskname[7] == 'p') - continue; + /* This assumes disks are listed directly before any of their partitions */ + String_safeStrncpy(lastTopDisk, diskname, sizeof(lastTopDisk)); read_sum += read_tmp; write_sum += write_tmp;