Skip to contentSkip to conversation

Integrating qemu to Linux Perf

PATCH Superseded vandersonmr 4 participants 10 messages Aug 15, 2019 – Aug 22, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Integrating qemu to Linux Perf

This patch is part of Google Summer of Code (GSoC) 2019. More about the project can be found in:
https://wiki.qemu.org/Internships/ProjectIdeas/TCGCodeQuality

This adds --perf command-line option to dump Linux Perf jitdump files. These files are used to enhant Perf report and to be able to analyze and dump JITed code with perf.

Example of use:
perf record -k 1 qemu-x86_64 -perf ./a.out
perf inject -j -i perf.data -o perf.data.jitted
perf report -i perf.data.jitted

vandersonmr (2):
accel/tcg: adding integration with linux perf
tb-stats: adding TBStatistics info into perf dump

 accel/tcg/Makefile.objs      |   1 +
 accel/tcg/perf/Makefile.objs |   1 +
 accel/tcg/perf/jitdump.c     | 193 +++++++++++++++++++++++++++++++++++
 accel/tcg/perf/jitdump.h     |  19 ++++
 accel/tcg/translate-all.c    |  12 +++
 include/qemu-common.h        |   3 +
 linux-user/main.c            |   7 ++
 qemu-options.hx              |  12 +++
 8 files changed, 248 insertions(+)
 create mode 100644 accel/tcg/perf/Makefile.objs
 create mode 100644 accel/tcg/perf/jitdump.c
 create mode 100644 accel/tcg/perf/jitdump.h
Signature · 1 line
2.22.0
#1 lore raw

PATCH 1/2 accel/tcg: adding integration with linux perf

This commit adds support to Linux Perf in order
to be able to analyze qemu jitted code and
also to able to see the TBs PC in it.

---
 accel/tcg/Makefile.objs      |   1 +
 accel/tcg/perf/Makefile.objs |   1 +
 accel/tcg/perf/jitdump.c     | 180 +++++++++++++++++++++++++++++++++++
 accel/tcg/perf/jitdump.h     |  19 ++++
 accel/tcg/translate-all.c    |  12 +++
 include/qemu-common.h        |   3 +
 linux-user/main.c            |   7 ++
 qemu-options.hx              |  12 +++
 8 files changed, 235 insertions(+)
 create mode 100644 accel/tcg/perf/Makefile.objs
 create mode 100644 accel/tcg/perf/jitdump.c
 create mode 100644 accel/tcg/perf/jitdump.h
accel/tcg/Makefile.objs +1 -0
index d381a02f34..f393a7438f 100644
@@ -3,6 +3,7 @@ obj-$(CONFIG_SOFTMMU) += cputlb.o
 obj-y += tcg-runtime.o tcg-runtime-gvec.o
 obj-y += cpu-exec.o cpu-exec-common.o translate-all.o
 obj-y += translator.o
+obj-y += perf/
 
 obj-$(CONFIG_USER_ONLY) += user-exec.o
 obj-$(call lnot,$(CONFIG_SOFTMMU)) += user-exec-stub.o
accel/tcg/perf/Makefile.objs new file +1 -0
new file mode 100644
index 0000000000..f82fba35e5
@@ -0,0 +1 @@
+obj-y += jitdump.o
accel/tcg/perf/jitdump.c new file +180 -0

180 lines not on this page · raw message

accel/tcg/perf/jitdump.h new file +19 -0
new file mode 100644
index 0000000000..12c0991b04
@@ -0,0 +1,19 @@
+#ifdef __linux__
+#ifndef JITDUMP_H
+#define JITDUMP_H
+
+#include "qemu/osdep.h"
+
+#include "disas/disas.h"
+#include "exec/exec-all.h"
+
+void start_jitdump_file(void);
+
+void append_load_in_jitdump_file(TranslationBlock *tb);
+void append_move_in_jitdump_file(TranslationBlock *tb);
+
+void close_jitdump_file(void);
+
+#endif
+
+#endif
accel/tcg/translate-all.c +12 -0
index 5d1e08b169..0a4211d3d2 100644
@@ -58,6 +58,8 @@
 #include "sysemu/cpus.h"
 #include "sysemu/tcg.h"
 
