linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Adrian Hunter <adrian.hunter@intel.com>
To: Stephane Eranian <eranian@google.com>, linux-kernel@vger.kernel.org
Cc: acme@redhat.com, peterz@infradead.org, mingo@elte.hu,
	ak@linux.intel.com, jolsa@redhat.com, namhyung@kernel.org,
	cel@us.ibm.com, dsahern@gmail.com, johnmccutchan@google.com,
	brendan.d.gregg@gmail.com
Subject: Re: [PATCH v7 3/5] perf inject: add jitdump mmap injection support
Date: Fri, 9 Oct 2015 16:17:22 +0300	[thread overview]
Message-ID: <5617BE62.8070409@intel.com> (raw)
In-Reply-To: <1443681949-6534-4-git-send-email-eranian@google.com>

On 01/10/15 09:45, Stephane Eranian wrote:
> This patch adds a --jit option to perf inject.
> 
> This options injects MMAP records into the perf.data
> file to cover the jitted code mmaps. It also emits
> ELF images for each function in the jidump file.
> Those images are created where the jitdump file is.
> The MMAP records point to that location as well.
> 
> Typical flow:
> $ perf record -k mono -- java -agentpath:libpjvmti.so java_class
> $ perf inject --jit -i perf.data -o perf.data.jitted
> $ perf report -i perf.data.jitted
> 
> Note that jitdump.h support is not limited to Java, it works with
> any jitted environment modified to emit the jitdump file format,
> include those where code can be jitted multiple times and moved
> around.
> 
> The jitdump.h format is adapted from the Oprofile project.
> 
> The genelf.c (ELF binary generation) depends on MD5 hash
> encoding for the buildid. To enable this, libssl-dev must
> be installed. If not, then genelf.c defaults to using
> urandom to generate the buildid, which is not ideal.
> The Makefile auto-detects the presence on libssl-dev.
> 
> This version mmaps the jitdump file to create a marker
> MMAP record in the perf.data file. The marker is used to detect
> jitdump and cause perf inject to inject the jitted mmaps and
> generate ELF images for jitted functions.
> 
> Signed-off-by: Stephane Eranian <eranian@google.com>
> ---

<SNIP>

