Skip to contentSkip to conversation

perf: add support for profiling jitted code

PATCH Superseded Stephane Eranian 2 participants 8 messages Feb 18, 2015 – Feb 22, 2015

Subsystems (2)

v3 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v3 cover perf: add support for profiling jitted code

This patch series extends perf record/report/annotate to enable profiling of jitted (just-in-time compiled) code. The current perf tool provides very limited support for profiling jitted code for some runtime environments. But the support is experimental and cannot be used in complex environments. It relies on files in /tmp, for instance. It does not support annotate mode or rejitted code.

This patch series adds a better way of profiling jitted code with the following advantages:
- support any jitted code environment (some with modifications)
- support Java runtime with JVMTI interface with no modifications
- provides a portable JVMTI agent library
- known to support V8 runtime
- known to support DART runtime
- supports code rejitting and movements
- no files in /tmp
- meta-data file is unique to each run
- no changes to perf report/annotate

The support is based on cooperation with the runtime. For Java runtimes, supporting the JVMTI interface, there is no change necessary. For other runtimes, modifications are necessary to emit the meta-data necessary to support symbolization and annotation of the samples. Those modifications are fairly straighforward and already tested on V8 and DART.

The jit environment emits a binary dump file which contains the jitted code (in raw format) and meta-data describing the mapping of functions. The binary format is documented in the jitdump.h header file. It is adapted from the OProfile jitdump format.

To enable synchronization of the runtime MMAPs with those recorded by the kernel on behalf of the perf tool, the runtime needs to timestamp any record in the dump file using the same time source. This third version is using Pawel Moll's CLOCK_MONOTONIC patch posted

	https://lkml.org/lkml/2015/1/21/711

The patch is packaged in this series for convenience. Without this patch installed, records emitted by the runtime cannot be properly synchronized (inserted in the flow of MMAPS) and symbolization is not possible.

The current support only works when the runtime is monitored from start to finish: perf record java --agentpath:libpfmjvmti.so my_class.

Once the run is completed, the jitdump file needs to be injected into the perf.data file. This is accomplished by using the perf inject command. This will also generate an ELF image for each jitted function. The inject MMAP records will point to those ELF images. The reasoning behind using ELF images is that it makes processing for perf report and annotate automatic and transparent. It also makes it easier to package and analyze on a remote machine.

The reporting is unchanged, simply invoke perf report or perf annotate on the modified perf.data file. The jitted code will appear symbolized and the assembly view will display the instruction level profile!

As an added bonus, the series includes support for demangling function signature from OpenJDK.

The current patch series does not include support for source line information. Therefore, the source code cannot yet be displayed in perf annotate. This will come in a later update.

Furthermore, we believe there is a way to skip the perf inject phase and have perf report/annotate directly inject the MMAP records on the fly during processing of the perf.data file. Perf report would also generate the ELF files if necessary. Such optimization, would make using this extension seamless in system-wide mode and larger environments. This will be added in a later update as well.

In V2, we have switchde to Pawell Moll and David Ahern posix clock kernel module instead. We have dropped the patch which modified the arguments to map_init() because the change was not used. We are not printing the return type of Java methods anymore and have made the Java demangler a separate module. We also rebased to 3.19.0+ from tip.git.

In V3, we switched to Pawel Moll's CLOCK_MONOTONIC perf clock patches. This patch switch perf_events from sched_clock to CLOCK_MONOTONIC, a clock source which is available to users.

To use the new feature:
- compile and install Pawel's CLOCK_MONOTONIC patch:
- compile the patched kernel
- install the patched (and modules)
- reboot the machine to this patched kernel
- check that you have /proc/sys/kerne/perf_sample_time_clk_id
- compile perf
- cd tools/perf/jvmti; make; install wherever is appropriate

Example using openJDK:

   $ perf record java -agentpath:libjvmti.so my_class
   java: jvmti: jitdump in $HOME/.debug/jit/java-jit-20150207.XXL9649H/jit-6320.dump
   $ perf inject -i perf.data -j $HOME/.debug/jit/java-jit-20150207.XXL9649H/jit-6320.dump -o perf.data.jitted
   $ perf report -i perf.data.jitted

Thanks to all the contributors and testers.

Enjoy,

Pawel Moll (1):
perf: Use monotonic clock as a source for timestamps

Stephane Eranian (3):
perf tools: add Java demangling support
perf inject: add jitdump mmap injection support
perf tools: add JVMTI agent library

 Documentation/kernel-parameters.txt      |   9 +
 kernel/events/core.c                     |  43 ++-
 tools/perf/Documentation/perf-inject.txt |  11 +
 tools/perf/Makefile.perf                 |   8 +-
 tools/perf/builtin-inject.c              | 205 ++++++++++++++
 tools/perf/jvmti/Makefile                |  70 +++++
 tools/perf/jvmti/jvmti_agent.c           | 334 ++++++++++++++++++++++
 tools/perf/jvmti/jvmti_agent.h           |  23 ++
 tools/perf/jvmti/libjvmti.c              | 149 ++++++++++
 tools/perf/util/demangle-java.c          | 199 +++++++++++++
 tools/perf/util/demangle-java.h          |  10 +
 tools/perf/util/genelf.c                 | 463 +++++++++++++++++++++++++++++++
 tools/perf/util/genelf.h                 |   6 +
 tools/perf/util/jit.h                    |  27 ++
 tools/perf/util/jitdump.c                | 233 ++++++++++++++++
 tools/perf/util/jitdump.h                |  92 ++++++
 tools/perf/util/symbol-elf.c             |   3 +
 tools/perf/util/symbol.c                 |   6 +-
 18 files changed, 1888 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/jvmti/Makefile
 create mode 100644 tools/perf/jvmti/jvmti_agent.c
 create mode 100644 tools/perf/jvmti/jvmti_agent.h
 create mode 100644 tools/perf/jvmti/libjvmti.c
 create mode 100644 tools/perf/util/demangle-java.c
 create mode 100644 tools/perf/util/demangle-java.h
 create mode 100644 tools/perf/util/genelf.c
 create mode 100644 tools/perf/util/genelf.h
 create mode 100644 tools/perf/util/jit.h
 create mode 100644 tools/perf/util/jitdump.c
 create mode 100644 tools/perf/util/jitdump.h
Signature · 1 line
1.9.1
#1 lore raw

PATCH v3 1/4 perf tools: add Java demangling support

Add Java function descriptor demangling support.
Something bfd cannot do.

Use the JAVA_DEMANGLE_NORET flag to avoid decoding the return type of functions.

---
 tools/perf/Makefile.perf        |   2 +
 tools/perf/util/demangle-java.c | 199 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/demangle-java.h |  10 ++
 tools/perf/util/symbol-elf.c    |   3 +
 tools/perf/util/symbol.c        |   6 +-
 5 files changed, 219 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/demangle-java.c
 create mode 100644 tools/perf/util/demangle-java.h
