Skip to contentSkip to conversation

Tidy user rdpmc documentation and testing

PATCH Superseded Ian Rogers 3 participants 10 messages Jun 9, 2022 – Jul 19, 2022 perf events

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

v2 cover Tidy user rdpmc documentation and testing

libperf's perf_mmap__read_self and the addition of arm64 support mean that the perf_event.h and the rdpmc perf test have become stale. Refresh the documentation in perf_event.h, remove the x86 rdpmc test and port the libperf test as a non-architecture specific test.

Address sanitizer testing showed libperf leaking fds when the perf_event_open failed, add error paths to handle this.

A comment is added to perf_event.h to avoid a divide by zero when scaling counts if the running time is 0. This was previously discussed in this thread:
https://lore.kernel.org/lkml/CAP-5=fVRdqvswtyQMg5cB+ntTGda+SAYskjTQednEH-AeZo13g@mail.gmail.com/

v2. Alters the skip in test_stat_user_read for open to always be a
skip as perf_event_open may fail with EACCES (permissions), ENOSYS (not supported) and ENOENT (hypervisor). Adds Rob Herring's acked-by on patch 3.

Ian Rogers (4):
libperf evsel: Open shouldn't leak fd on failure
perf: Align user space counter reading with code perf test: Remove x86 rdpmc test
perf test: Add user space counter reading tests

 include/uapi/linux/perf_event.h        |  32 +++--
 tools/include/uapi/linux/perf_event.h  |  32 +++--
 tools/lib/perf/evsel.c                 |  17 ++-
 tools/perf/arch/x86/tests/Build        |   1 -
 tools/perf/arch/x86/tests/arch-tests.c |   2 -
 tools/perf/arch/x86/tests/rdpmc.c      | 182 -------------------------
 tools/perf/tests/mmap-basic.c          | 127 ++++++++++++++++-
 7 files changed, 176 insertions(+), 217 deletions(-)
 delete mode 100644 tools/perf/arch/x86/tests/rdpmc.c
Signature · 1 line
2.36.1.255.ge46751e96f-goog
author reviewer
#1 lore raw

PATCH v2 1/4 libperf evsel: Open shouldn't leak fd on failure

If the perf_event_open fails the fd is opened but the fd is only freed by closing (not by delete). Typically when an open fails you don't call close and so this results in a memory leak. To avoid this, add a close when open fails.

---
 tools/lib/perf/evsel.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)
tools/lib/perf/evsel.c +12 -5
index c1d58673f6ef..952f3520d5c2 100644
@@ -149,23 +149,30 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
 			int fd, group_fd, *evsel_fd;
 
 			evsel_fd = FD(evsel, idx, thread);
-			if (evsel_fd == NULL)
-				return -EINVAL;
+			if (evsel_fd == NULL) {
+				err = -EINVAL;
+				goto out;
+			}
 
 			err = get_group_fd(evsel, idx, thread, &group_fd);
 			if (err < 0)
-				return err;
+				goto out;
 
 			fd = sys_perf_event_open(&evsel->attr,
 						 threads->map[thread].pid,
 						 cpu, group_fd, 0);
 
-			if (fd < 0)
-				return -errno;
+			if (fd < 0) {
+				err = -errno;
+				goto out;
+			}
 
 			*evsel_fd = fd;
 		}
 	}
+out:
+	if (err)
+		perf_evsel__close(evsel);
 
 	return err;
 }
Signature · 1 line
2.36.1.255.ge46751e96f-goog
Reviewed-by on v2 1/4
#2 lore raw

Re: [PATCH v2 1/4] libperf evsel: Open shouldn't leak fd on failure

Quoted 9 lines · Ian Rogers

If the perf_event_open fails the fd is opened but the fd is only freed by closing (not by delete). Typically when an open fails you don't call close and so this results in a memory leak. To avoid this, add a close when open fails.

---
 tools/lib/perf/evsel.c | 17 ++++++++++++-----
 1 file changed, 12 insertions(+), 5 deletions(-)

Patch looks fine to me

