From 99b43db66337131b30b6e9be2967073dda4787b7 Mon Sep 17 00:00:00 2001 From: Shaleen Bathla Date: Wed, 5 Oct 2022 12:20:13 +0530 Subject: [PATCH] mpstat : Add reset parameter to salloc_mp_struct() To reset stats structs memory, add parameter to salloc_mp_struct(). This will be used in the next commit for introducing -H option for handling vCPU physical hotplugs. Use realloc calls which will help in resizing the structs also. if reset is TRUE, memory will be cleared else not cleared Signed-off-by: Shaleen Bathla --- mpstat.c | 64 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/mpstat.c b/mpstat.c index 1032410..859d304 100644 --- a/mpstat.c +++ b/mpstat.c @@ -175,76 +175,82 @@ void int_handler(int sig) * IN: * @nr_cpus Number of CPUs. This is the real number of available CPUs + 1 * because we also have to allocate a structure for CPU 'all'. + * @reset reset the memory only when set to TRUE *************************************************************************** */ -void salloc_mp_struct(int nr_cpus) +void salloc_mp_struct(int nr_cpus, int reset) { int i; for (i = 0; i < 3; i++) { - if ((st_cpu[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus)) + if ((st_cpu[i] = (struct stats_cpu *) realloc(st_cpu[i], STATS_CPU_SIZE * nr_cpus)) == NULL) { - perror("malloc"); + perror("realloc"); exit(4); } - memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus); - if ((st_node[i] = (struct stats_cpu *) malloc(STATS_CPU_SIZE * nr_cpus)) + if ((st_node[i] = (struct stats_cpu *) realloc(st_node[i], STATS_CPU_SIZE * nr_cpus)) == NULL) { - perror("malloc"); + perror("realloc"); exit(4); } - memset(st_node[i], 0, STATS_CPU_SIZE * nr_cpus); - if ((st_irq[i] = (struct stats_global_irq *) malloc(STATS_GLOBAL_IRQ_SIZE * nr_cpus)) + if ((st_irq[i] = (struct stats_global_irq *) realloc(st_irq[i], STATS_GLOBAL_IRQ_SIZE * nr_cpus)) == NULL) { - perror("malloc"); + perror("realloc"); exit(4); } - memset(st_irq[i], 0, STATS_GLOBAL_IRQ_SIZE * nr_cpus); - if ((st_irqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr)) + if ((st_irqcpu[i] = (struct stats_irqcpu *) realloc(st_irqcpu[i], STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr)) == NULL) { - perror("malloc"); + perror("realloc"); exit(4); } - memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr); - if ((st_softirqcpu[i] = (struct stats_irqcpu *) malloc(STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr)) + if ((st_softirqcpu[i] = (struct stats_irqcpu *) realloc(st_softirqcpu[i], STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr)) == NULL) { - perror("malloc"); + perror("realloc"); exit(4); } - memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr); } - if ((cpu_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) { - perror("malloc"); + if ((cpu_bitmap = (unsigned char *) realloc(cpu_bitmap, (nr_cpus >> 3) + 1)) == NULL) { + perror("realloc"); exit(4); } - memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1); - if ((node_bitmap = (unsigned char *) malloc((nr_cpus >> 3) + 1)) == NULL) { - perror("malloc"); + if ((node_bitmap = (unsigned char *) realloc(node_bitmap, (nr_cpus >> 3) + 1)) == NULL) { + perror("realloc"); exit(4); } - memset(node_bitmap, 0, (nr_cpus >> 3) + 1); - if ((cpu_per_node = (int *) malloc(sizeof(int) * nr_cpus)) == NULL) { - perror("malloc"); + if ((cpu_per_node = (int *) realloc(cpu_per_node, sizeof(int) * nr_cpus)) == NULL) { + perror("realloc"); exit(4); } - if ((cpu2node = (int *) malloc(sizeof(int) * nr_cpus)) == NULL) { - perror("malloc"); + if ((cpu2node = (int *) realloc(cpu2node, sizeof(int) * nr_cpus)) == NULL) { + perror("realloc"); exit(4); } - if ((st_cpu_topology = (struct cpu_topology *) malloc(sizeof(struct cpu_topology) * nr_cpus)) == NULL) { - perror("malloc"); + if ((st_cpu_topology = (struct cpu_topology *) realloc(st_cpu_topology, sizeof(struct cpu_topology) * nr_cpus)) == NULL) { + perror("realloc"); exit(4); } + + if (reset == TRUE) { + for (i = 0; i < 3; i++) { + memset(st_cpu[i], 0, STATS_CPU_SIZE * nr_cpus); + memset(st_node[i], 0, STATS_CPU_SIZE * nr_cpus); + memset(st_irq[i], 0, STATS_GLOBAL_IRQ_SIZE * nr_cpus); + memset(st_irqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * irqcpu_nr); + memset(st_softirqcpu[i], 0, STATS_IRQCPU_SIZE * nr_cpus * softirqcpu_nr); + } + memset(cpu_bitmap, 0, (nr_cpus >> 3) + 1); + memset(node_bitmap, 0, (nr_cpus >> 3) + 1); + } } /* @@ -2175,7 +2181,7 @@ int main(int argc, char **argv) * cpu_nr: a value of 2 means there are 2 processors (0 and 1). * In this case, we have to allocate 3 structures: global, proc0 and proc1. */ - salloc_mp_struct(cpu_nr + 1); + salloc_mp_struct(cpu_nr + 1, TRUE); /* Get NUMA node placement */ node_nr = get_node_placement(cpu_nr, cpu_per_node, cpu2node); -- 2.40.0