All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v8 0/4] perf: add support for profiling jitted code
@ 2015-11-30  9:02 Stephane Eranian
  2015-11-30  9:02 ` [PATCH v8 1/4] perf tools: add Java demangling support Stephane Eranian
                   ` (3 more replies)
  0 siblings, 4 replies; 27+ messages in thread
From: Stephane Eranian @ 2015-11-30  9:02 UTC (permalink / raw)
  To: linux-kernel
  Cc: acme, peterz, mingo, ak, jolsa, namhyung, cel, sukadev, sonnyrao,
	johnmccutchan, dsahern, adrian.hunter, pawel.moll

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 code movements
   - no files in /tmp
   - meta-data file is unique to each run
   - no changes to perf report/annotate
   - support per-thread and system-wide profiling
   - support monitoring of multiple simultaneous Jit runtimes
   - source level view in perf annotate
   - works on x86_64, i386, arm32, arm64

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 to support
symbolization, annotation, source lines correlation of the samples.
Those modifications are relatively straighforward, some have been 
implemented in 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 is possible
since Linux 4.1 where the kernel supports per event timestamp clock source.
In the case of the JVMTI agent, the clock used is CLOCK_MONOTONIC, thus
perf record is invoked with -k mono such that it matches the agent.

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
injected MMAP records will point to these 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. Binutils tools can decode
the ELF images easily.

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 and
source level profile.

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

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 switched 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.

In V4, we rebased to 4.0-rc5. We also simplified the process by
getting rid of the requirement to pass the jitdump file name to
perf inject. Now, perf injects automtically detects if jitdumps
were generated and it merges the relevant meta-data. This is
accomplished by having the jit runtime mmap the jitdump file
for the purpose of creating a MMAP record in the perf.data file.
That MMAP contains all the info to locate the jitdump file and
generate the ELF images for jitted functions.

In V5, we rebase to acme's perf/core branch (instead of tip.git).
We fixed some bswap issues, switched to using scnprintf() and fixed
formatting issues. Also made sure all the files were included in the
patches. We also fix one error message in the JVMTI agent.


In V6, we switched back to using tip.git to leverage PeterZ's clockid
patch for perf_events in 4.0.0-rc6. Clock source can now be specified
per event and they are connected with the MONOTONIC Posix clock. We
leverage this extension to timestamp samples in the jit runtime and
correlate them with perf samples. Notice the -k mono option in perf
record example below.

In V7, we rebased to 4.3.0-rc3 using tip.git (at commit 0dc7757).
We fixed several issues in the agent. We also added source line
information in the jitdump file from the JVMTI agent. This is
still experimental and probably has some issues. The source
line info is encoded in DWARF2 format in each ELF image. The
code to do this is leveraged from Oprofile with some fixes
and cleanups.

In V8, we rebased to 4.4.0-rc2 using tip.git (at commit 9962da9).
We received great contributions from Andrian Hunter (adrian.hunter@intel.com).
He has fixed several issues in the jitdump injection code, see changelog
of the patch. The jitdump header has a new flags field to be used for
Intel PT. The series has been verified to work on x86_64, i386, arm32
and arm64 running 4.1 or later.

To use the new feature:
  - need to run with 4.1 or later
  - compile perf
  - cd tools/perf/jvmti; make; install wherever is appropriate

  Example using openJDK:
   $ perf record -k mono java -agentpath:libjvmti.so my_class
   $ perf inject -i perf.data -jit -o perf.data.jitted
   $ perf report -i perf.data.jitted

Thanks to all the contributors and testers. Special thanks
to PeterZ for adding the clock source to perf_events and solving
the problem of common timesource for user and kernel level samples.
Thanks to the Oprofile authors for the DWARF2 source line code
generation.

Special thanks to Adrian Hunter for his many bug fixes and improvements
for the V8 version of this series.

Enjoy,

Stephane Eranian (4):
  perf tools: add Java demangling support
  perf inject: add jitdump mmap injection support
  perf tools: add JVMTI agent library
  perf/jit: add source line info support

 tools/build/Makefile.feature             |   2 +
 tools/build/feature/Makefile             |   4 +
 tools/build/feature/test-all.c           |   5 +
 tools/build/feature/test-libcrypto.c     |  17 +
 tools/perf/Documentation/perf-inject.txt |   7 +
 tools/perf/builtin-inject.c              |  93 +++++
 tools/perf/config/Makefile               |  11 +
 tools/perf/jvmti/Makefile                |  73 ++++
 tools/perf/jvmti/jvmti_agent.c           | 465 +++++++++++++++++++++
 tools/perf/jvmti/jvmti_agent.h           |  36 ++
 tools/perf/jvmti/libjvmti.c              | 304 ++++++++++++++
 tools/perf/util/Build                    |   7 +
 tools/perf/util/demangle-java.c          | 199 +++++++++
 tools/perf/util/demangle-java.h          |  10 +
 tools/perf/util/genelf.c                 | 449 +++++++++++++++++++++
 tools/perf/util/genelf.h                 |  67 +++
 tools/perf/util/genelf_debug.c           | 610 ++++++++++++++++++++++++++++
 tools/perf/util/jit.h                    |  15 +
 tools/perf/util/jitdump.c                | 672 +++++++++++++++++++++++++++++++
 tools/perf/util/jitdump.h                | 124 ++++++
 tools/perf/util/symbol-elf.c             |   3 +
 21 files changed, 3173 insertions(+)
 create mode 100644 tools/build/feature/test-libcrypto.c
 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/genelf_debug.c
 create mode 100644 tools/perf/util/jit.h
 create mode 100644 tools/perf/util/jitdump.c
 create mode 100644 tools/perf/util/jitdump.h

