Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf machine: Use realloc_array_as_needed() in machine__set_current_tid()

Prepare machine__set_current_tid() for use with guest machines that do
not currently have a machine->env->nr_cpus_avail value by making use of
realloc_array_as_needed().

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: kvm@vger.kernel.org
Link: https://lore.kernel.org/r/20220711093218.10967-26-adrian.hunter@intel.com
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Adrian Hunter and committed by
Arnaldo Carvalho de Melo
eef8e06e 97406a7e

+8 -19
+7 -19
tools/perf/util/machine.c
··· 3174 3174 3175 3175 pid_t machine__get_current_tid(struct machine *machine, int cpu) 3176 3176 { 3177 - int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS); 3178 - 3179 - if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid) 3177 + if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz) 3180 3178 return -1; 3181 3179 3182 3180 return machine->current_tid[cpu]; ··· 3184 3186 pid_t tid) 3185 3187 { 3186 3188 struct thread *thread; 3187 - int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS); 3189 + const pid_t init_val = -1; 3188 3190 3189 3191 if (cpu < 0) 3190 3192 return -EINVAL; 3191 3193 3192 - if (!machine->current_tid) { 3193 - int i; 3194 - 3195 - machine->current_tid = calloc(nr_cpus, sizeof(pid_t)); 3196 - if (!machine->current_tid) 3197 - return -ENOMEM; 3198 - for (i = 0; i < nr_cpus; i++) 3199 - machine->current_tid[i] = -1; 3200 - } 3201 - 3202 - if (cpu >= nr_cpus) { 3203 - pr_err("Requested CPU %d too large. ", cpu); 3204 - pr_err("Consider raising MAX_NR_CPUS\n"); 3205 - return -EINVAL; 3206 - } 3194 + if (realloc_array_as_needed(machine->current_tid, 3195 + machine->current_tid_sz, 3196 + (unsigned int)cpu, 3197 + &init_val)) 3198 + return -ENOMEM; 3207 3199 3208 3200 machine->current_tid[cpu] = tid; 3209 3201
+1
tools/perf/util/machine.h
··· 57 57 struct map *vmlinux_map; 58 58 u64 kernel_start; 59 59 pid_t *current_tid; 60 + size_t current_tid_sz; 60 61 union { /* Tool specific area */ 61 62 void *priv; 62 63 u64 db_id;