2021-04-03 07:25:12

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 0/7] perf arm-spe: Enable timestamp

As we know, the timestamp is important for AUX trace; it's mainly used
to correlate between perf events and AUX trace, allows to generate
events with time ordered manner. There have several good examples of
enabling timestamp for AUX trace (like Intel-pt, Intel-bts, etc).

Since the conversion between TSC and kernel timestamp has been supported
on Arm64, TSC is a naming convention from x86, but perf now has reused
it to support Arm arch timer counter.

This patch set is to enable timestamp for Arm SPE trace. It reads out
TSC parameters from mmap page and stores into auxtrace info structure;
the TSC parameters are used for conversion between timer counter and
kernel time and which is applied for Arm SPE samples.

This patch set can be clearly applied on perf/core branch with:

commit 6859bc0e78c6 ("perf stat: Improve readability of shadow stats")

Ths patch series has been tested on Hisilicon D06 platform.

After:

# perf script -F comm,time,cpu,pid,dso,ip,sym

perf 2408 [032] 168.680297: ffffbd1253690a3c perf_event_exec ([kernel.kallsyms])
perf 2408 [032] 168.680297: ffffbd1253690a3c perf_event_exec ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680317: ffffbd1253683f50 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680317: ffffbd1253683f50 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680319: ffffbd1253683f70 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680319: ffffbd1253683f70 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680367: ffffbd12539b03ec __arch_clear_user ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680375: ffffbd1253721440 kmem_cache_alloc ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680375: ffffbd1253721440 kmem_cache_alloc ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680375: ffffbd1253721440 kmem_cache_alloc ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680375: ffffbd1253721440 kmem_cache_alloc ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70 perf_iterate_ctx.constprop.0 ([kernel.kallsyms])

Changes from v1:
* Rebased patch series on the latest perf/core branch;
* Fixed the patch for dumping TSC parameters to support both the
older and new auxtrace info format.


Leo Yan (7):
perf arm-spe: Remove unused enum value ARM_SPE_PER_CPU_MMAPS
perf arm-spe: Store TSC parameters in auxtrace info
perf arm-spe: Dump TSC parameters
perf arm-spe: Convert event kernel time to counter value
perf arm-spe: Assign kernel time to synthesized event
perf arm-spe: Bail out if the trace is later than perf event
perf arm-spe: Don't wait for PERF_RECORD_EXIT event

tools/perf/arch/arm64/util/arm-spe.c | 23 +++++++
tools/perf/util/arm-spe.c | 89 +++++++++++++++++++++++-----
tools/perf/util/arm-spe.h | 7 ++-
3 files changed, 103 insertions(+), 16 deletions(-)

--
2.25.1


2021-04-03 07:25:12

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 2/7] perf arm-spe: Store TSC parameters in auxtrace info

The TSC parameters are used for conversion between arch timer counter
and kernel timestamp, this patch stores the parameters into the struct
perf_record_auxtrace_info, and it is saved in perf data file.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/arch/arm64/util/arm-spe.c | 23 +++++++++++++++++++++++
tools/perf/util/arm-spe.h | 6 ++++++
2 files changed, 29 insertions(+)

