From 9df45d4256acbd75443ebc72ab72a064145d819c Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Fri, 25 Oct 2024 16:22:44 +0200 Subject: [PATCH] cpumf/lshwc: Allocate memory for per-cpu counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allocate the memory required to save the per CPU CPUMF counter value at the beginning of program execution when the involved number of CPUs is known. This a bit faster then checking for memory allocation each time a counter value is retrieved. Signed-off-by: Thomas Richter Reviewed-by: Hendrik Brueckner Signed-off-by: Jan Höppner --- cpumf/lshwc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/cpumf/lshwc.c b/cpumf/lshwc.c index fbd9e210..38a47f4e 100644 --- a/cpumf/lshwc.c +++ b/cpumf/lshwc.c @@ -490,11 +490,7 @@ static bool add_countervalue(size_t idx, unsigned int cpu, unsigned long value) warnx("Invalid CPU number %d", cpu); return false; } - if (!ctrname[idx].ccv) /* Unknown counter */ - 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; @@ -757,6 +753,9 @@ int main(int argc, char **argv) return EXIT_FAILURE; } + for (unsigned int i = 0; i < ARRAY_SIZE(ctrname); ++i) + ctrname[i].ccv = util_zalloc(max_possible_cpus * sizeof(unsigned long)); + if (optind >= argc) { ch = do_it(NULL); } else {