Quoted 41 lines
tools/lib/perf/evsel.c +12 -5
index c1d58673f6ef..952f3520d5c2 100644
@@ -149,23 +149,30 @@ int perf_evsel__open(struct perf_evsel *evsel, struct perf_cpu_map *cpus,
 			int fd, group_fd, *evsel_fd;
 
 			evsel_fd = FD(evsel, idx, thread);
-			if (evsel_fd == NULL)
-				return -EINVAL;
+			if (evsel_fd == NULL) {
+				err = -EINVAL;
+				goto out;
+			}
 
 			err = get_group_fd(evsel, idx, thread, &group_fd);
 			if (err < 0)
-				return err;
+				goto out;
 
 			fd = sys_perf_event_open(&evsel->attr,
 						 threads->map[thread].pid,
 						 cpu, group_fd, 0);
 
-			if (fd < 0)
-				return -errno;
+			if (fd < 0) {
+				err = -errno;
+				goto out;
+			}
 
 			*evsel_fd = fd;
 		}
 	}
+out:
+	if (err)
+		perf_evsel__close(evsel);
 
 	return err;
 }
author reviewer
#3 lore raw

PATCH v2 2/4 perf: Align user space counter reading with code

In reply to Ian Rogers

Align the user space counter reading documentation with the code in perf_mmap__read_self. Previously the documentation was based on the perf rdpmc test, but now general purpose code is provided by libperf.

---
 include/uapi/linux/perf_event.h       | 32 ++++++++++++++++-----------
 tools/include/uapi/linux/perf_event.h | 32 ++++++++++++++++-----------
 2 files changed, 38 insertions(+), 26 deletions(-)
