[PATCH V3 06/10] perf tools: Add new clock IDs to "perf time to TSC" test

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


The same "Convert perf time to TSC" test can be used with new clock IDs
CLOCK_PERF_HW_CLOCK and CLOCK_PERF_HW_CLOCK_NS.

Signed-off-by: Adrian Hunter <adrian.hunter@xxxxxxxxx>
---
tools/perf/tests/perf-time-to-tsc.c | 42 ++++++++++++++++++++++-------
1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/tools/perf/tests/perf-time-to-tsc.c b/tools/perf/tests/perf-time-to-tsc.c
index d12d0ad81801..62840ec98cea 100644
--- a/tools/perf/tests/perf-time-to-tsc.c
+++ b/tools/perf/tests/perf-time-to-tsc.c
@@ -22,6 +22,8 @@
#include "tests.h"
#include "pmu.h"
#include "pmu-hybrid.h"
+#include "clockid.h"
+#include "perf_api_probe.h"

/*
* Except x86_64/i386 and Arm64, other archs don't support TSC in perf. Just
@@ -47,15 +49,7 @@
} \
}

-/**
- * test__perf_time_to_tsc - test converting perf time to TSC.
- *
- * This function implements a test that checks that the conversion of perf time
- * to and from TSC is consistent with the order of events. If the test passes
- * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
- * supported then then the test passes but " (not supported)" is printed.
- */
-static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
+static int perf_time_to_tsc_test(bool use_clockid, s32 clockid)
{
struct record_opts opts = {
.mmap_pages = UINT_MAX,
@@ -104,6 +98,8 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su
evsel->core.attr.comm = 1;
evsel->core.attr.disabled = 1;
evsel->core.attr.enable_on_exec = 0;
+ evsel->core.attr.use_clockid = use_clockid;
+ evsel->core.attr.clockid = clockid;

/*
* For hybrid "cycles:u", it creates two events.
@@ -200,4 +196,32 @@ static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused, int su
return err;
}

+/**
+ * test__perf_time_to_tsc - test converting perf time to TSC.
+ *
+ * This function implements a test that checks that the conversion of perf time
+ * to and from TSC is consistent with the order of events. If the test passes
+ * %0 is returned, otherwise %-1 is returned. If TSC conversion is not
+ * supported then the test passes but " (not supported)" is printed.
+ */
+static int test__perf_time_to_tsc(struct test_suite *test __maybe_unused,
+ int subtest __maybe_unused)
+{
+ int err;
+
+ err = perf_time_to_tsc_test(false, 0);
+
+ if (!err && perf_can_perf_clock_hw_clock()) {
+ pr_debug("Testing CLOCK_PERF_HW_CLOCK\n");
+ err = perf_time_to_tsc_test(true, CLOCK_PERF_HW_CLOCK);
+ }
+
+ if (!err && perf_can_perf_clock_hw_clock_ns()) {
+ pr_debug("Testing CLOCK_PERF_HW_CLOCK_NS\n");
+ err = perf_time_to_tsc_test(true, CLOCK_PERF_HW_CLOCK_NS);
+ }
+
+ return err;
+}
+
DEFINE_SUITE("Convert perf time to TSC", perf_time_to_tsc);
--
2.25.1