Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Ian Rogers and committed by
Arnaldo Carvalho de Melo
fa7f7e73 704e2f5b

+57 -46
+1
tools/perf/tests/Build
··· 54 54 perf-y += mem2node.o 55 55 perf-y += maps.o 56 56 perf-y += time-utils-test.o 57 + perf-y += genelf.o 57 58 58 59 $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build 59 60 $(call rule_mkdir)
+4
tools/perf/tests/builtin-test.c
··· 302 302 .func = test__time_utils, 303 303 }, 304 304 { 305 + .desc = "Test jit_write_elf", 306 + .func = test__jit_write_elf, 307 + }, 308 + { 305 309 .desc = "maps__merge_in", 306 310 .func = test__maps__merge_in, 307 311 },
+51
tools/perf/tests/genelf.c
··· 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 + }
+1
tools/perf/tests/tests.h
··· 110 110 int test__mem2node(struct test *t, int subtest); 111 111 int test__maps__merge_in(struct test *t, int subtest); 112 112 int test__time_utils(struct test *t, int subtest); 113 + int test__jit_write_elf(struct test *test, int subtest); 113 114 114 115 bool test__bp_signal_is_supported(void); 115 116 bool test__bp_account_is_supported(void);
-46
tools/perf/util/genelf.c
··· 8 8 */ 9 9 10 10 #include <sys/types.h> 11 - #include <stdio.h> 12 - #include <getopt.h> 13 11 #include <stddef.h> 14 12 #include <libelf.h> 15 13 #include <string.h> 16 14 #include <stdlib.h> 17 15 #include <unistd.h> 18 16 #include <inttypes.h> 19 - #include <limits.h> 20 17 #include <fcntl.h> 21 18 #include <err.h> 22 19 #ifdef HAVE_DWARF_SUPPORT ··· 27 30 #ifndef NT_GNU_BUILD_ID 28 31 #define NT_GNU_BUILD_ID 3 29 32 #endif 30 - 31 - #define JVMTI 32 33 33 34 #define BUILD_ID_URANDOM /* different uuid for each run */ 34 35 ··· 506 511 507 512 return retval; 508 513 } 509 - 510 - #ifndef JVMTI 511 - 512 - static unsigned char x86_code[] = { 513 - 0xBB, 0x2A, 0x00, 0x00, 0x00, /* movl $42, %ebx */ 514 - 0xB8, 0x01, 0x00, 0x00, 0x00, /* movl $1, %eax */ 515 - 0xCD, 0x80 /* int $0x80 */ 516 - }; 517 - 518 - static struct options options; 519 - 520 - int main(int argc, char **argv) 521 - { 522 - int c, fd, ret; 523 - 524 - while ((c = getopt(argc, argv, "o:h")) != -1) { 525 - switch (c) { 526 - case 'o': 527 - options.output = optarg; 528 - break; 529 - case 'h': 530 - printf("Usage: genelf -o output_file [-h]\n"); 531 - return 0; 532 - default: 533 - errx(1, "unknown option"); 534 - } 535 - } 536 - 537 - fd = open(options.output, O_CREAT|O_TRUNC|O_RDWR, 0666); 538 - if (fd == -1) 539 - err(1, "cannot create file %s", options.output); 540 - 541 - ret = jit_write_elf(fd, "main", x86_code, sizeof(x86_code)); 542 - close(fd); 543 - 544 - if (ret != 0) 545 - unlink(options.output); 546 - 547 - return ret; 548 - } 549 - #endif