diff --git a/tools/perf/arch/arm64/util/arm-spe.c b/tools/perf/arch/arm64/util/arm-spe.c
index 414c8a5584b1..dd940cf16f49 100644
--- a/tools/perf/arch/arm64/util/arm-spe.c
+++ b/tools/perf/arch/arm64/util/arm-spe.c
@@ -15,7 +15,9 @@
#include "../../../util/event.h"
#include "../../../util/evsel.h"
#include "../../../util/evlist.h"
+#include "../../../util/mmap.h"
#include "../../../util/session.h"
+#include "../../../util/tsc.h"
#include <internal/lib.h> // page_size
#include "../../../util/pmu.h"
#include "../../../util/debug.h"
@@ -47,6 +49,9 @@ static int arm_spe_info_fill(struct auxtrace_record *itr,
struct arm_spe_recording *sper =
container_of(itr, struct arm_spe_recording, itr);
struct perf_pmu *arm_spe_pmu = sper->arm_spe_pmu;
+ struct perf_event_mmap_page *pc;
+ struct perf_tsc_conversion tc = { .time_mult = 0, };
+ int err;

if (priv_size != ARM_SPE_AUXTRACE_PRIV_SIZE)
return -EINVAL;
@@ -54,8 +59,26 @@ static int arm_spe_info_fill(struct auxtrace_record *itr,
if (!session->evlist->core.nr_mmaps)
return -EINVAL;

+ pc = session->evlist->mmap[0].core.base;
+ if (pc) {
+ err = perf_read_tsc_conversion(pc, &tc);
+ if (err) {
+ if (err != -EOPNOTSUPP)
+ return err;
+ }
+
+ if (!tc.time_mult)
+ ui__warning("Arm SPE: arch timer not available\n");
+ }
+
auxtrace_info->type = PERF_AUXTRACE_ARM_SPE;
auxtrace_info->priv[ARM_SPE_PMU_TYPE] = arm_spe_pmu->type;
+ auxtrace_info->priv[ARM_SPE_TIME_SHIFT] = tc.time_shift;
+ auxtrace_info->priv[ARM_SPE_TIME_MULT] = tc.time_mult;
+ auxtrace_info->priv[ARM_SPE_TIME_ZERO] = tc.time_zero;
+ auxtrace_info->priv[ARM_SPE_TIME_CYCLES] = tc.time_cycles;
+ auxtrace_info->priv[ARM_SPE_TIME_MASK] = tc.time_mask;
+ auxtrace_info->priv[ARM_SPE_CAP_USER_TIME_SHORT] = tc.cap_user_time_short;

return 0;
}
diff --git a/tools/perf/util/arm-spe.h b/tools/perf/util/arm-spe.h
index 105ce0ea0a01..5bf3e838d226 100644
--- a/tools/perf/util/arm-spe.h
+++ b/tools/perf/util/arm-spe.h
@@ -11,6 +11,12 @@

enum {
ARM_SPE_PMU_TYPE,
+ ARM_SPE_TIME_SHIFT,
+ ARM_SPE_TIME_MULT,
+ ARM_SPE_TIME_ZERO,
+ ARM_SPE_TIME_CYCLES,
+ ARM_SPE_TIME_MASK,
+ ARM_SPE_CAP_USER_TIME_SHORT,
ARM_SPE_AUXTRACE_PRIV_MAX,
};

--
2.25.1

2021-04-03 07:26:25

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 4/7] perf arm-spe: Convert event kernel time to counter value

When handle a perf event, Arm SPE decoder needs to decide if this perf
event is earlier or later than the samples from Arm SPE trace data; to
do comparision, it needs to use the same unit for the time.

This patch converts the event kernel time to arch timer's counter value,
thus it can be used to compare with counter value contained in Arm SPE
Timestamp packet.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/arm-spe.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 69ce3483d1af..4cf558b0218a 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -669,7 +669,7 @@ static int arm_spe_process_event(struct perf_session *session,
}

if (sample->time && (sample->time != (u64) -1))
- timestamp = sample->time;
+ timestamp = perf_time_to_tsc(sample->time, &spe->tc);
else
timestamp = 0;

--
2.25.1

2021-04-03 07:27:35

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 5/7] perf arm-spe: Assign kernel time to synthesized event

In current code, it assigns the arch timer counter to the synthesized
samples Arm SPE trace, thus the samples don't contain the kernel time
but only contain the raw counter value.

To fix the issue, this patch converts the timer counter to kernel time
and assigns it to sample timestamp.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/arm-spe.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 4cf558b0218a..80f5659e7f7e 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -234,7 +234,7 @@ static void arm_spe_prep_sample(struct arm_spe *spe,
struct arm_spe_record *record = &speq->decoder->record;

if (!spe->timeless_decoding)
- sample->time = speq->timestamp;
+ sample->time = tsc_to_perf_time(record->timestamp, &spe->tc);

sample->ip = record->from_ip;
sample->cpumode = arm_spe_cpumode(spe, sample->ip);
--
2.25.1

2021-04-03 07:27:41

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 6/7] perf arm-spe: Bail out if the trace is later than perf event

