diff mbox series

libtracefs: Add unit tests for the follow functions

Message ID 20221115000256.5a0c89d8@gandalf.local.home (mailing list archive)
State Accepted
Commit 2221ce0ec1284bdc99476af547f8460a645fd120
Headers show
Series libtracefs: Add unit tests for the follow functions | expand

Commit Message

Steven Rostedt Nov. 15, 2022, 5:02 a.m. UTC
From: "Steven Rostedt (Google)" <rostedt@goodmis.org>

Add unit tests for tracefs_follow_events() and
tracefs_follow_missed_events().

Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
---
 utest/tracefs-utest.c | 136 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 136 insertions(+)
diff mbox series

Patch

diff --git a/utest/tracefs-utest.c b/utest/tracefs-utest.c
index 2c9b267ea427..805cc3b9f975 100644
--- a/utest/tracefs-utest.c
+++ b/utest/tracefs-utest.c
@@ -605,6 +605,141 @@  static void test_trace_cpu_read(void)
 	test_instance_trace_cpu_read(test_instance);
 }
 
+struct follow_data {
+	struct tep_event *sched_switch;
+	struct tep_event *sched_waking;
+	struct tep_event *function;
+	int missed;
+};
+
+static int switch_callback(struct tep_event *event, struct tep_record *record,
+			   int cpu, void *data)
+{
+	struct follow_data *fdata = data;
+
+	CU_TEST(cpu == record->cpu);
+	CU_TEST(event->id == fdata->sched_switch->id);
+	return 0;
+}
+
+static int waking_callback(struct tep_event *event, struct tep_record *record,
+			   int cpu, void *data)
+{
+	struct follow_data *fdata = data;
+
+	CU_TEST(cpu == record->cpu);
+	CU_TEST(event->id == fdata->sched_waking->id);
+	return 0;
+}
+
+static int function_callback(struct tep_event *event, struct tep_record *record,
+			     int cpu, void *data)
+{
+	struct follow_data *fdata = data;
+
+	CU_TEST(cpu == record->cpu);
+	CU_TEST(event->id == fdata->function->id);
+	return 0;
+}
+
+static int missed_callback(struct tep_event *event, struct tep_record *record,
+			     int cpu, void *data)
+{
+	struct follow_data *fdata = data;
+
+	fdata->missed = record->missed_events;
+	return 0;
+}
+
+static int all_callback(struct tep_event *event, struct tep_record *record,
+			int cpu, void *data)
+{
+	struct follow_data *fdata = data;
+
+	CU_TEST(fdata->missed == record->missed_events);
+	fdata->missed = 0;
+	return 0;
+}
+
+static void *stop_thread(void *arg)
+{
+	struct tracefs_instance *instance = arg;
+
+	sleep(1);
+	tracefs_iterate_stop(instance);
+	return NULL;
+}
+
+static void test_instance_follow_events(struct tracefs_instance *instance)
+{
+	struct follow_data fdata;
+	struct tep_handle *tep;
+	pthread_t thread;
+	int ret;
+
+	memset(&fdata, 0, sizeof(fdata));
+
+	tep = tracefs_local_events(NULL);
+	CU_TEST(tep != NULL);
+	if (!tep)
+		return;
+
+	fdata.sched_switch = tep_find_event_by_name(tep, "sched", "sched_switch");
+	CU_TEST(fdata.sched_switch != NULL);
+	if (!fdata.sched_switch)
+		return;
+
+	fdata.sched_waking = tep_find_event_by_name(tep, "sched", "sched_waking");
+	CU_TEST(fdata.sched_waking != NULL);
+	if (!fdata.sched_waking)
+		return;
+
+	fdata.function = tep_find_event_by_name(tep, "ftrace", "function");
+	CU_TEST(fdata.function != NULL);
+	if (!fdata.function)
+		return;
+
+	ret = tracefs_follow_event(tep, instance, "sched", "sched_switch",
+				   switch_callback, &fdata);
+	CU_TEST(ret == 0);
+
+	ret = tracefs_follow_event(tep, instance, "sched", "sched_waking",
+				   waking_callback, &fdata);
+	CU_TEST(ret == 0);
+
+	ret = tracefs_follow_event(tep, instance, "ftrace", "function",
+				   function_callback, &fdata);
+	CU_TEST(ret == 0);
+
+	ret = tracefs_follow_missed_events(instance, missed_callback, &fdata);
+	CU_TEST(ret == 0);
+
+	ret = tracefs_event_enable(instance, "sched", "sched_switch");
+	CU_TEST(ret == 0);
+
+	ret = tracefs_event_enable(instance, "sched", "sched_waking");
+	CU_TEST(ret == 0);
+
+	ret = tracefs_tracer_set(instance, TRACEFS_TRACER_FUNCTION);
+	CU_TEST(ret == 0);
+
+	pthread_create(&thread, NULL, stop_thread, instance);
+
+	ret = tracefs_iterate_raw_events(tep, instance, NULL, 0, all_callback, &fdata);
+	CU_TEST(ret == 0);
+
+	pthread_join(thread, NULL);
+
+	tracefs_tracer_clear(instance);
+	tracefs_event_disable(instance, NULL, NULL); 
+}
+
+static void test_follow_events(void)
+{
+	test_instance_follow_events(NULL);
+	test_instance_follow_events(test_instance);
+}
+
 static int read_trace_cpu_file(struct test_cpu_data *data)
 {
 	unsigned long long ts;
@@ -2113,6 +2248,7 @@  void test_tracefs_lib(void)
 		fprintf(stderr, "Suite \"%s\" cannot be ceated\n", TRACEFS_SUITE);
 		return;
 	}
+	CU_add_test(suite, "Follow events", test_follow_events);
 	CU_add_test(suite, "trace cpu read",
 		    test_trace_cpu_read);
 	CU_add_test(suite, "trace cpu pipe",