-- 
1.9.1


^ permalink raw reply	[flat|nested] 27+ messages in thread
* Re: [PATCH v8 2/4] perf inject: add jitdump mmap injection support
@ 2016-01-22 22:13 Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 27+ messages in thread
From: Arnaldo Carvalho de Melo @ 2016-01-22 22:13 UTC (permalink / raw)
  To: Stephane Eranian
  Cc: LKML, Peter Zijlstra, mingo, ak, Jiri Olsa, Namhyung Kim,
	Rose Belcher, Sukadev Bhattiprolu, Sonny Rao, John Mccutchan,
	David Ahern, Adrian Hunter, Pawel Moll

Em Fri, Jan 22, 2016 at 01:22:51PM -0800, Stephane Eranian escreveu:
> On Fri, Jan 22, 2016 at 12:44 PM, Arnaldo Carvalho de Melo
> <acme@redhat.com> wrote:
> > Em Mon, Nov 30, 2015 at 10:02:21AM +0100, Stephane Eranian escreveu:
> >> This patch adds a --jit/-j 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:
> >> $ perf record -k mono -- java -agentpath:libpjvmti.so java_class
> >> $ perf inject --jit -i perf.data -o perf.data.jitted
> >> $ perf report -i perf.data.jitted
> >
> > So, it fails 'make -C tools/perf build-test', specifically the one where
> > we ask for a NO_LIBELF build, trying to fix:
> >
> I have rebase to tip.git last night. Will try your branch today.
> Will add a couple of minor adjustments and also better documentation
> on how to use it.

Ok, but please work on top of my branch, that I'll push soon, for
instance, I had to fold the patch below, to fix the NO_LIBELF=1 case,
so, when one builds that way perf inject -h will say:

[acme@jouet linux]$ tools/perf/perf inject -h

 Usage: perf inject [<options>]

    -b, --build-ids       Inject build-ids into the output stream
    -f, --force           don't complain, do it
    -i, --input <file>    input file name
    -j, --jit             merge jitdump files into perf.data file
                          (not built-in because NO_LIBELF=1)
    -o, --output <file>   output file name
    -s, --sched-stat      Merge sched-stat and sched-switch for getting
events where and how long tasks slept
    -v, --verbose         be more verbose (show build ids, etc)
        --itrace[=<opts>]
                          Instruction Tracing options
        --kallsyms <file>
                          kallsyms pathname
        --strip           strip non-synthesized events (use with
--itrace)

[acme@jouet linux]$ 

I did some other changes and carved out two bits into separate patches,
will soon push what I have to a perf/jit branch in my tree so that you
can take a look.

- Arnaldo


---------------------------------------------------------


diff --git a/tools/perf/builtin-inject.c b/tools/perf/builtin-inject.c
index 606653f30759..b38445f08c2f 100644
--- a/tools/perf/builtin-inject.c
+++ b/tools/perf/builtin-inject.c
@@ -73,12 +73,14 @@ static int perf_event__repipe_oe_synth(struct perf_tool *tool,
 	return perf_event__repipe_synth(tool, event);
 }
 
+#ifdef HAVE_LIBELF_SUPPORT
 static int perf_event__drop_oe(struct perf_tool *tool __maybe_unused,
 			       union perf_event *event __maybe_unused,
 			       struct ordered_events *oe __maybe_unused)
 {
 	return 0;
 }
+#endif
 
 static int perf_event__repipe_op2_synth(struct perf_tool *tool,
 					union perf_event *event,
@@ -243,6 +245,7 @@ static int perf_event__repipe_mmap(struct perf_tool *tool,
 	return err;
 }
 
+#ifdef HAVE_LIBELF_SUPPORT
 static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
 				       union perf_event *event,
 				       struct perf_sample *sample,
@@ -261,6 +264,7 @@ static int perf_event__jit_repipe_mmap(struct perf_tool *tool,
 	}
 	return perf_event__repipe_mmap(tool, event, sample, machine);
 }
+#endif
 
 static int perf_event__repipe_mmap2(struct perf_tool *tool,
 				   union perf_event *event,
@@ -275,6 +279,7 @@ static int perf_event__repipe_mmap2(struct perf_tool *tool,
 	return err;
 }
 
+#ifdef HAVE_LIBELF_SUPPORT
 static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
 					union perf_event *event,
 					struct perf_sample *sample,
@@ -293,6 +298,7 @@ static int perf_event__jit_repipe_mmap2(struct perf_tool *tool,
 	}
 	return perf_event__repipe_mmap2(tool, event, sample, machine);
 }
