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

perf tools: Add guest_cpu to hypervisor threads

It is possible to know which guest machine was running at a point in time
based on the PID of the currently running host thread. That is, perf
identifies guest machines by the PID of the hypervisor.

To determine the guest CPU, put it on the hypervisor (QEMU) thread for
that VCPU.

This is done when processing the id_index which provides the necessary
information.

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-13-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
797efbc5 ff7a78c2

+20
+18
tools/perf/util/session.c
··· 2769 2769 return 0; 2770 2770 } 2771 2771 2772 + static int perf_session__set_guest_cpu(struct perf_session *session, pid_t pid, 2773 + pid_t tid, int guest_cpu) 2774 + { 2775 + struct machine *machine = &session->machines.host; 2776 + struct thread *thread = machine__findnew_thread(machine, pid, tid); 2777 + 2778 + if (!thread) 2779 + return -ENOMEM; 2780 + thread->guest_cpu = guest_cpu; 2781 + thread__put(thread); 2782 + 2783 + return 0; 2784 + } 2785 + 2772 2786 int perf_event__process_id_index(struct perf_session *session, 2773 2787 union perf_event *event) 2774 2788 { ··· 2859 2845 last_pid = sid->machine_pid; 2860 2846 perf_guest = true; 2861 2847 } 2848 + 2849 + ret = perf_session__set_guest_cpu(session, sid->machine_pid, e->tid, e2->vcpu); 2850 + if (ret) 2851 + return ret; 2862 2852 } 2863 2853 return 0; 2864 2854 }
+1
tools/perf/util/thread.c
··· 47 47 thread->tid = tid; 48 48 thread->ppid = -1; 49 49 thread->cpu = -1; 50 + thread->guest_cpu = -1; 50 51 thread->lbr_stitch_enable = false; 51 52 INIT_LIST_HEAD(&thread->namespaces_list); 52 53 INIT_LIST_HEAD(&thread->comm_list);
+1
tools/perf/util/thread.h
··· 39 39 pid_t tid; 40 40 pid_t ppid; 41 41 int cpu; 42 + int guest_cpu; /* For QEMU thread */ 42 43 refcount_t refcnt; 43 44 bool comm_set; 44 45 int comm_len;