It's possible that record in Arm SPE trace is later than perf event and
vice versa. This asks to correlate the perf events and Arm SPE
synthesized events to be processed in the manner of correct timing.

To achieve the time ordering, this patch reverses the flow, it firstly
calls arm_spe_sample() and then calls arm_spe_decode(). By comparing
the timestamp value and detect the perf event is coming earlier than Arm
SPE trace data, it bails out from the decoding loop, the last record is
pushed into auxtrace stack and is deferred to generate sample. To track
the timestamp, everytime it updates timestamp for the latest record.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/arm-spe.c | 37 ++++++++++++++++++++++++++++++++++---
1 file changed, 34 insertions(+), 3 deletions(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 80f5659e7f7e..99a394c366e0 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -434,12 +434,36 @@ static int arm_spe_sample(struct arm_spe_queue *speq)
static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
{
struct arm_spe *spe = speq->spe;
+ struct arm_spe_record *record;
int ret;

if (!spe->kernel_start)
spe->kernel_start = machine__kernel_start(spe->machine);

while (1) {
+ /*
+ * The usual logic is firstly to decode the packets, and then
+ * based the record to synthesize sample; but here the flow is
+ * reversed: it calls arm_spe_sample() for synthesizing samples
+ * prior to arm_spe_decode().
+ *
+ * Two reasons for this code logic:
+ * 1. Firstly, when setup queue in arm_spe__setup_queue(), it
+ * has decoded trace data and generated a record, but the record
+ * is left to generate sample until run to here, so it's correct
+ * to synthesize sample for the left record.
+ * 2. After decoding trace data, it needs to compare the record
+ * timestamp with the coming perf event, if the record timestamp
+ * is later than the perf event, it needs bail out and pushs the
+ * record into auxtrace heap, thus the record can be deferred to
+ * synthesize sample until run to here at the next time; so this
+ * can correlate samples between Arm SPE trace data and other
+ * perf events with correct time ordering.
+ */
+ ret = arm_spe_sample(speq);
+ if (ret)
+ return ret;
+
ret = arm_spe_decode(speq->decoder);
if (!ret) {
pr_debug("No data or all data has been processed.\n");
@@ -453,10 +477,17 @@ static int arm_spe_run_decoder(struct arm_spe_queue *speq, u64 *timestamp)
if (ret < 0)
continue;

- ret = arm_spe_sample(speq);
- if (ret)
- return ret;
+ record = &speq->decoder->record;

+ /* Update timestamp for the last record */
+ if (record->timestamp > speq->timestamp)
+ speq->timestamp = record->timestamp;
+
+ /*
+ * If the timestamp of the queue is later than timestamp of the
+ * coming perf event, bail out so can allow the perf event to
+ * be processed ahead.
+ */
if (!spe->timeless_decoding && speq->timestamp >= *timestamp) {
*timestamp = speq->timestamp;
return 0;
--
2.25.1

2021-04-03 07:27:44

by Leo Yan

[permalink] [raw]
Subject: [PATCH v2 7/7] perf arm-spe: Don't wait for PERF_RECORD_EXIT event

When decode Arm SPE trace, it waits for PERF_RECORD_EXIT event (the last
perf event) for processing trace data, which is needless and even might
cause logic error, e.g. it might fail to correlate perf events with Arm
SPE events correctly.

So this patch removes the condition checking for PERF_RECORD_EXIT event.

Signed-off-by: Leo Yan <[email protected]>
---
tools/perf/util/arm-spe.c | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/tools/perf/util/arm-spe.c b/tools/perf/util/arm-spe.c
index 99a394c366e0..17dcad99912a 100644
--- a/tools/perf/util/arm-spe.c
+++ b/tools/perf/util/arm-spe.c
@@ -717,11 +717,7 @@ static int arm_spe_process_event(struct perf_session *session,
sample->time);
}
} else if (timestamp) {
- if (event->header.type == PERF_RECORD_EXIT) {
- err = arm_spe_process_queues(spe, timestamp);
- if (err)
- return err;
- }
+ err = arm_spe_process_queues(spe, timestamp);
}

return err;
--
2.25.1

2021-04-06 18:55:43

by Al Grant

[permalink] [raw]
Subject: RE: [PATCH v2 0/7] perf arm-spe: Enable timestamp



> -----Original Message-----
> From: Leo Yan <[email protected]>
> Sent: 03 April 2021 08:24
> To: Arnaldo Carvalho de Melo <[email protected]>; John Garry
> <[email protected]>; Will Deacon <[email protected]>; Mathieu Poirier
> <[email protected]>; James Clark <[email protected]>; Al Grant
> <[email protected]>; Peter Zijlstra <[email protected]>; Ingo Molnar
> <[email protected]>; Mark Rutland <[email protected]>; Alexander
> Shishkin <[email protected]>; Jiri Olsa <[email protected]>;
> Namhyung Kim <[email protected]>; Adrian Hunter
> <[email protected]>; [email protected]; linux-
> [email protected]
> Cc: [email protected]
> Subject: [PATCH v2 0/7] perf arm-spe: Enable timestamp
>
> As we know, the timestamp is important for AUX trace; it's mainly used to
> correlate between perf events and AUX trace, allows to generate events with
> time ordered manner. There have several good examples of enabling timestamp
> for AUX trace (like Intel-pt, Intel-bts, etc).
>
> Since the conversion between TSC and kernel timestamp has been supported on
> Arm64, TSC is a naming convention from x86, but perf now has reused it to
> support Arm arch timer counter.
>
> This patch set is to enable timestamp for Arm SPE trace. It reads out TSC
> parameters from mmap page and stores into auxtrace info structure;

Why not synthesize a PERF_RECORD_TIME_CONV - isn't that specifically to
capture the TSC parameters from the mmap page? If a generic mechanism
exists it would be better to use it, otherwise we'll have to do this again for
future trace formats.

perf_read_tsc_conversion and perf_event__synth_time_conv are currently
in arch/x86/util/tsc.c, but nothing in them is x86-specific and they could be
moved somewhere more generic.

Al


> the TSC
> parameters are used for conversion between timer counter and kernel time and
> which is applied for Arm SPE samples.
>
> This patch set can be clearly applied on perf/core branch with:
>
> commit 6859bc0e78c6 ("perf stat: Improve readability of shadow stats")
>
> Ths patch series has been tested on Hisilicon D06 platform.
>
> After:
>
> # perf script -F comm,time,cpu,pid,dso,ip,sym
>
> perf 2408 [032] 168.680297: ffffbd1253690a3c perf_event_exec
> ([kernel.kallsyms])
> perf 2408 [032] 168.680297: ffffbd1253690a3c perf_event_exec
> ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680317: ffffbd1253683f50
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680317: ffffbd1253683f50
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680319: ffffbd1253683f70
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680319: ffffbd1253683f70
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680367: ffffbd12539b03ec
> __arch_clear_user ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680375: ffffbd1253721440
> kmem_cache_alloc ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680375: ffffbd1253721440
> kmem_cache_alloc ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680375: ffffbd1253721440
> kmem_cache_alloc ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680375: ffffbd1253721440
> kmem_cache_alloc ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
> false_sharing.e 2408 [032] 168.680376: ffffbd1253683f70
> perf_iterate_ctx.constprop.0 ([kernel.kallsyms])
>
> Changes from v1:
> * Rebased patch series on the latest perf/core branch;
> * Fixed the patch for dumping TSC parameters to support both the
> older and new auxtrace info format.
>
>
> Leo Yan (7):
> perf arm-spe: Remove unused enum value ARM_SPE_PER_CPU_MMAPS
> perf arm-spe: Store TSC parameters in auxtrace info
> perf arm-spe: Dump TSC parameters
> perf arm-spe: Convert event kernel time to counter value
> perf arm-spe: Assign kernel time to synthesized event
> perf arm-spe: Bail out if the trace is later than perf event
> perf arm-spe: Don't wait for PERF_RECORD_EXIT event
>
> tools/perf/arch/arm64/util/arm-spe.c | 23 +++++++
> tools/perf/util/arm-spe.c | 89 +++++++++++++++++++++++-----
> tools/perf/util/arm-spe.h | 7 ++-
> 3 files changed, 103 insertions(+), 16 deletions(-)
>
> --
> 2.25.1

2021-04-07 21:00:40

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH v2 0/7] perf arm-spe: Enable timestamp

On 7/04/21 4:15 pm, Leo Yan wrote:
> Hi Al,
>
> On Tue, Apr 06, 2021 at 09:38:32AM +0000, Al Grant wrote:
>
> [...]
>
>>> This patch set is to enable timestamp for Arm SPE trace. It reads out TSC
>>> parameters from mmap page and stores into auxtrace info structure;
>>
>> Why not synthesize a PERF_RECORD_TIME_CONV - isn't that specifically to
>> capture the TSC parameters from the mmap page? If a generic mechanism
>> exists it would be better to use it, otherwise we'll have to do this again for
>> future trace formats.
>
> Good point! Actually "perf record" tool has synthesized event
> PERF_RECORD_TIME_CONV. This patch series is studying the
> implementation from Intel-PT, so the question is why the existed
> implementations (like Intel-PT, Intel-BTS) don't directly use
> PERF_RECORD_TIME_CONV for retriving TSC parameters.

PERF_RECORD_TIME_CONV was added later because the TSC information is
needed by jitdump.

2021-04-07 21:00:45

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH v2 0/7] perf arm-spe: Enable timestamp

On Wed, Apr 07, 2021 at 04:28:40PM +0300, Adrian Hunter wrote:
> On 7/04/21 4:15 pm, Leo Yan wrote:
> > Hi Al,
> >
> > On Tue, Apr 06, 2021 at 09:38:32AM +0000, Al Grant wrote:
> >
> > [...]
> >
> >>> This patch set is to enable timestamp for Arm SPE trace. It reads out TSC
> >>> parameters from mmap page and stores into auxtrace info structure;
> >>
> >> Why not synthesize a PERF_RECORD_TIME_CONV - isn't that specifically to
> >> capture the TSC parameters from the mmap page? If a generic mechanism
> >> exists it would be better to use it, otherwise we'll have to do this again for
> >> future trace formats.
> >
> > Good point! Actually "perf record" tool has synthesized event
> > PERF_RECORD_TIME_CONV. This patch series is studying the
> > implementation from Intel-PT, so the question is why the existed
> > implementations (like Intel-PT, Intel-BTS) don't directly use
> > PERF_RECORD_TIME_CONV for retriving TSC parameters.
>
> PERF_RECORD_TIME_CONV was added later because the TSC information is
> needed by jitdump.

Thanks for the info, Adrian.

If so, it's good for Arm SPE to use PERF_RECORD_TIME_CONV for TSC
parameters. Will spin patch series for this.

Leo

2021-04-07 21:01:15

by Leo Yan

[permalink] [raw]
Subject: Re: [PATCH v2 0/7] perf arm-spe: Enable timestamp

Hi Al,

On Tue, Apr 06, 2021 at 09:38:32AM +0000, Al Grant wrote:

[...]

> > This patch set is to enable timestamp for Arm SPE trace. It reads out TSC
> > parameters from mmap page and stores into auxtrace info structure;
>
> Why not synthesize a PERF_RECORD_TIME_CONV - isn't that specifically to
> capture the TSC parameters from the mmap page? If a generic mechanism
> exists it would be better to use it, otherwise we'll have to do this again for
> future trace formats.

Good point! Actually "perf record" tool has synthesized event
PERF_RECORD_TIME_CONV. This patch series is studying the
implementation from Intel-PT, so the question is why the existed
implementations (like Intel-PT, Intel-BTS) don't directly use
PERF_RECORD_TIME_CONV for retriving TSC parameters.

I agree using PERF_RECORD_TIME_CONV for TSC parameter is better than
extending auxtrace info. Will experiment for this.

> perf_read_tsc_conversion and perf_event__synth_time_conv are currently
> in arch/x86/util/tsc.c, but nothing in them is x86-specific and they could be
> moved somewhere more generic.

This is not true on the mainline kernel; these functions have been
moved into the file util/tsc.c.

Thanks for suggestions,
Leo