tools/perf/Makefile.perf +2 -0
index aa6a504..53a44be 100644
@@ -331,6 +331,7 @@ LIB_H += ui/ui.h
 LIB_H += util/data.h
 LIB_H += util/kvm-stat.h
 LIB_H += util/thread-stack.h
+LIB_H += util/demangle-java.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -410,6 +411,7 @@ LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/cloexec.o
 LIB_OBJS += $(OUTPUT)util/thread-stack.o
+LIB_OBJS += $(OUTPUT)util/demangle-java.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/demangle-java.c new file +199 -0

199 lines not on this page · raw message

tools/perf/util/demangle-java.h new file +10 -0
new file mode 100644
index 0000000..a981c1f
@@ -0,0 +1,10 @@
+#ifndef __PERF_DEMANGLE_JAVA
+#define __PERF_DEMANGLE_JAVA 1
+/*
+ * demangle function flags
+ */
+#define JAVA_DEMANGLE_NORET	0x1 /* do not process return type */
+
+char * java_demangle_sym(const char *str, int flags);
+
+#endif /* __PERF_DEMANGLE_JAVA */
tools/perf/util/symbol-elf.c +3 -0
index b24f9d8..73b3e8f 100644
@@ -6,6 +6,7 @@
 #include <inttypes.h>
 
 #include "symbol.h"
+#include "demangle-java.h"
 #include "machine.h"
 #include "vdso.h"
 #include <symbol/kallsyms.h>
@@ -1019,6 +1020,8 @@ int dso__load_sym(struct dso *dso, struct map *map,
 				demangle_flags = DMGL_PARAMS | DMGL_ANSI;
 
 			demangled = bfd_demangle(NULL, elf_name, demangle_flags);
+			if (demangled == NULL)
+				demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
 			if (demangled != NULL)
 				elf_name = demangled;
 		}
tools/perf/util/symbol.c +5 -1
index a690668..3bc779b 100644
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <bfd.h>
 #include "build-id.h"
 #include "util.h"
 #include "debug.h"
@@ -1257,6 +1258,7 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		u64 start, size;
 		struct symbol *sym;
 		int line_len, len;
+		char *name;
 
 		line_len = getline(&line, &n, file);
 		if (line_len < 0)
@@ -1279,7 +1281,9 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		if (len + 2 >= line_len)
 			continue;
 
-		sym = symbol__new(start, size, STB_GLOBAL, line + len);
+		name = line + len;
+
+		sym = symbol__new(start, size, STB_GLOBAL, name);
 
 		if (sym == NULL)
 			goto out_delete_line;
Signature · 1 line
1.9.1
#7 lore raw

PATCH v3 4/4 perf: Use monotonic clock as a source for timestamps

In reply to Stephane Eranian

From: Pawel Moll <[email protected]>

Until now, perf framework never defined the meaning of the timestamps captured as PERF_SAMPLE_TIME sample type. The values were obtaining from local (sched) clock, which is unavailable in userspace. This made it impossible to correlate perf data with any other events. Other tracing solutions have the source configurable (ftrace) or just share a common time domain between kernel and userspace (LTTng).

Follow the trend by using monotonic clock, which is readily available as POSIX CLOCK_MONOTONIC.

Also add a sysctl "perf_sample_time_clk_id" attribute (usually available as "/proc/sys/kernel/perf_sample_time_clk_id") which can be used by the user to obtain the clk_id to be used with POSIX clock API (eg. clock_gettime()) to obtain a time value comparable with perf samples.

Old behaviour can be restored by using "perf_use_local_clock" kernel parameter.

---
Changes since v4:
- using jump labels to reduce runtime overhead of the local/mono check

---
 Documentation/kernel-parameters.txt |  9 ++++++++
 kernel/events/core.c                | 43 ++++++++++++++++++++++++++++++++++++-
 2 files changed, 51 insertions(+), 1 deletion(-)
Documentation/kernel-parameters.txt +9 -0
index 176d4fe..5225567 100644
@@ -91,6 +91,7 @@ the beginning of each description states the restrictions within which a
 	NUMA	NUMA support is enabled.
 	NFS	Appropriate NFS support is enabled.
 	OSS	OSS sound support is enabled.
+	PERF	Performance events and counters support is enabled.
 	PV_OPS	A paravirtualized kernel is enabled.
 	PARIDE	The ParIDE (parallel port IDE) subsystem is enabled.
 	PARISC	The PA-RISC architecture is enabled.
@@ -2796,6 +2797,14 @@ bytes respectively. Such letter suffixes can also be entirely omitted.
 			allocator.  This parameter is primarily	for debugging
 			and performance comparison.
 
+	perf_use_local_clock
+			[PERF]
+			Use local_clock() as a source for perf timestamps
+			generation. This was be the default behaviour and
+			this parameter can be used to maintain backward
+			compatibility or on older hardware with expensive
+			monotonic clock source.
+
 	pf.		[PARIDE]
 			See Documentation/blockdev/paride.txt.
 
kernel/events/core.c +42 -1

63 lines not on this page · raw message

Signature · 1 line
1.9.1
#6 lore raw

PATCH v3 3/4 perf tools: add JVMTI agent library

In reply to Stephane Eranian

This is a standalone JVMTI library to help profile Java jitted code with perf record/perf report. The library is not installed or compiled automatically by perf Makefile. It is not used directly by perf. It is arch agnostic and has been tested on X86 and ARM. It needs to be used with a Java runtime, such as OpenJDK, as follows:

$ java -agentpath:libjvmti.so .......

When used this way, java will generate a jitdump binary file in $HOME/.debug/java/jit/java-jit-*

This binary dump file contains information to help symbolize and annotate jitted code.

The next step is to inject the jitdump information into the perf.data file:

$ perf inject -j $HOME/.debug/java/jit/java-jit-XXXX/jit-ZZZ.dump \
              -i perf.data -o perf.data.jitted

This injects the MMAP records to cover the jitted code and also generates one ELF image for each jitted function. The ELF images are created in the same subdir as the jitdump file. The MMAP records point there too.

Then to visualize the function or asm profile, simply use the regular perf commands:

$ perf report -i perf.data.jitted
or
$ perf annotate -i perf.data.jitted

JVMTI agent code adapted from the OProfile's opagent code.

This version of the JVMTI agent is using Pawel Moll's CLOCK_MONOTONIC perf clock patch posted here:

	https://lkml.org/lkml/2015/1/21/711
---
 tools/perf/jvmti/Makefile      |  70 +++++++++
 tools/perf/jvmti/jvmti_agent.c | 334 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/jvmti/jvmti_agent.h |  23 +++
 tools/perf/jvmti/libjvmti.c    | 149 ++++++++++++++++++
 4 files changed, 576 insertions(+)
 create mode 100644 tools/perf/jvmti/Makefile
 create mode 100644 tools/perf/jvmti/jvmti_agent.c
 create mode 100644 tools/perf/jvmti/jvmti_agent.h
 create mode 100644 tools/perf/jvmti/libjvmti.c
