Skip to contentSkip to conversation

perf test x86: address multiplexing in rdpmc test

PATCH Superseded Ian Rogers 2 participants 4 messages Mar 21, 2020 – Mar 22, 2020 perf events

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

PATCH 1/1 perf test x86: address multiplexing in rdpmc test

Counters may be being used for pinned or other events which inhibit the instruction counter in the test from being scheduled - time_enabled > 0 but time_running == 0. This causes the test to fail with division by 0. Add a sleep loop to ensure that the counter is run before computing the count.

---
 tools/perf/arch/x86/tests/rdpmc.c | 45 ++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 10 deletions(-)
tools/perf/arch/x86/tests/rdpmc.c +35 -10
index 1ea916656a2d..0b0792ae67f7 100644
@@ -34,19 +34,35 @@ static u64 rdtsc(void)
 	return low | ((u64)high) << 32;
 }
 
-static u64 mmap_read_self(void *addr)
+static u64 mmap_read_self(void *addr, bool *error)
 {
 	struct perf_event_mmap_page *pc = addr;
-	u32 seq, idx, time_mult = 0, time_shift = 0;
+	u32 seq, idx, time_mult = 0, time_shift = 0, sleep_count = 0;
 	u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
 
+	*error = false;
 	do {
-		seq = pc->lock;
-		barrier();
-
-		enabled = pc->time_enabled;
-		running = pc->time_running;
-
+		do {
+			seq = pc->lock;
+			barrier();
+
+			enabled = pc->time_enabled;
+			running = pc->time_running;
+
+			if (running == 0) {
+				/*
+				 * Event hasn't run, this may be caused by
+				 * multiplexing.
+				 */
+				sleep_count++;
+				if (sleep_count > 60) {
+					pr_err("Event failed to run. Are higher priority counters being sampled by a different process?\n");
+					*error = true;
+					return 0;
+				}
+				sleep(1);
+			}
+		} while (running == 0);
 		if (enabled != running) {
 			cyc = rdtsc();
 			time_mult = pc->time_mult;
@@ -131,13 +147,22 @@ static int __test__rdpmc(void)
 
 	for (n = 0; n < 6; n++) {
 		u64 stamp, now, delta;
+		bool error;
 
-		stamp = mmap_read_self(addr);
+		stamp = mmap_read_self(addr, &error);
+		if (error) {
+			delta_sum = 0;
+			goto out_close;
+		}
 
 		for (i = 0; i < loops; i++)
 			tmp++;
 
-		now = mmap_read_self(addr);
+		now = mmap_read_self(addr, &error);
+		if (error) {
+			delta_sum = 0;
+			goto out_close;
+		}
 		loops *= 10;
 
 		delta = now - stamp;
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
maintainer
#1 lore raw
Quoted 33 lines · Ian Rogers

Counters may be being used for pinned or other events which inhibit the instruction counter in the test from being scheduled - time_enabled > 0 but time_running == 0. This causes the test to fail with division by 0. Add a sleep loop to ensure that the counter is run before computing the count.

---
 tools/perf/arch/x86/tests/rdpmc.c | 45 ++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 10 deletions(-)
tools/perf/arch/x86/tests/rdpmc.c +3 -6
index 1ea916656a2d..0b0792ae67f7 100644
@@ -34,19 +34,35 @@ static u64 rdtsc(void)
 	return low | ((u64)high) << 32;
 }
 
-static u64 mmap_read_self(void *addr)
+static u64 mmap_read_self(void *addr, bool *error)
 {
 	struct perf_event_mmap_page *pc = addr;
-	u32 seq, idx, time_mult = 0, time_shift = 0;
+	u32 seq, idx, time_mult = 0, time_shift = 0, sleep_count = 0;
 	u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
 
+	*error = false;
 	do {
-		seq = pc->lock;
-		barrier();
-
-		enabled = pc->time_enabled;

tools/perf/arch/x86/tests/rdpmc.c static u64 rdtsc(void) · line 52

Quoted 10 lines
tools/perf/arch/x86/tests/rdpmc.c +8 -2
-		running = pc->time_running;
-
+		do {
+			seq = pc->lock;
+			barrier();
+
+			enabled = pc->time_enabled;
+			running = pc->time_running;
+
+			if (running == 0) {

This is not in fact the condition the Changelog calls out.

tools/perf/arch/x86/tests/rdpmc.c static u64 rdtsc(void) · line 65

Quoted 13 lines
tools/perf/arch/x86/tests/rdpmc.c +13 -0
+				/*
+				 * Event hasn't run, this may be caused by
+				 * multiplexing.
+				 */
+				sleep_count++;
+				if (sleep_count > 60) {
+					pr_err("Event failed to run. Are higher priority counters being sampled by a different process?\n");
+					*error = true;
+					return 0;
+				}
+				sleep(1);
+			}
+		} while (running == 0);

Also, I would much prefer this test to be in the caller of this function, and not deface this function.

I'd prefer this function to stay representative of the outlines in uapi/linux/perf_event.h and an example of how to actually use it.

Quoted 31 lines
tools/perf/arch/x86/tests/rdpmc.c +11 -2
 		if (enabled != running) {
 			cyc = rdtsc();
 			time_mult = pc->time_mult;
@@ -131,13 +147,22 @@ static int __test__rdpmc(void)
 
 	for (n = 0; n < 6; n++) {
 		u64 stamp, now, delta;
+		bool error;
 
-		stamp = mmap_read_self(addr);
+		stamp = mmap_read_self(addr, &error);
+		if (error) {
+			delta_sum = 0;
+			goto out_close;
+		}
 
 		for (i = 0; i < loops; i++)
 			tmp++;
 
-		now = mmap_read_self(addr);
+		now = mmap_read_self(addr, &error);
+		if (error) {
+			delta_sum = 0;
+			goto out_close;
+		}
 		loops *= 10;
 
 		delta = now - stamp;
Signature · 1 line
2.25.1.696.g5e7596f4ac-goog
author reviewer
#2 lore raw
Quoted 39 lines · Peter Zijlstra
Quoted 37 lines · Ian Rogers

Counters may be being used for pinned or other events which inhibit the instruction counter in the test from being scheduled - time_enabled > 0 but time_running == 0. This causes the test to fail with division by 0. Add a sleep loop to ensure that the counter is run before computing the count.

---
 tools/perf/arch/x86/tests/rdpmc.c | 45 ++++++++++++++++++++++++-------
 1 file changed, 35 insertions(+), 10 deletions(-)
tools/perf/arch/x86/tests/rdpmc.c +5 -8
index 1ea916656a2d..0b0792ae67f7 100644
@@ -34,19 +34,35 @@ static u64 rdtsc(void)
      return low | ((u64)high) << 32;
 }
 
-static u64 mmap_read_self(void *addr)
+static u64 mmap_read_self(void *addr, bool *error)
 {
      struct perf_event_mmap_page *pc = addr;
-     u32 seq, idx, time_mult = 0, time_shift = 0;
+     u32 seq, idx, time_mult = 0, time_shift = 0, sleep_count = 0;
      u64 count, cyc = 0, time_offset = 0, enabled, running, delta;
 
+     *error = false;
      do {
-             seq = pc->lock;
-             barrier();
-
-             enabled = pc->time_enabled;
-             running = pc->time_running;
-
+             do {
+                     seq = pc->lock;
Quoted 8 lines

tools/perf/arch/x86/tests/rdpmc.c static u64 rdtsc(void) · line 52

Quoted 6 lines
tools/perf/arch/x86/tests/rdpmc.c +6 -0
+                     barrier();
+
+                     enabled = pc->time_enabled;
+                     running = pc->time_running;
+
+                     if (running == 0) {

This is not in fact the condition the Changelog calls out.

Not sure I follow. As in the multiplexing? It is exactly the condition that time_running == 0.

Quoted 15 lines

tools/perf/arch/x86/tests/rdpmc.c static u64 rdtsc(void) · line 65

Quoted 13 lines
tools/perf/arch/x86/tests/rdpmc.c +13 -0
+                             /*
+                              * Event hasn't run, this may be caused by
+                              * multiplexing.
+                              */
+                             sleep_count++;
+                             if (sleep_count > 60) {
+                                     pr_err("Event failed to run. Are higher priority counters being sampled by a different process?\n");
+                                     *error = true;
+                                     return 0;
+                             }
+                             sleep(1);
+                     }
+             } while (running == 0);
Quoted 5 lines

Also, I would much prefer this test to be in the caller of this function, and not deface this function.

I'd prefer this function to stay representative of the outlines in uapi/linux/perf_event.h and an example of how to actually use it.

I 100% agree the code here should line up with uapi/linux/perf_event.h, there are already small divergences. I've tried to address these comments in v2 here:
https://lkml.org/lkml/2020/3/21/325
Of course, I don't mind just being the person reporting this issue and someone with a stronger opinion on the API to propose something better and line up the perf_event.h comments. Another option is to fail the test if the counter never runs rather than sleeping. That would be unfortunate for some of the testing conditions we have, and we'd have to run the test less frequently.

Thanks,
Ian

Quoted 31 lines
Quoted 31 lines
tools/perf/arch/x86/tests/rdpmc.c +11 -3
              if (enabled != running) {
                      cyc = rdtsc();
                      time_mult = pc->time_mult;
@@ -131,13 +147,22 @@ static int __test__rdpmc(void)
 
      for (n = 0; n < 6; n++) {
              u64 stamp, now, delta;
+             bool error;
 
-             stamp = mmap_read_self(addr);
+             stamp = mmap_read_self(addr, &error);
+             if (error) {
+                     delta_sum = 0;
+                     goto out_close;
+             }
 
              for (i = 0; i < loops; i++)
                      tmp++;
 
-             now = mmap_read_self(addr);
+             now = mmap_read_self(addr, &error);
+             if (error) {
+                     delta_sum = 0;
+                     goto out_close;
+             }
              loops *= 10;
 
              delta = now - stamp;
--

2.25.1.696.g5e7596f4ac-goog

maintainer
#3 lore raw
Quoted 9 lines · Ian Rogers
Quoted 7 lines · Peter Zijlstra
Quoted 5 lines · Ian Rogers

Counters may be being used for pinned or other events which inhibit the instruction counter in the test from being scheduled - time_enabled > 0 but time_running == 0. This causes the test to fail with division by 0. Add a sleep loop to ensure that the counter is run before computing the count.

Quoted 7 lines
Quoted 4 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+                     if (running == 0) {

This is not in fact the condition the Changelog calls out.

Not sure I follow. As in the multiplexing? It is exactly the condition that time_running == 0.

I meant the condition should be 'enabled && !running'.