include/uapi/linux/perf_event.h +19 -13
index d37629dbad72..3b84e0ad0723 100644
@@ -538,9 +538,13 @@ struct perf_event_mmap_page {
 	 *
 	 *     if (pc->cap_usr_time && enabled != running) {
 	 *       cyc = rdtsc();
-	 *       time_offset = pc->time_offset;
 	 *       time_mult   = pc->time_mult;
 	 *       time_shift  = pc->time_shift;
+	 *       time_offset = pc->time_offset;
+	 *       if (pc->cap_user_time_short) {
+	 *         time_cycles = pc->time_cycles;
+	 *         time_mask = pc->time_mask;
+	 *       }
 	 *     }
 	 *
 	 *     index = pc->index;
@@ -548,6 +552,9 @@ struct perf_event_mmap_page {
 	 *     if (pc->cap_user_rdpmc && index) {
 	 *       width = pc->pmc_width;
 	 *       pmc = rdpmc(index - 1);
+	 *       pmc <<= 64 - width;
+	 *       pmc >>= 64 - width;
+	 *       count += pmc;
 	 *     }
 	 *
 	 *     barrier();
@@ -590,25 +597,24 @@ struct perf_event_mmap_page {
 	 * If cap_usr_time the below fields can be used to compute the time
 	 * delta since time_enabled (in ns) using rdtsc or similar.
 	 *
-	 *   u64 quot, rem;
-	 *   u64 delta;
-	 *
-	 *   quot = (cyc >> time_shift);
-	 *   rem = cyc & (((u64)1 << time_shift) - 1);
-	 *   delta = time_offset + quot * time_mult +
-	 *              ((rem * time_mult) >> time_shift);
+	 *   cyc = time_cycles + ((cyc - time_cycles) & time_mask);
+	 *   delta = time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift);
 	 *
 	 * Where time_offset,time_mult,time_shift and cyc are read in the
 	 * seqcount loop described above. This delta can then be added to
-	 * enabled and possible running (if index), improving the scaling:
+	 * enabled and possible running (if index) to improve the scaling. Due
+	 * to event multiplexing, running maybe zero and so care is needed to
+	 * avoid division by zero.
 	 *
 	 *   enabled += delta;
-	 *   if (index)
+	 *   if (idx)
 	 *     running += delta;
 	 *
-	 *   quot = count / running;
-	 *   rem  = count % running;
-	 *   count = quot * enabled + (rem * enabled) / running;
+	 *   if (running != 0) {
+	 *     quot = count / running;
+	 *     rem  = count % running;
+	 *     count = quot * enabled + (rem * enabled) / running;
+	 *   }
 	 */
 	__u16	time_shift;
 	__u32	time_mult;
tools/include/uapi/linux/perf_event.h +19 -13
index d37629dbad72..3b84e0ad0723 100644
@@ -538,9 +538,13 @@ struct perf_event_mmap_page {
 	 *
 	 *     if (pc->cap_usr_time && enabled != running) {
 	 *       cyc = rdtsc();
-	 *       time_offset = pc->time_offset;
 	 *       time_mult   = pc->time_mult;
 	 *       time_shift  = pc->time_shift;
+	 *       time_offset = pc->time_offset;
+	 *       if (pc->cap_user_time_short) {
+	 *         time_cycles = pc->time_cycles;
+	 *         time_mask = pc->time_mask;
+	 *       }
 	 *     }
 	 *
 	 *     index = pc->index;
@@ -548,6 +552,9 @@ struct perf_event_mmap_page {
 	 *     if (pc->cap_user_rdpmc && index) {
 	 *       width = pc->pmc_width;
 	 *       pmc = rdpmc(index - 1);
+	 *       pmc <<= 64 - width;
+	 *       pmc >>= 64 - width;
+	 *       count += pmc;
 	 *     }
 	 *
 	 *     barrier();
@@ -590,25 +597,24 @@ struct perf_event_mmap_page {
 	 * If cap_usr_time the below fields can be used to compute the time
 	 * delta since time_enabled (in ns) using rdtsc or similar.
 	 *
-	 *   u64 quot, rem;
-	 *   u64 delta;
-	 *
-	 *   quot = (cyc >> time_shift);
-	 *   rem = cyc & (((u64)1 << time_shift) - 1);
-	 *   delta = time_offset + quot * time_mult +
-	 *              ((rem * time_mult) >> time_shift);
+	 *   cyc = time_cycles + ((cyc - time_cycles) & time_mask);
+	 *   delta = time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift);
 	 *
 	 * Where time_offset,time_mult,time_shift and cyc are read in the
 	 * seqcount loop described above. This delta can then be added to
-	 * enabled and possible running (if index), improving the scaling:
+	 * enabled and possible running (if index) to improve the scaling. Due
+	 * to event multiplexing, running maybe zero and so care is needed to
+	 * avoid division by zero.
 	 *
 	 *   enabled += delta;
-	 *   if (index)
+	 *   if (idx)
 	 *     running += delta;
 	 *
-	 *   quot = count / running;
-	 *   rem  = count % running;
-	 *   count = quot * enabled + (rem * enabled) / running;
+	 *   if (running != 0) {
+	 *     quot = count / running;
+	 *     rem  = count % running;
+	 *     count = quot * enabled + (rem * enabled) / running;
+	 *   }
 	 */
 	__u16	time_shift;
 	__u32	time_mult;
Signature · 1 line
2.36.1.255.ge46751e96f-goog
#4 lore raw

Re: [PATCH v2 2/4] perf: Align user space counter reading with code

Quoted 43 lines · Ian Rogers

Align the user space counter reading documentation with the code in perf_mmap__read_self. Previously the documentation was based on the perf rdpmc test, but now general purpose code is provided by libperf.

---
 include/uapi/linux/perf_event.h       | 32 ++++++++++++++++-----------
 tools/include/uapi/linux/perf_event.h | 32 ++++++++++++++++-----------
 2 files changed, 38 insertions(+), 26 deletions(-)
include/uapi/linux/perf_event.h +8 -1
index d37629dbad72..3b84e0ad0723 100644
@@ -538,9 +538,13 @@ struct perf_event_mmap_page {
         *
         *     if (pc->cap_usr_time && enabled != running) {
         *       cyc = rdtsc();
-        *       time_offset = pc->time_offset;
         *       time_mult   = pc->time_mult;
         *       time_shift  = pc->time_shift;
+        *       time_offset = pc->time_offset;
+        *       if (pc->cap_user_time_short) {
+        *         time_cycles = pc->time_cycles;
+        *         time_mask = pc->time_mask;
+        *       }
         *     }
         *
         *     index = pc->index;
@@ -548,6 +552,9 @@ struct perf_event_mmap_page {
         *     if (pc->cap_user_rdpmc && index) {
         *       width = pc->pmc_width;
         *       pmc = rdpmc(index - 1);
+        *       pmc <<= 64 - width;
+        *       pmc >>= 64 - width;
+        *       count += pmc;
         *     }
         *
         *     barrier();
@@ -590,25 +597,24 @@ struct perf_event_mmap_page {
         * If cap_usr_time the below fields can be used to compute the time
         * delta since time_enabled (in ns) using rdtsc or similar.

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 601

Quoted 10 lines
include/uapi/linux/perf_event.h +2 -7
         *
-        *   u64 quot, rem;
-        *   u64 delta;
-        *
-        *   quot = (cyc >> time_shift);
-        *   rem = cyc & (((u64)1 << time_shift) - 1);
-        *   delta = time_offset + quot * time_mult +
-        *              ((rem * time_mult) >> time_shift);
+        *   cyc = time_cycles + ((cyc - time_cycles) & time_mask);
+        *   delta = time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift);

I still think this chunk should stay as-is because mul_u64_u32_shr isn't defined here. At least a comment as to what the C version does:

/* time_offset + (u64)(((unsigned __int128)cyc * time_mult) >> time_shift) */

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 606

Quoted 6 lines
include/uapi/linux/perf_event.h +2 -1
         *
         * Where time_offset,time_mult,time_shift and cyc are read in the
         * seqcount loop described above. This delta can then be added to
-        * enabled and possible running (if index), improving the scaling:
+        * enabled and possible running (if index) to improve the scaling. Due
+        * to event multiplexing, running maybe zero and so care is needed to

s/maybe/may be/

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 610

Quoted 5 lines
include/uapi/linux/perf_event.h +2 -1
+        * avoid division by zero.
         *
         *   enabled += delta;
-        *   if (index)
+        *   if (idx)

This should remain 'index'.

Quoted 13 lines
include/uapi/linux/perf_event.h +5 -3
         *     running += delta;
         *
-        *   quot = count / running;
-        *   rem  = count % running;
-        *   count = quot * enabled + (rem * enabled) / running;
+        *   if (running != 0) {
+        *     quot = count / running;
+        *     rem  = count % running;
+        *     count = quot * enabled + (rem * enabled) / running;
+        *   }
         */
        __u16   time_shift;
        __u32   time_mult;
author reviewer
#5 lore raw

Re: [PATCH v2 2/4] perf: Align user space counter reading with code

Quoted 70 lines · Rob Herring
Quoted 43 lines · Ian Rogers

Align the user space counter reading documentation with the code in perf_mmap__read_self. Previously the documentation was based on the perf rdpmc test, but now general purpose code is provided by libperf.

---
 include/uapi/linux/perf_event.h       | 32 ++++++++++++++++-----------
 tools/include/uapi/linux/perf_event.h | 32 ++++++++++++++++-----------
 2 files changed, 38 insertions(+), 26 deletions(-)
include/uapi/linux/perf_event.h +8 -1
index d37629dbad72..3b84e0ad0723 100644
@@ -538,9 +538,13 @@ struct perf_event_mmap_page {
         *
         *     if (pc->cap_usr_time && enabled != running) {
         *       cyc = rdtsc();
-        *       time_offset = pc->time_offset;
         *       time_mult   = pc->time_mult;
         *       time_shift  = pc->time_shift;
+        *       time_offset = pc->time_offset;
+        *       if (pc->cap_user_time_short) {
+        *         time_cycles = pc->time_cycles;
+        *         time_mask = pc->time_mask;
+        *       }
         *     }
         *
         *     index = pc->index;
@@ -548,6 +552,9 @@ struct perf_event_mmap_page {
         *     if (pc->cap_user_rdpmc && index) {
         *       width = pc->pmc_width;
         *       pmc = rdpmc(index - 1);
+        *       pmc <<= 64 - width;
+        *       pmc >>= 64 - width;
+        *       count += pmc;
         *     }
         *
         *     barrier();
@@ -590,25 +597,24 @@ struct perf_event_mmap_page {
         * If cap_usr_time the below fields can be used to compute the time
         * delta since time_enabled (in ns) using rdtsc or similar.

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 601

Quoted 10 lines
include/uapi/linux/perf_event.h +2 -7
         *
-        *   u64 quot, rem;
-        *   u64 delta;
-        *
-        *   quot = (cyc >> time_shift);
-        *   rem = cyc & (((u64)1 << time_shift) - 1);
-        *   delta = time_offset + quot * time_mult +
-        *              ((rem * time_mult) >> time_shift);
+        *   cyc = time_cycles + ((cyc - time_cycles) & time_mask);
+        *   delta = time_offset + mul_u64_u32_shr(cyc, time_mult, time_shift);

I still think this chunk should stay as-is because mul_u64_u32_shr isn't defined here. At least a comment as to what the C version does:

/* time_offset + (u64)(((unsigned __int128)cyc * time_mult) >> time_shift) */

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 606

Quoted 6 lines
include/uapi/linux/perf_event.h +2 -1
         *
         * Where time_offset,time_mult,time_shift and cyc are read in the
         * seqcount loop described above. This delta can then be added to
-        * enabled and possible running (if index), improving the scaling:
+        * enabled and possible running (if index) to improve the scaling. Due
+        * to event multiplexing, running maybe zero and so care is needed to

s/maybe/may be/

Quoted 7 lines

include/uapi/linux/perf_event.h struct perf_event_mmap_page { · line 610

Quoted 5 lines
include/uapi/linux/perf_event.h +2 -1
+        * avoid division by zero.
         *
         *   enabled += delta;
-        *   if (index)
+        *   if (idx)

This should remain 'index'.

Thanks, I've tried to incorporate all of this into v3:
https://lore.kernel.org/lkml/[email protected]/

Ian

Quoted 13 lines
Quoted 13 lines
include/uapi/linux/perf_event.h +5 -3
         *     running += delta;
         *
-        *   quot = count / running;
-        *   rem  = count % running;
-        *   count = quot * enabled + (rem * enabled) / running;
+        *   if (running != 0) {
+        *     quot = count / running;
+        *     rem  = count % running;
+        *     count = quot * enabled + (rem * enabled) / running;
+        *   }
         */
        __u16   time_shift;
        __u32   time_mult;
author reviewer
#6 lore raw

PATCH v2 3/4 perf test: Remove x86 rdpmc test

In reply to Ian Rogers

This test has been superseded by test_stat_user_read in:
tools/lib/perf/tests/test-evsel.c
The updated test doesn't divide-by-0 when running time of a counter is
0. It also supports ARM64.

---
 tools/perf/arch/x86/tests/Build        |   1 -
 tools/perf/arch/x86/tests/arch-tests.c |   2 -
 tools/perf/arch/x86/tests/rdpmc.c      | 182 -------------------------
 3 files changed, 185 deletions(-)
 delete mode 100644 tools/perf/arch/x86/tests/rdpmc.c
tools/perf/arch/x86/tests/Build +0 -1
index 28d793390198..70b5bcbc15df 100644
@@ -2,7 +2,6 @@ perf-$(CONFIG_DWARF_UNWIND) += regs_load.o
 perf-$(CONFIG_DWARF_UNWIND) += dwarf-unwind.o
 
 perf-y += arch-tests.o
-perf-y += rdpmc.o
 perf-y += sample-parsing.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 -2
index 64fb73d14d2f..04018b8aa85b 100644
@@ -3,7 +3,6 @@
 #include "tests/tests.h"
 #include "arch-tests.h"
 
-DEFINE_SUITE("x86 rdpmc", rdpmc);
 #ifdef HAVE_AUXTRACE_SUPPORT
 DEFINE_SUITE("x86 instruction decoder - new instructions", insn_x86);
 DEFINE_SUITE("Intel PT packet decoder", intel_pt_pkt_decoder);
@@ -14,7 +13,6 @@ DEFINE_SUITE("x86 bp modify", bp_modify);
 DEFINE_SUITE("x86 Sample parsing", x86_sample_parsing);
 
 struct test_suite *arch_tests[] = {
-	&suite__rdpmc,
 #ifdef HAVE_DWARF_UNWIND_SUPPORT
 	&suite__dwarf_unwind,
 #endif
tools/perf/arch/x86/tests/rdpmc.c deleted +0 -182

182 lines not on this page · raw message

Signature · 1 line
2.36.1.255.ge46751e96f-goog
author reviewer
#7 lore raw

PATCH v2 4/4 perf test: Add user space counter reading tests

In reply to Ian Rogers

These tests are based on test_stat_user_read in tools/lib/perf/tests/test-evsel.c. The tests are modified to skip if perf_event_open fails or rdpmc isn't supported.

---
 tools/perf/tests/mmap-basic.c | 127 +++++++++++++++++++++++++++++++++-
 1 file changed, 126 insertions(+), 1 deletion(-)
tools/perf/tests/mmap-basic.c +126 -1
index 30bbe144648a..dfb6173b2a82 100644
@@ -170,14 +170,139 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
 	return err;
 }
 
+static int test_stat_user_read(int event)
+{
+	struct perf_counts_values counts = { .val = 0 };
+	struct perf_thread_map *threads;
+	struct perf_evsel *evsel;
+	struct perf_event_mmap_page *pc;
+	struct perf_event_attr attr = {
+		.type	= PERF_TYPE_HARDWARE,
+		.config	= event,
+#ifdef __aarch64__
+		.config1 = 0x2,		/* Request user access */
+#endif
+	};
+	int err, i, ret = TEST_FAIL;
+	bool opened = false, mapped = false;
+
+	threads = perf_thread_map__new_dummy();
+	TEST_ASSERT_VAL("failed to create threads", threads);
+
+	perf_thread_map__set_pid(threads, 0, 0);
+
+	evsel = perf_evsel__new(&attr);
+	TEST_ASSERT_VAL("failed to create evsel", evsel);
+
+	err = perf_evsel__open(evsel, NULL, threads);
+	if (err) {
+		pr_err("failed to open evsel: %s\n", strerror(-err));
+		ret = TEST_SKIP;
+		goto out;
+	}
+	opened = true;
+
+	err = perf_evsel__mmap(evsel, 0);
+	if (err) {
+		pr_err("failed to mmap evsel: %s\n", strerror(-err));
+		goto out;
+	}
+	mapped = true;
+
+	pc = perf_evsel__mmap_base(evsel, 0, 0);
+	if (!pc) {
+		pr_err("failed to get mmapped address\n");
+		goto out;
+	}
+
+	if (!pc->cap_user_rdpmc || !pc->index) {
+		pr_err("userspace counter access not %s\n",
+			!pc->cap_user_rdpmc ? "supported" : "enabled");
+		ret = TEST_SKIP;
+		goto out;
+	}
+	if (pc->pmc_width < 32) {
+		pr_err("userspace counter width not set (%d)\n", pc->pmc_width);
+		goto out;
+	}
+
+	perf_evsel__read(evsel, 0, 0, &counts);
+	if (counts.val == 0) {
+		pr_err("failed to read value for evsel\n");
+		goto out;
+	}
+
+	for (i = 0; i < 5; i++) {
+		volatile int count = 0x10000 << i;
+		__u64 start, end, last = 0;
+
+		pr_debug("\tloop = %u, ", count);
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		start = counts.val;
+
+		while (count--) ;
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		end = counts.val;
+
+		if ((end - start) < last) {
+			pr_err("invalid counter data: end=%llu start=%llu last= %llu\n",
+				end, start, last);
+			goto out;
+		}
+		last = end - start;
+		pr_debug("count = %llu\n", end - start);
+	}
+	ret = TEST_OK;
+
+out:
+	if (mapped)
+		perf_evsel__munmap(evsel);
+	if (opened)
+		perf_evsel__close(evsel);
+	perf_evsel__delete(evsel);
+
+	perf_thread_map__put(threads);
+	return ret;
+}
+
+static int test__mmap_user_read_instr(struct test_suite *test __maybe_unused,
+				      int subtest __maybe_unused)
+{
+	return test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS);
+}
+
+static int test__mmap_user_read_cycles(struct test_suite *test __maybe_unused,
+				       int subtest __maybe_unused)
+{
+	return test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES);
+}
+
 static struct test_case tests__basic_mmap[] = {
 	TEST_CASE_REASON("Read samples using the mmap interface",
 			 basic_mmap,
 			 "permissions"),
+	TEST_CASE_REASON("User space counter reading of instructions",
+			 mmap_user_read_instr,
+#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
+			 "permissions"
+#else
+			 "unsupported"
+#endif
+		),
+	TEST_CASE_REASON("User space counter reading of cycles",
+			 mmap_user_read_cycles,
+#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
+			 "permissions"
+#else
+			 "unsupported"
+#endif
+		),
 	{	.name = NULL, }
 };
 
 struct test_suite suite__basic_mmap = {
-	.desc = "Read samples using the mmap interface",
+	.desc = "mmap interface tests",
 	.test_cases = tests__basic_mmap,
 };
Signature · 1 line
2.36.1.255.ge46751e96f-goog
Reviewed-by on v2 4/4
#8 lore raw

Re: [PATCH v2 4/4] perf test: Add user space counter reading tests

Patch looks fine to me.

Thanks,
Kajol Jain

Quoted 154 lines · Ian Rogers

These tests are based on test_stat_user_read in tools/lib/perf/tests/test-evsel.c. The tests are modified to skip if perf_event_open fails or rdpmc isn't supported.

---
 tools/perf/tests/mmap-basic.c | 127 +++++++++++++++++++++++++++++++++-
 1 file changed, 126 insertions(+), 1 deletion(-)
tools/perf/tests/mmap-basic.c +126 -1
index 30bbe144648a..dfb6173b2a82 100644
@@ -170,14 +170,139 @@ static int test__basic_mmap(struct test_suite *test __maybe_unused, int subtest
 	return err;
 }
 
+static int test_stat_user_read(int event)
+{
+	struct perf_counts_values counts = { .val = 0 };
+	struct perf_thread_map *threads;
+	struct perf_evsel *evsel;
+	struct perf_event_mmap_page *pc;
+	struct perf_event_attr attr = {
+		.type	= PERF_TYPE_HARDWARE,
+		.config	= event,
+#ifdef __aarch64__
+		.config1 = 0x2,		/* Request user access */
+#endif
+	};
+	int err, i, ret = TEST_FAIL;
+	bool opened = false, mapped = false;
+
+	threads = perf_thread_map__new_dummy();
+	TEST_ASSERT_VAL("failed to create threads", threads);
+
+	perf_thread_map__set_pid(threads, 0, 0);
+
+	evsel = perf_evsel__new(&attr);
+	TEST_ASSERT_VAL("failed to create evsel", evsel);
+
+	err = perf_evsel__open(evsel, NULL, threads);
+	if (err) {
+		pr_err("failed to open evsel: %s\n", strerror(-err));
+		ret = TEST_SKIP;
+		goto out;
+	}
+	opened = true;
+
+	err = perf_evsel__mmap(evsel, 0);
+	if (err) {
+		pr_err("failed to mmap evsel: %s\n", strerror(-err));
+		goto out;
+	}
+	mapped = true;
+
+	pc = perf_evsel__mmap_base(evsel, 0, 0);
+	if (!pc) {
+		pr_err("failed to get mmapped address\n");
+		goto out;
+	}
+
+	if (!pc->cap_user_rdpmc || !pc->index) {
+		pr_err("userspace counter access not %s\n",
+			!pc->cap_user_rdpmc ? "supported" : "enabled");
+		ret = TEST_SKIP;
+		goto out;
+	}
+	if (pc->pmc_width < 32) {
+		pr_err("userspace counter width not set (%d)\n", pc->pmc_width);
+		goto out;
+	}
+
+	perf_evsel__read(evsel, 0, 0, &counts);
+	if (counts.val == 0) {
+		pr_err("failed to read value for evsel\n");
+		goto out;
+	}
+
+	for (i = 0; i < 5; i++) {
+		volatile int count = 0x10000 << i;
+		__u64 start, end, last = 0;
+
+		pr_debug("\tloop = %u, ", count);
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		start = counts.val;
+
+		while (count--) ;
+
+		perf_evsel__read(evsel, 0, 0, &counts);
+		end = counts.val;
+
+		if ((end - start) < last) {
+			pr_err("invalid counter data: end=%llu start=%llu last= %llu\n",
+				end, start, last);
+			goto out;
+		}
+		last = end - start;
+		pr_debug("count = %llu\n", end - start);
+	}
+	ret = TEST_OK;
+
+out:
+	if (mapped)
+		perf_evsel__munmap(evsel);
+	if (opened)
+		perf_evsel__close(evsel);
+	perf_evsel__delete(evsel);
+
+	perf_thread_map__put(threads);
+	return ret;
+}
+
+static int test__mmap_user_read_instr(struct test_suite *test __maybe_unused,
+				      int subtest __maybe_unused)
+{
+	return test_stat_user_read(PERF_COUNT_HW_INSTRUCTIONS);
+}
+
+static int test__mmap_user_read_cycles(struct test_suite *test __maybe_unused,
+				       int subtest __maybe_unused)
+{
+	return test_stat_user_read(PERF_COUNT_HW_CPU_CYCLES);
+}
+
 static struct test_case tests__basic_mmap[] = {
 	TEST_CASE_REASON("Read samples using the mmap interface",
 			 basic_mmap,
 			 "permissions"),
+	TEST_CASE_REASON("User space counter reading of instructions",
+			 mmap_user_read_instr,
+#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
+			 "permissions"
+#else
+			 "unsupported"
+#endif
+		),
+	TEST_CASE_REASON("User space counter reading of cycles",
+			 mmap_user_read_cycles,
+#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
+			 "permissions"
+#else
+			 "unsupported"
+#endif
+		),
 	{	.name = NULL, }
 };
 
 struct test_suite suite__basic_mmap = {
-	.desc = "Read samples using the mmap interface",
+	.desc = "mmap interface tests",
 	.test_cases = tests__basic_mmap,
 };
author reviewer
#9 lore raw
In reply to Ian Rogers
Quoted 19 lines · Ian Rogers

libperf's perf_mmap__read_self and the addition of arm64 support mean that the perf_event.h and the rdpmc perf test have become stale. Refresh the documentation in perf_event.h, remove the x86 rdpmc test and port the libperf test as a non-architecture specific test.

Address sanitizer testing showed libperf leaking fds when the perf_event_open failed, add error paths to handle this.

A comment is added to perf_event.h to avoid a divide by zero when scaling counts if the running time is 0. This was previously discussed in this thread:
https://lore.kernel.org/lkml/CAP-5=fVRdqvswtyQMg5cB+ntTGda+SAYskjTQednEH-AeZo13g@mail.gmail.com/

v2. Alters the skip in test_stat_user_read for open to always be a
skip as perf_event_open may fail with EACCES (permissions), ENOSYS (not supported) and ENOENT (hypervisor). Adds Rob Herring's acked-by on patch 3.

Quoted 5 lines

Ian Rogers (4):
libperf evsel: Open shouldn't leak fd on failure
perf: Align user space counter reading with code perf test: Remove x86 rdpmc test
perf test: Add user space counter reading tests

Ping. Would be nice to get this cleaned up in the next release, plus better testing.

Thanks,
Ian

Quoted 13 lines
 include/uapi/linux/perf_event.h        |  32 +++--
 tools/include/uapi/linux/perf_event.h  |  32 +++--
 tools/lib/perf/evsel.c                 |  17 ++-
 tools/perf/arch/x86/tests/Build        |   1 -
 tools/perf/arch/x86/tests/arch-tests.c |   2 -
 tools/perf/arch/x86/tests/rdpmc.c      | 182 -------------------------
 tools/perf/tests/mmap-basic.c          | 127 ++++++++++++++++-
 7 files changed, 176 insertions(+), 217 deletions(-)
 delete mode 100644 tools/perf/arch/x86/tests/rdpmc.c

--
2.36.1.255.ge46751e96f-goog