Skip to content

Commit 8e5dc84

Browse files
mhiramatacmel
authored andcommitted
perf test: Add a test case for SDT event
Add a basic test case for SDT event support. This test scans an SDT event in perftools and check whether the SDT event is correctly stored into the buildid cache. Here is an example: ---- $ perf test sdt -v 47: Test SDT event probing : --- start --- test child forked, pid 20732 Found 72 SDTs in /home/mhiramat/ksrc/linux/tools/perf/perf Writing cache: %sdt_perf:test_target=test_target Cache committed: 0 symbol:test_target file:(null) line:0 offset:0 return:0 lazy:(null) test child finished with 0 ---- end ---- Test SDT event probing: Ok ---- Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Ananth N Mavinakayanahalli <ananth@linux.vnet.ibm.com> Cc: Brendan Gregg <brendan.d.gregg@gmail.com> Cc: Hemant Kumar <hemant@linux.vnet.ibm.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Link: http://lkml.kernel.org/r/146831796546.17065.1502584370844087537.stgit@devbox Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent e26e63b commit 8e5dc84

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎tools/perf/tests/Build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ perf-y += stat.o
3939
perf-y += event_update.o
4040
perf-y += event-times.o
4141
perf-y += backward-ring-buffer.o
42+
perf-y += sdt.o
4243

4344
$(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build
4445
$(call rule_mkdir)

‎tools/perf/tests/builtin-test.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ static struct test generic_tests[] = {
217217
.desc = "Test cpu map print",
218218
.func = test__cpu_map_print,
219219
},
220+
{
221+
.desc = "Test SDT event probing",
222+
.func = test__sdt_event,
223+
},
220224
{
221225
.func = NULL,
222226
},

‎tools/perf/tests/sdt.c‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#include <stdio.h>
2+
#include <sys/epoll.h>
3+
#include <util/util.h>
4+
#include <util/evlist.h>
5+
#include <linux/filter.h>
6+
#include "tests.h"
7+
#include "debug.h"
8+
#include "probe-file.h"
9+
#include "build-id.h"
10+
11+
/* To test SDT event, we need libelf support to scan elf binary */
12+
#if defined(HAVE_SDT_EVENT) && defined(HAVE_LIBELF_SUPPORT)
13+
14+
#include <sys/sdt.h>
15+
16+
static int target_function(void)
17+
{
18+
DTRACE_PROBE(perf, test_target);
19+
return TEST_OK;
20+
}
21+
22+
/* Copied from builtin-buildid-cache.c */
23+
static int build_id_cache__add_file(const char *filename)
24+
{
25+
char sbuild_id[SBUILD_ID_SIZE];
26+
u8 build_id[BUILD_ID_SIZE];
27+
int err;
28+
29+
err = filename__read_build_id(filename, &build_id, sizeof(build_id));
30+
if (err < 0) {
31+
pr_debug("Failed to read build id of %s\n", filename);
32+
return err;
33+
}
34+
35+
build_id__sprintf(build_id, sizeof(build_id), sbuild_id);
36+
err = build_id_cache__add_s(sbuild_id, filename, false, false);
37+
if (err < 0)
38+
pr_debug("Failed to add build id cache of %s\n", filename);
39+
return err;
40+
}
41+
42+
static char *get_self_path(void)
43+
{
44+
char *buf = calloc(PATH_MAX, sizeof(char));
45+
46+
if (buf && readlink("/proc/self/exe", buf, PATH_MAX) < 0) {
47+
pr_debug("Failed to get correct path of perf\n");
48+
free(buf);
49+
return NULL;
50+
}
51+
return buf;
52+
}
53+
54+
static int search_cached_probe(const char *target,
55+
const char *group, const char *event)
56+
{
57+
struct probe_cache *cache = probe_cache__new(target);
58+
int ret = 0;
59+
60+
if (!cache) {
61+
pr_debug("Failed to open probe cache of %s\n", target);
62+
return -EINVAL;
63+
}
64+
65+
if (!probe_cache__find_by_name(cache, group, event)) {
66+
pr_debug("Failed to find %s:%s in the cache\n", group, event);
67+
ret = -ENOENT;
68+
}
69+
probe_cache__delete(cache);
70+
71+
return ret;
72+
}
73+
74+
int test__sdt_event(int subtests __maybe_unused)
75+
{
76+
int ret = TEST_FAIL;
77+
char __tempdir[] = "./test-buildid-XXXXXX";
78+
char *tempdir = NULL, *myself = get_self_path();
79+
80+
if (myself == NULL || mkdtemp(__tempdir) == NULL) {
81+
pr_debug("Failed to make a tempdir for build-id cache\n");
82+
goto error;
83+
}
84+
/* Note that buildid_dir must be an absolute path */
85+
tempdir = realpath(__tempdir, NULL);
86+
87+
/* At first, scan itself */
88+
set_buildid_dir(tempdir);
89+
if (build_id_cache__add_file(myself) < 0)
90+
goto error_rmdir;
91+
92+
/* Open a cache and make sure the SDT is stored */
93+
if (search_cached_probe(myself, "sdt_perf", "test_target") < 0)
94+
goto error_rmdir;
95+
96+
/* TBD: probing on the SDT event and collect logs */
97+
98+
/* Call the target and get an event */
99+
ret = target_function();
100+
101+
error_rmdir:
102+
/* Cleanup temporary buildid dir */
103+
rm_rf(tempdir);
104+
error:
105+
free(tempdir);
106+
free(myself);
107+
return ret;
108+
}
109+
#else
110+
int test__sdt_event(int subtests __maybe_unused)
111+
{
112+
pr_debug("Skip SDT event test because SDT support is not compiled\n");
113+
return TEST_SKIP;
114+
}
115+
#endif

‎tools/perf/tests/tests.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ int test__event_update(int subtest);
8888
int test__event_times(int subtest);
8989
int test__backward_ring_buffer(int subtest);
9090
int test__cpu_map_print(int subtest);
91+
int test__sdt_event(int subtest);
9192

9293
#if defined(__arm__) || defined(__aarch64__)
9394
#ifdef HAVE_DWARF_UNWIND_SUPPORT

0 commit comments

Comments
 (0)