Skip to content

Commit fa7f7e7

Browse files
captain5050acmel
authored andcommitted
perf jit: Move test functionality in to a test
Adds a test for minimal jit_write_elf functionality. Committer testing: # perf test jit 61: Test jit_write_elf : Ok # # perf test -v jit 61: Test jit_write_elf : --- start --- test child forked, pid 10460 Writing jit code to: /tmp/perf-test-KqxURR test child finished with 0 ---- end ---- Test jit_write_elf: Ok # Committer notes: Fix up the case where HAVE_JITDUMP is no defined. Signed-off-by: Ian Rogers <irogers@google.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com> Cc: Alexios Zavras <alexios.zavras@intel.com> Cc: Allison Randal <allison@lohutok.net> Cc: Florian Fainelli <f.fainelli@gmail.com> Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Kate Stewart <kstewart@linuxfoundation.org> Cc: Leo Yan <leo.yan@linaro.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Michael Petlan <mpetlan@redhat.com> Cc: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Song Liu <songliubraving@fb.com> Cc: Stephane Eranian <eranian@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: http://lore.kernel.org/lkml/20191126235913.41855-1-irogers@google.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 704e2f5 commit fa7f7e7

5 files changed

Lines changed: 57 additions & 46 deletions

File tree

‎tools/perf/tests/Build‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ perf-y += unit_number__scnprintf.o
5454
perf-y += mem2node.o
5555
perf-y += maps.o
5656
perf-y += time-utils-test.o
57+
perf-y += genelf.o
5758

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ static struct test generic_tests[] = {
301301
.desc = "time utils",
302302
.func = test__time_utils,
303303
},
304+
{
305+
.desc = "Test jit_write_elf",
306+
.func = test__jit_write_elf,
307+
},
304308
{
305309
.desc = "maps__merge_in",
306310
.func = test__maps__merge_in,

‎tools/perf/tests/genelf.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
#include <limits.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <string.h>
6+
#include <unistd.h>
7+
#include <linux/compiler.h>
8+
9+
#include "debug.h"
10+
#include "tests.h"
11+
12+
#ifdef HAVE_JITDUMP
13+
#include <libelf.h>
14+
#include "../util/genelf.h"
15+
#endif
16+
17+
#define TEMPL "/tmp/perf-test-XXXXXX"
18+
19+
int test__jit_write_elf(struct test *test __maybe_unused,
20+
int subtest __maybe_unused)
21+
{
22+
#ifdef HAVE_JITDUMP
23+
static unsigned char x86_code[] = {
24+
0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
25+
0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
26+
0xCD, 0x80 /* int $0x80 */
27+
};
28+
char path[PATH_MAX];
29+
int fd, ret;
30+
31+
strcpy(path, TEMPL);
32+
33+
fd = mkstemp(path);
34+
if (fd < 0) {
35+
perror("mkstemp failed");
36+
return TEST_FAIL;
37+
}
38+
39+
pr_info("Writing jit code to: %s\n", path);
40+
41+
ret = jit_write_elf(fd, 0, "main", x86_code, sizeof(x86_code),
42+
NULL, 0, NULL, 0, 0);
43+
close(fd);
44+
45+
unlink(path);
46+
47+
return ret ? TEST_FAIL : 0;
48+
#else
49+
return TEST_SKIP;
50+
#endif
51+
}

‎tools/perf/tests/tests.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ int test__unit_number__scnprint(struct test *test, int subtest);
110110
int test__mem2node(struct test *t, int subtest);
111111
int test__maps__merge_in(struct test *t, int subtest);
112112
int test__time_utils(struct test *t, int subtest);
113+
int test__jit_write_elf(struct test *test, int subtest);
113114

114115
bool test__bp_signal_is_supported(void);
115116
bool test__bp_account_is_supported(void);

‎tools/perf/util/genelf.c‎

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@
88
*/
99

1010
#include <sys/types.h>
11-
#include <stdio.h>
12-
#include <getopt.h>
1311
#include <stddef.h>
1412
#include <libelf.h>
1513
#include <string.h>
1614
#include <stdlib.h>
1715
#include <unistd.h>
1816
#include <inttypes.h>
19-
#include <limits.h>
2017
#include <fcntl.h>
2118
#include <err.h>
2219
#ifdef HAVE_DWARF_SUPPORT
@@ -31,8 +28,6 @@
3128
#define NT_GNU_BUILD_ID 3
3229
#endif
3330

34-
#define JVMTI
35-
3631
#define BUILD_ID_URANDOM /* different uuid for each run */
3732

3833
#ifdef HAVE_LIBCRYPTO
@@ -511,44 +506,3 @@ jit_write_elf(int fd, uint64_t load_addr, const char *sym,
511506

512507
return retval;
513508
}
514-
515-
#ifndef JVMTI
516-
517-
static unsigned char x86_code[] = {
518-
0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */
519-
0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */
520-
0xCD, 0x80 /* int $0x80 */
521-
};
522-
523-
static struct options options;
524-
525-
int main(int argc, char **argv)
526-
{
527-
int c, fd, ret;
528-
529-
while ((c = getopt(argc, argv, "o:h")) != -1) {
530-
switch (c) {
531-
case 'o':
532-
options.output = optarg;
533-
break;
534-
case 'h':
535-
printf("Usage: genelf -o output_file [-h]\n");
536-
return 0;
537-
default:
538-
errx(1, "unknown option");
539-
}
540-
}
541-
542-
fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666);
543-
if (fd == -1)
544-
err(1, "cannot create file %s", options.output);
545-
546-
ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code));
547-
close(fd);
548-
549-
if (ret != 0)
550-
unlink(options.output);
551-
552-
return ret;
553-
}
554-
#endif

0 commit comments

Comments
 (0)