diff mbox series

[30/35] perf intel-pt: Track guest context switches

Message ID 20220711093218.10967-31-adrian.hunter@intel.com (mailing list archive)
State New, archived
Headers show
Series perf intel-pt: Add support for tracing virtual machine user space on the host | expand

Commit Message

Adrian Hunter July 11, 2022, 9:32 a.m. UTC
Use guest context switch events to keep track of which guest thread is
running on a particular guest machine and VCPU.

Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
---
 tools/perf/util/intel-pt.c | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

Comments

Ian Rogers July 20, 2022, 1:13 a.m. UTC | #1
On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Use guest context switch events to keep track of which guest thread is
> running on a particular guest machine and VCPU.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>

Acked-by: Ian Rogers <irogers@google.com>

Thanks,
Ian

> ---
>  tools/perf/util/intel-pt.c | 23 +++++++++++++++++++++++
>  1 file changed, 23 insertions(+)
>
> diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
> index a8798b5bb311..98b097fec476 100644
> --- a/tools/perf/util/intel-pt.c
> +++ b/tools/perf/util/intel-pt.c
> @@ -78,6 +78,7 @@ struct intel_pt {
>         bool use_thread_stack;
>         bool callstack;
>         bool cap_event_trace;
> +       bool have_guest_sideband;
>         unsigned int br_stack_sz;
>         unsigned int br_stack_sz_plus;
>         int have_sched_switch;
> @@ -3079,6 +3080,25 @@ static int intel_pt_context_switch_in(struct intel_pt *pt,
>         return machine__set_current_tid(pt->machine, cpu, pid, tid);
>  }
>
> +static int intel_pt_guest_context_switch(struct intel_pt *pt,
> +                                        union perf_event *event,
> +                                        struct perf_sample *sample)
> +{
> +       bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
> +       struct machines *machines = &pt->session->machines;
> +       struct machine *machine = machines__find(machines, sample->machine_pid);
> +
> +       pt->have_guest_sideband = true;
> +
> +       if (out)
> +               return 0;
> +
> +       if (!machine)
> +               return -EINVAL;
> +
> +       return machine__set_current_tid(machine, sample->vcpu, sample->pid, sample->tid);
> +}
> +
>  static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
>                                    struct perf_sample *sample)
>  {
> @@ -3086,6 +3106,9 @@ static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
>         pid_t pid, tid;
>         int cpu, ret;
>
> +       if (perf_event__is_guest(event))
> +               return intel_pt_guest_context_switch(pt, event, sample);
> +
>         cpu = sample->cpu;
>
>         if (pt->have_sched_switch == 3) {
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index a8798b5bb311..98b097fec476 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -78,6 +78,7 @@  struct intel_pt {
 	bool use_thread_stack;
 	bool callstack;
 	bool cap_event_trace;
+	bool have_guest_sideband;
 	unsigned int br_stack_sz;
 	unsigned int br_stack_sz_plus;
 	int have_sched_switch;
@@ -3079,6 +3080,25 @@  static int intel_pt_context_switch_in(struct intel_pt *pt,
 	return machine__set_current_tid(pt->machine, cpu, pid, tid);
 }
 
+static int intel_pt_guest_context_switch(struct intel_pt *pt,
+					 union perf_event *event,
+					 struct perf_sample *sample)
+{
+	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
+	struct machines *machines = &pt->session->machines;
+	struct machine *machine = machines__find(machines, sample->machine_pid);
+
+	pt->have_guest_sideband = true;
+
+	if (out)
+		return 0;
+
+	if (!machine)
+		return -EINVAL;
+
+	return machine__set_current_tid(machine, sample->vcpu, sample->pid, sample->tid);
+}
+
 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
 				   struct perf_sample *sample)
 {
@@ -3086,6 +3106,9 @@  static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
 	pid_t pid, tid;
 	int cpu, ret;
 
+	if (perf_event__is_guest(event))
+		return intel_pt_guest_context_switch(pt, event, sample);
+
 	cpu = sample->cpu;
 
 	if (pt->have_sched_switch == 3) {