+#endif
 
 static int perf_event__repipe_fork(struct perf_tool *tool,
 				   union perf_event *event,
@@ -711,6 +717,7 @@ static int __cmd_inject(struct perf_inject *inject)
 	return ret;
 }
 
+#ifdef HAVE_LIBELF_SUPPORT
 static int
 jit_validate_events(struct perf_session *session)
 {
@@ -725,6 +732,7 @@ jit_validate_events(struct perf_session *session)
 	}
 	return 0;
 }
+#endif
 
 int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 {
@@ -765,7 +773,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 	};
 	int ret;
 
-	const struct option options[] = {
+	struct option options[] = {
 		OPT_BOOLEAN('b', "build-ids", &inject.build_ids,
 			    "Inject build-ids into the output stream"),
 		OPT_STRING('i', "input", &inject.input_name, "file",
@@ -792,7 +800,9 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 		"perf inject [<options>]",
 		NULL
 	};
-
+#ifndef HAVE_LIBELF_SUPPORT
+	set_option_nobuild(options, 'j', "jit", "NO_LIBELF=1", true);
+#endif
 	argc = parse_options(argc, argv, options, inject_usage, 0);
 
 	/*
@@ -828,7 +838,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 		inject.tool.ordered_events = true;
 		inject.tool.ordering_requires_timestamps = true;
 	}
-
+#ifdef HAVE_LIBELF_SUPPORT
 	if (inject.jit_mode) {
 		/*
 		 * validate event is using the correct clockid
@@ -847,7 +857,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 		 */
 		inject.tool.finished_round = perf_event__drop_oe;
 	}
-
+#endif
 	ret = symbol__init(&inject.session->header.env);
 	if (ret < 0)
 		goto out_delete;
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 9ef707198941..52a4a806ee2f 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -106,8 +106,8 @@ libperf-y += scripting-engines/
 libperf-$(CONFIG_ZLIB) += zlib.o
 libperf-$(CONFIG_LZMA) += lzma.o
 libperf-y += demangle-java.o
-libperf-y += jitdump.o
-libperf-y += genelf.o
+libperf-$(CONFIG_LIBELF) += jitdump.o
+libperf-$(CONFIG_LIBELF) += genelf.o
 
 CFLAGS_config.o   += -DETC_PERFCONFIG="BUILD_STR($(ETC_PERFCONFIG_SQ))"
 

----- End forwarded message -----

^ permalink raw reply related	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2016-02-15 17:14 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-30  9:02 [PATCH v8 0/4] perf: add support for profiling jitted code Stephane Eranian
2015-11-30  9:02 ` [PATCH v8 1/4] perf tools: add Java demangling support Stephane Eranian
2016-02-09 12:14   ` [tip:perf/core] perf symbols: " tip-bot for Stephane Eranian
2015-11-30  9:02 ` [PATCH v8 2/4] perf inject: add jitdump mmap injection support Stephane Eranian
2016-01-22 20:44   ` Arnaldo Carvalho de Melo
2016-01-22 21:22     ` Stephane Eranian
     [not found]       ` <20160122215542.GK4034@kernel.org>
     [not found]         ` <20160122220929.GL4034@kernel.org>
2016-01-22 22:10           ` Arnaldo Carvalho de Melo
2016-02-04 21:53       ` Arnaldo Carvalho de Melo
2016-02-04 23:02         ` Stephane Eranian
2016-02-05 13:47           ` Arnaldo Carvalho de Melo
2016-02-05 13:51             ` Arnaldo Carvalho de Melo
2016-02-05 13:57               ` Arnaldo Carvalho de Melo
2016-02-05 14:24                 ` Arnaldo Carvalho de Melo
2016-02-08 18:53                   ` Stephane Eranian
2016-02-11 22:16                     ` Arnaldo Carvalho de Melo
2016-02-12 20:32                       ` Stephane Eranian
2016-02-12 20:43                         ` Arnaldo Carvalho de Melo
2016-02-15  2:16                           ` Stephane Eranian
2016-02-15 17:14                             ` Arnaldo Carvalho de Melo
2016-02-09 12:14   ` [tip:perf/core] perf build: Add libcrypto feature detection tip-bot for Stephane Eranian
2016-02-09 12:15   ` [tip:perf/core] perf inject: Make sure mmap records are ordered when injecting build_ids tip-bot for Arnaldo Carvalho de Melo
2016-02-09 12:15   ` [tip:perf/core] perf inject: Add jitdump mmap injection support tip-bot for Stephane Eranian
2015-11-30  9:02 ` [PATCH v8 3/4] perf tools: add JVMTI agent library Stephane Eranian
2016-02-09 12:16   ` [tip:perf/core] " tip-bot for Stephane Eranian
2015-11-30  9:02 ` [PATCH v8 4/4] perf/jit: add source line info support Stephane Eranian
2016-02-09 12:16   ` [tip:perf/core] perf jit: " tip-bot for Stephane Eranian
2016-01-22 22:13 [PATCH v8 2/4] perf inject: add jitdump mmap injection support Arnaldo Carvalho de Melo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.