Skip to contentSkip to conversation

Update DSCR tests

PATCH Superseded Benjamin Gray 2 participants 7 messages Mar 7, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Update DSCR tests

The randomness based DSCR tests currently have a low probability of doing any writes to the DSCR, making them inefficient in uncovering bugs.

This series adds lockstep variants to these RNG tests, to ensure the happy path is always tested, and improves the randomness and size of the RNG tests.

It also removes many iterations of the sysfs DSCR test, allowing the default timeout to be re-enabled.

Benjamin Gray (5):
selftests/powerpc/dscr: Correct typos selftests/powerpc/dscr: Add lockstep test cases to DSCR explicit tests selftests/powerpc/dscr: Improve DSCR explicit random test case selftests/powerpc/dscr: Speed up DSCR sysfs tests selftests/powerpc/dscr: Restore timeout to DSCR selftests

 tools/testing/selftests/powerpc/dscr/Makefile |   3 +-
 tools/testing/selftests/powerpc/dscr/dscr.h   |  22 +-
 .../powerpc/dscr/dscr_default_test.c          | 223 +++++++++++-------
 .../powerpc/dscr/dscr_explicit_test.c         | 173 +++++++++++---
 .../powerpc/dscr/dscr_inherit_test.c          |   4 +-
 .../selftests/powerpc/dscr/dscr_sysfs_test.c  |  11 +-
 .../selftests/powerpc/dscr/dscr_user_test.c   |   4 +-
 tools/testing/selftests/powerpc/dscr/settings |   1 -
 8 files changed, 300 insertions(+), 141 deletions(-)
 delete mode 100644 tools/testing/selftests/powerpc/dscr/settings

base-commit: 422fbcbf91303706823bc3babceb1df1a42112bf --
2.39.2

#5 lore raw

PATCH 4/5 selftests/powerpc/dscr: Speed up DSCR sysfs tests

This test case is extremely slow, taking around a minute compared to most of the other DSCR tests taking a second at most. Perf shows most time is spent by the kernel switching to each CPU it reads in /sys/devices/system/cpu. This switching is an unavoidable consequnce of reading all the .../cpuN/dscr values.

Remove the outer iteration loop from this test case, reducing the reads from 1600 to 16. This still updates the DSCR 16 times and verifies on every CPU each time, so I do not expect the lower coverage to be meaningful. The speedup is significant: back down to ~1 second like the other tests.

---
 .../testing/selftests/powerpc/dscr/dscr_sysfs_test.c  | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)
tools/testing/selftests/powerpc/dscr/dscr_sysfs_test.c +4 -7
index 4f1fef6198fc..e7cd0d6b1fad 100644
@@ -67,17 +67,14 @@ static int check_all_cpu_dscr_defaults(unsigned long val)
 int dscr_sysfs(void)
 {
 	unsigned long orig_dscr_default;
-	int i, j;
 
 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_DSCR));
 
 	orig_dscr_default = get_default_dscr();
-	for (i = 0; i < COUNT; i++) {
-		for (j = 0; j < DSCR_MAX; j++) {
-			set_default_dscr(j);
-			if (check_all_cpu_dscr_defaults(j))
-				goto fail;
-		}
+	for (int i = 0; i < DSCR_MAX; i++) {
+		set_default_dscr(i);
+		if (check_all_cpu_dscr_defaults(i))
+			goto fail;
 	}
 	set_default_dscr(orig_dscr_default);
 	return 0;
Signature · 1 line
2.39.2
#1 lore raw

PATCH 1/5 selftests/powerpc/dscr: Correct typos

In reply to Benjamin Gray

Correct a couple of typos while working on other improvements to the DSCR tests.

---
 tools/testing/selftests/powerpc/dscr/dscr_explicit_test.c | 4 ++--
 tools/testing/selftests/powerpc/dscr/dscr_inherit_test.c  | 4 ++--
 tools/testing/selftests/powerpc/dscr/dscr_user_test.c     | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)
tools/testing/selftests/powerpc/dscr/dscr_explicit_test.c +2 -2
index 32fcf2b324b1..5659d98cf340 100644
@@ -7,8 +7,8 @@
  * privilege state SPR and the problem state SPR for this purpose.
  *
  * When using the privilege state SPR, the instructions such as
- * mfspr or mtspr are priviledged and the kernel emulates them
- * for us. Instructions using problem state SPR can be exuecuted
+ * mfspr or mtspr are privileged and the kernel emulates them
+ * for us. Instructions using problem state SPR can be executed
  * directly without any emulation if the HW supports them. Else
  * they also get emulated by the kernel.
  *
