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

From: Jiri Olsa
Date: Tue Jan 09 2018 - 10:36:07 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 87bf3edb037c..ca961bed1bb1 100644
--- a/tools/perf/tests/Build
+++ b/tools/perf/tests/Build
@@ -23,6 +23,7 @@ perf-y += bp_signal_overflow.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 fafa014240cd..6c715e030dfd 100644
--- a/tools/perf/tests/builtin-test.c
+++ b/tools/perf/tests/builtin-test.c
@@ -271,6 +271,10 @@ static struct test generic_tests[] = {
.func = test__unit_number__scnprint,
},
{
+ .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 2862b80bc288..d7880e5207ec 100644
--- a/tools/perf/tests/tests.h
+++ b/tools/perf/tests/tests.h
@@ -102,6 +102,7 @@ int test__clang(struct test *test, int subtest);
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__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.13.6