tools/perf/jvmti/Makefile new file +70 -0
new file mode 100644
index 0000000..22efd13
@@ -0,0 +1,70 @@
+ARCH=$(shell uname -m)
+
+ifeq ($(ARCH), x86_64)
+JARCH=amd64
+endif
+ifeq ($(ARCH), armv7l)
+JARCH=armhf
+endif
+ifeq ($(ARCH), armv6l)
+JARCH=armhf
+endif
+ifeq ($(ARCH), ppc64)
+JARCH=powerpc
+endif
+ifeq ($(ARCH), ppc64le)
+JARCH=powerpc
+endif
+
+DESTDIR=/usr/local
+
+VERSION=1
+REVISION=0
+AGE=0
+
+LN=ln -sf
+RM=rm
+
+SLIBJVMTI=libjvmti.so.$(VERSION).$(REVISION).$(AGE)
+VLIBJVMTI=libjvmti.so.$(VERSION)
+SLDFLAGS=-shared -Wl,-soname -Wl,$(VLIBJVMTI)
+SOLIBEXT=so
+
+JDIR=$(shell /usr/sbin/update-java-alternatives -l | head -1 | cut -d ' ' -f 3)
+# -lrt required in 32-bit mode for clock_gettime()
+LIBS=-lelf -lrt
+INCDIR=-I $(JDIR)/include -I $(JDIR)/include/linux
+
+TARGETS=$(SLIBJVMTI)
+
+SRCS=libjvmti.c jvmti_agent.c
+OBJS=$(SRCS:.c=.o)
+SOBJS=$(OBJS:.o=.lo)
+OPT=-O2 -g -Werror -Wall
+
+CFLAGS=$(INCDIR) $(OPT)
+
+all: $(TARGETS)
+
+.c.o:
+	$(CC) $(CFLAGS) -c $*.c
+.c.lo:
+	$(CC) -fPIC -DPIC $(CFLAGS) -c $*.c -o $*.lo
+
+$(OBJS) $(SOBJS): Makefile jvmti_agent.h ../util/jitdump.h
+
+$(SLIBJVMTI):  $(SOBJS)
+	$(CC) $(CFLAGS) $(SLDFLAGS)  -o $@ $(SOBJS) $(LIBS)
+	$(LN) $@ libjvmti.$(SOLIBEXT)
+
+clean:
+	$(RM) -f *.o *.so.* *.so *.lo
+
+install:
+	-mkdir -p $(DESTDIR)/lib
+	install -m 755 $(SLIBJVMTI) $(DESTDIR)/lib/
+	(cd $(DESTDIR)/lib; $(LN) $(SLIBJVMTI) $(VLIBJVMTI))
+	(cd $(DESTDIR)/lib; $(LN) $(SLIBJVMTI) libjvmti.$(SOLIBEXT))
+	ldconfig
+
+.SUFFIXES: .c .S .o .lo
tools/perf/jvmti/jvmti_agent.c new file +334 -0

334 lines not on this page · raw message

tools/perf/jvmti/jvmti_agent.h new file +23 -0
new file mode 100644
index 0000000..54e5c5e
@@ -0,0 +1,23 @@
+#ifndef __JVMTI_AGENT_H__
+#define __JVMTI_AGENT_H__
+
+#include <sys/types.h>
+#include <stdint.h>
+
+#define __unused __attribute__((unused))
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+void *jvmti_open(void);
+int   jvmti_close(void *agent);
+int   jvmti_write_code(void *agent, char const *symbol_name,
+		       uint64_t vma, void const *code,
+		       const unsigned int code_size);
+
+#if defined(__cplusplus)
+}
+
+#endif
+#endif /* __JVMTI_H__ */
tools/perf/jvmti/libjvmti.c new file +149 -0

149 lines not on this page · raw message

Signature · 1 line
1.9.1
#4 lore raw

PATCH v3 2/4 perf inject: add jitdump mmap injection support

In reply to Stephane Eranian

This patch adds a -j jitdump option to perf inject.

This options injects MMAP records into the perf.data file to cover the jitted code mmaps. It also emits ELF images for each function in the jidump file. Those images are created where the jitdump file is. The MMAP records point to that location as well.

Typical flow:

$ java -agentpath:libpjvmti.so java_class
$ perf inject -j ~/.debug/jit/java-jit-20140514.XXAb0e5C/jit-7640.dump \
              -i perf.data \
              -o perf.data.jitted
$ perf report -i perf.data.jitted

Note that jitdump.h support is not limited to Java, it works with any jitted environment modified to emit the jitdump file format, include those where code can be jitted multiple times and moved around.

The jitdump.h format is adapted from the Oprofile project.

---
 tools/perf/Documentation/perf-inject.txt |  11 +
 tools/perf/Makefile.perf                 |   6 +-
 tools/perf/builtin-inject.c              | 205 ++++++++++++++
 tools/perf/util/genelf.c                 | 463 +++++++++++++++++++++++++++++++
 tools/perf/util/genelf.h                 |   6 +
 tools/perf/util/jit.h                    |  27 ++
 tools/perf/util/jitdump.c                | 233 ++++++++++++++++
 tools/perf/util/jitdump.h                |  92 ++++++
 8 files changed, 1042 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/genelf.c
 create mode 100644 tools/perf/util/genelf.h
 create mode 100644 tools/perf/util/jit.h
 create mode 100644 tools/perf/util/jitdump.c
 create mode 100644 tools/perf/util/jitdump.h
tools/perf/Documentation/perf-inject.txt +11 -0
index dc7442c..237f195 100644
@@ -40,6 +40,17 @@ OPTIONS
 	Merge sched_stat and sched_switch for getting events where and how long
 	tasks slept. sched_switch contains a callchain where a task slept and
 	sched_stat contains a timeslice how long a task slept.
+-j::
+--jit::
+	Merge a jitdump file into the perf.data file by adding mmap records to
+	cover jitted code and emit ELF images for each jitted function. The ELF
+	images are saved in the same directory as the jidump. Use -E to suppress
+	ELF images generation.
+-E::
+--jit-disable-elf::
+	When used with -, it prevents creating the ELF images for each jitted
+	function. Only the jitted code mmap records are injected into the perf.data
+	file. Option as no effect when -j is not used.
 
 --kallsyms=<file>::
 	kallsyms pathname
tools/perf/Makefile.perf +5 -1
index 53a44be..d405632 100644
@@ -324,6 +324,7 @@ LIB_H += util/perf_regs.h
 LIB_H += util/unwind.h
 LIB_H += util/vdso.h
 LIB_H += util/tsc.h
+LIB_H += util/jitdump.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -412,6 +413,8 @@ LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/cloexec.o
 LIB_OBJS += $(OUTPUT)util/thread-stack.o
 LIB_OBJS += $(OUTPUT)util/demangle-java.o