tools/testing/selftests/powerpc/dscr/dscr_inherit_test.c +2 -2
index f9dfd3d3c2d5..68ce328e813e 100644
@@ -7,8 +7,8 @@
  * value using mfspr.
  *
  * When using the privilege state SPR, the instructions such as
- * mfspr or mtspr are priviledged and the kernel emulates them
- * for us. Instructions using problem state SPR can be exuecuted
+ * mfspr or mtspr are privileged and the kernel emulates them
+ * for us. Instructions using problem state SPR can be executed
  * directly without any emulation if the HW supports them. Else
  * they also get emulated by the kernel.
  *
tools/testing/selftests/powerpc/dscr/dscr_user_test.c +2 -2
index e09072446dd3..67bb872a246a 100644
@@ -8,8 +8,8 @@
  * numbers.
  *
  * When using the privilege state SPR, the instructions such as
- * mfspr or mtspr are priviledged and the kernel emulates them
- * for us. Instructions using problem state SPR can be exuecuted
+ * mfspr or mtspr are privileged and the kernel emulates them
+ * for us. Instructions using problem state SPR can be executed
  * directly without any emulation if the HW supports them. Else
  * they also get emulated by the kernel.
  *
Signature · 1 line
2.39.2
#2 lore raw

PATCH 2/5 selftests/powerpc/dscr: Add lockstep test cases to DSCR explicit tests

In reply to Benjamin Gray

Add new cases to the relevant tests that use explicitly synchronized threads to test the behaviour across context switches with less randomness. By locking the participants to the same CPU we guarantee a context switch occurs each time they make progress, which is a likely failure point if the kernel is not tracking the thread local DSCR correctly.

The random case is left in to keep exercising potential edge cases.

---
 tools/testing/selftests/powerpc/dscr/Makefile |  1 +
 tools/testing/selftests/powerpc/dscr/dscr.h   | 23 +++++
 .../powerpc/dscr/dscr_default_test.c          | 87 ++++++++++++++++---
 .../powerpc/dscr/dscr_explicit_test.c         | 87 ++++++++++++++++++-
 4 files changed, 183 insertions(+), 15 deletions(-)
tools/testing/selftests/powerpc/dscr/Makefile +1 -0
index 845db6273a1b..b29a8863a734 100644
@@ -9,5 +9,6 @@ top_srcdir = ../../../../..
 include ../../lib.mk
 
 $(OUTPUT)/dscr_default_test: LDLIBS += -lpthread
+$(OUTPUT)/dscr_explicit_test: LDLIBS += -lpthread
 
 $(TEST_GEN_PROGS): ../harness.c ../utils.c
tools/testing/selftests/powerpc/dscr/dscr.h +23 -0
index 2c54998d4715..903ee0c83fac 100644
@@ -11,6 +11,8 @@
 #ifndef _SELFTESTS_POWERPC_DSCR_DSCR_H
 #define _SELFTESTS_POWERPC_DSCR_DSCR_H
 
+#define _GNU_SOURCE
+
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -90,4 +92,25 @@ double uniform_deviate(int seed)
 {
 	return seed * (1.0 / (RAND_MAX + 1.0));
 }
+
+int restrict_to_one_cpu(void)
+{
+	cpu_set_t cpus;
+	int cpu;
+
+	FAIL_IF(sched_getaffinity(0, sizeof(cpu_set_t), &cpus));
+
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
+		if (CPU_ISSET(cpu, &cpus))
+			break;
+
+	FAIL_IF(cpu == CPU_SETSIZE);
+
+	CPU_ZERO(&cpus);
+	CPU_SET(cpu, &cpus);
+	FAIL_IF(sched_setaffinity(0, sizeof(cpu_set_t), &cpus));
+
+	return 0;
+}
+
 #endif	/* _SELFTESTS_POWERPC_DSCR_DSCR_H */
tools/testing/selftests/powerpc/dscr/dscr_default_test.c +75 -12

129 lines not on this page · raw message

tools/testing/selftests/powerpc/dscr/dscr_explicit_test.c +84 -3

98 lines not on this page · raw message

Signature · 1 line
2.39.2
#6 lore raw

PATCH 5/5 selftests/powerpc/dscr: Restore timeout to DSCR selftests

In reply to Benjamin Gray

Reducing the time taken by dscr_sysfs_test.c allows restoring the default timeout, which was removed in
commit 850507f30c38 ("selftests/powerpc: Turn off timeout setting for benchmarks, dscr, signal, tm") because that test took too long.

---
 tools/testing/selftests/powerpc/dscr/Makefile | 2 --
 tools/testing/selftests/powerpc/dscr/settings | 1 -
 2 files changed, 3 deletions(-)
 delete mode 100644 tools/testing/selftests/powerpc/dscr/settings
