Skip to contentSkip to conversation

Improve engine activity accuracy test with spinner reuse

PATCH Superseded Soham Purkait 1 participant 4 messages Dec 22, 2025

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover Improve engine activity accuracy test with spinner reuse

Instead of creating a new spinner during each pass, it's more efficient
to instantiate the spinner once and reuse it by properly ending and resetting it between iterations.
This approach reduces the overhead associated with repeated spinner creation,
saving time across multiple passes. By eliminating the cumulative time spent on spinner initialization (i.e., creation time * number of passes), the spinner's active duration more accurately reflects the actual engine busyness. This leads to more precise measurements of the percentage of engine busyness in terms of spinner running time.

With this change engine-activity-accuracy test result clearly shows the difference:

Before spinner reuse :

Starting subtest: engine-activity-accuracy-90
Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_RENDER0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=-2.90% (81.03% vs 83.93%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_RENDER0: SUCCESS (1.598s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_COPY0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=-2.81% (83.06% vs 85.87%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_COPY0: SUCCESS (1.684s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_COMPUTE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=-2.04% (83.83% vs 85.87%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_COMPUTE0: SUCCESS (1.667s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_VIDEO_DECODE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=-2.15% (83.99% vs 86.14%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_VIDEO_DECODE0: SUCCESS (1.647s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=-2.00% (83.59% vs 85.60%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE0: SUCCESS (1.664s) Subtest engine-activity-accuracy-90: SUCCESS (8.264s)

After spinner reuse:

Starting subtest: engine-activity-accuracy-90
Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_RENDER0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=0.03% (89.45% vs 89.42%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_RENDER0: SUCCESS (1.560s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_COPY0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=0.02% (89.56% vs 89.54%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_COPY0: SUCCESS (1.559s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_COMPUTE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=0.02% (89.42% vs 89.40%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_COMPUTE0: SUCCESS (1.571s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_VIDEO_DECODE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=0.01% (89.28% vs 89.27%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_VIDEO_DECODE0: SUCCESS (1.581s) Starting dynamic subtest: engine-DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE0 calibration=500ms, test=1000ms, cycle=100ms; ratio=90.00% (90000us/10000us) error=0.01% (89.37% vs 89.36%)
Dynamic subtest engine-DRM_XE_ENGINE_CLASS_VIDEO_ENHANCE0: SUCCESS (1.583s) Subtest engine-activity-accuracy-90: SUCCESS (7.859s)

Soham Purkait (3):
lib/xe/xe_spin: Introduce xe_spin_reset lib/xe/xe_spin: Export xe_spin_sync_wait tests/intel/xe_pmu: Refine engine activity accuracy test

 lib/xe/xe_spin.c     | 29 +++++++++++++++++++++++++++++
 lib/xe/xe_spin.h     |  2 ++
 tests/intel/xe_pmu.c | 17 +++++++++++++----
 3 files changed, 44 insertions(+), 4 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/3 lib/xe/xe_spin: Introduce xe_spin_reset

Create new function xe_spin_reset()
This function resets the state of spin, allowing its reuse for engine activity accuracy test.

v1:
- Add simple commit message and mention test where it is going to be used. (kamil)
v2:
- Add fd description for xe_spin_reset. (Kamil)

---
 lib/xe/xe_spin.c | 29 +++++++++++++++++++++++++++++
 lib/xe/xe_spin.h |  1 +
 2 files changed, 30 insertions(+)
lib/xe/xe_spin.c +29 -0
index ff19128b6..ad55293f5 100644
@@ -205,6 +205,35 @@ void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts)
 	igt_assert(b <= ARRAY_SIZE(spin->batch));
 }
 
+/**
+ * xe_spin_reset:
+ * @fd: xe device file descriptor
+ * @spin: spin state from xe_spin_new()
+ *
+ * Reset the state of spin, allowing its reuse.
+ */
+void xe_spin_reset(int fd, igt_spin_t *spin)
+{
+	struct drm_xe_sync sync = {
+		.type = DRM_XE_SYNC_TYPE_SYNCOBJ,
+		.flags = DRM_XE_SYNC_FLAG_SIGNAL,
+		.handle = spin->syncobj,
+	};
+	struct drm_xe_exec exec = {
+		.num_batch_buffer = 1,
+		.num_syncs = 1,
+		.syncs = to_user_pointer(&sync),
+		.exec_queue_id = spin->engine,
+		.address = spin->address,
+	};
+
+	xe_spin_init_opts((struct xe_spin *)spin->xe_spin,
+			  .addr = spin->address,
+			  .preempt = !(spin->opts.flags & IGT_SPIN_NO_PREEMPTION));
+	igt_assert_eq(igt_ioctl(fd, DRM_IOCTL_XE_EXEC, &exec), 0);
+	xe_spin_wait_started((struct xe_spin *)spin->xe_spin);
+}
+
 /**
  * xe_spin_started:
  * @spin: pointer to spinner mapped bo
lib/xe/xe_spin.h +1 -0
index 5c7566423..5dfe05cb5 100644
@@ -77,6 +77,7 @@ uint32_t xe_spin_nsec_to_ticks(int fd, int gt_id, uint64_t nsec);
 void xe_spin_init(struct xe_spin *spin, struct xe_spin_opts *opts);
 #define xe_spin_init_opts(fd, ...) \
 	xe_spin_init(fd, &((struct xe_spin_opts){__VA_ARGS__}))
+void xe_spin_reset(int fd, igt_spin_t *spin);
 bool xe_spin_started(struct xe_spin *spin);
 void xe_spin_wait_started(struct xe_spin *spin);
 void xe_spin_end(struct xe_spin *spin);
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/3 lib/xe/xe_spin: Export xe_spin_sync_wait

In reply to Soham Purkait

Make function xe_spin_sync_wait() public Which waits for the spin batch associated with the given igt_spin object to complete by waiting on its syncobj after spin end is executed.

v1:
- Add simple commit message. (Kamil)
v2:
- Add fd description for xe_spin_sync_wait. (Kamil)
- Add improved documentation for xe_spin_sync_wait. (Kamil)

---
 lib/xe/xe_spin.h | 1 +
 1 file changed, 1 insertion(+)
lib/xe/xe_spin.h +1 -0
index 5dfe05cb5..31154997b 100644
@@ -81,6 +81,7 @@ void xe_spin_reset(int fd, igt_spin_t *spin);
 bool xe_spin_started(struct xe_spin *spin);
 void xe_spin_wait_started(struct xe_spin *spin);
 void xe_spin_end(struct xe_spin *spin);
+void xe_spin_sync_wait(int fd, struct igt_spin *spin);
 void xe_spin_preempt_wait(struct xe_spin *spin);
 void xe_spin_preempt_nowait(struct xe_spin *spin);
 
Signature · 1 line
2.34.1
#3 lore raw

PATCH v3 3/3 tests/intel/xe_pmu: Refine engine activity accuracy test

In reply to Soham Purkait

Creating a new spinner during each pass was introducing unnecessary overhead, which could distort timing-sensitive measurements and increase error rates. The cumulative cost of repeated spinner initialization was impacting test accuracy and efficiency.

To address this, a single spinner instance is now reused and properly ended and reset between iterations. This approach reduces the overhead associated with repeated spinner creation, saving time across multiple passes by eliminating the cumulative time spent on spinner initialization (creation time × number of passes). As the spinner's active duration more accurately reflects actual engine busyness, this leads to more precise measurements of the percentage of engine busyness in terms of spinner running time.

With this change, the engine-activity-accuracy test results clearly show an improvement in error reduction ranging from 98.97% to 99.53%.

v1:
- Add how this patch improves the engine activity accuracy test. (Lucas)
- Show the numbers with the said improvement. (Lucas)

v2:
- Add an overview of the issue. (Riana)
- Add the links in Closes. (Riana)
- Add improvement in percentage. (Kamil)

v3:
- Add xe_spin_sync_wait() with excess time adjusted accordingly. (Riana)

Fixes 477154cbad2c ("tests/intel/xe_pmu: Add tests to validate engine activity accuracy")
---
 tests/intel/xe_pmu.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)
tests/intel/xe_pmu.c +13 -4
index c8b3cebf0..33852270a 100644
@@ -534,6 +534,10 @@ static void accuracy(int fd, struct drm_xe_engine_class_instance *eci,
 		intel_allocator_init();
 		ahnd = intel_allocator_open(fd, 0, INTEL_ALLOCATOR_RELOC);
 
+		spin = igt_spin_new(fd, .ahnd = ahnd, .vm = vm, .hwe = eci);
+		xe_spin_end(spin->xe_spin);
+		xe_spin_sync_wait(fd, spin);
+
 		for (int pass = 0; pass < ARRAY_SIZE(timeout); pass++) {
 			unsigned int target_idle_us = idle_us;
 			struct timespec start = { };
@@ -546,21 +550,25 @@ static void accuracy(int fd, struct drm_xe_engine_class_instance *eci,
 
 			while (pass_ns < timeout[pass]) {
 				unsigned long loop_ns, loop_active_ns, loop_idle_ns, now;
+				unsigned long after_sync;
 				double err, prev_avg, cur_val;
 
 				/* idle sleep */
 				igt_measured_usleep(target_idle_us);
 
 				/* start spinner */
-				spin = igt_spin_new(fd, .ahnd = ahnd, .vm = vm, .hwe = eci);
+				xe_spin_reset(fd, spin);
 				loop_idle_ns = igt_nsec_elapsed(&start);
 				igt_measured_usleep(active_us);
-				igt_spin_free(fd, spin);
+				xe_spin_end(spin->xe_spin);
 
 				now = igt_nsec_elapsed(&start);
+				xe_spin_sync_wait(fd, spin);
+				after_sync = igt_nsec_elapsed(&start);
+
 				loop_active_ns = now - loop_idle_ns;
 				loop_ns = now - pass_ns;
-				pass_ns = now;
+				pass_ns = after_sync;
 
 				pass_active_ns += loop_active_ns;
 				total_active_ns += loop_active_ns;
@@ -590,6 +598,7 @@ static void accuracy(int fd, struct drm_xe_engine_class_instance *eci,
 				      sizeof(expected));
 		}
 
+		igt_spin_free(fd, spin);
 		xe_vm_destroy(fd, vm);
 		put_ahnd(ahnd);
 	}
@@ -622,7 +631,7 @@ static void accuracy(int fd, struct drm_xe_engine_class_instance *eci,
 	igt_info("error=%.2f%% (%.2f%% vs %.2f%%)\n",
 		 (engine_activity - expected) * 100, 100 * engine_activity, 100 * expected);
 
-	assert_within(100.0 * engine_activity, 100.0 * expected, 3);
+	assert_within(100.0 * engine_activity, 100.0 * expected, 2);
 }
 
 static void engine_activity_all_fn(int fd, struct drm_xe_engine_class_instance *eci, int num_fns)
Signature · 1 line
2.34.1