[PATCH igt] igt/perf_pmu: Avoid underflow in measured_sleep()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Be careful not to underflow into a very large positive usec value and so
sleep forever^W until boredom kicks in.

Signed-off-by: Chris Wilson <chris@xxxxxxxxxxxxxxxxxx>
Cc: Tvrtko Ursulin <tvrtko.ursulin@xxxxxxxxx>
---
 tests/perf_pmu.c | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/tests/perf_pmu.c b/tests/perf_pmu.c
index 5d5f2b30..e153aaf3 100644
--- a/tests/perf_pmu.c
+++ b/tests/perf_pmu.c
@@ -111,22 +111,17 @@ static void pmu_read_multi(int fd, unsigned int num, uint64_t *val)
  */
 static unsigned int measured_usleep(unsigned int usec)
 {
-	uint64_t slept = 0;
-
-	while (usec > 0) {
-		struct timespec start = { };
-		uint64_t this_sleep;
+	struct timespec ts = { };
+	unsigned int slept;
 
-		igt_nsec_elapsed(&start);
-		usleep(usec);
-		this_sleep = igt_nsec_elapsed(&start);
-		slept += this_sleep;
-		if (this_sleep > usec * 1000)
-			break;
-		usec -= this_sleep;
-	}
+	slept = igt_nsec_elapsed(&ts);
+	igt_assert(slept == 0);
+	do {
+		usleep(usec - slept);
+		slept = igt_nsec_elapsed(&ts) / 1000;
+	} while (slept < usec);
 
-	return slept;
+	return igt_nsec_elapsed(&ts);
 }
 
 static unsigned int e2ring(int gem_fd, const struct intel_execution_engine2 *e)
-- 
2.15.0

_______________________________________________
Intel-gfx mailing list
Intel-gfx@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/intel-gfx




[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]
  Powered by Linux