+#include "perf/jitdump.h"
+
 /* #define DEBUG_TB_INVALIDATE */
 /* #define DEBUG_TB_FLUSH */
 /* make various TB consistency checks */
@@ -1148,6 +1150,11 @@ void tcg_exec_init(unsigned long tb_size)
     cpu_gen_init();
     page_init();
     tb_htable_init();
+#ifdef __linux__
+    if (jitdump_enabled()) {
+        start_jitdump_file();
+    }
+#endif
     code_gen_alloc(tb_size);
 #if defined(CONFIG_SOFTMMU)
     /* There's no guest base to take into account, so go ahead and
@@ -1877,6 +1884,11 @@ TranslationBlock *tb_gen_code(CPUState *cpu,
         return existing_tb;
     }
     tcg_tb_insert(tb);
+#ifdef __linux__
+    if (jitdump_enabled()) {
+        append_load_in_jitdump_file(tb);
+    }
+#endif
     return tb;
 }
 
include/qemu-common.h +3 -0
index 0235cd3b91..bd564a9e5c 100644
@@ -130,4 +130,7 @@ void page_size_init(void);
  * returned. */
 bool dump_in_progress(void);
 
+void jitdump_enable(void);
+bool jitdump_enabled(void);
+
 #endif
linux-user/main.c +7 -0
index 8ffc525195..6521cf0c68 100644
@@ -378,6 +378,11 @@ static void handle_arg_strace(const char *arg)
     do_strace = 1;
 }
 
