OSDN Git Service

perf machine: Use realloc_array_as_needed() in machine__set_current_tid()
authorAdrian Hunter <adrian.hunter@intel.com>
Mon, 11 Jul 2022 09:32:08 +0000 (12:32 +0300)
committerArnaldo Carvalho de Melo <acme@redhat.com>
Wed, 20 Jul 2022 14:08:37 +0000 (11:08 -0300)
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>
tools/perf/util/machine.c
tools/perf/util/machine.h

index 16d2251..44da170 100644 (file)
@@ -3174,9 +3174,7 @@ int machines__for_each_thread(struct machines *machines,
 
 pid_t machine__get_current_tid(struct machine *machine, int cpu)
 {
-       int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
-
-       if (cpu < 0 || cpu >= nr_cpus || !machine->current_tid)
+       if (cpu < 0 || (size_t)cpu >= machine->current_tid_sz)
                return -1;
 
        return machine->current_tid[cpu];
@@ -3186,26 +3184,16 @@ int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
                             pid_t tid)
 {
        struct thread *thread;
-       int nr_cpus = min(machine->env->nr_cpus_avail, MAX_NR_CPUS);
+       const pid_t init_val = -1;
 
        if (cpu < 0)
                return -EINVAL;
 
-       if (!machine->current_tid) {
-               int i;
-
-               machine->current_tid = calloc(nr_cpus, sizeof(pid_t));
-               if (!machine->current_tid)
-                       return -ENOMEM;
-               for (i = 0; i < nr_cpus; i++)
-                       machine->current_tid[i] = -1;
-       }
-
-       if (cpu >= nr_cpus) {
-               pr_err("Requested CPU %d too large. ", cpu);
-               pr_err("Consider raising MAX_NR_CPUS\n");
-               return -EINVAL;
-       }
+       if (realloc_array_as_needed(machine->current_tid,
+                                   machine->current_tid_sz,
+                                   (unsigned int)cpu,
+                                   &init_val))
+               return -ENOMEM;
 
        machine->current_tid[cpu] = tid;
 
index 199807a..74935df 100644 (file)
@@ -57,6 +57,7 @@ struct machine {
        struct map        *vmlinux_map;
        u64               kernel_start;
        pid_t             *current_tid;
+       size_t            current_tid_sz;
        union { /* Tool specific area */
                void      *priv;
                u64       db_id;