[PATCH V3 08/10] perf intel-pt: Add support for new clock IDs

From: Adrian Hunter
Date: Mon Mar 07 2022 - 03:54:12 EST


Add support for new clock IDs CLOCK_PERF_HW_CLOCK and
CLOCK_PERF_HW_CLOCK_NS. Mainly this means also keeping TSC conversion
information for CLOCK_PERF_HW_CLOCK_NS when CLOCK_PERF_HW_CLOCK is
being used, so that conversions from nanoseconds can still be done when
the perf event clock is TSC.

Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
---
tools/perf/arch/x86/util/intel-pt.c | 37 ++++++++++++++++++++++++++---
tools/perf/util/intel-pt.c | 24 +++++++++++++++----
tools/perf/util/intel-pt.h | 2 +-
3 files changed, 55 insertions(+), 8 deletions(-)

diff --git a/tools/perf/arch/x86/util/intel-pt.c b/tools/perf/arch/x86/util/intel-pt.c
index 8c31578d6f4a..5bc05b8dd7f0 100644
--- a/tools/perf/arch/x86/util/intel-pt.c
+++ b/tools/perf/arch/x86/util/intel-pt.c
@@ -31,6 +31,7 @@
#include "../../../util/tsc.h"
#include <internal/lib.h> // page_size
#include "../../../util/intel-pt.h"
+#include "../../../util/clockid.h"

#define KiB(x) ((x) * 1024)
#define MiB(x) ((x) * 1024 * 1024)
@@ -290,6 +291,20 @@ static const char *intel_pt_find_filter(struct evlist *evlist,
return NULL;
}

+static bool intel_pt_clockid(struct evlist *evlist, struct perf_pmu *intel_pt_pmu, s32 clockid)
+{
+ struct evsel *evsel;
+
+ evlist__for_each_entry(evlist, evsel) {
+ if (evsel->core.attr.type == intel_pt_pmu->type &&
+ evsel->core.attr.use_clockid &&
+ evsel->core.attr.clockid == clockid)
+ return true;
+ }
+
+ return false;
+}
+
static size_t intel_pt_filter_bytes(const char *filter)
{
size_t len = filter ? strlen(filter) : 0;
@@ -304,9 +319,11 @@ intel_pt_info_priv_size(struct auxtrace_record *itr, struct evlist *evlist)
container_of(itr, struct intel_pt_recording, itr);
const char *filter = intel_pt_find_filter(evlist, ptr->intel_pt_pmu);

- ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_MAX * sizeof(u64)) +
+ ptr->priv_size = (INTEL_PT_AUXTRACE_PRIV_FIXED * sizeof(u64)) +
intel_pt_filter_bytes(filter);
ptr->priv_size += sizeof(u64); /* Cap Event Trace */
+ ptr->priv_size += sizeof(u64); /* ns Time Shift */
+ ptr->priv_size += sizeof(u64); /* ns Time Multiplier */

return ptr->priv_size;
}
@@ -414,6 +431,18 @@ static int intel_pt_info_fill(struct auxtrace_record *itr,

*info++ = event_trace;

+ if (intel_pt_clockid(session->evlist, ptr->intel_pt_pmu, CLOCK_PERF_HW_CLOCK)) {
+ struct perf_tsc_conversion ns_tc;
+
+ if (perf_read_tsc_conv_for_clockid(CLOCK_PERF_HW_CLOCK_NS, &ns_tc))
+ return -EINVAL;
+ *info++ = ns_tc.time_shift;
+ *info++ = ns_tc.time_mult;
+ } else {
+ *info++ = tc.time_shift;
+ *info++ = tc.time_mult;
+ }
+
return 0;
}

@@ -664,8 +693,10 @@ static int intel_pt_recording_options(struct auxtrace_record *itr,
return -EINVAL;
}

- if (opts->use_clockid) {
- pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME "\n");
+ if (opts->use_clockid && opts->clockid != CLOCK_PERF_HW_CLOCK_NS &&
+ opts->clockid != CLOCK_PERF_HW_CLOCK) {
+ pr_err("Cannot use clockid (-k option) with " INTEL_PT_PMU_NAME
+ " except CLOCK_PERF_HW_CLOCK_NS and CLOCK_PERF_HW_CLOCK\n");
return -EINVAL;
}

diff --git a/tools/perf/util/intel-pt.c b/tools/perf/util/intel-pt.c
index ec43d364d0de..677ad0774464 100644
--- a/tools/perf/util/intel-pt.c
+++ b/tools/perf/util/intel-pt.c
@@ -89,6 +89,8 @@ struct intel_pt {

struct perf_tsc_conversion tc;
bool cap_user_time_zero;
+ u16 ns_time_shift;
+ u32 ns_time_mult;

struct itrace_synth_opts synth_opts;

@@ -1100,10 +1102,10 @@ static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
{
u64 quot, rem;

- quot = ns / pt->tc.time_mult;
- rem = ns % pt->tc.time_mult;
- return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
- pt->tc.time_mult;
+ quot = ns / pt->ns_time_mult;
+ rem = ns % pt->ns_time_mult;
+ return (quot << pt->ns_time_shift) + (rem << pt->ns_time_shift) /
+ pt->ns_time_mult;
}

static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt)
@@ -3987,6 +3989,20 @@ int intel_pt_process_auxtrace_info(union perf_event *event,
pt->cap_event_trace);
}

+ if ((void *)info < info_end) {
+ pt->ns_time_shift = *info++;
+ pt->ns_time_mult = *info++;
+ if (dump_trace) {
+ fprintf(stdout, " ns Time Shift %d\n", pt->ns_time_shift);
+ fprintf(stdout, " ns Time Multiplier %d\n", pt->ns_time_mult);
+ }
+ } else {
+ pt->ns_time_shift = pt->tc.time_shift;
+ pt->ns_time_mult = pt->tc.time_mult;
+ }
+ if (!pt->ns_time_mult)
+ pt->ns_time_mult = 1;
+
pt->timeless_decoding = intel_pt_timeless_decoding(pt);
if (pt->timeless_decoding && !pt->tc.time_mult)
pt->tc.time_mult = 1;
diff --git a/tools/perf/util/intel-pt.h b/tools/perf/util/intel-pt.h
index c7d6068e3a6b..a2c4474641c0 100644
--- a/tools/perf/util/intel-pt.h
+++ b/tools/perf/util/intel-pt.h
@@ -27,7 +27,7 @@ enum {
INTEL_PT_CYC_BIT,
INTEL_PT_MAX_NONTURBO_RATIO,
INTEL_PT_FILTER_STR_LEN,
- INTEL_PT_AUXTRACE_PRIV_MAX,
+ INTEL_PT_AUXTRACE_PRIV_FIXED,
};

struct auxtrace_record;
--
2.25.1