Commit abde8722 authored by Adrian Hunter's avatar Adrian Hunter Committed by Arnaldo Carvalho de Melo
Browse files

perf db-export: Export switch events



Export details of switch events including the threads and their current
comms.

Signed-off-by: default avatarAdrian Hunter <adrian.hunter@intel.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Link: http://lkml.kernel.org/r/20190710085810.1650-20-adrian.hunter@intel.com


Signed-off-by: default avatarArnaldo Carvalho de Melo <acme@redhat.com>
parent b3694e6c
Loading
Loading
Loading
Loading
+89 −0
Original line number Diff line number Diff line
@@ -519,3 +519,92 @@ int db_export__call_return(struct db_export *dbe, struct call_return *cr,

	return 0;
}

static int db_export__pid_tid(struct db_export *dbe, struct machine *machine,
			      pid_t pid, pid_t tid, u64 *db_id,
			      struct comm **comm_ptr, bool *is_idle)
{
	struct thread *thread = machine__find_thread(machine, pid, tid);
	struct thread *main_thread;
	int err = 0;

	if (!thread || !thread->comm_set)
		goto out_put;

	*is_idle = !thread->pid_ && !thread->tid;

	main_thread = thread__main_thread(machine, thread);

	err = db_export__threads(dbe, thread, main_thread, machine, comm_ptr);

	*db_id = thread->db_id;

	thread__put(main_thread);
out_put:
	thread__put(thread);

	return err;
}

int db_export__switch(struct db_export *dbe, union perf_event *event,
		      struct perf_sample *sample, struct machine *machine)
{
	bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
	bool out_preempt = out &&
		(event->header.misc & PERF_RECORD_MISC_SWITCH_OUT_PREEMPT);
	int flags = out | (out_preempt << 1);
	bool is_idle_a = false, is_idle_b = false;
	u64 th_a_id = 0, th_b_id = 0;
	u64 comm_out_id, comm_in_id;
	struct comm *comm_a = NULL;
	struct comm *comm_b = NULL;
	u64 th_out_id, th_in_id;
	u64 db_id;
	int err;

	err = db_export__machine(dbe, machine);
	if (err)
		return err;

	err = db_export__pid_tid(dbe, machine, sample->pid, sample->tid,
				 &th_a_id, &comm_a, &is_idle_a);
	if (err)
		return err;

	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
		pid_t pid = event->context_switch.next_prev_pid;
		pid_t tid = event->context_switch.next_prev_tid;

		err = db_export__pid_tid(dbe, machine, pid, tid, &th_b_id,
					 &comm_b, &is_idle_b);
		if (err)
			return err;
	}

	/*
	 * Do not export if both threads are unknown (i.e. not being traced),
	 * or one is unknown and the other is the idle task.
	 */
	if ((!th_a_id || is_idle_a) && (!th_b_id || is_idle_b))
		return 0;

	db_id = ++dbe->context_switch_last_db_id;

	if (out) {
		th_out_id   = th_a_id;
		th_in_id    = th_b_id;
		comm_out_id = comm_a ? comm_a->db_id : 0;
		comm_in_id  = comm_b ? comm_b->db_id : 0;
	} else {
		th_out_id   = th_b_id;
		th_in_id    = th_a_id;
		comm_out_id = comm_b ? comm_b->db_id : 0;
		comm_in_id  = comm_a ? comm_a->db_id : 0;
	}

	if (dbe->export_context_switch)
		return dbe->export_context_switch(dbe, db_id, machine, sample,
						  th_out_id, comm_out_id,
						  th_in_id, comm_in_id, flags);
	return 0;
}
+8 −0
Original line number Diff line number Diff line
@@ -57,6 +57,11 @@ struct db_export {
	int (*export_call_path)(struct db_export *dbe, struct call_path *cp);
	int (*export_call_return)(struct db_export *dbe,
				  struct call_return *cr);
	int (*export_context_switch)(struct db_export *dbe, u64 db_id,
				     struct machine *machine,
				     struct perf_sample *sample,
				     u64 th_out_id, u64 comm_out_id,
				     u64 th_in_id, u64 comm_in_id, int flags);
	struct call_return_processor *crp;
	struct call_path_root *cpr;
	u64 evsel_last_db_id;
@@ -69,6 +74,7 @@ struct db_export {
	u64 sample_last_db_id;
	u64 call_path_last_db_id;
	u64 call_return_last_db_id;
	u64 context_switch_last_db_id;
};

int db_export__init(struct db_export *dbe);
@@ -98,5 +104,7 @@ int db_export__branch_types(struct db_export *dbe);
int db_export__call_path(struct db_export *dbe, struct call_path *cp);
int db_export__call_return(struct db_export *dbe, struct call_return *cr,
			   u64 *parent_db_id);
int db_export__switch(struct db_export *dbe, union perf_event *event,
		      struct perf_sample *sample, struct machine *machine);

#endif
+41 −0
Original line number Diff line number Diff line
@@ -113,6 +113,7 @@ struct tables {
	PyObject		*call_path_handler;
	PyObject		*call_return_handler;
	PyObject		*synth_handler;
	PyObject		*context_switch_handler;
	bool			db_export_mode;
};

@@ -1237,6 +1238,34 @@ static int python_export_call_return(struct db_export *dbe,
	return 0;
}

static int python_export_context_switch(struct db_export *dbe, u64 db_id,
					struct machine *machine,
					struct perf_sample *sample,
					u64 th_out_id, u64 comm_out_id,
					u64 th_in_id, u64 comm_in_id, int flags)
{
	struct tables *tables = container_of(dbe, struct tables, dbe);
	PyObject *t;

	t = tuple_new(9);

	tuple_set_u64(t, 0, db_id);
	tuple_set_u64(t, 1, machine->db_id);
	tuple_set_u64(t, 2, sample->time);
	tuple_set_s32(t, 3, sample->cpu);
	tuple_set_u64(t, 4, th_out_id);
	tuple_set_u64(t, 5, comm_out_id);
	tuple_set_u64(t, 6, th_in_id);
	tuple_set_u64(t, 7, comm_in_id);
	tuple_set_s32(t, 8, flags);

	call_object(tables->context_switch_handler, t, "context_switch");

	Py_DECREF(t);

	return 0;
}

static int python_process_call_return(struct call_return *cr, u64 *parent_db_id,
				      void *data)
{
@@ -1300,6 +1329,16 @@ static void python_process_event(union perf_event *event,
	}
}

static void python_process_switch(union perf_event *event,
				  struct perf_sample *sample,
				  struct machine *machine)
{
	struct tables *tables = &tables_global;

	if (tables->db_export_mode)
		db_export__switch(&tables->dbe, event, sample, machine);
}

static void get_handler_name(char *str, size_t size,
			     struct perf_evsel *evsel)
{
@@ -1515,6 +1554,7 @@ static void set_table_handlers(struct tables *tables)
	SET_TABLE_HANDLER(sample);
	SET_TABLE_HANDLER(call_path);
	SET_TABLE_HANDLER(call_return);
	SET_TABLE_HANDLER(context_switch);

	/*
	 * Synthesized events are samples but with architecture-specific data
@@ -1833,6 +1873,7 @@ struct scripting_ops python_scripting_ops = {
	.flush_script		= python_flush_script,
	.stop_script		= python_stop_script,
	.process_event		= python_process_event,
	.process_switch		= python_process_switch,
	.process_stat		= python_process_stat,
	.process_stat_interval	= python_process_stat_interval,
	.generate_script	= python_generate_script,