]> www.infradead.org Git - users/hch/misc.git/commitdiff
selftests: cgroup: add values_close_report helper
authorSebastian Chlad <sebastianchlad@gmail.com>
Wed, 15 Oct 2025 10:33:56 +0000 (12:33 +0200)
committerTejun Heo <tj@kernel.org>
Wed, 15 Oct 2025 15:00:49 +0000 (05:00 -1000)
Some cgroup selftests, such as test_cpu, occasionally fail by a very
small margin and if run in the CI context, it is useful to have detailed
diagnostic output to understand the deviation.

Introduce a values_close_report() helper which performs the same
comparison as values_close(), but prints detailed information when the
values differ beyond the allowed tolerance.

Signed-off-by: Sebastian Chlad <sebastian.chlad@suse.com>
Signed-off-by: Tejun Heo <tj@kernel.org>
tools/testing/selftests/cgroup/lib/include/cgroup_util.h

index 9dc90a1b386d7771cb43035630195bd9ffd56ef8..7ab2824ed7b54d4ba2217cf4bd3d373bceb27c7f 100644 (file)
@@ -25,6 +25,26 @@ static inline int values_close(long a, long b, int err)
        return labs(a - b) <= (a + b) / 100 * err;
 }
 
+/*
+ * Checks if two given values differ by less than err% of their sum and assert
+ * with detailed debug info if not.
+ */
+static inline int values_close_report(long a, long b, int err)
+{
+       long diff  = labs(a - b);
+       long limit = (a + b) / 100 * err;
+       double actual_err = (a + b) ? (100.0 * diff / (a + b)) : 0.0;
+       int close = diff <= limit;
+
+       if (!close)
+               fprintf(stderr,
+                       "[FAIL] actual=%ld expected=%ld | diff=%ld | limit=%ld | "
+                       "tolerance=%d%% | actual_error=%.2f%%\n",
+                       a, b, diff, limit, err, actual_err);
+
+       return close;
+}
+
 extern ssize_t read_text(const char *path, char *buf, size_t max_len);
 extern ssize_t write_text(const char *path, char *buf, ssize_t len);