Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests: cgroup: add values_close_report helper

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>

authored by

Sebastian Chlad and committed by
Tejun Heo
3f9c60f4 93a4b36e

+20
+20
tools/testing/selftests/cgroup/lib/include/cgroup_util.h
··· 25 25 return labs(a - b) <= (a + b) / 100 * err; 26 26 } 27 27 28 + /* 29 + * Checks if two given values differ by less than err% of their sum and assert 30 + * with detailed debug info if not. 31 + */ 32 + static inline int values_close_report(long a, long b, int err) 33 + { 34 + long diff = labs(a - b); 35 + long limit = (a + b) / 100 * err; 36 + double actual_err = (a + b) ? (100.0 * diff / (a + b)) : 0.0; 37 + int close = diff <= limit; 38 + 39 + if (!close) 40 + fprintf(stderr, 41 + "[FAIL] actual=%ld expected=%ld | diff=%ld | limit=%ld | " 42 + "tolerance=%d%% | actual_error=%.2f%%\n", 43 + a, b, diff, limit, err, actual_err); 44 + 45 + return close; 46 + } 47 + 28 48 extern ssize_t read_text(const char *path, char *buf, size_t max_len); 29 49 extern ssize_t write_text(const char *path, char *buf, ssize_t len); 30 50