Skip to contentSkip to conversation

perf: Add support for Intel Processor Trace

PATCH In review Alexander Shishkin 11 participants 163 messages Dec 11, 2013 – Jan 8, 2014 2 messages not in the archive

Subsystems (6)
lore
Share & tools
lore

Alexander Shishkin

author reviewer
#0 lore raw

cover perf: Add support for Intel Processor Trace

Hi,

This patchset adds support for Intel Processor Trace (PT) extension [1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

The single most notable thing is that while PT outputs trace data in a compressed binary format, it will still generate hundreds of megabytes of trace data per second per core. Decoding this binary stream takes 2-3 orders of magnitude the cpu time that it takes to generate it. These considerations make it impossible to carry out decoding in kernel space. Therefore, the trace data is exported to userspace as a zero-copy mapping that userspace can collect and store for later decoding. To that end, perf is extended to support an additional ring buffer per event, which will export the trace data. This ring buffer is mapped from the event's file descriptor with a special "magic" offset. This ring buffer has its own user page with data_head and data_tail (in case the buffer is mapped writable) pointers used as read/write pointers in the buffer.

This way we get a normal perf data stream that provides sideband information that is required to decode the trace data, such as MMAPs, COMMs etc, plus the actual trace in a separate buffer.

If the trace buffer is mapped writable, the driver will stop tracing when it fills up (data_head approaches data_tail), till data is read, data_tail pointer is moved forward and an ioctl() is issued to re-enable tracing. If the trace buffer is mapped read only, the tracing will continue, overwriting older data, so that the buffer always contains the most recent data. Tracing can be stopped with an ioctl() and restarted once the data is collected.

Another use case is annotating samples of other perf events: if you set PERF_SAMPLE_ITRACE, attr.itrace_sample_size bytes of trace will be included in each event's sample.

Also, itrace data can be included in process core dumps, which can be enabled with a new rlimit -- RLIMIT_ITRACE.

This patchset consists of necessary changes to the perf kernel infrastructure, PT pmu driver and the remaining 60+ patches meticulously add itrace/PT support to perf userspace.

Patch Summary

1 - 5 kernel support for Intel PT
6 Allow set-output for task contexts of different types
7 - 34 perf tools preparatory changes
35 - 64 perf tools Instruction Tracing support
65 - 71 perf tools Intel PT support

[1] http://software.intel.com/en-us/intel-isa-extensions
[2] http://events.linuxfoundation.org/sites/events/files/slides/lcna13_kleen.pdf

Adrian Hunter (66):
perf: Allow set-output for task contexts of different types perf tools: Record whether a dso is 64-bit
perf tools: Let a user specify a PMU event without any config terms perf tools: Let default config be defined for a PMU
perf tools: Add perf_pmu__scan_file()
perf tools: Add perf_event_paranoid()
perf tools: Add dsos__hit_all()
perf tools: Add machine__get_thread_pid()
perf tools: Add cpu to struct thread
perf tools: Add ability to record the current tid for each cpu perf tools: Allow header->data_offset to be predetermined perf tools: Add perf_evlist__can_select_event()
perf session: Flag if the event stream is entirely in memory perf evlist: Pass mmap parameters in a struct
perf tools: Move mem_bswap32/64 to util.c
perf tools: Add feature test for __sync_val_compare_and_swap perf tools: Add option macro OPT_CALLBACK_OPTARG
perf evlist: Add perf_evlist__to_front()
perf evlist: Add perf_evlist__set_tracking_event()
perf evsel: Add 'no_aux_samples' option
perf evsel: Add 'immediate' option
perf evlist: Add 'system_wide' option
perf tools: Add id index
perf pmu: Let pmu's with no events show up on perf list perf session: Add ability to skip 4GiB or more
perf session: Add perf_session__deliver_synth_event() perf tools: Allow TSC conversion on any arch
perf tools: Move rdtsc() function
perf evlist: Add perf_evlist__enable_event_idx()
perf tools: Add itrace members of struct perf_event_attr perf tools: Add support for parsing pmu itrace_config perf tools: Add support for PERF_RECORD_ITRACE_LOST
perf tools: Add itrace sample parsing
perf header: Add Instruction Tracing feature
perf evlist: Add ability to mmap itrace buffers
perf tools: Add user events for Instruction Tracing
perf tools: Add support for Instruction Trace recording perf record: Add basic Instruction Tracing support
perf record: Extend -m option for Instruction Tracing mmap pages perf tools: Add a user event for Instruction Tracing errors perf session: Add Instruction Tracing hooks
perf session: Add Instruction Tracing options
perf session: Make perf_event__itrace_swap() non-static perf itrace: Add helpers for Instruction Tracing errors perf itrace: Add helpers for queuing Instruction Tracing data perf itrace: Add a heap for sorting Instruction Tracing queues perf itrace: Add processing for Instruction Tracing events perf script: Add Instruction Tracing support
perf script: Always allow fields 'addr' and 'cpu' for itrace perf report: Add Instruction Tracing support
perf tools: Add Instruction Trace sampling support
perf record: Add Instruction Trace sampling support
perf tools: Add Instruction Tracing Snapshot Mode
perf record: Add Instruction Tracing Snapshot Mode support perf inject: Re-pipe Instruction Tracing events
perf inject: Add Instruction Tracing support
perf inject: Cut Instruction Tracing samples
perf tools: Add Instruction Tracing index
perf tools: Hit all build ids when Instruction Tracing perf itrace: Add Intel PT as an Instruction Tracing type perf tools: Add Intel PT packet decoder
perf tools: Add Intel PT instruction decoder
perf tools: Add Intel PT log
perf tools: Add Intel PT decoder
perf tools: Add Intel PT support
perf tools: Take Intel PT into use

Alexander Shishkin (5):
perf: Disable all pmus on unthrottling and rescheduling
x86: Add Intel Processor Trace (INTEL_PT) cpu feature detection
perf: Abstract ring_buffer backing store operations
itrace: Infrastructure for instruction flow tracing units
x86: perf: Intel PT PMU driver

 arch/x86/include/asm/cpufeature.h                  |    1 +
 arch/x86/include/uapi/asm/msr-index.h              |   18 +
 arch/x86/kernel/cpu/Makefile                       |    1 +
 arch/x86/kernel/cpu/intel_pt.h                     |  129 ++
 arch/x86/kernel/cpu/perf_event.c                   |    4 +
 arch/x86/kernel/cpu/perf_event_intel.c             |   10 +
 arch/x86/kernel/cpu/perf_event_intel_pt.c          | 1167 +++++++++++
 arch/x86/kernel/cpu/scattered.c                    |    1 +
 fs/binfmt_elf.c                                    |    6 +
 fs/proc/base.c                                     |    1 +
 include/asm-generic/resource.h                     |    1 +
 include/linux/itrace.h                             |  147 ++
 include/linux/perf_event.h                         |   33 +-
 include/uapi/asm-generic/resource.h                |    3 +-
 include/uapi/linux/elf.h                           |    1 +
 include/uapi/linux/perf_event.h                    |   25 +-
 kernel/events/Makefile                             |    2 +-
 kernel/events/core.c                               |  329 ++-
 kernel/events/internal.h                           |   21 +-
 kernel/events/itrace.c                             |  589 ++++++
 kernel/events/ring_buffer.c                        |  176 +-
 kernel/exit.c                                      |    3 +
 kernel/sys.c                                       |    5 +
 tools/perf/Documentation/intel-pt.txt              |  581 ++++++
 tools/perf/Documentation/perf-inject.txt           |   20 +
 tools/perf/Documentation/perf-record.txt           |   14 +
 tools/perf/Documentation/perf-report.txt           |   21 +
 tools/perf/Documentation/perf-script.txt           |   21 +
 tools/perf/Makefile.perf                           |   30 +-
 tools/perf/arch/x86/Makefile                       |    2 +
 tools/perf/arch/x86/util/itrace.c                  |   41 +
 tools/perf/arch/x86/util/pmu.c                     |   13 +
 tools/perf/arch/x86/util/tsc.c                     |   31 +-
 tools/perf/arch/x86/util/tsc.h                     |    3 -
 tools/perf/builtin-buildid-list.c                  |    9 +
 tools/perf/builtin-inject.c                        |  193 +-
 tools/perf/builtin-record.c                        |  277 ++-
 tools/perf/builtin-report.c                        |   12 +
 tools/perf/builtin-script.c                        |   13 +
 tools/perf/config/Makefile                         |    5 +
 tools/perf/config/feature-checks/Makefile          |    4 +
 tools/perf/config/feature-checks/test-all.c        |    5 +
 .../feature-checks/test-sync-compare-and-swap.c    |   14 +
 tools/perf/perf.h                                  |   14 +
 tools/perf/tests/perf-time-to-tsc.c                |   12 +-
 tools/perf/tests/pmu.c                             |    2 +-
 tools/perf/tests/sample-parsing.c                  |    7 +-
 tools/perf/util/dso.c                              |    1 +
 tools/perf/util/dso.h                              |    1 +
 tools/perf/util/event.c                            |   21 +
 tools/perf/util/event.h                            |   70 +
 tools/perf/util/evlist.c                           |  289 ++-
 tools/perf/util/evlist.h                           |   19 +
 tools/perf/util/evsel.c                            |   86 +-
 tools/perf/util/evsel.h                            |   19 +-
 tools/perf/util/header.c                           |   73 +-
 tools/perf/util/header.h                           |    3 +
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 1678 +++++++++++++++
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |   83 +
 .../util/intel-pt-decoder/intel-pt-insn-decoder.c  |  224 ++
 .../util/intel-pt-decoder/intel-pt-insn-decoder.h  |   67 +
 tools/perf/util/intel-pt-decoder/intel-pt-log.c    |  119 ++
 tools/perf/util/intel-pt-decoder/intel-pt-log.h    |   52 +
 .../util/intel-pt-decoder/intel-pt-pkt-decoder.c   |  404 ++++
 .../util/intel-pt-decoder/intel-pt-pkt-decoder.h   |   68 +
 tools/perf/util/intel-pt.c                         | 2193 ++++++++++++++++++++
 tools/perf/util/intel-pt.h                         |   40 +
 tools/perf/util/itrace.c                           | 1273 ++++++++++++
 tools/perf/util/itrace.h                           |  476 +++++
 tools/perf/util/machine.c                          |   85 +
 tools/perf/util/machine.h                          |   11 +
 tools/perf/util/parse-events.c                     |   17 +-
 tools/perf/util/parse-events.h                     |    1 +
 tools/perf/util/parse-events.l                     |    1 +
 tools/perf/util/parse-events.y                     |   10 +
 tools/perf/util/parse-options.h                    |    5 +
 tools/perf/util/pmu.c                              |   95 +-
 tools/perf/util/pmu.h                              |   14 +-
 tools/perf/util/pmu.l                              |    1 +
 tools/perf/util/pmu.y                              |    9 +-
 tools/perf/util/record.c                           |   43 +-
 tools/perf/util/session.c                          |  343 ++-
 tools/perf/util/session.h                          |   27 +-
 tools/perf/util/symbol-elf.c                       |    3 +
 tools/perf/util/symbol-minimal.c                   |   23 +
 tools/perf/util/symbol.c                           |    1 +
 tools/perf/util/symbol.h                           |    1 +
 tools/perf/util/thread.c                           |    1 +
 tools/perf/util/thread.h                           |    1 +
 tools/perf/util/tool.h                             |   12 +-
 tools/perf/util/tsc.c                              |   30 +
 tools/perf/util/tsc.h                              |   12 +
 tools/perf/util/util.c                             |   41 +
 tools/perf/util/util.h                             |    6 +
 94 files changed, 11708 insertions(+), 361 deletions(-)
 create mode 100644 arch/x86/kernel/cpu/intel_pt.h
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_pt.c
 create mode 100644 include/linux/itrace.h
 create mode 100644 kernel/events/itrace.c
 create mode 100644 tools/perf/Documentation/intel-pt.txt
 create mode 100644 tools/perf/arch/x86/util/itrace.c
 create mode 100644 tools/perf/arch/x86/util/pmu.c
 create mode 100644 tools/perf/config/feature-checks/test-sync-compare-and-swap.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.h
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.h
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-log.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-log.h
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.h
 create mode 100644 tools/perf/util/intel-pt.c
 create mode 100644 tools/perf/util/intel-pt.h
 create mode 100644 tools/perf/util/itrace.c
 create mode 100644 tools/perf/util/itrace.h
 create mode 100644 tools/perf/util/tsc.c
 create mode 100644 tools/perf/util/tsc.h
Signature · 1 line
1.8.5.1
author reviewer
#1 lore raw

PATCH 1/71 perf: Disable all pmus on unthrottling and rescheduling

Currently, only one pmu in a context gets disabled during unthrottling and event_sched_{out,in}, however, events in one context may belong to different pmus, which results in pmus being reprogrammed while they are still enabled. This patch temporarily disables pmus that correspond to each event in the context while these events are being modified.

---
 kernel/events/core.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/events/core.c +24 -3
index 403b781..d656cd6 100644
@@ -1396,6 +1396,9 @@ event_sched_out(struct perf_event *event,
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	if (event->pending_disable) {
 		event->pending_disable = 0;
@@ -1412,6 +1415,9 @@ event_sched_out(struct perf_event *event,
 		ctx->nr_freq--;
 	if (event->attr.exclusive || !cpuctx->active_oncpu)
 		cpuctx->exclusive = 0;
+
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
 }
 
 static void
@@ -1652,6 +1658,7 @@ event_sched_in(struct perf_event *event,
 		 struct perf_event_context *ctx)
 {
 	u64 tstamp = perf_event_time(event);
+	int ret = 0;
 
 	if (event->state <= PERF_EVENT_STATE_OFF)
 		return 0;
@@ -1674,10 +1681,14 @@ event_sched_in(struct perf_event *event,
 	 */
 	smp_wmb();
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	if (event->pmu->add(event, PERF_EF_START)) {
 		event->state = PERF_EVENT_STATE_INACTIVE;
 		event->oncpu = -1;
-		return -EAGAIN;
+		ret = -EAGAIN;
+		goto out;
 	}
 
 	event->tstamp_running += tstamp - event->tstamp_stopped;
@@ -1693,7 +1704,11 @@ event_sched_in(struct perf_event *event,
 	if (event->attr.exclusive)
 		cpuctx->exclusive = 1;
 
-	return 0;
+out:
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
+
+	return ret;
 }
 
 static int
@@ -2743,6 +2758,9 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 		if (!event_filter_match(event))
 			continue;
 
+		if (ctx->pmu != event->pmu)
+			perf_pmu_disable(event->pmu);
+
 		hwc = &event->hw;
 
 		if (hwc->interrupts == MAX_INTERRUPTS) {
@@ -2752,7 +2770,7 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 		}
 
 		if (!event->attr.freq || !event->attr.sample_freq)
-			continue;
+			goto next;
 
 		/*
 		 * stop the event and update event->count
@@ -2774,6 +2792,9 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 			perf_adjust_period(event, period, delta, false);
 
 		event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
+	next:
+		if (ctx->pmu != event->pmu)
+			perf_pmu_enable(event->pmu);
 	}
 
 	perf_pmu_enable(ctx->pmu);
Signature · 1 line
1.8.5.1
Reviewed-by on v0 1/71
#2 lore raw

Re: [PATCH v0 01/71] perf: Disable all pmus on unthrottling and rescheduling

Quoted 5 lines · Alexander Shishkin

Currently, only one pmu in a context gets disabled during unthrottling and event_sched_{out,in}, however, events in one context may belong to different pmus, which results in pmus being reprogrammed while they are still enabled. This patch temporarily disables pmus that correspond to each event in the context while these events are being modified.

This is a generic bug fix and should be merged independent of the PT code. This affects any setup using multiple PMUs at the same time.

-Andi

Quoted 99 lines
---
 kernel/events/core.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/events/core.c +24 -3
index 403b781..d656cd6 100644
@@ -1396,6 +1396,9 @@ event_sched_out(struct perf_event *event,
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	if (event->pending_disable) {
 		event->pending_disable = 0;
@@ -1412,6 +1415,9 @@ event_sched_out(struct perf_event *event,
 		ctx->nr_freq--;
 	if (event->attr.exclusive || !cpuctx->active_oncpu)
 		cpuctx->exclusive = 0;
+
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
 }
 
 static void
@@ -1652,6 +1658,7 @@ event_sched_in(struct perf_event *event,
 		 struct perf_event_context *ctx)
 {
 	u64 tstamp = perf_event_time(event);
+	int ret = 0;
 
 	if (event->state <= PERF_EVENT_STATE_OFF)
 		return 0;
@@ -1674,10 +1681,14 @@ event_sched_in(struct perf_event *event,
 	 */
 	smp_wmb();
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	if (event->pmu->add(event, PERF_EF_START)) {
 		event->state = PERF_EVENT_STATE_INACTIVE;
 		event->oncpu = -1;
-		return -EAGAIN;
+		ret = -EAGAIN;
+		goto out;
 	}
 
 	event->tstamp_running += tstamp - event->tstamp_stopped;
@@ -1693,7 +1704,11 @@ event_sched_in(struct perf_event *event,
 	if (event->attr.exclusive)
 		cpuctx->exclusive = 1;
 
-	return 0;
+out:
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
+
+	return ret;
 }
 
 static int
@@ -2743,6 +2758,9 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 		if (!event_filter_match(event))
 			continue;
 
+		if (ctx->pmu != event->pmu)
+			perf_pmu_disable(event->pmu);
+
 		hwc = &event->hw;
 
 		if (hwc->interrupts == MAX_INTERRUPTS) {
@@ -2752,7 +2770,7 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 		}
 
 		if (!event->attr.freq || !event->attr.sample_freq)
-			continue;
+			goto next;
 
 		/*
 		 * stop the event and update event->count
@@ -2774,6 +2792,9 @@ static void perf_adjust_freq_unthr_context(struct perf_event_context *ctx,
 			perf_adjust_period(event, period, delta, false);
 
 		event->pmu->start(event, delta > 0 ? PERF_EF_RELOAD : 0);
+	next:
+		if (ctx->pmu != event->pmu)
+			perf_pmu_enable(event->pmu);
 	}
 
 	perf_pmu_enable(ctx->pmu);
Signature · 1 line
1.8.5.1
Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#3 lore raw

Re: [PATCH v0 01/71] perf: Disable all pmus on unthrottling and rescheduling

In reply to Alexander Shishkin
Quoted 26 lines · Alexander Shishkin

Currently, only one pmu in a context gets disabled during unthrottling and event_sched_{out,in}, however, events in one context may belong to different pmus, which results in pmus being reprogrammed while they are still enabled. This patch temporarily disables pmus that correspond to each event in the context while these events are being modified.

---
 kernel/events/core.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/events/core.c +3 -0
index 403b781..d656cd6 100644
@@ -1396,6 +1396,9 @@ event_sched_out(struct perf_event *event,
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	if (event->pending_disable) {
 		event->pending_disable = 0;

kernel/events/core.c event_sched_out(struct perf_event *event, · line 1423

Quoted 9 lines
kernel/events/core.c +3 -0
@@ -1412,6 +1415,9 @@ event_sched_out(struct perf_event *event,
 		ctx->nr_freq--;
 	if (event->attr.exclusive || !cpuctx->active_oncpu)
 		cpuctx->exclusive = 0;
+
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
 }
 
 static void

Hmm, indeed. Does it make sense to drop the conditional? perf_pmu_{en,dis}able() is recursive and the thinking is that if its the same PMU the cacheline is hot because we touched it already recently anyway, so the unconditional inc/dec might actually be faster.. dunno.

author reviewer
#4 lore raw

Re: [PATCH v0 01/71] perf: Disable all pmus on unthrottling and rescheduling

Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Alexander Shishkin

Currently, only one pmu in a context gets disabled during unthrottling and event_sched_{out,in}, however, events in one context may belong to different pmus, which results in pmus being reprogrammed while they are still enabled. This patch temporarily disables pmus that correspond to each event in the context while these events are being modified.

---
 kernel/events/core.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/events/core.c +6 -0
index 403b781..d656cd6 100644
@@ -1396,6 +1396,9 @@ event_sched_out(struct perf_event *event,
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	if (event->pending_disable) {
 		event->pending_disable = 0;
@@ -1412,6 +1415,9 @@ event_sched_out(struct perf_event *event,
 		ctx->nr_freq--;
 	if (event->attr.exclusive || !cpuctx->active_oncpu)
 		cpuctx->exclusive = 0;
+
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
 }
 
Quoted 6 lines

kernel/events/core.c event_sched_out(struct perf_event *event, · line 1423

Quoted 1 line
kernel/events/core.c +0 -0
 static void

Hmm, indeed. Does it make sense to drop the conditional? perf_pmu_{en,dis}able() is recursive and the thinking is that if its the same PMU the cacheline is hot because we touched it already recently anyway, so the unconditional inc/dec might actually be faster.. dunno.

Well, given the disable_count check in perf_pmu_{en,dis}able, this one indeed looks redundant to me. Should I resend this one separately?

Regards,
--
Alex

maintainer
#5 lore raw

Re: [PATCH v0 01/71] perf: Disable all pmus on unthrottling and rescheduling

Quoted 44 lines · Alexander Shishkin
Quoted 35 lines · Peter Zijlstra
Quoted 34 lines · Alexander Shishkin

Currently, only one pmu in a context gets disabled during unthrottling and event_sched_{out,in}, however, events in one context may belong to different pmus, which results in pmus being reprogrammed while they are still enabled. This patch temporarily disables pmus that correspond to each event in the context while these events are being modified.

---
 kernel/events/core.c | 27 ++++++++++++++++++++++++---
 1 file changed, 24 insertions(+), 3 deletions(-)
kernel/events/core.c +6 -0
index 403b781..d656cd6 100644
@@ -1396,6 +1396,9 @@ event_sched_out(struct perf_event *event,
 	if (event->state != PERF_EVENT_STATE_ACTIVE)
 		return;
 
+	if (event->pmu != ctx->pmu)
+		perf_pmu_disable(event->pmu);
+
 	event->state = PERF_EVENT_STATE_INACTIVE;
 	if (event->pending_disable) {
 		event->pending_disable = 0;
@@ -1412,6 +1415,9 @@ event_sched_out(struct perf_event *event,
 		ctx->nr_freq--;
 	if (event->attr.exclusive || !cpuctx->active_oncpu)
 		cpuctx->exclusive = 0;
+
+	if (event->pmu != ctx->pmu)
+		perf_pmu_enable(event->pmu);
 }
 
Quoted 6 lines

kernel/events/core.c event_sched_out(struct perf_event *event, · line 1423

Quoted 1 line
kernel/events/core.c +0 -0
 static void

Hmm, indeed. Does it make sense to drop the conditional? perf_pmu_{en,dis}able() is recursive and the thinking is that if its the same PMU the cacheline is hot because we touched it already recently anyway, so the unconditional inc/dec might actually be faster.. dunno.

Quoted 2 lines

Well, given the disable_count check in perf_pmu_{en,dis}able, this one indeed looks redundant to me. Should I resend this one separately?

Yes, it seems an unrelated bugfix, like Andi said.

author reviewer
#6 lore raw

PATCH 2/71 x86: Add Intel Processor Trace (INTEL_PT) cpu feature detection

In reply to Alexander Shishkin

Intel Processor Trace is an architecture extension that allows for program flow tracing.

---
 arch/x86/include/asm/cpufeature.h | 1 +
 arch/x86/kernel/cpu/scattered.c   | 1 +
 2 files changed, 2 insertions(+)
arch/x86/include/asm/cpufeature.h +1 -0
index 89270b4..cb9864f 100644
@@ -186,6 +186,7 @@
 #define X86_FEATURE_DTHERM	(7*32+ 7) /* Digital Thermal Sensor */
 #define X86_FEATURE_HW_PSTATE	(7*32+ 8) /* AMD HW-PState */
 #define X86_FEATURE_PROC_FEEDBACK (7*32+ 9) /* AMD ProcFeedbackInterface */
+#define X86_FEATURE_INTEL_PT	(7*32+10) /* Intel Processor Trace */
 
 /* Virtualization flags: Linux defined, word 8 */
 #define X86_FEATURE_TPR_SHADOW  (8*32+ 0) /* Intel TPR Shadow */
arch/x86/kernel/cpu/scattered.c +1 -0
index b6f794a..726e6a3 100644
@@ -36,6 +36,7 @@ void init_scattered_cpuid_features(struct cpuinfo_x86 *c)
 		{ X86_FEATURE_ARAT,		CR_EAX, 2, 0x00000006, 0 },
 		{ X86_FEATURE_PLN,		CR_EAX, 4, 0x00000006, 0 },
 		{ X86_FEATURE_PTS,		CR_EAX, 6, 0x00000006, 0 },
+		{ X86_FEATURE_INTEL_PT,		CR_EBX,25, 0x00000007, 0 },
 		{ X86_FEATURE_APERFMPERF,	CR_ECX, 0, 0x00000006, 0 },
 		{ X86_FEATURE_EPB,		CR_ECX, 3, 0x00000006, 0 },
 		{ X86_FEATURE_XSAVEOPT,		CR_EAX,	0, 0x0000000d, 1 },
Signature · 1 line
1.8.5.1
author reviewer
#7 lore raw

PATCH 3/71 perf: Abstract ring_buffer backing store operations

In reply to Alexander Shishkin

This patch extends perf's ring_buffer code so that buffers with different backing can be allocated simultaneously with rb_alloc(). This allows the reuse of ring_buffer code for exporting hardware-written trace buffers (such as those of Intel PT) to userspace.

---
 kernel/events/core.c        |   2 +-
 kernel/events/internal.h    |  14 +++-
 kernel/events/ring_buffer.c | 174 +++++++++++++++++++++++++++-----------------
 3 files changed, 122 insertions(+), 68 deletions(-)
kernel/events/core.c +1 -1
index d656cd6..7c3faf1 100644
@@ -4098,7 +4098,7 @@ again:
 
 	rb = rb_alloc(nr_pages, 
 		event->attr.watermark ? event->attr.wakeup_watermark : 0,
-		event->cpu, flags);
+		event->cpu, flags, NULL);
 
 	if (!rb) {
 		ret = -ENOMEM;
kernel/events/internal.h +13 -1
index 569b2187..8835f00 100644
@@ -6,6 +6,16 @@
 
 /* Buffer handling */
 
+struct ring_buffer;
+
+struct ring_buffer_ops {
+	unsigned long	(*get_size)(int);
+	int		(*alloc_user_page)(struct ring_buffer *, int, int);
+	int		(*alloc_data_page)(struct ring_buffer *, int, int, int);
+	void		(*free_buffer)(struct ring_buffer *);
+	struct page	*(*mmap_to_page)(struct ring_buffer *, unsigned long);
+};
+
 #define RING_BUFFER_WRITABLE		0x01
 
 struct ring_buffer {
@@ -15,6 +25,7 @@ struct ring_buffer {
 	struct work_struct		work;
 	int				page_order;	/* allocation order  */
 #endif
+	struct ring_buffer_ops		*ops;
 	int				nr_pages;	/* nr of data pages  */
 	int				overwrite;	/* can overwrite itself */
 
@@ -41,7 +52,8 @@ struct ring_buffer {
 
 extern void rb_free(struct ring_buffer *rb);
 extern struct ring_buffer *
-rb_alloc(int nr_pages, long watermark, int cpu, int flags);
+rb_alloc(int nr_pages, long watermark, int cpu, int flags,
+	 struct ring_buffer_ops *rb_ops);
 extern void perf_event_wakeup(struct perf_event *event);
 
 extern void
kernel/events/ring_buffer.c +108 -66

260 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#8 lore raw

PATCH 4/71 itrace: Infrastructure for instruction flow tracing units

In reply to Alexander Shishkin

Instruction tracing PMUs are capable of recording a log of instruction execution flow on a cpu core, which can be useful for profiling and crash analysis. This patch adds itrace infrastructure for perf events and the rest of the kernel to use.

Since such PMUs can produce copious amounts of trace data, it may be impractical to process it inside the kernel in real time, but instead export raw trace streams to userspace for subsequent analysis. Thus, itrace PMUs may export their trace buffers, which can be mmap()ed to userspace from a perf event fd with a PERF_EVENT_ITRACE_OFFSET offset. To that end, perf is extended to work with multiple ring buffers per event, reusing the ring_buffer code in an attempt to reduce complexity.

Also, trace data from such PMUs can be used to annotate other perf events by including it in sample records when PERF_SAMPLE_ITRACE flag is set. In this case, a PT kernel counter is created for each such event and trace data is retrieved from it and stored in the perf data stream.

Finally, such per thread trace data can be included in process core dumps, which is controlled via a new rlimit parameter RLIMIT_ITRACE. This again is done by a per-thread kernel counter that is created when this RLIMIT_ITRACE is set.

This infrastructure should also be useful for ARM ETM/PTM and other program flow tracing units that can potentially generate a lot of trace data very fast.

---
 fs/binfmt_elf.c                     |   6 +
 fs/proc/base.c                      |   1 +
 include/asm-generic/resource.h      |   1 +
 include/linux/itrace.h              | 147 +++++++++
 include/linux/perf_event.h          |  33 +-
 include/uapi/asm-generic/resource.h |   3 +-
 include/uapi/linux/elf.h            |   1 +
 include/uapi/linux/perf_event.h     |  25 +-
 kernel/events/Makefile              |   2 +-
 kernel/events/core.c                | 299 ++++++++++++------
 kernel/events/internal.h            |   7 +
 kernel/events/itrace.c              | 589 ++++++++++++++++++++++++++++++++++++
 kernel/events/ring_buffer.c         |   2 +-
 kernel/exit.c                       |   3 +
 kernel/sys.c                        |   5 +
 15 files changed, 1020 insertions(+), 104 deletions(-)
 create mode 100644 include/linux/itrace.h
 create mode 100644 kernel/events/itrace.c
fs/binfmt_elf.c +6 -0
index 571a423..c7fcd49 100644
@@ -34,6 +34,7 @@
 #include <linux/utsname.h>
 #include <linux/coredump.h>
 #include <linux/sched.h>
+#include <linux/itrace.h>
 #include <asm/uaccess.h>
 #include <asm/param.h>
 #include <asm/page.h>
@@ -1576,6 +1577,8 @@ static int fill_thread_core_info(struct elf_thread_core_info *t,
 		}
 	}
 
+	*total += itrace_elf_note_size(t->task);
+
 	return 1;
 }
 
@@ -1608,6 +1611,7 @@ static int fill_note_info(struct elfhdr *elf, int phdrs,
 	for (i = 0; i < view->n; ++i)
 		if (view->regsets[i].core_note_type != 0)
 			++info->thread_notes;
+	info->thread_notes++; /* ITRACE */
 
 	/*
 	 * Sanity check.  We rely on regset 0 being in NT_PRSTATUS,
@@ -1710,6 +1714,8 @@ static int write_note_info(struct elf_note_info *info,
 			    !writenote(&t->notes[i], cprm))
 				return 0;
 
+		itrace_elf_note_write(cprm, t->task);
+
 		first = 0;
 		t = t->next;
 	} while (t);
fs/proc/base.c +1 -0
index 1485e38..41785ec 100644
@@ -471,6 +471,7 @@ static const struct limit_names lnames[RLIM_NLIMITS] = {
 	[RLIMIT_NICE] = {"Max nice priority", NULL},
 	[RLIMIT_RTPRIO] = {"Max realtime priority", NULL},
 	[RLIMIT_RTTIME] = {"Max realtime timeout", "us"},
+	[RLIMIT_ITRACE] = {"Max ITRACE buffer size", "bytes"},
 };
 
 /* Display limits for a process */
include/asm-generic/resource.h +1 -0
index b4ea8f5..e6e5657 100644
@@ -25,6 +25,7 @@
 	[RLIMIT_NICE]		= { 0, 0 },				\
 	[RLIMIT_RTPRIO]		= { 0, 0 },				\
 	[RLIMIT_RTTIME]		= {  RLIM_INFINITY,  RLIM_INFINITY },	\
+	[RLIMIT_ITRACE]		= {              0,  RLIM_INFINITY },	\
 }
 
 #endif
include/linux/itrace.h new file +147 -0

147 lines not on this page · raw message

include/linux/perf_event.h +30 -3
index 8f4a70f..b27cfc7 100644
@@ -83,6 +83,12 @@ struct perf_regs_user {
 	struct pt_regs	*regs;
 };
 
+struct perf_trace_record {
+	u64		size;
+	unsigned long	from;
+	unsigned long	to;
+};
+
 struct task_struct;
 
 /*
@@ -97,6 +103,14 @@ struct hw_perf_event_extra {
 
 struct event_constraint;
 
+enum perf_itrace_counter_type {
+	PERF_ITRACE_USER	= BIT(1),
+	PERF_ITRACE_SAMPLING	= BIT(2),
+	PERF_ITRACE_COREDUMP	= BIT(3),
+	PERF_ITRACE_KERNEL	= (PERF_ITRACE_SAMPLING | PERF_ITRACE_COREDUMP),
+	PERF_ITRACE_ANY		= (PERF_ITRACE_KERNEL | PERF_ITRACE_USER),
+};
+
 /**
  * struct hw_perf_event - performance event hardware details:
  */
@@ -126,6 +140,10 @@ struct hw_perf_event {
 			/* for tp_event->class */
 			struct list_head	tp_list;
 		};
+		struct { /* itrace */
+			struct task_struct	*itrace_target;
+			unsigned int		counter_type;
+		};
 #ifdef CONFIG_HAVE_HW_BREAKPOINT
 		struct { /* breakpoint */
 			/*
@@ -289,6 +307,12 @@ struct swevent_hlist {
 struct perf_cgroup;
 struct ring_buffer;
 
+enum perf_event_rb {
+	PERF_RB_MAIN = 0,
+	PERF_RB_ITRACE,
+	PERF_NR_RB,
+};
+
 /**
  * struct perf_event - performance event kernel representation:
  */
@@ -400,10 +424,10 @@ struct perf_event {
 
 	/* mmap bits */
 	struct mutex			mmap_mutex;
-	atomic_t			mmap_count;
+	atomic_t			mmap_count[PERF_NR_RB];
 
-	struct ring_buffer		*rb;
-	struct list_head		rb_entry;
+	struct ring_buffer		*rb[PERF_NR_RB];
+	struct list_head		rb_entry[PERF_NR_RB];
 
 	/* poll related */
 	wait_queue_head_t		waitq;
@@ -426,6 +450,7 @@ struct perf_event {
 	perf_overflow_handler_t		overflow_handler;
 	void				*overflow_handler_context;
 
+	struct perf_event		*trace_event;
 #ifdef CONFIG_EVENT_TRACING
 	struct ftrace_event_call	*tp_event;
 	struct event_filter		*filter;
@@ -583,6 +608,7 @@ struct perf_sample_data {
 	union  perf_mem_data_src	data_src;
 	struct perf_callchain_entry	*callchain;
 	struct perf_raw_record		*raw;
+	struct perf_trace_record	trace;
 	struct perf_branch_stack	*br_stack;
 	struct perf_regs_user		regs_user;
 	u64				stack_user_size;
@@ -603,6 +629,7 @@ static inline void perf_sample_data_init(struct perf_sample_data *data,
 	data->period = period;
 	data->regs_user.abi = PERF_SAMPLE_REGS_ABI_NONE;
 	data->regs_user.regs = NULL;
+	data->trace.from = data->trace.to = data->trace.size = 0;
 	data->stack_user_size = 0;
 	data->weight = 0;
 	data->data_src.val = 0;
include/uapi/asm-generic/resource.h +2 -1
index f863428..073f413 100644
@@ -45,7 +45,8 @@
 					   0-39 for nice level 19 .. -20 */
 #define RLIMIT_RTPRIO		14	/* maximum realtime priority */
 #define RLIMIT_RTTIME		15	/* timeout for RT tasks in us */
-#define RLIM_NLIMITS		16
+#define RLIMIT_ITRACE		16	/* max itrace size */
+#define RLIM_NLIMITS		17
 
 /*
  * SuS says limits have to be unsigned.
include/uapi/linux/elf.h +1 -0
index ef6103b..4bfbf66 100644
@@ -369,6 +369,7 @@ typedef struct elf64_shdr {
 #define NT_PRPSINFO	3
 #define NT_TASKSTRUCT	4
 #define NT_AUXV		6
+#define NT_ITRACE	7
 /*
  * Note to userspace developers: size of NT_SIGINFO note may increase
  * in the future to accomodate more fields, don't assume it is fixed!
include/uapi/linux/perf_event.h +24 -1
index e1802d6..9e3a890 100644
@@ -137,8 +137,9 @@ enum perf_event_sample_format {
 	PERF_SAMPLE_DATA_SRC			= 1U << 15,
 	PERF_SAMPLE_IDENTIFIER			= 1U << 16,
 	PERF_SAMPLE_TRANSACTION			= 1U << 17,
+	PERF_SAMPLE_ITRACE			= 1U << 18,
 
-	PERF_SAMPLE_MAX = 1U << 18,		/* non-ABI */
+	PERF_SAMPLE_MAX = 1U << 19,		/* non-ABI */
 };
 
 /*
@@ -237,6 +238,10 @@ enum perf_event_read_format {
 #define PERF_ATTR_SIZE_VER2	80	/* add: branch_sample_type */
 #define PERF_ATTR_SIZE_VER3	96	/* add: sample_regs_user */
 					/* add: sample_stack_user */
+#define PERF_ATTR_SIZE_VER4	120	/* add: itrace_config */
+					/* add: itrace_watermark */
+					/* add: itrace_sample_type */
+					/* add: itrace_sample_size */
 
 /*
  * Hardware event_id to monitor via a performance monitoring event:
@@ -333,6 +338,11 @@ struct perf_event_attr {
 
 	/* Align to u64. */
 	__u32	__reserved_2;
+
+	__u64	itrace_config;
+	__u32	itrace_watermark;	/* wakeup every n pages */
+	__u32	itrace_sample_type;	/* pmu->type of the itrace PMU */
+	__u64	itrace_sample_size;
 };
 
 #define perf_flags(attr)	(*(&(attr)->read_format + 1))
@@ -679,6 +689,8 @@ enum perf_event_type {
 	 *
 	 *	{ u64			weight;   } && PERF_SAMPLE_WEIGHT
 	 *	{ u64			data_src; } && PERF_SAMPLE_DATA_SRC
+	 *	{ u64			size;
+	 *	  char			data[size]; } && PERF_SAMPLE_ITRACE
 	 * };
 	 */
 	PERF_RECORD_SAMPLE			= 9,
@@ -704,9 +716,20 @@ enum perf_event_type {
 	 */
 	PERF_RECORD_MMAP2			= 10,
 
+	/*
+	 * struct {
+	 *   u64 offset;
+	 * }
+	 */
+	PERF_RECORD_ITRACE_LOST			= 11,
+
 	PERF_RECORD_MAX,			/* non-ABI */
 };
 
+/* Architecture-specific data */
+
+#define PERF_EVENT_ITRACE_OFFSET	0x40000000
+
 #define PERF_MAX_STACK_DEPTH		127
 
 enum perf_callchain_context {
kernel/events/Makefile +1 -1
index 103f5d1..46a3770 100644
@@ -2,7 +2,7 @@ ifdef CONFIG_FUNCTION_TRACER
 CFLAGS_REMOVE_core.o = -pg
 endif
 
-obj-y := core.o ring_buffer.o callchain.o
+obj-y := core.o ring_buffer.o callchain.o itrace.o
 
 obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
 obj-$(CONFIG_UPROBES) += uprobes.o
kernel/events/core.c +202 -97

628 lines not on this page · raw message

kernel/events/internal.h +7 -0
index 8835f00..f183efe 100644
@@ -45,6 +45,7 @@ struct ring_buffer {
 	atomic_t			mmap_count;
 	unsigned long			mmap_locked;
 	struct user_struct		*mmap_user;
+	void				*priv;
 
 	struct perf_event_mmap_page	*user_page;
 	void				*data_pages[0];
@@ -55,6 +56,12 @@ extern struct ring_buffer *
 rb_alloc(int nr_pages, long watermark, int cpu, int flags,
 	 struct ring_buffer_ops *rb_ops);
 extern void perf_event_wakeup(struct perf_event *event);
+extern struct ring_buffer *ring_buffer_get(struct perf_event *event, int rbx);
+extern void ring_buffer_put(struct ring_buffer *rb);
+extern void ring_buffer_attach(struct perf_event *event,
+			       struct ring_buffer *rb);
+extern void ring_buffer_detach(struct perf_event *event,
+			       struct ring_buffer *rb);
 
 extern void
 perf_event_header__init_id(struct perf_event_header *header,
kernel/events/itrace.c new file +589 -0

589 lines not on this page · raw message

kernel/events/ring_buffer.c +1 -1
index d7ec426..0bee352 100644
@@ -119,7 +119,7 @@ int perf_output_begin(struct perf_output_handle *handle,
 	if (event->parent)
 		event = event->parent;
 
-	rb = rcu_dereference(event->rb);
+	rb = rcu_dereference(event->rb[PERF_RB_MAIN]);
 	if (unlikely(!rb))
 		goto out;
 
kernel/exit.c +3 -0
index a949819..28138ef 100644
@@ -48,6 +48,7 @@
 #include <linux/fs_struct.h>
 #include <linux/init_task.h>
 #include <linux/perf_event.h>
+#include <linux/itrace.h>
 #include <trace/events/sched.h>
 #include <linux/hw_breakpoint.h>
 #include <linux/oom.h>
@@ -788,6 +789,8 @@ void do_exit(long code)
 	check_stack_usage();
 	exit_thread();
 
+	exit_itrace(tsk);
+
 	/*
 	 * Flush inherited counters to the parent - before the parent
 	 * gets woken up by child-exit notifications.
kernel/sys.c +5 -0
index c723113..7651d6f 100644
@@ -14,6 +14,7 @@
 #include <linux/fs.h>
 #include <linux/kmod.h>
 #include <linux/perf_event.h>
+#include <linux/itrace.h>
 #include <linux/resource.h>
 #include <linux/kernel.h>
 #include <linux/workqueue.h>
@@ -1402,6 +1403,10 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource,
 		update_rlimit_cpu(tsk, new_rlim->rlim_cur);
 out:
 	read_unlock(&tasklist_lock);
+
+	if (!retval && new_rlim && resource == RLIMIT_ITRACE)
+		retval = update_itrace_rlimit(tsk, new_rlim->rlim_cur);
+
 	return retval;
 }
 
Signature · 1 line
1.8.5.1
maintainer
#9 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 5 lines · Alexander Shishkin

Instruction tracing PMUs are capable of recording a log of instruction execution flow on a cpu core, which can be useful for profiling and crash analysis. This patch adds itrace infrastructure for perf events and the rest of the kernel to use.

Quoted 7 lines

Since such PMUs can produce copious amounts of trace data, it may be impractical to process it inside the kernel in real time, but instead export raw trace streams to userspace for subsequent analysis. Thus, itrace PMUs may export their trace buffers, which can be mmap()ed to userspace from a perf event fd with a PERF_EVENT_ITRACE_OFFSET offset. To that end, perf is extended to work with multiple ring buffers per event, reusing the ring_buffer code in an attempt to reduce complexity.

Please read the thread here: https://lkml.org/lkml/2008/12/4/64

On my thoughts of this creative mmap() usage.

tl;dr: no f*cking way.

author reviewer
#10 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 14 lines · Peter Zijlstra
Quoted 5 lines · Alexander Shishkin

Instruction tracing PMUs are capable of recording a log of instruction execution flow on a cpu core, which can be useful for profiling and crash analysis. This patch adds itrace infrastructure for perf events and the rest of the kernel to use.

Quoted 7 lines

Since such PMUs can produce copious amounts of trace data, it may be impractical to process it inside the kernel in real time, but instead export raw trace streams to userspace for subsequent analysis. Thus, itrace PMUs may export their trace buffers, which can be mmap()ed to userspace from a perf event fd with a PERF_EVENT_ITRACE_OFFSET offset. To that end, perf is extended to work with multiple ring buffers per event, reusing the ring_buffer code in an attempt to reduce complexity.

Quoted 3 lines

Please read the thread here: https://lkml.org/lkml/2008/12/4/64

On my thoughts of this creative mmap() usage.

That's unfortunate, it made sense to me. But let's then have a look at the alternative approaches. Bearing in mind that it is crucial for us to export trace buffers to userspace as opposed to processing the trace data in the kernel, the fact that we still need the normal perf data stream and your dislike for mmap trickery, we need two separate file
descriptors: one for the perf data and one for the trace data.

One way of doing this would be to call sys_perf_event_open() once for each. The first call would return a file descriptor, which provides good old perf data buffer; the second call would use this file descriptor for a group leader and will return another descriptor (thus creating another perf_event), which, when mmap()ed, will provide a trace buffer.

Or, we could introduce a new PERF_FLAG_XXX to mean that we want a descriptor with a trace buffer. And then, of course, one could always add an ioctl(), but that'd probably be a bit over the top.

Do any of these sound reasonable? Any other possibilities that I'm missing here?

Thanks,
--
Alex

maintainer
#11 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 18 lines · Alexander Shishkin
Quoted 16 lines · Peter Zijlstra
Quoted 5 lines · Alexander Shishkin

Instruction tracing PMUs are capable of recording a log of instruction execution flow on a cpu core, which can be useful for profiling and crash analysis. This patch adds itrace infrastructure for perf events and the rest of the kernel to use.

Quoted 7 lines

Since such PMUs can produce copious amounts of trace data, it may be impractical to process it inside the kernel in real time, but instead export raw trace streams to userspace for subsequent analysis. Thus, itrace PMUs may export their trace buffers, which can be mmap()ed to userspace from a perf event fd with a PERF_EVENT_ITRACE_OFFSET offset. To that end, perf is extended to work with multiple ring buffers per event, reusing the ring_buffer code in an attempt to reduce complexity.

Please read the thread here: https://lkml.org/lkml/2008/12/4/64

Quoted 8 lines
Quoted 1 line

On my thoughts of this creative mmap() usage.

That's unfortunate, it made sense to me. But let's then have a look at the alternative approaches. Bearing in mind that it is crucial for us to export trace buffers to userspace as opposed to processing the trace data in the kernel, the fact that we still need the normal perf data stream and your dislike for mmap trickery, we need two separate file
descriptors: one for the perf data and one for the trace data.

Why don't you start by explaining _why_ you need a second stream to begin with?

author reviewer
#12 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 28 lines · Peter Zijlstra
Quoted 18 lines · Alexander Shishkin
Quoted 16 lines · Peter Zijlstra
Quoted 5 lines · Alexander Shishkin

Instruction tracing PMUs are capable of recording a log of instruction execution flow on a cpu core, which can be useful for profiling and crash analysis. This patch adds itrace infrastructure for perf events and the rest of the kernel to use.

Quoted 7 lines

Since such PMUs can produce copious amounts of trace data, it may be impractical to process it inside the kernel in real time, but instead export raw trace streams to userspace for subsequent analysis. Thus, itrace PMUs may export their trace buffers, which can be mmap()ed to userspace from a perf event fd with a PERF_EVENT_ITRACE_OFFSET offset. To that end, perf is extended to work with multiple ring buffers per event, reusing the ring_buffer code in an attempt to reduce complexity.

Please read the thread here: https://lkml.org/lkml/2008/12/4/64

Quoted 8 lines
Quoted 1 line

On my thoughts of this creative mmap() usage.

That's unfortunate, it made sense to me. But let's then have a look at the alternative approaches. Bearing in mind that it is crucial for us to export trace buffers to userspace as opposed to processing the trace data in the kernel, the fact that we still need the normal perf data stream and your dislike for mmap trickery, we need two separate file
descriptors: one for the perf data and one for the trace data.

Quoted 2 lines

Why don't you start by explaining _why_ you need a second stream to begin with?

Oh, I'm sure I've explained it earlier ([1], [2]), but why not. The data in the second stream is generated at a rate which is hundreds of megabytes per second per core. Decoding this data is ~1000 times slower than generating it. Ergo, can't be done in kernel, needs to be exported as-is to userspace for later retreival and decoding. Doing it via perf stream means an extra copy, which at these rates is a waste. Ergo, a second buffer.

[1] https://lkml.org/lkml/2013/12/11/213
[2] https://lkml.org/lkml/2013/12/11/358

Regards,
--
Alex

maintainer
#13 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 4 lines · Alexander Shishkin
Quoted 2 lines

Why don't you start by explaining _why_ you need a second stream to begin with?

Oh, I'm sure I've explained it earlier ([1], [2])

See, I didn't read 0 because that information gets lost and patches should be self explanatory, and i didn't get to the Intel driver yet because well, I got stuck in the generic code.

Quoted 7 lines

but why not. The data
in the second stream is generated at a rate which is hundreds of megabytes per second per core. Decoding this data is ~1000 times slower than generating it. Ergo, can't be done in kernel, needs to be exported as-is to userspace for later retreival and decoding. Doing it via perf stream means an extra copy, which at these rates is a waste. Ergo, a second buffer.

Still confused, if you cannot copy it into one buffer, then why can you copy it into a second buffer?

author reviewer
#14 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Alexander Shishkin
Quoted 2 lines

Why don't you start by explaining _why_ you need a second stream to begin with?

Oh, I'm sure I've explained it earlier ([1], [2])

See, I didn't read 0 because that information gets lost and patches should be self explanatory, and i didn't get to the Intel driver yet because well, I got stuck in the generic code.

Sure. The general concept is more important than the actual driver at this point anyway.

Quoted 10 lines
Quoted 7 lines

but why not. The data
in the second stream is generated at a rate which is hundreds of megabytes per second per core. Decoding this data is ~1000 times slower than generating it. Ergo, can't be done in kernel, needs to be exported as-is to userspace for later retreival and decoding. Doing it via perf stream means an extra copy, which at these rates is a waste. Ergo, a second buffer.

Still confused, if you cannot copy it into one buffer, then why can you copy it into a second buffer?

It's not copied, hardware writes directly into that second buffer.

I've done the same with BTS now (as Ingo suggested) and it also benefits from this approach.

Regards,
--
Alex

maintainer
#15 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 4 lines · Alexander Shishkin
Quoted 2 lines

Still confused, if you cannot copy it into one buffer, then why can you copy it into a second buffer?

It's not copied, hardware writes directly into that second buffer.

Where's the PT documentation? I can't find it in the SDM and your ISA extensions link is a generic Intel website which is friggin useless (like all corporate websites strive to be).

Your actual PT patch doesn't describe how the things works either, and while I could go read the code, I'm too lazy.

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

Machinery like that would also be useful to zero-copy bits out of the buffer right into the page-cache.

Quoted 2 lines

I've done the same with BTS now (as Ingo suggested) and it also benefits from this approach.

The problem with DS is that it needs physically contiguous pages is it not? So you cannot really allocate a large buffer, and you end up needing to copy or swizzle stuff.

author reviewer
#16 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 9 lines · Peter Zijlstra
Quoted 4 lines · Alexander Shishkin
Quoted 2 lines

Still confused, if you cannot copy it into one buffer, then why can you copy it into a second buffer?

It's not copied, hardware writes directly into that second buffer.

Where's the PT documentation? I can't find it in the SDM and your ISA extensions link is a generic Intel website which is friggin useless (like all corporate websites strive to be).

[1]

Quoted 5 lines

Your actual PT patch doesn't describe how the things works either, and while I could go read the code, I'm too lazy.

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

I'm not sure I understand. You mean, have the buffer split between perf data and trace data?

Quoted 2 lines

Machinery like that would also be useful to zero-copy bits out of the buffer right into the page-cache.

Please elaborate.

Quoted 6 lines
Quoted 2 lines

I've done the same with BTS now (as Ingo suggested) and it also benefits from this approach.

The problem with DS is that it needs physically contiguous pages is it not? So you cannot really allocate a large buffer, and you end up needing to copy or swizzle stuff.

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

[1] http://download-software.intel.com/sites/default/files/managed/71/2e/319433-017.pdf

Regards,
--
Alex

maintainer
#17 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Peter Zijlstra

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

I'm not sure I understand. You mean, have the buffer split between perf data and trace data?

Yep, I don't see any reason why this wouldn't work.

When the hardware thing sends an interrupt to notify us its buffer is 'full', stop the recorder, try to create a single record in the buffer that's big enough + 1 page, then swizzle the hardware pages and the buffer pages for that record, using the +1 page to page align the actual data. Then (re)start the hardware on the 'new' pages.

author reviewer
#18 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Peter Zijlstra

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

I'm not sure I understand. You mean, have the buffer split between perf data and trace data?

Quoted 7 lines

Yep, I don't see any reason why this wouldn't work.

When the hardware thing sends an interrupt to notify us its buffer is 'full', stop the recorder, try to create a single record in the buffer that's big enough + 1 page, then swizzle the hardware pages and the buffer pages for that record, using the +1 page to page align the actual data. Then (re)start the hardware on the 'new' pages.

We configure the hardware thing to send an interrupt *before* the buffer is full, keep the recorder running while userspace saves stuff to perf.data file. Recording only stops if perf fails to read the trace data out fast enough and the buffer fills up. So you'd have a complete trace.

Also, we have what we call a "snapshot" mode, where we keep the hardware thing running, writing data to a circular buffer till it's stopped, in case we're only interested in the most recent trace data to see what it is that takes too long to respond, etc. And while it is running, we're getting new records in the perf stream all the time (mmaps, etc).

Put simple: perf data and trace data are two different separate types of information that originate from two different sources, can exist and make sense separately from one another and should not be mixed.

Regards,
--
Alex

maintainer
#19 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 30 lines · Alexander Shishkin
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Peter Zijlstra

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

I'm not sure I understand. You mean, have the buffer split between perf data and trace data?

Quoted 7 lines

Yep, I don't see any reason why this wouldn't work.

When the hardware thing sends an interrupt to notify us its buffer is 'full', stop the recorder, try to create a single record in the buffer that's big enough + 1 page, then swizzle the hardware pages and the buffer pages for that record, using the +1 page to page align the actual data. Then (re)start the hardware on the 'new' pages.

We configure the hardware thing to send an interrupt *before* the buffer is full, keep the recorder running while userspace saves stuff to perf.data file. Recording only stops if perf fails to read the trace data out fast enough and the buffer fills up. So you'd have a complete trace.

Also, we have what we call a "snapshot" mode, where we keep the hardware thing running, writing data to a circular buffer till it's stopped, in case we're only interested in the most recent trace data to see what it is that takes too long to respond, etc. And while it is running, we're getting new records in the perf stream all the time (mmaps, etc).

Quoted 3 lines

Put simple: perf data and trace data are two different separate types of information that originate from two different sources, can exist and make sense separately from one another and should not be mixed.

Well you're either having to change your stance or we're done talking right now.

author reviewer
#20 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 31 lines · Peter Zijlstra
Quoted 30 lines · Alexander Shishkin
Quoted 8 lines · Peter Zijlstra
Quoted 6 lines · Alexander Shishkin
Quoted 2 lines · Peter Zijlstra

The thing is; why can't you zero-copy whatever buffer the hardware writes into, into the normal buffer?

I'm not sure I understand. You mean, have the buffer split between perf data and trace data?

Quoted 7 lines

Yep, I don't see any reason why this wouldn't work.

When the hardware thing sends an interrupt to notify us its buffer is 'full', stop the recorder, try to create a single record in the buffer that's big enough + 1 page, then swizzle the hardware pages and the buffer pages for that record, using the +1 page to page align the actual data. Then (re)start the hardware on the 'new' pages.

We configure the hardware thing to send an interrupt *before* the buffer is full, keep the recorder running while userspace saves stuff to perf.data file. Recording only stops if perf fails to read the trace data out fast enough and the buffer fills up. So you'd have a complete trace.

Also, we have what we call a "snapshot" mode, where we keep the hardware thing running, writing data to a circular buffer till it's stopped, in case we're only interested in the most recent trace data to see what it is that takes too long to respond, etc. And while it is running, we're getting new records in the perf stream all the time (mmaps, etc).

Quoted 6 lines
Quoted 3 lines

Put simple: perf data and trace data are two different separate types of information that originate from two different sources, can exist and make sense separately from one another and should not be mixed.

Well you're either having to change your stance or we're done talking right now.

I'm making a case in favor of 2 separate buffers just like you asked in one of the previous emails. It's backed by some very real usecases. That said, I'm not personally attached to any one design, only what makes sense. There is no 'stance'.

Regards,
--
Alex

maintainer
#21 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Alexander Shishkin
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

Also, 'some implementations', that sounds like a fail right there. Why are there already different implementations, and some which such stupid design, of something this new?

How about just saying NO to the ones that requires physically contiguous allocations?

author reviewer
#22 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

One chunk is 8M. You can have as many as the buddy allocator permits you to have. When you get a PMI, you simply switch one chunk for another and on the tracing goes.

Quoted 6 lines

Also, 'some implementations', that sounds like a fail right there. Why are there already different implementations, and some which such stupid design, of something this new?

How about just saying NO to the ones that requires physically contiguous allocations?

No reason to leave those out, because they are still extremely useful for tracing and fit perfectly fine in a model with two buffers.

Regards,
--
Alex

maintainer
#23 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 7 lines · Alexander Shishkin
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

Quoted 7 lines
Quoted 3 lines

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

One chunk is 8M. You can have as many as the buddy allocator permits you to have. When you get a PMI, you simply switch one chunk for another and on the tracing goes.

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

That said, I'm still reading..

Quoted 9 lines
Quoted 6 lines

Also, 'some implementations', that sounds like a fail right there. Why are there already different implementations, and some which such stupid design, of something this new?

How about just saying NO to the ones that requires physically contiguous allocations?

No reason to leave those out, because they are still extremely useful for tracing and fit perfectly fine in a model with two buffers.

Maybe; but lets start with the sane hardware. Then we'll look at the amount of pain needed to support these broken pieces of crap and decide later.

So drop all support for crappy hardware now.

author reviewer
#24 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 12 lines · Peter Zijlstra
Quoted 11 lines · Alexander Shishkin
Quoted 8 lines · Peter Zijlstra
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

Quoted 8 lines
Quoted 3 lines

One chunk is 8M. You can have as many as the buddy allocator permits you to have. When you get a PMI, you simply switch one chunk for another and on the tracing goes.

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

JFYI, 11.2.4.1, "Single Output Region ToPA Implementation".

Regards,
--
Alex

maintainer
#25 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Peter Zijlstra
Quoted 6 lines · Peter Zijlstra

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

That said, I'm still reading..

Found it:

"Single Output Region ToPA Implementation

The first processor generation to implement Intel PT supports only ToPA configurations with a single ToPA entry followed by an END entry that points back to the first entry (creating one circular output buffer). Such processors enumerate CPUID.(EAX=14H,ECX=0):EBX[bit 1] as 0."

So basically you guys buggered the hardware.

More specifically, what actual hardware is this? Is this first generation HSW or so?

Please enumerate the actual hardware that supports this PT stuff and which hardware has it fixed.

maintainer
#26 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Found more:

"Note that no “freezing” takes place with the ToPA PMI. Thus, packet generation is not frozen, and the interrupt handler will be traced (though filtering can prevent this). Further, the setting of IA32_DEBUGCTL.Freeze_Perfmon_on_PMI is ignored and performance counters are not frozen by a ToPA PMI."

Can someone confirm with the hardware people what happens when an actual PMU counter overflows and tries to raise the PMI while we're in one that ignores the 'Freeze_perfmon_on_PMI' bit?

Since you cannot assert an interrupt that already asserted, but that handler can see the overflow status bit set and will likely process it; assuming the PMU is actually frozen.

Also, this just smells ripe for errata and ugly bugs.

maintainer
#27 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Peter Zijlstra
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Peter Zijlstra

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

That said, I'm still reading..

Found it:

Quoted 9 lines

"Single Output Region ToPA Implementation

The first processor generation to implement Intel PT supports only ToPA configurations with a single ToPA entry followed by an END entry that points back to the first entry (creating one circular output buffer). Such processors enumerate CPUID.(EAX=14H,ECX=0):EBX[bit 1] as 0."

So basically you guys buggered the hardware.

"ToPA PMI and Single Output Region ToPA Implementation

A processor that supports only a single ToPA output region implementation (such that only one output region is supported; see above) will attempt to signal a ToPA PMI interrupt before the output wraps and overwrites the top of the buffer. To support this functionality, the PMI handler should disable packet generation as soon as possible. Due to PMI skid, it is possible, in rare cases, that the wrap will have occurred before the PMI is delivered. Software can avoid this by setting the STOP bit in the ToPA entry (see Table 11-3); this will disable tracing once the region is filled, and no wrap will occur. This approach has the downside of disabling packet generation so that some of the instructions that led up to the PMI will not be traced. If the PMI skid is significant enough to cause the region to fill and tracing to be disabled, the PMI handler will need to clear the IA32_RTIT_STATUS.Stopped indication before tracing can resume."

So you're basically forced to stop the tracing on PMI anyhow; so your continuous tracing argument goes out the window.

Also, what a complete clusterfuck. I think we're far better of pretending PT doesn't exist until its fixed.

author reviewer
#28 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 3 lines · Peter Zijlstra

On Thu, Dec 19, 2013 at 12:57:59PM +0100, Peter Zijlstra wrote:
So you're basically forced to stop the tracing on PMI anyhow; so your continuous tracing argument goes out the window.

It's only stopped inside the PMI handler to set up another buffer, and is then started again, so no useful trace is lost. PMI handler is not traced. What you're proposing is stopping it for good till perf collects the previous data, which will lose us a lot of trace. So my argument stands.

Regards,
--
Alex

maintainer
#29 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 11 lines · Alexander Shishkin
Quoted 3 lines · Peter Zijlstra

On Thu, Dec 19, 2013 at 12:57:59PM +0100, Peter Zijlstra wrote:
So you're basically forced to stop the tracing on PMI anyhow; so your continuous tracing argument goes out the window.

It's only stopped inside the PMI handler to set up another buffer, and is then started again, so no useful trace is lost. PMI handler is not traced. What you're proposing is stopping it for good till perf collects the previous data, which will lose us a lot of trace. So my argument stands.

That is not what I proposed at all.

The PMI will swizzle the pages and resume recording. If there is no space in the output buffer, we'll simply re-use the existing pages and overwrite data.

#30 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Peter Zijlstra
Quoted 30 lines · Peter Zijlstra
Quoted 7 lines · Alexander Shishkin
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

Quoted 7 lines
Quoted 3 lines

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

One chunk is 8M. You can have as many as the buddy allocator permits you to have. When you get a PMI, you simply switch one chunk for another and on the tracing goes.

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

That said, I'm still reading..

Quoted 7 lines
Quoted 6 lines

Also, 'some implementations', that sounds like a fail right there. Why are there already different implementations, and some which such stupid design, of something this new?

How about just saying NO to the ones that requires physically contiguous allocations?

Quoted 8 lines
Quoted 2 lines

No reason to leave those out, because they are still extremely useful for tracing and fit perfectly fine in a model with two buffers.

Maybe; but lets start with the sane hardware. Then we'll look at the amount of pain needed to support these broken pieces of crap and decide later.

So drop all support for crappy hardware now.

Absolutely agreed ...

The thing is, BTS itself is rarely used (and not primarily because it's slow, but because its tooling and thus its utility is poor), so the last thing we want is another piece of broken hardware with a quirky software interface to it that tooling has trouble utilizing.

Sigh, when will Intel learn to talk to Linux PMU experts _before_ committing to a hardware interface??

Thanks,

Ingo

author reviewer
#31 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 39 lines · Ingo Molnar
Quoted 37 lines · Peter Zijlstra
Quoted 7 lines · Alexander Shishkin
Quoted 5 lines · Peter Zijlstra
Quoted 3 lines · Alexander Shishkin

Yes and some implementations of PT have the same issue, but you can do a sufficiently large high order allocation and map it to userspace and still no copying (or parsing/decoding) in kernel space required.

Quoted 7 lines
Quoted 3 lines

What's sufficiently large? The largest we could possibly allocate is something like 4k^11 which is 8M or so. That's not all that big given you keep saying it generates in the order of 100 MB/s.

One chunk is 8M. You can have as many as the buddy allocator permits you to have. When you get a PMI, you simply switch one chunk for another and on the tracing goes.

This document you referred me to looks to specify something with a proper s/g implementation; called ToPA. There doesn't appear to be a limit to the linked entries and you can specify a size per entry, and I don't see anywhere why 4k would be bad.

That said, I'm still reading..

Quoted 9 lines
Quoted 6 lines

Also, 'some implementations', that sounds like a fail right there. Why are there already different implementations, and some which such stupid design, of something this new?

How about just saying NO to the ones that requires physically contiguous allocations?

No reason to leave those out, because they are still extremely useful for tracing and fit perfectly fine in a model with two buffers.

Maybe; but lets start with the sane hardware. Then we'll look at the amount of pain needed to support these broken pieces of crap and decide later.

Quoted 8 lines
Quoted 1 line

So drop all support for crappy hardware now.

Absolutely agreed ...

The thing is, BTS itself is rarely used (and not primarily because it's slow, but because its tooling and thus its utility is poor), so the last thing we want is another piece of broken hardware with a quirky software interface to it that tooling has trouble utilizing.

Or the interface and implementation of BTS support in the kernel discourage its use and that is why it is so rarely used.

What I'm proposing is a unified interface for trace units to export their traces and not only the "non-crappy" ones, in a way that won't discourage its use from day one.

So I'd like to steer away from the ways in which hardware can be broken and talk about a usable interface, to begin with.

Regargs,
--
Alex

#32 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 2 lines · Alexander Shishkin

Or the interface and implementation of BTS support in the kernel discourage its use and that is why it is so rarely used.

I never heard complains about it. It's a simple dump of from/to address couples. I just think nobody take the time to develop userspace tooling to exploit it. But it's famous slowness might have had a bad influence on this. And may be also the fact that it's very architecture specific. AMD doesn't support BTS if I recall correctly. Or may be it has its own different implementation?

maintainer
#33 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 9 lines · Frederic Weisbecker
Quoted 2 lines · Alexander Shishkin

Or the interface and implementation of BTS support in the kernel discourage its use and that is why it is so rarely used.

I never heard complains about it. It's a simple dump of from/to address couples. I just think nobody take the time to develop userspace tooling to exploit it. But it's famous slowness might have had a bad influence on this. And may be also the fact that it's very architecture specific. AMD doesn't support BTS if I recall correctly. Or may be it has its own different implementation?

No AMD doesn't do anything like that.

There was some attempt to cure some of the wobblies:

https://lkml.org/lkml/2013/7/8/154

But people never pursued that.

That said, if people want overwrite mode to work for PT we'd need to fix the same thing.

maintainer
#34 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Alexander Shishkin
Quoted 2 lines · Alexander Shishkin

So I'd like to steer away from the ways in which hardware can be broken and talk about a usable interface, to begin with.

Just dump it into the regular one buffer like I outlined.

That said; we very much need to have at least two architectures implemented for any of this code to move.

But we cannot ignore the hardware trainwreck; we cannot shape our interface around something that's utterly broken.

Some hardware is just too broken to support.

#35 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Alexander Shishkin

So I'd like to steer away from the ways in which hardware can be broken and talk about a usable interface, to begin with.

Just dump it into the regular one buffer like I outlined.

Just getting back to this.

Do you realize that PT buffers have to be page aligned.

So to mix it with a regular perf buffer would need padding every PT message by 4K, which wastes a lot of memory. The side band messages are usually only a few bytes (e.g. context switch).

If the sideband is mfrequent it could even take up >half of the buffer, but mostly only with padding.

Is that what you intended?

perf doesn't support gaps today, so your proposal wouldn't even seem to fit into the current perf design.

Also of course it requires disabling/enabling PT explicitly for every perf message, which is slow. So you add at least 2*WRMSR cost (thousands of cycles).

Quoted 7 lines

That said; we very much need to have at least two architectures implemented for any of this code to move.

But we cannot ignore the hardware trainwreck; we cannot shape our interface around something that's utterly broken.

Some hardware is just too broken to support.

I don't think the PT design is broken in any way, it's straight forward and simple.

Trying to mix hardware tracing and software tracing in the same buffer on the other hand ...

Anyways if perf is not flexible enough to support this I suppose it could switch to a simple device driver, and only run perf with separate fds for side band purposes.

Would you prefer that?

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#36 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 16 lines · Andi Kleen
Quoted 5 lines · Peter Zijlstra
Quoted 2 lines · Alexander Shishkin

So I'd like to steer away from the ways in which hardware can be broken and talk about a usable interface, to begin with.

Just dump it into the regular one buffer like I outlined.

Just getting back to this.

Do you realize that PT buffers have to be page aligned.

So to mix it with a regular perf buffer would need padding every PT message by 4K, which wastes a lot of memory. The side band messages are usually only a few bytes (e.g. context switch).

Quoted 7 lines

If the sideband is mfrequent it could even take up >half of the buffer, but mostly only with padding.

Is that what you intended?

perf doesn't support gaps today, so your proposal wouldn't even seem to fit into the current perf design.

That would a really trivial addition.

Quoted 3 lines

Also of course it requires disabling/enabling PT explicitly for every perf message, which is slow. So you add at least 2*WRMSR cost (thousands of cycles).

That's just dumb, no flush the entire PT buffer into a few large records.

Quoted 10 lines
Quoted 7 lines

That said; we very much need to have at least two architectures implemented for any of this code to move.

But we cannot ignore the hardware trainwreck; we cannot shape our interface around something that's utterly broken.

Some hardware is just too broken to support.

I don't think the PT design is broken in any way, it's straight forward and simple.

If it were actually implemented like the spec says and not have this crappy S/G limitation, then maybe.

Quoted 8 lines

Trying to mix hardware tracing and software tracing in the same buffer on the other hand ...

Anyways if perf is not flexible enough to support this I suppose it could switch to a simple device driver, and only run perf with separate fds for side band purposes.

Would you prefer that?

Don't be stupid.

#37 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 6 lines
Quoted 3 lines

Also of course it requires disabling/enabling PT explicitly for every perf message, which is slow. So you add at least 2*WRMSR cost (thousands of cycles).

That's just dumb, no flush the entire PT buffer into a few large records.

How would that work?

You mean a separate buffer and then copy or map?

------

Also here are some more problems with interleaving:

A common PT config is to just run it as a ring buffer in the background and only take the data out when something happens (sample, crash etc.)

But the side band still needs to be logged and at arbitary times.

So the PT wrapping will happen much more often than the perf wrapping.

If you interleave you may actually end up with lots of small rings in a single buffer, unless you stop every time the buffer fills up (which would add a lot more overhead)

I suppose it could be somehow parsed, but it would very different from what perf does today.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
#38 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 20 lines · Andi Kleen
Quoted 6 lines
Quoted 3 lines

Also of course it requires disabling/enabling PT explicitly for every perf message, which is slow. So you add at least 2*WRMSR cost (thousands of cycles).

That's just dumb, no flush the entire PT buffer into a few large records.

How would that work?

You mean a separate buffer and then copy or map?

------

Also here are some more problems with interleaving:

A common PT config is to just run it as a ring buffer in the background and only take the data out when something happens (sample, crash etc.)

But the side band still needs to be logged and at arbitary times.

Quoted 8 lines

So the PT wrapping will happen much more often than the perf wrapping.

If you interleave you may actually end up with lots of small rings in a single buffer, unless you stop every time the buffer fills up (which would add a lot more overhead)

I suppose it could be somehow parsed, but it would very different from what perf does today.

Thinking about it more it's likely very hard to parse. Dropping instructions is fine, dropping perf metadata is not (or only as last resort).

If we miss a MMAP we may never be able to parse that code region. If we miss a context switch we may be also completely lost until the next switch.

That means PT couldn't overwrite perf metadata normally.

So you could easily get into situations where the interleaved PT buffer is between two perf metadata statements and ends up really small, while large other parts of the buffer are unused.

The only way around it would be likely to move entries around -- to garbage collect so to say -- but doing that non-blocking from a NMI will be challenging.

With the separate buffers we don't have any of these problems.

-Andi

maintainer
#39 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Andi Kleen
Quoted 13 lines · Andi Kleen
Quoted 6 lines
Quoted 3 lines

Also of course it requires disabling/enabling PT explicitly for every perf message, which is slow. So you add at least 2*WRMSR cost (thousands of cycles).

That's just dumb, no flush the entire PT buffer into a few large records.

How would that work?

You mean a separate buffer and then copy or map?

------

Quoted 8 lines

Also here are some more problems with interleaving:

A common PT config is to just run it as a ring buffer in the background and only take the data out when something happens (sample, crash etc.)

But the side band still needs to be logged and at arbitary times.

So the PT wrapping will happen much more often than the perf wrapping.

So create two events, one for the PT stuff and one to track the side-band stuff. We have a NOP event for just this purpose.

#40 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 2 lines

So create two events, one for the PT stuff and one to track the side-band stuff. We have a NOP event for just this purpose.

Ok I guess that could work.

Essentially replace the magic mmap offset with a second fd.

Alex, what do you think?

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
author reviewer
#41 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 8 lines · Andi Kleen
Quoted 2 lines

So create two events, one for the PT stuff and one to track the side-band stuff. We have a NOP event for just this purpose.

Ok I guess that could work.

Essentially replace the magic mmap offset with a second fd.

Alex, what do you think?

Yes, that's what I suggested some time ago in [1]. A second buffer (through another fd or otherwise) is an essential thing from my point of view.

[1] http://marc.info/?l=linux-kernel&m=138737306725663

Regards,
--
Alex

maintainer
#42 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Andi Kleen
Quoted 2 lines

I don't think the PT design is broken in any way, it's straight forward and simple.

Also, do clarify the other points I asked about. Esp. the non FREEZE_ON_PMI behaviour of the PT PMI is worrying me immensely.

To me it seems very weird that PT is hooked to the same PMI as the normal PMU, it really should have been a different interrupt.

#43 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Peter Zijlstra <[email protected]> writes:

Can you please clarify your position on the interleaved buffer?

I still can't see how it is a efficient design.

It's generally true in scather-gather (be it software or hardware) that each additional SG entry increases the cost. So to make things efficient you always want to minimize entries as much as possible.

Quoted 5 lines
Quoted 2 lines

I don't think the PT design is broken in any way, it's straight forward and simple.

Also, do clarify the other points I asked about. Esp. the non FREEZE_ON_PMI behaviour of the PT PMI is worrying me immensely.

The only reason for hardware freeze is when you have a few entries (like with LBRs) so the interrupt entry code could overwhelm it.

But PT is not small, it's gigantic: even with the smallest buffer you have many thousands of entries.

So you will get a few branches in the interrupt entry, but it's not a problem because everything you really wanted to trace is still there.

Eventually the handler disables PT, so there's no risk of racing with the update or anything like that.

Did I miss anything?

Quoted 2 lines

To me it seems very weird that PT is hooked to the same PMI as the normal PMU, it really should have been a different interrupt.

It's in the same STATUS register, so it's cheap to check both.

It shouldn't add any new spurious problems (or at least nothing worse than what we already have)

I understand that it would be nice to separate other NMI users from all of PMI, but that would be an orthogonal problem.

Any other issues?

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#44 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 10 lines · Andi Kleen
Quoted 2 lines · Peter Zijlstra

Also, do clarify the other points I asked about. Esp. the non FREEZE_ON_PMI behaviour of the PT PMI is worrying me immensely.

The only reason for hardware freeze is when you have a few entries (like with LBRs) so the interrupt entry code could overwhelm it.

But PT is not small, it's gigantic: even with the smallest buffer you have many thousands of entries.

Quoted 7 lines

So you will get a few branches in the interrupt entry, but it's not a problem because everything you really wanted to trace is still there.

Eventually the handler disables PT, so there's no risk of racing with the update or anything like that.

Did I miss anything?

Yes; go read this:

lkml.kernel.org/r/[email protected]

#45 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 3 lines

Yes; go read this:

lkml.kernel.org/r/[email protected]

Hmm, but AFAIK we're not using freeze counters on PMI today. We just rely on the explicit disabling in the counters through the global ctrl.

So it should be the same as with any other PMI which also does not automatically freeze. Not true?

Or do you mean interaction with the LBRs here?
(currently LBRs and PT are mutually exclusive)

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only.
maintainer
#46 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 10 lines · Andi Kleen
Quoted 3 lines

Yes; go read this:

lkml.kernel.org/r/[email protected]

Hmm, but AFAIK we're not using freeze counters on PMI today. We just rely on the explicit disabling in the counters through the global ctrl.

So it should be the same as with any other PMI which also does not automatically freeze. Not true?

Regardless whether its used or not; I'd very much like that answered.

Quoted 2 lines

Or do you mean interaction with the LBRs here?
(currently LBRs and PT are mutually exclusive)

Yes we very much rely on the FREEZE bits for LBR. PT and LBR being mutually exclusive wasn't documented (or I missed it) and completely blows.

#47 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 5 lines · Peter Zijlstra
Quoted 4 lines · Andi Kleen
Quoted 3 lines

Yes; go read this:

lkml.kernel.org/r/[email protected]

Quoted 8 lines
Quoted 6 lines

Hmm, but AFAIK we're not using freeze counters on PMI today. We just rely on the explicit disabling in the counters through the global ctrl.

So it should be the same as with any other PMI which also does not automatically freeze. Not true?

Regardless whether its used or not; I'd very much like that answered.

The freeze always starts with the counter overflow, independent if the interrupt is blocked or not. So everything should be ok.

-Andi

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

maintainer
#50 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

restoring the list.. I really should drop all emails you send off list into /dev/null.

Quoted 22 lines · Peter Zijlstra
Quoted 6 lines · Andi Kleen
Quoted 3 lines

Yes we very much rely on the FREEZE bits for LBR. PT and LBR being mutually exclusive wasn't documented (or I missed it) and completely blows.

Can you describe why it is a problem? I had considered it only a minor inconvenience, for many things you would use LBRs for PT is far better.

Because is someone writes a GCC tool using perf-LBR support for some basic block analysis, and someone else writes another tool for PT, then the first tool magically stops working when the PT tool is started.

We cannot refuse to create perf-LBR events, because at that time there might not be a PT user -- and even if there was one, it might go away.

But as long as there's a PT user around, the LBR events will not be able to be scheduled and will simply starve, for no apparent reason.

Complete and utterly miserable position.

And it makes sense to write LBR tools because they cover a much greater spread of hardware.

maintainer
#51 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

In reply to Andi Kleen
Quoted 5 lines · Andi Kleen
Quoted 2 lines

To me it seems very weird that PT is hooked to the same PMI as the normal PMU, it really should have been a different interrupt.

It's in the same STATUS register, so it's cheap to check both.

Quoted 7 lines

It shouldn't add any new spurious problems (or at least nothing worse than what we already have)

I understand that it would be nice to separate other NMI users from all of PMI, but that would be an orthogonal problem.

Any other issues?

Aside from the fact that PT and the PMU are otherwise unrelated, so it being in the global status register is weird too.

Also, the PT interrupt doesn't actually need to be an NMI; when the proposed S/G implementation would actually work as stated there can be plenty room left when we trigger the interrupt.

But again, see the other email I referenced; the PMU triggering a PMI while we're in one PT triggered is my biggest concern; esp. since both have different FREEZE semantics.

#52 lore raw

Re: [PATCH v0 04/71] itrace: Infrastructure for instruction flow tracing units

Quoted 3 lines

Also, the PT interrupt doesn't actually need to be an NMI; when the proposed S/G implementation would actually work as stated there can be plenty room left when we trigger the interrupt.

That's true.

-andi

Signature · 1 line
[email protected] -- Speaking for myself only.
author reviewer
#53 lore raw

PATCH 5/71 x86: perf: Intel PT PMU driver

In reply to Alexander Shishkin

Add support for Intel Processor Trace (PT) to kernel's perf/itrace events. PT is an extension of Intel Architecture that collects information about software execuction such as control flow, execution modes and timings and formats it into highly compressed binary packets. Even being compressed, these packets are generated at hundreds of megabytes per second per core, which makes it impractical to decode them on the fly in the kernel. Thus, buffers containing this binary stream are zero-copy mapped to the debug tools in userspace for subsequent decoding and analysis.

PT trace data can also be used to annotate other perf events by setting a corresponding bit in sample_type; it can also be included in process core dumps. This relies on itrace infrastructure extension to perf core.

---
 arch/x86/include/uapi/asm/msr-index.h     |   18 +
 arch/x86/kernel/cpu/Makefile              |    1 +
 arch/x86/kernel/cpu/intel_pt.h            |  129 ++++
 arch/x86/kernel/cpu/perf_event.c          |    4 +
 arch/x86/kernel/cpu/perf_event_intel.c    |   10 +
 arch/x86/kernel/cpu/perf_event_intel_pt.c | 1167 +++++++++++++++++++++++++++++
 6 files changed, 1329 insertions(+)
 create mode 100644 arch/x86/kernel/cpu/intel_pt.h
 create mode 100644 arch/x86/kernel/cpu/perf_event_intel_pt.c
arch/x86/include/uapi/asm/msr-index.h +18 -0
index b93e09a..6dfa422 100644
@@ -74,6 +74,24 @@
 #define MSR_IA32_PERF_CAPABILITIES	0x00000345
 #define MSR_PEBS_LD_LAT_THRESHOLD	0x000003f6
 
+#define MSR_IA32_RTIT_CTL		0x00000570
+#define RTIT_CTL_TRACEEN		BIT(0)
+#define RTIT_CTL_OS			BIT(2)
+#define RTIT_CTL_USR			BIT(3)
+#define RTIT_CTL_CR3EN			BIT(7)
+#define RTIT_CTL_TOPA			BIT(8)
+#define RTIT_CTL_TSC_EN			BIT(10)
+#define RTIT_CTL_DISRETC		BIT(11)
+#define RTIT_CTL_BRANCH_EN		BIT(13)
+#define MSR_IA32_RTIT_STATUS		0x00000571
+#define RTIT_STATUS_CONTEXTEN		BIT(1)
+#define RTIT_STATUS_TRIGGEREN		BIT(2)
+#define RTIT_STATUS_ERROR		BIT(4)
+#define RTIT_STATUS_STOPPED		BIT(5)
+#define MSR_IA32_RTIT_CR3_MATCH		0x00000572
+#define MSR_IA32_RTIT_OUTPUT_BASE	0x00000560
+#define MSR_IA32_RTIT_OUTPUT_MASK	0x00000561
+
 #define MSR_MTRRfix64K_00000		0x00000250
 #define MSR_MTRRfix16K_80000		0x00000258
 #define MSR_MTRRfix16K_A0000		0x00000259
arch/x86/kernel/cpu/Makefile +1 -0
index 6359506..cb69de3 100644
@@ -37,6 +37,7 @@ endif
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_p6.o perf_event_knc.o perf_event_p4.o
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_lbr.o perf_event_intel_ds.o perf_event_intel.o
 obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_uncore.o perf_event_intel_rapl.o
+obj-$(CONFIG_CPU_SUP_INTEL)		+= perf_event_intel_pt.o
 endif
 
 
arch/x86/kernel/cpu/intel_pt.h new file +129 -0

129 lines not on this page · raw message

arch/x86/kernel/cpu/perf_event.c +4 -0
index 8e13293..9125797 100644
@@ -385,6 +385,10 @@ static inline int precise_br_compat(struct perf_event *event)
 
 int x86_pmu_hw_config(struct perf_event *event)
 {
+	if (event->attr.sample_type & PERF_SAMPLE_ITRACE &&
+	    event->attr.sample_type & PERF_SAMPLE_BRANCH_STACK)
+		return -EINVAL;
+
 	if (event->attr.precise_ip) {
 		int precise = 0;
 
arch/x86/kernel/cpu/perf_event_intel.c +10 -0
index 0fa4f24..28b5023 100644
@@ -1312,6 +1312,8 @@ int intel_pmu_save_and_restart(struct perf_event *event)
 	return x86_perf_event_set_period(event);
 }
 
+void intel_pt_interrupt(void);
+
 static void intel_pmu_reset(void)
 {
 	struct debug_store *ds = __this_cpu_read(cpu_hw_events.ds);
@@ -1393,6 +1395,14 @@ again:
 	}
 
 	/*
+	 * Intel PT
+	 */
+	if (__test_and_clear_bit(55, (unsigned long *)&status)) {
+		handled++;
+		intel_pt_interrupt();
+	}
+
+	/*
 	 * Checkpointed counters can lead to 'spurious' PMIs because the
 	 * rollback caused by the PMI will have cleared the overflow status
 	 * bit. Therefore always force probe these counters.
arch/x86/kernel/cpu/perf_event_intel_pt.c new file +1167 -0

1,167 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#54 lore raw

PATCH 6/71 perf: Allow set-output for task contexts of different types

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Set-output must be limited to events that cannot be active on different cpus at the same time. Thus either the event cpu must be the same, or the event task must be the same. Current logic does not check the task directly but checks whether the perf_event_context is the same, however there are separate contexts for hardware and software events so in that case the perf_event_context is different even though the task is the same. This patch changes the logic to check the task directly.

---
 kernel/events/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/events/core.c +2 -1
index ca8a130..93d712d 100644
@@ -6982,7 +6982,8 @@ perf_event_set_output(struct perf_event *event, struct perf_event *output_event)
 	/*
 	 * If its not a per-cpu rb, it must be the same task.
 	 */
-	if (output_event->cpu == -1 && output_event->ctx != event->ctx)
+	if (output_event->cpu == -1 &&
+	    output_event->ctx->task != event->ctx->task)
 		goto out;
 	/*
 	 * XXX^2: that's all bollocks
Signature · 1 line
1.8.5.1
author reviewer
#55 lore raw

PATCH 7/71 perf tools: Record whether a dso is 64-bit

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a flag to 'struct dso' to record if the dso is 64-bit or not. Update the flag when reading the ELF.

This is needed for instruction decoding. For example, x86 instruction decoding depends on whether or not the 64-bit instruction set is used.

---
 tools/perf/util/dso.c            |  1 +
 tools/perf/util/dso.h            |  1 +
 tools/perf/util/symbol-elf.c     |  3 +++
 tools/perf/util/symbol-minimal.c | 23 +++++++++++++++++++++++
 tools/perf/util/symbol.c         |  1 +
 tools/perf/util/symbol.h         |  1 +
 6 files changed, 30 insertions(+)
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
 		dso->cache = RB_ROOT;
 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+		dso->is_64_bit = (sizeof(void *) == 8);
 		dso->loaded = 0;
 		dso->rel = 0;
 		dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
 	u8		 annotate_warned:1;
 	u8		 sname_alloc:1;
 	u8		 lname_alloc:1;
+	u8		 is_64_bit:1;
 	u8		 sorted_by_name;
 	u8		 loaded;
 	u8		 rel;
tools/perf/util/symbol-elf.c +3 -0
index eed0b96..a0fc81b 100644
@@ -595,6 +595,8 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 			goto out_elf_end;
 	}
 
+	ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
+
 	ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
 			NULL);
 	if (ss->symshdr.sh_type != SHT_SYMTAB)
@@ -694,6 +696,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
 	bool remap_kernel = false, adjust_kernel_syms = false;
 
 	dso->symtab_type = syms_ss->type;
+	dso->is_64_bit = syms_ss->is_64_bit;
 	dso->rel = syms_ss->ehdr.e_type == ET_REL;
 
 	/*
tools/perf/util/symbol-minimal.c +23 -0
index ac7070a..b9d1119 100644
@@ -1,3 +1,4 @@
+#include "util.h"
 #include "symbol.h"
 
 #include <stdio.h>
@@ -287,6 +288,23 @@ int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
 	return 0;
 }
 
+static int fd__is_64_bit(int fd)
+{
+	u8 e_ident[EI_NIDENT];
+
+	if (lseek(fd, 0, SEEK_SET))
+		return -1;
+
+	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
+		return -1;
+
+	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+	    e_ident[EI_VERSION] != EV_CURRENT)
+		return -1;
+
+	return e_ident[EI_CLASS] == ELFCLASS64;
+}
+
 int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
 		  struct symsrc *ss,
 		  struct symsrc *runtime_ss __maybe_unused,
@@ -294,6 +312,11 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
 		  int kmodule __maybe_unused)
 {
 	unsigned char *build_id[BUILD_ID_SIZE];
+	int ret;
+
+	ret = fd__is_64_bit(ss->fd);
+	if (ret >= 0)
+		dso->is_64_bit = ret;
 
 	if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
 		dso__set_build_id(dso, build_id);
tools/perf/util/symbol.c +1 -0
index de87dba..511de06 100644
@@ -1103,6 +1103,7 @@ static int dso__load_kcore(struct dso *dso, struct map *map,
 			      &is_64_bit);
 	if (err)
 		goto out_err;
+	dso->is_64_bit = is_64_bit;
 
 	if (list_empty(&md.maps)) {
 		err = -EINVAL;
tools/perf/util/symbol.h +1 -0
index f1031a1..7f23a05 100644
@@ -195,6 +195,7 @@ struct symsrc {
 	GElf_Shdr dynshdr;
 
 	bool adjust_symbols;
+	bool is_64_bit;
 #endif
 };
 
Signature · 1 line
1.8.5.1
#56 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

tools/perf/util/dso.c struct dso *dso__new(const char *name) · line 449

Quoted 9 lines · Alexander Shishkin
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
  		dso->cache = RB_ROOT;
  		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+		dso->is_64_bit = (sizeof(void *) == 8);

Isnt' that going to record the bitness of perf when it is compiled?

tools/perf/util/dso.h struct dso { · line 94

Quoted 12 lines
tools/perf/util/dso.c +0 -0
  		dso->loaded = 0;
  		dso->rel = 0;
  		dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
  	u8		 annotate_warned:1;
  	u8		 sname_alloc:1;
  	u8		 lname_alloc:1;
+	u8		 is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

David

#57 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Em Wed, Dec 11, 2013 at 12:26:21PM -0700, David Ahern escreveu:

Quoted 12 lines · Alexander Shishkin

tools/perf/util/dso.c struct dso *dso__new(const char *name) · line 449

Quoted 9 lines
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
 		dso->cache = RB_ROOT;
 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+		dso->is_64_bit = (sizeof(void *) == 8);

Isnt' that going to record the bitness of perf when it is compiled?

Right, it will. Its just a default, I assume this will be reset after the DSO is loaded, i.e. the ELF file gets accessed, no?

Which then would make this init to be just a distraction, no?

I wonder if we couldn't extern PERF_RECORD_MMAP to have the binary type encoded there somehow...

Quoted 19 lines

tools/perf/util/dso.h struct dso { · line 94

Quoted 12 lines
tools/perf/util/dso.c +0 -0
 		dso->loaded = 0;
 		dso->rel = 0;
 		dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
 	u8		 annotate_warned:1;
 	u8		 sname_alloc:1;
 	u8		 lname_alloc:1;
+	u8		 is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

David

reviewer
#58 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Quoted 14 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 11, 2013 at 12:26:21PM -0700, David Ahern escreveu:

Quoted 12 lines · Alexander Shishkin

tools/perf/util/dso.c struct dso *dso__new(const char *name) · line 449

Quoted 9 lines
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
 		dso->cache = RB_ROOT;
 		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
 		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+		dso->is_64_bit = (sizeof(void *) == 8);

Isnt' that going to record the bitness of perf when it is compiled?

Quoted 2 lines

Right, it will. Its just a default, I assume this will be reset after the DSO is loaded, i.e. the ELF file gets accessed, no?

Yes

Quoted 2 lines

Which then would make this init to be just a distraction, no?

Yes

Quoted 25 lines

I wonder if we couldn't extern PERF_RECORD_MMAP to have the binary type encoded there somehow...

Quoted 19 lines

tools/perf/util/dso.h struct dso { · line 94

Quoted 12 lines
tools/perf/util/dso.c +0 -0
 		dso->loaded = 0;
 		dso->rel = 0;
 		dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
 	u8		 annotate_warned:1;
 	u8		 sname_alloc:1;
 	u8		 lname_alloc:1;
+	u8		 is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

David

reviewer
#59 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

In reply to David Ahern
Quoted 13 lines · David Ahern

tools/perf/util/dso.c struct dso *dso__new(const char *name) · line 449

Quoted 9 lines · Alexander Shishkin
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
          dso->cache = RB_ROOT;
          dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
          dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+        dso->is_64_bit = (sizeof(void *) == 8);

Isnt' that going to record the bitness of perf when it is compiled?

Quoted 16 lines

tools/perf/util/dso.h struct dso { · line 94

Quoted 12 lines
tools/perf/util/dso.c +0 -0
          dso->loaded = 0;
          dso->rel = 0;
          dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
      u8         annotate_warned:1;
      u8         sname_alloc:1;
      u8         lname_alloc:1;
+    u8         is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

I am not sure what you mean by "machine type". For itrace the implementation only deals with its own architecture (e.g. the intel_pt pmu is only on Intel architecture) so it is not necessary to record the architecture.

is_64_bit corresponds to ELFCLASS64 (vs ELFCLASS32) which is needed to determine whether the instruction set is 64-bit. That should work for other architectures too.

#60 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Quoted 14 lines · Adrian Hunter
Quoted 5 lines
Quoted 5 lines
tools/perf/util/dso.h +0 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
Quoted 8 lines

tools/perf/util/dso.h struct dso { · line 94

Quoted 4 lines
tools/perf/util/dso.h +1 -0
       u8         annotate_warned:1;
       u8         sname_alloc:1;
       u8         lname_alloc:1;
+    u8         is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

Quoted 9 lines

I am not sure what you mean by "machine type". For itrace the implementation only deals with its own architecture (e.g. the intel_pt pmu is only on Intel architecture) so it is not necessary to record the architecture.

is_64_bit corresponds to ELFCLASS64 (vs ELFCLASS32) which is needed to determine whether the instruction set is 64-bit. That should work for other architectures too.

perf-trace needs something similar -- an audit machine type to know how to convert syscall numbers to functions. One of the following per task:

typedef enum {
    MACH_X86=0,
    MACH_86_64,
    MACH_IA64,
    MACH_PPC64,
    MACH_PPC,
    MACH_S390X,
    MACH_S390,
    MACH_ALPHA,
    MACH_ARMEB
} machine_t;

I was pondering how the 2 can be combined into a common flag.

David

#61 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Em Thu, Dec 12, 2013 at 09:45:26AM -0700, David Ahern escreveu:

Quoted 42 lines · Adrian Hunter
Quoted 15 lines
Quoted 14 lines

tools/perf/util/dso.h struct dso { · line 94

Quoted 10 lines
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
      u8         annotate_warned:1;
      u8         sname_alloc:1;
      u8         lname_alloc:1;
+    u8         is_64_bit:1;

The is_64_bit name seems a bit hardcoded. We need something similar for perf-trace to set the audit machine type for resolving syscalls. How about having this field set a machine type rather than a "64-bit" flag?

Quoted 9 lines

I am not sure what you mean by "machine type". For itrace the implementation only deals with its own architecture (e.g. the intel_pt pmu is only on Intel architecture) so it is not necessary to record the architecture.

is_64_bit corresponds to ELFCLASS64 (vs ELFCLASS32) which is needed to determine whether the instruction set is 64-bit. That should work for other architectures too.

perf-trace needs something similar -- an audit machine type to know how to convert syscall numbers to functions. One of the following per task:

typedef enum {
    MACH_X86=0,
    MACH_86_64,
    MACH_IA64,
    MACH_PPC64,
    MACH_PPC,
    MACH_S390X,
    MACH_S390,
    MACH_ALPHA,
    MACH_ARMEB
} machine_t;
Quoted 1 line

I was pondering how the 2 can be combined into a common flag.

Well, if we can pass somehow the magic number of an executable mmap in the PERF_RECORD_MMAP2 record, we would be able, together with the data we already have in the perf.data header (uname in a live session), to figure that out, no?

I.e. we wouldn't be limiting ourselves to the ELF executable format.

Time to read the kernel loader for the various formats we support...

- Arnaldo

#62 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Quoted 4 lines · Arnaldo Carvalho de Melo

Well, if we can pass somehow the magic number of an executable mmap in the PERF_RECORD_MMAP2 record, we would be able, together with the data we already have in the perf.data header (uname in a live session), to figure that out, no?

Sure, but any kernel-side only solution will be extremely limited in user base for years.

David

#63 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Em Thu, Dec 12, 2013 at 12:16:16PM -0700, David Ahern escreveu:

Quoted 9 lines · Arnaldo Carvalho de Melo
Quoted 5 lines

Well, if we can pass somehow the magic number of an executable mmap in the PERF_RECORD_MMAP2 record, we would be able, together with the data we already have in the perf.data header (uname in a live session), to figure that out, no?

Sure, but any kernel-side only solution will be extremely limited in user base for years.

You mean it will take time for the kernel with this feature to become widespread?

Sure, but how do you propose to properly implement this using existing facilities?

I can't think of any way that doesn't requires having access to the file referenced via PERF_RECORD_{MMAP,MMAP2}, in userspace, and that is racy.

For older kernels, that doesn't support this, we can do as I think you envision, but that doesn't precludes trying to put in place a more robust solution.

- Arnaldo

#64 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

In reply to Alexander Shishkin
Quoted 80 lines · Alexander Shishkin
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
  		dso->cache = RB_ROOT;
  		dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
  		dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+		dso->is_64_bit = (sizeof(void *) == 8);
  		dso->loaded = 0;
  		dso->rel = 0;
  		dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
  	u8		 annotate_warned:1;
  	u8		 sname_alloc:1;
  	u8		 lname_alloc:1;
+	u8		 is_64_bit:1;
  	u8		 sorted_by_name;
  	u8		 loaded;
  	u8		 rel;
tools/perf/util/symbol-elf.c +3 -0
index eed0b96..a0fc81b 100644
@@ -595,6 +595,8 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
  			goto out_elf_end;
  	}
 
+	ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
+
  	ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
  			NULL);
  	if (ss->symshdr.sh_type != SHT_SYMTAB)
@@ -694,6 +696,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
  	bool remap_kernel = false, adjust_kernel_syms = false;
 
  	dso->symtab_type = syms_ss->type;
+	dso->is_64_bit = syms_ss->is_64_bit;
  	dso->rel = syms_ss->ehdr.e_type == ET_REL;
 
  	/*
tools/perf/util/symbol-minimal.c +18 -0
index ac7070a..b9d1119 100644
@@ -1,3 +1,4 @@
+#include "util.h"
  #include "symbol.h"
 
  #include <stdio.h>
@@ -287,6 +288,23 @@ int dso__synthesize_plt_symbols(struct dso *dso __maybe_unused,
  	return 0;
  }
 
+static int fd__is_64_bit(int fd)
+{
+	u8 e_ident[EI_NIDENT];
+
+	if (lseek(fd, 0, SEEK_SET))
+		return -1;
+
+	if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
+		return -1;
+
+	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+	    e_ident[EI_VERSION] != EV_CURRENT)
+		return -1;
+
+	return e_ident[EI_CLASS] == ELFCLASS64;
+}
+
  int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
  		  struct symsrc *ss,
  		  struct symsrc *runtime_ss __maybe_unused,
@@ -294,6 +312,11 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
  		  int kmodule __maybe_unused)

tools/perf/util/symbol-minimal.c int dso__load_sym(struct dso *dso, struct map *map __maybe_unused, · line 322

Quoted 10 lines
tools/perf/util/symbol-minimal.c +5 -0
  {
  	unsigned char *build_id[BUILD_ID_SIZE];
+	int ret;
+
+	ret = fd__is_64_bit(ss->fd);
+	if (ret >= 0)
+		dso->is_64_bit = ret;
 
  	if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
  		dso__set_build_id(dso, build_id);

Here's what is wrong with this API: you are determining DSO bitness at symbol load time, not when the DSO is created and added to the maps.

As I pointed out in a prior comment you are initializing dso->is_64_bit to perf's bitness when the dso object is created (dso__new) but the value is not correctly set until dso__load time. Some tools (perf-trace) never load symbols, so that value is always wrong in the sense that its value has no correlation to the dso object.

David

reviewer
#65 lore raw

Re: [PATCH v0 07/71] perf tools: Record whether a dso is 64-bit

Quoted 100 lines · David Ahern
Quoted 93 lines · Alexander Shishkin
tools/perf/util/dso.c +1 -0
index a0c7c59..80817ec 100644
@@ -446,6 +446,7 @@ struct dso *dso__new(const char *name)
          dso->cache = RB_ROOT;
          dso->symtab_type = DSO_BINARY_TYPE__NOT_FOUND;
          dso->data_type   = DSO_BINARY_TYPE__NOT_FOUND;
+        dso->is_64_bit = (sizeof(void *) == 8);
          dso->loaded = 0;
          dso->rel = 0;
          dso->sorted_by_name = 0;
tools/perf/util/dso.h +1 -0
index 384f2d9..62680e1 100644
@@ -91,6 +91,7 @@ struct dso {
      u8         annotate_warned:1;
      u8         sname_alloc:1;
      u8         lname_alloc:1;
+    u8         is_64_bit:1;
      u8         sorted_by_name;
      u8         loaded;
      u8         rel;
tools/perf/util/symbol-elf.c +2 -0
index eed0b96..a0fc81b 100644
@@ -595,6 +595,8 @@ int symsrc__init(struct symsrc *ss, struct dso *dso,
 const char *name,
              goto out_elf_end;
      }
 
+    ss->is_64_bit = (gelf_getclass(elf) == ELFCLASS64);
+
      ss->symtab = elf_section_by_name(elf, &ehdr, &ss->symshdr, ".symtab",
              NULL);
      if (ss->symshdr.sh_type != SHT_SYMTAB)
tools/perf/util/symbol-elf.c +1 -0
@@ -694,6 +696,7 @@ int dso__load_sym(struct dso *dso, struct map *map,
      bool remap_kernel = false, adjust_kernel_syms = false;
 
      dso->symtab_type = syms_ss->type;
+    dso->is_64_bit = syms_ss->is_64_bit;
      dso->rel = syms_ss->ehdr.e_type == ET_REL;
 
      /*
tools/perf/util/symbol-minimal.c +18 -0
index ac7070a..b9d1119 100644
@@ -1,3 +1,4 @@
+#include "util.h"
  #include "symbol.h"
 
  #include <stdio.h>
@@ -287,6 +288,23 @@ int dso__synthesize_plt_symbols(struct dso *dso
 __maybe_unused,
      return 0;
  }
 
+static int fd__is_64_bit(int fd)
+{
+    u8 e_ident[EI_NIDENT];
+
+    if (lseek(fd, 0, SEEK_SET))
+        return -1;
+
+    if (readn(fd, e_ident, sizeof(e_ident)) != sizeof(e_ident))
+        return -1;
+
+    if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+        e_ident[EI_VERSION] != EV_CURRENT)
+        return -1;
+
+    return e_ident[EI_CLASS] == ELFCLASS64;
+}
+
  int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
            struct symsrc *ss,

struct symsrc *runtime_ss __maybe_unused,

tools/perf/util/symbol-minimal.c +5 -0
@@ -294,6 +312,11 @@ int dso__load_sym(struct dso *dso, struct map *map
 __maybe_unused,
            int kmodule __maybe_unused)
  {
      unsigned char *build_id[BUILD_ID_SIZE];
+    int ret;
+
+    ret = fd__is_64_bit(ss->fd);
+    if (ret >= 0)
+        dso->is_64_bit = ret;
 
      if (filename__read_build_id(ss->name, build_id, BUILD_ID_SIZE) > 0) {
Quoted 1 line
          dso__set_build_id(dso, build_id);

Here's what is wrong with this API: you are determining DSO bitness at symbol load time, not when the DSO is created and added to the maps.

Quoted 5 lines

As I pointed out in a prior comment you are initializing dso->is_64_bit to perf's bitness when the dso object is created (dso__new) but the value is not correctly set until dso__load time. Some tools (perf-trace) never load symbols, so that value is always wrong in the sense that its value has no correlation to the dso object.

That is a very good point. I think Arnaldo has previously noted that symbol loading needs to be split from dso (map) loading. I don't have the time to do that right now.

author reviewer
#66 lore raw

PATCH 8/71 perf tools: Let a user specify a PMU event without any config terms

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

This enables a PMU event to be specified in the form:

	pmu//

which is effectively the same as:

	pmu/config=0/

This patch is a precursor to defining
default config for a PMU.

---
 tools/perf/util/parse-events.c |  6 ++++++
 tools/perf/util/parse-events.y | 10 ++++++++++
 2 files changed, 16 insertions(+)
tools/perf/util/parse-events.c +6 -0
index 969cb8f..98547e0 100644
@@ -644,6 +644,12 @@ int parse_events_add_pmu(struct list_head *list, int *idx,
 
 	memset(&attr, 0, sizeof(attr));
 
+	if (!head_config) {
+		attr.type = pmu->type;
+		evsel = __add_event(list, idx, &attr, NULL, pmu->cpus);
+		return evsel ? 0 : -ENOMEM;
+	}
+
 	if (perf_pmu__check_alias(pmu, head_config, &unit, &scale))
 		return -EINVAL;
 
tools/perf/util/parse-events.y +10 -0
index 4eb67ec..8fad267 100644
@@ -210,6 +210,16 @@ PE_NAME '/' event_config '/'
 	parse_events__free_terms($3);
 	$$ = list;
 }
+|
+PE_NAME '/' '/'
+{
+	struct parse_events_evlist *data = _data;
+	struct list_head *list;
+
+	ALLOC_LIST(list);
+	ABORT_ON(parse_events_add_pmu(list, &data->idx, $1, NULL));
+	$$ = list;
+}
 
 value_sym:
 PE_VALUE_SYM_HW
Signature · 1 line
1.8.5.1
author reviewer
#67 lore raw

PATCH 9/71 perf tools: Let default config be defined for a PMU

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

This allows default config terms to be provided
for a PMU. So, for example, when the Intel PT
PMU is added, it will be possible to specify:

	intel_pt//

which will be the same as:

	intel_pt/tsc=1,noretcomp=1/

meaning that the trace should contain
TSC timestamps but not perform 'return
compression'.

An important consideration of this
patch is that it must be possible
to overwrite the default values.
That has meant changing the logic
so that a zero value can replace
a non-zero value.

---
 tools/perf/tests/pmu.c         |  2 +-
 tools/perf/util/parse-events.c |  7 ++++++-
 tools/perf/util/pmu.c          | 42 ++++++++++++++++++++++++++----------------
 tools/perf/util/pmu.h          |  9 ++++++++-
 4 files changed, 41 insertions(+), 19 deletions(-)
tools/perf/tests/pmu.c +1 -1
index 12b322f..eeb68bb1 100644
@@ -152,7 +152,7 @@ int test__pmu(void)
 		if (ret)
 			break;
 
-		ret = perf_pmu__config_terms(&formats, &attr, terms);
+		ret = perf_pmu__config_terms(&formats, &attr, terms, false);
 		if (ret)
 			break;
 
tools/perf/util/parse-events.c +6 -1
index 98547e0..464dafd 100644
@@ -642,7 +642,12 @@ int parse_events_add_pmu(struct list_head *list, int *idx,
 	if (!pmu)
 		return -EINVAL;
 
-	memset(&attr, 0, sizeof(attr));
+	if (pmu->default_config) {
+		memcpy(&attr, pmu->default_config,
+		       sizeof(struct perf_event_attr));
+	} else {
+		memset(&attr, 0, sizeof(attr));
+	}
 
 	if (!head_config) {
 		attr.type = pmu->type;
tools/perf/util/pmu.c +26 -16

105 lines not on this page · raw message

tools/perf/util/pmu.h +8 -1
index 9183380..df762fd 100644
@@ -13,9 +13,12 @@ enum {
 
 #define PERF_PMU_FORMAT_BITS 64
 
+struct perf_event_attr;
+
 struct perf_pmu {
 	char *name;
 	__u32 type;
+	struct perf_event_attr *default_config;
 	struct cpu_map *cpus;
 	struct list_head format;
 	struct list_head aliases;
@@ -27,7 +30,8 @@ int perf_pmu__config(struct perf_pmu *pmu, struct perf_event_attr *attr,
 		     struct list_head *head_terms);
 int perf_pmu__config_terms(struct list_head *formats,
 			   struct perf_event_attr *attr,
-			   struct list_head *head_terms);
+			   struct list_head *head_terms,
+			   bool zero);
 int perf_pmu__check_alias(struct perf_pmu *pmu, struct list_head *head_terms,
 			  char **unit, double *scale);
 struct list_head *perf_pmu__alias(struct perf_pmu *pmu,
@@ -46,4 +50,7 @@ void print_pmu_events(const char *event_glob, bool name_only);
 bool pmu_have_event(const char *pname, const char *name);
 
 int perf_pmu__test(void);
+
+struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu);
+
 #endif /* __PMU_H */
Signature · 1 line
1.8.5.1
author reviewer
#68 lore raw

PATCH 10/71 perf tools: Add perf_pmu__scan_file()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to scan a sysfs file within the pmu device directory.

This will be used to read capability values from the PMU 'caps' subdirectory.

---
 tools/perf/util/pmu.c | 37 +++++++++++++++++++++++++++++++++++++
 tools/perf/util/pmu.h |  3 +++
 2 files changed, 40 insertions(+)
tools/perf/util/pmu.c +37 -0
index a53b8ac..a742eeb 100644
@@ -3,6 +3,7 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdbool.h>
+#include <stdarg.h>
 #include <dirent.h>
 #include "fs.h"
 #include <locale.h>
@@ -788,3 +789,39 @@ bool pmu_have_event(const char *pname, const char *name)
 	}
 	return false;
 }
+
+static FILE *perf_pmu__open_file(struct perf_pmu *pmu, const char *name)
+{
+	struct stat st;
+	char path[PATH_MAX];
+	const char *sysfs;
+
+	sysfs = sysfs__mountpoint();
+	if (!sysfs)
+		return NULL;
+
+	snprintf(path, PATH_MAX,
+		 "%s" EVENT_SOURCE_DEVICE_PATH "%s/%s", sysfs, pmu->name, name);
+
+	if (stat(path, &st) < 0)
+		return NULL;
+
+	return fopen(path, "r");
+}
+
+int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name, const char *fmt,
+			...)
+{
+	va_list args;
+	FILE *file;
+	int ret = EOF;
+
+	va_start(args, fmt);
+	file = perf_pmu__open_file(pmu, name);
+	if (file) {
+		ret = vfscanf(file, fmt, args);
+		fclose(file);
+	}
+	va_end(args);
+	return ret;
+}
tools/perf/util/pmu.h +3 -0
index df762fd..437fdb2 100644
@@ -49,6 +49,9 @@ struct perf_pmu *perf_pmu__scan(struct perf_pmu *pmu);
 void print_pmu_events(const char *event_glob, bool name_only);
 bool pmu_have_event(const char *pname, const char *name);
 
+int perf_pmu__scan_file(struct perf_pmu *pmu, const char *name,
+		const char *fmt, ...) __attribute__((format(scanf, 3, 4)));
+
 int perf_pmu__test(void);
 
 struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu);
Signature · 1 line
1.8.5.1
author reviewer
#69 lore raw

PATCH 11/71 perf tools: Add perf_event_paranoid()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to return the value of /proc/sys/kernel/perf_event_paranoid.

This will be used to determine default values for mmap size because perf is not subject to mmap limits when perf_event_paranoid is less than zero.

---
 tools/perf/util/evlist.c |  3 +--
 tools/perf/util/util.c   | 19 +++++++++++++++++++
 tools/perf/util/util.h   |  3 +++
 3 files changed, 23 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +1 -2
index 7bb6ee1..50fadde 100644
@@ -1189,8 +1189,7 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 				    "Error:\t%s.\n"
 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
 
-		if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value))
-			break;
+		value = perf_event_paranoid();
 
 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
 
tools/perf/util/util.c +19 -0
index bae8756..3aed4af 100644
@@ -1,5 +1,6 @@
 #include "../perf.h"
 #include "util.h"
+#include "fs.h"
 #include <sys/mman.h>
 #ifdef HAVE_BACKTRACE_SUPPORT
 #include <execinfo.h>
@@ -8,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include <linux/kernel.h>
 
 /*
@@ -482,3 +484,20 @@ int filename__read_str(const char *filename, char **buf, size_t *sizep)
 	close(fd);
 	return err;
 }
+
+int perf_event_paranoid(void)
+{
+	char path[PATH_MAX];
+	const char *procfs = procfs__mountpoint();
+	int value;
+
+	if (!procfs)
+		return INT_MAX;
+
+	scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
+
+	if (filename__read_int(path, &value))
+		return INT_MAX;
+
+	return value;
+}
tools/perf/util/util.h +3 -0
index adb39f2..4b6b260 100644
@@ -309,4 +309,7 @@ void free_srcline(char *srcline);
 
 int filename__read_int(const char *filename, int *value);
 int filename__read_str(const char *filename, char **buf, size_t *sizep);
+
+int perf_event_paranoid(void);
+
 #endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.8.5.1
#70 lore raw

[tip:perf/core] perf tools: Add perf_event_paranoid()

perf tools: Add perf_event_paranoid()
Commit-ID:  1a47245d2f3bf6276c95cd37901b562962d6ae47
Gitweb:     http://git.kernel.org/tip/1a47245d2f3bf6276c95cd37901b562962d6ae47
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 11 Dec 2013 14:36:23 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 13 Dec 2013 10:30:20 -0300

perf tools: Add perf_event_paranoid()

Add a function to return the value of
/proc/sys/kernel/perf_event_paranoid.

This will be used to determine default values for mmap size because perf is not subject to mmap limits when perf_event_paranoid is less than zero.

---
 tools/perf/util/evlist.c |  3 +--
 tools/perf/util/util.c   | 19 +++++++++++++++++++
 tools/perf/util/util.h   |  1 +
 3 files changed, 21 insertions(+), 2 deletions(-)
tools/perf/util/evlist.c +1 -2
index af25055..2eb7378 100644
@@ -1191,8 +1191,7 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 				    "Error:\t%s.\n"
 				    "Hint:\tCheck /proc/sys/kernel/perf_event_paranoid setting.", emsg);
 
-		if (filename__read_int("/proc/sys/kernel/perf_event_paranoid", &value))
-			break;
+		value = perf_event_paranoid();
 
 		printed += scnprintf(buf + printed, size - printed, "\nHint:\t");
 
tools/perf/util/util.c +19 -0
index 4a57609..8f63dba 100644
@@ -1,5 +1,6 @@
 #include "../perf.h"
 #include "util.h"
+#include "fs.h"
 #include <sys/mman.h>
 #ifdef HAVE_BACKTRACE_SUPPORT
 #include <execinfo.h>
@@ -8,6 +9,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <errno.h>
+#include <limits.h>
 #include <linux/kernel.h>
 
 /*
@@ -496,3 +498,20 @@ const char *get_filename_for_perf_kvm(void)
 
 	return filename;
 }
+
+int perf_event_paranoid(void)
+{
+	char path[PATH_MAX];
+	const char *procfs = procfs__mountpoint();
+	int value;
+
+	if (!procfs)
+		return INT_MAX;
+
+	scnprintf(path, PATH_MAX, "%s/sys/kernel/perf_event_paranoid", procfs);
+
+	if (filename__read_int(path, &value))
+		return INT_MAX;
+
+	return value;
+}
tools/perf/util/util.h +1 -0
index 0171213..1e7d413 100644
@@ -321,6 +321,7 @@ void free_srcline(char *srcline);
 
 int filename__read_int(const char *filename, int *value);
 int filename__read_str(const char *filename, char **buf, size_t *sizep);
+int perf_event_paranoid(void);
 
 const char *get_filename_for_perf_kvm(void);
 #endif /* GIT_COMPAT_UTIL_H */
author reviewer
#71 lore raw

PATCH 12/71 perf tools: Add dsos__hit_all()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add ability to mark all dsos as hit.

This is needed in the case of Instruction Tracing. It takes so long to decode an Instruction Trace that it is not worth doing just to determine which dsos are hit. A later patch takes this into use.

---
 tools/perf/util/header.c | 41 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/header.h |  2 ++
 2 files changed, 43 insertions(+)
tools/perf/util/header.c +41 -0
index 125cdc9..49c4896 100644
@@ -200,6 +200,47 @@ static int write_buildid(char *name, size_t name_len, u8 *build_id,
 	return write_padded(fd, name, name_len + 1, len);
 }
 
+static int __dsos__hit_all(struct list_head *head)
+{
+	struct dso *pos;
+
+	list_for_each_entry(pos, head, node)
+		pos->hit = true;
+
+	return 0;
+}
+
+static int machine__hit_all_dsos(struct machine *machine)
+{
+	int err;
+
+	err = __dsos__hit_all(&machine->kernel_dsos);
+	if (err)
+		return err;
+
+	return __dsos__hit_all(&machine->user_dsos);
+}
+
+int dsos__hit_all(struct perf_session *session)
+{
+	struct rb_node *nd;
+	int err;
+
+	err = machine__hit_all_dsos(&session->machines.host);
+	if (err)
+		return err;
+
+	for (nd = rb_first(&session->machines.guests); nd; nd = rb_next(nd)) {
+		struct machine *pos = rb_entry(nd, struct machine, rb_node);
+
+		err = machine__hit_all_dsos(pos);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
 static int __dsos__write_buildid_table(struct list_head *head,
 				       struct machine *machine,
 				       pid_t pid, u16 misc, int fd)
tools/perf/util/header.h +2 -0
index 307c9ae..e8c45fa 100644
@@ -151,6 +151,8 @@ int perf_event__process_build_id(struct perf_tool *tool,
 				 struct perf_session *session);
 bool is_perf_magic(u64 magic);
 
+int dsos__hit_all(struct perf_session *session);
+
 /*
  * arch specific callback
  */
Signature · 1 line
1.8.5.1
author reviewer
#72 lore raw

PATCH 13/71 perf tools: Add machine__get_thread_pid()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to get the pid from the tid.

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch identifies the thread but, to find the process mmaps, we need the process pid.

---
 tools/perf/util/machine.c | 10 ++++++++++
 tools/perf/util/machine.h |  2 ++
 2 files changed, 12 insertions(+)
tools/perf/util/machine.c +10 -0
index bac817a..55f3608 100644
@@ -1402,3 +1402,13 @@ int __machine__synthesize_threads(struct machine *machine, struct perf_tool *too
 	/* command specified */
 	return 0;
 }
+
+pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
+{
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	return thread->pid_;
+}
tools/perf/util/machine.h +2 -0
index 4771330..b800a5a 100644
@@ -190,4 +190,6 @@ int machine__synthesize_threads(struct machine *machine, struct target *target,
 					     perf_event__process, data_mmap);
 }
 
+pid_t machine__get_thread_pid(struct machine *machine, pid_t tid);
+
 #endif /* __PERF_MACHINE_H */
Signature · 1 line
1.8.5.1
#73 lore raw

Re: [PATCH v0 13/71] perf tools: Add machine__get_thread_pid()

Quoted 9 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to get the pid from the tid.

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch identifies the thread but, to find the process mmaps, we need the process pid.

Are you looking up the current or next task? If the former why not use sample->pid rather than parsing the sched_switch tracepoint?

David

#74 lore raw

Re: [PATCH v0 13/71] perf tools: Add machine__get_thread_pid()

Quoted 3 lines · David Ahern

Are you looking up the current or next task? If the former why not use sample->pid rather than parsing the sched_switch tracepoint?

The itrace stream doesn't have a pid field, and it needs the exact time stamp of the switch. There may not actually be any samples before decoding.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#75 lore raw

Re: [PATCH v0 13/71] perf tools: Add machine__get_thread_pid()

Quoted 11 lines · Andi Kleen
Quoted 3 lines · David Ahern

Are you looking up the current or next task? If the former why not use sample->pid rather than parsing the sched_switch tracepoint?

The itrace stream doesn't have a pid field, and it needs the exact time stamp of the switch. There may not actually be any samples before decoding.

-Andi

What I meant is this:

perf record -e sched:sched_switch -a -- sleep 1 | perf script -f comm,tid,pid,event,trace

qemu-system-x86 8688/8692 sched:sched_switch: prev_comm=qemu-system-x86 prev_pid=8692 prev_prio=120 prev_state=S ==> next_comm=swapper/15 next_pid=0 next_prio=120

8688/8692 are the pid and tid of the running task. If you are monitoring sched_switch events and looking at running task -- the one getting scheduled out -- you don't need to parse the tracepoint. But, if you want to know next task then you do need to parse it. I was wondering which task is getting looked up.

David

reviewer
#76 lore raw

Re: [PATCH v0 13/71] perf tools: Add machine__get_thread_pid()

In reply to David Ahern
Quoted 5 lines · David Ahern
Quoted 4 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to get the pid from the tid.

Quoted 8 lines
Quoted 5 lines

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch identifies the thread but, to find the process mmaps, we need the process pid.

Are you looking up the current or next task? If the former why not use sample->pid rather than parsing the sched_switch tracepoint?

Next pid unfortunately.

author reviewer
#77 lore raw

PATCH 14/71 perf tools: Add cpu to struct thread

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Tools may wish to track on which cpu a thread is running. Add 'cpu' to struct thread for that purpose. Also add machine functions to get / set the cpu for a tid.

This will be used to determine the cpu when decoding a per-thread Instruction Trace.

---
 tools/perf/util/machine.c | 26 ++++++++++++++++++++++++++
 tools/perf/util/machine.h |  3 +++
 tools/perf/util/thread.c  |  1 +
 tools/perf/util/thread.h  |  1 +
 4 files changed, 31 insertions(+)
tools/perf/util/machine.c +26 -0
index 55f3608..52fbfb6 100644
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
 	return thread->pid_;
 }
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;
+
+	return thread->cpu;
+}
+
+int machine__set_thread_cpu(struct machine *machine, pid_t pid, pid_t tid,
+			    int cpu)
+{
+	struct thread *thread = machine__findnew_thread(machine, pid, tid);
+
+	if (!thread)
+		return -ENOMEM;
+
+	thread->cpu = cpu;
+
+	return 0;
+}
tools/perf/util/machine.h +3 -0
index b800a5a..27486af 100644
@@ -191,5 +191,8 @@ int machine__synthesize_threads(struct machine *machine, struct target *target,
 }
 
 pid_t machine__get_thread_pid(struct machine *machine, pid_t tid);
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid);
+int machine__set_thread_cpu(struct machine *machine, pid_t pid, pid_t tid,
+			    int cpu);
 
 #endif /* __PERF_MACHINE_H */
tools/perf/util/thread.c +1 -0
index 49eaf1d..a120af3 100644
@@ -19,6 +19,7 @@ struct thread *thread__new(pid_t pid, pid_t tid)
 		thread->pid_ = pid;
 		thread->tid = tid;
 		thread->ppid = -1;
+		thread->cpu = -1;
 		INIT_LIST_HEAD(&thread->comm_list);
 
 		comm_str = malloc(32);
tools/perf/util/thread.h +1 -0
index 5b856bf..7914050 100644
@@ -17,6 +17,7 @@ struct thread {
 	pid_t			pid_; /* Not all tools update this */
 	pid_t			tid;
 	pid_t			ppid;
+	int			cpu;
 	char			shortname[3];
 	bool			comm_set;
 	bool			dead; /* if set thread has exited */
Signature · 1 line
1.8.5.1
#78 lore raw

Re: [PATCH v0 14/71] perf tools: Add cpu to struct thread

Em Wed, Dec 11, 2013 at 02:36:26PM +0200, Alexander Shishkin escreveu:

Quoted 19 lines

From: Adrian Hunter <[email protected]>

Tools may wish to track on which cpu a thread is running. Add 'cpu' to struct thread for that purpose. Also add machine functions to get / set the cpu for a tid.

This will be used to determine the cpu when decoding a per-thread Instruction Trace.

+++ b/tools/perf/util/machine.c

tools/perf/util/machine.c +3 -0
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
 	return thread->pid_;
 }
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{

tools/perf/util/machine.c pid_t machine__get_thread_pid(struct machine *machine, pid_t tid) · line 1427

Quoted 10 lines
tools/perf/util/machine.c +10 -0
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;
+
+	return thread->cpu;
+}

What is the problem with:

	struct thread *thread = machine__find_thread(machine, tid);
	pid_t pid = thread->pid_;
	int cpu = thread->cpu;

In your case you'll have:

	int pid;
	int cpu = machine__get_thread_cpu(machine, tid, &pid);

Which is slightly more compact, but then we end up with a function that from its name should just get a 'cpu' but also asks for the pid.

I think it is better to just use what we have (machine__find_thread), have a 'thread' variable and then use any of its members, directly.

- ARnaldo

reviewer
#79 lore raw

Re: [PATCH v0 14/71] perf tools: Add cpu to struct thread

Quoted 39 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 11, 2013 at 02:36:26PM +0200, Alexander Shishkin escreveu:

Quoted 19 lines

From: Adrian Hunter <[email protected]>

Tools may wish to track on which cpu a thread is running. Add 'cpu' to struct thread for that purpose. Also add machine functions to get / set the cpu for a tid.

This will be used to determine the cpu when decoding a per-thread Instruction Trace.

+++ b/tools/perf/util/machine.c

tools/perf/util/machine.c +3 -0
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
 	return thread->pid_;
 }
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{

tools/perf/util/machine.c pid_t machine__get_thread_pid(struct machine *machine, pid_t tid) · line 1427

Quoted 10 lines
tools/perf/util/machine.c +10 -0
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;
+
+	return thread->cpu;
+}

What is the problem with:

	struct thread *thread = machine__find_thread(machine, tid);
	pid_t pid = thread->pid_;
	int cpu = thread->cpu;

In your case you'll have:

Quoted 8 lines
	int pid;
	int cpu = machine__get_thread_cpu(machine, tid, &pid);

Which is slightly more compact, but then we end up with a function that from its name should just get a 'cpu' but also asks for the pid.

I think it is better to just use what we have (machine__find_thread), have a 'thread' variable and then use any of its members, directly.

OK

#80 lore raw

Re: [PATCH v0 14/71] perf tools: Add cpu to struct thread

In reply to Alexander Shishkin
Quoted 8 lines · Alexander Shishkin
tools/perf/util/machine.c +0 -0
index 55f3608..52fbfb6 100644
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
  	return thread->pid_;
  }

tools/perf/util/machine.c pid_t machine__get_thread_pid(struct machine *machine, pid_t tid) · line 1424

Quoted 10 lines
tools/perf/util/machine.c +10 -0
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;

Why is a 'get' function modifying the thread data?

David

#81 lore raw

Re: [PATCH v0 14/71] perf tools: Add cpu to struct thread

Em Wed, Dec 11, 2013 at 12:30:40PM -0700, David Ahern escreveu:

Quoted 8 lines · Alexander Shishkin
Quoted 7 lines
tools/perf/util/machine.c +0 -0
index 55f3608..52fbfb6 100644
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
Quoted 14 lines

tools/perf/util/machine.c pid_t machine__get_thread_pid(struct machine *machine, pid_t tid) · line 1424

Quoted 12 lines
tools/perf/util/machine.c +10 -0
 	return thread->pid_;
 }
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;

Why is a 'get' function modifying the thread data?

Where is this happening? :-)

My main complaint here was that we would be getting more things than what the function name implies, see another message with my reply to this patch.

- Arnaldo

#82 lore raw

Re: [PATCH v0 14/71] perf tools: Add cpu to struct thread

Quoted 24 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 11, 2013 at 12:30:40PM -0700, David Ahern escreveu:

Quoted 8 lines · Alexander Shishkin
Quoted 7 lines
tools/perf/util/machine.c +0 -0
index 55f3608..52fbfb6 100644
@@ -1412,3 +1412,29 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid)
 
Quoted 14 lines

tools/perf/util/machine.c pid_t machine__get_thread_pid(struct machine *machine, pid_t tid) · line 1424

Quoted 12 lines
tools/perf/util/machine.c +10 -0
  	return thread->pid_;
  }
+
+int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid)
+{
+	struct thread *thread = machine__find_thread(machine, tid);
+
+	if (!thread)
+		return -1;
+
+	if (pid)
+		*pid = thread->pid_;

Why is a 'get' function modifying the thread data?

Quoted 1 line

Where is this happening? :-)

D'oh. code review while on a call ... multitasking failed.

David

author reviewer
#83 lore raw

PATCH 15/71 perf tools: Add ability to record the current tid for each cpu

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an array to struct machine to store the current tid running on each cpu. Add machine functions to get / set the tid for a cpu.

This will be used to determine the tid when decoding a per-cpu Instruction Trace.

---
 tools/perf/util/machine.c | 39 +++++++++++++++++++++++++++++++++++++++
 tools/perf/util/machine.h |  4 ++++
 2 files changed, 43 insertions(+)
tools/perf/util/machine.c +39 -0
index 52fbfb6..a04210d 100644
@@ -43,6 +43,8 @@ int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
 		thread__set_comm(thread, comm, 0);
 	}
 
+	machine->current_tid = NULL;
+
 	return 0;
 }
 
@@ -103,6 +105,8 @@ void machine__exit(struct machine *machine)
 	dsos__delete(&machine->kernel_dsos);
 	free(machine->root_dir);
 	machine->root_dir = NULL;
+	free(machine->current_tid);
+	machine->current_tid = NULL;
 }
 
 void machine__delete(struct machine *machine)
@@ -1438,3 +1442,38 @@ int machine__set_thread_cpu(struct machine *machine, pid_t pid, pid_t tid,
 
 	return 0;
 }
+
+pid_t machine__get_current_tid(struct machine *machine, int cpu)
+{
+	if (cpu < 0 || cpu >= MAX_NR_CPUS || !machine->current_tid)
+		return -1;
+
+	return machine->current_tid[cpu];
+}
+
+int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
+			     pid_t tid)
+{
+	if (cpu < 0)
+		return -EINVAL;
+
+	if (!machine->current_tid) {
+		int i;
+
+		machine->current_tid = calloc(MAX_NR_CPUS, sizeof(pid_t));
+		if (!machine->current_tid)
+			return -ENOMEM;
+		for (i = 0; i < MAX_NR_CPUS; i++)
+			machine->current_tid[i] = -1;
+	}
+
+	if (cpu >= MAX_NR_CPUS) {
+		pr_err("Requested CPU %d too large. ", cpu);
+		pr_err("Consider raising MAX_NR_CPUS\n");
+		return -EINVAL;
+	}
+
+	machine->current_tid[cpu] = tid;
+
+	return machine__set_thread_cpu(machine, pid, tid, cpu);
+}
tools/perf/util/machine.h +4 -0
index 27486af..aaad99a 100644
@@ -31,6 +31,7 @@ struct machine {
 	struct map_groups kmaps;
 	struct map	  *vmlinux_maps[MAP__NR_TYPES];
 	symbol_filter_t	  symbol_filter;
+	pid_t		  *current_tid;
 };
 
 static inline
@@ -194,5 +195,8 @@ pid_t machine__get_thread_pid(struct machine *machine, pid_t tid);
 int machine__get_thread_cpu(struct machine *machine, pid_t tid, pid_t *pid);
 int machine__set_thread_cpu(struct machine *machine, pid_t pid, pid_t tid,
 			    int cpu);
+pid_t machine__get_current_tid(struct machine *machine, int cpu);
+int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
+			     pid_t tid);
 
 #endif /* __PERF_MACHINE_H */
Signature · 1 line
1.8.5.1
author reviewer
#84 lore raw

PATCH 16/71 perf tools: Allow header->data_offset to be predetermined

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

It will be necessary to predetermine header->data_offset to allow space for attributes that are added later. Consequently, do not change header->data_offset if it is non-zero.

---
 tools/perf/util/header.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/header.c +2 -1
index 49c4896..0114f0a 100644
@@ -2368,7 +2368,8 @@ int perf_session__write_header(struct perf_session *session,
 		}
 	}
 
-	header->data_offset = lseek(fd, 0, SEEK_CUR);
+	if (!header->data_offset)
+		header->data_offset = lseek(fd, 0, SEEK_CUR);
 	header->feat_offset = header->data_offset + header->data_size;
 
 	if (at_exit) {
Signature · 1 line
1.8.5.1
#85 lore raw

[tip:perf/core] perf header: Allow header-> data_offset to be predetermined

perf header: Allow header->data_offset to be predetermined
Commit-ID:  d645c442e68d24e64c46845bc8bb5d5a0a70b249
Gitweb:     http://git.kernel.org/tip/d645c442e68d24e64c46845bc8bb5d5a0a70b249
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 11 Dec 2013 14:36:28 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 13 Dec 2013 10:30:20 -0300

perf header: Allow header->data_offset to be predetermined

It will be necessary to predetermine header->data_offset to allow space for attributes that are added later. Consequently, do not change header->data_offset if it is non-zero.

---
 tools/perf/util/header.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/perf/util/header.c +2 -1
index 0bb830f..61c5421 100644
@@ -2327,7 +2327,8 @@ int perf_session__write_header(struct perf_session *session,
 		}
 	}
 
-	header->data_offset = lseek(fd, 0, SEEK_CUR);
+	if (!header->data_offset)
+		header->data_offset = lseek(fd, 0, SEEK_CUR);
 	header->feat_offset = header->data_offset + header->data_size;
 
 	if (at_exit) {
author reviewer
#86 lore raw

PATCH 17/71 perf tools: Add perf_evlist__can_select_event()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to determine whether an event can be selected.

This function is needed to allow a tool to automatically select additional events, but only if they are available.

---
 tools/perf/util/evlist.h |  2 ++
 tools/perf/util/record.c | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
tools/perf/util/evlist.h +2 -0
index 649d6ea..8a04aae 100644
@@ -193,4 +193,6 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 	pc->data_tail = tail;
 }
 
+bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+
 #endif /* __PERF_EVLIST_H */
tools/perf/util/record.c +37 -0
index c8845b1..e510453 100644
@@ -177,3 +177,40 @@ int perf_record_opts__config(struct perf_record_opts *opts)
 {
 	return perf_record_opts__config_freq(opts);
 }
+
+bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
+{
+	struct perf_evlist *temp_evlist;
+	struct perf_evsel *evsel;
+	int err, fd, cpu;
+	bool ret = false;
+
+	temp_evlist = perf_evlist__new();
+	if (!temp_evlist)
+		return false;
+
+	err = parse_events(temp_evlist, str);
+	if (err)
+		goto out_delete;
+
+	evsel = perf_evlist__last(temp_evlist);
+
+	if (!evlist || cpu_map__empty(evlist->cpus)) {
+		struct cpu_map *cpus = cpu_map__new(NULL);
+
+		cpu =  cpus ? cpus->map[0] : 0;
+		cpu_map__delete(cpus);
+	} else {
+		cpu = evlist->cpus->map[0];
+	}
+
+	fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
+	if (fd >= 0) {
+		close(fd);
+		ret = true;
+	}
+
+out_delete:
+	perf_evlist__delete(temp_evlist);
+	return ret;
+}
Signature · 1 line
1.8.5.1
#87 lore raw

[tip:perf/core] perf evlist: Add can_select_event() method

perf evlist: Add can_select_event() method
Commit-ID:  c09ec622629eeb4b7877646a42852e7156363425
Gitweb:     http://git.kernel.org/tip/c09ec622629eeb4b7877646a42852e7156363425
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 11 Dec 2013 14:36:29 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 13 Dec 2013 10:30:20 -0300

perf evlist: Add can_select_event() method

Add a function to determine whether an event can be selected.

This function is needed to allow a tool to automatically select additional events, but only if they are available.

---
 tools/perf/util/evlist.h |  2 ++
 tools/perf/util/record.c | 37 +++++++++++++++++++++++++++++++++++++
 2 files changed, 39 insertions(+)
tools/perf/util/evlist.h +2 -0
index 649d6ea..8a04aae 100644
@@ -193,4 +193,6 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 	pc->data_tail = tail;
 }
 
+bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+
 #endif /* __PERF_EVLIST_H */
tools/perf/util/record.c +37 -0
index c8845b1..e510453 100644
@@ -177,3 +177,40 @@ int perf_record_opts__config(struct perf_record_opts *opts)
 {
 	return perf_record_opts__config_freq(opts);
 }
+
+bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str)
+{
+	struct perf_evlist *temp_evlist;
+	struct perf_evsel *evsel;
+	int err, fd, cpu;
+	bool ret = false;
+
+	temp_evlist = perf_evlist__new();
+	if (!temp_evlist)
+		return false;
+
+	err = parse_events(temp_evlist, str);
+	if (err)
+		goto out_delete;
+
+	evsel = perf_evlist__last(temp_evlist);
+
+	if (!evlist || cpu_map__empty(evlist->cpus)) {
+		struct cpu_map *cpus = cpu_map__new(NULL);
+
+		cpu =  cpus ? cpus->map[0] : 0;
+		cpu_map__delete(cpus);
+	} else {
+		cpu = evlist->cpus->map[0];
+	}
+
+	fd = sys_perf_event_open(&evsel->attr, -1, cpu, -1, 0);
+	if (fd >= 0) {
+		close(fd);
+		ret = true;
+	}
+
+out_delete:
+	perf_evlist__delete(temp_evlist);
+	return ret;
+}
author reviewer
#88 lore raw

PATCH 18/71 perf session: Flag if the event stream is entirely in memory

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Flag if the event stream is a file that has been mmapped in one go.

This is useful, for example, if a tool needs to keep an event for later reference. If the new flag is set, a pointer to the event can be retained, otherwise the event must be copied.

---
 tools/perf/util/session.c | 9 ++++++++-
 tools/perf/util/session.h | 3 +++
 2 files changed, 11 insertions(+), 1 deletion(-)
tools/perf/util/session.c +8 -1
index 8a7da6f..10ac07a 100644
@@ -1305,8 +1305,10 @@ int __perf_session__process_events(struct perf_session *session,
 	ui_progress__init(&prog, file_size, "Processing events...");
 
 	mmap_size = MMAP_SIZE;
-	if (mmap_size > file_size)
+	if (mmap_size > file_size) {
 		mmap_size = file_size;
+		session->one_mmap = true;
+	}
 
 	memset(mmaps, 0, sizeof(mmaps));
 
@@ -1328,6 +1330,10 @@ remap:
 	mmaps[map_idx] = buf;
 	map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
 	file_pos = file_offset + head;
+	if (session->one_mmap) {
+		session->one_mmap_addr = buf;
+		session->one_mmap_offset = file_offset;
+	}
 
 more:
 	event = fetch_mmaped_event(session, head, mmap_size, buf);
@@ -1373,6 +1379,7 @@ out_err:
 	ui_progress__finish();
 	perf_session__warn_about_errors(session, tool);
 	perf_session_free_sample_buffers(session);
+	session->one_mmap = false;
 	return err;
 }
 
tools/perf/util/session.h +3 -0
index 004d3e8..ca1d734 100644
@@ -36,6 +36,9 @@ struct perf_session {
 	struct trace_event	tevent;
 	struct events_stats	stats;
 	bool			repipe;
+	bool			one_mmap;
+	void			*one_mmap_addr;
+	u64			one_mmap_offset;
 	struct ordered_samples	ordered_samples;
 	struct perf_data_file	*file;
 };
Signature · 1 line
1.8.5.1
author reviewer
#89 lore raw

PATCH 19/71 perf evlist: Pass mmap parameters in a struct

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

In preparation for adding more mmap parameters, pass existing parameters in a struct.

---
 tools/perf/util/evlist.c | 46 ++++++++++++++++++++++++++--------------------
 1 file changed, 26 insertions(+), 20 deletions(-)
tools/perf/util/evlist.c +26 -20
index 50fadde..f9dbf5f 100644
@@ -600,12 +600,17 @@ static int perf_evlist__alloc_mmap(struct perf_evlist *evlist)
 	return evlist->mmap != NULL ? 0 : -ENOMEM;
 }
 
-static int __perf_evlist__mmap(struct perf_evlist *evlist,
-			       int idx, int prot, int mask, int fd)
+struct mmap_params {
+	int prot;
+	int mask;
+};
+
+static int __perf_evlist__mmap(struct perf_evlist *evlist, int idx,
+			       struct mmap_params *mp, int fd)
 {
 	evlist->mmap[idx].prev = 0;
-	evlist->mmap[idx].mask = mask;
-	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, prot,
+	evlist->mmap[idx].mask = mp->mask;
+	evlist->mmap[idx].base = mmap(NULL, evlist->mmap_len, mp->prot,
 				      MAP_SHARED, fd, 0);
 	if (evlist->mmap[idx].base == MAP_FAILED) {
 		pr_debug2("failed to mmap perf event ring buffer, error %d\n",
@@ -619,8 +624,8 @@ static int __perf_evlist__mmap(struct perf_evlist *evlist,
 }
 
 static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
-				       int prot, int mask, int cpu, int thread,
-				       int *output)
+				       struct mmap_params *mp, int cpu,
+				       int thread, int *output)
 {
 	struct perf_evsel *evsel;
 
@@ -629,8 +634,7 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
 
 		if (*output == -1) {
 			*output = fd;
-			if (__perf_evlist__mmap(evlist, idx, prot, mask,
-						*output) < 0)
+			if (__perf_evlist__mmap(evlist, idx, mp, *output) < 0)
 				return -1;
 		} else {
 			if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, *output) != 0)
@@ -645,8 +649,8 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
 	return 0;
 }
 
-static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
-				     int mask)
+static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist,
+				     struct mmap_params *mp)
 {
 	int cpu, thread;
 	int nr_cpus = cpu_map__nr(evlist->cpus);
@@ -657,8 +661,8 @@ static int perf_evlist__mmap_per_cpu(struct perf_evlist *evlist, int prot,
 		int output = -1;
 
 		for (thread = 0; thread < nr_threads; thread++) {
-			if (perf_evlist__mmap_per_evsel(evlist, cpu, prot, mask,
-							cpu, thread, &output))
+			if (perf_evlist__mmap_per_evsel(evlist, cpu, mp, cpu,
+							thread, &output))
 				goto out_unmap;
 		}
 	}
@@ -671,8 +675,8 @@ out_unmap:
 	return -1;
 }
 
-static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
-					int mask)
+static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist,
+					struct mmap_params *mp)
 {
 	int thread;
 	int nr_threads = thread_map__nr(evlist->threads);
@@ -681,8 +685,8 @@ static int perf_evlist__mmap_per_thread(struct perf_evlist *evlist, int prot,
 	for (thread = 0; thread < nr_threads; thread++) {
 		int output = -1;
 
-		if (perf_evlist__mmap_per_evsel(evlist, thread, prot, mask, 0,
-						thread, &output))
+		if (perf_evlist__mmap_per_evsel(evlist, thread, mp, 0, thread,
+						&output))
 			goto out_unmap;
 	}
 
@@ -785,7 +789,9 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 	struct perf_evsel *evsel;
 	const struct cpu_map *cpus = evlist->cpus;
 	const struct thread_map *threads = evlist->threads;
-	int prot = PROT_READ | (overwrite ? 0 : PROT_WRITE), mask;
+	struct mmap_params mp = {
+		.prot = PROT_READ | (overwrite ? 0 : PROT_WRITE),
+	};
 
 	if (evlist->mmap == NULL && perf_evlist__alloc_mmap(evlist) < 0)
 		return -ENOMEM;
@@ -796,7 +802,7 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 	evlist->overwrite = overwrite;
 	evlist->mmap_len = perf_evlist__mmap_size(pages);
 	pr_debug("mmap size %zuB\n", evlist->mmap_len);
-	mask = evlist->mmap_len - page_size - 1;
+	mp.mask = evlist->mmap_len - page_size - 1;
 
 	list_for_each_entry(evsel, &evlist->entries, node) {
 		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
@@ -806,9 +812,9 @@ int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 	}
 
 	if (cpu_map__empty(cpus))
-		return perf_evlist__mmap_per_thread(evlist, prot, mask);
+		return perf_evlist__mmap_per_thread(evlist, &mp);
 
-	return perf_evlist__mmap_per_cpu(evlist, prot, mask);
+	return perf_evlist__mmap_per_cpu(evlist, &mp);
 }
 
 int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target)
Signature · 1 line
1.8.5.1
author reviewer
#90 lore raw

PATCH 20/71 perf tools: Move mem_bswap32/64 to util.c

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Move functions mem_bswap_32() and mem_bswap_64()
so they can be reused.

---
 tools/perf/util/session.c | 21 ---------------------
 tools/perf/util/session.h |  2 --
 tools/perf/util/util.c    | 22 ++++++++++++++++++++++
 tools/perf/util/util.h    |  3 +++
 4 files changed, 25 insertions(+), 23 deletions(-)
tools/perf/util/session.c +0 -21
index 10ac07a..6bfb36b 100644
@@ -247,27 +247,6 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
 	}
 }
  
-void mem_bswap_32(void *src, int byte_size)
-{
-	u32 *m = src;
-	while (byte_size > 0) {
-		*m = bswap_32(*m);
-		byte_size -= sizeof(u32);
-		++m;
-	}
-}
-
-void mem_bswap_64(void *src, int byte_size)
-{
-	u64 *m = src;
-
-	while (byte_size > 0) {
-		*m = bswap_64(*m);
-		byte_size -= sizeof(u64);
-		++m;
-	}
-}
-
 static void swap_sample_id_all(union perf_event *event, void *data)
 {
 	void *end = (void *) event + event->header.size;
tools/perf/util/session.h +0 -2
index ca1d734..69e3bad 100644
@@ -76,8 +76,6 @@ int perf_session__resolve_callchain(struct perf_session *session,
 
 bool perf_session__has_traces(struct perf_session *session, const char *msg);
 
-void mem_bswap_64(void *src, int byte_size);
-void mem_bswap_32(void *src, int byte_size);
 void perf_event__attr_swap(struct perf_event_attr *attr);
 
 int perf_session__create_kernel_maps(struct perf_session *session);
tools/perf/util/util.c +22 -0
index 3aed4af..6df02a9 100644
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
+#include <byteswap.h>
 #include <linux/kernel.h>
 
 /*
@@ -501,3 +502,24 @@ int perf_event_paranoid(void)
 
 	return value;
 }
+
+void mem_bswap_32(void *src, int byte_size)
+{
+	u32 *m = src;
+	while (byte_size > 0) {
+		*m = bswap_32(*m);
+		byte_size -= sizeof(u32);
+		++m;
+	}
+}
+
+void mem_bswap_64(void *src, int byte_size)
+{
+	u64 *m = src;
+
+	while (byte_size > 0) {
+		*m = bswap_64(*m);
+		byte_size -= sizeof(u64);
+		++m;
+	}
+}
tools/perf/util/util.h +3 -0
index 4b6b260..24da45e 100644
@@ -312,4 +312,7 @@ int filename__read_str(const char *filename, char **buf, size_t *sizep);
 
 int perf_event_paranoid(void);
 
+void mem_bswap_64(void *src, int byte_size);
+void mem_bswap_32(void *src, int byte_size);
+
 #endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.8.5.1
#91 lore raw

[tip:perf/core] perf tools: Move mem_bswap32/64 to util.c

perf tools: Move mem_bswap32/64 to util.c
Commit-ID:  71db07b12eace6a3619335d03eaf3cbe2de131ed
Gitweb:     http://git.kernel.org/tip/71db07b12eace6a3619335d03eaf3cbe2de131ed
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 11 Dec 2013 14:36:32 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 13 Dec 2013 10:30:21 -0300

perf tools: Move mem_bswap32/64 to util.c

Move functions mem_bswap_32() and mem_bswap_64() so they can be reused.

---
 tools/perf/util/session.c | 21 ---------------------
 tools/perf/util/session.h |  2 --
 tools/perf/util/util.c    | 22 ++++++++++++++++++++++
 tools/perf/util/util.h    |  3 +++
 4 files changed, 25 insertions(+), 23 deletions(-)
tools/perf/util/session.c +0 -21
index e748f29..989b2e3 100644
@@ -247,27 +247,6 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
 	}
 }
  
-void mem_bswap_32(void *src, int byte_size)
-{
-	u32 *m = src;
-	while (byte_size > 0) {
-		*m = bswap_32(*m);
-		byte_size -= sizeof(u32);
-		++m;
-	}
-}
-
-void mem_bswap_64(void *src, int byte_size)
-{
-	u64 *m = src;
-
-	while (byte_size > 0) {
-		*m = bswap_64(*m);
-		byte_size -= sizeof(u64);
-		++m;
-	}
-}
-
 static void swap_sample_id_all(union perf_event *event, void *data)
 {
 	void *end = (void *) event + event->header.size;
tools/perf/util/session.h +0 -2
index 2a3955e..9c25d49 100644
@@ -74,8 +74,6 @@ int perf_session__resolve_callchain(struct perf_session *session,
 
 bool perf_session__has_traces(struct perf_session *session, const char *msg);
 
-void mem_bswap_64(void *src, int byte_size);
-void mem_bswap_32(void *src, int byte_size);
 void perf_event__attr_swap(struct perf_event_attr *attr);
 
 int perf_session__create_kernel_maps(struct perf_session *session);
tools/perf/util/util.c +22 -0
index 8f63dba..42ad667 100644
@@ -10,6 +10,7 @@
 #include <string.h>
 #include <errno.h>
 #include <limits.h>
+#include <byteswap.h>
 #include <linux/kernel.h>
 
 /*
@@ -515,3 +516,24 @@ int perf_event_paranoid(void)
 
 	return value;
 }
+
+void mem_bswap_32(void *src, int byte_size)
+{
+	u32 *m = src;
+	while (byte_size > 0) {
+		*m = bswap_32(*m);
+		byte_size -= sizeof(u32);
+		++m;
+	}
+}
+
+void mem_bswap_64(void *src, int byte_size)
+{
+	u64 *m = src;
+
+	while (byte_size > 0) {
+		*m = bswap_64(*m);
+		byte_size -= sizeof(u64);
+		++m;
+	}
+}
tools/perf/util/util.h +3 -0
index 1e7d413..a1eea3e 100644
@@ -323,5 +323,8 @@ int filename__read_int(const char *filename, int *value);
 int filename__read_str(const char *filename, char **buf, size_t *sizep);
 int perf_event_paranoid(void);
 
+void mem_bswap_64(void *src, int byte_size);
+void mem_bswap_32(void *src, int byte_size);
+
 const char *get_filename_for_perf_kvm(void);
 #endif /* GIT_COMPAT_UTIL_H */
author reviewer
#92 lore raw

PATCH 21/71 perf tools: Add feature test for __sync_val_compare_and_swap

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a feature test for __sync_val_compare_and_swap() and __sync_bool_compare_and_swap()

---
 tools/perf/config/Makefile                                 |  5 +++++
 tools/perf/config/feature-checks/Makefile                  |  4 ++++
 tools/perf/config/feature-checks/test-all.c                |  5 +++++
 .../config/feature-checks/test-sync-compare-and-swap.c     | 14 ++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 tools/perf/config/feature-checks/test-sync-compare-and-swap.c
tools/perf/config/Makefile +5 -0
index bae1072..43a2879 100644
@@ -126,6 +126,7 @@ CORE_FEATURE_TESTS =			\
 	backtrace			\
 	dwarf				\
 	fortify-source			\
+	sync-compare-and-swap		\
 	glibc				\
 	gtk2				\
 	gtk2-infobar			\
@@ -234,6 +235,10 @@ CFLAGS += -I$(LIB_INCLUDE)
 
 CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
 
+ifeq ($(feature-sync-compare-and-swap), 1)
+  CFLAGS += -DHAVE_SYNC_COMPARE_AND_SWAP_SUPPORT
+endif
+
 ifndef NO_BIONIC
   $(call feature_check,bionic)
   ifeq ($(feature-bionic), 1)
tools/perf/config/feature-checks/Makefile +4 -0
index b8bb749..b4b7bb2 100644
@@ -5,6 +5,7 @@ FILES=					\
 	test-bionic			\
 	test-dwarf			\
 	test-fortify-source		\
+	test-sync-compare-and-swap	\
 	test-glibc			\
 	test-gtk2			\
 	test-gtk2-infobar		\
@@ -140,6 +141,9 @@ test-backtrace:
 test-timerfd:
 	$(BUILD)
 
+test-sync-compare-and-swap:
+	$(BUILD)
+
 -include *.d
 
 ###############################
tools/perf/config/feature-checks/test-all.c +5 -0
index 9b8a544..5cfec18 100644
@@ -89,6 +89,10 @@
 # include "test-stackprotector-all.c"
 #undef main
 
+#define main main_test_sync_compare_and_swap
+# include "test-sync-compare-and-swap.c"
+#undef main
+
 int main(int argc, char *argv[])
 {
 	main_test_libpython();
@@ -111,6 +115,7 @@ int main(int argc, char *argv[])
 	main_test_libnuma();
 	main_test_timerfd();
 	main_test_stackprotector_all();
+	main_test_sync_compare_and_swap();
 
 	return 0;
 }
tools/perf/config/feature-checks/test-sync-compare-and-swap.c new file +14 -0
new file mode 100644
index 0000000..c34d4ca
@@ -0,0 +1,14 @@
+#include <stdint.h>
+
+volatile uint64_t x;
+
+int main(int argc, char *argv[])
+{
+	uint64_t old, new = argc;
+
+	argv = argv;
+	do {
+		old = __sync_val_compare_and_swap(&x, 0, 0);
+	} while (!__sync_bool_compare_and_swap(&x, old, new));
+	return old == new;
+}
Signature · 1 line
1.8.5.1
#93 lore raw

Re: [PATCH v0 21/71] perf tools: Add feature test for __sync_val_compare_and_swap

Em Wed, Dec 11, 2013 at 02:36:33PM +0200, Alexander Shishkin escreveu:

Quoted 4 lines

From: Adrian Hunter <[email protected]>

Add a feature test for __sync_val_compare_and_swap() and __sync_bool_compare_and_swap()

This makes the global feature tests to be rebuilt all the time, i.e. no more caching on a relatively recent system:

[acme@ssdandy linux]$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --disable-build-with-cxx --disable-build-poststage1-with-cxx --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC) [acme@ssdandy linux]$o

[acme@ssdandy linux]$ cat /etc/fedora-release
Fedora release 18 (Spherical Cow)

Can you provide more info about these gcc builtins and what is the minimum system where this test will succeed?

In this system it works, as I can see:

... sync-compare-and-swap: [ on ]

[acme@ssdandy linux]$ time make O=/tmp/build/perf -C tools/perf/ install-bin
make: Entering directory `/home/acme/git/linux/tools/perf'
BUILD: Doing 'make -j8' parallel build

Auto-detecting system features:

...                     backtrace: [ on  ]
...                         dwarf: [ on  ]
...                fortify-source: [ on  ]
...         sync-compare-and-swap: [ on  ]
...                         glibc: [ on  ]
...                          gtk2: [ on  ]
...                  gtk2-infobar: [ on  ]
...                      libaudit: [ on  ]
...                        libbfd: [ on  ]
...                        libelf: [ on  ]
...             libelf-getphdrnum: [ on  ]
...                   libelf-mmap: [ on  ]
...                       libnuma: [ on  ]
...                       libperl: [ on  ]
...                     libpython: [ on  ]
...             libpython-version: [ on  ]
...                      libslang: [ on  ]
...                     libunwind: [ on  ]
...                       on-exit: [ on  ]
...            stackprotector-all: [ on  ]
...                       timerfd: [ on  ]

GEN perf-archive

Please check the recent changes from Jean Pihet, I think he had similar problems, i.e. caching stopped working.

- Arnaldo

Quoted 99 lines
---
 tools/perf/config/Makefile                                 |  5 +++++
 tools/perf/config/feature-checks/Makefile                  |  4 ++++
 tools/perf/config/feature-checks/test-all.c                |  5 +++++
 .../config/feature-checks/test-sync-compare-and-swap.c     | 14 ++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 tools/perf/config/feature-checks/test-sync-compare-and-swap.c
tools/perf/config/Makefile +5 -0
index bae1072..43a2879 100644
@@ -126,6 +126,7 @@ CORE_FEATURE_TESTS =			\
 	backtrace			\
 	dwarf				\
 	fortify-source			\
+	sync-compare-and-swap		\
 	glibc				\
 	gtk2				\
 	gtk2-infobar			\
@@ -234,6 +235,10 @@ CFLAGS += -I$(LIB_INCLUDE)
 
 CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
 
+ifeq ($(feature-sync-compare-and-swap), 1)
+  CFLAGS += -DHAVE_SYNC_COMPARE_AND_SWAP_SUPPORT
+endif
+
 ifndef NO_BIONIC
   $(call feature_check,bionic)
   ifeq ($(feature-bionic), 1)
tools/perf/config/feature-checks/Makefile +4 -0
index b8bb749..b4b7bb2 100644
@@ -5,6 +5,7 @@ FILES=					\
 	test-bionic			\
 	test-dwarf			\
 	test-fortify-source		\
+	test-sync-compare-and-swap	\
 	test-glibc			\
 	test-gtk2			\
 	test-gtk2-infobar		\
@@ -140,6 +141,9 @@ test-backtrace:
 test-timerfd:
 	$(BUILD)
 
+test-sync-compare-and-swap:
+	$(BUILD)
+
 -include *.d
 
 ###############################
tools/perf/config/feature-checks/test-all.c +5 -0
index 9b8a544..5cfec18 100644
@@ -89,6 +89,10 @@
 # include "test-stackprotector-all.c"
 #undef main
 
+#define main main_test_sync_compare_and_swap
+# include "test-sync-compare-and-swap.c"
+#undef main
+
 int main(int argc, char *argv[])
 {
 	main_test_libpython();
@@ -111,6 +115,7 @@ int main(int argc, char *argv[])
 	main_test_libnuma();
 	main_test_timerfd();
 	main_test_stackprotector_all();
+	main_test_sync_compare_and_swap();
 
 	return 0;
 }
tools/perf/config/feature-checks/test-sync-compare-and-swap.c new file +14 -0
new file mode 100644
index 0000000..c34d4ca
@@ -0,0 +1,14 @@
+#include <stdint.h>
+
+volatile uint64_t x;
+
+int main(int argc, char *argv[])
+{
+	uint64_t old, new = argc;
+
+	argv = argv;
+	do {
+		old = __sync_val_compare_and_swap(&x, 0, 0);
+	} while (!__sync_bool_compare_and_swap(&x, old, new));
+	return old == new;
+}
Signature · 1 line
1.8.5.1
#94 lore raw

Re: [PATCH v0 21/71] perf tools: Add feature test for __sync_val_compare_and_swap

Quoted 2 lines

Can you provide more info about these gcc builtins and what is the minimum system where this test will succeed?

CMPXCHG for x86 is available since the 486 or so, so practically everywhere.

I think that's mainly for other architectures.

it would be reasonable to just use #if defined(__x86_64__) || defined(__i386__) instead.

-Andi

reviewer
#95 lore raw

Re: [PATCH v0 21/71] perf tools: Add feature test for __sync_val_compare_and_swap

Quoted 10 lines · Andi Kleen
Quoted 2 lines

Can you provide more info about these gcc builtins and what is the minimum system where this test will succeed?

CMPXCHG for x86 is available since the 486 or so, so practically everywhere.

I think that's mainly for other architectures.

it would be reasonable to just use #if defined(__x86_64__) || defined(__i386__) instead.

__sync_val_compare_and_swap() is being used in the itrace abstraction which is architecture neutral, so I can't use x86 defines.

reviewer
#96 lore raw

Re: [PATCH v0 21/71] perf tools: Add feature test for __sync_val_compare_and_swap

In reply to Arnaldo Carvalho de Melo
Quoted 33 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 11, 2013 at 02:36:33PM +0200, Alexander Shishkin escreveu:

Quoted 4 lines

From: Adrian Hunter <[email protected]>

Add a feature test for __sync_val_compare_and_swap() and __sync_bool_compare_and_swap()

This makes the global feature tests to be rebuilt all the time, i.e. no more caching on a relatively recent system:

[acme@ssdandy linux]$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.7.2/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --disable-build-with-cxx --disable-build-poststage1-with-cxx --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-linker-hash-style=gnu --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,go,lto --enable-plugin --enable-initfini-array --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
gcc version 4.7.2 20121109 (Red Hat 4.7.2-8) (GCC) [acme@ssdandy linux]$o

Quoted 5 lines

[acme@ssdandy linux]$ cat /etc/fedora-release
Fedora release 18 (Spherical Cow)

Can you provide more info about these gcc builtins and what is the minimum system where this test will succeed?

The first reference in the gcc manuals is in gcc version 4.1.2

	http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/

However I am not sure what will happen on all architectures. The gcc manual says:

Not all operations are supported by all target processors. If a particular operation cannot be implemented on the target processor, a warning is generated and a call an external function is generated. The external function carries the same name as the built-in version, with an additional suffix ‘_n’ where n is the size of the data type.

Quoted 33 lines

In this system it works, as I can see:

... sync-compare-and-swap: [ on ]

[acme@ssdandy linux]$ time make O=/tmp/build/perf -C tools/perf/ install-bin
make: Entering directory `/home/acme/git/linux/tools/perf'
BUILD: Doing 'make -j8' parallel build

Auto-detecting system features:

...                     backtrace: [ on  ]
...                         dwarf: [ on  ]
...                fortify-source: [ on  ]
...         sync-compare-and-swap: [ on  ]
...                         glibc: [ on  ]
...                          gtk2: [ on  ]
...                  gtk2-infobar: [ on  ]
...                      libaudit: [ on  ]
...                        libbfd: [ on  ]
...                        libelf: [ on  ]
...             libelf-getphdrnum: [ on  ]
...                   libelf-mmap: [ on  ]
...                       libnuma: [ on  ]
...                       libperl: [ on  ]
...                     libpython: [ on  ]
...             libpython-version: [ on  ]
...                      libslang: [ on  ]
...                     libunwind: [ on  ]
...                       on-exit: [ on  ]
...            stackprotector-all: [ on  ]
...                       timerfd: [ on  ]
Quoted 4 lines

GEN perf-archive

Please check the recent changes from Jean Pihet, I think he had similar problems, i.e. caching stopped working.

The problem is argc and argv must be passed to main_test_sync_compare_and_swap(). Will be fixed next version.

Quoted 105 lines

- Arnaldo

Quoted 99 lines
---
 tools/perf/config/Makefile                                 |  5 +++++
 tools/perf/config/feature-checks/Makefile                  |  4 ++++
 tools/perf/config/feature-checks/test-all.c                |  5 +++++
 .../config/feature-checks/test-sync-compare-and-swap.c     | 14 ++++++++++++++
 4 files changed, 28 insertions(+)
 create mode 100644 tools/perf/config/feature-checks/test-sync-compare-and-swap.c
tools/perf/config/Makefile +5 -0
index bae1072..43a2879 100644
@@ -126,6 +126,7 @@ CORE_FEATURE_TESTS =			\
 	backtrace			\
 	dwarf				\
 	fortify-source			\
+	sync-compare-and-swap		\
 	glibc				\
 	gtk2				\
 	gtk2-infobar			\
@@ -234,6 +235,10 @@ CFLAGS += -I$(LIB_INCLUDE)
 
 CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE
 
+ifeq ($(feature-sync-compare-and-swap), 1)
+  CFLAGS += -DHAVE_SYNC_COMPARE_AND_SWAP_SUPPORT
+endif
+
 ifndef NO_BIONIC
   $(call feature_check,bionic)
   ifeq ($(feature-bionic), 1)
tools/perf/config/feature-checks/Makefile +4 -0
index b8bb749..b4b7bb2 100644
@@ -5,6 +5,7 @@ FILES=					\
 	test-bionic			\
 	test-dwarf			\
 	test-fortify-source		\
+	test-sync-compare-and-swap	\
 	test-glibc			\
 	test-gtk2			\
 	test-gtk2-infobar		\
@@ -140,6 +141,9 @@ test-backtrace:
 test-timerfd:
 	$(BUILD)
 
+test-sync-compare-and-swap:
+	$(BUILD)
+
 -include *.d
 
 ###############################
tools/perf/config/feature-checks/test-all.c +5 -0
index 9b8a544..5cfec18 100644
@@ -89,6 +89,10 @@
 # include "test-stackprotector-all.c"
 #undef main
 
+#define main main_test_sync_compare_and_swap
+# include "test-sync-compare-and-swap.c"
+#undef main
+
 int main(int argc, char *argv[])
 {
 	main_test_libpython();
@@ -111,6 +115,7 @@ int main(int argc, char *argv[])
 	main_test_libnuma();
 	main_test_timerfd();
 	main_test_stackprotector_all();
+	main_test_sync_compare_and_swap();
 
 	return 0;
 }
tools/perf/config/feature-checks/test-sync-compare-and-swap.c new file +14 -0
new file mode 100644
index 0000000..c34d4ca
@@ -0,0 +1,14 @@
+#include <stdint.h>
+
+volatile uint64_t x;
+
+int main(int argc, char *argv[])
+{
+	uint64_t old, new = argc;
+
+	argv = argv;
+	do {
+		old = __sync_val_compare_and_swap(&x, 0, 0);
+	} while (!__sync_bool_compare_and_swap(&x, old, new));
+	return old == new;
+}
Signature · 1 line
1.8.5.1
author reviewer
#97 lore raw

PATCH 22/71 perf tools: Add option macro OPT_CALLBACK_OPTARG

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option macro that is the same as OPT_CALLBACK except that the argument is optional and it is possible to associate additional data with it.

---
 tools/perf/util/parse-options.h | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/parse-options.h +5 -0
index cbf0149..aec0afb 100644
@@ -98,6 +98,7 @@ struct option {
 	parse_opt_cb *callback;
 	intptr_t defval;
 	bool *set;
+	void *data;
 };
 
 #define check_vtype(v, type) ( BUILD_BUG_ON_ZERO(!__builtin_types_compatible_p(typeof(v), type)) + v )
@@ -131,6 +132,10 @@ struct option {
 	{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l),\
 	.value = (v), (a), .help = (h), .callback = (f), .defval = (intptr_t)d,\
 	.flags = PARSE_OPT_LASTARG_DEFAULT | PARSE_OPT_NOARG}
+#define OPT_CALLBACK_OPTARG(s, l, v, d, a, h, f) \
+	{ .type = OPTION_CALLBACK, .short_name = (s), .long_name = (l), \
+	  .value = (v), (a), .help = (h), .callback = (f), \
+	  .flags = PARSE_OPT_OPTARG, .data = (d) }
 
 /* parse_options() will filter out the processed options and leave the
  * non-option argments in argv[].
Signature · 1 line
1.8.5.1
author reviewer
#98 lore raw

PATCH 23/71 perf evlist: Add perf_evlist__to_front()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to move a selected event to the front of the list.

This is needed because it is not possible to use the PERF_EVENT_IOC_SET_OUTPUT IOCTL from an Instruction Tracing event to a non-Instruction Tracing event. Thus the Instruction Tracing event must come first.

---
 tools/perf/util/evlist.c | 17 +++++++++++++++++
 tools/perf/util/evlist.h |  3 +++
 2 files changed, 20 insertions(+)
tools/perf/util/evlist.c +17 -0
index f9dbf5f..93683bc 100644
@@ -1216,3 +1216,20 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 
 	return 0;
 }
+
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel)
+{
+	struct perf_evsel *evsel, *n;
+	LIST_HEAD(move);
+
+	if (move_evsel == perf_evlist__first(evlist))
+		return;
+
+	list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
+		if (evsel->leader == move_evsel->leader)
+			list_move_tail(&evsel->node, &move);
+	}
+
+	list_splice(&move, &evlist->entries);
+}
tools/perf/util/evlist.h +3 -0
index 8a04aae..9f64ede 100644
@@ -194,5 +194,8 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 }
 
 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel);
+
 
 #endif /* __PERF_EVLIST_H */
Signature · 1 line
1.8.5.1
#99 lore raw

Re: [PATCH v0 23/71] perf evlist: Add perf_evlist__to_front()

Em Wed, Dec 11, 2013 at 02:36:35PM +0200, Alexander Shishkin escreveu:

Quoted 8 lines

From: Adrian Hunter <[email protected]>

Add a function to move a selected event to the front of the list.

This is needed because it is not possible to use the PERF_EVENT_IOC_SET_OUTPUT IOCTL from an Instruction Tracing event to a non-Instruction Tracing event. Thus the Instruction Tracing event must come first.

The description doesn't matches what the code is doing, as it is moving a _group_, not an event.

Also I wonder if you can't do this more effiently by finding where it starts, ends and then doing some more splice like operatins instead of moving member by member to a temp list. Setting the (next, prev) fields of the various sublists to the right places.

There is even list_cut_position() already in list.h, used with list_move() and list_splice() I think you can do it more efficiently, I think.

- Arnaldo

Quoted 46 lines
---
 tools/perf/util/evlist.c | 17 +++++++++++++++++
 tools/perf/util/evlist.h |  3 +++
 2 files changed, 20 insertions(+)
tools/perf/util/evlist.c +17 -0
index f9dbf5f..93683bc 100644
@@ -1216,3 +1216,20 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 
 	return 0;
 }
+
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel)
+{
+	struct perf_evsel *evsel, *n;
+	LIST_HEAD(move);
+
+	if (move_evsel == perf_evlist__first(evlist))
+		return;
+
+	list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
+		if (evsel->leader == move_evsel->leader)
+			list_move_tail(&evsel->node, &move);
+	}
+
+	list_splice(&move, &evlist->entries);
+}
tools/perf/util/evlist.h +3 -0
index 8a04aae..9f64ede 100644
@@ -194,5 +194,8 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 }
 
 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel);
+
 
 #endif /* __PERF_EVLIST_H */
Signature · 1 line
1.8.5.1
reviewer
#100 lore raw

Re: [PATCH v0 23/71] perf evlist: Add perf_evlist__to_front()

Quoted 5 lines · Arnaldo Carvalho de Melo

Em Wed, Dec 11, 2013 at 02:36:35PM +0200, Alexander Shishkin escreveu:

Quoted 3 lines

From: Adrian Hunter <[email protected]>

Add a function to move a selected event to the front of the list.

Quoted 8 lines
Quoted 4 lines

This is needed because it is not possible to use the PERF_EVENT_IOC_SET_OUTPUT IOCTL from an Instruction Tracing event to a non-Instruction Tracing event. Thus the Instruction Tracing event must come first.

The description doesn't matches what the code is doing, as it is moving a _group_, not an event.

OK I will rename it

Quoted 9 lines

Also I wonder if you can't do this more effiently by finding where it starts, ends and then doing some more splice like operatins instead of moving member by member to a temp list. Setting the (next, prev) fields of the various sublists to the right places.

There is even list_cut_position() already in list.h, used with list_move() and list_splice() I think you can do it more efficiently, I think.

OK

Quoted 51 lines

- Arnaldo

Quoted 46 lines
---
 tools/perf/util/evlist.c | 17 +++++++++++++++++
 tools/perf/util/evlist.h |  3 +++
 2 files changed, 20 insertions(+)
tools/perf/util/evlist.c +17 -0
index f9dbf5f..93683bc 100644
@@ -1216,3 +1216,20 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 
 	return 0;
 }
+
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel)
+{
+	struct perf_evsel *evsel, *n;
+	LIST_HEAD(move);
+
+	if (move_evsel == perf_evlist__first(evlist))
+		return;
+
+	list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
+		if (evsel->leader == move_evsel->leader)
+			list_move_tail(&evsel->node, &move);
+	}
+
+	list_splice(&move, &evlist->entries);
+}
tools/perf/util/evlist.h +3 -0
index 8a04aae..9f64ede 100644
@@ -194,5 +194,8 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 }
 
 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel);
+
 
 #endif /* __PERF_EVLIST_H */
Signature · 1 line
1.8.5.1
#101 lore raw

[tip:perf/core] perf evlist: Add perf_evlist__to_front()

In reply to Alexander Shishkin
perf evlist: Add perf_evlist__to_front()
Commit-ID:  a025e4f0d8a92b38539d39b495b530015296b4d9
Gitweb:     http://git.kernel.org/tip/a025e4f0d8a92b38539d39b495b530015296b4d9
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 11 Dec 2013 14:36:35 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 13 Dec 2013 10:30:21 -0300

perf evlist: Add perf_evlist__to_front()

Add a function to move a selected event to the
front of the list.

This is needed because it is not possible
to use the PERF_EVENT_IOC_SET_OUTPUT IOCTL
from an Instruction Tracing event to a
non-Instruction Tracing event. Thus the
Instruction Tracing event must come first.

---
 tools/perf/util/evlist.c | 17 +++++++++++++++++
 tools/perf/util/evlist.h |  3 +++
 2 files changed, 20 insertions(+)
tools/perf/util/evlist.c +17 -0
index 2eb7378..0b31cee 100644
@@ -1212,3 +1212,20 @@ int perf_evlist__strerror_open(struct perf_evlist *evlist __maybe_unused,
 
 	return 0;
 }
+
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel)
+{
+	struct perf_evsel *evsel, *n;
+	LIST_HEAD(move);
+
+	if (move_evsel == perf_evlist__first(evlist))
+		return;
+
+	list_for_each_entry_safe(evsel, n, &evlist->entries, node) {
+		if (evsel->leader == move_evsel->leader)
+			list_move_tail(&evsel->node, &move);
+	}
+
+	list_splice(&move, &evlist->entries);
+}
tools/perf/util/evlist.h +3 -0
index 8a04aae..9f64ede 100644
@@ -194,5 +194,8 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 }
 
 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
+void perf_evlist__to_front(struct perf_evlist *evlist,
+			   struct perf_evsel *move_evsel);
+
 
 #endif /* __PERF_EVLIST_H */
author reviewer
#102 lore raw

PATCH 24/71 perf evlist: Add perf_evlist__set_tracking_event()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to change which event is used to track mmap, comm and task events.

This is needed with Instruction Tracing because the Instruction Tracing event must come first but cannot be used for tracking because it will be disabled under some circumstances.

---
 tools/perf/util/evlist.c | 20 ++++++++++++++++++++
 tools/perf/util/evlist.h |  3 ++-
 2 files changed, 22 insertions(+), 1 deletion(-)
tools/perf/util/evlist.c +20 -0
index 93683bc..ae9cbe6 100644
@@ -1233,3 +1233,23 @@ void perf_evlist__to_front(struct perf_evlist *evlist,
 
 	list_splice(&move, &evlist->entries);
 }
+
+int perf_evlist__set_tracking_event(struct perf_evlist *evlist,
+				    struct perf_evsel *tracking_evsel)
+{
+	struct perf_evsel *evsel;
+
+	if (tracking_evsel->idx == 0)
+		return 0;
+
+	if (tracking_evsel->leader->nr_members > 1)
+		return -EINVAL;
+
+	list_for_each_entry(evsel, &evlist->entries, node) {
+		if (evsel->idx < tracking_evsel->idx)
+			evsel->idx += 1;
+	}
+	tracking_evsel->idx = 0;
+
+	return 0;
+}
tools/perf/util/evlist.h +2 -1
index 9f64ede..2c8d068 100644
@@ -196,6 +196,7 @@ static inline void perf_mmap__write_tail(struct perf_mmap *md,
 bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
 void perf_evlist__to_front(struct perf_evlist *evlist,
 			   struct perf_evsel *move_evsel);
-
+int perf_evlist__set_tracking_event(struct perf_evlist *evlist,
+				    struct perf_evsel *tracking_evsel);
 
 #endif /* __PERF_EVLIST_H */
Signature · 1 line
1.8.5.1
author reviewer
#103 lore raw

PATCH 25/71 perf evsel: Add 'no_aux_samples' option

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option to prevent additional samples being added to a selected event by perf_evsel__config().

This is needed when using the sched_switch tracepoint to follow object code execution. Since sched_switch will be used only for switch information, additional sampling is wasteful.

---
 tools/perf/util/evsel.c | 6 +++---
 tools/perf/util/evsel.h | 1 +
 2 files changed, 4 insertions(+), 3 deletions(-)
tools/perf/util/evsel.c +3 -3
index 7b510fd..dbf737c 100644
@@ -596,7 +596,7 @@ void perf_evsel__config(struct perf_evsel *evsel,
 		attr->mmap_data = track;
 	}
 
-	if (opts->call_graph) {
+	if (opts->call_graph && !evsel->no_aux_samples) {
 		perf_evsel__set_sample_bit(evsel, CALLCHAIN);
 
 		if (opts->call_graph == CALLCHAIN_DWARF) {
@@ -619,7 +619,7 @@ void perf_evsel__config(struct perf_evsel *evsel,
 	     target__has_cpu(&opts->target) || per_cpu))
 		perf_evsel__set_sample_bit(evsel, TIME);
 
-	if (opts->raw_samples) {
+	if (opts->raw_samples && !evsel->no_aux_samples) {
 		perf_evsel__set_sample_bit(evsel, TIME);
 		perf_evsel__set_sample_bit(evsel, RAW);
 		perf_evsel__set_sample_bit(evsel, CPU);
@@ -632,7 +632,7 @@ void perf_evsel__config(struct perf_evsel *evsel,
 		attr->watermark = 0;
 		attr->wakeup_events = 1;
 	}
-	if (opts->branch_stack) {
+	if (opts->branch_stack && !evsel->no_aux_samples) {
 		perf_evsel__set_sample_bit(evsel, BRANCH_STACK);
 		attr->branch_sample_type = opts->branch_stack;
 	}
tools/perf/util/evsel.h +1 -0
index 8120eeb..af38e2c 100644
@@ -83,6 +83,7 @@ struct perf_evsel {
 	int			is_pos;
 	bool 			supported;
 	bool 			needs_swap;
+	bool			no_aux_samples;
 	/* parse modifier helper */
 	int			exclude_GH;
 	int			nr_members;
Signature · 1 line
1.8.5.1
author reviewer
#104 lore raw

PATCH 26/71 perf evsel: Add 'immediate' option

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option to cause a selected event to be enabled immediately when configured by perf_evsel__config().

This is needed when using the sched_switch tracepoint to follow object code execution. By having sched_switch enabled immediately the first sched_switch event always precedes the start of other tracing.

---
 tools/perf/util/evsel.c | 5 +++++
 tools/perf/util/evsel.h | 1 +
 2 files changed, 6 insertions(+)
tools/perf/util/evsel.c +5 -0
index dbf737c..5fcd7cb 100644
@@ -661,6 +661,11 @@ void perf_evsel__config(struct perf_evsel *evsel,
 	 */
 	if (target__none(&opts->target) && perf_evsel__is_group_leader(evsel))
 		attr->enable_on_exec = 1;
+
+	if (evsel->immediate) {
+		attr->disabled = 0;
+		attr->enable_on_exec = 0;
+	}
 }
 
 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
tools/perf/util/evsel.h +1 -0
index af38e2c..de1b36e 100644
@@ -84,6 +84,7 @@ struct perf_evsel {
 	bool 			supported;
 	bool 			needs_swap;
 	bool			no_aux_samples;
+	bool			immediate;
 	/* parse modifier helper */
 	int			exclude_GH;
 	int			nr_members;
Signature · 1 line
1.8.5.1
author reviewer
#105 lore raw

PATCH 27/71 perf evlist: Add 'system_wide' option

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option to cause a selected event to be opened always without a pid when configured by perf_evsel__config().

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch occurs before the task switch and so it cannot record it in a context limited to that task. Note that also means that sched_switch is useless when capturing data per-thread, as is the 'context-switches' software event for the same reason.

---
 tools/perf/util/evlist.c | 45 +++++++++++++++++++++++++++++++++++++--------
 tools/perf/util/evsel.c  | 31 ++++++++++++++++++++++++++-----
 tools/perf/util/evsel.h  |  1 +
 3 files changed, 64 insertions(+), 13 deletions(-)
tools/perf/util/evlist.c +37 -8
index ae9cbe6..3959978 100644
@@ -261,17 +261,27 @@ int perf_evlist__add_newtp(struct perf_evlist *evlist,
 	return 0;
 }
 
+static int perf_evlist__nr_threads(struct perf_evlist *evlist,
+				   struct perf_evsel *evsel)
+{
+	if (evsel->system_wide)
+		return 1;
+	else
+		return thread_map__nr(evlist->threads);
+}
+
 void perf_evlist__disable(struct perf_evlist *evlist)
 {
 	int cpu, thread;
 	struct perf_evsel *pos;
 	int nr_cpus = cpu_map__nr(evlist->cpus);
-	int nr_threads = thread_map__nr(evlist->threads);
+	int nr_threads;
 
 	for (cpu = 0; cpu < nr_cpus; cpu++) {
 		list_for_each_entry(pos, &evlist->entries, node) {
 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
 				continue;
+			nr_threads = perf_evlist__nr_threads(evlist, pos);
 			for (thread = 0; thread < nr_threads; thread++)
 				ioctl(FD(pos, cpu, thread),
 				      PERF_EVENT_IOC_DISABLE, 0);
@@ -284,12 +294,13 @@ void perf_evlist__enable(struct perf_evlist *evlist)
 	int cpu, thread;
 	struct perf_evsel *pos;
 	int nr_cpus = cpu_map__nr(evlist->cpus);
-	int nr_threads = thread_map__nr(evlist->threads);
+	int nr_threads;
 
 	for (cpu = 0; cpu < nr_cpus; cpu++) {
 		list_for_each_entry(pos, &evlist->entries, node) {
 			if (!perf_evsel__is_group_leader(pos) || !pos->fd)
 				continue;
+			nr_threads = perf_evlist__nr_threads(evlist, pos);
 			for (thread = 0; thread < nr_threads; thread++)
 				ioctl(FD(pos, cpu, thread),
 				      PERF_EVENT_IOC_ENABLE, 0);
@@ -301,12 +312,14 @@ int perf_evlist__disable_event(struct perf_evlist *evlist,
 			       struct perf_evsel *evsel)
 {
 	int cpu, thread, err;
+	int nr_cpus = cpu_map__nr(evlist->cpus);
+	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
 
 	if (!evsel->fd)
 		return 0;
 
-	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
-		for (thread = 0; thread < evlist->threads->nr; thread++) {
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		for (thread = 0; thread < nr_threads; thread++) {
 			err = ioctl(FD(evsel, cpu, thread),
 				    PERF_EVENT_IOC_DISABLE, 0);
 			if (err)
@@ -320,12 +333,14 @@ int perf_evlist__enable_event(struct perf_evlist *evlist,
 			      struct perf_evsel *evsel)
 {
 	int cpu, thread, err;
+	int nr_cpus = cpu_map__nr(evlist->cpus);
+	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
 
 	if (!evsel->fd)
 		return -EINVAL;
 
-	for (cpu = 0; cpu < evlist->cpus->nr; cpu++) {
-		for (thread = 0; thread < evlist->threads->nr; thread++) {
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		for (thread = 0; thread < nr_threads; thread++) {
 			err = ioctl(FD(evsel, cpu, thread),
 				    PERF_EVENT_IOC_ENABLE, 0);
 			if (err)
@@ -339,7 +354,16 @@ static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
 {
 	int nr_cpus = cpu_map__nr(evlist->cpus);
 	int nr_threads = thread_map__nr(evlist->threads);
-	int nfds = nr_cpus * nr_threads * evlist->nr_entries;
+	int nfds = 0;
+	struct perf_evsel *evsel;
+
+	list_for_each_entry(evsel, &evlist->entries, node) {
+		if (evsel->system_wide)
+			nfds += nr_cpus;
+		else
+			nfds += nr_cpus * nr_threads;
+	}
+
 	evlist->pollfd = malloc(sizeof(struct pollfd) * nfds);
 	return evlist->pollfd != NULL ? 0 : -ENOMEM;
 }
@@ -630,7 +654,12 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
 	struct perf_evsel *evsel;
 
 	list_for_each_entry(evsel, &evlist->entries, node) {
-		int fd = FD(evsel, cpu, thread);
+		int fd;
+
+		if (evsel->system_wide && thread)
+			continue;
+
+		fd = FD(evsel, cpu, thread);
 
 		if (*output == -1) {
 			*output = fd;
tools/perf/util/evsel.c +26 -5
index 5fcd7cb..4e92a22 100644
@@ -671,6 +671,10 @@ void perf_evsel__config(struct perf_evsel *evsel,
 int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
 {
 	int cpu, thread;
+
+	if (evsel->system_wide)
+		nthreads = 1;
+
 	evsel->fd = xyarray__new(ncpus, nthreads, sizeof(int));
 
 	if (evsel->fd) {
@@ -689,6 +693,9 @@ static int perf_evsel__run_ioctl(struct perf_evsel *evsel, int ncpus, int nthrea
 {
 	int cpu, thread;
 
+	if (evsel->system_wide)
+		nthreads = 1;
+
 	for (cpu = 0; cpu < ncpus; cpu++) {
 		for (thread = 0; thread < nthreads; thread++) {
 			int fd = FD(evsel, cpu, thread),
@@ -719,6 +726,9 @@ int perf_evsel__enable(struct perf_evsel *evsel, int ncpus, int nthreads)
 
 int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads)
 {
+	if (evsel->system_wide)
+		nthreads = 1;
+
 	evsel->sample_id = xyarray__new(ncpus, nthreads, sizeof(struct perf_sample_id));
 	if (evsel->sample_id == NULL)
 		return -ENOMEM;
@@ -764,6 +774,9 @@ void perf_evsel__close_fd(struct perf_evsel *evsel, int ncpus, int nthreads)
 {
 	int cpu, thread;
 
+	if (evsel->system_wide)
+		nthreads = 1;
+
 	for (cpu = 0; cpu < ncpus; cpu++)
 		for (thread = 0; thread < nthreads; ++thread) {
 			close(FD(evsel, cpu, thread));
@@ -852,6 +865,9 @@ int __perf_evsel__read(struct perf_evsel *evsel,
 	int cpu, thread;
 	struct perf_counts_values *aggr = &evsel->counts->aggr, count;
 
+	if (evsel->system_wide)
+		nthreads = 1;
+
 	aggr->val = aggr->ena = aggr->run = 0;
 
 	for (cpu = 0; cpu < ncpus; cpu++) {
@@ -974,13 +990,18 @@ static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
 static int __perf_evsel__open(struct perf_evsel *evsel, struct cpu_map *cpus,
 			      struct thread_map *threads)
 {
-	int cpu, thread;
+	int cpu, thread, nthreads;
 	unsigned long flags = 0;
 	int pid = -1, err;
 	enum { NO_CHANGE, SET_TO_MAX, INCREASED_MAX } set_rlimit = NO_CHANGE;
 
+	if (evsel->system_wide)
+		nthreads = 1;
+	else
+		nthreads = threads->nr;
+
 	if (evsel->fd == NULL &&
-	    perf_evsel__alloc_fd(evsel, cpus->nr, threads->nr) < 0)
+	    perf_evsel__alloc_fd(evsel, cpus->nr, nthreads) < 0)
 		return -ENOMEM;
 
 	if (evsel->cgrp) {
@@ -1002,10 +1023,10 @@ retry_sample_id:
 
 	for (cpu = 0; cpu < cpus->nr; cpu++) {
 
-		for (thread = 0; thread < threads->nr; thread++) {
+		for (thread = 0; thread < nthreads; thread++) {
 			int group_fd;
 
-			if (!evsel->cgrp)
+			if (!evsel->cgrp && !evsel->system_wide)
 				pid = threads->map[thread];
 
 			group_fd = get_group_fd(evsel, cpu, thread);
@@ -1075,7 +1096,7 @@ out_close:
 			close(FD(evsel, cpu, thread));
 			FD(evsel, cpu, thread) = -1;
 		}
-		thread = threads->nr;
+		thread = nthreads;
 	} while (--cpu >= 0);
 	return err;
 }
tools/perf/util/evsel.h +1 -0
index de1b36e..7b8795d 100644
@@ -85,6 +85,7 @@ struct perf_evsel {
 	bool 			needs_swap;
 	bool			no_aux_samples;
 	bool			immediate;
+	bool			system_wide;
 	/* parse modifier helper */
 	int			exclude_GH;
 	int			nr_members;
Signature · 1 line
1.8.5.1
#106 lore raw

Re: [PATCH v0 27/71] perf evlist: Add 'system_wide' option

Quoted 6 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option to cause a selected event to be opened always without a pid when configured by perf_evsel__config().

Quoted 9 lines

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch occurs before the task switch and so it cannot record it in a context limited to that task. Note that also means that sched_switch is useless when capturing data per-thread, as is the 'context-switches' software event for the same reason.

This seems like a tailored solution for what is really a generic problem: you need events to have different attributes -- a mix of system wide, task based, with or without callchains and other sample options.

David

reviewer
#107 lore raw

Re: [PATCH v0 27/71] perf evlist: Add 'system_wide' option

Quoted 17 lines · David Ahern
Quoted 6 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an option to cause a selected event to be opened always without a pid when configured by perf_evsel__config().

Quoted 9 lines

This is needed when using the sched_switch tracepoint to follow object code execution. sched_switch occurs before the task switch and so it cannot record it in a context limited to that task. Note that also means that sched_switch is useless when capturing data per-thread, as is the 'context-switches' software event for the same reason.

Quoted 3 lines

This seems like a tailored solution for what is really a generic problem:
you need events to have different attributes -- a mix of system wide, task based, with or without callchains and other sample options.

Actually in this case it is not the attribute but another parameter of the perf_event_open syscall, namely the pid. The effect of that is that there are potentially fewer file descriptors needed for that event i.e. just 1 per cpu compared with 1 per cpu per thread.

This is a generic solution for the case where you want to mix an event that is not tied to a process, with other events that are.

If it were the attribute it would be easy because 'attr' is a member of 'struct evsel' so it can simply be changed directly.

author reviewer
#108 lore raw

PATCH 28/71 perf tools: Add id index

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an index of the event identifiers.

This is needed to queue Instruction
Trace samples according to the mmap
buffer from which they were recorded.

---
 tools/perf/builtin-inject.c |   1 +
 tools/perf/util/event.c     |   1 +
 tools/perf/util/event.h     |  15 ++++++
 tools/perf/util/evlist.c    |  26 ++++++++--
 tools/perf/util/evsel.h     |   3 ++
 tools/perf/util/session.c   | 122 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/session.h   |  10 ++++
 tools/perf/util/tool.h      |   3 +-
 8 files changed, 177 insertions(+), 4 deletions(-)
tools/perf/builtin-inject.c +1 -0
index 6a25085..8400d29 100644
@@ -424,6 +424,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 			.tracing_data	= perf_event__repipe_op2_synth,
 			.finished_round	= perf_event__repipe_op2_synth,
 			.build_id	= perf_event__repipe_op2_synth,
+			.id_index	= perf_event__repipe_op2_synth,
 		},
 		.input_name  = "-",
 		.samples = LIST_HEAD_INIT(inject.samples),
tools/perf/util/event.c +1 -0
index c77814b..30f91d7 100644
@@ -25,6 +25,7 @@ static const char *perf_event__names[] = {
 	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
 	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
 	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
+	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
 };
 
 const char *perf_event__name(unsigned int id)
tools/perf/util/event.h +15 -0
index 30fec99..88e27cb 100644
@@ -153,6 +153,7 @@ enum perf_user_event_type { /* above any possible kernel type */
 	PERF_RECORD_HEADER_TRACING_DATA		= 66,
 	PERF_RECORD_HEADER_BUILD_ID		= 67,
 	PERF_RECORD_FINISHED_ROUND		= 68,
+	PERF_RECORD_ID_INDEX			= 69,
 	PERF_RECORD_HEADER_MAX
 };
 
@@ -179,6 +180,19 @@ struct tracing_data_event {
 	u32 size;
 };
 
+struct id_index_entry {
+	u64 id;
+	u64 idx;
+	u64 cpu;
+	u64 tid;
+};
+
+struct id_index_event {
+	struct perf_event_header header;
+	u64 nr;
+	struct id_index_entry entries[0];
+};
+
 union perf_event {
 	struct perf_event_header	header;
 	struct mmap_event		mmap;
@@ -193,6 +207,7 @@ union perf_event {
 	struct event_type_event		event_type;
 	struct tracing_data_event	tracing_data;
 	struct build_id_event		build_id;
+	struct id_index_event		id_index;
 };
 
 void perf_event__print_totals(void);
tools/perf/util/evlist.c +23 -3
index 3959978..7ae3139 100644
@@ -437,6 +437,22 @@ static int perf_evlist__id_add_fd(struct perf_evlist *evlist,
 	return 0;
 }
 
+static void perf_evlist__set_sid_idx(struct perf_evlist *evlist,
+				     struct perf_evsel *evsel, int idx, int cpu,
+				     int thread)
+{
+	struct perf_sample_id *sid = SID(evsel, cpu, thread);
+	sid->idx = idx;
+	if (evlist->cpus && cpu >= 0)
+		sid->cpu = evlist->cpus->map[cpu];
+	else
+		sid->cpu = -1;
+	if (!evsel->system_wide && evlist->threads && thread >= 0)
+		sid->tid = evlist->threads->map[thread];
+	else
+		sid->tid = -1;
+}
+
 struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id)
 {
 	struct hlist_head *head;
@@ -670,9 +686,13 @@ static int perf_evlist__mmap_per_evsel(struct perf_evlist *evlist, int idx,
 				return -1;
 		}
 
-		if ((evsel->attr.read_format & PERF_FORMAT_ID) &&
-		    perf_evlist__id_add_fd(evlist, evsel, cpu, thread, fd) < 0)
-			return -1;
+		if (evsel->attr.read_format & PERF_FORMAT_ID) {
+			if (perf_evlist__id_add_fd(evlist, evsel, cpu, thread,
+						   fd) < 0)
+				return -1;
+			perf_evlist__set_sid_idx(evlist, evsel, idx, cpu,
+						 thread);
+		}
 	}
 
 	return 0;
tools/perf/util/evsel.h +3 -0
index 7b8795d..3e25e23 100644
@@ -38,6 +38,9 @@ struct perf_sample_id {
 	struct hlist_node 	node;
 	u64		 	id;
 	struct perf_evsel	*evsel;
+	int			idx;
+	int			cpu;
+	pid_t			tid;
 
 	/* Holds total ID period value for PERF_SAMPLE_READ processing. */
 	u64			period;
tools/perf/util/session.c +122 -0

149 lines not on this page · raw message

tools/perf/util/session.h +10 -0
index 69e3bad..60a31db 100644
@@ -128,4 +128,14 @@ int __perf_session__set_tracepoints_handlers(struct perf_session *session,
 extern volatile int session_done;
 
 #define session_done()	(*(volatile int *)(&session_done))
+
+int perf_event__process_id_index(struct perf_tool *tool,
+				 union perf_event *event,
+				 struct perf_session *session);
+
+int perf_event__synthesize_id_index(struct perf_tool *tool,
+				    perf_event__handler_t process,
+				    struct perf_evlist *evlist,
+				    struct machine *machine);
+
 #endif /* __PERF_SESSION_H */
tools/perf/util/tool.h +2 -1
index 4385816..f07d6fe 100644
@@ -39,7 +39,8 @@ struct perf_tool {
 	event_attr_op	attr;
 	event_op2	tracing_data;
 	event_op2	finished_round,
-			build_id;
+			build_id,
+			id_index;
 	bool		ordered_samples;
 	bool		ordering_requires_timestamps;
 };
Signature · 1 line
1.8.5.1
author reviewer
#109 lore raw

PATCH 29/71 perf pmu: Let pmu's with no events show up on perf list

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

perf list only lists PMUs with events. Add a
flag to cause a PMU to be also listed separately.

---
 tools/perf/util/pmu.c | 13 +++++++++++--
 tools/perf/util/pmu.h |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)
tools/perf/util/pmu.c +11 -2
index a742eeb..c6c240f 100644
@@ -733,15 +733,18 @@ void print_pmu_events(const char *event_glob, bool name_only)
 
 	pmu = NULL;
 	len = 0;
-	while ((pmu = perf_pmu__scan(pmu)) != NULL)
+	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
 		list_for_each_entry(alias, &pmu->aliases, list)
 			len++;
+		if (pmu->selectable)
+			len++;
+	}
 	aliases = malloc(sizeof(char *) * len);
 	if (!aliases)
 		return;
 	pmu = NULL;
 	j = 0;
-	while ((pmu = perf_pmu__scan(pmu)) != NULL)
+	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
 		list_for_each_entry(alias, &pmu->aliases, list) {
 			char *name = format_alias(buf, sizeof(buf), pmu, alias);
 			bool is_cpu = !strcmp(pmu->name, "cpu");
@@ -758,6 +761,12 @@ void print_pmu_events(const char *event_glob, bool name_only)
 			aliases[j] = strdup(aliases[j]);
 			j++;
 		}
+		if (pmu->selectable) {
+			scnprintf(buf, sizeof(buf), "%s//", pmu->name);
+			aliases[j] = strdup(buf);
+			j++;
+		}
+	}
 	len = j;
 	qsort(aliases, len, sizeof(char *), cmp_string);
 	for (j = 0; j < len; j++) {
tools/perf/util/pmu.h +1 -0
index 437fdb2..d5266d1 100644
@@ -18,6 +18,7 @@ struct perf_event_attr;
 struct perf_pmu {
 	char *name;
 	__u32 type;
+	bool selectable;
 	struct perf_event_attr *default_config;
 	struct cpu_map *cpus;
 	struct list_head format;
Signature · 1 line
1.8.5.1
author reviewer
#110 lore raw

PATCH 30/71 perf session: Add ability to skip 4GiB or more

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

A session can be made to skip portions of the input file. Do not limit that size to 32-bits.

---
 tools/perf/util/session.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
tools/perf/util/session.c +13 -7
index 81fb4ad..3de0831 100644
@@ -1000,8 +1000,10 @@ static int perf_session_deliver_event(struct perf_session *session,
 	}
 }
 
-static int perf_session__process_user_event(struct perf_session *session, union perf_event *event,
-					    struct perf_tool *tool, u64 file_offset)
+static s64 perf_session__process_user_event(struct perf_session *session,
+					    union perf_event *event,
+					    struct perf_tool *tool,
+					    u64 file_offset)
 {
 	int fd = perf_data_file__fd(session->file);
 	int err;
@@ -1039,7 +1041,7 @@ static void event_swap(union perf_event *event, bool sample_id_all)
 		swap(event, sample_id_all);
 }
 
-static int perf_session__process_event(struct perf_session *session,
+static s64 perf_session__process_event(struct perf_session *session,
 				       union perf_event *event,
 				       struct perf_tool *tool,
 				       u64 file_offset)
@@ -1149,7 +1151,7 @@ static int __perf_session__process_pipe_events(struct perf_session *session,
 	union perf_event *event;
 	uint32_t size, cur_size = 0;
 	void *buf = NULL;
-	int skip = 0;
+	s64 skip = 0;
 	u64 head;
 	ssize_t err;
 	void *p;
@@ -1278,13 +1280,13 @@ int __perf_session__process_events(struct perf_session *session,
 				   u64 file_size, struct perf_tool *tool)
 {
 	int fd = perf_data_file__fd(session->file);
-	u64 head, page_offset, file_offset, file_pos;
+	u64 head, page_offset, file_offset, file_pos, size;
 	int err, mmap_prot, mmap_flags, map_idx = 0;
 	size_t	mmap_size;
 	char *buf, *mmaps[NUM_MMAPS];
 	union perf_event *event;
-	uint32_t size;
 	struct ui_progress prog;
+	s64 skip;
 
 	perf_tool__fill_defaults(tool);
 
@@ -1345,7 +1347,8 @@ more:
 	size = event->header.size;
 
 	if (size < sizeof(struct perf_event_header) ||
-	    perf_session__process_event(session, event, tool, file_pos) < 0) {
+	    (skip = perf_session__process_event(session, event, tool, file_pos))
+									< 0) {
 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
 		       file_offset + head, event->header.size,
 		       event->header.type);
@@ -1353,6 +1356,9 @@ more:
 		goto out_err;
 	}
 
+	if (skip)
+		size += skip;
+
 	head += size;
 	file_pos += size;
 
Signature · 1 line
1.8.5.1
author reviewer
#111 lore raw

PATCH 31/71 perf session: Add perf_session__deliver_synth_event()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to deliver synthesized events from
within a session.

---
 tools/perf/util/session.c | 14 ++++++++++++++
 tools/perf/util/session.h |  5 +++++
 2 files changed, 19 insertions(+)
tools/perf/util/session.c +14 -0
index 3de0831..f2ac351 100644
@@ -1032,6 +1032,20 @@ static s64 perf_session__process_user_event(struct perf_session *session,
 	}
 }
 
+int perf_session__deliver_synth_event(struct perf_session *session,
+				      union perf_event *event,
+				      struct perf_sample *sample,
+				      struct perf_tool *tool)
+{
+	events_stats__inc(&session->stats, event->header.type);
+
+	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
+		return perf_session__process_user_event(session, event, tool,
+							0);
+
+	return perf_session_deliver_event(session, event, sample, tool, 0);
+}
+
 static void event_swap(union perf_event *event, bool sample_id_all)
 {
 	perf_event__swap_op swap;
tools/perf/util/session.h +5 -0
index 60a31db..64d8145 100644
@@ -129,6 +129,11 @@ extern volatile int session_done;
 
 #define session_done()	(*(volatile int *)(&session_done))
 
+int perf_session__deliver_synth_event(struct perf_session *session,
+				      union perf_event *event,
+				      struct perf_sample *sample,
+				      struct perf_tool *tool);
+
 int perf_event__process_id_index(struct perf_tool *tool,
 				 union perf_event *event,
 				 struct perf_session *session);
Signature · 1 line
1.8.5.1
author reviewer
#112 lore raw

PATCH 32/71 perf tools: Allow TSC conversion on any arch

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

It is possible to record a perf.data file on
one architecture and process it on another.
Consequently, TSC conversion functions need
to be moved out of the arch directory.

---
 tools/perf/Makefile.perf            |  2 ++
 tools/perf/arch/x86/util/tsc.c      | 22 +---------------------
 tools/perf/arch/x86/util/tsc.h      |  3 ---
 tools/perf/tests/perf-time-to-tsc.c |  3 +--
 tools/perf/util/tsc.c               | 25 +++++++++++++++++++++++++
 tools/perf/util/tsc.h               | 11 +++++++++++
 6 files changed, 40 insertions(+), 26 deletions(-)
 create mode 100644 tools/perf/util/tsc.c
 create mode 100644 tools/perf/util/tsc.h
tools/perf/Makefile.perf +2 -0
index 9a8cf37..c1f9a54 100644
@@ -292,6 +292,7 @@ LIB_H += util/intlist.h
 LIB_H += util/perf_regs.h
 LIB_H += util/unwind.h
 LIB_H += util/vdso.h
+LIB_H += util/tsc.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -370,6 +371,7 @@ LIB_OBJS += $(OUTPUT)util/stat.o
 LIB_OBJS += $(OUTPUT)util/record.o
 LIB_OBJS += $(OUTPUT)util/srcline.o
 LIB_OBJS += $(OUTPUT)util/data.o
+LIB_OBJS += $(OUTPUT)util/tsc.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/arch/x86/util/tsc.c +1 -21
index b2519e4..831b6ed 100644
@@ -6,29 +6,9 @@
 #include "../../perf.h"
 #include "../../util/types.h"
 #include "../../util/debug.h"
+#include "../../util/tsc.h"
 #include "tsc.h"
 
-u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
-{
-	u64 t, quot, rem;
-
-	t = ns - tc->time_zero;
-	quot = t / tc->time_mult;
-	rem  = t % tc->time_mult;
-	return (quot << tc->time_shift) +
-	       (rem << tc->time_shift) / tc->time_mult;
-}
-
-u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
-{
-	u64 quot, rem;
-
-	quot = cyc >> tc->time_shift;
-	rem  = cyc & ((1 << tc->time_shift) - 1);
-	return tc->time_zero + quot * tc->time_mult +
-	       ((rem * tc->time_mult) >> tc->time_shift);
-}
-
 int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
 			     struct perf_tsc_conversion *tc)
 {
tools/perf/arch/x86/util/tsc.h +0 -3
index a24dec8..18fb762 100644
@@ -14,7 +14,4 @@ struct perf_event_mmap_page;
 int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
 			     struct perf_tsc_conversion *tc);
 
-u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc);
-u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc);
-
 #endif /* TOOLS_PERF_ARCH_X86_UTIL_TSC_H__ */
tools/perf/tests/perf-time-to-tsc.c +1 -2
index 4ca1b93..9ba7d38 100644
@@ -9,10 +9,9 @@
 #include "evsel.h"
 #include "thread_map.h"
 #include "cpumap.h"
+#include "tsc.h"
 #include "tests.h"
 
-#include "../arch/x86/util/tsc.h"
-
 #define CHECK__(x) {				\
 	while ((x) < 0) {			\
 		pr_debug(#x " failed!\n");	\
tools/perf/util/tsc.c new file +25 -0
new file mode 100644
index 0000000..bc69d86
@@ -0,0 +1,25 @@
+#include <linux/compiler.h>
+
+#include "types.h"
+#include "tsc.h"
+
+u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc)
+{
+	u64 t, quot, rem;
+
+	t = ns - tc->time_zero;
+	quot = t / tc->time_mult;
+	rem  = t % tc->time_mult;
+	return (quot << tc->time_shift) +
+	       (rem << tc->time_shift) / tc->time_mult;
+}
+
+u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
+{
+	u64 quot, rem;
+
+	quot = cyc >> tc->time_shift;
+	rem  = cyc & ((1 << tc->time_shift) - 1);
+	return tc->time_zero + quot * tc->time_mult +
+	       ((rem * tc->time_mult) >> tc->time_shift);
+}
tools/perf/util/tsc.h new file +11 -0
new file mode 100644
index 0000000..5083766
@@ -0,0 +1,11 @@
+#ifndef __PERF_TSC_H
+#define __PERF_TSC_H
+
+#include "types.h"
+
+#include "../arch/x86/util/tsc.h"
+
+u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc);
+u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc);
+
+#endif
Signature · 1 line
1.8.5.1
author reviewer
#113 lore raw

PATCH 33/71 perf tools: Move rdtsc() function

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Move the rdtsc() function so it can
be reusued.

---
 tools/perf/arch/x86/util/tsc.c      | 9 +++++++++
 tools/perf/tests/perf-time-to-tsc.c | 9 ---------
 tools/perf/util/tsc.c               | 5 +++++
 tools/perf/util/tsc.h               | 1 +
 4 files changed, 15 insertions(+), 9 deletions(-)
tools/perf/arch/x86/util/tsc.c +9 -0
index 831b6ed..e478f28 100644
@@ -37,3 +37,12 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc,
 
 	return 0;
 }
+
+u64 rdtsc(void)
+{
+	unsigned int low, high;
+
+	asm volatile("rdtsc" : "=a" (low), "=d" (high));
+
+	return low | ((u64)high) << 32;
+}
tools/perf/tests/perf-time-to-tsc.c +0 -9
index 9ba7d38..a5aebf9 100644
@@ -26,15 +26,6 @@
 	}					\
 }
 
-static u64 rdtsc(void)
-{
-	unsigned int low, high;
-
-	asm volatile("rdtsc" : "=a" (low), "=d" (high));
-
-	return low | ((u64)high) << 32;
-}
-
 /**
  * test__perf_time_to_tsc - test converting perf time to TSC.
  *
tools/perf/util/tsc.c +5 -0
index bc69d86..617debb 100644
@@ -23,3 +23,8 @@ u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)
 	return tc->time_zero + quot * tc->time_mult +
 	       ((rem * tc->time_mult) >> tc->time_shift);
 }
+
+u64 __weak rdtsc(void)
+{
+	return 0;
+}
tools/perf/util/tsc.h +1 -0
index 5083766..181f778 100644
@@ -7,5 +7,6 @@
 
 u64 perf_time_to_tsc(u64 ns, struct perf_tsc_conversion *tc);
 u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc);
+u64 rdtsc(void);
 
 #endif
Signature · 1 line
1.8.5.1
author reviewer
#114 lore raw

PATCH 34/71 perf evlist: Add perf_evlist__enable_event_idx()

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a function to enable a specific event
within a specific perf event buffer.

---
 tools/perf/util/evlist.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/evlist.h |  2 ++
 2 files changed, 49 insertions(+)
tools/perf/util/evlist.c +47 -0
index 7ae3139..e750a21 100644
@@ -350,6 +350,53 @@ int perf_evlist__enable_event(struct perf_evlist *evlist,
 	return 0;
 }
 
+static int perf_evlist__enable_event_cpu(struct perf_evlist *evlist,
+					 struct perf_evsel *evsel, int cpu)
+{
+	int thread, err;
+	int nr_threads = perf_evlist__nr_threads(evlist, evsel);
+
+	if (!evsel->fd)
+		return -EINVAL;
+
+	for (thread = 0; thread < nr_threads; thread++) {
+		err = ioctl(FD(evsel, cpu, thread),
+				PERF_EVENT_IOC_ENABLE, 0);
+		if (err)
+			return err;
+	}
+	return 0;
+}
+
+static int perf_evlist__enable_event_thread(struct perf_evlist *evlist,
+					    struct perf_evsel *evsel,
+					    int thread)
+{
+	int cpu, err;
+	int nr_cpus = cpu_map__nr(evlist->cpus);
+
+	if (!evsel->fd)
+		return -EINVAL;
+
+	for (cpu = 0; cpu < nr_cpus; cpu++) {
+		err = ioctl(FD(evsel, cpu, thread), PERF_EVENT_IOC_ENABLE, 0);
+		if (err)
+			return err;
+	}
+	return 0;
+}
+
+int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
+				  struct perf_evsel *evsel, int idx)
+{
+	bool per_cpu_mmaps = !cpu_map__empty(evlist->cpus);
+
+	if (per_cpu_mmaps)
+		return perf_evlist__enable_event_cpu(evlist, evsel, idx);
+	else
+		return perf_evlist__enable_event_thread(evlist, evsel, idx);
+}
+
 static int perf_evlist__alloc_pollfd(struct perf_evlist *evlist)
 {
 	int nr_cpus = cpu_map__nr(evlist->cpus);
tools/perf/util/evlist.h +2 -0
index 2c8d068..f0ce3bf 100644
@@ -122,6 +122,8 @@ int perf_evlist__disable_event(struct perf_evlist *evlist,
 			       struct perf_evsel *evsel);
 int perf_evlist__enable_event(struct perf_evlist *evlist,
 			      struct perf_evsel *evsel);
+int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
+				  struct perf_evsel *evsel, int idx);
 
 void perf_evlist__set_selected(struct perf_evlist *evlist,
 			       struct perf_evsel *evsel);
Signature · 1 line
1.8.5.1
author reviewer
#115 lore raw

PATCH 35/71 perf tools: Add itrace members of struct perf_event_attr

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add new Instruction Tracing members of struct perf_event_attr to debug prints and byte swapping.

---
 tools/perf/util/evsel.c   | 4 ++++
 tools/perf/util/session.c | 4 ++++
 2 files changed, 8 insertions(+)
tools/perf/util/evsel.c +4 -0
index 4e92a22..da2116c 100644
@@ -981,6 +981,10 @@ static size_t perf_event_attr__fprintf(struct perf_event_attr *attr, FILE *fp)
 	ret += PRINT_ATTR_X64(branch_sample_type);
 	ret += PRINT_ATTR_X64(sample_regs_user);
 	ret += PRINT_ATTR_U32(sample_stack_user);
+	ret += PRINT_ATTR_X64(itrace_config);
+	ret += PRINT_ATTR_U32(itrace_watermark);
+	ret += PRINT_ATTR_U32(itrace_sample_type);
+	ret += PRINT_ATTR_U64(itrace_sample_size);
 
 	ret += fprintf(fp, "%.60s\n", graph_dotted_line);
 
tools/perf/util/session.c +4 -0
index f2ac351..7847096 100644
@@ -407,6 +407,10 @@ void perf_event__attr_swap(struct perf_event_attr *attr)
 	attr->branch_sample_type = bswap_64(attr->branch_sample_type);
 	attr->sample_regs_user	 = bswap_64(attr->sample_regs_user);
 	attr->sample_stack_user  = bswap_32(attr->sample_stack_user);
+	attr->itrace_config	 = bswap_64(attr->itrace_config);
+	attr->itrace_watermark	 = bswap_32(attr->itrace_watermark);
+	attr->itrace_sample_type = bswap_32(attr->itrace_sample_type);
+	attr->itrace_sample_size = bswap_64(attr->itrace_sample_size);
 
 	swap_bitfield((u8 *) (&attr->read_format + 1), sizeof(u64));
 }
Signature · 1 line
1.8.5.1
author reviewer
#116 lore raw

PATCH 36/71 perf tools: Add support for parsing pmu itrace_config

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Instruction Tracing uses a new perf_event_attr member named itrace_config. Add support for parsing pmu format values and term types based on itrace_config.

---
 tools/perf/util/parse-events.c | 4 ++++
 tools/perf/util/parse-events.h | 1 +
 tools/perf/util/parse-events.l | 1 +
 tools/perf/util/pmu.c          | 3 +++
 tools/perf/util/pmu.h          | 1 +
 tools/perf/util/pmu.l          | 1 +
 tools/perf/util/pmu.y          | 9 ++++++++-
 7 files changed, 19 insertions(+), 1 deletion(-)
tools/perf/util/parse-events.c +4 -0
index 464dafd..d494a5a 100644
@@ -576,6 +576,10 @@ do {								\
 	case PARSE_EVENTS__TERM_TYPE_NAME:
 		CHECK_TYPE_VAL(STR);
 		break;
+	case PARSE_EVENTS__TERM_TYPE_ITRACE_CONFIG:
+		CHECK_TYPE_VAL(NUM);
+		attr->itrace_config = term->val.num;
+		break;
 	default:
 		return -EINVAL;
 	}
tools/perf/util/parse-events.h +1 -0
index f1cb4c4..86a2721 100644
@@ -49,6 +49,7 @@ enum {
 	PARSE_EVENTS__TERM_TYPE_NAME,
 	PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD,
 	PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE,
+	PARSE_EVENTS__TERM_TYPE_ITRACE_CONFIG,
 };
 
 struct parse_events_term {
tools/perf/util/parse-events.l +1 -0
index 3432995..85106c4 100644
@@ -133,6 +133,7 @@ config2			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_CONFIG2); }
 name			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_NAME); }
 period			{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_SAMPLE_PERIOD); }
 branch_type		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_BRANCH_SAMPLE_TYPE); }
+itrace_config		{ return term(yyscanner, PARSE_EVENTS__TERM_TYPE_ITRACE_CONFIG); }
 ,			{ return ','; }
 "/"			{ BEGIN(INITIAL); return '/'; }
 {name_minus}		{ return str(yyscanner, PE_NAME); }
tools/perf/util/pmu.c +3 -0
index c6c240f..e308dfe 100644
@@ -547,6 +547,9 @@ static int pmu_config_term(struct list_head *formats,
 	case PERF_PMU_FORMAT_VALUE_CONFIG2:
 		vp = &attr->config2;
 		break;
+	case PERF_PMU_FORMAT_VALUE_ITRACE_CONFIG:
+		vp = &attr->itrace_config;
+		break;
 	default:
 		return -EINVAL;
 	}
tools/perf/util/pmu.h +1 -0
index d5266d1..52e4d30 100644
@@ -9,6 +9,7 @@ enum {
 	PERF_PMU_FORMAT_VALUE_CONFIG,
 	PERF_PMU_FORMAT_VALUE_CONFIG1,
 	PERF_PMU_FORMAT_VALUE_CONFIG2,
+	PERF_PMU_FORMAT_VALUE_ITRACE_CONFIG,
 };
 
 #define PERF_PMU_FORMAT_BITS 64
tools/perf/util/pmu.l +1 -0
index a15d9fb..c94ee8cd 100644
@@ -29,6 +29,7 @@ num_dec         [0-9]+
 config		{ return PP_CONFIG; }
 config1		{ return PP_CONFIG1; }
 config2		{ return PP_CONFIG2; }
+itrace_config	{ return PP_ITRACE_CONFIG; }
 -		{ return '-'; }
 :		{ return ':'; }
 ,		{ return ','; }
tools/perf/util/pmu.y +8 -1
index bfd7e85..caa190e 100644
@@ -20,7 +20,7 @@ do { \
 
 %}
 
-%token PP_CONFIG PP_CONFIG1 PP_CONFIG2
+%token PP_CONFIG PP_CONFIG1 PP_CONFIG2 PP_ITRACE_CONFIG
 %token PP_VALUE PP_ERROR
 %type <num> PP_VALUE
 %type <bits> bit_term
@@ -60,6 +60,13 @@ PP_CONFIG2 ':' bits
 				      PERF_PMU_FORMAT_VALUE_CONFIG2,
 				      $3));
 }
+|
+PP_ITRACE_CONFIG ':' bits
+{
+	ABORT_ON(perf_pmu__new_format(format, name,
+				      PERF_PMU_FORMAT_VALUE_ITRACE_CONFIG,
+				      $3));
+}
 
 bits:
 bits ',' bit_term
Signature · 1 line
1.8.5.1
author reviewer
#117 lore raw

PATCH 37/71 perf tools: Add support for PERF_RECORD_ITRACE_LOST

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Instruction Tracing may lose data, for example when buffers are full. In that case, an event of type PERF_RECORD_ITRACE_LOST is created to inform of the loss. Add support for that event type.

---
 tools/perf/builtin-inject.c |  1 +
 tools/perf/util/event.c     | 17 +++++++++++++++++
 tools/perf/util/event.h     | 11 +++++++++++
 tools/perf/util/machine.c   | 10 ++++++++++
 tools/perf/util/machine.h   |  2 ++
 tools/perf/util/session.c   |  5 +++++
 tools/perf/util/tool.h      |  1 +
 7 files changed, 47 insertions(+)
tools/perf/builtin-inject.c +1 -0
index 8400d29..78911a3 100644
@@ -417,6 +417,7 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 			.fork		= perf_event__repipe,
 			.exit		= perf_event__repipe,
 			.lost		= perf_event__repipe,
+			.itrace_lost	= perf_event__repipe,
 			.read		= perf_event__repipe_sample,
 			.throttle	= perf_event__repipe,
 			.unthrottle	= perf_event__repipe,
tools/perf/util/event.c +17 -0
index 30f91d7..4d001d9 100644
@@ -20,6 +20,7 @@ static const char *perf_event__names[] = {
 	[PERF_RECORD_FORK]			= "FORK",
 	[PERF_RECORD_READ]			= "READ",
 	[PERF_RECORD_SAMPLE]			= "SAMPLE",
+	[PERF_RECORD_ITRACE_LOST]		= "ITRACE_LOST",
 	[PERF_RECORD_HEADER_ATTR]		= "ATTR",
 	[PERF_RECORD_HEADER_EVENT_TYPE]		= "EVENT_TYPE",
 	[PERF_RECORD_HEADER_TRACING_DATA]	= "TRACING_DATA",
@@ -536,6 +537,14 @@ int perf_event__process_lost(struct perf_tool *tool __maybe_unused,
 	return machine__process_lost_event(machine, event, sample);
 }
 
+int perf_event__process_itrace_lost(struct perf_tool *tool __maybe_unused,
+				    union perf_event *event,
+				    struct perf_sample *sample __maybe_unused,
+				    struct machine *machine)
+{
+	return machine__process_itrace_lost_event(machine, event);
+}
+
 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp)
 {
 	return fprintf(fp, " %d/%d: [%#" PRIx64 "(%#" PRIx64 ") @ %#" PRIx64 "]: %c %s\n",
@@ -596,6 +605,11 @@ int perf_event__process_exit(struct perf_tool *tool __maybe_unused,
 	return machine__process_exit_event(machine, event, sample);
 }
 
+size_t perf_event__fprintf_itrace_lost(union perf_event *event, FILE *fp)
+{
+	return fprintf(fp, " offset: %#"PRIx64"\n", event->itrace_lost.offset);
+}
+
 size_t perf_event__fprintf(union perf_event *event, FILE *fp)
 {
 	size_t ret = fprintf(fp, "PERF_RECORD_%s",
@@ -615,6 +629,9 @@ size_t perf_event__fprintf(union perf_event *event, FILE *fp)
 	case PERF_RECORD_MMAP2:
 		ret += perf_event__fprintf_mmap2(event, fp);
 		break;
+	case PERF_RECORD_ITRACE_LOST:
+		ret += perf_event__fprintf_itrace_lost(event, fp);
+		break;
 	default:
 		ret += fprintf(fp, "\n");
 	}
tools/perf/util/event.h +11 -0
index 88e27cb..b684398 100644
@@ -193,6 +193,11 @@ struct id_index_event {
 	struct id_index_entry entries[0];
 };
 
+struct itrace_lost_event {
+	struct perf_event_header header;
+	u64 offset;
+};
+
 union perf_event {
 	struct perf_event_header	header;
 	struct mmap_event		mmap;
@@ -208,6 +213,7 @@ union perf_event {
 	struct tracing_data_event	tracing_data;
 	struct build_id_event		build_id;
 	struct id_index_event		id_index;
+	struct itrace_lost_event	itrace_lost;
 };
 
 void perf_event__print_totals(void);
@@ -244,6 +250,10 @@ int perf_event__process_lost(struct perf_tool *tool,
 			     union perf_event *event,
 			     struct perf_sample *sample,
 			     struct machine *machine);
+int perf_event__process_itrace_lost(struct perf_tool *tool,
+				    union perf_event *event,
+				    struct perf_sample *sample,
+				    struct machine *machine);
 int perf_event__process_mmap(struct perf_tool *tool,
 			     union perf_event *event,
 			     struct perf_sample *sample,
@@ -285,6 +295,7 @@ size_t perf_event__fprintf_comm(union perf_event *event, FILE *fp);
 size_t perf_event__fprintf_mmap(union perf_event *event, FILE *fp);
 size_t perf_event__fprintf_mmap2(union perf_event *event, FILE *fp);
 size_t perf_event__fprintf_task(union perf_event *event, FILE *fp);
+size_t perf_event__fprintf_itrace_lost(union perf_event *event, FILE *fp);
 size_t perf_event__fprintf(union perf_event *event, FILE *fp);
 
 #endif /* __PERF_RECORD_H */
tools/perf/util/machine.c +10 -0
index a04210d..a224cf7 100644
@@ -361,6 +361,14 @@ int machine__process_lost_event(struct machine *machine __maybe_unused,
 	return 0;
 }
 
+int machine__process_itrace_lost_event(struct machine *machine __maybe_unused,
+				       union perf_event *event)
+{
+	if (dump_trace)
+		perf_event__fprintf_itrace_lost(event, stdout);
+	return 0;
+}
+
 struct map *machine__new_module(struct machine *machine, u64 start,
 				const char *filename)
 {
@@ -1149,6 +1157,8 @@ int machine__process_event(struct machine *machine, union perf_event *event,
 		ret = machine__process_exit_event(machine, event, sample); break;
 	case PERF_RECORD_LOST:
 		ret = machine__process_lost_event(machine, event, sample); break;
+	case PERF_RECORD_ITRACE_LOST:
+		ret = machine__process_itrace_lost_event(machine, event); break;
 	default:
 		ret = -1;
 		break;
tools/perf/util/machine.h +2 -0
index aaad99a..fee1f89 100644
@@ -50,6 +50,8 @@ int machine__process_fork_event(struct machine *machine, union perf_event *event
 				struct perf_sample *sample);
 int machine__process_lost_event(struct machine *machine, union perf_event *event,
 				struct perf_sample *sample);
+int machine__process_itrace_lost_event(struct machine *machine,
+				       union perf_event *event);
 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
 				struct perf_sample *sample);
 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
tools/perf/util/session.c +5 -0
index 7847096..de09c2e 100644
@@ -236,6 +236,8 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
 		tool->exit = process_event_stub;
 	if (tool->lost == NULL)
 		tool->lost = perf_event__process_lost;
+	if (tool->itrace_lost == NULL)
+		tool->itrace_lost = perf_event__process_itrace_lost;
 	if (tool->read == NULL)
 		tool->read = process_event_sample_stub;
 	if (tool->throttle == NULL)
@@ -454,6 +456,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
 	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
 	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
+	[PERF_RECORD_ITRACE_LOST]	  = perf_event__all64_swap,
 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
@@ -998,6 +1001,8 @@ static int perf_session_deliver_event(struct perf_session *session,
 		return tool->throttle(tool, event, sample, machine);
 	case PERF_RECORD_UNTHROTTLE:
 		return tool->unthrottle(tool, event, sample, machine);
+	case PERF_RECORD_ITRACE_LOST:
+		return tool->itrace_lost(tool, event, sample, machine);
 	default:
 		++session->stats.nr_unknown_events;
 		return -1;
tools/perf/util/tool.h +1 -0
index f07d6fe..18afd13 100644
@@ -34,6 +34,7 @@ struct perf_tool {
 			fork,
 			exit,
 			lost,
+			itrace_lost,
 			throttle,
 			unthrottle;
 	event_attr_op	attr;
Signature · 1 line
1.8.5.1
author reviewer
#118 lore raw

PATCH 38/71 perf tools: Add itrace sample parsing

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add supoprt for parsing samples containing
Instruction Traces i.e. PERF_SAMPLE_ITRACE.

---
 tools/perf/tests/sample-parsing.c |  7 ++++++-
 tools/perf/util/event.h           |  6 ++++++
 tools/perf/util/evlist.c          | 12 ++++++++++++
 tools/perf/util/evlist.h          |  4 ++++
 tools/perf/util/evsel.c           | 33 +++++++++++++++++++++++++++++++--
 tools/perf/util/evsel.h           | 13 +++++++++++--
 tools/perf/util/session.c         |  3 ++-
 7 files changed, 72 insertions(+), 6 deletions(-)
tools/perf/tests/sample-parsing.c +6 -1
index 1b67720..acc8132 100644
@@ -155,6 +155,7 @@ static int do_test(u64 sample_type, u64 sample_regs_user, u64 read_format)
 	u64 user_regs[64];
 	const u64 raw_data[] = {0x123456780a0b0c0dULL, 0x1102030405060708ULL};
 	const u64 data[] = {0x2211443366558877ULL, 0, 0xaabbccddeeff4321ULL};
+	const u64 itrace_data[] = {0xa55a, 0, 0xeeddee, 0x0282028202820282};
 	struct perf_sample sample = {
 		.ip		= 101,
 		.pid		= 102,
@@ -184,6 +185,10 @@ static int do_test(u64 sample_type, u64 sample_regs_user, u64 read_format)
 			.time_enabled = 0x030a59d664fca7deULL,
 			.time_running = 0x011b6ae553eb98edULL,
 		},
+		.itrace_sample	= {
+			.size	= sizeof(itrace_data),
+			.data	= (void *)itrace_data,
+		},
 	};
 	struct sample_read_value values[] = {{1, 5}, {9, 3}, {2, 7}, {6, 4},};
 	struct perf_sample sample_out;
@@ -280,7 +285,7 @@ int test__sample_parsing(void)
 	 * were added.  Please actually update the test rather than just change
 	 * the condition below.
 	 */
-	if (PERF_SAMPLE_MAX > PERF_SAMPLE_TRANSACTION << 1) {
+	if (PERF_SAMPLE_MAX > PERF_SAMPLE_ITRACE << 1) {
 		pr_debug("sample format has changed, some new PERF_SAMPLE_ bit was introduced - test needs updating\n");
 		return -1;
 	}
tools/perf/util/event.h +6 -0
index b684398..cc49148 100644
@@ -111,6 +111,11 @@ struct sample_read {
 	};
 };
 
+struct itrace_sample {
+	u64 size;
+	void *data;
+};
+
 struct perf_sample {
 	u64 ip;
 	u32 pid, tid;
@@ -130,6 +135,7 @@ struct perf_sample {
 	struct regs_dump  user_regs;
 	struct stack_dump user_stack;
 	struct sample_read read;
+	struct itrace_sample itrace_sample;
 };
 
 #define PERF_MEM_DATA_SRC_NONE \
tools/perf/util/evlist.c +12 -0
index e750a21..490af37 100644
@@ -1238,6 +1238,18 @@ int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *even
 	return perf_evsel__parse_sample(evsel, event, sample);
 }
 
+int __perf_evlist__parse_sample(struct perf_evlist *evlist,
+				union perf_event *event,
+				struct perf_sample *sample,
+				bool fix_swap)
+{
+	struct perf_evsel *evsel = perf_evlist__event2evsel(evlist, event);
+
+	if (!evsel)
+		return -EFAULT;
+	return __perf_evsel__parse_sample(evsel, event, sample, fix_swap);
+}
+
 size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp)
 {
 	struct perf_evsel *evsel;
tools/perf/util/evlist.h +4 -0
index f0ce3bf..6f3166e 100644
@@ -151,6 +151,10 @@ u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist);
 
 int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
 			      struct perf_sample *sample);
+int __perf_evlist__parse_sample(struct perf_evlist *evlist,
+				union perf_event *event,
+				struct perf_sample *sample,
+				bool fix_swap);
 
 bool perf_evlist__valid_sample_type(struct perf_evlist *evlist);
 bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist);
tools/perf/util/evsel.c +31 -2
index da2116c..88b7edd 100644
@@ -1233,8 +1233,10 @@ static inline bool overflow(const void *endp, u16 max_size, const void *offset,
 #define OVERFLOW_CHECK_u64(offset) \
 	OVERFLOW_CHECK(offset, sizeof(u64), sizeof(u64))
 
-int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
-			     struct perf_sample *data)
+int __perf_evsel__parse_sample(struct perf_evsel *evsel,
+			       union perf_event *event,
+			       struct perf_sample *data,
+			       bool fix_swap)
 {
 	u64 type = evsel->attr.sample_type;
 	bool swapped = evsel->needs_swap;
@@ -1479,6 +1481,19 @@ int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
 		array++;
 	}
 
+	if (type & PERF_SAMPLE_ITRACE) {
+		OVERFLOW_CHECK_u64(array);
+		sz = *array++;
+
+		OVERFLOW_CHECK(array, sz, max_size);
+		/* Undo swap of data */
+		if (fix_swap && swapped)
+			mem_bswap_64((char *)array, sz);
+		data->itrace_sample.size = sz;
+		data->itrace_sample.data = (char *)array;
+		array = (void *)array + sz;
+	}
+
 	return 0;
 }
 
@@ -1574,6 +1589,11 @@ size_t perf_event__sample_event_size(const struct perf_sample *sample, u64 type,
 	if (type & PERF_SAMPLE_TRANSACTION)
 		result += sizeof(u64);
 
+	if (type & PERF_SAMPLE_ITRACE) {
+		result += sizeof(u64);
+		result += sample->itrace_sample.size;
+	}
+
 	return result;
 }
 
@@ -1752,6 +1772,15 @@ int perf_event__synthesize_sample(union perf_event *event, u64 type,
 		array++;
 	}
 
+	if (type & PERF_SAMPLE_ITRACE) {
+		sz = sample->itrace_sample.size;
+		*array++ = sz;
+		memcpy(array, sample->itrace_sample.data, sz);
+		if (swapped)
+			mem_bswap_64((char *)array, sz);
+		array = (void *)array + sz;
+	}
+
 	return 0;
 }
 
tools/perf/util/evsel.h +11 -2
index 3e25e23..b0433fd 100644
@@ -280,8 +280,17 @@ static inline int perf_evsel__read_scaled(struct perf_evsel *evsel,
 
 void hists__init(struct hists *hists);
 
-int perf_evsel__parse_sample(struct perf_evsel *evsel, union perf_event *event,
-			     struct perf_sample *sample);
+int __perf_evsel__parse_sample(struct perf_evsel *evsel,
+			       union perf_event *event,
+			       struct perf_sample *data,
+			       bool fix_swap);
+
+static inline int perf_evsel__parse_sample(struct perf_evsel *evsel,
+					   union perf_event *event,
+					   struct perf_sample *data)
+{
+	return __perf_evsel__parse_sample(evsel, event, data, false);
+}
 
 static inline struct perf_evsel *perf_evsel__next(struct perf_evsel *evsel)
 {
tools/perf/util/session.c +2 -1
index de09c2e..fbf9024 100644
@@ -1086,7 +1086,8 @@ static s64 perf_session__process_event(struct perf_session *session,
 	/*
 	 * For all kernel events we get the sample data
 	 */
-	ret = perf_evlist__parse_sample(session->evlist, event, &sample);
+	ret = __perf_evlist__parse_sample(session->evlist, event, &sample,
+					  true);
 	if (ret)
 		return ret;
 
Signature · 1 line
1.8.5.1
author reviewer
#119 lore raw

PATCH 39/71 perf header: Add Instruction Tracing feature

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a feature to indicate that a perf.data file
contains Instruction Tracing data.

---
 tools/perf/util/header.c | 14 ++++++++++++++
 tools/perf/util/header.h |  1 +
 2 files changed, 15 insertions(+)
tools/perf/util/header.c +14 -0
index 0114f0a..72bcca9 100644
@@ -1188,6 +1188,13 @@ static int write_branch_stack(int fd __maybe_unused,
 	return 0;
 }
 
+static int write_itrace(int fd __maybe_unused,
+			struct perf_header *h __maybe_unused,
+			struct perf_evlist *evlist __maybe_unused)
+{
+	return 0;
+}
+
 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
 			   FILE *fp)
 {
@@ -1485,6 +1492,12 @@ static void print_branch_stack(struct perf_header *ph __maybe_unused,
 	fprintf(fp, "# contains samples with branch stack\n");
 }
 
+static void print_itrace(struct perf_header *ph __maybe_unused,
+			 int fd __maybe_unused, FILE *fp)
+{
+	fprintf(fp, "# contains Instruction Traces\n");
+}
+
 static void print_pmu_mappings(struct perf_header *ph, int fd __maybe_unused,
 			       FILE *fp)
 {
@@ -2195,6 +2208,7 @@ static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
 	FEAT_OPA(HEADER_BRANCH_STACK,	branch_stack),
 	FEAT_OPP(HEADER_PMU_MAPPINGS,	pmu_mappings),
 	FEAT_OPP(HEADER_GROUP_DESC,	group_desc),
+	FEAT_OPA(HEADER_ITRACE,		itrace),
 };
 
 struct header_print_data {
tools/perf/util/header.h +1 -0
index e8c45fa..be60483 100644
@@ -30,6 +30,7 @@ enum {
 	HEADER_BRANCH_STACK,
 	HEADER_PMU_MAPPINGS,
 	HEADER_GROUP_DESC,
+	HEADER_ITRACE,
 	HEADER_LAST_FEATURE,
 	HEADER_FEAT_BITS	= 256,
 };
Signature · 1 line
1.8.5.1
author reviewer
#120 lore raw

PATCH 40/71 perf evlist: Add ability to mmap itrace buffers

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Instruction Tracing data is provided in a separate mmap made a special offset PERF_EVENT_ITRACE_OFFSET. Add the ability to mmap that offset.

---
 tools/perf/Makefile.perf |   2 +
 tools/perf/util/evlist.c |  57 +++++++++++++++++++++--
 tools/perf/util/evlist.h |   5 ++
 tools/perf/util/itrace.c | 104 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h | 118 +++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 283 insertions(+), 3 deletions(-)
 create mode 100644 tools/perf/util/itrace.c
 create mode 100644 tools/perf/util/itrace.h
tools/perf/Makefile.perf +2 -0
index c1f9a54..6ef50f9 100644
@@ -293,6 +293,7 @@ LIB_H += util/perf_regs.h
 LIB_H += util/unwind.h
 LIB_H += util/vdso.h
 LIB_H += util/tsc.h
+LIB_H += util/itrace.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -372,6 +373,7 @@ LIB_OBJS += $(OUTPUT)util/record.o
 LIB_OBJS += $(OUTPUT)util/srcline.o
 LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
+LIB_OBJS += $(OUTPUT)util/itrace.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/evlist.c +54 -3

121 lines not on this page · raw message

tools/perf/util/evlist.h +5 -0
index 6f3166e..7f56fdc 100644
@@ -7,6 +7,7 @@
 #include "event.h"
 #include "evsel.h"
 #include "util.h"
+#include "itrace.h"
 #include <unistd.h>
 
 struct pollfd;
@@ -21,6 +22,7 @@ struct perf_mmap {
 	void		 *base;
 	int		 mask;
 	unsigned int	 prev;
+	struct itrace_mmap itrace_mmap;
 	char		 event_copy[PERF_SAMPLE_MAX_SIZE];
 };
 
@@ -111,6 +113,9 @@ int perf_evlist__parse_mmap_pages(const struct option *opt,
 				  const char *str,
 				  int unset);
 
+int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
+			 bool overwrite, unsigned int itrace_pages,
+			 bool itrace_overwrite);
 int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
 		      bool overwrite);
 void perf_evlist__munmap(struct perf_evlist *evlist);
tools/perf/util/itrace.c new file +104 -0

104 lines not on this page · raw message

tools/perf/util/itrace.h new file +118 -0

118 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#121 lore raw

PATCH 41/71 perf tools: Add user events for Instruction Tracing

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add two user events for Instruction Tracing.

PERF_RECORD_ITRACE_INFO contains metadata, consisting primarily the type of the Instruction Tracing data plus some amount of architecture-specific information. There should be only one PERF_RECORD_ITRACE_INFO event.

PERF_RECORD_ITRACE identifies Instruction Tracing data copied from the mmapped Instruction Tracing region. The actual data is not part of the event but immediately follows it.

---
 tools/perf/util/event.c   |  2 ++
 tools/perf/util/event.h   | 22 +++++++++++++++
 tools/perf/util/session.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/tool.h    |  9 ++++++-
 4 files changed, 101 insertions(+), 1 deletion(-)
tools/perf/util/event.c +2 -0
index 4d001d9..990d10b 100644
@@ -27,6 +27,8 @@ static const char *perf_event__names[] = {
 	[PERF_RECORD_HEADER_BUILD_ID]		= "BUILD_ID",
 	[PERF_RECORD_FINISHED_ROUND]		= "FINISHED_ROUND",
 	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
+	[PERF_RECORD_ITRACE_INFO]		= "ITRACE_INFO",
+	[PERF_RECORD_ITRACE]			= "ITRACE",
 };
 
 const char *perf_event__name(unsigned int id)
tools/perf/util/event.h +22 -0
index cc49148..ad3625c 100644
@@ -160,6 +160,8 @@ enum perf_user_event_type { /* above any possible kernel type */
 	PERF_RECORD_HEADER_BUILD_ID		= 67,
 	PERF_RECORD_FINISHED_ROUND		= 68,
 	PERF_RECORD_ID_INDEX			= 69,
+	PERF_RECORD_ITRACE_INFO			= 70,
+	PERF_RECORD_ITRACE			= 71,
 	PERF_RECORD_HEADER_MAX
 };
 
@@ -199,6 +201,24 @@ struct id_index_event {
 	struct id_index_entry entries[0];
 };
 
+struct itrace_info_event {
+	struct perf_event_header header;
+	u32 type;
+	u32 reserved__; /* For alignment */
+	u64 priv[];
+};
+
+struct itrace_event {
+	struct perf_event_header header;
+	u64 size;
+	u64 offset;
+	u64 reference;
+	u32 idx;
+	u32 tid;
+	u32 cpu;
+	u32 reserved__; /* For alignment */
+};
+
 struct itrace_lost_event {
 	struct perf_event_header header;
 	u64 offset;
@@ -220,6 +240,8 @@ union perf_event {
 	struct build_id_event		build_id;
 	struct id_index_event		id_index;
 	struct itrace_lost_event	itrace_lost;
+	struct itrace_info_event	itrace_info;
+	struct itrace_event		itrace;
 };
 
 void perf_event__print_totals(void);
tools/perf/util/session.c +69 -0

99 lines not on this page · raw message

tools/perf/util/tool.h +8 -1
index 18afd13..c1e8744 100644
@@ -3,6 +3,8 @@
 
 #include <stdbool.h>
 
+#include "types.h"
+
 struct perf_session;
 union perf_event;
 struct perf_evlist;
@@ -25,6 +27,9 @@ typedef int (*event_attr_op)(struct perf_tool *tool,
 typedef int (*event_op2)(struct perf_tool *tool, union perf_event *event,
 			 struct perf_session *session);
 
+typedef s64 (*event_op3)(struct perf_tool *tool, union perf_event *event,
+			 struct perf_session *session);
+
 struct perf_tool {
 	event_sample	sample,
 			read;
@@ -41,7 +46,9 @@ struct perf_tool {
 	event_op2	tracing_data;
 	event_op2	finished_round,
 			build_id,
-			id_index;
+			id_index,
+			itrace_info;
+	event_op3	itrace;
 	bool		ordered_samples;
 	bool		ordering_requires_timestamps;
 };
Signature · 1 line
1.8.5.1
author reviewer
#122 lore raw

PATCH 42/71 perf tools: Add support for Instruction Trace recording

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for reading from the Instruction
Tracing mmap and synthesizing Instruction
Tracing events.

---
 tools/perf/perf.h        |   2 +
 tools/perf/util/itrace.c | 190 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h |  51 ++++++++++++-
 tools/perf/util/record.c |   6 +-
 4 files changed, 247 insertions(+), 2 deletions(-)
tools/perf/perf.h +2 -0
index b23fed5..b68b469 100644
@@ -261,8 +261,10 @@ struct perf_record_opts {
 	bool	     sample_weight;
 	bool	     sample_time;
 	bool	     period;
+	bool	     full_itrace;
 	unsigned int freq;
 	unsigned int mmap_pages;
+	unsigned int itrace_mmap_pages;
 	unsigned int user_freq;
 	u64          branch_stack;
 	u64	     default_interval;
tools/perf/util/itrace.c +190 -0

205 lines not on this page · raw message

tools/perf/util/itrace.h +50 -1

73 lines not on this page · raw message

tools/perf/util/record.c +5 -1
index e510453..86f980e 100644
@@ -93,7 +93,11 @@ void perf_evlist__config(struct perf_evlist *evlist,
 	list_for_each_entry(evsel, &evlist->entries, node)
 		perf_evsel__config(evsel, opts);
 
-	if (evlist->nr_entries > 1) {
+	if (opts->full_itrace) {
+		use_sample_identifier = true;
+		list_for_each_entry(evsel, &evlist->entries, node)
+			perf_evsel__set_sample_id(evsel, use_sample_identifier);
+	} else if (evlist->nr_entries > 1) {
 		struct perf_evsel *first = perf_evlist__first(evlist);
 
 		list_for_each_entry(evsel, &evlist->entries, node) {
Signature · 1 line
1.8.5.1
author reviewer
#123 lore raw

PATCH 43/71 perf record: Add basic Instruction Tracing support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Amend the perf record tool to read the
Instruction Tracing mmap and synthesize
Instruction Tracing events.

---
 tools/perf/builtin-record.c | 103 +++++++++++++++++++++++++++++++++++++-------
 1 file changed, 87 insertions(+), 16 deletions(-)
tools/perf/builtin-record.c +87 -16
index d93e2ee..4613f55 100644
@@ -25,6 +25,7 @@
 #include "util/cpumap.h"
 #include "util/thread_map.h"
 #include "util/data.h"
+#include "util/itrace.h"
 
 #include <unistd.h>
 #include <sched.h>
@@ -67,6 +68,7 @@ struct perf_record {
 	struct perf_record_opts	opts;
 	u64			bytes_written;
 	struct perf_data_file	file;
+	struct itrace_record	*itr;
 	struct perf_evlist	*evlist;
 	struct perf_session	*session;
 	const char		*progname;
@@ -150,6 +152,42 @@ out:
 	return rc;
 }
 
+static int perf_record__process_itrace(struct perf_tool *tool,
+				       union perf_event *event, void *data1,
+				       size_t len1, void *data2, size_t len2)
+{
+	struct perf_record *rec = container_of(tool, struct perf_record, tool);
+	size_t padding;
+	u8 pad[8] = {0};
+
+	padding = (len1 + len2) & 7;
+	if (padding)
+		padding = 8 - padding;
+
+	perf_record__write(rec, event, event->header.size);
+	perf_record__write(rec, data1, len1);
+	perf_record__write(rec, data2, len2);
+	perf_record__write(rec, &pad, padding);
+
+	return 0;
+}
+
+static int perf_record__itrace_mmap_read(struct perf_record *rec,
+					 struct itrace_mmap *mm)
+{
+	int ret;
+
+	ret = itrace_mmap__read(mm, rec->itr, &rec->tool,
+				perf_record__process_itrace);
+	if (ret < 0)
+		return ret;
+
+	if (ret)
+		rec->samples++;
+
+	return 0;
+}
+
 static volatile int done = 0;
 static volatile int signr = -1;
 static volatile int child_finished = 0;
@@ -218,13 +256,16 @@ try_again:
 		goto out;
 	}
 
-	if (perf_evlist__mmap(evlist, opts->mmap_pages, false) < 0) {
+	if (perf_evlist__mmap_ex(evlist, opts->mmap_pages, false,
+				 opts->itrace_mmap_pages,
+				 false) < 0) {
 		if (errno == EPERM) {
 			pr_err("Permission error mapping pages.\n"
 			       "Consider increasing "
 			       "/proc/sys/kernel/perf_event_mlock_kb,\n"
 			       "or try again with a smaller value of -m/--mmap_pages.\n"
-			       "(current value: %d)\n", opts->mmap_pages);
+			       "(current value: %u,%u)\n",
+			       opts->mmap_pages, opts->itrace_mmap_pages);
 			rc = -errno;
 		} else {
 			pr_err("failed to mmap with %d (%s)\n", errno, strerror(errno));
@@ -318,12 +359,20 @@ static int perf_record__mmap_read_all(struct perf_record *rec)
 	int rc = 0;
 
 	for (i = 0; i < rec->evlist->nr_mmaps; i++) {
+		struct itrace_mmap *mm = &rec->evlist->mmap[i].itrace_mmap;
+
 		if (rec->evlist->mmap[i].base) {
 			if (perf_record__mmap_read(rec, &rec->evlist->mmap[i]) != 0) {
 				rc = -1;
 				goto out;
 			}
 		}
+
+		if (mm->base &&
+		    perf_record__itrace_mmap_read(rec, mm) != 0) {
+			rc = -1;
+			goto out;
+		}
 	}
 
 	if (perf_header__has_feat(&rec->session->header, HEADER_TRACING_DATA))
@@ -351,6 +400,9 @@ static void perf_record__init_features(struct perf_record *rec)
 
 	if (!rec->opts.branch_stack)
 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
+
+	if (!rec->opts.full_itrace)
+		perf_header__clear_feat(&session->header, HEADER_ITRACE);
 }
 
 static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
@@ -455,6 +507,13 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 		}
 	}
 
+	if (rec->opts.full_itrace) {
+		err = perf_event__synthesize_itrace_info(rec->itr, tool,
+					session, process_synthesized_event);
+		if (err)
+			goto out_delete_session;
+	}
+
 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
 						 machine, "_text");
 	if (err < 0)
@@ -536,16 +595,17 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 	if (quiet || signr == SIGUSR1)
 		return 0;
 
-	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n", waking);
-
-	/*
-	 * Approximate RIP event size: 24 bytes.
-	 */
-	fprintf(stderr,
-		"[ perf record: Captured and wrote %.3f MB %s (~%" PRIu64 " samples) ]\n",
-		(double)rec->bytes_written / 1024.0 / 1024.0,
-		file->path,
-		rec->bytes_written / 24);
+	fprintf(stderr, "[ perf record: Woken up %ld times to write data ]\n",
+		waking);
+	fprintf(stderr, "[ perf record: Captured and wrote %.3f MB %s",
+		(double)rec->bytes_written / 1024.0 / 1024.0, file->path);
+	if (rec->opts.full_itrace) {
+		fprintf(stderr, " ]\n");
+	} else {
+		/* Approximate RIP event size: 24 bytes */
+		fprintf(stderr, " (~%" PRIu64 " samples) ]\n",
+			rec->bytes_written / 24);
+	}
 
 	return 0;
 
@@ -889,14 +949,19 @@ const struct option record_options[] = {
 
 int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 {
-	int err = -ENOMEM;
+	int err;
 	struct perf_evlist *evsel_list;
 	struct perf_record *rec = &record;
 	char errbuf[BUFSIZ];
 
+	rec->itr = itrace_record__init(&err);
+	if (err)
+		return err;
+
+	err = -ENOMEM;
 	evsel_list = perf_evlist__new();
 	if (evsel_list == NULL)
-		return -ENOMEM;
+		goto out_itrace_free;
 
 	rec->evlist = evsel_list;
 
@@ -956,18 +1021,24 @@ int cmd_record(int argc, const char **argv, const char *prefix __maybe_unused)
 	if (perf_evlist__create_maps(evsel_list, &rec->opts.target) < 0)
 		usage_with_options(record_usage, record_options);
 
+	err = itrace_record__options(rec->itr, evsel_list, &rec->opts);
+	if (err)
+		goto out_symbol_exit;
+
 	if (perf_record_opts__config(&rec->opts)) {
 		err = -EINVAL;
-		goto out_free_fd;
+		goto out_delete_maps;
 	}
 
 	err = __cmd_record(&record, argc, argv);
 
 	perf_evlist__munmap(evsel_list);
 	perf_evlist__close(evsel_list);
-out_free_fd:
+out_delete_maps:
 	perf_evlist__delete_maps(evsel_list);
 out_symbol_exit:
 	symbol__exit();
+out_itrace_free:
+	itrace_record__free(rec->itr);
 	return err;
 }
Signature · 1 line
1.8.5.1
author reviewer
#124 lore raw

PATCH 44/71 perf record: Extend -m option for Instruction Tracing mmap pages

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Extend the -m option so that the number
of mmap pages for Instruction Tracing
can be specified.

---
 tools/perf/Documentation/perf-record.txt |  2 ++
 tools/perf/builtin-record.c              | 49 ++++++++++++++++++++++++++++++--
 tools/perf/util/evlist.c                 | 16 +++++++----
 tools/perf/util/evlist.h                 |  2 ++
 4 files changed, 60 insertions(+), 9 deletions(-)
tools/perf/Documentation/perf-record.txt +2 -0
index c407897..bb01df7 100644
@@ -92,6 +92,8 @@ OPTIONS
 	Number of mmap data pages (must be a power of two) or size
 	specification with appended unit character - B/K/M/G. The
 	size is rounded up to have nearest pages power of two value.
+	Also, by adding a comma, the number of mmap pages for Instruction
+	Tracing can be specified.
 
 -g::
 	Enables call-graph (stack chain/backtrace) recording.
tools/perf/builtin-record.c +46 -3

61 lines not on this page · raw message

tools/perf/util/evlist.c +10 -6
index c720c6c..dbb1898 100644
@@ -843,7 +843,7 @@ static size_t perf_evlist__mmap_size(unsigned long pages)
 }
 
 static long parse_pages_arg(const char *str, unsigned long min,
-			    unsigned long max)
+			    unsigned long max, bool po2)
 {
 	unsigned long pages, val;
 	static struct parse_tag tags[] = {
@@ -871,7 +871,7 @@ static long parse_pages_arg(const char *str, unsigned long min,
 
 	if ((pages == 0) && (min == 0)) {
 		/* leave number of pages at 0 */
-	} else if (pages < (1UL << 31) && !is_power_of_2(pages)) {
+	} else if (po2 && pages < (1UL << 31) && !is_power_of_2(pages)) {
 		/* round pages up to next power of 2 */
 		pages = next_pow2(pages);
 		pr_info("rounding mmap pages size to %lu bytes (%lu pages)\n",
@@ -884,17 +884,15 @@ static long parse_pages_arg(const char *str, unsigned long min,
 	return pages;
 }
 
-int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
-				  int unset __maybe_unused)
+int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str, bool po2)
 {
-	unsigned int *mmap_pages = opt->value;
 	unsigned long max = UINT_MAX;
 	long pages;
 
 	if (max < SIZE_MAX / page_size)
 		max = SIZE_MAX / page_size;
 
-	pages = parse_pages_arg(str, 1, max);
+	pages = parse_pages_arg(str, 1, max, po2);
 	if (pages < 0) {
 		pr_err("Invalid argument for --mmap_pages/-m\n");
 		return -1;
@@ -904,6 +902,12 @@ int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
 	return 0;
 }
 
+int perf_evlist__parse_mmap_pages(const struct option *opt, const char *str,
+				  int unset __maybe_unused)
+{
+	return __perf_evlist__parse_mmap_pages(opt->value, str, true);
+}
+
 /**
  * perf_evlist__mmap_ex - Create mmaps to receive events.
  * @evlist: list of events
tools/perf/util/evlist.h +2 -0
index 7f56fdc..c5ef575 100644
@@ -109,6 +109,8 @@ int perf_evlist__prepare_workload(struct perf_evlist *evlist,
 				  bool want_signal);
 int perf_evlist__start_workload(struct perf_evlist *evlist);
 
+int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str,
+				    bool po2);
 int perf_evlist__parse_mmap_pages(const struct option *opt,
 				  const char *str,
 				  int unset);
Signature · 1 line
1.8.5.1
author reviewer
#125 lore raw

PATCH 45/71 perf tools: Add a user event for Instruction Tracing errors

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Errors encountered when decoding an Instruction Trace need to be reported to the user. However the "user" might be a script or another tool, so provide a new user event to capture those errors.

---
 tools/perf/util/event.c   |  1 +
 tools/perf/util/event.h   | 16 ++++++++++++++++
 tools/perf/util/session.c | 25 +++++++++++++++++++++++++
 tools/perf/util/tool.h    |  3 ++-
 4 files changed, 44 insertions(+), 1 deletion(-)
tools/perf/util/event.c +1 -0
index 990d10b..9ae8a26 100644
@@ -29,6 +29,7 @@ static const char *perf_event__names[] = {
 	[PERF_RECORD_ID_INDEX]			= "ID_INDEX",
 	[PERF_RECORD_ITRACE_INFO]		= "ITRACE_INFO",
 	[PERF_RECORD_ITRACE]			= "ITRACE",
+	[PERF_RECORD_ITRACE_ERROR]		= "ITRACE_ERROR",
 };
 
 const char *perf_event__name(unsigned int id)
tools/perf/util/event.h +16 -0
index ad3625c..9d9b9d2 100644
@@ -162,6 +162,7 @@ enum perf_user_event_type { /* above any possible kernel type */
 	PERF_RECORD_ID_INDEX			= 69,
 	PERF_RECORD_ITRACE_INFO			= 70,
 	PERF_RECORD_ITRACE			= 71,
+	PERF_RECORD_ITRACE_ERROR		= 72,
 	PERF_RECORD_HEADER_MAX
 };
 
@@ -224,6 +225,20 @@ struct itrace_lost_event {
 	u64 offset;
 };
 
+#define MAX_ITRACE_ERROR_MSG 64
+
+struct itrace_error_event {
+	struct perf_event_header header;
+	u32 type;
+	u32 code;
+	u32 cpu;
+	u32 pid;
+	u32 tid;
+	u32 reserved__; /* For alignment */
+	u64 ip;
+	char msg[MAX_ITRACE_ERROR_MSG];
+};
+
 union perf_event {
 	struct perf_event_header	header;
 	struct mmap_event		mmap;
@@ -242,6 +257,7 @@ union perf_event {
 	struct itrace_lost_event	itrace_lost;
 	struct itrace_info_event	itrace_info;
 	struct itrace_event		itrace;
+	struct itrace_error_event	itrace_error;
 };
 
 void perf_event__print_totals(void);
tools/perf/util/session.c +25 -0
index ac71006..95067b6 100644
@@ -254,6 +254,15 @@ static s64 process_event_itrace_stub(struct perf_tool *tool __maybe_unused,
 	return event->itrace.size;
 }
 
+static
+int process_event_itrace_error_stub(struct perf_tool *tool __maybe_unused,
+				    union perf_event *event __maybe_unused,
+				    struct perf_session *session __maybe_unused)
+{
+	dump_printf(": unhandled!\n");
+	return 0;
+}
+
 void perf_tool__fill_defaults(struct perf_tool *tool)
 {
 	if (tool->sample == NULL)
@@ -296,6 +305,8 @@ void perf_tool__fill_defaults(struct perf_tool *tool)
 		tool->itrace_info = process_event_itrace_info_stub;
 	if (tool->itrace == NULL)
 		tool->itrace = process_event_itrace_stub;
+	if (tool->itrace_error == NULL)
+		tool->itrace_error = process_event_itrace_error_stub;
 }
  
 static void swap_sample_id_all(union perf_event *event, void *data)
@@ -503,6 +514,17 @@ static void perf_event__itrace_swap(union perf_event *event,
 	event->itrace.cpu       = bswap_32(event->itrace.cpu);
 }
 
+static void perf_event__itrace_error_swap(union perf_event *event,
+					  bool sample_id_all __maybe_unused)
+{
+	event->itrace_error.type = bswap_32(event->itrace_error.type);
+	event->itrace_error.code = bswap_32(event->itrace_error.code);
+	event->itrace_error.cpu  = bswap_32(event->itrace_error.cpu);
+	event->itrace_error.pid  = bswap_32(event->itrace_error.pid);
+	event->itrace_error.tid  = bswap_32(event->itrace_error.tid);
+	event->itrace_error.ip   = bswap_64(event->itrace_error.ip);
+}
+
 typedef void (*perf_event__swap_op)(union perf_event *event,
 				    bool sample_id_all);
 
@@ -525,6 +547,7 @@ static perf_event__swap_op perf_event__swap_ops[] = {
 	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
 	[PERF_RECORD_ITRACE_INFO]	  = perf_event__itrace_info_swap,
 	[PERF_RECORD_ITRACE]		  = perf_event__itrace_swap,
+	[PERF_RECORD_ITRACE_ERROR]	  = perf_event__itrace_error_swap,
 	[PERF_RECORD_HEADER_MAX]	  = NULL,
 };
 
@@ -1105,6 +1128,8 @@ static s64 perf_session__process_user_event(struct perf_session *session,
 		/* setup for reading amidst mmap */
 		lseek(fd, file_offset + event->header.size, SEEK_SET);
 		return tool->itrace(tool, event, session);
+	case PERF_RECORD_ITRACE_ERROR:
+		return tool->itrace_error(tool, event, session);
 	default:
 		return -EINVAL;
 	}
tools/perf/util/tool.h +2 -1
index c1e8744..0700257 100644
@@ -47,7 +47,8 @@ struct perf_tool {
 	event_op2	finished_round,
 			build_id,
 			id_index,
-			itrace_info;
+			itrace_info,
+			itrace_error;
 	event_op3	itrace;
 	bool		ordered_samples;
 	bool		ordering_requires_timestamps;
Signature · 1 line
1.8.5.1
author reviewer
#126 lore raw

PATCH 46/71 perf session: Add Instruction Tracing hooks

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Hook into session processing so that
Instruction Trace decoding can
synthesize events transparently to
the tools.

---
 tools/perf/util/itrace.h  | 49 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/session.c | 45 +++++++++++++++++++++++++++++++++++++------
 tools/perf/util/session.h |  3 +++
 3 files changed, 91 insertions(+), 6 deletions(-)
tools/perf/util/itrace.h +49 -0
index da52a29..e6b0cc0 100644
@@ -26,6 +26,7 @@
 #include <linux/perf_event.h>
 
 #include "../perf.h"
+#include "session.h"
 #include "types.h"
 
 union perf_event;
@@ -35,6 +36,18 @@ struct perf_tool;
 struct perf_record_opts;
 struct itrace_info_event;
 
+struct itrace {
+	int (*process_event)(struct perf_session *session,
+			     union perf_event *event,
+			     struct perf_sample *sample,
+			     struct perf_tool *tool);
+	int (*flush_events)(struct perf_session *session,
+			    struct perf_tool *tool);
+	void (*free_events)(struct perf_session *session);
+	void (*free)(struct perf_session *session);
+	unsigned long long error_count;
+};
+
 /**
  * struct itrace_mmap - records an mmap at PERF_EVENT_ITRACE_OFFSET.
  * @base: address of mapped area
@@ -164,4 +177,40 @@ int perf_event__synthesize_itrace(struct perf_tool *tool,
 				  size_t size, u64 offset, u64 ref, int idx,
 				  u32 tid, u32 cpu);
 
+static inline int itrace__process_event(struct perf_session *session,
+					union perf_event *event,
+					struct perf_sample *sample,
+					struct perf_tool *tool)
+{
+	if (!session->itrace)
+		return 0;
+
+	return session->itrace->process_event(session, event, sample, tool);
+}
+
+static inline int itrace__flush_events(struct perf_session *session,
+				       struct perf_tool *tool)
+{
+	if (!session->itrace)
+		return 0;
+
+	return session->itrace->flush_events(session, tool);
+}
+
+static inline void itrace__free_events(struct perf_session *session)
+{
+	if (!session->itrace)
+		return;
+
+	return session->itrace->free_events(session);
+}
+
+static inline void itrace__free(struct perf_session *session)
+{
+	if (!session->itrace)
+		return;
+
+	return session->itrace->free(session);
+}
+
 #endif
tools/perf/util/session.c +39 -6

101 lines not on this page · raw message

tools/perf/util/session.h +3 -0
index 64d8145..9000193 100644
@@ -29,10 +29,13 @@ struct ordered_samples {
 	unsigned int		nr_samples;
 };
 
+struct itrace;
+
 struct perf_session {
 	struct perf_header	header;
 	struct machines		machines;
 	struct perf_evlist	*evlist;
+	struct itrace		*itrace;
 	struct trace_event	tevent;
 	struct events_stats	stats;
 	bool			repipe;
Signature · 1 line
1.8.5.1
author reviewer
#127 lore raw

PATCH 47/71 perf session: Add Instruction Tracing options

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Instruction Trace decoding synthesizes
"instructions" and "branches" events.
Add options for that.

---
 tools/perf/util/itrace.c  | 100 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h  |  33 +++++++++++++++
 tools/perf/util/session.h |   2 +
 3 files changed, 135 insertions(+)
tools/perf/util/itrace.c +100 -0
index 9596cc2..b80411d 100644
@@ -38,6 +38,7 @@
 
 #include "event.h"
 #include "debug.h"
+#include "parse-options.h"
 
 int itrace_mmap__mmap(struct itrace_mmap *mm, struct itrace_mmap_params *mp,
 		      int fd)
@@ -210,6 +211,105 @@ int perf_event__synthesize_itrace(struct perf_tool *tool,
 	return process(tool, &ev, NULL, NULL);
 }
 
+#define PERF_ITRACE_DEFAULT_PERIOD_TYPE	PERF_ITRACE_PERIOD_INSTRUCTIONS
+#define PERF_ITRACE_DEFAULT_PERIOD	1000
+
+void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
+{
+	synth_opts->instructions = true;
+	synth_opts->branches = true;
+	synth_opts->errors = true;
+	synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
+	synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
+}
+
+int itrace_parse_synth_opts(const struct option *opt, const char *str,
+			    int unset)
+{
+	struct itrace_synth_opts *synth_opts = opt->value;
+	const char *p;
+	char *endptr;
+
+	synth_opts->set = true;
+
+	if (unset) {
+		synth_opts->dont_decode = true;
+		return 0;
+	}
+
+	if (!str) {
+		itrace_synth_opts__set_default(synth_opts);
+		return 0;
+	}
+
+	for (p = str; *p;) {
+		switch (*p++) {
+		case 'i':
+			synth_opts->instructions = true;
+			while (*p == ' ' || *p == ',')
+				p += 1;
+			if (isdigit(*p)) {
+				synth_opts->period = strtoull(p, &endptr, 10);
+				p = endptr;
+				while (*p == ' ' || *p == ',')
+					p += 1;
+				switch (*p++) {
+				case 'i':
+					synth_opts->period_type =
+						PERF_ITRACE_PERIOD_INSTRUCTIONS;
+					break;
+				case 't':
+					synth_opts->period_type =
+						PERF_ITRACE_PERIOD_TICKS;
+					break;
+				case 'm':
+					synth_opts->period *= 1000;
+					/* Fall through */
+				case 'u':
+					synth_opts->period *= 1000;
+					/* Fall through */
+				case 'n':
+					if (*p++ != 's')
+						goto out_err;
+					synth_opts->period_type =
+						PERF_ITRACE_PERIOD_NANOSECS;
+					break;
+				case '\0':
+					goto out;
+				default:
+					goto out_err;
+				}
+			}
+			break;
+		case 'b':
+			synth_opts->branches = true;
+			break;
+		case 'e':
+			synth_opts->errors = true;
+			break;
+		case ' ':
+		case ',':
+			break;
+		default:
+			goto out_err;
+		}
+	}
+out:
+	if (synth_opts->instructions) {
+		if (!synth_opts->period_type)
+			synth_opts->period_type =
+					PERF_ITRACE_DEFAULT_PERIOD_TYPE;
+		if (!synth_opts->period)
+			synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
+	}
+
+	return 0;
+
+out_err:
+	pr_err("Bad instruction trace options '%s'\n", str);
+	return -EINVAL;
+}
+
 int itrace_mmap__read(struct itrace_mmap *mm, struct itrace_record *itr,
 		      struct perf_tool *tool, process_itrace_t fn)
 {
tools/perf/util/itrace.h +33 -0
index e6b0cc0..8453351 100644
@@ -33,9 +33,39 @@ union perf_event;
 struct perf_session;
 struct perf_evlist;
 struct perf_tool;
+struct option;
 struct perf_record_opts;
 struct itrace_info_event;
 
+enum itrace_period_type {
+	PERF_ITRACE_PERIOD_INSTRUCTIONS,
+	PERF_ITRACE_PERIOD_TICKS,
+	PERF_ITRACE_PERIOD_NANOSECS,
+};
+
+/**
+ * struct itrace_synth_opts - Instruction Tracing synthesis options.
+ * @set: indicates whether or not options have been set
+ * @inject: indicates the event (not just the sample) must be fully synthesized
+ *          because 'perf inject' will write it out
+ * @instructions: whether to synthesize 'instructions' events
+ * @branches: whether to synthesize 'branches' events
+ * @errors: whether to synthesize decoder error events
+ * @dont_decode: whether to skip decoding entirely
+ * @period: 'instructions' events period
+ * @period_type: 'instructions' events period type
+ */
+struct itrace_synth_opts {
+	bool			set;
+	bool			inject;
+	bool			instructions;
+	bool			branches;
+	bool			errors;
+	bool			dont_decode;
+	unsigned long long	period;
+	enum itrace_period_type	period_type;
+};
+
 struct itrace {
 	int (*process_event)(struct perf_session *session,
 			     union perf_event *event,
@@ -176,6 +206,9 @@ int perf_event__synthesize_itrace(struct perf_tool *tool,
 				  perf_event__handler_t process,
 				  size_t size, u64 offset, u64 ref, int idx,
 				  u32 tid, u32 cpu);
+int itrace_parse_synth_opts(const struct option *opt, const char *str,
+			    int unset);
+void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
 
 static inline int itrace__process_event(struct perf_session *session,
 					union perf_event *event,
tools/perf/util/session.h +2 -0
index 9000193..a7873c0 100644
@@ -30,12 +30,14 @@ struct ordered_samples {
 };
 
 struct itrace;
+struct itrace_synth_opts;
 
 struct perf_session {
 	struct perf_header	header;
 	struct machines		machines;
 	struct perf_evlist	*evlist;
 	struct itrace		*itrace;
+	struct itrace_synth_opts *itrace_synth_opts;
 	struct trace_event	tevent;
 	struct events_stats	stats;
 	bool			repipe;
Signature · 1 line
1.8.5.1
author reviewer
#128 lore raw

PATCH 48/71 perf session: Make perf_event__itrace_swap() non-static

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Make it possible for the Instruction Trace decoder to read and byte-swap Instruction Tracing events directly from file.

---
 tools/perf/util/session.c | 4 ++--
 tools/perf/util/session.h | 1 +
 2 files changed, 3 insertions(+), 2 deletions(-)
tools/perf/util/session.c +2 -2
index 55aead5..49c89e7 100644
@@ -505,8 +505,8 @@ static void perf_event__itrace_info_swap(union perf_event *event,
 	mem_bswap_64(event->itrace_info.priv, size);
 }
 
-static void perf_event__itrace_swap(union perf_event *event,
-				    bool sample_id_all __maybe_unused)
+void perf_event__itrace_swap(union perf_event *event,
+			     bool sample_id_all __maybe_unused)
 {
 	event->itrace.size      = bswap_64(event->itrace.size);
 	event->itrace.offset    = bswap_64(event->itrace.offset);
tools/perf/util/session.h +1 -0
index a7873c0..25aa9e7 100644
@@ -61,6 +61,7 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
 void perf_session__delete(struct perf_session *session);
 
 void perf_event_header__bswap(struct perf_event_header *hdr);
+void perf_event__itrace_swap(union perf_event *event, bool sample_id_all);
 
 int __perf_session__process_events(struct perf_session *session,
 				   u64 data_offset, u64 data_size, u64 size,
Signature · 1 line
1.8.5.1
author reviewer
#129 lore raw

PATCH 49/71 perf itrace: Add helpers for Instruction Tracing errors

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add functions to synthesize, count and print
Instruction Tracing error events.

---
 tools/perf/util/itrace.c | 55 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h | 16 ++++++++++++++
 2 files changed, 71 insertions(+)
tools/perf/util/itrace.c +55 -0
index b80411d..865b584 100644
@@ -25,6 +25,7 @@
 #include <linux/perf_event.h>
 
 #include <stdlib.h>
+#include <stdio.h>
 #include <string.h>
 #include <errno.h>
 
@@ -37,6 +38,7 @@
 #include "itrace.h"
 
 #include "event.h"
+#include "session.h"
 #include "debug.h"
 #include "parse-options.h"
 
@@ -162,6 +164,28 @@ struct itrace_record *__attribute__ ((weak)) itrace_record__init(int *err)
 	return NULL;
 }
 
+void itrace_synth_error(struct itrace_error_event *itrace_error, int type,
+			int code, int cpu, pid_t pid, pid_t tid, u64 ip,
+			const char *msg)
+{
+	size_t size;
+
+	memset(itrace_error, 0, sizeof(struct itrace_error_event));
+
+	itrace_error->header.type = PERF_RECORD_ITRACE_ERROR;
+	itrace_error->type = type;
+	itrace_error->code = code;
+	itrace_error->cpu = cpu;
+	itrace_error->pid = pid;
+	itrace_error->tid = tid;
+	itrace_error->ip = ip;
+	strncpy(itrace_error->msg, msg, MAX_ITRACE_ERROR_MSG - 1);
+
+	size = (void *)itrace_error->msg - (void *)itrace_error +
+	       strlen(itrace_error->msg);
+	itrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
+}
+
 int perf_event__synthesize_itrace_info(struct itrace_record *itr,
 				       struct perf_tool *tool,
 				       struct perf_session *session,
@@ -310,6 +334,37 @@ out_err:
 	return -EINVAL;
 }
 
+size_t perf_event__fprintf_itrace_error(union perf_event *event, FILE *fp)
+{
+	struct itrace_error_event *e = &event->itrace_error;
+	int ret;
+
+	ret = fprintf(fp, " Instruction trace error type %u", e->type);
+	ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
+		       e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
+	return ret;
+}
+
+int perf_event__process_itrace_error(struct perf_tool *tool __maybe_unused,
+				     union perf_event *event,
+				     struct perf_session *session)
+{
+	if (session->itrace)
+		session->itrace->error_count += 1;
+
+	perf_event__fprintf_itrace_error(event, stdout);
+	return 0;
+}
+
+int perf_event__count_itrace_error(struct perf_tool *tool __maybe_unused,
+				   union perf_event *event __maybe_unused,
+				   struct perf_session *session)
+{
+	if (session->itrace)
+		session->itrace->error_count += 1;
+	return 0;
+}
+
 int itrace_mmap__read(struct itrace_mmap *mm, struct itrace_record *itr,
 		      struct perf_tool *tool, process_itrace_t fn)
 {
tools/perf/util/itrace.h +16 -0
index 8453351..08877d2 100644
@@ -37,6 +37,10 @@ struct option;
 struct perf_record_opts;
 struct itrace_info_event;
 
+enum itrace_error_type {
+	PERF_ITRACE_DECODER_ERROR = 1,
+};
+
 enum itrace_period_type {
 	PERF_ITRACE_PERIOD_INSTRUCTIONS,
 	PERF_ITRACE_PERIOD_TICKS,
@@ -198,6 +202,10 @@ int itrace_record__info_fill(struct itrace_record *itr,
 void itrace_record__free(struct itrace_record *itr);
 u64 itrace_record__reference(struct itrace_record *itr);
 
+void itrace_synth_error(struct itrace_error_event *itrace_error, int type,
+			int code, int cpu, pid_t pid, pid_t tid, u64 ip,
+			const char *msg);
+
 int perf_event__synthesize_itrace_info(struct itrace_record *itr,
 				       struct perf_tool *tool,
 				       struct perf_session *session,
@@ -206,10 +214,18 @@ int perf_event__synthesize_itrace(struct perf_tool *tool,
 				  perf_event__handler_t process,
 				  size_t size, u64 offset, u64 ref, int idx,
 				  u32 tid, u32 cpu);
+int perf_event__process_itrace_error(struct perf_tool *tool,
+				     union perf_event *event,
+				     struct perf_session *session);
+int perf_event__count_itrace_error(struct perf_tool *tool __maybe_unused,
+				   union perf_event *event __maybe_unused,
+				   struct perf_session *session);
 int itrace_parse_synth_opts(const struct option *opt, const char *str,
 			    int unset);
 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
 
+size_t perf_event__fprintf_itrace_error(union perf_event *event, FILE *fp);
+
 static inline int itrace__process_event(struct perf_session *session,
 					union perf_event *event,
 					struct perf_sample *sample,
Signature · 1 line
1.8.5.1
author reviewer
#130 lore raw

PATCH 50/71 perf itrace: Add helpers for queuing Instruction Tracing data

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Provide functions to queue Instruction Tracing data buffers for processing. There is one queue for each of the mmap buffers used for recording.

---
 tools/perf/util/itrace.c | 278 +++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h |  77 +++++++++++++
 2 files changed, 355 insertions(+)
tools/perf/util/itrace.c +278 -0
index 865b584..f26d6cd 100644
@@ -23,11 +23,15 @@
 
 #include <linux/kernel.h>
 #include <linux/perf_event.h>
+#include <linux/string.h>
 
+#include <sys/param.h>
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
+#include <limits.h>
 #include <errno.h>
+#include <linux/list.h>
 
 #include "../perf.h"
 #include "types.h"
@@ -113,6 +117,233 @@ void itrace_mmap_params__set_idx(struct itrace_mmap_params *mp,
 	}
 }
 
+#define ITRACE_INIT_NR_QUEUES	32
+
+static struct itrace_queue *itrace_alloc_queue_array(unsigned int nr_queues)
+{
+	struct itrace_queue *queue_array;
+	unsigned int max_nr_queues, i;
+
+	max_nr_queues = MIN(UINT_MAX, SIZE_MAX) / sizeof(struct itrace_queue);
+	if (nr_queues > max_nr_queues)
+		return NULL;
+
+	queue_array = calloc(nr_queues, sizeof(struct itrace_queue));
+	if (!queue_array)
+		return NULL;
+
+	for (i = 0; i < nr_queues; i++) {
+		INIT_LIST_HEAD(&queue_array[i].head);
+		queue_array[i].priv = NULL;
+	}
+
+	return queue_array;
+}
+
+int itrace_queues__init(struct itrace_queues *queues)
+{
+	queues->nr_queues = ITRACE_INIT_NR_QUEUES;
+	queues->queue_array = itrace_alloc_queue_array(queues->nr_queues);
+	if (!queues->queue_array)
+		return -ENOMEM;
+	return 0;
+}
+
+static int itrace_queues__grow(struct itrace_queues *queues,
+			       unsigned int new_nr_queues)
+{
+	unsigned int nr_queues = queues->nr_queues;
+	struct itrace_queue *queue_array;
+	unsigned int i;
+
+	if (!nr_queues)
+		nr_queues = ITRACE_INIT_NR_QUEUES;
+
+	while (nr_queues && nr_queues < new_nr_queues)
+		nr_queues <<= 1;
+
+	if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
+		return -EINVAL;
+
+	queue_array = itrace_alloc_queue_array(nr_queues);
+	if (!queue_array)
+		return -ENOMEM;
+
+	for (i = 0; i < queues->nr_queues; i++) {
+		list_splice_tail(&queues->queue_array[i].head,
+				 &queue_array[i].head);
+		queue_array[i].priv = queues->queue_array[i].priv;
+	}
+
+	queues->nr_queues = nr_queues;
+	queues->queue_array = queue_array;
+
+	return 0;
+}
+
+static void *itrace_event__copy_data(union perf_event *event,
+				     struct perf_session *session)
+{
+	int fd = perf_data_file__fd(session->file);
+	void *p;
+	ssize_t ret;
+
+	if (event->itrace.size > SSIZE_MAX)
+		return NULL;
+
+	p = malloc(event->itrace.size);
+	if (!p)
+		return NULL;
+
+	ret = readn(fd, p, event->itrace.size);
+	if (ret != (ssize_t)event->itrace.size) {
+		free(p);
+		return NULL;
+	}
+
+	return p;
+}
+
+static int itrace_queues__add_buffer(struct itrace_queues *queues,
+				     unsigned int idx,
+				     struct itrace_buffer *buffer)
+{
+	struct itrace_queue *queue;
+	int err;
+
+	if (idx >= queues->nr_queues) {
+		err = itrace_queues__grow(queues, idx + 1);
+		if (err)
+			goto out_err;
+	}
+
+	queue = &queues->queue_array[idx];
+
+	if (!queue->set) {
+		queue->set = true;
+		queue->tid = buffer->tid;
+		queue->cpu = buffer->cpu;
+	} else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
+		pr_err("itrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
+		       queue->cpu, queue->tid, buffer->cpu, buffer->tid);
+		err = -EINVAL;
+		goto out_err;
+	}
+
+	list_add_tail(&buffer->list, &queue->head);
+
+	queues->new_data = true;
+
+	return 0;
+
+out_err:
+	if (buffer->data_needs_freeing)
+		free(buffer->data);
+	free(buffer);
+	return err;
+}
+
+/* Limit buffers to 32MiB on 32-bit */
+#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
+
+static int itrace_queues__split_buffer(struct itrace_queues *queues,
+				       union perf_event *event,
+				       struct itrace_buffer *buffer)
+{
+	u64 sz = event->itrace.size;
+	bool consecutive = false;
+	struct itrace_buffer *b;
+	int err;
+
+	while (sz > BUFFER_LIMIT_FOR_32_BIT) {
+		b = memdup(buffer, sizeof(struct itrace_buffer));
+		if (!b)
+			return -ENOMEM;
+		b->size = BUFFER_LIMIT_FOR_32_BIT;
+		b->consecutive = consecutive;
+		err = itrace_queues__add_buffer(queues, event->itrace.idx, b);
+		if (err)
+			return err;
+		buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
+		sz -= BUFFER_LIMIT_FOR_32_BIT;
+		consecutive = true;
+	}
+
+	buffer->size = sz;
+	buffer->consecutive = consecutive;
+
+	return 0;
+}
+
+int itrace_queues__add_event(struct itrace_queues *queues,
+			     struct perf_session *session,
+			     union perf_event *event, off_t data_offset,
+			     struct itrace_buffer **buffer_ptr)
+{
+	struct itrace_buffer *buffer;
+	int err;
+
+	queues->populated = true;
+
+	buffer = zalloc(sizeof(struct itrace_buffer));
+	if (!buffer)
+		return -ENOMEM;
+
+	if (buffer_ptr)
+		*buffer_ptr = buffer;
+
+	buffer->tid = event->itrace.tid;
+	buffer->cpu = event->itrace.cpu;
+
+	buffer->offset = event->itrace.offset;
+	buffer->reference = event->itrace.reference;
+
+	buffer->size = event->itrace.size;
+
+	if (session->one_mmap) {
+		buffer->data = data_offset - session->one_mmap_offset +
+			       session->one_mmap_addr;
+	} else if (perf_data_file__is_pipe(session->file)) {
+		buffer->data = itrace_event__copy_data(event, session);
+		if (!buffer->data)
+			return -ENOMEM;
+		buffer->data_needs_freeing = true;
+	} else if (BITS_PER_LONG == 64 ||
+		   event->itrace.size <= BUFFER_LIMIT_FOR_32_BIT) {
+		buffer->data_offset = data_offset;
+	} else {
+		buffer->data_offset = data_offset;
+		err = itrace_queues__split_buffer(queues, event, buffer);
+		if (err)
+			return err;
+	}
+
+	return itrace_queues__add_buffer(queues, event->itrace.idx, buffer);
+}
+
+void itrace_queues__free(struct itrace_queues *queues)
+{
+	unsigned int i;
+
+	for (i = 0; i < queues->nr_queues; i++) {
+		while (!list_empty(&queues->queue_array[i].head)) {
+			struct itrace_buffer *buffer;
+
+			buffer = list_entry(queues->queue_array[i].head.next,
+					     struct itrace_buffer, list);
+			itrace_buffer__put_data(buffer);
+			if (buffer->data_needs_freeing)
+				free(buffer->data);
+			list_del(&buffer->list);
+			free(buffer);
+		}
+	}
+
+	free(queues->queue_array);
+	queues->queue_array = NULL;
+	queues->nr_queues = 0;
+}
+
 size_t itrace_record__info_priv_size(struct itrace_record *itr)
 {
 	if (itr)
@@ -164,6 +395,53 @@ struct itrace_record *__attribute__ ((weak)) itrace_record__init(int *err)
 	return NULL;
 }
 
+struct itrace_buffer *itrace_buffer__next(struct itrace_queue *queue,
+					  struct itrace_buffer *buffer)
+{
+	if (buffer) {
+		if (list_is_last(&buffer->list, &queue->head))
+			return NULL;
+		return list_entry(buffer->list.next, struct itrace_buffer,
+				  list);
+	} else {
+		if (list_empty(&queue->head))
+			return NULL;
+		return list_entry(queue->head.next, struct itrace_buffer, list);
+	}
+}
+
+void *itrace_buffer__get_data(struct itrace_buffer *buffer, int fd)
+{
+	size_t adj = buffer->data_offset & (page_size - 1);
+	size_t size = buffer->size + adj;
+	off_t file_offset = buffer->data_offset - adj;
+	void *addr;
+
+	if (buffer->data)
+		return buffer->data;
+
+	addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
+	if (addr == MAP_FAILED)
+		return NULL;
+
+	buffer->mmap_addr = addr;
+	buffer->mmap_size = size;
+
+	buffer->data = addr + adj;
+
+	return buffer->data;
+}
+
+void itrace_buffer__put_data(struct itrace_buffer *buffer)
+{
+	if (!buffer->data || !buffer->mmap_addr)
+		return;
+	munmap(buffer->mmap_addr, buffer->mmap_size);
+	buffer->mmap_addr = NULL;
+	buffer->mmap_size = 0;
+	buffer->data = NULL;
+}
+
 void itrace_synth_error(struct itrace_error_event *itrace_error, int type,
 			int code, int cpu, pid_t pid, pid_t tid, u64 ip,
 			const char *msg)
tools/perf/util/itrace.h +77 -0

95 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#131 lore raw

PATCH 51/71 perf itrace: Add a heap for sorting Instruction Tracing queues

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

In order to process Instruction Tracing data in time order, the queue with data with the lowest timestamp must be processed first. Provide a heap to keep track of which queue that is.

---
 tools/perf/util/itrace.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h | 29 ++++++++++++++++
 2 files changed, 115 insertions(+)
tools/perf/util/itrace.c +86 -0
index f26d6cd..44214bc 100644
@@ -344,6 +344,92 @@ void itrace_queues__free(struct itrace_queues *queues)
 	queues->nr_queues = 0;
 }
 
+static void itrace_heapify(struct itrace_heap_item *heap_array,
+			   unsigned int pos, unsigned int queue_nr,
+			   u64 ordinal)
+{
+	unsigned int parent;
+
+	while (pos) {
+		parent = (pos - 1) >> 1;
+		if (heap_array[parent].ordinal <= ordinal)
+			break;
+		heap_array[pos] = heap_array[parent];
+		pos = parent;
+	}
+	heap_array[pos].queue_nr = queue_nr;
+	heap_array[pos].ordinal = ordinal;
+}
+
+int itrace_heap__add(struct itrace_heap *heap, unsigned int queue_nr,
+		     u64 ordinal)
+{
+	struct itrace_heap_item *heap_array;
+
+	if (queue_nr >= heap->heap_sz) {
+		unsigned int heap_sz = ITRACE_INIT_NR_QUEUES;
+
+		while (heap_sz <= queue_nr)
+			heap_sz <<= 1;
+		heap_array = realloc(heap->heap_array,
+				     heap_sz * sizeof(struct itrace_heap_item));
+		if (!heap_array)
+			return -ENOMEM;
+		heap->heap_array = heap_array;
+		heap->heap_sz = heap_sz;
+	}
+
+	itrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
+
+	return 0;
+}
+
+void itrace_heap__free(struct itrace_heap *heap)
+{
+	free(heap->heap_array);
+	heap->heap_array = NULL;
+	heap->heap_cnt = 0;
+	heap->heap_sz = 0;
+}
+
+void itrace_heap__pop(struct itrace_heap *heap)
+{
+	unsigned int pos, last, heap_cnt = heap->heap_cnt;
+	struct itrace_heap_item *heap_array;
+
+	if (!heap_cnt)
+		return;
+
+	heap->heap_cnt -= 1;
+
+	heap_array = heap->heap_array;
+
+	pos = 0;
+	while (1) {
+		unsigned int left, right;
+
+		left = (pos << 1) + 1;
+		if (left >= heap_cnt)
+			break;
+		right = left + 1;
+		if (right >= heap_cnt) {
+			heap_array[pos] = heap_array[left];
+			return;
+		}
+		if (heap_array[left].ordinal < heap_array[right].ordinal) {
+			heap_array[pos] = heap_array[left];
+			pos = left;
+		} else {
+			heap_array[pos] = heap_array[right];
+			pos = right;
+		}
+	}
+
+	last = heap_cnt - 1;
+	itrace_heapify(heap_array, pos, heap_array[last].queue_nr,
+		       heap_array[last].ordinal);
+}
+
 size_t itrace_record__info_priv_size(struct itrace_record *itr)
 {
 	if (itr)
tools/perf/util/itrace.h +29 -0
index b4aca53..304d377 100644
@@ -150,6 +150,29 @@ struct itrace_queues {
 };
 
 /**
+ * struct itrace_heap_item - element of struct itrace_heap.
+ * @queue_nr: queue number
+ * @ordinal: value used for sorting (lowest ordinal is top of the heap) expected
+ *           to be a timestamp
+ */
+struct itrace_heap_item {
+	unsigned int		queue_nr;
+	u64			ordinal;
+};
+
+/**
+ * struct itrace_heap - a heap suitable for sorting Instruction Tracing queues.
+ * @heap_array: the heap
+ * @heap_cnt: the number of elements in the heap
+ * @heap_sz: maximum number of elements (grows as needed)
+ */
+struct itrace_heap {
+	struct itrace_heap_item	*heap_array;
+	unsigned int		heap_cnt;
+	unsigned int		heap_sz;
+};
+
+/**
  * struct itrace_mmap - records an mmap at PERF_EVENT_ITRACE_OFFSET.
  * @base: address of mapped area
  * @mask: %0 if @len is not a power of two, otherwise (@len - %1)
@@ -266,6 +289,12 @@ struct itrace_buffer *itrace_buffer__next(struct itrace_queue *queue,
 					  struct itrace_buffer *buffer);
 void *itrace_buffer__get_data(struct itrace_buffer *buffer, int fd);
 void itrace_buffer__put_data(struct itrace_buffer *buffer);
+
+int itrace_heap__add(struct itrace_heap *heap, unsigned int queue_nr,
+		     u64 ordinal);
+void itrace_heap__pop(struct itrace_heap *heap);
+void itrace_heap__free(struct itrace_heap *heap);
+
 struct itrace_record *itrace_record__init(int *err);
 
 int itrace_record__options(struct itrace_record *itr,
Signature · 1 line
1.8.5.1
author reviewer
#132 lore raw

PATCH 52/71 perf itrace: Add processing for Instruction Tracing events

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Provide hooks so that an Instruction Trace
decoder can process Instruction Tracing
events.

---
 tools/perf/util/itrace.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h | 13 ++++++++++++
 2 files changed, 65 insertions(+)
tools/perf/util/itrace.c +52 -0
index 44214bc..91f1fb5 100644
@@ -579,6 +579,31 @@ out_free:
 	return err;
 }
 
+static bool itrace__dont_decode(struct perf_session *session)
+{
+	return !session->itrace_synth_opts ||
+	       session->itrace_synth_opts->dont_decode;
+}
+
+int perf_event__process_itrace_info(struct perf_tool *tool __maybe_unused,
+				    union perf_event *event,
+				    struct perf_session *session __maybe_unused)
+{
+	enum itrace_type type = event->itrace_info.type;
+
+	if (dump_trace)
+		fprintf(stdout, " type: %u\n", type);
+
+	if (itrace__dont_decode(session))
+		return 0;
+
+	switch (type) {
+	case PERF_ITRACE_UNKNOWN:
+	default:
+		return -EINVAL;
+	}
+}
+
 int perf_event__synthesize_itrace(struct perf_tool *tool,
 				  perf_event__handler_t process,
 				  size_t size, u64 offset, u64 ref, int idx,
@@ -599,6 +624,30 @@ int perf_event__synthesize_itrace(struct perf_tool *tool,
 	return process(tool, &ev, NULL, NULL);
 }
 
+s64 perf_event__process_itrace(struct perf_tool *tool, union perf_event *event,
+			       struct perf_session *session)
+{
+	s64 err;
+
+	if (dump_trace)
+		fprintf(stdout, " size: %"PRIu64"  offset: %"PRIx64"  ref: %"PRIx64"  idx: %u  tid: %d  cpu: %d\n",
+			event->itrace.size, event->itrace.offset,
+			event->itrace.reference, event->itrace.idx,
+			event->itrace.tid, event->itrace.cpu);
+
+	if (itrace__dont_decode(session))
+		return event->itrace.size;
+
+	if (!session->itrace || event->header.type != PERF_RECORD_ITRACE)
+		return -EINVAL;
+
+	err = session->itrace->process_itrace_event(session, event, tool);
+	if (err < 0)
+		return err;
+
+	return event->itrace.size;
+}
+
 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE	PERF_ITRACE_PERIOD_INSTRUCTIONS
 #define PERF_ITRACE_DEFAULT_PERIOD	1000
 
@@ -713,6 +762,9 @@ int perf_event__process_itrace_error(struct perf_tool *tool __maybe_unused,
 				     union perf_event *event,
 				     struct perf_session *session)
 {
+	if (itrace__dont_decode(session))
+		return 0;
+
 	if (session->itrace)
 		session->itrace->error_count += 1;
 
tools/perf/util/itrace.h +13 -0
index 304d377..ec3b78a 100644
@@ -38,6 +38,10 @@ struct option;
 struct perf_record_opts;
 struct itrace_info_event;
 
+enum itrace_type {
+	PERF_ITRACE_UNKNOWN,
+};
+
 enum itrace_error_type {
 	PERF_ITRACE_DECODER_ERROR = 1,
 };
@@ -76,6 +80,9 @@ struct itrace {
 			     union perf_event *event,
 			     struct perf_sample *sample,
 			     struct perf_tool *tool);
+	int (*process_itrace_event)(struct perf_session *session,
+				    union perf_event *event,
+				    struct perf_tool *tool);
 	int (*flush_events)(struct perf_session *session,
 			    struct perf_tool *tool);
 	void (*free_events)(struct perf_session *session);
@@ -316,10 +323,16 @@ int perf_event__synthesize_itrace_info(struct itrace_record *itr,
 				       struct perf_tool *tool,
 				       struct perf_session *session,
 				       perf_event__handler_t process);
+int perf_event__process_itrace_info(struct perf_tool *tool,
+				    union perf_event *event,
+				    struct perf_session *session);
 int perf_event__synthesize_itrace(struct perf_tool *tool,
 				  perf_event__handler_t process,
 				  size_t size, u64 offset, u64 ref, int idx,
 				  u32 tid, u32 cpu);
+s64 perf_event__process_itrace(struct perf_tool *tool,
+			       union perf_event *event,
+			       struct perf_session *session);
 int perf_event__process_itrace_error(struct perf_tool *tool,
 				     union perf_event *event,
 				     struct perf_session *session);
Signature · 1 line
1.8.5.1
author reviewer
#133 lore raw

PATCH 53/71 perf script: Add Instruction Tracing support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding an Instruction Trace.

---
 tools/perf/Documentation/perf-script.txt | 21 +++++++++++++++++++++
 tools/perf/builtin-script.c              | 11 +++++++++++
 2 files changed, 32 insertions(+)
tools/perf/Documentation/perf-script.txt +21 -0
index cfdbb1e..f9ad25e 100644
@@ -209,6 +209,27 @@ OPTIONS
 --show-mmap-events
 	Display mmap related events (e.g. MMAP, MMAP2).
 
+-Z::
+--itrace::
+	Options for decoding Instruction Tracing data. The options are:
+
+		i	synthesize instructions events
+		b	synthesize branches events
+		e	synthesize error events
+
+	The default is all events i.e. the same as -Zibe
+
+	In addition, the period (default 1000) for instructions events can be
+	specified in units of:
+
+		i	instructions (default)
+		t	ticks
+		ms	milliseconds
+		us	microseconds
+		ns	nanoseconds
+
+	To disable decoding entirely, use --no-itrace.
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-script-perl[1],
tools/perf/builtin-script.c +11 -0
index 4484886..96cdcd8 100644
@@ -16,6 +16,7 @@
 #include "util/evsel.h"
 #include "util/sort.h"
 #include "util/data.h"
+#include "util/itrace.h"
 #include <linux/bitmap.h>
 
 static char const		*script_name;
@@ -1487,6 +1488,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 	char *rec_script_path = NULL;
 	char *rep_script_path = NULL;
 	struct perf_session *session;
+	struct itrace_synth_opts itrace_synth_opts = {0};
 	char *script_path = NULL;
 	const char **__argv;
 	int i, j, err;
@@ -1501,6 +1503,10 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 			.attr		 = process_attr,
 			.tracing_data	 = perf_event__process_tracing_data,
 			.build_id	 = perf_event__process_build_id,
+			.id_index	 = perf_event__process_id_index,
+			.itrace_info	 = perf_event__process_itrace_info,
+			.itrace		 = perf_event__process_itrace,
+			.itrace_error	 = perf_event__process_itrace_error,
 			.ordered_samples = true,
 			.ordering_requires_timestamps = true,
 		},
@@ -1550,6 +1556,9 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 		    "Show the fork/comm/exit events"),
 	OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
 		    "Show the mmap events"),
+	OPT_CALLBACK_OPTARG('Z', "itrace", &itrace_synth_opts, NULL, "opts",
+			    "Instruction Tracing options",
+			    itrace_parse_synth_opts),
 	OPT_END()
 	};
 	const char * const script_usage[] = {
@@ -1740,6 +1749,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 
 	script.session = session;
 
+	session->itrace_synth_opts = &itrace_synth_opts;
+
 	if (cpu_list) {
 		if (perf_session__cpu_bitmap(session, cpu_list, cpu_bitmap))
 			return -1;
Signature · 1 line
1.8.5.1
author reviewer
#134 lore raw

PATCH 54/71 perf script: Always allow fields 'addr' and 'cpu' for itrace

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

If a file contains Instruction Tracing data then always allow fields 'addr' and 'cpu' to be selected as options for perf script. This is necessary because Instruction Trace decoding may synthesize events with that information.

---
 tools/perf/builtin-script.c | 2 ++
 1 file changed, 2 insertions(+)
tools/perf/builtin-script.c +2 -0
index 96cdcd8..15f4941 100644
@@ -190,6 +190,7 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 	}
 
 	if (PRINT_FIELD(ADDR) &&
+		!perf_header__has_feat(&session->header, HEADER_ITRACE) &&
 		perf_evsel__check_stype(evsel, PERF_SAMPLE_ADDR, "ADDR",
 					PERF_OUTPUT_ADDR))
 		return -EINVAL;
@@ -223,6 +224,7 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 		return -EINVAL;
 
 	if (PRINT_FIELD(CPU) &&
+		!perf_header__has_feat(&session->header, HEADER_ITRACE) &&
 		perf_evsel__check_stype(evsel, PERF_SAMPLE_CPU, "CPU",
 					PERF_OUTPUT_CPU))
 		return -EINVAL;
Signature · 1 line
1.8.5.1
#135 lore raw

Re: [PATCH v0 54/71] perf script: Always allow fields 'addr' and 'cpu' for itrace

Quoted 6 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

If a file contains Instruction Tracing data then always allow fields 'addr' and 'cpu' to be selected as options for perf script. This is necessary because Instruction Trace decoding may synthesize events with that information.

Why hardcode it? If it is present and the user opts for it then it will be printed. Why is the itrace check needed?

David

reviewer
#136 lore raw

Re: [PATCH v0 54/71] perf script: Always allow fields 'addr' and 'cpu' for itrace

Quoted 10 lines · David Ahern
Quoted 6 lines · Alexander Shishkin

From: Adrian Hunter <[email protected]>

If a file contains Instruction Tracing data then always allow fields 'addr' and 'cpu' to be selected as options for perf script. This is necessary because Instruction Trace decoding may synthesize events with that information.

Why hardcode it? If it is present and the user opts for it then it will be printed. Why is the itrace check needed?

itrace synthesizes events, so the events do not exist until the decoding starts, thus the validation prevents you from having 'addr' or 'cpu' even though they will show up in events later.

author reviewer
#137 lore raw

PATCH 55/71 perf report: Add Instruction Tracing support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding an Instruction Trace.

---
 tools/perf/Documentation/perf-report.txt | 21 +++++++++++++++++++++
 tools/perf/builtin-report.c              | 12 ++++++++++++
 2 files changed, 33 insertions(+)
tools/perf/Documentation/perf-report.txt +21 -0
index 10a2798..0c10bfb 100644
@@ -237,6 +237,27 @@ OPTIONS
 	Do not show entries which have an overhead under that percent.
 	(Default: 0).
 
+-Z::
+--itrace::
+	Options for decoding Instruction Tracing data. The options are:
+
+		i	synthesize instructions events
+		b	synthesize branches events
+		e	synthesize error events
+
+	The default is all events i.e. the same as -Zibe
+
+	In addition, the period (default 1000) for instructions events can be
+	specified in units of:
+
+		i	instructions (default)
+		t	ticks
+		ms	milliseconds
+		us	microseconds
+		ns	nanoseconds
+
+	To disable decoding entirely, use --no-itrace.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-annotate[1]
tools/perf/builtin-report.c +12 -0
index 8cf8e66..97e3ee6 100644
@@ -36,6 +36,8 @@
 #include "util/data.h"
 #include "arch/common.h"
 
+#include "util/itrace.h"
+
 #include <dlfcn.h>
 #include <linux/bitmap.h>
 
@@ -768,6 +770,7 @@ parse_percent_limit(const struct option *opt, const char *str,
 int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 {
 	struct perf_session *session;
+	struct itrace_synth_opts itrace_synth_opts = {0};
 	struct stat st;
 	bool has_br_stack = false;
 	int branch_mode = -1;
@@ -790,6 +793,10 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 			.attr		 = perf_event__process_attr,
 			.tracing_data	 = perf_event__process_tracing_data,
 			.build_id	 = perf_event__process_build_id,
+			.id_index	 = perf_event__process_id_index,
+			.itrace_info	 = perf_event__process_itrace_info,
+			.itrace		 = perf_event__process_itrace,
+			.itrace_error	 = perf_event__count_itrace_error,
 			.ordered_samples = true,
 			.ordering_requires_timestamps = true,
 		},
@@ -884,6 +891,9 @@ int cmd_report(int argc, const char **argv, const char *prefix __maybe_unused)
 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
 		     "Don't show entries under that percent", parse_percent_limit),
+	OPT_CALLBACK_OPTARG('Z', "itrace", &itrace_synth_opts, NULL, "opts",
+			    "Instruction Tracing options",
+			    itrace_parse_synth_opts),
 	OPT_END()
 	};
 	struct perf_data_file file = {
@@ -919,6 +929,8 @@ repeat:
 	if (session == NULL)
 		return -ENOMEM;
 
+	session->itrace_synth_opts = &itrace_synth_opts;
+
 	report.session = session;
 
 	has_br_stack = perf_header__has_feat(&session->header,
Signature · 1 line
1.8.5.1
author reviewer
#138 lore raw

PATCH 56/71 perf tools: Add Instruction Trace sampling support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add functions to configure Instruction
Trace sampling and queue Instruction
Trace samples for processing

---
 tools/perf/perf.h         |  10 ++++
 tools/perf/util/evsel.c   |   7 +++
 tools/perf/util/itrace.c  | 117 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h  |  36 ++++++++++++++
 tools/perf/util/record.c  |   2 +-
 tools/perf/util/session.c |   6 ++-
 6 files changed, 176 insertions(+), 2 deletions(-)
tools/perf/perf.h +10 -0
index b68b469..c748383 100644
@@ -262,6 +262,7 @@ struct perf_record_opts {
 	bool	     sample_time;
 	bool	     period;
 	bool	     full_itrace;
+	bool	     sample_itrace;
 	unsigned int freq;
 	unsigned int mmap_pages;
 	unsigned int itrace_mmap_pages;
@@ -269,8 +270,17 @@ struct perf_record_opts {
 	u64          branch_stack;
 	u64	     default_interval;
 	u64	     user_interval;
+	u64	     itrace_sample_config;
+	u32	     itrace_sample_type;
+	size_t	     itrace_sample_size;
 	u16	     stack_dump_size;
 	bool	     sample_transaction;
 };
 
+static
+inline bool perf_record_opts_itracing(const struct perf_record_opts *opts)
+{
+	return opts->full_itrace || opts->sample_itrace;
+}
+
 #endif
tools/perf/util/evsel.c +7 -0
index 88b7edd..0972b20 100644
@@ -640,6 +640,13 @@ void perf_evsel__config(struct perf_evsel *evsel,
 	if (opts->sample_weight)
 		perf_evsel__set_sample_bit(evsel, WEIGHT);
 
+	if (opts->sample_itrace && !evsel->no_aux_samples) {
+		perf_evsel__set_sample_bit(evsel, ITRACE);
+		attr->itrace_config = opts->itrace_sample_config;
+		attr->itrace_sample_size = opts->itrace_sample_size;
+		attr->itrace_sample_type = opts->itrace_sample_type;
+	}
+
 	attr->mmap  = track;
 	attr->comm  = track;
 
tools/perf/util/itrace.c +117 -0

129 lines not on this page · raw message

tools/perf/util/itrace.h +36 -0
index ec3b78a..2ebcdec 100644
@@ -80,9 +80,14 @@ struct itrace {
 			     union perf_event *event,
 			     struct perf_sample *sample,
 			     struct perf_tool *tool);
+	int (*queue_event)(struct perf_session *session,
+			   union perf_event *event,
+			   struct perf_sample *sample);
 	int (*process_itrace_event)(struct perf_session *session,
 				    union perf_event *event,
 				    struct perf_tool *tool);
+	void (*dump_itrace_sample)(struct perf_session *session,
+				   struct perf_sample *sample);
 	int (*flush_events)(struct perf_session *session,
 			    struct perf_tool *tool);
 	void (*free_events)(struct perf_session *session);
@@ -224,6 +229,9 @@ struct itrace_mmap_params {
 };
 
 struct itrace_record {
+	int (*parse_sample_options)(struct itrace_record *itr,
+				    struct perf_record_opts *opts,
+				    const char *str);
 	int (*recording_options)(struct itrace_record *itr,
 				 struct perf_evlist *evlist,
 				 struct perf_record_opts *opts);
@@ -291,6 +299,13 @@ int itrace_queues__add_event(struct itrace_queues *queues,
 			     struct perf_session *session,
 			     union perf_event *event, off_t data_offset,
 			     struct itrace_buffer **buffer_ptr);
+struct itrace_queue *itrace_queues__sample_queue(struct itrace_queues *queues,
+						 struct perf_sample *sample,
+						 struct perf_session *session);
+int itrace_queues__add_sample(struct itrace_queues *queues,
+			      struct perf_sample *sample,
+			      struct perf_session *session,
+			      unsigned int *queue_nr, u64 ref);
 void itrace_queues__free(struct itrace_queues *queues);
 struct itrace_buffer *itrace_buffer__next(struct itrace_queue *queue,
 					  struct itrace_buffer *buffer);
@@ -304,6 +319,8 @@ void itrace_heap__free(struct itrace_heap *heap);
 
 struct itrace_record *itrace_record__init(int *err);
 
+int itrace_parse_sample_options(const struct option *opt, const char *str,
+				int unset);
 int itrace_record__options(struct itrace_record *itr,
 			     struct perf_evlist *evlist,
 			     struct perf_record_opts *opts);
@@ -356,6 +373,25 @@ static inline int itrace__process_event(struct perf_session *session,
 	return session->itrace->process_event(session, event, sample, tool);
 }
 
+static inline int itrace__queue_event(struct perf_session *session,
+				      union perf_event *event,
+				      struct perf_sample *sample)
+{
+	if (!session->itrace)
+		return 0;
+
+	return session->itrace->queue_event(session, event, sample);
+}
+
+static inline void itrace__dump_itrace_sample(struct perf_session *session,
+					      struct perf_sample *sample)
+{
+	if (!session->itrace)
+		return;
+
+	session->itrace->dump_itrace_sample(session, sample);
+}
+
 static inline int itrace__flush_events(struct perf_session *session,
 				       struct perf_tool *tool)
 {
tools/perf/util/record.c +1 -1
index 86f980e..52d5bca 100644
@@ -93,7 +93,7 @@ void perf_evlist__config(struct perf_evlist *evlist,
 	list_for_each_entry(evsel, &evlist->entries, node)
 		perf_evsel__config(evsel, opts);
 
-	if (opts->full_itrace) {
+	if (perf_record_opts_itracing(opts)) {
 		use_sample_identifier = true;
 		list_for_each_entry(evsel, &evlist->entries, node)
 			perf_evsel__set_sample_id(evsel, use_sample_identifier);
tools/perf/util/session.c +5 -1
index 49c89e7..c60238a 100644
@@ -771,7 +771,7 @@ int perf_session_queue_event(struct perf_session *s, union perf_event *event,
 
 	__queue_event(new, s);
 
-	return 0;
+	return itrace__queue_event(s, event, sample);
 }
 
 static void callchain__printf(struct perf_sample *sample)
@@ -885,6 +885,10 @@ static void dump_event(struct perf_session *session, union perf_event *event,
 
 	trace_event(event);
 
+	/* Instruction trace sample is so big it is better printed here */
+	if (sample && sample->itrace_sample.size)
+		itrace__dump_itrace_sample(session, sample);
+
 	if (sample)
 		perf_session__print_tstamp(session, event, sample);
 
Signature · 1 line
1.8.5.1
author reviewer
#139 lore raw

PATCH 57/71 perf record: Add Instruction Trace sampling support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for Instruction Trace sampling.

---
 tools/perf/Documentation/perf-record.txt |  5 +++++
 tools/perf/builtin-record.c              | 15 +++++++++++++--
 2 files changed, 18 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-record.txt +5 -0
index bb01df7..58bd3c1 100644
@@ -211,6 +211,11 @@ overrides that and uses per-thread mmaps.  A side-effect of that is that
 inheritance is automatically disabled.  --per-thread is ignored with a warning
 if combined with -a or -C options.
 
+-I::
+--itrace::
+Enable Instruction Trace sampling. Each sample captures the specified number of
+bytes (default 4096) of trace. Instruction Tracing config can also be specified.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
tools/perf/builtin-record.c +13 -2
index 344603f..e75a15e 100644
@@ -401,7 +401,7 @@ static void perf_record__init_features(struct perf_record *rec)
 	if (!rec->opts.branch_stack)
 		perf_header__clear_feat(&session->header, HEADER_BRANCH_STACK);
 
-	if (!rec->opts.full_itrace)
+	if (!perf_record_opts_itracing(&rec->opts))
 		perf_header__clear_feat(&session->header, HEADER_ITRACE);
 }
 
@@ -507,13 +507,21 @@ static int __cmd_record(struct perf_record *rec, int argc, const char **argv)
 		}
 	}
 
-	if (rec->opts.full_itrace) {
+	if (perf_record_opts_itracing(&rec->opts)) {
 		err = perf_event__synthesize_itrace_info(rec->itr, tool,
 					session, process_synthesized_event);
 		if (err)
 			goto out_delete_session;
 	}
 
+	if (rec->opts.sample_itrace) {
+		err = perf_event__synthesize_id_index(tool,
+						      process_synthesized_event,
+						      session->evlist, machine);
+		if (err)
+			goto out_delete_session;
+	}
+
 	err = perf_event__synthesize_kernel_mmap(tool, process_synthesized_event,
 						 machine, "_text");
 	if (err < 0)
@@ -987,6 +995,9 @@ const struct option record_options[] = {
 		    "sample transaction flags (special events only)"),
 	OPT_BOOLEAN(0, "per-thread", &record.opts.target.per_thread,
 		    "use per-thread mmaps"),
+	OPT_CALLBACK_OPTARG('I', "itrace", &record.opts, &record.itr, "opts",
+			    "sample Instruction Trace",
+			    itrace_parse_sample_options),
 	OPT_END()
 };
 
Signature · 1 line
1.8.5.1
author reviewer
#140 lore raw

PATCH 58/71 perf tools: Add Instruction Tracing Snapshot Mode

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for making snapshots of
Instruction Tracing data.

---
 tools/perf/perf.h        |  2 ++
 tools/perf/util/itrace.c | 79 +++++++++++++++++++++++++++++++++++++++++++-----
 tools/perf/util/itrace.h | 20 ++++++++++++
 3 files changed, 93 insertions(+), 8 deletions(-)
tools/perf/perf.h +2 -0
index c748383..531b258 100644
@@ -263,6 +263,7 @@ struct perf_record_opts {
 	bool	     period;
 	bool	     full_itrace;
 	bool	     sample_itrace;
+	bool	     itrace_snapshot_mode;
 	unsigned int freq;
 	unsigned int mmap_pages;
 	unsigned int itrace_mmap_pages;
@@ -273,6 +274,7 @@ struct perf_record_opts {
 	u64	     itrace_sample_config;
 	u32	     itrace_sample_type;
 	size_t	     itrace_sample_size;
+	size_t	     itrace_snapshot_size;
 	u16	     stack_dump_size;
 	bool	     sample_transaction;
 };
tools/perf/util/itrace.c +71 -8

117 lines not on this page · raw message

tools/perf/util/itrace.h +20 -0
index 2ebcdec..9ff633c 100644
@@ -241,6 +241,14 @@ struct itrace_record {
 			 struct itrace_info_event *itrace_info,
 			 size_t priv_size);
 	void (*free)(struct itrace_record *itr);
+	int (*snapshot_start)(struct itrace_record *itr);
+	int (*snapshot_finish)(struct itrace_record *itr);
+	int (*find_snapshot)(struct itrace_record *itr, int idx,
+			     struct itrace_mmap *mm, unsigned char *data,
+			     u64 *head, u64 *old);
+	int (*parse_snapshot_options)(struct itrace_record *itr,
+				      struct perf_record_opts *opts,
+				      const char *str);
 	u64 (*reference)(struct itrace_record *itr);
 	int (*read_finish)(struct itrace_record *itr, int idx);
 };
@@ -294,6 +302,11 @@ int itrace_mmap__read(struct itrace_mmap *mm,
 			    struct itrace_record *itr, struct perf_tool *tool,
 			    process_itrace_t fn);
 
+int itrace_mmap__read_snapshot(struct itrace_mmap *mm,
+			       struct itrace_record *itr,
+			       struct perf_tool *tool, process_itrace_t fn,
+			       size_t snapshot_size);
+
 int itrace_queues__init(struct itrace_queues *queues);
 int itrace_queues__add_event(struct itrace_queues *queues,
 			     struct perf_session *session,
@@ -321,6 +334,8 @@ struct itrace_record *itrace_record__init(int *err);
 
 int itrace_parse_sample_options(const struct option *opt, const char *str,
 				int unset);
+int itrace_parse_snapshot_options(const struct option *opt, const char *str,
+				  int unset);
 int itrace_record__options(struct itrace_record *itr,
 			     struct perf_evlist *evlist,
 			     struct perf_record_opts *opts);
@@ -330,6 +345,11 @@ int itrace_record__info_fill(struct itrace_record *itr,
 			     struct itrace_info_event *itrace_info,
 			     size_t priv_size);
 void itrace_record__free(struct itrace_record *itr);
+int itrace_record__snapshot_start(struct itrace_record *itr);
+int itrace_record__snapshot_finish(struct itrace_record *itr);
+int itrace_record__find_snapshot(struct itrace_record *itr, int idx,
+				 struct itrace_mmap *mm,
+				 unsigned char *data, u64 *head, u64 *old);
 u64 itrace_record__reference(struct itrace_record *itr);
 
 void itrace_synth_error(struct itrace_error_event *itrace_error, int type,
Signature · 1 line
1.8.5.1
author reviewer
#141 lore raw

PATCH 59/71 perf record: Add Instruction Tracing Snapshot Mode support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a new option and support for Instruction Tracing Snapshot Mode. When the new option is selected, no Instruction Tracing data is captured until a signal (SIGUSR2) is received.

---
 tools/perf/Documentation/perf-record.txt |  7 +++
 tools/perf/builtin-record.c              | 90 +++++++++++++++++++++++++++++++-
 2 files changed, 95 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-record.txt +7 -0
index 58bd3c1..279c808 100644
@@ -216,6 +216,13 @@ if combined with -a or -C options.
 Enable Instruction Trace sampling. Each sample captures the specified number of
 bytes (default 4096) of trace. Instruction Tracing config can also be specified.
 
+-S::
+--snapshot::
+Select Instruction Tracing Snapshot Mode. This option is valid only with an
+Instruction Tracing event. Optionally the number of bytes to capture per
+snapshot can be specified. In Snapshot Mode, trace data is captured only when
+signal SIGUSR2 is received.
+
 SEE ALSO
 --------
 linkperf:perf-stat[1], linkperf:perf-list[1]
tools/perf/builtin-record.c +88 -2

156 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#142 lore raw

PATCH 60/71 perf inject: Re-pipe Instruction Tracing events

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

New Instruction Tracing events must be re-piped by default.

---
 tools/perf/builtin-inject.c | 68 +++++++++++++++++++++++++++++++++++++++------
 1 file changed, 59 insertions(+), 9 deletions(-)
tools/perf/builtin-inject.c +59 -9
index 78911a3..8c47982 100644
@@ -38,20 +38,14 @@ struct event_entry {
 	union perf_event event[0];
 };
 
-static int perf_event__repipe_synth(struct perf_tool *tool,
-				    union perf_event *event)
+static int output_bytes(struct perf_inject *inject, void *buf, size_t size)
 {
-	struct perf_inject *inject = container_of(tool, struct perf_inject, tool);
-	uint32_t size;
-	void *buf = event;
-
-	size = event->header.size;
+	ssize_t ret;
 
 	while (size) {
-		int ret = write(inject->output, buf, size);
+		ret = write(inject->output, buf, size);
 		if (ret < 0)
 			return -errno;
-
 		size -= ret;
 		buf += ret;
 		inject->bytes_written += ret;
@@ -60,6 +54,34 @@ static int perf_event__repipe_synth(struct perf_tool *tool,
 	return 0;
 }
 
+static int copy_bytes(struct perf_inject *inject, int fd, size_t size)
+{
+	char buf[4096];
+	ssize_t ssz;
+	int ret;
+
+	while (size) {
+		ssz = read(fd, buf, min(size, sizeof(buf)));
+		if (ssz < 0)
+			return -errno;
+		ret = output_bytes(inject, buf, ssz);
+		if (ret)
+			return ret;
+		size -= ssz;
+	}
+
+	return 0;
+}
+
+static int perf_event__repipe_synth(struct perf_tool *tool,
+				    union perf_event *event)
+{
+	struct perf_inject *inject = container_of(tool, struct perf_inject,
+						  tool);
+
+	return output_bytes(inject, event, event->header.size);
+}
+
 static int perf_event__repipe_op2_synth(struct perf_tool *tool,
 					union perf_event *event,
 					struct perf_session *session
@@ -86,6 +108,31 @@ static int perf_event__repipe_attr(struct perf_tool *tool,
 	return perf_event__repipe_synth(tool, event);
 }
 
+static s64 perf_event__repipe_itrace(struct perf_tool *tool,
+				     union perf_event *event,
+				     struct perf_session *session
+				     __maybe_unused)
+{
+	struct perf_inject *inject = container_of(tool, struct perf_inject,
+						  tool);
+	int ret;
+
+	if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
+		ret = output_bytes(inject, event, event->header.size);
+		if (ret < 0)
+			return ret;
+		ret = copy_bytes(inject, perf_data_file__fd(session->file),
+				 event->itrace.size);
+	} else {
+		ret = output_bytes(inject, event,
+				   event->header.size + event->itrace.size);
+	}
+	if (ret < 0)
+		return ret;
+
+	return event->itrace.size;
+}
+
 static int perf_event__repipe(struct perf_tool *tool,
 			      union perf_event *event,
 			      struct perf_sample *sample __maybe_unused,
@@ -423,6 +470,9 @@ int cmd_inject(int argc, const char **argv, const char *prefix __maybe_unused)
 			.unthrottle	= perf_event__repipe,
 			.attr		= perf_event__repipe_attr,
 			.tracing_data	= perf_event__repipe_op2_synth,
+			.itrace_info	= perf_event__repipe_op2_synth,
+			.itrace		= perf_event__repipe_itrace,
+			.itrace_error	= perf_event__repipe_op2_synth,
 			.finished_round	= perf_event__repipe_op2_synth,
 			.build_id	= perf_event__repipe_op2_synth,
 			.id_index	= perf_event__repipe_op2_synth,
Signature · 1 line
1.8.5.1
author reviewer
#143 lore raw

PATCH 61/71 perf inject: Add Instruction Tracing support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding an Instruction Trace. The Instruction Tracing events are stripped and replaced by synthesized events.

---
 tools/perf/Documentation/perf-inject.txt | 20 +++++++++
 tools/perf/builtin-inject.c              | 71 +++++++++++++++++++++++++++++++-
 2 files changed, 89 insertions(+), 2 deletions(-)
tools/perf/Documentation/perf-inject.txt +20 -0
index a00a342..c64cfea 100644
@@ -41,6 +41,26 @@ OPTIONS
 	tasks slept. sched_switch contains a callchain where a task slept and
 	sched_stat contains a timeslice how long a task slept.
 
+-Z::
+--itrace::
+	Decode Instruction Tracing data, replacing it with synthesized events.
+	Options are:
+
+		i	synthesize instructions events
+		b	synthesize branches events
+		e	synthesize error events
+
+	The default is all events i.e. the same as -Zibe
+
+	In addition, the period (default 1000) for instructions events can be
+	specified in units of:
+
+		i	instructions (default)
+		t	ticks
+		ms	milliseconds
+		us	microseconds
+		ns	nanoseconds
+
 SEE ALSO
 --------
 linkperf:perf-record[1], linkperf:perf-report[1], linkperf:perf-archive[1]
tools/perf/builtin-inject.c +69 -2

129 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#144 lore raw

PATCH 62/71 perf inject: Cut Instruction Tracing samples

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

After decoding Instruction Tracing samples, the Instruction Tracing data is no longer needed (having been replaced by synthesized events) so cut it out.

---
 tools/perf/builtin-inject.c | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)
tools/perf/builtin-inject.c +29 -0
index feeeb56..ed2b48f 100644
@@ -32,6 +32,7 @@ struct perf_inject {
 	u64		 bytes_written;
 	struct list_head samples;
 	struct itrace_synth_opts itrace_synth_opts;
+	char		 event_copy[PERF_SAMPLE_MAX_SIZE];
 };
 
 struct event_entry {
@@ -143,6 +144,28 @@ static int perf_event__repipe(struct perf_tool *tool,
 	return perf_event__repipe_synth(tool, event);
 }
 
+static union perf_event *
+perf_inject__cut_itrace_sample(struct perf_inject *inject,
+			       union perf_event *event,
+			       struct perf_sample *sample)
+{
+	size_t sz1 = sample->itrace_sample.data - (void *)event;
+	size_t sz2 = event->header.size - sample->itrace_sample.size - sz1;
+	union perf_event *ev = (union perf_event *)inject->event_copy;
+
+	if (sz1 > event->header.size || sz2 > event->header.size ||
+	    sz1 + sz2 > event->header.size ||
+	    sz1 < sizeof(struct perf_event_header) + sizeof(u64))
+		return event;
+
+	memcpy(ev, event, sz1);
+	memcpy((void *)ev + sz1, (void *)event + event->header.size - sz2, sz2);
+	ev->header.size = sz1 + sz2;
+	((u64 *)((void *)ev + sz1))[-1] = 0;
+
+	return ev;
+}
+
 typedef int (*inject_handler)(struct perf_tool *tool,
 			      union perf_event *event,
 			      struct perf_sample *sample,
@@ -155,6 +178,9 @@ static int perf_event__repipe_sample(struct perf_tool *tool,
 				     struct perf_evsel *evsel,
 				     struct machine *machine)
 {
+	struct perf_inject *inject = container_of(tool, struct perf_inject,
+						  tool);
+
 	if (evsel->handler) {
 		inject_handler f = evsel->handler;
 		return f(tool, event, sample, evsel, machine);
@@ -162,6 +188,9 @@ static int perf_event__repipe_sample(struct perf_tool *tool,
 
 	build_id__mark_dso_hit(tool, event, sample, evsel, machine);
 
+	if (inject->itrace_synth_opts.set && sample->itrace_sample.size)
+		event = perf_inject__cut_itrace_sample(inject, event, sample);
+
 	return perf_event__repipe_synth(tool, event);
 }
 
Signature · 1 line
1.8.5.1
author reviewer
#145 lore raw

PATCH 63/71 perf tools: Add Instruction Tracing index

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add an index of Instruction Tracing events within a perf.data file.

Instruction Tracing events contain data that can span back to the very beginning of the recording period. Consequently decoding cannot begin until all events are sorted. By adding an index, Instruction Tracing events can be found in advance and decoding can begin earlier.

---
 tools/perf/builtin-inject.c |  15 +++
 tools/perf/builtin-record.c |  18 +++-
 tools/perf/util/header.c    |  23 ++++-
 tools/perf/util/itrace.c    | 224 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/itrace.h    |  35 +++++++
 tools/perf/util/session.c   |   2 +
 tools/perf/util/session.h   |   1 +
 7 files changed, 313 insertions(+), 5 deletions(-)
tools/perf/builtin-inject.c +15 -0
index ed2b48f..ce1f298 100644
@@ -120,6 +120,18 @@ static s64 perf_event__repipe_itrace(struct perf_tool *tool,
 						  tool);
 	int ret;
 
+	if (!inject->pipe_output) {
+		off_t offset;
+
+		offset = lseek(inject->output, 0, SEEK_CUR);
+		if (offset == -1)
+			return -errno;
+		ret = itrace_index__itrace_event(&session->itrace_index, event,
+						 offset);
+		if (ret < 0)
+			return ret;
+	}
+
 	if (perf_data_file__is_pipe(session->file) || !session->one_mmap) {
 		ret = output_bytes(inject, event, event->header.size);
 		if (ret < 0)
@@ -523,6 +535,9 @@ static int __cmd_inject(struct perf_inject *inject)
 		output_data_offset = 4096;
 	}
 
+	if (!inject->itrace_synth_opts.set)
+		itrace_index__free(&session->itrace_index);
+
 	if (!inject->pipe_output)
 		lseek(inject->output, output_data_offset, SEEK_SET);
 
tools/perf/builtin-record.c +17 -1
index 46c451c..b35963f 100644
@@ -157,9 +157,24 @@ static int perf_record__process_itrace(struct perf_tool *tool,
 				       size_t len1, void *data2, size_t len2)
 {
 	struct perf_record *rec = container_of(tool, struct perf_record, tool);
+	struct perf_data_file *file = &rec->file;
 	size_t padding;
 	u8 pad[8] = {0};
 
+	if (!perf_data_file__is_pipe(file)) {
+		off_t file_offset;
+		int fd = perf_data_file__fd(file);
+		int err;
+
+		file_offset = lseek(fd, 0, SEEK_CUR);
+		if (file_offset == -1)
+			return -1;
+		err = itrace_index__itrace_event(&rec->session->itrace_index,
+						 event, file_offset);
+		if (err)
+			return err;
+	}
+
 	padding = (len1 + len2) & 7;
 	if (padding)
 		padding = 8 - padding;
@@ -395,7 +410,8 @@ static int perf_record__mmap_read_all(struct perf_record *rec)
 		}
 	}
 
-	if (perf_header__has_feat(&rec->session->header, HEADER_TRACING_DATA))
+	if (perf_header__has_feat(&rec->session->header, HEADER_TRACING_DATA) ||
+	    perf_header__has_feat(&rec->session->header, HEADER_ITRACE))
 		rc = perf_record__write(rec, &finished_round_event,
 					sizeof(finished_round_event));
 
tools/perf/util/header.c +19 -4
index 72bcca9..dd1a1f9 100644
@@ -1188,11 +1188,14 @@ static int write_branch_stack(int fd __maybe_unused,
 	return 0;
 }
 
-static int write_itrace(int fd __maybe_unused,
-			struct perf_header *h __maybe_unused,
+static int write_itrace(int fd, struct perf_header *h,
 			struct perf_evlist *evlist __maybe_unused)
 {
-	return 0;
+	struct perf_session *session;
+
+	session = container_of(h, struct perf_session, header);
+
+	return itrace_index__write(fd, &session->itrace_index);
 }
 
 static void print_hostname(struct perf_header *ph, int fd __maybe_unused,
@@ -2168,6 +2171,18 @@ out_free:
 	return ret;
 }
 
+static int process_itrace(struct perf_file_section *section,
+			  struct perf_header *ph, int fd,
+			  void *data __maybe_unused)
+{
+	struct perf_session *session;
+
+	session = container_of(ph, struct perf_session, header);
+
+	return itrace_index__process(fd, section->size, session,
+				     ph->needs_swap);
+}
+
 struct feature_ops {
 	int (*write)(int fd, struct perf_header *h, struct perf_evlist *evlist);
 	void (*print)(struct perf_header *h, int fd, FILE *fp);
@@ -2208,7 +2223,7 @@ static const struct feature_ops feat_ops[HEADER_LAST_FEATURE] = {
 	FEAT_OPA(HEADER_BRANCH_STACK,	branch_stack),
 	FEAT_OPP(HEADER_PMU_MAPPINGS,	pmu_mappings),
 	FEAT_OPP(HEADER_GROUP_DESC,	group_desc),
-	FEAT_OPA(HEADER_ITRACE,		itrace),
+	FEAT_OPP(HEADER_ITRACE,		itrace),
 };
 
 struct header_print_data {
tools/perf/util/itrace.c +224 -0

236 lines not on this page · raw message

tools/perf/util/itrace.h +35 -0
index 9ff633c..1005715 100644
@@ -75,6 +75,32 @@ struct itrace_synth_opts {
 	enum itrace_period_type	period_type;
 };
 
+/**
+ * struct itrace_index_entry - indexes a Instruction Tracing event within a
+ *                             perf.data file.
+ * @file_offset: offset within the perf.data file
+ * @sz: size of the event
+ */
+struct itrace_index_entry {
+	u64			file_offset;
+	u64			sz;
+};
+
+#define PERF_ITRACE_INDEX_ENTRY_COUNT 256
+
+/**
+ * struct itrace_index - index of Instruction Tracing events within a perf.data
+ *                       file.
+ * @list: linking a number of arrays of entries
+ * @nr: number of entries
+ * @entries: array of entries
+ */
+struct itrace_index {
+	struct list_head	list;
+	size_t			nr;
+	struct itrace_index_entry entries[PERF_ITRACE_INDEX_ENTRY_COUNT];
+};
+
 struct itrace {
 	int (*process_event)(struct perf_session *session,
 			     union perf_event *event,
@@ -320,6 +346,8 @@ int itrace_queues__add_sample(struct itrace_queues *queues,
 			      struct perf_session *session,
 			      unsigned int *queue_nr, u64 ref);
 void itrace_queues__free(struct itrace_queues *queues);
+int itrace_queues__process_index(struct itrace_queues *queues,
+				 struct perf_session *session);
 struct itrace_buffer *itrace_buffer__next(struct itrace_queue *queue,
 					  struct itrace_buffer *buffer);
 void *itrace_buffer__get_data(struct itrace_buffer *buffer, int fd);
@@ -352,6 +380,13 @@ int itrace_record__find_snapshot(struct itrace_record *itr, int idx,
 				 unsigned char *data, u64 *head, u64 *old);
 u64 itrace_record__reference(struct itrace_record *itr);
 
+int itrace_index__itrace_event(struct list_head *head, union perf_event *event,
+			       off_t file_offset);
+int itrace_index__write(int fd, struct list_head *head);
+int itrace_index__process(int fd, u64 size, struct perf_session *session,
+			  bool needs_swap);
+void itrace_index__free(struct list_head *head);
+
 void itrace_synth_error(struct itrace_error_event *itrace_error, int type,
 			int code, int cpu, pid_t pid, pid_t tid, u64 ip,
 			const char *msg);
tools/perf/util/session.c +2 -0
index c60238a..8d5f457 100644
@@ -80,6 +80,7 @@ struct perf_session *perf_session__new(struct perf_data_file *file,
 	INIT_LIST_HEAD(&session->ordered_samples.samples);
 	INIT_LIST_HEAD(&session->ordered_samples.sample_cache);
 	INIT_LIST_HEAD(&session->ordered_samples.to_free);
+	INIT_LIST_HEAD(&session->itrace_index);
 	machines__init(&session->machines);
 
 	if (file) {
@@ -150,6 +151,7 @@ static void perf_session_env__delete(struct perf_session_env *env)
 void perf_session__delete(struct perf_session *session)
 {
 	itrace__free(session);
+	itrace_index__free(&session->itrace_index);
 	perf_session__destroy_kernel_maps(session);
 	perf_session__delete_dead_threads(session);
 	perf_session__delete_threads(session);
tools/perf/util/session.h +1 -0
index 25aa9e7..9cf3840 100644
@@ -38,6 +38,7 @@ struct perf_session {
 	struct perf_evlist	*evlist;
 	struct itrace		*itrace;
 	struct itrace_synth_opts *itrace_synth_opts;
+	struct list_head	itrace_index;
 	struct trace_event	tevent;
 	struct events_stats	stats;
 	bool			repipe;
Signature · 1 line
1.8.5.1
author reviewer
#146 lore raw

PATCH 64/71 perf tools: Hit all build ids when Instruction Tracing

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

We need to include all buildids when a perf.data file contains Instruction Tracing data because we do not decode the trace for that purpose because it would take too long.

---
 tools/perf/builtin-buildid-list.c |  9 +++++++++
 tools/perf/builtin-inject.c       |  8 ++++++++
 tools/perf/builtin-record.c       | 10 +++++++++-
 3 files changed, 26 insertions(+), 1 deletion(-)
tools/perf/builtin-buildid-list.c +9 -0
index ed3873b..d694309 100644
@@ -69,6 +69,15 @@ static int perf_session__list_build_ids(bool force, bool with_hits)
 	session = perf_session__new(&file, false, &build_id__mark_dso_hit_ops);
 	if (session == NULL)
 		return -1;
+
+	/*
+	 * We take all buildids when the file contains Instruction Tracing data
+	 * because we do not decode the trace because it would take too long.
+	 */
+	if (!perf_data_file__is_pipe(&file) &&
+	    perf_header__has_feat(&session->header, HEADER_ITRACE))
+		with_hits = false;
+
 	/*
 	 * in pipe-mode, the only way to get the buildids is to parse
 	 * the record stream. Buildids are stored as RECORD_HEADER_BUILD_ID
tools/perf/builtin-inject.c +8 -0
index ce1f298..cafa6ab 100644
@@ -26,6 +26,7 @@ struct perf_inject {
 	struct perf_tool tool;
 	bool		 build_ids;
 	bool		 sched_stat;
+	bool		 have_itrace;
 	const char	 *input_name;
 	int		 pipe_output,
 			 output;
@@ -120,6 +121,8 @@ static s64 perf_event__repipe_itrace(struct perf_tool *tool,
 						  tool);
 	int ret;
 
+	inject->have_itrace = true;
+
 	if (!inject->pipe_output) {
 		off_t offset;
 
@@ -544,6 +547,11 @@ static int __cmd_inject(struct perf_inject *inject)
 	ret = perf_session__process_events(session, &inject->tool);
 
 	if (!inject->pipe_output) {
+		if (inject->build_ids && inject->have_itrace) {
+			perf_header__set_feat(&session->header,
+					      HEADER_BUILD_ID);
+			dsos__hit_all(session);
+		}
 		/*
 		 * The instruction traces have been removed and replaced with
 		 * synthesized hardware events, so clear the feature flag.
tools/perf/builtin-record.c +9 -1
index b35963f..d51fba6 100644
@@ -341,8 +341,16 @@ static void perf_record__exit(int status, void *arg)
 	if (!file->is_pipe) {
 		rec->session->header.data_size += rec->bytes_written;
 
-		if (!rec->no_buildid)
+		if (!rec->no_buildid) {
 			process_buildids(rec);
+			/*
+			 * We take all buildids when the file contains
+			 * Instruction Tracing data because we do not decode the
+			 * trace because it would take too long.
+			 */
+			if (perf_record_opts_itracing(&rec->opts))
+				dsos__hit_all(rec->session);
+		}
 		perf_session__write_header(rec->session, rec->evlist,
 					   file->fd, true);
 		perf_session__delete(rec->session);
Signature · 1 line
1.8.5.1
author reviewer
#147 lore raw

PATCH 65/71 perf itrace: Add Intel PT as an Instruction Tracing type

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add the Intel Processor Trace type
constant PERF_ITRACE_INTEL_PT.

---
 tools/perf/util/itrace.c | 1 +
 tools/perf/util/itrace.h | 1 +
 2 files changed, 2 insertions(+)
tools/perf/util/itrace.c +1 -0
index 28455f8..8ecbfb1 100644
@@ -977,6 +977,7 @@ int perf_event__process_itrace_info(struct perf_tool *tool __maybe_unused,
 		return 0;
 
 	switch (type) {
+	case PERF_ITRACE_INTEL_PT:
 	case PERF_ITRACE_UNKNOWN:
 	default:
 		return -EINVAL;
tools/perf/util/itrace.h +1 -0
index 1005715..de4b7a0 100644
@@ -40,6 +40,7 @@ struct itrace_info_event;
 
 enum itrace_type {
 	PERF_ITRACE_UNKNOWN,
+	PERF_ITRACE_INTEL_PT,
 };
 
 enum itrace_error_type {
Signature · 1 line
1.8.5.1
author reviewer
#148 lore raw

PATCH 66/71 perf tools: Add Intel PT packet decoder

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding Intel Processor Trace
packets.

---
 tools/perf/Makefile.perf                           |   2 +
 .../util/intel-pt-decoder/intel-pt-pkt-decoder.c   | 404 +++++++++++++++++++++
 .../util/intel-pt-decoder/intel-pt-pkt-decoder.h   |  68 ++++
 3 files changed, 474 insertions(+)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.h
tools/perf/Makefile.perf +2 -0
index 6ef50f9..a006fac 100644
@@ -294,6 +294,7 @@ LIB_H += util/unwind.h
 LIB_H += util/vdso.h
 LIB_H += util/tsc.h
 LIB_H += util/itrace.h
+LIB_H += util/intel-pt-decoder/intel-pt-pkt-decoder.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -374,6 +375,7 @@ LIB_OBJS += $(OUTPUT)util/srcline.o
 LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/itrace.o
+LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-pkt-decoder.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.c new file +404 -0

404 lines not on this page · raw message

tools/perf/util/intel-pt-decoder/intel-pt-pkt-decoder.h new file +68 -0

68 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#149 lore raw

PATCH 67/71 perf tools: Add Intel PT instruction decoder

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding instructions for Intel
Processor Trace. The kernel x86 instruction
decoder is used for this.

---
 tools/perf/Makefile.perf                           |  18 +-
 .../util/intel-pt-decoder/intel-pt-insn-decoder.c  | 224 +++++++++++++++++++++
 .../util/intel-pt-decoder/intel-pt-insn-decoder.h  |  67 ++++++
 3 files changed, 308 insertions(+), 1 deletion(-)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.h
tools/perf/Makefile.perf +17 -1
index a006fac..77310c0 100644
@@ -85,6 +85,7 @@ INSTALL = install
 FLEX    = flex
 BISON   = bison
 STRIP   = strip
+AWK     = awk
 
 LK_DIR          = $(srctree)/tools/lib/lk/
 TRACE_EVENT_DIR = $(srctree)/tools/lib/traceevent/
@@ -295,6 +296,7 @@ LIB_H += util/vdso.h
 LIB_H += util/tsc.h
 LIB_H += util/itrace.h
 LIB_H += util/intel-pt-decoder/intel-pt-pkt-decoder.h
+LIB_H += util/intel-pt-decoder/intel-pt-insn-decoder.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -376,6 +378,7 @@ LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/itrace.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-pkt-decoder.o
+LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-insn-decoder.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
@@ -660,6 +663,18 @@ $(OUTPUT)tests/python-use.o: tests/python-use.c $(OUTPUT)PERF-CFLAGS
 $(OUTPUT)util/config.o: util/config.c $(OUTPUT)PERF-CFLAGS
 	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DETC_PERFCONFIG='"$(ETC_PERFCONFIG_SQ)"' $<
 
+inat_tables_script = ../../arch/x86/tools/gen-insn-attr-x86.awk
+inat_tables_maps = ../../arch/x86/lib/x86-opcode-map.txt
+
+$(OUTPUT)util/intel-pt-decoder/inat-tables.c: $(inat_tables_script) $(inat_tables_maps)
+	$(QUIET_GEN)$(AWK) -f $(inat_tables_script) $(inat_tables_maps) > $@ || rm -f $@
+
+$(OUTPUT)util/intel-pt-decoder/inat.c:
+	$(QUIET_GEN)cp ../../arch/x86/lib/inat.c $(OUTPUT)util/intel-pt-decoder/inat.c
+
+$(OUTPUT)util/intel-pt-decoder/intel-pt-insn-decoder.o: util/intel-pt-decoder/intel-pt-insn-decoder.c ../../arch/x86/include/asm/insn.h ../../arch/x86/lib/insn.c $(OUTPUT)util/intel-pt-decoder/inat.c $(OUTPUT)util/intel-pt-decoder/inat-tables.c $(OUTPUT)PERF-CFLAGS
+	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -I../../arch/x86/include -I$(OUTPUT)util/intel-pt-decoder -I../../arch/x86/lib $<
+
 $(OUTPUT)ui/setup.o: ui/setup.c $(OUTPUT)PERF-CFLAGS
 	$(QUIET_CC)$(CC) -o $@ -c $(CFLAGS) -DLIBDIR='"$(libdir_SQ)"' $<
 
@@ -885,7 +900,8 @@ config-clean:
 clean: $(LIBTRACEEVENT)-clean $(LIBLK)-clean config-clean
 	$(call QUIET_CLEAN, core-objs)  $(RM) $(LIB_OBJS) $(BUILTIN_OBJS) $(LIB_FILE) $(OUTPUT)perf-archive $(OUTPUT)perf.o $(LANG_BINDINGS) $(GTK_OBJS)
 	$(call QUIET_CLEAN, core-progs) $(RM) $(ALL_PROGRAMS) perf
-	$(call QUIET_CLEAN, core-gen)   $(RM)  *.spec *.pyc *.pyo */*.pyc */*.pyo $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)PERF-CFLAGS $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex*
+	$(call QUIET_CLEAN, core-gen)   $(RM)  *.spec *.pyc *.pyo */*.pyc */*.pyo $(OUTPUT)common-cmds.h TAGS tags cscope* $(OUTPUT)PERF-VERSION-FILE $(OUTPUT)PERF-CFLAGS $(OUTPUT)util/*-bison* $(OUTPUT)util/*-flex* \
+		$(OUTPUT)util/intel-pt-decoder/inat.c $(OUTPUT)util/intel-pt-decoder/inat-tables.c
 	$(call QUIET_CLEAN, Documentation)
 	@$(MAKE) -C Documentation O=$(OUTPUT) clean >/dev/null
 	$(python-clean)
tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.c new file +224 -0

224 lines not on this page · raw message

tools/perf/util/intel-pt-decoder/intel-pt-insn-decoder.h new file +67 -0

67 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#150 lore raw

PATCH 68/71 perf tools: Add Intel PT log

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add a facility to log Intel Processor Trace
decoding. The log is intended for debugging
purposes only. If the log file exists, then
logging proceeds otherwise there is no
logging.

The log file name is "intel_pt.log" and is
opened in the current directory.

---
 tools/perf/Makefile.perf                        |   2 +
 tools/perf/util/intel-pt-decoder/intel-pt-log.c | 119 ++++++++++++++++++++++++
 tools/perf/util/intel-pt-decoder/intel-pt-log.h |  52 +++++++++++
 3 files changed, 173 insertions(+)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-log.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-log.h
tools/perf/Makefile.perf +2 -0
index 77310c0..e4faac1 100644
@@ -297,6 +297,7 @@ LIB_H += util/tsc.h
 LIB_H += util/itrace.h
 LIB_H += util/intel-pt-decoder/intel-pt-pkt-decoder.h
 LIB_H += util/intel-pt-decoder/intel-pt-insn-decoder.h
+LIB_H += util/intel-pt-decoder/intel-pt-log.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -379,6 +380,7 @@ LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/itrace.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-pkt-decoder.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-insn-decoder.o
+LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-log.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/intel-pt-decoder/intel-pt-log.c new file +119 -0

119 lines not on this page · raw message

tools/perf/util/intel-pt-decoder/intel-pt-log.h new file +52 -0

52 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#151 lore raw

PATCH 69/71 perf tools: Add Intel PT decoder

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for decoding an Intel Processor Trace.

---
 tools/perf/Makefile.perf                           |    2 +
 .../perf/util/intel-pt-decoder/intel-pt-decoder.c  | 1678 ++++++++++++++++++++
 .../perf/util/intel-pt-decoder/intel-pt-decoder.h  |   83 +
 3 files changed, 1763 insertions(+)
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.c
 create mode 100644 tools/perf/util/intel-pt-decoder/intel-pt-decoder.h
tools/perf/Makefile.perf +2 -0
index e4faac1..41f8a97 100644
@@ -298,6 +298,7 @@ LIB_H += util/itrace.h
 LIB_H += util/intel-pt-decoder/intel-pt-pkt-decoder.h
 LIB_H += util/intel-pt-decoder/intel-pt-insn-decoder.h
 LIB_H += util/intel-pt-decoder/intel-pt-log.h
+LIB_H += util/intel-pt-decoder/intel-pt-decoder.h
 LIB_H += ui/helpline.h
 LIB_H += ui/progress.h
 LIB_H += ui/util.h
@@ -381,6 +382,7 @@ LIB_OBJS += $(OUTPUT)util/itrace.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-pkt-decoder.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-insn-decoder.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-log.o
+LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-decoder.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/intel-pt-decoder/intel-pt-decoder.c new file +1678 -0

1,678 lines not on this page · raw message

tools/perf/util/intel-pt-decoder/intel-pt-decoder.h new file +83 -0

83 lines not on this page · raw message

Signature · 1 line
1.8.5.1
author reviewer
#152 lore raw

PATCH 70/71 perf tools: Add Intel PT support

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

Add support for Intel Processor Trace.

---
 tools/perf/Makefile.perf   |    2 +
 tools/perf/util/intel-pt.c | 2193 ++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/intel-pt.h |   40 +
 3 files changed, 2235 insertions(+)
 create mode 100644 tools/perf/util/intel-pt.c
 create mode 100644 tools/perf/util/intel-pt.h
tools/perf/Makefile.perf +2 -0
index 41f8a97..8ed9434 100644
@@ -295,6 +295,7 @@ LIB_H += util/unwind.h
 LIB_H += util/vdso.h
 LIB_H += util/tsc.h
 LIB_H += util/itrace.h
+LIB_H += util/intel-pt.h
 LIB_H += util/intel-pt-decoder/intel-pt-pkt-decoder.h
 LIB_H += util/intel-pt-decoder/intel-pt-insn-decoder.h
 LIB_H += util/intel-pt-decoder/intel-pt-log.h
@@ -379,6 +380,7 @@ LIB_OBJS += $(OUTPUT)util/srcline.o
 LIB_OBJS += $(OUTPUT)util/data.o
 LIB_OBJS += $(OUTPUT)util/tsc.o
 LIB_OBJS += $(OUTPUT)util/itrace.o
+LIB_OBJS += $(OUTPUT)util/intel-pt.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-pkt-decoder.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-insn-decoder.o
 LIB_OBJS += $(OUTPUT)util/intel-pt-decoder/intel-pt-log.o
tools/perf/util/intel-pt.c new file +2193 -0

2,193 lines not on this page · raw message

tools/perf/util/intel-pt.h new file +40 -0
new file mode 100644
index 0000000..99898ca
@@ -0,0 +1,40 @@
+/*
+ * intel_pt.h: Intel Processor Trace support
+ * Copyright (c) 2013, Intel Corporation.
+ *
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms and conditions of the GNU General Public License,
+ * version 2, as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ */
+
+#ifndef INCLUDE__PERF_INTEL_PT_H__
+#define INCLUDE__PERF_INTEL_PT_H__
+
+#define INTEL_PT_PMU_NAME "intel_pt"
+
+struct itrace_record;
+struct perf_tool;
+union perf_event;
+struct perf_session;
+struct perf_event_attr;
+struct perf_pmu;
+
+struct itrace_record *intel_pt_recording_init(int *err);
+
+int intel_pt_process_itrace_info(struct perf_tool *tool,
+				 union perf_event *event,
+				 struct perf_session *session);
+
+struct perf_event_attr *intel_pt_pmu_default_config(struct perf_pmu *pmu);
+
+#endif
Signature · 1 line
1.8.5.1
author reviewer
#153 lore raw

PATCH 71/71 perf tools: Take Intel PT into use

In reply to Alexander Shishkin

From: Adrian Hunter <[email protected]>

To record an Instruction Trace, the weak function
itrace_record__init() must be implemented.

Equally to decode an Instruction Trace, the
Instruction Tracing type must be added to the
perf_event__process_itrace_info() function.

This patch makes those two changes plus hooks
up default config for the intel_pt PMU. Also
some brief documentation is provided for
using the tools with intel_pt.

---
 tools/perf/Documentation/intel-pt.txt | 581 ++++++++++++++++++++++++++++++++++
 tools/perf/arch/x86/Makefile          |   2 +
 tools/perf/arch/x86/util/itrace.c     |  41 +++
 tools/perf/arch/x86/util/pmu.c        |  13 +
 tools/perf/util/itrace.c              |   7 +-
 5 files changed, 642 insertions(+), 2 deletions(-)
 create mode 100644 tools/perf/Documentation/intel-pt.txt
 create mode 100644 tools/perf/arch/x86/util/itrace.c
 create mode 100644 tools/perf/arch/x86/util/pmu.c
tools/perf/Documentation/intel-pt.txt new file +581 -0
new file mode 100644
index 0000000..977f5a0
@@ -0,0 +1,581 @@
+Intel Processor Trace
+=====================
+
+perf record
+===========
+
+new event
+---------
+
+The Intel PT kernel driver creates a new PMU for Intel PT.  PMU events are
+selected by providing the PMU name followed by the "config" separated by slashes.
+An enhancement has been made to allow default "config" e.g. the option
+
+	-e intel_pt//
+
+will use a default config value.  Currently that is the same as
+
+	-e intel_pt/tsc,noretcomp=0/
+
+which is the same as
+
+	-e intel_pt/tsc=1,noretcomp=0/
+
+The config terms are listed in /sys/devices/intel_pt/format.  They are bit
+fields within the itrace_config member of the struct perf_event_attr which is
+passed to the kernel by the perf_event_open system call.  They correspond to bit
+fields in the IA32_RTIT_CTL MSR.  Here is a list of them and their definitions:
+
+	$ for f in `ls /sys/devices/intel_pt/format`;do
+	> echo $f
+	> cat /sys/devices/intel_pt/format/$f
+	> done
+	noretcomp
+	itrace_config:11
+	tsc
+	itrace_config:10
+
+Note that the default config must be overridden for each term i.e.
+
+	-e intel_pt/noretcomp=0/
+
+is the same as:
+
+	-e intel_pt/tsc=1,noretcomp=0/
+
+So, to disable TSC packets use:
+
+	-e intel_pt/tsc=0/
+
+It is also possible to specify the itrace_config value explicitly:
+
+	-e intel_pt/itrace_config=0x400/
+
+Note that, as with all events, the event is suffixed with event modifiers:
+
+	u	userspace
+	k	kernel
+	h	hypervisor
+	G	guest
+	H	host
+	p	precise ip
+
+'h', 'G' and 'H' are for virtualization which is not supported by Intel PT.
+'p' is also not relevant to Intel PT.  So only options 'u' and 'k' are
+meaningful for Intel PT.
+
+perf_event_attr is displayed if the -vv option is used e.g.
+
+	------------------------------------------------------------
+	perf_event_attr:
+	type                6
+	size                120
+	config              0
+	sample_period       1
+	sample_freq         1
+	sample_type         0x10087
+	read_format         0x4
+	disabled            1    inherit             1
+	pinned              0    exclusive           0
+	exclude_user        0    exclude_kernel      1
+	exclude_hv          1    exclude_idle        0
+	mmap                0    comm                0
+	freq                0    inherit_stat        0
+	enable_on_exec      1    task                0
+	watermark           0    precise_ip          0
+	mmap_data           0    sample_id_all       1
+	exclude_host        0    exclude_guest       0
+	excl.callchain_kern 0    excl.callchain_user 0
+	mmap2               0
+	wakeup_events       0
+	wakeup_watermark    0
+	bp_type             0
+	bp_addr             0
+	config1             0
+	bp_len              0
+	config2             0
+	branch_sample_type  0
+	sample_regs_user    0
+	sample_stack_user   0
+	itrace_config       0xc00
+	itrace_watermark    0
+	itrace_sample_type  0
+	itrace_sample_size  0
+	------------------------------------------------------------
+	perf_event_open: pid 20956  cpu 0  group_fd -1  flags 0
+	perf_event_open: pid 20956  cpu 1  group_fd -1  flags 0
+	perf_event_open: pid 20956  cpu 2  group_fd -1  flags 0
+	perf_event_open: pid 20956  cpu 3  group_fd -1  flags 0
+	------------------------------------------------------------
+
+
+new sampling option
+-------------------
+
+To select Intel PT "sampling" a new option has been added:
+
+	-I
+
+Optionally it can be followed by the sample size in bytes e.g.
+
+	-I4096
+
+It is important to select a sample size that is big enough to contain at least
+one PSB packet.  If not a warning will be displayed:
+
+	Intel PT sample size (%zu) may be too small for PSB period (%zu)
+
+The calculation used for that is: if sample_size <= psb_period + 256 display the
+warning.
+
+The default sample size is currently 4KiB
+
+The sample size is passed in itrace_sample_size in struct perf_event_attr.  The
+sample size is limited by the maximum event size which is 64KiB.  It is
+difficult to know how big the event might be without the trace sample attached,
+but the tool validates that the sample size is not greater than 60KiB.
+
+The sample size is displayed if the option -vv is used e.g.
+
+	Intel PT sample size: %zu
+
+Optionally the "config" can be specified in exactly the same fashion as the
+intel_pt event but without slashes e.g.
+
+	-Itsc=0,noretcomp=0
+
+or
+
+	-I1024tsc=0,noretcomp=0
+
+
+new snapshot option
+-------------------
+
+To select snapshot mode a new option has been added:
+
+	-S
+
+Optionally it can be followed by the snapshot size e.g.
+
+	-S0x100000
+
+The default snapshot size is the itrace mmap size.  If neither itrace mmap size
+nor snapshot size is specified, then the default is 4MiB for privileged users
+(or if /proc/sys/kernel/perf_event_paranoid < 0), 128KiB for unprivileged users.
+If an unprivileged user does not specify mmap pages, the mmap pages will be
+reduced as described in the 'new itrace mmap size option' section below.
+
+The snapshot size is displayed if the option -vv is used e.g.
+
+	Intel PT snapshot size: %zu
+
+
+new itrace mmap size option
+---------------------------
+
+Intel PT buffer size is specified by an addition to the -m option e.g.
+
+	-m,16
+
+selects a buffer size of 16 pages i.e. 64KiB.
+
+Note that the existing functionality of -m is unchanged.  The itrace mmap size
+is specified by the optional addition of a comma and the value.
+
+The default itrace mmap size for Intel PT is 4MiB/page_size for privileged users
+(or if /proc/sys/kernel/perf_event_paranoid < 0), 128KiB for unprivileged users.
+If an unprivileged user does not specify mmap pages, the mmap pages will be
+reduced from the default 512KiB/page_size to 256KiB/page_size, otherwise the
+user is likely to get an error as they exceed their mlock limit (Max locked
+memory as shown in /proc/self/limits).  Note that perf does not count the first
+512KiB (actually /proc/sys/kernel/perf_event_mlock_kb minus 1 page) per cpu
+against the mlock limit so an unprivileged user is allowed 512KiB per cpu plus
+their mlock limit (which defaults to 64KiB but is not multiplied by the number
+of cpus).
+
+In full-trace mode, powers of two and multiples of 128MiB are allowed for buffer
+size, with a minimum size of 1 page.  In snapshot mode, it is the same but the
+minimum size is 2 pages.  In sample mode, the driver manages the buffer size.
+
+The mmap size and itrace mmap size are displayed if the -vv option is used e.g.
+
+	mmap length 528384
+	itrace mmap length 4198400
+
+
+Intel PT modes of operation
+---------------------------
+
+Intel PT can be used in 3 modes:
+	full-trace mode
+	sample mode
+	snapshot mode
+
+Full-trace mode traces continuously e.g.
+
+	perf record -e intel_pt//u uname
+
+Sample mode attaches a Intel PT sample to other events e.g.
+
+	perf record -e branch-misses:u -I uname
+
+Snapshot mode captures the available data when a signal is sent e.g.
+
+	perf record -v -e intel_pt//u -S ./loopy 1000000000 &
+	[1] 11435
+	kill -USR2 11435
+	Recording instruction tracing snapshot
+
+Note that the signal sent is SIGUSR2.
+Note that "Recording instruction tracing snapshot" is displayed because the -v
+option is used.
+
+None of the 3 modes can be used together.
+
+
+Buffer handling
+---------------
+
+There may be buffer limitations (i.e. single ToPa entry) which means that actual
+buffer sizes are limited to powers of 2 up to 128MiB.  In order to provide other
+sizes, and in particular an arbitrarily large size, multiple buffers are
+logically concatenated.  However an interrupt must be used to switch between
+buffers.  That has two potential problems:
+	a) the interrupt may not be handled in time so that the current buffer
+	becomes full and some trace data is lost.
+	b) the interrupts may slow the system and affect the performance
+	results.
+
+If trace data is lost, the driver adds a PERF_RECORD_ITRACE_LOST event to the
+event stream which the tools report as an error.
+
+In full-trace mode, the driver waits for data to be copied out before allowing
+the (logical) buffer to wrap-around.  If data is not copied out quickly enough,
+again a PERF_RECORD_ITRACE_LOST event is added to the event stream.  If the
+driver has to wait, the intel_pt event gets disabled.  Because it is difficult
+to know when that happens, perf tools always re-enable the intel_pt event
+after copying out data.
+
+Note that the choice of buffer size and output device (e.g. a fast SSD vs a slow
+disk) will affect the ability to capture a complete trace.
+
+
+Intel PT and build ids
+----------------------
+
+By default "perf record" post-processes the event stream to find all build ids
+for executables for all addresses sampled.  Deliberately, Intel PT is not
+decoded for that purpose (it would take too long).  Instead the build ids for
+all executables encountered (due to mmap, comm or task events) are included
+in the perf.data file.
+
+To see buildids included in the perf.data file use the command:
+
+	perf buildid-list
+
+If the perf.data file contains Intel PT data, that is the same as:
+
+	perf buildid-list --with-hits
+
+
+Snapshot mode and event disabling
+---------------------------------
+
+In order to make a snapshot, the intel_pt event is disabled using an IOCTL,
+namely PERF_EVENT_IOC_DISABLE.  However doing that can also disable the
+collection of side-band information.  In order to prevent that,  a dummy
+software event has been introduced that permits tracking events (like mmaps) to
+continue to be recorded while intel_pt is disabled.  That is important to ensure
+there is complete side-band information to allow the decoding of subsequent
+snapshots.
+
+A test has been created for that.  To find the test:
+
+	perf test list
+	...
+	23: Test using a dummy software event to keep tracking
+
+To run the test:
+
+	perf test 23
+	23: Test using a dummy software event to keep tracking     : Ok
+
+
+perf record modes (nothing new here)
+------------------------------------
+
+perf record essentially operates in one of three modes:
+	per thread
+	per cpu
+	workload only
+
+"per thread" mode is selected by -t or by --per-thread (with -p or -u or just a
+workload).
+"per cpu" is selected by -C or -a.
+"workload only" mode is selected by not using the other options but providing a
+command to run (i.e. the workload).
+
+In per-thread mode an exact list of threads is traced.  There is no inheritance.
+Each thread has its own event buffer.
+
+In per-cpu mode all processes (or processes from the selected cgroup i.e. -G
+option, or processes selected with -p or -u) are traced.  Each cpu has its own
+buffer. Inheritance is allowed.
+
+In workload-only mode, the workload is traced but with per-cpu buffers.
+Inheritance is allowed.  Note that you can now trace a workload in per-thread
+mode by using the --per-thread option.
+
+
+Privileged vs non-privileged users
+----------------------------------
+
+Unless /proc/sys/kernel/perf_event_paranoid is set to -1, unprivileged users
+have memory limits imposed upon them.  That affects what buffer sizes they can
+have as outlined above.
+
+Unless /proc/sys/kernel/perf_event_paranoid is set to -1, unprivileged users are
+not permitted to use tracepoints which means there is insufficient side-band
+information to decode Intel PT in per-cpu mode, and potentially workload-only
+mode too if the workload creates new processes.
+
+Note also, that to use tracepoints, read-access to debugfs is required.  So if
+debugfs is not mounted or the user does not have read-access, it will again not
+be possible to decode Intel PT in per-cpu mode.
+
+Note however, Intel PT samples are always decoded because the sample is very
+likely to be mainly from the thread that was running when the sample was taken.
+Obviously, if the sample includes a context switch from another process the
+decoding will fail if tracepoints were not available.
+
+
+sched_switch tracepoint
+-----------------------
+
+The sched_switch tracepoint is used to provide side-band data for Intel PT
+decoding.  sched_switch events are automatically added. e.g. the second event
+shown below
+
+	$ perf record -vv -e intel_pt//u uname
+	------------------------------------------------------------
+	perf_event_attr:
+	type                6
+	size                120
+	config              0
+	sample_period       1
+	sample_freq         1
+	sample_type         0x10087
+	read_format         0x4
+	disabled            1    inherit             1
+	pinned              0    exclusive           0
+	exclude_user        0    exclude_kernel      1
+	exclude_hv          1    exclude_idle        0
+	mmap                0    comm                0
+	freq                0    inherit_stat        0
+	enable_on_exec      1    task                0
+	watermark           0    precise_ip          0
+	mmap_data           0    sample_id_all       1
+	exclude_host        0    exclude_guest       0
+	excl.callchain_kern 0    excl.callchain_user 0
+	mmap2               0
+	wakeup_events       0
+	wakeup_watermark    0
+	bp_type             0
+	bp_addr             0
+	config1             0
+	bp_len              0
+	config2             0
+	branch_sample_type  0
+	sample_regs_user    0
+	sample_stack_user   0
+	itrace_config       0xc00
+	itrace_watermark    0
+	itrace_sample_type  0
+	itrace_sample_size  0
+	------------------------------------------------------------
+	perf_event_open: pid 21206  cpu 0  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 1  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 2  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 3  group_fd -1  flags 0
+	------------------------------------------------------------
+	perf_event_attr:
+	type                2
+	size                120
+	config              0x143
+	sample_period       1
+	sample_freq         1
+	sample_type         0x10587
+	read_format         0x4
+	disabled            0    inherit             1
+	pinned              0    exclusive           0
+	exclude_user        0    exclude_kernel      0
+	exclude_hv          0    exclude_idle        0
+	mmap                0    comm                0
+	freq                0    inherit_stat        0
+	enable_on_exec      0    task                0
+	watermark           0    precise_ip          0
+	mmap_data           0    sample_id_all       1
+	exclude_host        0    exclude_guest       1
+	excl.callchain_kern 0    excl.callchain_user 0
+	mmap2               0
+	wakeup_events       0
+	wakeup_watermark    0
+	bp_type             0
+	bp_addr             0
+	config1             0
+	bp_len              0
+	config2             0
+	branch_sample_type  0
+	sample_regs_user    0
+	sample_stack_user   0
+	itrace_config       0
+	itrace_watermark    0
+	itrace_sample_type  0
+	itrace_sample_size  0
+	------------------------------------------------------------
+	perf_event_open: pid -1  cpu 0  group_fd -1  flags 0
+	perf_event_open: pid -1  cpu 1  group_fd -1  flags 0
+	perf_event_open: pid -1  cpu 2  group_fd -1  flags 0
+	perf_event_open: pid -1  cpu 3  group_fd -1  flags 0
+	------------------------------------------------------------
+	perf_event_attr:
+	type                1
+	size                120
+	config              0x9
+	sample_period       1
+	sample_freq         1
+	sample_type         0x10007
+	read_format         0x4
+	disabled            1    inherit             1
+	pinned              0    exclusive           0
+	exclude_user        0    exclude_kernel      1
+	exclude_hv          1    exclude_idle        0
+	mmap                1    comm                1
+	freq                0    inherit_stat        0
+	enable_on_exec      1    task                0
+	watermark           0    precise_ip          0
+	mmap_data           0    sample_id_all       1
+	exclude_host        0    exclude_guest       0
+	excl.callchain_kern 0    excl.callchain_user 0
+	mmap2               1
+	wakeup_events       0
+	wakeup_watermark    0
+	bp_type             0
+	bp_addr             0
+	config1             0
+	bp_len              0
+	config2             0
+	branch_sample_type  0
+	sample_regs_user    0
+	sample_stack_user   0
+	itrace_config       0
+	itrace_watermark    0
+	itrace_sample_type  0
+	itrace_sample_size  0
+	------------------------------------------------------------
+	perf_event_open: pid 21206  cpu 0  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 1  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 2  group_fd -1  flags 0
+	perf_event_open: pid 21206  cpu 3  group_fd -1  flags 0
+	mmap length 528384
+	itrace mmap length 4198400
+	perf event ring buffer mmapped per cpu
+	Synthesizing itrace information
+	Linux
+	[ perf record: Woken up 1 times to write data ]
+	[ perf record: Captured and wrote 0.060 MB perf.data ]
+
+Note, the sched_switch event is only added if the user is permitted to use it
+and only in per-cpu mode.
+
+Note also, the sched_switch event is only added if TSC packets are requested.
+That is because, in the absence of timing information, the sched_switch events
+cannot be matched against the Intel PT trace.
+
+
+perf script
+===========
+
+By default, perf script will decode trace data found in the perf.data file.
+This can be further controlled by new option -Z.
+
+
+New instruction trace option
+----------------------------
+
+Having no option is the same as
+
+	-Z
+
+which, in turn, is the same as
+
+	-Zibe
+
+The letters are:
+
+	i	synthesize "instructions" events
+	b	synthesize "branches" events
+	e	synthesize tracing error events
+
+"Instructions" events look like they were recorded by "perf record -e
+instructions".
+
+"Branches" events look like they were recorded by "perf record -e branches".
+
+Error events are new.  They show where the decoder lost the trace.  Error events
+are quite important.  Users must know if what they are seeing is a complete
+picture or not.
+
+In addition, the period of the "instructions" event can be specified. e.g.
+
+	-Zi10us
+
+sets the period to 10us i.e. one  instruction sample is synthesized for each 10
+microseconds of trace.  Alternatives to "us" are "ms" (milliseconds),
+"ns" (nanoseconds), "t" (TSC ticks) or "i" (instructions).
+
+"ms", "us" and "ns" are converted to TSC ticks.
+
+The timing information included with Intel PT does not give the time of every
+instruction.  Consequently, for the purpose of sampling, the decoder estimates
+the time since the last timing packet based on 1 tick per instruction.  The time
+on the sample is *not* adjusted and reflects the last known value of TSC.
+
+For Intel PT, the default period is 1000 instructions.
+
+To disable trace decoding entirely, use the option --no-itrace.
+
+
+dump option
+-----------
+
+perf script has an option (-D) to "dump" the events i.e. display the binary
+data.
+
+When -D is used, Intel PT packets are displayed.  The packet decoder does not
+pay attention to PSB packets, but just decodes the bytes - so the packets seen
+by the actual decoder may not be identical in places where the data is corrupt.
+One example of that would be when the buffer-switching interrupt has been too
+slow, and the buffer has been filled completely.  In that case, the last packet
+in the buffer might be truncated and immediately followed by a PSB as the trace
+continues in the next buffer.
+
+To disable the display of Intel PT packets, combine the -D option with
+--no-itrace.
+
+
+perf report
+===========
+
+By default, perf report will decode trace data found in the perf.data file.
+This can be further controlled by new option -Z exactly the same as perf script.
+
+
+perf inject
+===========
+
+perf inject also accepts the -Z option in which case tracing data is removed and
+replaced with the synthesized events. e.g.
+
+	perf inject -Z -i perf.data -o perf.data.new
tools/perf/arch/x86/Makefile +2 -0
index 8801fe0..2700698 100644
@@ -7,4 +7,6 @@ LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/unwind.o
 endif
 LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/header.o
 LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/tsc.o
+LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/pmu.o
+LIB_OBJS += $(OUTPUT)arch/$(ARCH)/util/itrace.o
 LIB_H += arch/$(ARCH)/util/tsc.h
tools/perf/arch/x86/util/itrace.c new file +41 -0

41 lines not on this page · raw message

tools/perf/arch/x86/util/pmu.c new file +13 -0
new file mode 100644
index 0000000..699a7c2
@@ -0,0 +1,13 @@
+#include <string.h>
+
+#include <linux/perf_event.h>
+
+#include "../../util/intel-pt.h"
+#include "../../util/pmu.h"
+
+struct perf_event_attr *perf_pmu__get_default_config(struct perf_pmu *pmu)
+{
+	if (!strcmp(pmu->name, INTEL_PT_PMU_NAME))
+		return intel_pt_pmu_default_config(pmu);
+	return NULL;
+}
tools/perf/util/itrace.c +5 -2
index 8ecbfb1..bea3cf7 100644
@@ -46,6 +46,8 @@
 #include "debug.h"
 #include "parse-options.h"
 
+#include "intel-pt.h"
+
 int itrace_mmap__mmap(struct itrace_mmap *mm, struct itrace_mmap_params *mp,
 		      int fd)
 {
@@ -964,9 +966,9 @@ static bool itrace__dont_decode(struct perf_session *session)
 	       session->itrace_synth_opts->dont_decode;
 }
 
-int perf_event__process_itrace_info(struct perf_tool *tool __maybe_unused,
+int perf_event__process_itrace_info(struct perf_tool *tool,
 				    union perf_event *event,
-				    struct perf_session *session __maybe_unused)
+				    struct perf_session *session)
 {
 	enum itrace_type type = event->itrace_info.type;
 
@@ -978,6 +980,7 @@ int perf_event__process_itrace_info(struct perf_tool *tool __maybe_unused,
 
 	switch (type) {
 	case PERF_ITRACE_INTEL_PT:
+		return intel_pt_process_itrace_info(tool, event, session);
 	case PERF_ITRACE_UNKNOWN:
 	default:
 		return -EINVAL;
Signature · 1 line
1.8.5.1
#154 lore raw
In reply to Alexander Shishkin
Quoted 7 lines · Alexander Shishkin

Hi,

This patchset adds support for Intel Processor Trace (PT) extension
[1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

Ok, this feature looks rather interesting.

On the hardware side this is essentially BTS (Branch Trace Store) on steroids (with many extensions), right?

Thanks,

Ingo

author reviewer
#155 lore raw
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Alexander Shishkin

Hi,

This patchset adds support for Intel Processor Trace (PT) extension
[1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

Quoted 4 lines

Ok, this feature looks rather interesting.

On the hardware side this is essentially BTS (Branch Trace Store) on steroids (with many extensions), right?

Yes, you get timestamps and all sorts of other useful data in the trace and the performance intrusion is much less than that of BTS.

Regards,
--
Alex

#156 lore raw
Quoted 12 lines · Alexander Shishkin
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Alexander Shishkin

Hi,

This patchset adds support for Intel Processor Trace (PT) extension
[1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

Quoted 7 lines
Quoted 4 lines

Ok, this feature looks rather interesting.

On the hardware side this is essentially BTS (Branch Trace Store) on steroids (with many extensions), right?

Yes, you get timestamps and all sorts of other useful data in the trace and the performance intrusion is much less than that of BTS.

So the problem I see here right now that BTS is rarely used and AFAICS close to unmaintained. It has some very minimal support in 'perf script' but that's all I can see.

So one necessary precondition to merging PT support would be to have a convincing case that this kind of stuff is generally useful.

One good approach to do that would be to unify the BTS and PT tooling (the kernel side can be unified as well, to the extent it makes sense), and to prove it via actual functionality that this stuff matters. BTS is available widely, so the tooling can be tested by anyone who's interested.

Allow people to record crashes in core dumps, allow them to look at histograms/spectrograms of BTS/PT traces, zoom in on actual traces, etc. - make it easier to handle this huge amount of data and visualize traces in other ways you find useful, etc.

None of that is done right now via BTS so nobody uses it.

Thanks,

Ingo

author reviewer
#157 lore raw
Quoted 22 lines · Ingo Molnar
Quoted 12 lines · Alexander Shishkin
Quoted 10 lines · Ingo Molnar
Quoted 7 lines · Alexander Shishkin

Hi,

This patchset adds support for Intel Processor Trace (PT) extension
[1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

Quoted 7 lines
Quoted 4 lines

Ok, this feature looks rather interesting.

On the hardware side this is essentially BTS (Branch Trace Store) on steroids (with many extensions), right?

Yes, you get timestamps and all sorts of other useful data in the trace and the performance intrusion is much less than that of BTS.

Quoted 6 lines

So the problem I see here right now that BTS is rarely used and AFAICS close to unmaintained. It has some very minimal support in 'perf script' but that's all I can see.

So one necessary precondition to merging PT support would be to have a convincing case that this kind of stuff is generally useful.

This is not unreasonable. We can have some of this functionality with BTS.

Quoted 6 lines

One good approach to do that would be to unify the BTS and PT tooling (the kernel side can be unified as well, to the extent it makes sense), and to prove it via actual functionality that this stuff matters. BTS is available widely, so the tooling can be tested by anyone who's interested.

Quoted 6 lines

Allow people to record crashes in core dumps, allow them to look at histograms/spectrograms of BTS/PT traces, zoom in on actual traces, etc. - make it easier to handle this huge amount of data and visualize traces in other ways you find useful, etc.

None of that is done right now via BTS so nobody uses it.

So I can make BTS appear as an "itrace" pmu similarly to PT. One question that comes to mind is should we then dispose of the old interface that's used for accessing BTS functionality or make it coexist with the new one.

Regards,
--
Alex

#158 lore raw
Quoted 11 lines · Alexander Shishkin
Quoted 11 lines

One good approach to do that would be to unify the BTS and PT tooling (the kernel side can be unified as well, to the extent it makes sense), and to prove it via actual functionality that this stuff matters. BTS is available widely, so the tooling can be tested by anyone who's interested.

Allow people to record crashes in core dumps, allow them to look at histograms/spectrograms of BTS/PT traces, zoom in on actual traces, etc. - make it easier to handle this huge amount of data and visualize traces in other ways you find useful, etc.

Quoted 6 lines
Quoted 1 line

None of that is done right now via BTS so nobody uses it.

So I can make BTS appear as an "itrace" pmu similarly to PT. One question that comes to mind is should we then dispose of the old interface that's used for accessing BTS functionality or make it coexist with the new one.

So we could make the old ABI a CONFIG_PERF_EVENTS_COMPAT_X86_BTS kind of legacy option, turned off by default. That allows us its eventual future phasing out.

It all depends on how useful the new tooling becomes: if interesting things can be done with it via an obvious, powerful interface then people might start using it.

Thanks,

Ingo

#159 lore raw
Quoted 7 lines

So we could make the old ABI a CONFIG_PERF_EVENTS_COMPAT_X86_BTS kind of legacy option, turned off by default. That allows us its eventual future phasing out.

It all depends on how useful the new tooling becomes: if interesting things can be done with it via an obvious, powerful interface then people might start using it.

The thing to keep in mind is that BTS is really really slow.

It's unlikely it'll be ever all that useful no matter how the API looks like.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#160 lore raw
Quoted 4 lines · Andi Kleen
Quoted 4 lines

So we could make the old ABI a CONFIG_PERF_EVENTS_COMPAT_X86_BTS kind of legacy option, turned off by default. That allows us its eventual future phasing out.

Quoted 8 lines
Quoted 3 lines

It all depends on how useful the new tooling becomes: if interesting things can be done with it via an obvious, powerful interface then people might start using it.

The thing to keep in mind is that BTS is really really slow.

It's unlikely it'll be ever all that useful no matter how the API looks like.

You're right it's extremely slow. But it can still be relevant for debugging, at least for apps that don't do too much CPU bound stuffs.

My hope has always been that we can make a userspace function graph tracer out of its dumps. And I think we can, I'm pretty sure that would be a useful tool.

Even better would be to allow for some perf timehist which we can use to navigate through the execution flow including all branches. But that's quite sophisticated (although possibly very useful), still a function graph would be a good beginning.

Now if we find a faster replacement that can dump similar sources or even better if we can filter by branch type (call and ret is all we need for a function graph tracer) I'm all for it. But I agree with Ingo that some useful tooling should come along.

#161 lore raw
Quoted 2 lines

You're right it's extremely slow. But it can still be relevant for debugging, at least for apps that don't do too much CPU bound stuffs.

There are patches from Markus already for gdb to use it (using the old BTS perf interface). I'm not sure they have been merged into gdb mainline yet though.

Quoted 2 lines

My hope has always been that we can make a userspace function graph tracer out of its dumps. And I think we can, I'm pretty sure that would be a useful tool.

I wrote one, based on the __fentry__, like the kernel:
http://github.com/andikleen/ftracer

BTS has no timing information, so you could at best do a function tracer without timing.

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
#162 lore raw
Quoted 6 lines · Andi Kleen
Quoted 2 lines

You're right it's extremely slow. But it can still be relevant for debugging, at least for apps that don't do too much CPU bound stuffs.

There are patches from Markus already for gdb to use it (using the old BTS perf interface). I'm not sure they have been merged into gdb mainline yet though.

Ok.

Quoted 6 lines
Quoted 3 lines

My hope has always been that we can make a userspace function graph tracer out of its dumps. And I think we can, I'm pretty sure that would be a useful tool.

I wrote one, based on the __fentry__, like the kernel:
http://github.com/andikleen/ftracer

Sounds like nice stuff, but that implies building with the gcc option I think.

Quoted 3 lines

BTS has no timing information, so you could at best do a function tracer without timing.

Right, now although the function timing was the initial purpose of the function graph tracer, the graph itself proved to be much more useful :)

But yeah the timing is nice too when we chase hotspot though probably perf report deprecated it.

Quoted 4 lines

-Andi

Signature · 1 line
[email protected] -- Speaking for myself only
maintainer
#163 lore raw
In reply to Andi Kleen

Hi Andi,

Quoted 5 lines · Andi Kleen
Quoted 2 lines

My hope has always been that we can make a userspace function graph tracer out of its dumps. And I think we can, I'm pretty sure that would be a useful tool.

I wrote one, based on the __fentry__, like the kernel:
http://github.com/andikleen/ftracer

Oh, I'm writing a similar one too. I'll take a look at yours.

Thanks,
Namhyung

#164 lore raw
In reply to Alexander Shishkin

Em Wed, Dec 11, 2013 at 02:36:12PM +0200, Alexander Shishkin escreveu:

Quoted 7 lines

Hi,

This patchset adds support for Intel Processor Trace (PT) extension [1] of Intel Architecture that allows the capture of information about software execution flow, to the perf kernel and userspace infrastructure. We provide an abstraction for it called "itrace" for "instruction trace" ([2]).

Nice stuff! And big patchset, I'll start by cherry picking the tooling chunks that I find OK, will then comment on whatever is left.

While that happens I'm sure the new kernel bits will be discussed.

- Arnaldo