Skip to contentSkip to conversation

perf_event_open02: stop groups with hw counters first

PATCH New Jan Stancek 2 participants 8 messages Oct 16, 2019 – Oct 18, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 perf_event_open02: stop groups with hw counters first

Nick reports that prctl(PR_TASK_PERF_EVENTS_DISABLE) doesn't stop tsk0 as last one, which sometimes causes test to fail, because of way test calculates 'ratio' value and very low tolerance.

Looking at implementation, prctl() walks the list and does appear to disable events in order they were created (tsk0 first).

Stop groups with hw counters before tsk0.

ratio     Before        After
mean      4.99921       4.99871
stdev     0.00006315    0.0000746
Fixes #588
---
 testcases/kernel/syscalls/perf_event_open/perf_event_open02.c | 6 ++++++
 1 file changed, 6 insertions(+)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +6 -0
index 95e30519fe42..1cfe29bb3d8a 100644
@@ -324,6 +324,12 @@ static void verify(void)
 
 	do_work();
 
+	/* stop groups with hw counters first before tsk0 */
+	for (i = 0; i < n; i++) {
+		ioctl(hwfd[i], PERF_EVENT_IOC_DISABLE);
+		ioctl(tskfd[i], PERF_EVENT_IOC_DISABLE);
+	}
+
 	if (prctl(PR_TASK_PERF_EVENTS_DISABLE) == -1) {
 		tst_brkm(TBROK | TERRNO, cleanup,
 			 "prctl(PR_TASK_PERF_EVENTS_DISABLE) failed");
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH 2/2 perf_event_open02: make test more reliable on -rt kernels

Test is failing on -rt kernels quite reliably as reported here:
https://lkml.org/lkml/2019/8/14/714

Suspected cause is extra time spend in scheduler, which counts towards test process hw counters.

Pin test process to single CPU (doesn't matter which one) to lessen effects from scheduling:

ratio     Before        After
mean      5.02582       4.99844
stdev     0.0090435     0.0000272
          95+% FAIL     100% PASS

Tested with "perf_event_open02: stop groups with hw counters first" patch on RHEL8 kernel-rt 4.18.0-147.rt24.93.el8.x86_64 (HP dl380g7).

---
 .../kernel/syscalls/perf_event_open/perf_event_open02.c   | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +15 -0
index 1cfe29bb3d8a..6fc6f4afa119 100644
@@ -52,6 +52,7 @@ Usage is: ./performance_counter02  [-v]
 The -v flag makes it print out the values of each counter.
 */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -222,6 +223,20 @@ static void setup(void)
 	int i;
 	struct perf_event_attr tsk_event, hw_event;
 
+#ifdef HAVE_SCHED_GETCPU
+	cpu_set_t mask;
+	int cpu = sched_getcpu();
+
+	if (cpu == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_getcpu() failed");
+
+	CPU_ZERO(&mask);
+	CPU_SET(cpu, &mask);
+
+	if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_setaffinity() failed");
+#endif
+
 	/*
 	 * According to perf_event_open's manpage, the official way of
 	 * knowing if perf_event_open() support is enabled is checking for
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/2 perf_event_open02: make test more reliable on -rt kernels

Hi!

Quoted 18 lines
---
 .../kernel/syscalls/perf_event_open/perf_event_open02.c   | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +1 -0
index 1cfe29bb3d8a..6fc6f4afa119 100644
@@ -52,6 +52,7 @@ Usage is: ./performance_counter02  [-v]
 The -v flag makes it print out the values of each counter.
 */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>

testcases/kernel/syscalls/perf_event_open/perf_event_open02.c static void setup(void) · line 227

Quoted 5 lines
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +2 -0
@@ -222,6 +223,20 @@ static void setup(void)
 	int i;
 	struct perf_event_attr tsk_event, hw_event;
 
+#ifdef HAVE_SCHED_GETCPU
+	cpu_set_t mask;

Don't we have to allocate the mask dynamically so that we do not fail on systems with more than CPU_SETSIZE (1024) CPUs?

We already have fallback macros for older libc in include/lapi/cpuset.h so that we can use CPU_ALLOC() unconditionally.

Quoted 21 lines
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +12 -0
+	int cpu = sched_getcpu();
+
+	if (cpu == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_getcpu() failed");
+
+	CPU_ZERO(&mask);
+	CPU_SET(cpu, &mask);
+
+	if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_setaffinity() failed");
+#endif
+
 	/*
 	 * According to perf_event_open's manpage, the official way of
 	 * knowing if perf_event_open() support is enabled is checking for
Signature · 5 lines
1.8.3.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH 2/2 perf_event_open02: make test more reliable on -rt kernels

----- Original Message -----

Quoted 24 lines

Hi!

Quoted 23 lines
---
 .../kernel/syscalls/perf_event_open/perf_event_open02.c   | 15
 +++++++++++++++
 1 file changed, 15 insertions(+)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +1 -0
index 1cfe29bb3d8a..6fc6f4afa119 100644
@@ -52,6 +52,7 @@ Usage is: ./performance_counter02  [-v]
 The -v flag makes it print out the values of each counter.
 */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -222,6 +223,20 @@ static void setup(void)
 	int i;
 	struct perf_event_attr tsk_event, hw_event;
 
Quoted 8 lines

testcases/kernel/syscalls/perf_event_open/perf_event_open02.c static void setup(void) · line 227

Quoted 2 lines
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +2 -0
+#ifdef HAVE_SCHED_GETCPU
+	cpu_set_t mask;

Don't we have to allocate the mask dynamically so that we do not fail on systems with more than CPU_SETSIZE (1024) CPUs?

We already have fallback macros for older libc in include/lapi/cpuset.h so that we can use CPU_ALLOC() unconditionally.

Haven't thought of that. I'll look into adding that for v2.

#4 lore raw

PATCH v2 1/1 perf_event_open02: make test more reliable on -rt kernels

Test is failing on -rt kernels quite reliably as reported here:
https://lkml.org/lkml/2019/8/14/714

Suspected cause is extra time spend in scheduler, which counts towards test process hw counters.

Pin test process to single CPU (doesn't matter which one) to lessen effects from scheduling:

ratio     Before        After
mean      5.02582       4.99844
stdev     0.0090435     0.0000272
          95+% FAIL     100% PASS

Tested with "perf_event_open02: stop groups with hw counters first" patch on RHEL8 kernel-rt 4.18.0-147.rt24.93.el8.x86_64 (HP dl380g7).

---
 .../syscalls/perf_event_open/perf_event_open02.c      | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)
testcases/kernel/syscalls/perf_event_open/perf_event_open02.c +19 -0
index 1cfe29bb3d8a..c2831aa20d40 100644
@@ -52,6 +52,7 @@ Usage is: ./performance_counter02  [-v]
 The -v flag makes it print out the values of each counter.
 */
 
+#define _GNU_SOURCE
 #include <stdio.h>
 #include <stddef.h>
 #include <stdlib.h>
@@ -72,6 +73,7 @@ The -v flag makes it print out the values of each counter.
 
 #include "test.h"
 #include "safe_macros.h"
+#include "lapi/cpuset.h"
 #include "lapi/syscalls.h"
 
 char *TCID = "perf_event_open02";
@@ -222,6 +224,23 @@ static void setup(void)
 	int i;
 	struct perf_event_attr tsk_event, hw_event;
 
+#ifdef HAVE_SCHED_GETCPU
+	int cpu = sched_getcpu();
+	size_t mask_size;
+	cpu_set_t *mask;
+
+	if (cpu == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_getcpu() failed");
+
+	mask = CPU_ALLOC(cpu + 1);
+	mask_size = CPU_ALLOC_SIZE(cpu + 1);
+	CPU_ZERO_S(mask_size, mask);
+	CPU_SET(cpu, mask);
+	if (sched_setaffinity(0, mask_size, mask) == -1)
+		tst_brkm(TBROK | TERRNO, NULL, "sched_setaffinity() failed");
+	CPU_FREE(mask);
+#endif
+
 	/*
 	 * According to perf_event_open's manpage, the official way of
 	 * knowing if perf_event_open() support is enabled is checking for
Signature · 1 line
1.8.3.1
#5 lore raw

PATCH v2 1/1 perf_event_open02: make test more reliable on -rt kernels

Hi!
This version looks good, acked.

Signature · 2 lines
Cyril Hrubis
[email protected]
#6 lore raw

PATCH v2 1/1 perf_event_open02: make test more reliable on -rt kernels

----- Original Message -----

Quoted 2 lines

Hi!
This version looks good, acked.

Both pushed.