perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <jvmti.h>
|
2015-11-30 02:02:23 -07:00
|
|
|
#include <jvmticmlr.h>
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include "jvmti_agent.h"
|
|
|
|
|
|
|
|
static int has_line_numbers;
|
|
|
|
void *jvmti_agent;
|
|
|
|
|
2015-11-30 02:02:23 -07:00
|
|
|
static jvmtiError
|
|
|
|
do_get_line_numbers(jvmtiEnv *jvmti, void *pc, jmethodID m, jint bci,
|
|
|
|
jvmti_line_info_t *tab, jint *nr)
|
|
|
|
{
|
|
|
|
jint i, lines = 0;
|
|
|
|
jint nr_lines = 0;
|
|
|
|
jvmtiLineNumberEntry *loc_tab = NULL;
|
|
|
|
jvmtiError ret;
|
|
|
|
|
|
|
|
ret = (*jvmti)->GetLineNumberTable(jvmti, m, &nr_lines, &loc_tab);
|
|
|
|
if (ret != JVMTI_ERROR_NONE)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_lines; i++) {
|
|
|
|
if (loc_tab[i].start_location < bci) {
|
|
|
|
tab[lines].pc = (unsigned long)pc;
|
|
|
|
tab[lines].line_number = loc_tab[i].line_number;
|
|
|
|
tab[lines].discrim = 0; /* not yet used */
|
|
|
|
lines++;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)loc_tab);
|
|
|
|
*nr = lines;
|
|
|
|
return JVMTI_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static jvmtiError
|
|
|
|
get_line_numbers(jvmtiEnv *jvmti, const void *compile_info, jvmti_line_info_t **tab, int *nr_lines)
|
|
|
|
{
|
|
|
|
const jvmtiCompiledMethodLoadRecordHeader *hdr;
|
|
|
|
jvmtiCompiledMethodLoadInlineRecord *rec;
|
|
|
|
jvmtiLineNumberEntry *lne = NULL;
|
|
|
|
PCStackInfo *c;
|
|
|
|
jint nr, ret;
|
|
|
|
int nr_total = 0;
|
|
|
|
int i, lines_total = 0;
|
|
|
|
|
|
|
|
if (!(tab && nr_lines))
|
|
|
|
return JVMTI_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Phase 1 -- get the number of lines necessary
|
|
|
|
*/
|
|
|
|
for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
|
|
|
|
if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
|
|
|
|
rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
|
|
|
|
for (i = 0; i < rec->numpcs; i++) {
|
|
|
|
c = rec->pcinfo + i;
|
|
|
|
nr = 0;
|
|
|
|
/*
|
|
|
|
* unfortunately, need a tab to get the number of lines!
|
|
|
|
*/
|
|
|
|
ret = (*jvmti)->GetLineNumberTable(jvmti, c->methods[0], &nr, &lne);
|
|
|
|
if (ret == JVMTI_ERROR_NONE) {
|
|
|
|
/* free what was allocated for nothing */
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)lne);
|
|
|
|
nr_total += (int)nr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nr_total == 0)
|
|
|
|
return JVMTI_ERROR_NOT_FOUND;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Phase 2 -- allocate big enough line table
|
|
|
|
*/
|
|
|
|
*tab = malloc(nr_total * sizeof(**tab));
|
|
|
|
if (!*tab)
|
|
|
|
return JVMTI_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
for (hdr = compile_info; hdr != NULL; hdr = hdr->next) {
|
|
|
|
if (hdr->kind == JVMTI_CMLR_INLINE_INFO) {
|
|
|
|
rec = (jvmtiCompiledMethodLoadInlineRecord *)hdr;
|
|
|
|
for (i = 0; i < rec->numpcs; i++) {
|
|
|
|
c = rec->pcinfo + i;
|
|
|
|
nr = 0;
|
|
|
|
ret = do_get_line_numbers(jvmti, c->pc,
|
|
|
|
c->methods[0],
|
|
|
|
c->bcis[0],
|
|
|
|
*tab + lines_total,
|
|
|
|
&nr);
|
|
|
|
if (ret == JVMTI_ERROR_NONE)
|
|
|
|
lines_total += nr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*nr_lines = lines_total;
|
|
|
|
return JVMTI_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
static void JNICALL
|
|
|
|
compiled_method_load_cb(jvmtiEnv *jvmti,
|
|
|
|
jmethodID method,
|
|
|
|
jint code_size,
|
|
|
|
void const *code_addr,
|
|
|
|
jint map_length,
|
|
|
|
jvmtiAddrLocationMap const *map,
|
2015-11-30 02:02:23 -07:00
|
|
|
const void *compile_info)
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
{
|
2015-11-30 02:02:23 -07:00
|
|
|
jvmti_line_info_t *line_tab = NULL;
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
jclass decl_class;
|
|
|
|
char *class_sign = NULL;
|
|
|
|
char *func_name = NULL;
|
|
|
|
char *func_sign = NULL;
|
|
|
|
char *file_name= NULL;
|
|
|
|
char fn[PATH_MAX];
|
|
|
|
uint64_t addr = (uint64_t)(uintptr_t)code_addr;
|
|
|
|
jvmtiError ret;
|
2015-11-30 02:02:23 -07:00
|
|
|
int nr_lines = 0; /* in line_tab[] */
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
size_t len;
|
|
|
|
|
|
|
|
ret = (*jvmti)->GetMethodDeclaringClass(jvmti, method,
|
|
|
|
&decl_class);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: cannot get declaring class");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (has_line_numbers && map && map_length) {
|
2015-11-30 02:02:23 -07:00
|
|
|
ret = get_line_numbers(jvmti, compile_info, &line_tab, &nr_lines);
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: cannot get line table for method");
|
2015-11-30 02:02:23 -07:00
|
|
|
nr_lines = 0;
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-30 02:02:23 -07:00
|
|
|
ret = (*jvmti)->GetSourceFileName(jvmti, decl_class, &file_name);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: cannot get source filename ret=%d", ret);
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
ret = (*jvmti)->GetClassSignature(jvmti, decl_class,
|
|
|
|
&class_sign, NULL);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: getclassignature failed");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (*jvmti)->GetMethodName(jvmti, method, &func_name,
|
|
|
|
&func_sign, NULL);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: failed getmethodname");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Assume path name is class hierarchy, this is a common practice with Java programs
|
|
|
|
*/
|
|
|
|
if (*class_sign == 'L') {
|
|
|
|
int j, i = 0;
|
|
|
|
char *p = strrchr(class_sign, '/');
|
|
|
|
if (p) {
|
|
|
|
/* drop the 'L' prefix and copy up to the final '/' */
|
|
|
|
for (i = 0; i < (p - class_sign); i++)
|
|
|
|
fn[i] = class_sign[i+1];
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* append file name, we use loops and not string ops to avoid modifying
|
|
|
|
* class_sign which is used later for the symbol name
|
|
|
|
*/
|
|
|
|
for (j = 0; i < (PATH_MAX - 1) && file_name && j < strlen(file_name); j++, i++)
|
|
|
|
fn[i] = file_name[j];
|
|
|
|
fn[i] = '\0';
|
|
|
|
} else {
|
|
|
|
/* fallback case */
|
|
|
|
strcpy(fn, file_name);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* write source line info record if we have it
|
|
|
|
*/
|
2015-11-30 02:02:23 -07:00
|
|
|
if (jvmti_write_debug_info(jvmti_agent, addr, fn, line_tab, nr_lines))
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
warnx("jvmti: write_debug_info() failed");
|
|
|
|
|
|
|
|
len = strlen(func_name) + strlen(class_sign) + strlen(func_sign) + 2;
|
|
|
|
{
|
|
|
|
char str[len];
|
|
|
|
snprintf(str, len, "%s%s%s", class_sign, func_name, func_sign);
|
2015-11-30 02:02:23 -07:00
|
|
|
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
if (jvmti_write_code(jvmti_agent, str, addr, code_addr, code_size))
|
|
|
|
warnx("jvmti: write_code() failed");
|
|
|
|
}
|
|
|
|
error:
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)func_name);
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)func_sign);
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)class_sign);
|
|
|
|
(*jvmti)->Deallocate(jvmti, (unsigned char *)file_name);
|
2015-11-30 02:02:23 -07:00
|
|
|
free(line_tab);
|
perf tools: add JVMTI agent library
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 .......
See the "Committer Notes" below on how to build it.
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 jitdump information must be injected into the perf.data file
using:
$ perf inject --jit -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 the CLOCK_MONOTONIC as the time
source to timestamp jit samples. To correlate with perf_events samples,
it needs to run on kernel 4.0.0-rc5+ or later with the following commit
from Peter Zijlstra:
34f439278cef ("perf: Add per event clockid support")
With this patch recording jitted code is done as follows:
$ perf record -k mono -- java -agentpath:libjvmti.so .......
--------------------------------------------------------------------------
Committer Notes:
Extended testing instructions:
$ cd tools/perf/jvmti/
$ dnf install java-devel
$ make
Then, create some simple java stuff to record some samples:
$ cat hello.java
public class hello {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
$ javac hello.java
$ java hello
Hello, World
$
And then record it using this jvmti thing:
$ perf record -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hello
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jit-1908.dump
Hello, World
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.030 MB perf.data (268 samples) ]
$
Now lets insert the PERF_RECORD_MMAP2 records to point jitted mmaps to
files created by the agent:
$ perf inject --jit -i perf.data -o perf.data.jitted
And finally see that it did its job:
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | tail -5
79197149129422 0xfe10 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428bd60(0x80) @ 0x40 fd:02 1840554 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-283.so
79197149235701 0xfeb0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428ba60(0x180) @ 0x40 fd:02 1840555 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-284.so
79197149250558 0xff50 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b860(0x180) @ 0x40 fd:02 1840556 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-285.so
79197149714746 0xfff0 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b660(0x180) @ 0x40 fd:02 1840557 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-286.so
79197149806558 0x10090 [0xa0]: PERF_RECORD_MMAP2 1908/1923: [0x7f172428b460(0x180) @ 0x40 fd:02 1840558 1]: --xs /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-287.so
$
So:
$ perf report -D -i perf.data | grep PERF_RECORD_MMAP2 | wc -l
Failed to open /tmp/perf-1908.map, continuing without symbols
21
$ perf report -D -i perf.data.jitted | grep PERF_RECORD_MMAP2 | wc -l
307
$ echo $((307 - 21))
286
$
286 extra PERF_RECORD_MMAP2 records.
All for thise tiny, with just one function, ELF files:
$ file /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
/home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), corrupted program header size, BuildID[sha1]=ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f, not stripped
$ readelf -sw /home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so
Symbol table '.symtab' contains 2 entries:
Num: Value Size Type Bind Vis Ndx Name
0: 0000000000000000 0 NOTYPE LOCAL DEFAULT UND
1: 0000000000000040 9 FUNC LOCAL DEFAULT 1 atomic_cmpxchg_long
$
Inserted into the build-id cache:
$ ls -la ~/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f
lrwxrwxrwx. 1 acme acme 111 Feb 5 11:30 /home/acme/.debug/.build-id/ae/54a2ebc3ecf0ba547bfc8cabdea1519df5203f -> ../../home/acme/.debug/jit/java-jit-20160205.XXWIEDls/jitted-1908-9.so/ae54a2ebc3ecf0ba547bfc8cabdea1519df5203f
Note: check why 'file' reports that 'corrupted program header size'.
With a stupid java hog to do some profiling:
$ cat hog.java
public class hog {
private static double do_something_else(int i) {
double total = 0;
while (i > 0) {
total += Math.log(i--);
}
return total;
}
private static double do_something(int i) {
double total = 0;
while (i > 0) {
total += Math.sqrt(i--) + do_something_else(i / 100);
}
return total;
}
public static void main(String[] args) {
System.out.println(String.format("%s=%f & %f", args[0],
do_something(Integer.parseInt(args[0])),
do_something_else(Integer.parseInt(args[1]))));
}
}
$ javac hog.java
$ perf record -F 10000 -g -k mono java -agentpath:/home/acme/git/linux/tools/perf/jvmti/libjvmti.so hog 100000 2345000
java: jvmti: jitdump in /home/acme/.debug/jit/java-jit-20160205.XX4sqd14/jit-8670.dump
100000=291561592.669602 & 32050989.778714
[ perf record: Woken up 6 times to write data ]
[ perf record: Captured and wrote 1.536 MB perf.data (12538 samples) ]
$ perf inject --jit -i perf.data -o perf.data.jitted
Looking at the 'perf report' TUI, at one expanded callchain leading
to the jitted code:
$ perf report --no-children -i perf.data.jitted
Samples: 12K of event 'cycles:pp', Event count (approx.): 3829569932
Overhead Comm Shared Object Symbol
- 93.38% java jitted-8670-291.so [.] class hog.do_something_else(int)
class hog.do_something_else(int)
- Interpreter
- 75.86% call_stub
JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
- 17.52% JavaCalls::call_helper
jni_invoke_static
jni_CallStaticVoidMethod
JavaMain
start_thread
Signed-off-by: Stephane Eranian <eranian@google.com>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Carl Love <cel@us.ibm.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: John McCutchan <johnmccutchan@google.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Pawel Moll <pawel.moll@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Sonny Rao <sonnyrao@chromium.org>
Cc: Sukadev Bhattiprolu <sukadev@linux.vnet.ibm.com>
Link: http://lkml.kernel.org/r/1448874143-7269-4-git-send-email-eranian@google.com
[ Made it build on fedora23, added some build/usage instructions ]
[ Check if filename != NULL in compiled_method_load_cb, fixing segfault ]
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-30 02:02:22 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void JNICALL
|
|
|
|
code_generated_cb(jvmtiEnv *jvmti,
|
|
|
|
char const *name,
|
|
|
|
void const *code_addr,
|
|
|
|
jint code_size)
|
|
|
|
{
|
|
|
|
uint64_t addr = (uint64_t)(unsigned long)code_addr;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = jvmti_write_code(jvmti_agent, name, addr, code_addr, code_size);
|
|
|
|
if (ret)
|
|
|
|
warnx("jvmti: write_code() failed for code_generated");
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL
|
|
|
|
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved __unused)
|
|
|
|
{
|
|
|
|
jvmtiEventCallbacks cb;
|
|
|
|
jvmtiCapabilities caps1;
|
|
|
|
jvmtiJlocationFormat format;
|
|
|
|
jvmtiEnv *jvmti = NULL;
|
|
|
|
jint ret;
|
|
|
|
|
|
|
|
jvmti_agent = jvmti_open();
|
|
|
|
if (!jvmti_agent) {
|
|
|
|
warnx("jvmti: open_agent failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Request a JVMTI interface version 1 environment
|
|
|
|
*/
|
|
|
|
ret = (*jvm)->GetEnv(jvm, (void *)&jvmti, JVMTI_VERSION_1);
|
|
|
|
if (ret != JNI_OK) {
|
|
|
|
warnx("jvmti: jvmti version 1 not supported");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* acquire method_load capability, we require it
|
|
|
|
* request line numbers (optional)
|
|
|
|
*/
|
|
|
|
memset(&caps1, 0, sizeof(caps1));
|
|
|
|
caps1.can_generate_compiled_method_load_events = 1;
|
|
|
|
|
|
|
|
ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: acquire compiled_method capability failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ret = (*jvmti)->GetJLocationFormat(jvmti, &format);
|
|
|
|
if (ret == JVMTI_ERROR_NONE && format == JVMTI_JLOCATION_JVMBCI) {
|
|
|
|
memset(&caps1, 0, sizeof(caps1));
|
|
|
|
caps1.can_get_line_numbers = 1;
|
|
|
|
caps1.can_get_source_file_name = 1;
|
|
|
|
ret = (*jvmti)->AddCapabilities(jvmti, &caps1);
|
|
|
|
if (ret == JVMTI_ERROR_NONE)
|
|
|
|
has_line_numbers = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(&cb, 0, sizeof(cb));
|
|
|
|
|
|
|
|
cb.CompiledMethodLoad = compiled_method_load_cb;
|
|
|
|
cb.DynamicCodeGenerated = code_generated_cb;
|
|
|
|
|
|
|
|
ret = (*jvmti)->SetEventCallbacks(jvmti, &cb, sizeof(cb));
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: cannot set event callbacks");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
|
|
|
|
JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: setnotification failed for method_load");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
|
|
|
|
JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL);
|
|
|
|
if (ret != JVMTI_ERROR_NONE) {
|
|
|
|
warnx("jvmti: setnotification failed on code_generated");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL
|
|
|
|
Agent_OnUnload(JavaVM *jvm __unused)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = jvmti_close(jvmti_agent);
|
|
|
|
if (ret)
|
|
|
|
errx(1, "Error: op_close_agent()");
|
|
|
|
}
|