+LIB_OBJS += $(OUTPUT)util/jitdump.o
+LIB_OBJS += $(OUTPUT)util/genelf.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
@@ -498,7 +501,8 @@ BUILTIN_OBJS += $(OUTPUT)builtin-inject.o
 BUILTIN_OBJS += $(OUTPUT)tests/builtin-test.o
 BUILTIN_OBJS += $(OUTPUT)builtin-mem.o
 
-PERFLIBS = $(LIB_FILE) $(LIBAPIKFS) $(LIBTRACEEVENT)
+PERFLIBS = $(LIB_FILE) $(LIBAPIKFS) $(LIBTRACEEVENT) -lcrypto
+
 
 # We choose to avoid "if .. else if .. else .. endif endif"
 # because maintaining the nesting to match is a pain.  If
tools/perf/builtin-inject.c +205 -0

253 lines not on this page · raw message

tools/perf/util/genelf.c new file +463 -0

463 lines not on this page · raw message

tools/perf/util/genelf.h new file +6 -0
new file mode 100644
index 0000000..11307a2
@@ -0,0 +1,6 @@
+#ifndef __GENELF_H__
+#define __GENELF_H__
+
+extern int jit_write_elf(int fd, unsigned long code_addr, const char *sym, const void *code, int csize);
+
+#endif
tools/perf/util/jit.h new file +27 -0
new file mode 100644
index 0000000..e7a54cb
@@ -0,0 +1,27 @@
+#ifndef __JIT_H__
+#define __JIT_H__
+#include <sys/time.h>
+#include <stdint.h>
+
+#include "jitdump.h"
+
+struct jit_buf_desc {
+	union jr_entry  *entry;
+	void            *buf;
+	size_t          bufsize;
+	FILE            *in;
+	int             needs_bswap; /* handles cross-endianess */
+	uint32_t        code_load_count;
+	struct rb_root  code_root;
+};
+ 
+extern int jit_open_dump(const char *name, struct jit_buf_desc *jd);
+extern void jit_close_dump(struct jit_buf_desc *jd);
+extern union jr_entry *jit_get_next_entry(struct jit_buf_desc *jd);
+
+/*
+ * use type = -1 to match any record type
+ */
+extern union jr_entry *jit_get_next_entry_type(struct jit_buf_desc *jd, int type);
+
+#endif /* __JIT_H__ */
tools/perf/util/jitdump.c new file +233 -0

233 lines not on this page · raw message

tools/perf/util/jitdump.h new file +92 -0

92 lines not on this page · raw message

Signature · 1 line
1.9.1
maintainer
#2 lore raw

Re: [PATCH v3 1/4] perf tools: add Java demangling support

In reply to Stephane Eranian

Hi Stephane,

Quoted 67 lines · Stephane Eranian

Add Java function descriptor demangling support.
Something bfd cannot do.

Use the JAVA_DEMANGLE_NORET flag to avoid decoding the return type of functions.

---
 tools/perf/Makefile.perf        |   2 +
 tools/perf/util/demangle-java.c | 199 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/demangle-java.h |  10 ++
 tools/perf/util/symbol-elf.c    |   3 +
 tools/perf/util/symbol.c        |   6 +-
 5 files changed, 219 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/demangle-java.c
 create mode 100644 tools/perf/util/demangle-java.h
tools/perf/Makefile.perf +2 -0
index aa6a504..53a44be 100644
@@ -331,6 +331,7 @@ LIB_H += ui/ui.h
 LIB_H += util/data.h
 LIB_H += util/kvm-stat.h
 LIB_H += util/thread-stack.h
+LIB_H += util/demangle-java.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -410,6 +411,7 @@ LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/cloexec.o
 LIB_OBJS += $(OUTPUT)util/thread-stack.o
