[PATCH 15/48] perf tools: Add a test case for thread comm handling

From: Jiri Olsa
Date: Thu Sep 13 2018 - 08:55:31 EST


From: Namhyung Kim <namhyung@xxxxxxxxxx>

The new test case checks various thread comm handling APIs like
overridding and time sorting.

Cc: Frederic Weisbecker <fweisbec@xxxxxxxxx>
Link: http://lkml.kernel.org/n/tip-b31f0pktgl0k2dba3lqp159w@xxxxxxxxxxxxxx
Signed-off-by: Namhyung Kim <namhyung@xxxxxxxxxx>
Signed-off-by: Jiri Olsa <jolsa@xxxxxxxxxx>
---
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 4 +++
tools/perf/tests/tests.h | 1 +
tools/perf/tests/thread-comm.c | 48 +++++++++++++++++++++++++++++++++
4 files changed, 54 insertions(+)
create mode 100644 tools/perf/tests/thread-comm.c

diff --git a/tools/perf/tests/Build b/tools/perf/tests/Build
index 6c108fa79ae3..713fc29871e2 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -24,6 +24,7 @@ perf-y += bp_account.o
perf-y += task-exit.o
perf-y += sw-clock.o
perf-y += mmap-thread-lookup.o
+perf-y += thread-comm.o
perf-y += thread-mg-share.o
perf-y += switch-tracking.o
perf-y += keep-tracking.o
diff --git a/tools/perf/tests/builtin-test.c b/tools/perf/tests/builtin-test.c
index d7a5e1b9aa6f..982e5f64df62 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -279,6 +279,10 @@ static struct test generic_tests[] = {
.desc = "mem2node",
.func = test__mem2node,
},
+ {
+ .desc = "Test thread comm handling",
+ .func = test__thread_comm,
+ },
{
.func = NULL,
},
diff --git a/tools/perf/tests/tests.h b/tools/perf/tests/tests.h
index a9760e790563..5d16a56f262f 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -104,6 +104,7 @@ const char *test__clang_subtest_get_desc(int subtest);
int test__clang_subtest_get_nr(void);
int test__unit_number__scnprint(struct test *test, int subtest);
int test__mem2node(struct test *t, int subtest);
+int test__thread_comm(struct test *test, int subtest);

bool test__bp_signal_is_supported(void);

diff --git a/tools/perf/tests/thread-comm.c b/tools/perf/tests/thread-comm.c
new file mode 100644
index 000000000000..9fcfd2c43488
--- /dev/null
+++ b/tools/perf/tests/thread-comm.c
@@ -0,0 +1,48 @@
+#include <linux/compiler.h>
+#include "tests.h"
+#include "machine.h"
+#include "thread.h"
+#include "debug.h"
+
+int test__thread_comm(struct test *test __maybe_unused, int subtest __maybe_unused)
+{
+ struct machines machines;
+ struct machine *machine;
+ struct thread *t;
+
+ /*
+ * This test is to check whether it can retrieve a correct
+ * comm for a given time. When multi-file data storage is
+ * enabled, those task/comm events are processed first so the
+ * later sample should find a matching comm properly.
+ */
+ machines__init(&machines);
+ machine = &machines.host;
+
+ t = machine__findnew_thread(machine, 100, 100);
+ TEST_ASSERT_VAL("wrong init thread comm",
+ !strcmp(thread__comm_str(t), ":100"));
+
+ thread__set_comm(t, "perf-test1", 10000);
+ TEST_ASSERT_VAL("failed to override thread comm",
+ !strcmp(thread__comm_str(t), "perf-test1"));
+
+ thread__set_comm(t, "perf-test2", 20000);
+ thread__set_comm(t, "perf-test3", 30000);
+ thread__set_comm(t, "perf-test4", 40000);
+
+ TEST_ASSERT_VAL("failed to find timed comm",
+ !strcmp(thread__comm_str_by_time(t, 20000), "perf-test2"));
+ TEST_ASSERT_VAL("failed to find timed comm",
+ !strcmp(thread__comm_str_by_time(t, 35000), "perf-test3"));
+ TEST_ASSERT_VAL("failed to find timed comm",
+ !strcmp(thread__comm_str_by_time(t, 50000), "perf-test4"));
+
+ thread__set_comm(t, "perf-test1.5", 15000);
+ TEST_ASSERT_VAL("failed to sort timed comm",
+ !strcmp(thread__comm_str_by_time(t, 15000), "perf-test1.5"));
+
+ machine__delete_threads(machine);
+ machines__exit(&machines);
+ return 0;
+}
--
2.17.1