Perf tool: Support TSC for Arm64
Subsystems (2)
v3 cover Perf tool: Support TSC for Arm64
This patch set is to refactor the changes for the old patch set 'Perf
tool: Enable Arm arch timer counter and arm-spe's timestamp' [1].
After reviewed the old patch sets (thanks Wei Li and Al Grant), we think it's right way to consolidate TSC code and extend the TSC implementation to common code which can support both x86 and Arm64; so far, for x86 it needs to support cap_user_time_zero and for Arm64 it needs to support cap_user_time_short. For architecture specific code, every arch only needs to implement its own rdtsc() to read out timer's counter.
This patch set is to refactor TSC implementation and move TSC code from x86 folder to util/tsc.c, this allows all archs to reuse the code. And alse move the TSC testing from x86 folder to tests so can work as a common testing.
This patch set has been tested on Arm64 (DB410c) and x86_64. Both can pass the testing:
$ perf test list
[...]
68: Convert perf time to TSC
[...] $ perf test 68 -v
68: Convert perf time to TSC
--- start ---
test child forked, pid 10961
mmap size 528384B
1st event perf time 35715036563417 tsc 686221770989
rdtsc time 35715036649719 tsc 686221772647
2nd event perf time 35715036660448 tsc 686221772852
test child finished with 0
---- end ----
Convert perf time to TSC: OkChanges from v2:
* Refactored patch set to move TSC common code to util/tsc.c (Wei/Al);
* Moved TSC testing to perf/tests (Wei);
* Dropped Arm SPE timestamp patch so can have clear purpose and easier reviewing; will send Arm SPE timestamp as separate patch.
[1] https://lore.kernel.org/patchwork/cover/1285130/
Leo Yan (6):
perf tsc: Move out common functions from x86 perf tsc: Add rdtsc() for Arm64
perf tsc: Calculate timestamp with cap_user_time_short perf tsc: Support cap_user_time_short for event TIME_CONV perf tests tsc: Make tsc testing as a common testing perf tests tsc: Add checking helper is_supported()
tools/lib/perf/include/perf/event.h | 4 +
tools/perf/arch/arm64/util/Build | 1 +
tools/perf/arch/arm64/util/tsc.c | 14 ++++
tools/perf/arch/x86/include/arch-tests.h | 1 -
tools/perf/arch/x86/tests/Build | 1 -
tools/perf/arch/x86/tests/arch-tests.c | 4 -
tools/perf/arch/x86/util/tsc.c | 73 +----------------
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 5 ++
.../{arch/x86 => }/tests/perf-time-to-tsc.c | 13 +++
tools/perf/tests/tests.h | 2 +
tools/perf/util/jitdump.c | 14 ++--
tools/perf/util/synthetic-events.c | 8 --
tools/perf/util/tsc.c | 81 +++++++++++++++++++
tools/perf/util/tsc.h | 5 ++
15 files changed, 136 insertions(+), 91 deletions(-)
create mode 100644 tools/perf/arch/arm64/util/tsc.c
rename tools/perf/{arch/x86 => }/tests/perf-time-to-tsc.c (93%)Signature · 1 line
2.17.1
PATCH v3 1/6 perf tsc: Move out common functions from x86
Functions perf_read_tsc_conversion() and perf_event__synth_time_conv() should work as common functions rather than x86 specific, so move these two functions out from arch/x86 folder and place them into util/tsc.c.
Since the function perf_event__synth_time_conv() will be linked in util/tsc.c, remove its weak version.
--- tools/perf/arch/x86/util/tsc.c | 73 +----------------------------- tools/perf/util/synthetic-events.c | 8 ---- tools/perf/util/tsc.c | 71 +++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 80 deletions(-)
tools/perf/arch/x86/util/tsc.c +1 -72
@@ -1,45 +1,7 @@// SPDX-License-Identifier: GPL-2.0 -#include <stdbool.h> -#include <errno.h> - -#include <linux/stddef.h> -#include <linux/perf_event.h> -#include <linux/types.h> -#include <asm/barrier.h> -#include "../../../util/debug.h" -#include "../../../util/event.h" -#include "../../../util/synthetic-events.h" -#include "../../../util/tsc.h" - -int perf_read_tsc_conversion (const struct perf_event_mmap_page *pc, -struct perf_tsc_conversion *tc) -{ -bool cap_user_time_zero; -u32 seq; -int i =0 ; - -while (1 ) { - seq = pc->lock; -rmb (); - tc->time_mult = pc->time_mult; - tc->time_shift = pc->time_shift; - tc->time_zero = pc->time_zero; - cap_user_time_zero = pc->cap_user_time_zero; -rmb (); -if (pc->lock == seq && !(seq &1 )) -break ; -if (++i >10000 ) { -pr_debug ("failed to get perf_event_mmap_page lock\n" ); -return -EINVAL ; - } - } -if (!cap_user_time_zero) -return -EOPNOTSUPP ; - -return 0 ; -} +#include "../../../util/tsc.h" u64 rdtsc (void ) {
@@ -49,36 +11,3 @@ u64 rdtsc(void)return low | ((u64 )high) <<32 ; } - -int perf_event__synth_time_conv (const struct perf_event_mmap_page *pc, -struct perf_tool *tool, -perf_event__handler_t process, -struct machine *machine) -{ -union perf_event event = { - .time_conv = { - .header = { - .type =PERF_RECORD_TIME_CONV , - .size =sizeof (struct perf_record_time_conv ), - }, - }, - }; -struct perf_tsc_conversion tc; -int err; - -if (!pc) -return 0 ; - err =perf_read_tsc_conversion (pc, &tc); -if (err == -EOPNOTSUPP ) -return 0 ; -if (err) -return err; - -pr_debug2 ("Synthesizing TSC conversion information\n" ); - - event.time_conv.time_mult = tc.time_mult; - event.time_conv.time_shift = tc.time_shift; - event.time_conv.time_zero = tc.time_zero; - -return process (tool, &event,NULL , machine); -}
tools/perf/util/synthetic-events.c +0 -8
@@ -2006,14 +2006,6 @@ int perf_event__synthesize_stat_events(struct perf_stat_config *config, struct preturn 0 ; } -int __weak perf_event__synth_time_conv (const struct perf_event_mmap_page *pc__maybe_unused , -struct perf_tool *tool__maybe_unused , -perf_event__handler_t process__maybe_unused , -struct machine *machine__maybe_unused ) -{ -return 0 ; -} -extern const struct perf_header_feature_ops feat_ops[HEADER_LAST_FEATURE ]; int perf_event__synthesize_features(struct perf_tool *tool, struct perf_session *session,
tools/perf/util/tsc.c +71 -0
84 lines not on this page · raw message
Signature · 1 line
2.17.1
PATCH v3 2/6 perf tsc: Add rdtsc() for Arm64
In reply to Leo YanThe system register CNTVCT_EL0 can be used to retrieve the counter from user space. Add rdtsc() for Arm64.
--- tools/perf/arch/arm64/util/Build | 1 + tools/perf/arch/arm64/util/tsc.c | 14 ++++++++++++++ 2 files changed, 15 insertions(+) create mode 100644 tools/perf/arch/arm64/util/tsc.c
tools/perf/arch/arm64/util/Build +1 -0
@@ -1,6 +1,7 @@ perf-y += header.o perf-y += machine.o perf-y += perf_regs.o +perf-y += tsc.o perf-$(CONFIG_DWARF) += dwarf-regs.o perf-$(CONFIG_LOCAL_LIBUNWIND) += unwind-libunwind.o perf-$(CONFIG_LIBDW_DWARF_UNWIND) += unwind-libdw.o
tools/perf/arch/arm64/util/tsc.c new file +14 -0
@@ -0,0 +1,14 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/types.h> + +#include "../../../util/tsc.h" + +u64 rdtsc (void ) +{ +u64 val; + + asmvolatile ("mrs %0, cntvct_el0" :"=r" (val)); + +return val; +}
Signature · 1 line
2.17.1
[email protected]
maintainerRe: [PATCH v3 2/6] perf tsc: Add rdtsc() for Arm64
tools/perf/arch/arm64/util/tsc.c line 9
Quoted 6 lines · Leo Yan
The system register
CNTVCT_EL0can be used to retrieve the counter from user space. Addrdtsc()for Arm64.tools/perf/arch/arm64/util/tsc.c new file +3
-0+u64 rdtsc (void ) +{ +u64 val;
Would it make sense to put a comment in that this counter is/could-be 'short' ? Because unlike x86-TSC, this thing isn't architecturally specified to be 64bits wide.
Quoted 7 lines
tools/perf/arch/arm64/util/tsc.c new file +4
-0+ asmvolatile ("mrs %0, cntvct_el0" :"=r" (val)); + +return val; +}Signature · 1 line
2.17.1
Re: [PATCH v3 2/6] perf tsc: Add rdtsc() for Arm64
Hi Peter,
Quoted 11 lines · Peter Zijlstra
tools/perf/arch/arm64/util/tsc.c line 9
Quoted 6 lines · Leo Yan
The system register
CNTVCT_EL0can be used to retrieve the counter from user space. Addrdtsc()for Arm64.tools/perf/arch/arm64/util/tsc.c new file +3
-0+u64 rdtsc (void ) +{ +u64 val;Would it make sense to put a comment in that this counter is/could-be 'short' ? Because unlike x86-TSC, this thing isn't architecturally specified to be 64bits wide.
Will add below comments:
According to ARM DDI 0487F.c, from Armv8.0 to Armv8.5 inclusive, the system counter is at least 56 bits wide; from Armv8.6, the counter must be 64 bits wide. So the system counter could be less than 64 bits wide and it is attributed with the flag 'cap_user_time_short' is true.
Thanks for reviewing,
Leo
Quoted 8 lines
Quoted 8 lines
tools/perf/arch/arm64/util/tsc.c new file +4
-0+ asmvolatile ("mrs %0, cntvct_el0" :"=r" (val)); + +return val; +}Signature · 1 line
2.17.1
Arnaldo Carvalho de Melo
maintainerRe: [PATCH v3 2/6] perf tsc: Add rdtsc() for Arm64
Em Thu, Sep 03, 2020 at 10:23:54AM +0800, Leo Yan escreveu:
Quoted 15 lines
Hi Peter,
Quoted 11 lines · Peter Zijlstra
tools/perf/arch/arm64/util/tsc.c line 9
Quoted 6 lines · Leo Yan
The system register
CNTVCT_EL0can be used to retrieve the counter from user space. Addrdtsc()for Arm64.tools/perf/arch/arm64/util/tsc.c new file +3
-0+u64 rdtsc (void ) +{ +u64 val;Would it make sense to put a comment in that this counter is/could-be 'short' ? Because unlike x86-TSC, this thing isn't architecturally specified to be 64bits wide.
Quoted 6 lines
Will add below comments:
According to ARM DDI
0487F.c, from Armv8.0 to Armv8.5 inclusive, the system counter is at least 56 bits wide; from Armv8.6, the counter must be 64 bits wide. So the system counter could be less than 64 bits wide and it is attributed with the flag 'cap_user_time_short' is true.
Ok, so waiting for v4.
- Arnaldo
PATCH v3 3/6 perf tsc: Calculate timestamp with cap_user_time_short
In reply to Leo YanThe perf mmap'ed buffer contains the flag 'cap_user_time_short' and two extra fields 'time_cycles' and 'time_mask', perf tool needs to know them for handling the counter wrapping case.
This patch is to reads out the relevant parameters from the head of the first mmap'ed page and stores into the structure 'perf_tsc_conversion', if the flag 'cap_user_time_short' has been set, it will firstly calibrate cycle value for timestamp calculation.
--- tools/perf/util/tsc.c | 12 +++++++++--- tools/perf/util/tsc.h | 5 +++++ 2 files changed, 14 insertions(+), 3 deletions(-)
tools/perf/util/tsc.c +9 -3
@@ -28,6 +28,10 @@ u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc) {u64 quot, rem; +if (tc->cap_user_time_short) + cyc = tc->time_cycles + + ((cyc - tc->time_cycles) & tc->time_mask); + quot = cyc >> tc->time_shift; rem = cyc & (((u64 )1 << tc->time_shift) -1 ); return tc->time_zero + quot * tc->time_mult +
@@ -37,7 +41,6 @@ u64 tsc_to_perf_time(u64 cyc, struct perf_tsc_conversion *tc)int perf_read_tsc_conversion (const struct perf_event_mmap_page *pc,struct perf_tsc_conversion *tc) { -bool cap_user_time_zero;u32 seq;int i =0 ;
@@ -47,7 +50,10 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, tc->time_mult = pc->time_mult; tc->time_shift = pc->time_shift; tc->time_zero = pc->time_zero; - cap_user_time_zero = pc->cap_user_time_zero; + tc->time_cycles = pc->time_cycles; + tc->time_mask = pc->time_mask; + tc->cap_user_time_zero = pc->cap_user_time_zero; + tc->cap_user_time_short = pc->cap_user_time_short;rmb ();if (pc->lock == seq && !(seq &1 ))break ;
@@ -57,7 +63,7 @@ int perf_read_tsc_conversion(const struct perf_event_mmap_page *pc, } } -if (!cap_user_time_zero) +if (!tc->cap_user_time_zero)return -EOPNOTSUPP ;return 0 ;
tools/perf/util/tsc.h +5 -0
@@ -8,6 +8,11 @@ struct perf_tsc_conversion {u16 time_shift;u32 time_mult;u64 time_zero; +u64 time_cycles; +u64 time_mask; + +bool cap_user_time_zero; +bool cap_user_time_short; };struct perf_event_mmap_page ;
Signature · 1 line
2.17.1
PATCH v3 4/6 perf tsc: Support cap_user_time_short for event TIME_CONV
In reply to Leo YanThe synthesized event TIME_CONV doesn't contain the complete parameters for counters, this will lead to wrong conversion between counter cycles and timestamp.
This patch extends event TIME_CONV to record flags 'cap_user_time_zero' which is used to indicate the counter parameters are valid or not, if not will directly return 0 for timestamp calculation. And record the flag 'cap_user_time_short' and its relevant fields 'time_cycles' and 'time_mask' for cycle calibration.
--- tools/lib/perf/include/perf/event.h | 4 ++++ tools/perf/util/jitdump.c | 14 +++++++++----- tools/perf/util/tsc.c | 4 ++++ 3 files changed, 17 insertions(+), 5 deletions(-)
tools/lib/perf/include/perf/event.h +4 -0
@@ -324,6 +324,10 @@ struct perf_record_time_conv {__u64 time_shift;__u64 time_mult;__u64 time_zero; +__u64 time_cycles; +__u64 time_mask; +bool cap_user_time_zero; +bool cap_user_time_short; };struct perf_record_header_feature {
tools/perf/util/jitdump.c +9 -5
@@ -374,11 +374,15 @@ static uint64_t convert_timestamp(struct jit_buf_desc *jd, uint64_t timestamp)if (!jd->use_arch_timestamp)return timestamp; - tc.time_shift = jd->session->time_conv.time_shift; - tc.time_mult = jd->session->time_conv.time_mult; - tc.time_zero = jd->session->time_conv.time_zero; - -if (!tc.time_mult) + tc.time_shift = jd->session->time_conv.time_shift; + tc.time_mult = jd->session->time_conv.time_mult; + tc.time_zero = jd->session->time_conv.time_zero; + tc.time_cycles = jd->session->time_conv.time_cycles; + tc.time_mask = jd->session->time_conv.time_mask; + tc.cap_user_time_zero = jd->session->time_conv.cap_user_time_zero; + tc.cap_user_time_short = jd->session->time_conv.cap_user_time_short; + +if (!tc.cap_user_time_zero)return 0 ;return tsc_to_perf_time (timestamp, &tc);
tools/perf/util/tsc.c +4 -0
@@ -98,6 +98,10 @@ int perf_event__synth_time_conv(const struct perf_event_mmap_page *pc, event.time_conv.time_mult = tc.time_mult; event.time_conv.time_shift = tc.time_shift; event.time_conv.time_zero = tc.time_zero; + event.time_conv.time_cycles = tc.time_cycles; + event.time_conv.time_mask = tc.time_mask; + event.time_conv.cap_user_time_zero = tc.cap_user_time_zero; + event.time_conv.cap_user_time_short = tc.cap_user_time_short;return process (tool, &event,NULL , machine); }
Signature · 1 line
2.17.1
PATCH v3 5/6 perf tests tsc: Make tsc testing as a common testing
In reply to Leo Yanx86 arch provides the testing for conversion between tsc and perf time, the testing is located in x86 arch folder. Move this testing out from x86 arch folder and place it into the common testing folder, so allows to execute tsc testing on other architectures (e.g. Arm64).
---
tools/perf/arch/x86/include/arch-tests.h | 1 -
tools/perf/arch/x86/tests/Build | 1 -
tools/perf/arch/x86/tests/arch-tests.c | 4 ----
tools/perf/tests/Build | 1 +
tools/perf/tests/builtin-test.c | 4 ++++
tools/perf/{arch/x86 => }/tests/perf-time-to-tsc.c | 0
tools/perf/tests/tests.h | 1 +
7 files changed, 6 insertions(+), 6 deletions(-)
rename tools/perf/{arch/x86 => }/tests/perf-time-to-tsc.c (100%)tools/perf/arch/x86/include/arch-tests.h +0 -1
@@ -7,7 +7,6 @@ struct test;/* Tests */ int test__rdpmc (struct test *test__maybe_unused ,int subtest); -int test__perf_time_to_tsc (struct test *test__maybe_unused ,int subtest);int test__insn_x86 (struct test *test__maybe_unused ,int subtest);int test__intel_pt_pkt_decoder (struct test *test,int subtest);int test__bp_modify (struct test *test,int subtest);
tools/perf/arch/x86/tests/Build +0 -1
@@ -3,6 +3,5 @@ perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o perf-y += arch-tests.o perf-y += rdpmc.o -perf-y += perf-time-to-tsc.o perf-$(CONFIG_AUXTRACE) += insn-x86.o intel-pt-pkt-decoder-test.o perf-$(CONFIG_X86_64) += bp-modify.o
tools/perf/arch/x86/tests/arch-tests.c +0 -4
@@ -8,10 +8,6 @@ struct test arch_tests[] = { .desc = "x86 rdpmc", .func = test__rdpmc, }, - { - .desc = "Convert perf time to TSC", - .func = test__perf_time_to_tsc, - },#ifdef HAVE_DWARF_UNWIND_SUPPORT { .desc = "DWARF unwind",
tools/perf/tests/Build +1 -0
@@ -60,6 +60,7 @@ perf-y += api-io.o perf-y += demangle-java-test.o perf-y += pfm.o perf-y += parse-metric.o +perf-y += perf-time-to-tsc.o $(OUTPUT)tests/llvm-src-base.c: tests/bpf-script-example.c tests/Build $(call rule_mkdir)
tools/perf/tests/builtin-test.c +4 -0
@@ -341,6 +341,10 @@ static struct test generic_tests[] = { .desc = "Parse and process metrics", .func = test__parse_metric, }, + { + .desc = "Convert perf time to TSC", + .func = test__perf_time_to_tsc, + }, { .func = NULL, },
tools/perf/tests/perf-time-to-tsc.c renamed from tools/perf/arch/x86/tests/perf-time-to-tsc.c +0 -0
tools/perf/tests/tests.h +1 -0
@@ -122,6 +122,7 @@ int test__pfm(struct test *test, int subtest);const char *test__pfm_subtest_get_desc (int subtest);int test__pfm_subtest_get_nr (void );int test__parse_metric (struct test *test,int subtest); +int test__perf_time_to_tsc (struct test *test,int subtest);bool test__bp_signal_is_supported (void );bool test__bp_account_is_supported (void );
Signature · 1 line
2.17.1
PATCH v3 6/6 perf tests tsc: Add checking helper is_supported()
In reply to Leo YanSo far tsc is enabled on x86_64, i386 and Arm64 architectures, add checking helper to skip this testing for other architectures.
--- tools/perf/tests/builtin-test.c | 1 + tools/perf/tests/perf-time-to-tsc.c | 13 +++++++++++++ tools/perf/tests/tests.h | 1 + 3 files changed, 15 insertions(+)
tools/perf/tests/builtin-test.c +1 -0
@@ -344,6 +344,7 @@ static struct test generic_tests[] = { { .desc = "Convert perf time to TSC", .func = test__perf_time_to_tsc, + .is_supported = test__tsc_is_supported, }, { .func = NULL,
tools/perf/tests/perf-time-to-tsc.c +13 -0
@@ -171,3 +171,16 @@ int test__perf_time_to_tsc(struct test *test __maybe_unused, int subtest __maybeevlist__delete (evlist);return err; } + +bool test__tsc_is_supported (void ) +{ +/* +* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. +* Just enable the test for x86_64/i386 and Arm64 archs. +*/ +#if defined(__x86_64__) || defined(__i386__) || defined(__aarch64__) +return true; +#else +return false; +#endif +}
tools/perf/tests/tests.h +1 -0
@@ -127,6 +127,7 @@ int test__perf_time_to_tsc(struct test *test, int subtest);bool test__bp_signal_is_supported (void );bool test__bp_account_is_supported (void );bool test__wp_is_supported (void ); +bool test__tsc_is_supported (void );#if defined(__arm__) || defined(__aarch64__)#ifdef HAVE_DWARF_UNWIND_SUPPORT
Signature · 1 line
2.17.1