+LIB_OBJS += $(OUTPUT)util/demangle-java.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/demangle-java.c new file +24 -0
new file mode 100644
index 0000000..79d5484
@@ -0,0 +1,199 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include "util.h"
+#include "debug.h"
+#include "symbol.h"
+
+#include "demangle-java.h"
+
+enum {
+	MODE_PREFIX=0,
+	MODE_CLASS=1,
+	MODE_FUNC=2,
+	MODE_TYPE=3,
+	MODE_CTYPE=3, /* class arg */
+};
+
+#define BASE_ENT(c, n)	[c-'A']=n
+static const char *base_types['Z'-'A' + 1]={
+	BASE_ENT('B', "byte" ),
+	BASE_ENT('C', "char" ),
+	BASE_ENT('D', "double" ),
+	BASE_ENT('F', "float" ),
+	BASE_ENT('I', "int" ),

tools/perf/util/demangle-java.c line 34

Quoted 10 lines
tools/perf/util/demangle-java.c new file +10 -0
+	BASE_ENT('J', "long" ),
+	BASE_ENT('S', "short" ),
+	BASE_ENT('Z', "bool" ),
+};
+
+/*
+ * demangle Java symbol between str and end positions and stores
+ * up to maxlen characters into buf. The parser starts in mode.
+ *
+ * Use MODE_PREFIX to process entire prototype till end position

So this still includes the argument (type) right? I just worry about that it might fill up the precious screen width of TUI like C++ symbols did before..

tools/perf/util/demangle-java.c line 35

Quoted 1 line
tools/perf/util/demangle-java.c new file +1 -0
+ * Use MODE_TYPE to process return type if str starts on return type char

What about MODE_CTYPE? I cannot find where it's used..

Quoted 206 lines
tools/perf/util/demangle-java.c new file +164 -0
+ *
+ *  Return:
+ *	success: buf
+ *	error  : NULL
+ */
+static char *
+__demangle_java_sym(const char *str, const char *end, char *buf, int maxlen, int mode)
+{
+	int rlen = 0;
+	int array = 0;
+	int narg = 0;
+	const char *q;
+
+	if (!end)
+		end = str + strlen(str);
+
+	for (q = str; q != end; q++) {
+
+		if (rlen == (maxlen - 1))
+			break;
+
+		switch (*q) {
+		case 'L':
+			if (mode == MODE_PREFIX || mode == MODE_CTYPE) {
+				if (mode == MODE_CTYPE) {
+					if (narg)
+						rlen += snprintf(buf+rlen, maxlen - rlen, ", ");
+					narg++;
+				}
+				rlen += snprintf(buf+rlen, maxlen - rlen, "class ");
+				if (mode == MODE_PREFIX)
+					mode = MODE_CLASS;
+			} else
+				buf[rlen++] = *q;
+			break;
+		case 'B':
+		case 'C':
+		case 'D':
+		case 'F':
+		case 'I':
+		case 'J':
+		case 'S':
+		case 'Z':
+			if (mode == MODE_TYPE) {
+				if (narg)
+					rlen += snprintf(buf+rlen, maxlen - rlen, ", ");
+				rlen += snprintf(buf+rlen, maxlen - rlen, "%s", base_types[*q - 'A']);
+				while(array--)
+					rlen += snprintf(buf+rlen, maxlen - rlen, "[]");
+				array = 0;
+				narg++;
+			} else
+				buf[rlen++] = *q;
+			break;
+		case 'V':
+			if (mode == MODE_TYPE) {
+				rlen += snprintf(buf+rlen, maxlen - rlen, "void");
+				while(array--)
+					rlen += snprintf(buf+rlen, maxlen - rlen, "[]");
+				array = 0;
+			} else
+				buf[rlen++] = *q;
+			break;
+		case '[':
+			if (mode != MODE_TYPE)
+				goto error;
+			array++;
+			break;
+		case '(':
+			if (mode != MODE_FUNC)
+				goto error;
+			buf[rlen++] = *q;
+			mode = MODE_TYPE;
+			break;
+		case ')':
+			if (mode != MODE_TYPE)
+				goto error;
+			buf[rlen++] = *q;
+			narg = 0;
+			break;
+		case ';':
+			if (mode != MODE_CLASS && mode != MODE_CTYPE)
+				goto error;
+			/* safe because at least one other char to process */
+			if (isalpha(*(q+1)))
+				rlen += snprintf(buf+rlen, maxlen - rlen, ".");
+			if (mode == MODE_CLASS)
+				mode = MODE_FUNC;
+			else if (mode == MODE_CTYPE)
+				mode = MODE_TYPE;
+			break;
+		case '/':
+			if (mode != MODE_CLASS && mode != MODE_CTYPE)
+				goto error;
+			rlen += snprintf(buf+rlen, maxlen - rlen, ".");
+			break;
+		default :
+			buf[rlen++] = *q;
+		}
+	}
+	buf[rlen] = '\0';
+	return buf;
+error:
+	return NULL;
+}
+
+/*
+ * Demangle Java function signature (openJDK, not GCJ)
+ * input:
+ * 	str: string to parse. String is not modified
+ *    flags: comobination of JAVA_DEMANGLE_* flags to modify demangling
+ * return:
+ *	if input can be demangled, then a newly allocated string is returned.
+ *	if input cannot be demangled, then NULL is returned
+ *
+ * Note: caller is responsible for freeing demangled string
+ */
+char *
+java_demangle_sym(const char *str, int flags)
+{
+	char *buf, *ptr;
+	char *p;
+	size_t len, l1 = 0;
+
+	if (!str)
+		return NULL;
+
+	/* find start of retunr type */
+	p = strrchr(str, ')');
+	if (!p)
+		return NULL;
+
+	/*
+	 * expansion factor estimated to 3x
+	 */
+	len = strlen(str) * 3 + 1;
+	buf = malloc(len);
+	if (!buf)
+		return NULL;
+
+	buf[0] = '\0';
+	if (!(flags & JAVA_DEMANGLE_NORET)) {
+		/*
+		 * get return type first
+		 */
+		ptr = __demangle_java_sym(p+1, NULL, buf, len, MODE_TYPE);
+		if (!ptr)
+			goto error;
+
+		/* add space between return type and function prototype */
+		l1 = strlen(buf);
+		buf[l1++] = ' ';
+	}
+
+	/* process function up to return type */
+	ptr = __demangle_java_sym(str, p + 1, buf + l1, len - l1, MODE_PREFIX);
+	if (!ptr)
+		goto error;
+
+	return buf;
+error:
+	free(buf);
+	return NULL;
+}
tools/perf/util/demangle-java.h new file +10 -0
new file mode 100644
index 0000000..a981c1f
@@ -0,0 +1,10 @@
+#ifndef __PERF_DEMANGLE_JAVA
+#define __PERF_DEMANGLE_JAVA 1
+/*
+ * demangle function flags
+ */
+#define JAVA_DEMANGLE_NORET	0x1 /* do not process return type */
+
+char * java_demangle_sym(const char *str, int flags);
+
+#endif /* __PERF_DEMANGLE_JAVA */
tools/perf/util/symbol-elf.c +3 -0
index b24f9d8..73b3e8f 100644
@@ -6,6 +6,7 @@
 #include <inttypes.h>
 
 #include "symbol.h"
+#include "demangle-java.h"
 #include "machine.h"
 #include "vdso.h"
 #include <symbol/kallsyms.h>
@@ -1019,6 +1020,8 @@ int dso__load_sym(struct dso *dso, struct map *map,
 				demangle_flags = DMGL_PARAMS | DMGL_ANSI;
 
 			demangled = bfd_demangle(NULL, elf_name, demangle_flags);
+			if (demangled == NULL)
+				demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
 			if (demangled != NULL)
 				elf_name = demangled;
 		}
tools/perf/util/symbol.c +0 -0
index a690668..3bc779b 100644

tools/perf/util/symbol.c line 12

Quoted 4 lines
tools/perf/util/symbol.c +1 -0
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <bfd.h>

Wouldn't it break build on systems don't have the bfd devel package? And I don't see why it's needed here..

Quoted 12 lines
tools/perf/util/symbol.c +1 -0
 #include "build-id.h"
 #include "util.h"
 #include "debug.h"
@@ -1257,6 +1258,7 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		u64 start, size;
 		struct symbol *sym;
 		int line_len, len;
+		char *name;
 
 		line_len = getline(&line, &n, file);
 		if (line_len < 0)

