Linux cgroups development
 help / color / mirror / Atom feed
From: Sebastian Chlad <sebastianchlad@gmail.com>
To: cgroups@vger.kernel.org
Cc: mkoutny@suse.com, Sebastian Chlad <sebastian.chlad@suse.com>
Subject: [PATCH 1/2] selftests: cgroup: add values_close_assert helper
Date: Tue, 14 Oct 2025 16:31:50 +0200	[thread overview]
Message-ID: <20251014143151.5790-2-sebastian.chlad@suse.com> (raw)
In-Reply-To: <20251014143151.5790-1-sebastian.chlad@suse.com>

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_assert() 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>
---
 .../cgroup/lib/include/cgroup_util.h          | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
index 9dc90a1b386d..1ed0c6d96c66 100644
--- a/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
+++ b/tools/testing/selftests/cgroup/lib/include/cgroup_util.h
@@ -25,6 +25,27 @@ 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_assert(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);
 
-- 
2.51.0


  reply	other threads:[~2025-10-14 14:32 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-14 14:31 [PATCH 0/2] selftests: cgroup: improve diagnostics for CPU test failures Sebastian Chlad
2025-10-14 14:31 ` Sebastian Chlad [this message]
2025-10-14 19:32   ` [PATCH 1/2] selftests: cgroup: add values_close_assert helper Tejun Heo
2025-10-14 14:31 ` [PATCH 2/2] selftests: cgroup: Use values_close_assert in test_cpu Sebastian Chlad
2025-10-15  8:00 ` [PATCH v2 0/2] selftests: cgroup: improve diagnostics for CPU test failures Sebastian Chlad
2025-10-15  8:00   ` [PATCH v2 1/2] selftests: cgroup: add values_close_assert helper Sebastian Chlad
2025-10-15  8:00   ` [PATCH v2 2/2] selftests: cgroup: Use values_close_assert in test_cpu Sebastian Chlad
2025-10-15 10:33   ` [PATCH v3 0/2] selftests: cgroup: improve diagnostics for CPU test failures Sebastian Chlad
2025-10-15 10:33     ` [PATCH v3 1/2] selftests: cgroup: add values_close_report helper Sebastian Chlad
2025-10-15 10:33     ` [PATCH v3 2/2] selftests: cgroup: Use values_close_report in test_cpu Sebastian Chlad
2025-10-15 15:00     ` [PATCH v3 0/2] selftests: cgroup: improve diagnostics for CPU test failures Tejun Heo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20251014143151.5790-2-sebastian.chlad@suse.com \
    --to=sebastianchlad@gmail.com \
    --cc=cgroups@vger.kernel.org \
    --cc=mkoutny@suse.com \
    --cc=sebastian.chlad@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox