Skip to contentSkip to conversation

libtraceeval: Updates

PATCH New Steven Rostedt 1 participant 5 messages Oct 25, 2024 Documentation

lore
Share & tools
lore
#0 lore raw

cover libtraceeval: Updates

From: "Steven Rostedt (Google)" <[email protected]>

Add more updates to make it easier to use the library, and simplify task-eval.

Steven Rostedt (Google) (4):
libtraceeval task-eval: Add wake up latency libtraceeval samples: Add PARKED and DEAD fields
libtraceevent: Add DECLARE_TRACEEVAL_<types>
libtraceeval: Add traceeval_type_index() API

 Documentation/libtraceeval-init.txt |  52 ++-
 Documentation/libtraceeval.txt      |   4 +
 include/traceeval.h                 |  34 ++
 samples/task-eval.c                 | 499 ++++++++++++++++++++--------
 4 files changed, 434 insertions(+), 155 deletions(-)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/4 libtraceeval task-eval: Add wake up latency

From: "Steven Rostedt (Google)" <[email protected]>

Add logic to keep track of the wake up latency if the sched_waking event is recorded.

---
 samples/task-eval.c | 238 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 222 insertions(+), 16 deletions(-)
samples/task-eval.c +222 -16
index 178936dec686..7b709f715332 100644
@@ -92,7 +92,8 @@ enum sched_state {
 	IDLE,
 	ZOMBIE,
 	EXITED,
-	OTHER
+	OTHER,
+	WAKEUP,
 };
 
 /*
@@ -222,6 +223,68 @@ static void insert_cpu_data(struct traceeval *teval, int cpu, int state,
 	traceeval_insert(teval, keys, vals);
 }
 
+/*
+ * When tracking tasks and threads wake up timings.
+ */
+static struct traceeval_type wakeup_delta_keys[] = {
+	{
+		.type = TRACEEVAL_TYPE_NUMBER,
+		.name = "PID",
+	},
+};
+
+/*
+ * When finishing the timings of the task being woken up.
+ */
+static struct traceeval_type wakeup_delta_vals[] = {
+	{
+		.type = TRACEEVAL_TYPE_STRING,
+		.name = "COMM",
+	},
+	{
+		.type = TRACEEVAL_TYPE_NUMBER,
+		.name = "Prio",
+	},
+};
+
+static void start_wakeup_data(struct traceeval *teval, int pid,
+			      const char *comm, int prio, unsigned long long ts)
+{
+	struct traceeval_data keys[1];
+	struct traceeval_data vals[2];
+
+	TRACEEVAL_SET_NUMBER(keys[0], pid);
+
+	TRACEEVAL_SET_CSTRING(vals[0], comm);
+	TRACEEVAL_SET_NUMBER(vals[1], prio);
+
+	traceeval_delta_start(teval, keys, vals, ts);
+}
+
+static int stop_wakeup_data(struct traceeval *teval, int pid,
+			    const char **comm, int *prio, unsigned long long ts,
+			    unsigned long long *delta)
+{
+	const struct traceeval_data *results;
+	struct traceeval_data keys[1];
+	int ret;
+
+	TRACEEVAL_SET_NUMBER(keys[0], pid);
+
+	ret = traceeval_delta_stop(teval, keys, &results, ts, delta, NULL);
+	if (ret < 1)
+		return ret;
+
+	if (comm)
+		*comm = results[0].string;
+
+	if (prio)
+		*prio = results[1].number;
+
+	traceeval_results_release(teval, results);
+	return 1;
+}
+
 /*
  * When tracking tasks and threads, remember the task id (PID)
  * when scheduling out (for sleep state) or in (for running state).
@@ -405,11 +468,11 @@ static struct traceeval_type thread_keys[] = {
 	},
 	{
 		.type = TRACEEVAL_TYPE_NUMBER,
-		.name = "Schedule state",
+		.name = "Prio",
 	},
 	{
 		.type = TRACEEVAL_TYPE_NUMBER,
-		.name = "Prio",
+		.name = "Schedule state",
 	},
 };
 
@@ -432,8 +495,64 @@ static void insert_thread_data(struct traceeval *teval,
 	struct traceeval_data vals[1];
 
 	TRACEEVAL_SET_NUMBER(keys[0], tid);
-	TRACEEVAL_SET_NUMBER(keys[1], state);
-	TRACEEVAL_SET_NUMBER(keys[2], prio);
+	TRACEEVAL_SET_NUMBER(keys[1], prio);
+	TRACEEVAL_SET_NUMBER(keys[2], state);
+
+	TRACEEVAL_SET_DELTA(vals[0], delta, timestamp);
+
+	traceeval_insert(teval, keys, vals);
+}
+
+static struct traceeval_type wakeup_task_keys[] = {
+	{
+		.type = TRACEEVAL_TYPE_STRING,
+		.name = "COMM"
+	},
+};
+
+static struct traceeval_type wakeup_thread_keys[] = {
+	{
+		.type = TRACEEVAL_TYPE_NUMBER,
+		.name = "PID"
+	},
+	{
+		.type = TRACEEVAL_TYPE_NUMBER,
+		.name = "Prio"
+	},
+};
+
+static struct traceeval_type wakeup_vals[] = {
+	{
+		.type = TRACEEVAL_TYPE_DELTA,
+		.name = DELTA_NAME,
+	},
+};
+
+static void insert_wakeup_task_data(struct traceeval *teval,
+				    const char *comm,
+				    unsigned long long delta,
+				    unsigned long long timestamp)
+{
+	struct traceeval_data keys[1];
+	struct traceeval_data vals[1];
+
+	TRACEEVAL_SET_CSTRING(keys[0], comm);
+
+	TRACEEVAL_SET_DELTA(vals[0], delta, timestamp);
+
+	traceeval_insert(teval, keys, vals);
+}
+
+static void insert_wakeup_thread_data(struct traceeval *teval,
+				      int tid, int prio,
+				      unsigned long long delta,
+				      unsigned long long timestamp)
+{
+	struct traceeval_data keys[2];
+	struct traceeval_data vals[1];
+
+	TRACEEVAL_SET_NUMBER(keys[0], tid);
+	TRACEEVAL_SET_NUMBER(keys[1], prio);
 
 	TRACEEVAL_SET_DELTA(vals[0], delta, timestamp);
 
@@ -443,11 +562,13 @@ static void insert_thread_data(struct traceeval *teval,
 struct process_data {
 	struct traceeval	*teval_cpus;
 	struct traceeval	*teval_threads;
+	struct traceeval	*teval_wakeup;
 };
 
 struct task_data {
 	struct traceeval	*teval_cpus;
 	struct traceeval	*teval_tasks;
+	struct traceeval	*teval_wakeup;
 	unsigned long long	last_ts;
 	char			*comm;
 };
@@ -468,6 +589,7 @@ static void release_data(const struct traceeval_type *type,
 	pdata = data->pointer;
 	traceeval_release(pdata->teval_cpus);
 	traceeval_release(pdata->teval_threads);
+	traceeval_release(pdata->teval_wakeup);
 	free(pdata);
 	data->pointer = NULL;
 }
@@ -481,6 +603,10 @@ static void init_process_data(struct process_data *pdata)
 	pdata->teval_threads = traceeval_init(thread_keys, thread_vals);
 	if (!pdata->teval_threads)
 		pdie("Creating trace eval threads");
+
+	pdata->teval_wakeup = traceeval_init(wakeup_thread_keys, wakeup_vals);
+	if (!pdata->teval_wakeup)
+		pdie("Creating trace eval wakeup");
 }
 
 void set_process_data(struct task_data *tdata, const char *comm, void *data)
@@ -576,6 +702,7 @@ static void update_thread(struct task_data *tdata, int pid, const char *comm,
 			  unsigned long long ts)
 {
 	struct process_data *pdata;
+	int ret;
 
 	pdata = get_process_data(tdata, comm);
 
@@ -583,6 +710,12 @@ static void update_thread(struct task_data *tdata, int pid, const char *comm,
 
 	/* Also update the process */
 	insert_task_data(tdata->teval_tasks, comm, state, pdata, delta, ts);
+
+	ret = stop_wakeup_data(tdata->teval_wakeup, pid, &comm, &prio, ts, &delta);
+	if (ret < 1)
+		return;
+	insert_wakeup_task_data(tdata->teval_wakeup, comm, delta, ts);
+	insert_wakeup_thread_data(pdata->teval_wakeup, pid, prio, delta, ts);
 }
 
 static void start_running_thread(struct task_data *tdata,
@@ -660,6 +793,12 @@ static void sched_out(struct task_data *tdata, const char *comm,
 	/* Start timing the task while it's off the CPU */
 	start_task_data(tdata->teval_tasks, pid, state, comm, prio, record->ts);
 
+	/*
+	 * If a wakeup happened when the task was running, do not record
+	 * the wakeup latency.
+	 */
+	stop_wakeup_data(tdata->teval_wakeup, pid, NULL, NULL, record->ts, NULL);
+
 	if (ret <= 0)
 		return;
 
@@ -759,6 +898,43 @@ static int switch_func(struct tracecmd_input *handle, struct tep_event *event,
 	return 0;
 }
 
+static int wakeup_func(struct tracecmd_input *handle, struct tep_event *event,
+		       struct tep_record *record, int cpu, void *data)
+{
+	static struct tep_format_field *comm_field;
+	static struct tep_format_field *pid_field;
+	static struct tep_format_field *prio_field;
+	struct task_data *tdata = data;
+	unsigned long long val;
+	const char *comm;
+	int prio;
+	int pid;
+	int ret;
+
+	if (!comm_field) {
+		comm_field = get_field(event, "comm");
+		pid_field = get_field(event, "pid");
+		prio_field = get_field(event, "prio");
+	}
+
+	comm = record->data + comm_field->offset;
+	if (tdata->comm && strcmp(comm, tdata->comm) != 0)
+		return 0;
+
+	ret = tep_read_number_field(pid_field, record->data, &val);
+	if (ret < 0)
+		die("Could not read sched_switch pid for record");
+	pid = val;
+
+	ret = tep_read_number_field(prio_field, record->data, &val);
+	if (ret < 0)
+		die("Could not read sched_switch prio for record");
+	prio = val;
+
+	start_wakeup_data(tdata->teval_wakeup, pid, comm, prio, record->ts);
+	return 0;
+}
+
 static void print_microseconds(int idx, unsigned long long nsecs)
 {
 	unsigned long long usecs;
@@ -947,10 +1123,15 @@ static void display_state_times(int state, struct traceeval_stat *stat)
 	case SLEEP:
 		printf("      Total sleep time (us):");
 		print_stats(12, stat);
+		break;
+	case WAKEUP:
+		printf("      Total wakeup time (us):");
+		print_stats(11, stat);
+		break;
 	}
 }
 
-static void display_threads(struct traceeval *teval)
+static void display_threads(struct traceeval *teval, struct traceeval *wake_teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
 	const struct traceeval_data *keys;
@@ -962,15 +1143,15 @@ static void display_threads(struct traceeval *teval)
 	traceeval_iterator_sort(iter, thread_keys[0].name, 0, true);
 
 	/* PRIO */
-	traceeval_iterator_sort(iter, thread_keys[2].name, 1, true);
+	traceeval_iterator_sort(iter, thread_keys[1].name, 1, true);
 
 	/* STATE */
-	traceeval_iterator_sort(iter, thread_keys[1].name, 2, true);
+	traceeval_iterator_sort(iter, thread_keys[2].name, 2, true);
 
 	while (traceeval_iterator_next(iter, &keys) > 0) {
 		int tid = keys[0].number;
-		int state = keys[1].number;
-		int prio = keys[2].number;
+		int prio = keys[1].number;
+		int state = keys[2].number;
 
 		stat = traceeval_iterator_stat(iter, DELTA_NAME);
 		if (!stat)
@@ -987,6 +1168,14 @@ static void display_threads(struct traceeval *teval)
 		last_prio = prio;
 
 		display_state_times(state, stat);
+
+		if (state != RUNNING)
+			continue;
+
+		stat = traceeval_stat_size(wake_teval, keys, 2, DELTA_NAME);
+		if (!stat)
+			continue;
+		display_state_times(WAKEUP, stat);
 	}
 
 	traceeval_iterator_put(iter);
@@ -997,12 +1186,12 @@ static void display_threads(struct traceeval *teval)
 
 static void display_process(struct process_data *pdata)
 {
-	display_threads(pdata->teval_threads);
+	display_threads(pdata->teval_threads, pdata->teval_wakeup);
 	display_cpus(pdata->teval_cpus);
 	printf("\n");
 }
 
-static void display_process_stats(struct traceeval *teval,
+static void display_process_stats(struct traceeval *teval, struct traceeval *wake_teval,
 				  struct process_data *pdata, const char *comm)
 {
 	struct traceeval_stat *stat;
@@ -1016,10 +1205,18 @@ static void display_process_stats(struct traceeval *teval,
 
 		stat = traceeval_stat(teval, keys, DELTA_NAME);
 		display_state_times(i, stat);
+
+		if (i != RUNNING)
+			continue;
+
+		stat = traceeval_stat_size(wake_teval, keys, 1, DELTA_NAME);
+		if (!stat)
+			continue;
+		display_state_times(WAKEUP, stat);
 	}
 }
 
-static void display_processes(struct traceeval *teval)
+static void display_processes(struct traceeval *teval, struct traceeval *wake_teval)
 {
 	struct traceeval_iterator *iter = traceeval_iterator_get(teval);
 	const struct traceeval_data *keys;
@@ -1049,7 +1246,7 @@ static void display_processes(struct traceeval *teval)
 
 		printf("Task: %s\n", comm);
 
-		display_process_stats(teval, pdata, comm);
+		display_process_stats(teval, wake_teval, pdata, comm);
 		if (pdata)
 			display_process(pdata);
 	}
@@ -1066,7 +1263,7 @@ static void display(struct task_data *tdata)
 	unsigned long long idle_time = 0;
 
 	if (tdata->comm) {
-		return display_processes(tdata->teval_tasks);
+		return display_processes(tdata->teval_tasks, tdata->teval_wakeup);
 	}
 
 	iter = traceeval_iterator_get(teval);
@@ -1105,7 +1302,7 @@ static void display(struct task_data *tdata)
 	display_cpus(tdata->teval_cpus);
 
 	printf("\n");
-	display_processes(tdata->teval_tasks);
+	display_processes(tdata->teval_tasks, tdata->teval_wakeup);
 }
 
 static void free_tdata(struct task_data *tdata)
@@ -1115,6 +1312,7 @@ static void free_tdata(struct task_data *tdata)
 
 	traceeval_release(tdata->teval_cpus);
 	traceeval_release(tdata->teval_tasks);
+	traceeval_release(tdata->teval_wakeup);
 }
 
 /*
@@ -1237,7 +1435,15 @@ int main (int argc, char **argv)
 	if (traceeval_delta_create(data.teval_cpus, cpu_delta_keys, cpu_delta_vals) < 0)
 		pdie("Creating trace delta cpus");
 
+	data.teval_wakeup = traceeval_init(wakeup_task_keys, wakeup_vals);
+	if (!data.teval_wakeup)
+		pdie("Creating trace eval wakeup");
+
+	if (traceeval_delta_create(data.teval_wakeup, wakeup_delta_keys, wakeup_delta_vals) < 0)
+		pdie("Creating trace delta wakeup");
+
 	tracecmd_follow_event(handle, "sched", "sched_switch", switch_func, &data);
+	tracecmd_follow_event(handle, "sched", "sched_waking", wakeup_func, &data);
 
 	tracecmd_iterate_events(handle, NULL, 0, event_callback, &data);
 
Signature · 1 line
2.45.2
#2 lore raw

PATCH 2/4 libtraceeval samples: Add PARKED and DEAD fields

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

get_stop_state() was returning PREEMPT when a task was really in the PARKED or DEAD state. Check those as well.

---
 samples/task-eval.c | 6 ++++++
 1 file changed, 6 insertions(+)
samples/task-eval.c +6 -0
index 7b709f715332..47ccce8e4737 100644
@@ -91,7 +91,9 @@ enum sched_state {
 	SLEEP,
 	IDLE,
 	ZOMBIE,
+	PARKED,
 	EXITED,
+	DEAD,
 	OTHER,
 	WAKEUP,
 };
@@ -749,6 +751,10 @@ static int get_stop_state(unsigned long long val)
 		return ZOMBIE;
 	if (val & 0x20)
 		return EXITED;
+	if (val & 0x40)
+		return PARKED;
+	if (val & 0x80)
+		return DEAD;
 	return PREEMPT;
 }
 
Signature · 1 line
2.45.2
#3 lore raw

PATCH 3/4 libtraceevent: Add DECLARE_TRACEEVAL_<types>

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Using the format of:

{
.type = TRACEEVAL_TYPE_NUMBER,
.name = "Schedule state"
},

  {
	.type = TRACEEVAL_TYPE_STRING,
	.name = "COMM",
  },
  {
	.type = TRACEEVAL_TYPE_NUMBER,
	.name = "Prio",

},

Is cumbersome, instead add macros that make it easier to assign these values:

DECLARE_TRACEEVAL_NUMBER("Schedule state"), DECLARE_TRACEEVAL_STRING("COMM"), DECLARE_TRACEEVAL_NUMBER("Prio"),

---
 include/traceeval.h |  19 +++++++
 samples/task-eval.c | 122 +++++++++-----------------------------------
 2 files changed, 43 insertions(+), 98 deletions(-)
include/traceeval.h +19 -0
index f02ecdd58ffb..7de59a302445 100644
@@ -97,6 +97,24 @@ struct traceeval_data {
 	};
 };
 
+#define __TRACEEVAL_DECLARE(data_type, data_name)			\
+	{  .type = TRACEEVAL_TYPE_##data_type, .name = (data_name) }
+
+#define DECLARE_TRACEEVAL_NUMBER(data)	   __TRACEEVAL_DECLARE(NUMBER, data)
+#define DECLARE_TRACEEVAL_NUMBER_8(data)   __TRACEEVAL_DECLARE(NUMBER_8, data)
+#define DECLARE_TRACEEVAL_NUMBER_16(data)  __TRACEEVAL_DECLARE(NUMBER_16, data)
+#define DECLARE_TRACEEVAL_NUMBER_32(data)  __TRACEEVAL_DECLARE(NUMBER_32, data)
+#define DECLARE_TRACEEVAL_NUMBER_64(data)  __TRACEEVAL_DECLARE(NUMBER_64, data)
+#define DECLARE_TRACEEVAL_STRING(data)	   __TRACEEVAL_DECLARE(STRING, data)
+#define DECLARE_TRACEEVAL_CSTRING(data)	   __TRACEEVAL_DECLARE(STRING, data)
+#define DECLARE_TRACEEVAL_DELTA(data)	   __TRACEEVAL_DECLARE(DELTA, data)
+#define DECLARE_TRACEEVAL_POINTER(data_name, data_release, data_copy)	\
+	{								\
+		.type = TRACEEVAL_TYPE_POINTER,				\
+		.name = (data_name),					\
+		.release = data_release, .copy = data_copy,		\
+	}
+
 #define __TRACEEVAL_DATA(data_type, member, data)			\
 	{  .type = TRACEEVAL_TYPE_##data_type, .member = (data) }
 
@@ -362,6 +380,7 @@ int traceeval_iterator_query(struct traceeval_iterator *iter,
 			     const struct traceeval_data **results);
 void traceeval_iterator_results_release(struct traceeval_iterator *iter,
 					const struct traceeval_data *results);
+int traceeval_iterator_reset(struct traceeval_iterator *iter);
 struct traceeval_stat *traceeval_iterator_stat(struct traceeval_iterator *iter,
 					       const char *val_name);
 int traceeval_iterator_delta_stop(struct traceeval_iterator *iter,
samples/task-eval.c +24 -98

206 lines not on this page · raw message

Signature · 1 line
2.45.2
#4 lore raw

PATCH v2 4/4 libtraceeval: Add traceeval_type_index() API

In reply to Steven Rostedt

From: "Steven Rostedt (Google)" <[email protected]>

Add a way to get the index of a traceeval_type from its name. This makes indexing the elements more robust and less error prone. By adding a way to index the names via variables, keys can be added and rearranged without worrying about the other accesses in the program being affected.

The new functions are:

traceeval_type_index()
traceeval_type_index_size()

The use case can by:

static struct traceeval_type keys[] = {
	{
		.type = TRACEEVAL_TYPE_NUMBER,
		.name = "PID"
	}
};

static ssize_t MY_KEY;

static void init_keys(void)

{
	MY_KEY = traceeval_type_index("PID", keys);
}

static void insert_data(struct traceeval *teval, int data)

{
	struct traceeval_data keys[TRACEEVAL_ARRAY_SIZE(keys)];

	TRACEEVAL_SET_NUMBER(keys[MY_KEY], data);

	traceeval_insert(teval, keys, NULL);
}

Using this method keeps from having to constantly tinker with hardcoded index numbers when adding or rearranging keys and values.

---
Changes since v1: https://lore.kernel.org/linux-trace-devel/[email protected]

- Rebased on latest work

 Documentation/libtraceeval-init.txt |  52 ++++--
 Documentation/libtraceeval.txt      |   4 +
 include/traceeval.h                 |  15 ++
 samples/task-eval.c                 | 241 ++++++++++++++++++----------
 4 files changed, 217 insertions(+), 95 deletions(-)
Documentation/libtraceeval-init.txt +42 -10
index 1c09cee319c8..10a2729372bb 100644
@@ -3,7 +3,8 @@ libtraceeval(3)
 
 NAME
 ----
-traceeval_init, traceeval_init_size, traceeval_release - Create a trace evaluation helper mechanism.
+traceeval_init, traceeval_init_size, traceeval_release, traceeval_type_index, traceeval_type_index_size
+- Create a trace evaluation helper mechanism.
 
 SYNOPSIS
 --------
@@ -15,6 +16,8 @@ struct traceeval pass:[*]*traceeval_init*(struct traceeval_type pass:[*]_keys_,
 struct traceeval pass:[*]*traceeval_init_size*(struct traceeval_type pass:[*]_keys_, struct traceeval_type pass:[*]_vals_,
 				       int _nr_keys_, int _nr_vals_);
 void *traceeval_release*(struct traceeval pass:[*]_teval_);
+ssize_t *traceeval_type_index*(const char pass:[*]_name_, struct traceeval_type pass:*]_type_);
+ssize_t *traceeval_type_index_size*(const char pass:[*]_name_, struct traceeval_type pass:*]_type_, size_t nr_types);
 --
 
 DESCRIPTION
@@ -164,11 +167,22 @@ requires static size arrays is because it is really just a macro that calles
 The *traceeval_release()* function releases and frees all the resources of
 a traceeval returned by *traceeval_init()* and *traceeval_init_size()*.
 
+The *traceeval_type_index()* is a helper function that is used to find the
+index of a traceeval_type _type_ by its given _name_. If the name is not found, it returns
+-1. This is handy to not have to remember which index belongs to which field.
+By creating variables to hold the indexes, the variables can be used instead.
+The *traceeval_type_index()* requires that _type_ is a static array. If a dynamic
+array is required, then *traceeval_type_index_size()* can be used where _nr_types_
+is the size of the _type_ array.
+
 RETURN VALUE
 ------------
 The *traceeval_init()* and *traceeval_init_size()* both return a descriptor
 to the traceeval or NULL on error.
 
+The *traceeval_type_index()* and *traceeval_type_index_size()* return the index
+of the given _name_ in _type_ or -1 if not found.
+
 EXAMPLE
 -------
 [source,c]
@@ -202,13 +216,29 @@ struct traceeval_type sched_keys[] = {
 	},
 };
 
+static ssize_t SCHED_COMM;
+static ssize_t SCHED_PID;
+static ssize_t SCHED_STATE;
+
+static void init_indexes(void)
+{
+	SCHED_COMM = traceeval_type_index("COMM", sched_keys);
+	SCHED_PID = traceeval_type_index("PID", sched_keys);
+	SCHED_STATE = traceeval_type_index("State", sched_keys);
+
+	if (SCHED_COMM < 0 || SCHED_PID < 0 || SCHED_STATE < 0) {
+		printf("Had a typo in assiging indexes\n");
+		exit(-1);
+	}
+}
+
 static int sched_callback(struct tep_event *event, struct tep_record *record,
 			  int cpu, void *data)
 {
 	static struct tep_format_field *pid_field;
 	static struct tep_format_field *comm_field;
 	static struct tep_format_field *state_field;
-	struct traceeval_data keys[3];
+	struct traceeval_data keys[TRACEEVAL_ARRAY_SIZE(sched_keys)];
 	struct traceeval *teval = data;
 	unsigned long long val;
 	char *comm;
@@ -232,9 +262,9 @@ static int sched_callback(struct tep_event *event, struct tep_record *record,
 
 	comm = record->data + comm_field->offset;
 
-	TRACEEVAL_SET_STRING(keys[0], comm);
-	TRACEEVAL_SET_NUMBER(keys[1], pid);
-	TRACEEVAL_SET_NUMBER(keys[2], state);
+	TRACEEVAL_SET_STRING(keys[SCHED_COMM], comm);
+	TRACEEVAL_SET_NUMBER(keys[SCHED_PID], pid);
+	TRACEEVAL_SET_NUMBER(keys[SCHED_STATE], state);
 
 	traceeval_insert(teval, keys, NULL);
 
@@ -243,7 +273,7 @@ static int sched_callback(struct tep_event *event, struct tep_record *record,
 
 static char *get_state(int state)
 {
-	switch (state & 7) {
+	switch (state & 0x1ff) {
 	case 0:
 		return "R";
 	case 1:
@@ -260,17 +290,17 @@ static void display_teval(struct traceeval *teval)
 	const struct traceeval_data *keys;
 
 	/* Sort comms first. */
-	traceeval_iterator_sort(iter, sched_keys[0].name, 0, true);
+	traceeval_iterator_sort(iter, sched_keys[SCHED_COMM].name, 0, true);
 	/* Sort pids next */
-	traceeval_iterator_sort(iter, sched_keys[1].name, 1, true);
+	traceeval_iterator_sort(iter, sched_keys[SCHED_PID].name, 1, true);
 	/* Sort state last */
-	traceeval_iterator_sort(iter, sched_keys[2].name, 2, true);
+	traceeval_iterator_sort(iter, sched_keys[SCHED_STATE].name, 2, true);
 
 	while (traceeval_iterator_next(iter, &keys) > 0) {
 		ssize_t hits = traceeval_hitcount_size(teval, keys, TRACEEVAL_ARRAY_SIZE(sched_keys));
 
 		printf("%s [%ld] %s: %zd\n",
-		       keys[0].string, keys[1].number, get_state(keys[2].number), hits);
+		       keys[SCHED_COMM].string, keys[SCHED_PID].number, get_state(keys[SCHED_STATE].number), hits);
 	}
 	traceeval_iterator_put(iter);
 }
