diff mbox series

[11/35] perf session: Create guest machines from id_index

Message ID 20220711093218.10967-12-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:31 a.m. UTC
Now that id_index has machine_pid, use it to create guest machines.
Create the guest machines with an idle thread because guest events
for "swapper" will be possible.

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

Comments

Ian Rogers July 19, 2022, 5:51 p.m. UTC | #1
On Mon, Jul 11, 2022 at 2:33 AM Adrian Hunter <adrian.hunter@intel.com> wrote:
>
> Now that id_index has machine_pid, use it to create guest machines.
> Create the guest machines with an idle thread because guest events
> for "swapper" will be possible.
>
> Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>

Nothing obviously off to my unqualified eyes :-)

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

Thanks,
Ian

> ---
>  tools/perf/util/session.c | 31 +++++++++++++++++++++++++++++++
>  1 file changed, 31 insertions(+)
>
> diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
> index 5141fe164e97..1af981d5ad3c 100644
> --- a/tools/perf/util/session.c
> +++ b/tools/perf/util/session.c
> @@ -2751,6 +2751,24 @@ void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
>         fprintf(fp, "# ========\n#\n");
>  }
>
> +static int perf_session__register_guest(struct perf_session *session, pid_t machine_pid)
> +{
> +       struct machine *machine = machines__findnew(&session->machines, machine_pid);
> +       struct thread *thread;
> +
> +       if (!machine)
> +               return -ENOMEM;
> +
> +       machine->single_address_space = session->machines.host.single_address_space;
> +
> +       thread = machine__idle_thread(machine);
> +       if (!thread)
> +               return -ENOMEM;
> +       thread__put(thread);
> +
> +       return 0;
> +}
> +
>  int perf_event__process_id_index(struct perf_session *session,
>                                  union perf_event *event)
>  {
> @@ -2762,6 +2780,7 @@ int perf_event__process_id_index(struct perf_session *session,
>         size_t e2_sz = sizeof(struct id_index_entry_2);
>         size_t etot_sz = e1_sz + e2_sz;
>         struct id_index_entry_2 *e2;
> +       pid_t last_pid = 0;
>
>         max_nr = sz / e1_sz;
>         nr = ie->nr;
> @@ -2787,6 +2806,7 @@ int perf_event__process_id_index(struct perf_session *session,
>         for (i = 0; i < nr; i++, (e2 ? e2++ : 0)) {
>                 struct id_index_entry *e = &ie->entries[i];
>                 struct perf_sample_id *sid;
> +               int ret;
>
>                 if (dump_trace) {
>                         fprintf(stdout, " ... id: %"PRI_lu64, e->id);
> @@ -2814,6 +2834,17 @@ int perf_event__process_id_index(struct perf_session *session,
>
>                 sid->machine_pid = e2->machine_pid;
>                 sid->vcpu.cpu = e2->vcpu;
> +
> +               if (!sid->machine_pid)
> +                       continue;
> +
> +               if (sid->machine_pid != last_pid) {
> +                       ret = perf_session__register_guest(session, sid->machine_pid);
> +                       if (ret)
> +                               return ret;
> +                       last_pid = sid->machine_pid;
> +                       perf_guest = true;
> +               }
>         }
>         return 0;
>  }
> --
> 2.25.1
>
diff mbox series

Patch

diff --git a/tools/perf/util/session.c b/tools/perf/util/session.c
index 5141fe164e97..1af981d5ad3c 100644
--- a/tools/perf/util/session.c
+++ b/tools/perf/util/session.c
@@ -2751,6 +2751,24 @@  void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
 	fprintf(fp, "# ========\n#\n");
 }
 
+static int perf_session__register_guest(struct perf_session *session, pid_t machine_pid)
+{
+	struct machine *machine = machines__findnew(&session->machines, machine_pid);
+	struct thread *thread;
+
+	if (!machine)
+		return -ENOMEM;
+
+	machine->single_address_space = session->machines.host.single_address_space;
+
+	thread = machine__idle_thread(machine);
+	if (!thread)
+		return -ENOMEM;
+	thread__put(thread);
+
+	return 0;
+}
+
 int perf_event__process_id_index(struct perf_session *session,
 				 union perf_event *event)
 {
@@ -2762,6 +2780,7 @@  int perf_event__process_id_index(struct perf_session *session,
 	size_t e2_sz = sizeof(struct id_index_entry_2);
 	size_t etot_sz = e1_sz + e2_sz;
 	struct id_index_entry_2 *e2;
+	pid_t last_pid = 0;
 
 	max_nr = sz / e1_sz;
 	nr = ie->nr;
@@ -2787,6 +2806,7 @@  int perf_event__process_id_index(struct perf_session *session,
 	for (i = 0; i < nr; i++, (e2 ? e2++ : 0)) {
 		struct id_index_entry *e = &ie->entries[i];
 		struct perf_sample_id *sid;
+		int ret;
 
 		if (dump_trace) {
 			fprintf(stdout,	" ... id: %"PRI_lu64, e->id);
@@ -2814,6 +2834,17 @@  int perf_event__process_id_index(struct perf_session *session,
 
 		sid->machine_pid = e2->machine_pid;
 		sid->vcpu.cpu = e2->vcpu;
+
+		if (!sid->machine_pid)
+			continue;
+
+		if (sid->machine_pid != last_pid) {
+			ret = perf_session__register_guest(session, sid->machine_pid);
+			if (ret)
+				return ret;
+			last_pid = sid->machine_pid;
+			perf_guest = true;
+		}
 	}
 	return 0;
 }