> +static int jit_repipe_code_load(struct jit_buf_desc *jd, union jr_entry *jr)
> +{
> +	struct perf_sample sample;
> +	union perf_event *event;
> +	struct perf_tool *tool = jd->session->tool;
> +	uint64_t code, addr;
> +	char *filename;
> +	struct stat st;
> +	size_t size;
> +	u16 idr_size;
> +	const char *sym;
> +	uint32_t count;
> +	int ret, csize;
> +	pid_t pid, tid;
> +	struct {
> +		u32 pid, tid;
> +		u64 time;
> +	} *id;
> +
> +	pid   = jr->load.pid;
> +	tid   = jr->load.tid;
> +	csize = jr->load.code_size;
> +	addr  = jr->load.code_addr;
> +	sym   = (void *)((unsigned long)jr + sizeof(jr->load));
> +	code  = (unsigned long)jr + jr->load.p.total_size - csize;
> +	count = jr->load.code_index;
> +	idr_size = jd->machine->id_hdr_size;
> +	/*
> +	 * +16 to account for sample_id_all (hack)
> +	 */
> +	event = calloc(1, sizeof(*event) + idr_size);
> +	if (!event)
> +		return -1;
> +
> +	filename = event->mmap2.filename;
> +	size = snprintf(filename, PATH_MAX, "%s/jitted-%d-%u.so",
> +			jd->dir,
> +			pid,
> +			count);
> +
> +	size++; /* for \0 */
> +
> +	size = PERF_ALIGN(size, sizeof(u64));
> +
> +	ret = jit_emit_elf(filename, sym, addr, (const void *)code, csize);
> +
> +	if (jd->debug_data && jd->nr_debug_entries) {
> +		free(jd->debug_data);
> +		jd->debug_data = NULL;
> +		jd->nr_debug_entries = 0;
> +	}
> +
> +	if (ret) {
> +		free(event);
> +		return -1;
> +	}
> +	if (stat(filename, &st))
> +		memset(&st, 0, sizeof(stat));
> +
> +	event->mmap2.header.type = PERF_RECORD_MMAP2;
> +	event->mmap2.header.misc = PERF_RECORD_MISC_USER;
> +	event->mmap2.header.size = (sizeof(event->mmap2) -
> +			(sizeof(event->mmap2.filename) - size) + idr_size);
> +
> +	event->mmap2.pgoff = 0;

I was testing this with Intel PT but getting decoding errors.
I think the problem is here, as described below, but I will look at it more
next week.

event->mmap2.pgoff = 0 says that the code running at 'addr' can be found at
offset 0 in the elf file, which is not correct.  The MMAP event has to
inform where in the file the code is.

> +	event->mmap2.start = addr;
> +	event->mmap2.len   = csize;
> +	event->mmap2.pid   = pid;
> +	event->mmap2.tid   = tid;
> +	event->mmap2.ino   = st.st_ino;
> +	event->mmap2.maj   = major(st.st_dev);
> +	event->mmap2.min   = minor(st.st_dev);
> +	event->mmap2.prot  = st.st_mode;
> +	event->mmap2.flags = MAP_SHARED;
> +	event->mmap2.ino_generation = 1;
> +
> +	id = (void *)((unsigned long)event + event->mmap.header.size - idr_size);
> +	if (jd->sample_type & PERF_SAMPLE_TID) {
> +		id->pid  = pid;
> +		id->tid  = tid;
> +	}
> +	if (jd->sample_type & PERF_SAMPLE_TIME)
> +		id->time = jr->load.p.timestamp;
> +
> +	/*
> +	 * create pseudo sample to induce dso hit increment
> +	 * use first address as sample address
> +	 */
> +	memset(&sample, 0, sizeof(sample));
> +	sample.pid  = pid;
> +	sample.tid  = tid;
> +	sample.time = id->time;
> +	sample.ip   = addr;
> +
> +	ret = perf_event__process_mmap2(tool, event, &sample, jd->machine, jd->session);
> +	if (ret)
> +		return ret;
> +
> +	ret = jit_inject_event(jd, event);
> +	/*
> +	 * mark dso as use to generate buildid in the header
> +	 */
> +	if (!ret)
> +		build_id__mark_dso_hit(tool, event, &sample, NULL, jd->machine);
> +
> +	return ret;


  reply	other threads:[~2015-10-09 13:20 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-01  6:45 [PATCH v7 0/4] perf: add support for profiling jitted code Stephane Eranian
2015-10-01  6:45 ` [PATCH v7 1/5] perf tools: add Java demangling support Stephane Eranian
2015-10-01  6:45 ` [PATCH v7 2/5] perf tools: pass session to mmap processing code Stephane Eranian
2015-10-01  6:45 ` [PATCH v7 3/5] perf inject: add jitdump mmap injection support Stephane Eranian
2015-10-09 13:17   ` Adrian Hunter [this message]
2015-10-01  6:45 ` [PATCH v7 4/5] perf tools: add JVMTI agent library Stephane Eranian
2015-10-01  6:45 ` [PATCH v7 5/5] perf/jit: add source line info support Stephane Eranian
2015-10-01  9:17 ` [PATCH v7 0/4] perf: add support for profiling jitted code Peter Zijlstra
2015-10-01  9:39   ` Ingo Molnar
2015-10-01 16:13     ` Arnaldo Carvalho de Melo
2015-10-04 19:52   ` Stephane Eranian
2015-10-01 22:45 ` Brendan Gregg
2015-10-04 20:05   ` Stephane Eranian
2015-10-09 20:51     ` Brendan Gregg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5617BE62.8070409@intel.com \
    --to=adrian.hunter@intel.com \
    --cc=acme@redhat.com \
    --cc=ak@linux.intel.com \
    --cc=brendan.d.gregg@gmail.com \
    --cc=cel@us.ibm.com \
    --cc=dsahern@gmail.com \
    --cc=eranian@google.com \
    --cc=johnmccutchan@google.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).