From 2ced5853b4ad622ffd1d9d5629df357a9c27ef7c Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Fri, 21 Oct 2022 15:13:51 +0200 Subject: [PATCH] cpumf/lshwc: fix incremented counter output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit lshwc displays counter values extracted from the /dev/hwctr device. The counter values are retrieved using a loop and incremented instead of just displayed on output. This leads to the situation where a counter is incremented in large numbers while the counter triggering program is active. When this program terminates, the counter should not increase but remain steady. This is not the case as this example shows: # lshwc -i 2 -l90 -a :a | fgrep Total | \ awk -F ',' '{print $2 "," $152 "," $157}' Report time and values of DFLT_ACCESS and DFLT_CC counters sleep 10 # DFLTCC=1 python3 -c "import zlib; zlib.compress(b'A'*6000000000); Increment counter DFLT_ACCESS and DFLT_CC, runs about 45 seconds Output before: 15:24:20,0,0 15:24:22,0,0 15:24:24,0,0 15:24:26,0,0 15:24:28,0,0 15:24:30,0,0 15:24:32,2067027,592996481 <--- python3 program start 15:24:34,6953101,2039012580 15:24:36,14494585,4264340060 15:24:38,24759568,7346128396 .... 15:24:56,232929262,70526362509 15:24:58,269037963,81464038124 15:25:00,307757248,93171809261 15:25:02,348900718,105620730492 15:25:04,392646075,118841031269 <--- python3 program stop 15:25:06,438956016,132853968029 <-- Value column two increased by 49148217 15:25:08,488104233,147709011658 <-- Similar high increase The reason is that lshwc takes the last read counter value and keeps adding it to the total count, even if there zero delta to the previous read value. Fix this by clearing the total count value between ioctl() read operations. Output after: 15:39:26,0,0 15:39:28,0,0 15:39:30,0,0 15:39:32,0,0 15:39:34,0,0 15:39:36,1367250,380721127 <--- python3 program start 15:39:38,4104791,1108181711 15:39:40,6740994,1858843730 15:39:42,9277545,2646351211 15:39:44,11895180,3444375804 .... 15:40:16,54060774,16315924126 15:40:18,57830349,17513712210 15:40:20,61628170,18690791102 15:40:22,65410470,19864086006 15:40:24,69070765,21028646754 <--- python3 program stop 15:40:26,70429758,21472772340 <-- No increase of value in column two 15:40:28,70429758,21472772340 15:40:30,70429758,21472772340 15:40:32,70429758,21472772340 15:40:34,70429758,21472772340 15:40:36,70429758,21472772340 15:40:38,70429758,21472772340 Fixes: 27a562da0ad5 ("cpumf/lshwc: Program to extract complete counter sets") Reported-by: Axel Busch Signed-off-by: Thomas Richter Acked-by: Sumanth Korikkar Signed-off-by: Jan Höppner --- cpumf/lshwc.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cpumf/lshwc.c b/cpumf/lshwc.c index fe4e483e..328cd135 100644 --- a/cpumf/lshwc.c +++ b/cpumf/lshwc.c @@ -356,6 +356,8 @@ static void line(char *header) putchar(','); printf("%ld", ctrname[i].total); comma = true; + ctrname[i].total = 0; + ctrname[i].hitcnt = false; } putchar('\n'); } @@ -446,7 +448,7 @@ static bool add_countervalue(size_t idx, unsigned int cpu, unsigned long value) ctrname[idx].ccv = calloc(max_possible_cpus, sizeof(unsigned long)); if (ctrname[idx].ccv) - ctrname[idx].ccv[cpu] += value; + ctrname[idx].ccv[cpu] = value; ctrname[idx].total += value; ctrname[idx].hitcnt = true; return true; @@ -458,10 +460,8 @@ static int test_read(struct s390_hwctr_read *read) size_t offset = 0; /* Clear previous hit counters */ - for (unsigned int i = 0; i < max_possible_cpus; ++i) { - check[i].sets_hit = 0; + for (unsigned int i = 0; i < max_possible_cpus; ++i) check[i].cpu_hit = false; - } /* Iterate over all CPUs */ for (unsigned int i = 0; i < read->no_cpus; ++i) {