@@ -289,6 +319,8 @@ int main (int argc, char **argv)
 	bool finished = false;
 	int ret;
 
+	init_indexes();
+
 	teval = traceeval_init(sched_keys, NULL);
 	if (!teval)
 		pdie("Creating traceeval");
Documentation/libtraceeval.txt +4 -0
index a8644b3b3c74..0c25ff7b1fa2 100644
@@ -17,6 +17,10 @@ Creating and releasing the traceeval resources:
 				       int _nr_keys_, int _nr_vals_);
 	void *traceeval_release*(struct traceeval pass:[*]_teval_);
 
+Helper functions for indexing the traceeval_type and traceveal_data elements:
+	ssize_t *traceeval_type_index*(const char pass:[*]_name_, struct traceeval_type pass:*]_type_);
+	ssize_t *traceeval_type_index_size*(const char pass:[*]_name_, struct traceeval_type pass:*]_type_, size_t nr_types);
+
 Inserting and removing elements from the traceeval:
 	int *traceeval_insert*(struct traceeval pass:[*]_teval_,
 			  const struct traceeval_data pass:[*]_keys_,
include/traceeval.h +15 -0
index 7de59a302445..a1079c3dba98 100644
@@ -11,6 +11,7 @@
 #include <stdlib.h>
 #include <stddef.h>
 #include <stdarg.h>
+#include <string.h>
 #include <stdbool.h>
 
 /* Data definition interfaces */
@@ -242,6 +243,20 @@ struct traceeval_type {
 	traceeval_data_hash_fn		hash;
 };
 
+#define traceeval_type_index(name, type)					\
+	traceeval_type_index_size(name, type, TRACEEVAL_ARRAY_SIZE(type))
+
+static inline ssize_t traceeval_type_index_size(const char *name,
+						struct traceeval_type *type,
+						size_t size)
+{
+	for (size_t i = 0; i < size; i++) {
+		if (strcmp(type[i].name, name) == 0)
+			return i;
+	}
+	return -1;
+}
+
 /* Statistics about a given entry element */
 struct traceeval_stat;
 
samples/task-eval.c +156 -85

479 lines not on this page · raw message

Signature · 1 line
2.45.2