[RFC PATCH V2 9/9] perf evsel: Enable post-processing monotonic raw conversion by default

From: kan . liang
Date: Mon Feb 13 2023 - 14:08:36 EST


From: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>

The HW time is more accurate than the time recorded in the NMI handler.
Set the hw_time by default for the monotonic raw clock, and convert the
HW time to the monotonic raw clock in perf tool.

For the legacy kernel which doesn't support the attr, nothing is
changed. The monotonic raw clock is still from the time recorded in the
NMI handler.

Print the hw_time in perf_event_attr__fprintf

Signed-off-by: Kan Liang <kan.liang@xxxxxxxxxxxxxxx>
---
tools/perf/util/evsel.c | 11 ++++++++++-
tools/perf/util/evsel.h | 1 +
tools/perf/util/perf_event_attr_fprintf.c | 1 +
3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index 5e27ac2b9f9b..d182c12fd291 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -1349,6 +1349,8 @@ void evsel__config(struct evsel *evsel, struct record_opts *opts,
if (opts->use_clockid) {
attr->use_clockid = 1;
attr->clockid = opts->clockid;
+ if (opts->clockid == CLOCK_MONOTONIC_RAW)
+ attr->hw_time = 1;
}

if (evsel->precise_max)
@@ -1853,6 +1855,8 @@ static int __evsel__prepare_open(struct evsel *evsel, struct perf_cpu_map *cpus,

static void evsel__disable_missing_features(struct evsel *evsel)
{
+ if (perf_missing_features.hw_time)
+ evsel->core.attr.hw_time = 0;
if (perf_missing_features.read_lost)
evsel->core.attr.read_format &= ~PERF_FORMAT_LOST;
if (perf_missing_features.weight_struct) {
@@ -1906,7 +1910,12 @@ bool evsel__detect_missing_features(struct evsel *evsel)
* Must probe features in the order they were added to the
* perf_event_attr interface.
*/
- if (!perf_missing_features.read_lost &&
+ if (!perf_missing_features.hw_time &&
+ evsel->core.attr.hw_time) {
+ perf_missing_features.hw_time = true;
+ pr_debug2("switching off hw_time support\n");
+ return true;
+ } else if (!perf_missing_features.read_lost &&
(evsel->core.attr.read_format & PERF_FORMAT_LOST)) {
perf_missing_features.read_lost = true;
pr_debug2("switching off PERF_FORMAT_LOST support\n");
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index d1ef67852bda..c1d6fd40ea39 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -189,6 +189,7 @@ struct perf_missing_features {
bool code_page_size;
bool weight_struct;
bool read_lost;
+ bool hw_time;
};

extern struct perf_missing_features perf_missing_features;
diff --git a/tools/perf/util/perf_event_attr_fprintf.c b/tools/perf/util/perf_event_attr_fprintf.c
index 7e5e7b30510d..7b3669430b87 100644
--- a/tools/perf/util/perf_event_attr_fprintf.c
+++ b/tools/perf/util/perf_event_attr_fprintf.c
@@ -154,6 +154,7 @@ int perf_event_attr__fprintf(FILE *fp, struct perf_event_attr *attr,
PRINT_ATTRf(sample_max_stack, p_unsigned);
PRINT_ATTRf(aux_sample_size, p_unsigned);
PRINT_ATTRf(sig_data, p_unsigned);
+ PRINT_ATTRf(hw_time, p_unsigned);

return ret;
}
--
2.35.1