tools/testing/selftests/powerpc/dscr/Makefile +0 -2
index b29a8863a734..9289d5febe1e 100644
@@ -3,8 +3,6 @@ TEST_GEN_PROGS := dscr_default_test dscr_explicit_test dscr_user_test	\
 	      dscr_inherit_test dscr_inherit_exec_test dscr_sysfs_test	\
 	      dscr_sysfs_thread_test
 
-TEST_FILES := settings
-
 top_srcdir = ../../../../..
 include ../../lib.mk
 
tools/testing/selftests/powerpc/dscr/settings deleted +0 -1
deleted file mode 100644
index e7b9417537fb..000000000000
@@ -1 +0,0 @@
-timeout=0
Signature · 1 line
2.39.2
#4 lore raw

PATCH 3/5 selftests/powerpc/dscr: Improve DSCR explicit random test case

In reply to Benjamin Gray

The tests currently have a single writer thread updating the system DSCR with a 1/1000 chance looped only 100 times. So only around one in 10 runs actually do anything.

* Add multiple threads to the dscr_explicit_random_test case.
* Use a barrier to make all the threads start work as simultaneously as possible.
* Use a rwlock and make all threads have a reasonable chance to write to the DSCR on each iteration. PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP is used to prevent writers from starving while all the other threads keep reading. Logging the reads/writes shows a decent mix across the whole test.
* Allow all threads a chance to write.
* Make the chance of writing more likely.

---
 tools/testing/selftests/powerpc/dscr/dscr.h   |   5 -
 .../powerpc/dscr/dscr_default_test.c          | 132 ++++++++----------
 .../powerpc/dscr/dscr_explicit_test.c         |  94 ++++++++-----
 3 files changed, 114 insertions(+), 117 deletions(-)
tools/testing/selftests/powerpc/dscr/dscr.h +0 -5
index 903ee0c83fac..9cd5488ab7c2 100644
@@ -88,11 +88,6 @@ void set_default_dscr(unsigned long val)
 	}
 }
 
-double uniform_deviate(int seed)
-{
-	return seed * (1.0 / (RAND_MAX + 1.0));
-}
-
 int restrict_to_one_cpu(void)
 {
 	cpu_set_t cpus;
tools/testing/selftests/powerpc/dscr/dscr_default_test.c +56 -76

161 lines not on this page · raw message

tools/testing/selftests/powerpc/dscr/dscr_explicit_test.c +58 -36

108 lines not on this page · raw message

Signature · 1 line
2.39.2
#3 lore raw

Re: [PATCH 2/5] selftests/powerpc/dscr: Add lockstep test cases to DSCR explicit tests

In reply to Benjamin Gray
Quoted 16 lines · Benjamin Gray

Add new cases to the relevant tests that use explicitly synchronized threads to test the behaviour across context switches with less randomness. By locking the participants to the same CPU we guarantee a context switch occurs each time they make progress, which is a likely failure point if the kernel is not tracking the thread local DSCR correctly.

The random case is left in to keep exercising potential edge cases.

---
 tools/testing/selftests/powerpc/dscr/Makefile |  1 +
 tools/testing/selftests/powerpc/dscr/dscr.h   | 23 +++++
 .../powerpc/dscr/dscr_default_test.c          | 87 ++++++++++++++++---
 .../powerpc/dscr/dscr_explicit_test.c         | 87 ++++++++++++++++++-
 4 files changed, 183 insertions(+), 15 deletions(-)

...

Quoted 18 lines
tools/testing/selftests/powerpc/dscr/dscr.h +10 -0
index 2c54998d4715..903ee0c83fac 100644
@@ -90,4 +92,25 @@ double uniform_deviate(int seed)
 {
 	return seed * (1.0 / (RAND_MAX + 1.0));
 }
+
+int restrict_to_one_cpu(void)
+{
+	cpu_set_t cpus;
+	int cpu;
+
+	FAIL_IF(sched_getaffinity(0, sizeof(cpu_set_t), &cpus));
+
+	for (cpu = 0; cpu < CPU_SETSIZE; cpu++)
+		if (CPU_ISSET(cpu, &cpus))

tools/testing/selftests/powerpc/dscr/dscr.h double uniform_deviate(int seed) · line 114

Quoted 10 lines
tools/testing/selftests/powerpc/dscr/dscr.h +10 -0
+			break;
+
+	FAIL_IF(cpu == CPU_SETSIZE);
+
+	CPU_ZERO(&cpus);
+	CPU_SET(cpu, &cpus);
+	FAIL_IF(sched_setaffinity(0, sizeof(cpu_set_t), &cpus));
+
+	return 0;
+}

We have pick_online_cpu() in utils.c, can you use that?

You probably also need to move bind_to_cpu() from pmu/lib.c to utils.c so you can use it.

cheers