+static void handle_arg_perf(const char *arg)
+{
+    jitdump_enable();
+}
+
 static void handle_arg_version(const char *arg)
 {
     printf("qemu-" TARGET_NAME " version " QEMU_FULL_VERSION
@@ -443,6 +448,8 @@ static const struct qemu_argument arg_table[] = {
      "",           "Seed for pseudo-random number generator"},
     {"trace",      "QEMU_TRACE",       true,  handle_arg_trace,
      "",           "[[enable=]<pattern>][,events=<file>][,file=<file>]"},
+    {"perf",      "QEMU_PERF",         false, handle_arg_perf,
+     "",           "dump jitdump files to help linux perf JIT code visualization"},
     {"version",    "QEMU_VERSION",     false, handle_arg_version,
      "",           "display version information and exit"},
     {NULL, NULL, false, NULL, NULL, NULL}
qemu-options.hx +12 -0
index 9621e934c0..1c26eeeb9c 100644
@@ -4147,6 +4147,18 @@ STEXI
 Enable FIPS 140-2 compliance mode.
 ETEXI
 
+#ifdef __linux__
+DEF("perf", 0, QEMU_OPTION_perf,
+    "-perf    dump jitdump files to help linux perf JIT code visualization\n",
+    QEMU_ARCH_ALL)
+#endif
+STEXI
+@item -perf
+@findex -perf
+Dumps jitdump files to help linux perf JIT code visualization
+ETEXI
+
+
 HXCOMM Deprecated by -machine accel=tcg property
 DEF("no-kvm", 0, QEMU_OPTION_no_kvm, "", QEMU_ARCH_I386)
 
Signature · 1 line
2.22.0
#2 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] accel/tcg: adding integration with linux perf

Quoted 3 lines · vandersonmr

This commit adds support to Linux Perf in order
to be able to analyze qemu jitted code and
also to able to see the TBs PC in it.

Is there any reference to the file format? Please include it in a code comment, if such a thing exists.

accel/tcg/perf/jitdump.c

Quoted 6 lines
accel/tcg/perf/jitdump.c new file +0 -0
new file mode 100644
index 0000000000..6f4c0911c2
@@ -0,0 +1,180 @@

License header?

accel/tcg/perf/jitdump.c line 1

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+#ifdef __linux__

If the entire source file is #ifdef __linux__ then Makefile.objs should probably contain obj-$(CONFIG_LINUX) += jitdump.o instead. Letting the build system decide what to compile is cleaner than ifdeffing large amounts of code.

accel/tcg/perf/jitdump.c line 12

Quoted 11 lines
accel/tcg/perf/jitdump.c new file +11 -0
+
+#include <stdint.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <elf.h>
+
+#include "jitdump.h"
+#include "qemu-common.h"

Please follow QEMU's header ordering conventions. See ./HACKING "1.2. Include directives".

accel/tcg/perf/jitdump.c line 105

Quoted 4 lines
accel/tcg/perf/jitdump.c new file +4 -0
+void start_jitdump_file(void)
+{
+    GString *dumpfile_name = g_string_new(NULL);;
+    g_string_printf(dumpfile_name, "./jit-%d.dump", getpid());

Simpler:

  gchar *dumpfile_name = g_strdup_printf("./jit-%d.dump", getpid());
  ...
  g_free(dumpfile_name);

accel/tcg/perf/jitdump.c line 106

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+    dumpfile = fopen(dumpfile_name->str, "w+");

getpid() and the global dumpfile variable make me wonder what happens with multi-threaded TCG?

accel/tcg/perf/jitdump.c line 19

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    perf_marker = mmap(NULL, sysconf(_SC_PAGESIZE),

Please mention the point of this mmap in a comment. My best guess is that perf stores the /proc/$PID/maps and this is how it finds the jitdump file?

accel/tcg/perf/jitdump.c line 121

Quoted 13 lines
accel/tcg/perf/jitdump.c new file +13 -0
+                          PROT_READ | PROT_EXEC,
+                          MAP_PRIVATE,
+                          fileno(dumpfile), 0);
+
+    if (perf_marker == MAP_FAILED) {
+        printf("Failed to create mmap marker file for perf %d\n", fileno(dumpfile));
+        fclose(dumpfile);
+        return;
+    }
+
+    g_string_free(dumpfile_name, TRUE);
+
+    struct jitheader *header = g_new0(struct jitheader, 1);

Why g_new this struct? It's small and can be declared on the stack.

Please use g_free() with g_malloc/new/etc(). It's not safe to mismatch glib and libc memory allocation functions.

Quoted 7 lines
accel/tcg/perf/jitdump.c new file +7 -0
+    header->magic = 0x4A695444;
+    header->version = 1;
+    header->elf_mach = get_e_machine();
+    header->total_size = sizeof(struct jitheader);
+    header->pid = getpid();
+    header->timestamp = get_timestamp();
+

accel/tcg/perf/jitdump.c line 138

Quoted 10 lines
accel/tcg/perf/jitdump.c new file +10 -0
+    fwrite(header, header->total_size, 1, dumpfile);
+
+    free(header);
+    fflush(dumpfile);
+}
+
+void append_load_in_jitdump_file(TranslationBlock *tb)
+{
+    GString *func_name = g_string_new(NULL);
+    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');

The explicit NUL character looks strange to me. I think the idea is to avoid func_name->len + 1? Adding NUL characters to C strings can be a source of bugs, I would stick to convention and do len + 1 instead of putting NUL characters into the GString. This is a question of style though.

accel/tcg/perf/jitdump.c line 51

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    struct jr_code_load *load_event = g_new0(struct jr_code_load, 1);

No need to allocate load_event on the heap.

Quoted 7 lines
qemu-options.hx +0 -0
index 9621e934c0..1c26eeeb9c 100644
@@ -4147,6 +4147,18 @@ STEXI
 Enable FIPS 140-2 compliance mode.
 ETEXI

qemu-options.hx STEXI · line 4158

Quoted 10 lines
qemu-options.hx +9 -0
 
+#ifdef __linux__
+DEF("perf", 0, QEMU_OPTION_perf,
+    "-perf    dump jitdump files to help linux perf JIT code visualization\n",
+    QEMU_ARCH_ALL)
+#endif
+STEXI
+@item -perf
+@findex -perf
+Dumps jitdump files to help linux perf JIT code visualization

Suggestions on expanding the documentation:

Where are the jitdump files dumped? The current working directory?

Anything to say about the naming scheme for these files?

Can you include an example of how to load them into perf(1)?

#3 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] accel/tcg: adding integration with linux perf

Quoted 96 lines · Stefan Hajnoczi
Quoted 3 lines · vandersonmr

This commit adds support to Linux Perf in order
to be able to analyze qemu jitted code and
also to able to see the TBs PC in it.

Is there any reference to the file format? Please include it in a code comment, if such a thing exists.

accel/tcg/perf/jitdump.c

Quoted 6 lines
accel/tcg/perf/jitdump.c new file +0 -0
new file mode 100644
index 0000000000..6f4c0911c2
@@ -0,0 +1,180 @@

License header?

accel/tcg/perf/jitdump.c line 1

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+#ifdef __linux__

If the entire source file is #ifdef __linux__ then Makefile.objs should probably contain obj-$(CONFIG_LINUX) += jitdump.o instead. Letting the build system decide what to compile is cleaner than ifdeffing large amounts of code.

accel/tcg/perf/jitdump.c line 12

Quoted 11 lines
accel/tcg/perf/jitdump.c new file +11 -0
+
+#include <stdint.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <elf.h>
+
+#include "jitdump.h"
+#include "qemu-common.h"

Please follow QEMU's header ordering conventions. See ./HACKING "1.2. Include directives".

accel/tcg/perf/jitdump.c line 105

Quoted 4 lines
accel/tcg/perf/jitdump.c new file +4 -0
+void start_jitdump_file(void)
+{
+    GString *dumpfile_name = g_string_new(NULL);;
+    g_string_printf(dumpfile_name, "./jit-%d.dump", getpid());

Simpler:

  gchar *dumpfile_name = g_strdup_printf("./jit-%d.dump", getpid());
  ...
  g_free(dumpfile_name);

accel/tcg/perf/jitdump.c line 106

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+    dumpfile = fopen(dumpfile_name->str, "w+");

getpid() and the global dumpfile variable make me wonder what happens with multi-threaded TCG?

accel/tcg/perf/jitdump.c line 19

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    perf_marker = mmap(NULL, sysconf(_SC_PAGESIZE),

Please mention the point of this mmap in a comment. My best guess is that perf stores the /proc/$PID/maps and this is how it finds the jitdump file?

accel/tcg/perf/jitdump.c line 121

Quoted 13 lines
accel/tcg/perf/jitdump.c new file +13 -0
+                          PROT_READ | PROT_EXEC,
+                          MAP_PRIVATE,
+                          fileno(dumpfile), 0);
+
+    if (perf_marker == MAP_FAILED) {
+        printf("Failed to create mmap marker file for perf %d\n", fileno(dumpfile));
+        fclose(dumpfile);
+        return;
+    }
+
+    g_string_free(dumpfile_name, TRUE);
+
+    struct jitheader *header = g_new0(struct jitheader, 1);

Why g_new this struct? It's small and can be declared on the stack.

Please use g_free() with g_malloc/new/etc(). It's not safe to mismatch glib and libc memory allocation functions.

Quoted 15 lines
accel/tcg/perf/jitdump.c new file +15 -0
+    header->magic = 0x4A695444;
+    header->version = 1;
+    header->elf_mach = get_e_machine();
+    header->total_size = sizeof(struct jitheader);
+    header->pid = getpid();
+    header->timestamp = get_timestamp();
+
+    fwrite(header, header->total_size, 1, dumpfile);
+
+    free(header);
+    fflush(dumpfile);
+}
+
+void append_load_in_jitdump_file(TranslationBlock *tb)
+{
Quoted 8 lines

accel/tcg/perf/jitdump.c line 138

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+    GString *func_name = g_string_new(NULL);
+    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');

The explicit NUL character looks strange to me. I think the idea is to avoid func_name->len + 1? Adding NUL characters to C strings can be a source of bugs, I would stick to convention and do len + 1 instead of putting NUL characters into the GString. This is a question of style though.

The glib functions always add null characters so you shouldn't need to manually.

Quoted 26 lines

accel/tcg/perf/jitdump.c line 51

Quoted 3 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    struct jr_code_load *load_event = g_new0(struct jr_code_load, 1);

No need to allocate load_event on the heap.

Quoted 7 lines
qemu-options.hx +0 -0
index 9621e934c0..1c26eeeb9c 100644
@@ -4147,6 +4147,18 @@ STEXI
 Enable FIPS 140-2 compliance mode.
 ETEXI

qemu-options.hx STEXI · line 4158

Quoted 10 lines
qemu-options.hx +9 -0
 
+#ifdef __linux__
+DEF("perf", 0, QEMU_OPTION_perf,
+    "-perf    dump jitdump files to help linux perf JIT code visualization\n",
+    QEMU_ARCH_ALL)
+#endif
+STEXI
+@item -perf
+@findex -perf
+Dumps jitdump files to help linux perf JIT code visualization

Suggestions on expanding the documentation:

Quoted 5 lines

Where are the jitdump files dumped? The current working directory?

Anything to say about the naming scheme for these files?

Can you include an example of how to load them into perf(1)?

--
Alex Bennée

#4 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] accel/tcg: adding integration with linux perf

Quoted 108 lines · Alex Bennée
Quoted 96 lines · Stefan Hajnoczi
Quoted 3 lines · vandersonmr

This commit adds support to Linux Perf in order
to be able to analyze qemu jitted code and
also to able to see the TBs PC in it.

Is there any reference to the file format? Please include it in a code comment, if such a thing exists.

accel/tcg/perf/jitdump.c

Quoted 6 lines
accel/tcg/perf/jitdump.c new file +0 -0
new file mode 100644
index 0000000000..6f4c0911c2
@@ -0,0 +1,180 @@

License header?

accel/tcg/perf/jitdump.c line 1

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+#ifdef __linux__

If the entire source file is #ifdef __linux__ then Makefile.objs should probably contain obj-$(CONFIG_LINUX) += jitdump.o instead. Letting the build system decide what to compile is cleaner than ifdeffing large amounts of code.

accel/tcg/perf/jitdump.c line 12

Quoted 11 lines
accel/tcg/perf/jitdump.c new file +11 -0
+
+#include <stdint.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <elf.h>
+
+#include "jitdump.h"
+#include "qemu-common.h"

Please follow QEMU's header ordering conventions. See ./HACKING "1.2. Include directives".

accel/tcg/perf/jitdump.c line 105

Quoted 4 lines
accel/tcg/perf/jitdump.c new file +4 -0
+void start_jitdump_file(void)
+{
+    GString *dumpfile_name = g_string_new(NULL);;
+    g_string_printf(dumpfile_name, "./jit-%d.dump", getpid());

Simpler:

  gchar *dumpfile_name = g_strdup_printf("./jit-%d.dump", getpid());
  ...
  g_free(dumpfile_name);

accel/tcg/perf/jitdump.c line 106

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+    dumpfile = fopen(dumpfile_name->str, "w+");

getpid() and the global dumpfile variable make me wonder what happens with multi-threaded TCG?

accel/tcg/perf/jitdump.c line 19

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    perf_marker = mmap(NULL, sysconf(_SC_PAGESIZE),

Please mention the point of this mmap in a comment. My best guess is that perf stores the /proc/$PID/maps and this is how it finds the jitdump file?

accel/tcg/perf/jitdump.c line 121

Quoted 13 lines
accel/tcg/perf/jitdump.c new file +13 -0
+                          PROT_READ | PROT_EXEC,
+                          MAP_PRIVATE,
+                          fileno(dumpfile), 0);
+
+    if (perf_marker == MAP_FAILED) {
+        printf("Failed to create mmap marker file for perf %d\n", fileno(dumpfile));
+        fclose(dumpfile);
+        return;
+    }
+
+    g_string_free(dumpfile_name, TRUE);
+
+    struct jitheader *header = g_new0(struct jitheader, 1);

Why g_new this struct? It's small and can be declared on the stack.

Please use g_free() with g_malloc/new/etc(). It's not safe to mismatch glib and libc memory allocation functions.

Quoted 15 lines
accel/tcg/perf/jitdump.c new file +15 -0
+    header->magic = 0x4A695444;
+    header->version = 1;
+    header->elf_mach = get_e_machine();
+    header->total_size = sizeof(struct jitheader);
+    header->pid = getpid();
+    header->timestamp = get_timestamp();
+
+    fwrite(header, header->total_size, 1, dumpfile);
+
+    free(header);
+    fflush(dumpfile);
+}
+
+void append_load_in_jitdump_file(TranslationBlock *tb)
+{
Quoted 8 lines

accel/tcg/perf/jitdump.c line 138

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+    GString *func_name = g_string_new(NULL);
+    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');

The explicit NUL character looks strange to me. I think the idea is to avoid func_name->len + 1? Adding NUL characters to C strings can be a source of bugs, I would stick to convention and do len + 1 instead of putting NUL characters into the GString. This is a question of style though.

Quoted 2 lines

The glib functions always add null characters so you shouldn't need to manually.

Yep, just remember to do func_name->len + 1 since glib doesn't count the NUL character that gets added automatically.

#5 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] accel/tcg: adding integration with linux perf

In reply to Stefan Hajnoczi
Quoted 46 lines · Stefan Hajnoczi
Quoted 3 lines · vandersonmr

This commit adds support to Linux Perf in order
to be able to analyze qemu jitted code and
also to able to see the TBs PC in it.

Is there any reference to the file format? Please include it in a code comment, if such a thing exists.

accel/tcg/perf/jitdump.c

Quoted 6 lines
accel/tcg/perf/jitdump.c new file +0 -0
new file mode 100644
index 0000000000..6f4c0911c2
@@ -0,0 +1,180 @@

License header?

accel/tcg/perf/jitdump.c line 1

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+#ifdef __linux__

If the entire source file is #ifdef __linux__ then Makefile.objs should probably contain obj-$(CONFIG_LINUX) += jitdump.o instead. Letting the build system decide what to compile is cleaner than ifdeffing large amounts of code.

accel/tcg/perf/jitdump.c line 12

Quoted 11 lines
accel/tcg/perf/jitdump.c new file +11 -0
+
+#include <stdint.h>
+
+#include <stdio.h>
+#include <unistd.h>
+#include <time.h>
+#include <sys/syscall.h>
+#include <elf.h>
+
+#include "jitdump.h"
+#include "qemu-common.h"

Please follow QEMU's header ordering conventions. See ./HACKING "1.2. Include directives".

accel/tcg/perf/jitdump.c line 105

Quoted 4 lines
accel/tcg/perf/jitdump.c new file +4 -0
+void start_jitdump_file(void)
+{
+    GString *dumpfile_name = g_string_new(NULL);;
+    g_string_printf(dumpfile_name, "./jit-%d.dump", getpid());

Simpler:

Quoted 9 lines
  gchar *dumpfile_name = g_strdup_printf("./jit-%d.dump", getpid());
  ...
  g_free(dumpfile_name);

accel/tcg/perf/jitdump.c line 106

Quoted 1 line
accel/tcg/perf/jitdump.c new file +1 -0
+    dumpfile = fopen(dumpfile_name->str, "w+");

getpid() and the global dumpfile variable make me wonder what happens with multi-threaded TCG?

I did some tests and it appears to be working fine with multi-threaded TCG. tcg_exec_init should execute only once even in multi-threaded TCG, right? If so, we are going to create only one jitdump file. Also, both in Windows and Linux/POSIX fwrites is thread-safe, thus we would be safely updating the jitdump file. Does it make sense?

Quoted 85 lines

accel/tcg/perf/jitdump.c line 19

Quoted 3 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    perf_marker = mmap(NULL, sysconf(_SC_PAGESIZE),

Please mention the point of this mmap in a comment. My best guess is that perf stores the /proc/$PID/maps and this is how it finds the jitdump file?

accel/tcg/perf/jitdump.c

Quoted 6 lines
accel/tcg/perf/jitdump.c new file +6 -0
+                          PROT_READ | PROT_EXEC,
+                          MAP_PRIVATE,
+                          fileno(dumpfile), 0);
+
+    if (perf_marker == MAP_FAILED) {
+        printf("Failed to create mmap marker file for perf %d\n",

fileno(dumpfile));

accel/tcg/perf/jitdump.c line 121

Quoted 7 lines
accel/tcg/perf/jitdump.c new file +7 -0
+        fclose(dumpfile);
+        return;
+    }
+
+    g_string_free(dumpfile_name, TRUE);
+
+    struct jitheader *header = g_new0(struct jitheader, 1);

Why g_new this struct? It's small and can be declared on the stack.

Please use g_free() with g_malloc/new/etc(). It's not safe to mismatch glib and libc memory allocation functions.

Quoted 7 lines
accel/tcg/perf/jitdump.c new file +7 -0
+    header->magic = 0x4A695444;
+    header->version = 1;
+    header->elf_mach = get_e_machine();
+    header->total_size = sizeof(struct jitheader);
+    header->pid = getpid();
+    header->timestamp = get_timestamp();
+

accel/tcg/perf/jitdump.c

Quoted 10 lines
accel/tcg/perf/jitdump.c new file +10 -0
+    fwrite(header, header->total_size, 1, dumpfile);
+
+    free(header);
+    fflush(dumpfile);
+}
+
+void append_load_in_jitdump_file(TranslationBlock *tb)
+{
+    GString *func_name = g_string_new(NULL);
+    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc,

'\0');

The explicit NUL character looks strange to me. I think the idea is to avoid func_name->len + 1? Adding NUL characters to C strings can be a source of bugs, I would stick to convention and do len + 1 instead of putting NUL characters into the GString. This is a question of style though.

accel/tcg/perf/jitdump.c line 51

Quoted 2 lines
accel/tcg/perf/jitdump.c new file +2 -0
+
+    struct jr_code_load *load_event = g_new0(struct jr_code_load, 1);

No need to allocate load_event on the heap.

qemu-options.hx STEXI · line 4152

Quoted 11 lines
qemu-options.hx +3 -0
index 9621e934c0..1c26eeeb9c 100644
@@ -4147,6 +4147,18 @@ STEXI
 Enable FIPS 140-2 compliance mode.
 ETEXI
 
+#ifdef __linux__
+DEF("perf", 0, QEMU_OPTION_perf,
+    "-perf    dump jitdump files to help linux perf JIT code

visualization\n",

qemu-options.hx STEXI · line 4158

Quoted 6 lines
qemu-options.hx +6 -0
+    QEMU_ARCH_ALL)
+#endif
+STEXI
+@item -perf
+@findex -perf
+Dumps jitdump files to help linux perf JIT code visualization

Suggestions on expanding the documentation:

Where are the jitdump files dumped? The current working directory?

Anything to say about the naming scheme for these files?

Can you include an example of how to load them into perf(1)?

#6 lore raw

Re: [Qemu-devel] [PATCH v1 1/2] accel/tcg: adding integration with linux perf

Quoted 19 lines · Vanderson Martins do Rosario
Quoted 8 lines · Stefan Hajnoczi

accel/tcg/perf/jitdump.c line 105

Quoted 4 lines · vandersonmr
accel/tcg/perf/jitdump.c new file +4 -0
+void start_jitdump_file(void)
+{
+    GString *dumpfile_name = g_string_new(NULL);;
+    g_string_printf(dumpfile_name, "./jit-%d.dump", getpid());

Simpler:

Quoted 9 lines
  gchar *dumpfile_name = g_strdup_printf("./jit-%d.dump", getpid());
  ...
  g_free(dumpfile_name);
Quoted 1 line

+ dumpfile = fopen(dumpfile_name->str, "w+");

getpid() and the global dumpfile variable make me wonder what happens with multi-threaded TCG?

Quoted 5 lines

I did some tests and it appears to be working fine with multi-threaded TCG. tcg_exec_init should execute only once even in multi-threaded TCG, right? If so, we are going to create only one jitdump file. Also, both in Windows and Linux/POSIX fwrites is thread-safe, thus we would be safely updating the jitdump file. Does it make sense?

append_load_in_jitdump_file() calls fwrite() multiple times. What guarantees they will not be interleaved when multiple threads call this function?

Stefan

#7 lore raw

PATCH 2/2 tb-stats: adding TBStatistics info into perf dump

In reply to vandersonmr

Adding TBStatistics information to linux perf TB's symbol names.

This commit depends on the following PATCH:
[PATCH v5 00/10] Measure Tiny Code Generation Quality

---
 accel/tcg/perf/jitdump.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
accel/tcg/perf/jitdump.c +14 -1
index 6f4c0911c2..b2334fd601 100644
@@ -8,6 +8,7 @@
 #include <sys/syscall.h>
 #include <elf.h>
 
+#include "exec/tb-stats.h"
 #include "jitdump.h"
 #include "qemu-common.h"
 
@@ -135,7 +136,19 @@ void start_jitdump_file(void)
 void append_load_in_jitdump_file(TranslationBlock *tb)
 {
     GString *func_name = g_string_new(NULL);
-    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');
+    if (tb->tb_stats) {
+        TBStatistics *tbs = tb->tb_stats;
+        g = stat_per_translation(tbs, code.num_guest_inst);
+        ops = stat_per_translation(tbs, code.num_tcg_ops);
+        ops_opt = stat_per_translation(tbs, code.num_tcg_ops_opt);
+        spills = stat_per_translation(tbs, code.spills);
+
+        g_string_printf(func_name,
+                "TB virt:0x"TARGET_FMT_lx" (g:%u op:%u opt:%u spills:%d)%c",
+                tb->pc, g, ops, ops_opt, spills, '\0');
+    } else {
+        g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');
+    }
 
     struct jr_code_load *load_event = g_new0(struct jr_code_load, 1);
     load_event->p.id = JIT_CODE_LOAD;
Signature · 1 line
2.22.0
#8 lore raw

Re: [Qemu-devel] [PATCH v1 2/2] tb-stats: adding TBStatistics info into perf dump

Quoted 23 lines · vandersonmr

Adding TBStatistics information to linux perf TB's symbol names.

This commit depends on the following PATCH:
[PATCH v5 00/10] Measure Tiny Code Generation Quality

---
 accel/tcg/perf/jitdump.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)
accel/tcg/perf/jitdump.c +1 -0
index 6f4c0911c2..b2334fd601 100644
@@ -8,6 +8,7 @@
 #include <sys/syscall.h>
 #include <elf.h>
 
+#include "exec/tb-stats.h"
 #include "jitdump.h"
 #include "qemu-common.h"
 

accel/tcg/perf/jitdump.c void start_jitdump_file(void) · line 144

Quoted 10 lines
accel/tcg/perf/jitdump.c +6 -1
@@ -135,7 +136,19 @@ void start_jitdump_file(void)
 void append_load_in_jitdump_file(TranslationBlock *tb)
 {
     GString *func_name = g_string_new(NULL);
-    g_string_printf(func_name, "TB virt:0x"TARGET_FMT_lx"%c", tb->pc, '\0');
+    if (tb->tb_stats) {
+        TBStatistics *tbs = tb->tb_stats;
+        g = stat_per_translation(tbs, code.num_guest_inst);
+        ops = stat_per_translation(tbs, code.num_tcg_ops);
+        ops_opt = stat_per_translation(tbs, code.num_tcg_ops_opt);
+        spills = stat_per_translation(tbs, code.spills);

Where are the declarations for these functions?

--
Alex Bennée

#9 lore raw
In reply to vandersonmr
Patchew URL: https://patchew.org/QEMU/[email protected]/

Patchew URL: https://patchew.org/QEMU/[email protected]/

Hi,

This series failed the asan build test. Please find the testing commands and their output below. If you have Docker installed, you can probably reproduce it locally.

=== TEST SCRIPT BEGIN ===
#!/bin/bash
make docker-image-fedora V=1 NETWORK=1
time make docker-test-debug@fedora TARGET_LIST=x86_64-softmmu J=14 NETWORK=1 === TEST SCRIPT END ===

  CC      x86_64-softmmu/hw/intc/apic_common.o
  CC      x86_64-softmmu/hw/intc/ioapic.o
  CC      x86_64-softmmu/hw/isa/lpc_ich9.o
/tmp/qemu-test/src/accel/tcg/perf/jitdump.c:11:10: fatal error: 'exec/tb-stats.h' file not found
#include "exec/tb-stats.h"
         ^~~~~~~~~~~~~~~~~

CC x86_64-softmmu/hw/misc/ivshmem.o

The full log is available at
http://patchew.org/logs/[email protected]/testing.asan/?type=message.
---
Email generated automatically by Patchew [https://patchew.org/].
Please send your feedback to [email protected]