tools/perf/util/symbol.c static int dso__load_perf_map(struct dso *dso, struct map *map, · line 1286

Quoted 7 lines
tools/perf/util/symbol.c +3 -1
@@ -1279,7 +1281,9 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		if (len + 2 >= line_len)
 			continue;
 
-		sym = symbol__new(start, size, STB_GLOBAL, line + len);
+		name = line + len;
+
+		sym = symbol__new(start, size, STB_GLOBAL, name);

I think this file is not a part of this changeset.

Thanks,
Namhyung

Quoted 6 lines
tools/perf/util/symbol.c +0 -0
 		if (sym == NULL)
 			goto out_delete_line;
Signature · 1 line
1.9.1
maintainer
#5 lore raw

Re: [PATCH v3 2/4] perf inject: add jitdump mmap injection support

In reply to Stephane Eranian
Quoted 84 lines · Stephane Eranian

This patch adds a -j jitdump option to perf inject.

This options injects MMAP records into the perf.data file to cover the jitted code mmaps. It also emits ELF images for each function in the jidump file. Those images are created where the jitdump file is. The MMAP records point to that location as well.

Typical flow:

$ java -agentpath:libpjvmti.so java_class
$ perf inject -j ~/.debug/jit/java-jit-20140514.XXAb0e5C/jit-7640.dump \
              -i perf.data \
              -o perf.data.jitted
$ perf report -i perf.data.jitted

Note that jitdump.h support is not limited to Java, it works with any jitted environment modified to emit the jitdump file format, include those where code can be jitted multiple times and moved around.

The jitdump.h format is adapted from the Oprofile project.

---
 tools/perf/Documentation/perf-inject.txt |  11 +
 tools/perf/Makefile.perf                 |   6 +-
 tools/perf/builtin-inject.c              | 205 ++++++++++++++
 tools/perf/util/genelf.c                 | 463 +++++++++++++++++++++++++++++++
 tools/perf/util/genelf.h                 |   6 +
 tools/perf/util/jit.h                    |  27 ++
 tools/perf/util/jitdump.c                | 233 ++++++++++++++++
 tools/perf/util/jitdump.h                |  92 ++++++
 8 files changed, 1042 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/genelf.c
 create mode 100644 tools/perf/util/genelf.h
 create mode 100644 tools/perf/util/jit.h
 create mode 100644 tools/perf/util/jitdump.c
 create mode 100644 tools/perf/util/jitdump.h
tools/perf/Documentation/perf-inject.txt +11 -0
index dc7442c..237f195 100644
@@ -40,6 +40,17 @@ OPTIONS
 	Merge sched_stat and sched_switch for getting events where and how long
 	tasks slept. sched_switch contains a callchain where a task slept and
 	sched_stat contains a timeslice how long a task slept.
+-j::
+--jit::
+	Merge a jitdump file into the perf.data file by adding mmap records to
+	cover jitted code and emit ELF images for each jitted function. The ELF
+	images are saved in the same directory as the jidump. Use -E to suppress
+	ELF images generation.
+-E::
+--jit-disable-elf::
+	When used with -, it prevents creating the ELF images for each jitted
+	function. Only the jitted code mmap records are injected into the perf.data
+	file. Option as no effect when -j is not used.
 
 --kallsyms=<file>::
 	kallsyms pathname
tools/perf/Makefile.perf +3 -0
index 53a44be..d405632 100644
@@ -324,6 +324,7 @@ LIB_H += util/perf_regs.h
 LIB_H += util/unwind.h
 LIB_H += util/vdso.h
 LIB_H += util/tsc.h
+LIB_H += util/jitdump.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -412,6 +413,8 @@ LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/cloexec.o
 LIB_OBJS += $(OUTPUT)util/thread-stack.o
 LIB_OBJS += $(OUTPUT)util/demangle-java.o
+LIB_OBJS += $(OUTPUT)util/jitdump.o
+LIB_OBJS += $(OUTPUT)util/genelf.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o

tools/perf/Makefile.perf BUILTIN_OBJS += $(OUTPUT)builtin-inject.o · line 505

Quoted 6 lines
tools/perf/Makefile.perf +2 -1
@@ -498,7 +501,8 @@ BUILTIN_OBJS += $(OUTPUT)builtin-inject.o
 BUILTIN_OBJS += $(OUTPUT)tests/builtin-test.o
 BUILTIN_OBJS += $(OUTPUT)builtin-mem.o
 
-PERFLIBS = $(LIB_FILE) $(LIBAPIKFS) $(LIBTRACEEVENT)
+PERFLIBS = $(LIB_FILE) $(LIBAPIKFS) $(LIBTRACEEVENT) -lcrypto
+

This should be guarded by a feature test.

Quoted 3 lines
tools/perf/Makefile.perf +0 -0
 # We choose to avoid "if .. else if .. else .. endif endif"
 # because maintaining the nesting to match is a pain.  If

[SNIP]

Quoted 15 lines
tools/perf/Makefile.perf +15 -0
+static int jit_repipe_code_load(struct perf_inject *inject, union jr_entry *jr)
+{
+	struct perf_sample sample;
+	union perf_event *event;
+	unsigned long code, addr;
+	size_t size;
+	const char *sym;
+	uint32_t count;
+	int ret, csize;
+	pid_t pid;
+	struct {
+		u32 pid, tid;
+		u64 time;
+	} *id;
+

tools/perf/Makefile.perf

Quoted 10 lines
tools/perf/Makefile.perf +10 -0
+	pid   = jr->load.pid;
+	csize = jr->load.code_size;
+	addr  = jr->load.code_addr;
+	sym   = (void *)((unsigned long)jr + sizeof(jr->load));
+	code  = (unsigned long)jr + jr->load.p.total_size - csize;
+	count = jr->load.code_index;
+
+	/*
+	 * +16 to account for sample_id_all (hack)
+	 */

Why not use perf_evlist__id_hdr_size() ?

Quoted 40 lines
tools/perf/Makefile.perf +40 -0
+	event = malloc(sizeof(*event) + 16);
+	if (!event)
+		return -1;
+
+	memset(event, 0, sizeof(*event));
+
+	size = snprintf(event->mmap.filename, PATH_MAX, "%s/jitted-%d-%u",
+		 inject->jit_dir,
+		 pid,
+		 count) + 1;
+	size = PERF_ALIGN(size, sizeof(u64));
+	if (!inject->jit_disable_elf) {
+		ret = jit_emit_elf(event->mmap.filename, sym, code, csize);
+		if (ret) {
+			free(event);
+			return -1;
+		}
+	}
+
+	event->mmap.header.type = PERF_RECORD_MMAP;
+	event->mmap.header.misc = PERF_RECORD_MISC_USER;
+	//event->mmap.header.size = sizeof(event->mmap) + 16;
+	event->mmap.header.size = (sizeof(event->mmap) -
+			(sizeof(event->mmap.filename) - size) + 16); //machine->id_hdr_size);
+	event->mmap.pgoff = 0;
+	event->mmap.start = addr;
+	event->mmap.len   = csize;
+	event->mmap.pid   = pid;
+	event->mmap.tid   = jr->load.tid;
+
+	id = (void *)((unsigned long)event + event->mmap.header.size - 16);
+	id->pid  = pid;
+	id->tid  = jr->load.tid;
+	id->time = jr->load.p.timestamp;
+
+	memset(&sample, 0, sizeof(sample));
+	sample.time = id->time;
+
+	return perf_event__repipe_synth(&inject->tool, event);
+}

[SNIP]

Quoted 59 lines
tools/perf/util/genelf.c new file +53 -0
new file mode 100644
index 0000000..a6f9e43
@@ -0,0 +1,463 @@
+/*
+ * genelf.c
+ * Copyright (C) 2014, Google, Inc
+ *
+ * Contributed by:
+ * 	Stephane Eranian <[email protected]>
+ *
+ * Released under the GPL v2. (and only v2, not any later version)
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <getopt.h>
+#include <stddef.h>
+#include <libelf.h>
+#include <string.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <err.h>
+
+#include "perf.h"
+#include "genelf.h"
+
+#define JVMTI
+#define BUILD_ID_MD5
+#undef BUILD_ID_SHA	/* does not seem to work well when linked with Java */
+#undef  BUILD_ID_URANDOM	/* different uuid for each run */
+
+#ifdef BUILD_ID_SHA
+#include <openssl/sha.h>
+#endif
+
+#ifdef BUILD_ID_MD5
+#include <openssl/md5.h>
+#endif
+
+#if   defined(__arm__)
+#define GEN_ELF_ARCH	EM_ARM
+#define GEN_ELF_ENDIAN	ELFDATA2LSB
+#define GEN_ELF_CLASS	ELFCLASS32
+#elif defined(__x86_64__)
+#define GEN_ELF_ARCH	EM_X86_64
+#define GEN_ELF_ENDIAN	ELFDATA2LSB
+#define GEN_ELF_CLASS	ELFCLASS64
+#elif defined(__i386__)
+#define GEN_ELF_ARCH	EM_386
+#define GEN_ELF_ENDIAN	ELFDATA2LSB
+#define GEN_ELF_CLASS	ELFCLASS32
+#elif defined(__ppcle__)
+#define GEN_ELF_ARCH	EM_PPC
+#define GEN_ELF_ENDIAN	ELFDATA2LSB
+#define GEN_ELF_CLASS	ELFCLASS64
+#elif defined(__powerpc__)

tools/perf/util/genelf.c line 63

Quoted 10 lines
tools/perf/util/genelf.c new file +10 -0
+#define GEN_ELF_ARCH	EM_PPC64
+#define GEN_ELF_ENDIAN	ELFDATA2MSB
+#define GEN_ELF_CLASS	ELFCLASS64
+#elif defined(__powerpcle__)
+#define GEN_ELF_ARCH	EM_PPC64
+#define GEN_ELF_ENDIAN	ELFDATA2LSB
+#define GEN_ELF_CLASS	ELFCLASS64
+#else
+#error "unsupported architecture"
+#endif

This makes other arch like s390 or sparc cannot build perf, right?

Thanks,
Namhyung

Quoted 20 lines
tools/perf/util/genelf.c new file +20 -0
+
+#if GEN_ELF_CLASS == ELFCLASS64
+#define elf_newehdr	elf64_newehdr
+#define elf_getshdr	elf64_getshdr
+#define Elf_Ehdr	Elf64_Ehdr
+#define Elf_Shdr	Elf64_Shdr
+#define Elf_Sym		Elf64_Sym
+#define ELF_ST_TYPE(a)	ELF64_ST_TYPE(a)
+#define ELF_ST_BIND(a)	ELF64_ST_BIND(a)
+#define ELF_ST_VIS(a)	ELF64_ST_VISIBILITY(a)
+#else
+#define elf_newehdr	elf32_newehdr
+#define elf_getshdr	elf32_getshdr
+#define Elf_Ehdr	Elf32_Ehdr
+#define Elf_Shdr	Elf32_Shdr
+#define Elf_Sym		Elf32_Sym
+#define ELF_ST_TYPE(a)	ELF32_ST_TYPE(a)
+#define ELF_ST_BIND(a)	ELF32_ST_BIND(a)
+#define ELF_ST_VIS(a)	ELF32_ST_VISIBILITY(a)
+#endif
#3 lore raw

Re: [PATCH v3 1/4] perf tools: add Java demangling support

In reply to Namhyung Kim
Quoted 77 lines · Namhyung Kim

Hi Stephane,

Quoted 73 lines · Stephane Eranian

Add Java function descriptor demangling support.
Something bfd cannot do.

Use the JAVA_DEMANGLE_NORET flag to avoid decoding the return type of functions.

---
 tools/perf/Makefile.perf        |   2 +
 tools/perf/util/demangle-java.c | 199 ++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/demangle-java.h |  10 ++
 tools/perf/util/symbol-elf.c    |   3 +
 tools/perf/util/symbol.c        |   6 +-
 5 files changed, 219 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/demangle-java.c
 create mode 100644 tools/perf/util/demangle-java.h
tools/perf/Makefile.perf +2 -0
index aa6a504..53a44be 100644
@@ -331,6 +331,7 @@ LIB_H += ui/ui.h
 LIB_H += util/data.h
 LIB_H += util/kvm-stat.h
 LIB_H += util/thread-stack.h
+LIB_H += util/demangle-java.h
 
 LIB_OBJS += $(OUTPUT)util/abspath.o
 LIB_OBJS += $(OUTPUT)util/alias.o
@@ -410,6 +411,7 @@ LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/cloexec.o
 LIB_OBJS += $(OUTPUT)util/thread-stack.o
+LIB_OBJS += $(OUTPUT)util/demangle-java.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/demangle-java.c new file +30 -0
new file mode 100644
index 0000000..79d5484
@@ -0,0 +1,199 @@
+#include <sys/types.h>
+#include <stdio.h>
+#include <string.h>
+#include "util.h"
+#include "debug.h"
+#include "symbol.h"
+
+#include "demangle-java.h"
+
+enum {
+     MODE_PREFIX=0,
+     MODE_CLASS=1,
+     MODE_FUNC=2,
+     MODE_TYPE=3,
+     MODE_CTYPE=3, /* class arg */
+};
+
+#define BASE_ENT(c, n)       [c-'A']=n
+static const char *base_types['Z'-'A' + 1]={
+     BASE_ENT('B', "byte" ),
+     BASE_ENT('C', "char" ),
+     BASE_ENT('D', "double" ),
+     BASE_ENT('F', "float" ),
+     BASE_ENT('I', "int" ),
+     BASE_ENT('J', "long" ),
+     BASE_ENT('S', "short" ),
+     BASE_ENT('Z', "bool" ),
+};
+
+/*
Quoted 10 lines

tools/perf/util/demangle-java.c line 34

Quoted 4 lines
tools/perf/util/demangle-java.c new file +4 -0
+ * demangle Java symbol between str and end positions and stores
+ * up to maxlen characters into buf. The parser starts in mode.
+ *
+ * Use MODE_PREFIX to process entire prototype till end position

So this still includes the argument (type) right? I just worry about that it might fill up the precious screen width of TUI like C++ symbols did before..

Yes, because just like C++, argument type are part of signature of the function. int Foo(int a) vs int Foo(float). Not the same code.

Quoted 6 lines

tools/perf/util/demangle-java.c line 35

Quoted 2 lines
tools/perf/util/demangle-java.c new file +1 -0
+ * Use MODE_TYPE to process return type if str starts on return type char

What about MODE_CTYPE? I cannot find where it's used..

CTYPE is ued internally.

Quoted 206 lines
Quoted 206 lines
tools/perf/util/demangle-java.c new file +164 -0
+ *
+ *  Return:
+ *   success: buf
+ *   error  : NULL
+ */
+static char *
+__demangle_java_sym(const char *str, const char *end, char *buf, int maxlen, int mode)
+{
+     int rlen = 0;
+     int array = 0;
+     int narg = 0;
+     const char *q;
+
+     if (!end)
+             end = str + strlen(str);
+
+     for (q = str; q != end; q++) {
+
+             if (rlen == (maxlen - 1))
+                     break;
+
+             switch (*q) {
+             case 'L':
+                     if (mode == MODE_PREFIX || mode == MODE_CTYPE) {
+                             if (mode == MODE_CTYPE) {
+                                     if (narg)
+                                             rlen += snprintf(buf+rlen, maxlen - rlen, ", ");
+                                     narg++;
+                             }
+                             rlen += snprintf(buf+rlen, maxlen - rlen, "class ");
+                             if (mode == MODE_PREFIX)
+                                     mode = MODE_CLASS;
+                     } else
+                             buf[rlen++] = *q;
+                     break;
+             case 'B':
+             case 'C':
+             case 'D':
+             case 'F':
+             case 'I':
+             case 'J':
+             case 'S':
+             case 'Z':
+                     if (mode == MODE_TYPE) {
+                             if (narg)
+                                     rlen += snprintf(buf+rlen, maxlen - rlen, ", ");
+                             rlen += snprintf(buf+rlen, maxlen - rlen, "%s", base_types[*q - 'A']);
+                             while(array--)
+                                     rlen += snprintf(buf+rlen, maxlen - rlen, "[]");
+                             array = 0;
+                             narg++;
+                     } else
+                             buf[rlen++] = *q;
+                     break;
+             case 'V':
+                     if (mode == MODE_TYPE) {
+                             rlen += snprintf(buf+rlen, maxlen - rlen, "void");
+                             while(array--)
+                                     rlen += snprintf(buf+rlen, maxlen - rlen, "[]");
+                             array = 0;
+                     } else
+                             buf[rlen++] = *q;
+                     break;
+             case '[':
+                     if (mode != MODE_TYPE)
+                             goto error;
+                     array++;
+                     break;
+             case '(':
+                     if (mode != MODE_FUNC)
+                             goto error;
+                     buf[rlen++] = *q;
+                     mode = MODE_TYPE;
+                     break;
+             case ')':
+                     if (mode != MODE_TYPE)
+                             goto error;
+                     buf[rlen++] = *q;
+                     narg = 0;
+                     break;
+             case ';':
+                     if (mode != MODE_CLASS && mode != MODE_CTYPE)
+                             goto error;
+                     /* safe because at least one other char to process */
+                     if (isalpha(*(q+1)))
+                             rlen += snprintf(buf+rlen, maxlen - rlen, ".");
+                     if (mode == MODE_CLASS)
+                             mode = MODE_FUNC;
+                     else if (mode == MODE_CTYPE)
+                             mode = MODE_TYPE;
+                     break;
+             case '/':
+                     if (mode != MODE_CLASS && mode != MODE_CTYPE)
+                             goto error;
+                     rlen += snprintf(buf+rlen, maxlen - rlen, ".");
+                     break;
+             default :
+                     buf[rlen++] = *q;
+             }
+     }
+     buf[rlen] = '\0';
+     return buf;
+error:
+     return NULL;
+}
+
+/*
+ * Demangle Java function signature (openJDK, not GCJ)
+ * input:
+ *   str: string to parse. String is not modified
+ *    flags: comobination of JAVA_DEMANGLE_* flags to modify demangling
+ * return:
+ *   if input can be demangled, then a newly allocated string is returned.
+ *   if input cannot be demangled, then NULL is returned
+ *
+ * Note: caller is responsible for freeing demangled string
+ */
+char *
+java_demangle_sym(const char *str, int flags)
+{
+     char *buf, *ptr;
+     char *p;
+     size_t len, l1 = 0;
+
+     if (!str)
+             return NULL;
+
+     /* find start of retunr type */
+     p = strrchr(str, ')');
+     if (!p)
+             return NULL;
+
+     /*
+      * expansion factor estimated to 3x
+      */
+     len = strlen(str) * 3 + 1;
+     buf = malloc(len);
+     if (!buf)
+             return NULL;
+
+     buf[0] = '\0';
+     if (!(flags & JAVA_DEMANGLE_NORET)) {
+             /*
+              * get return type first
+              */
+             ptr = __demangle_java_sym(p+1, NULL, buf, len, MODE_TYPE);
+             if (!ptr)
+                     goto error;
+
+             /* add space between return type and function prototype */
+             l1 = strlen(buf);
+             buf[l1++] = ' ';
+     }
+
+     /* process function up to return type */
+     ptr = __demangle_java_sym(str, p + 1, buf + l1, len - l1, MODE_PREFIX);
+     if (!ptr)
+             goto error;
+
+     return buf;
+error:
+     free(buf);
+     return NULL;
+}
tools/perf/util/demangle-java.h new file +10 -0
new file mode 100644
index 0000000..a981c1f
@@ -0,0 +1,10 @@
+#ifndef __PERF_DEMANGLE_JAVA
+#define __PERF_DEMANGLE_JAVA 1
+/*
+ * demangle function flags
+ */
+#define JAVA_DEMANGLE_NORET  0x1 /* do not process return type */
+
+char * java_demangle_sym(const char *str, int flags);
+
+#endif /* __PERF_DEMANGLE_JAVA */
tools/perf/util/symbol-elf.c +3 -0
index b24f9d8..73b3e8f 100644
@@ -6,6 +6,7 @@
 #include <inttypes.h>
 
 #include "symbol.h"
+#include "demangle-java.h"
 #include "machine.h"
 #include "vdso.h"
 #include <symbol/kallsyms.h>
@@ -1019,6 +1020,8 @@ int dso__load_sym(struct dso *dso, struct map *map,
                              demangle_flags = DMGL_PARAMS | DMGL_ANSI;
 
                      demangled = bfd_demangle(NULL, elf_name, demangle_flags);
+                     if (demangled == NULL)
+                             demangled = java_demangle_sym(elf_name, JAVA_DEMANGLE_NORET);
                      if (demangled != NULL)
                              elf_name = demangled;
              }
tools/perf/util/symbol.c +0 -0
index a690668..3bc779b 100644
Quoted 10 lines

tools/perf/util/symbol.c line 12

Quoted 5 lines
tools/perf/util/symbol.c +1 -0
@@ -9,6 +9,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <inttypes.h>
+#include <bfd.h>

Wouldn't it break build on systems don't have the bfd devel package? And I don't see why it's needed here..

I will fix that in v4.

Quoted 16 lines
Quoted 16 lines
tools/perf/util/symbol.c +1 -0
 #include "build-id.h"
 #include "util.h"
 #include "debug.h"
@@ -1257,6 +1258,7 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
              u64 start, size;
              struct symbol *sym;
              int line_len, len;
+             char *name;
 
              line_len = getline(&line, &n, file);
              if (line_len < 0)
@@ -1279,7 +1281,9 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
              if (len + 2 >= line_len)
                      continue;
 
Quoted 7 lines

tools/perf/util/symbol.c static int dso__load_perf_map(struct dso *dso, struct map *map, · line 1286

Quoted 4 lines
tools/perf/util/symbol.c +3 -1
-             sym = symbol__new(start, size, STB_GLOBAL, line + len);
+             name = line + len;
+
+             sym = symbol__new(start, size, STB_GLOBAL, name);

I think this file is not a part of this changeset.

Removed.

Quoted 11 lines

Thanks,
Namhyung

Quoted 6 lines
tools/perf/util/symbol.c +0 -1
              if (sym == NULL)
                      goto out_delete_line;
--

1.9.1