Skip to contentSkip to conversation

lib: add a checked-attribute counter to sysfs assert helpers

PATCH In review Li Wang (Hieco) 2 participants 5 messages Oct 21 – Oct 22 Library code

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/2 lib: add a checked-attribute counter to sysfs assert helpers

The TST_SYSFS_ASSERT_*_SILENT() assertions skip silently when the attribute does not exist. A test that only uses SILENT assertions can therefore finish without reporting a single result on machines where none of the probed attributes are present, which the library turns into:

tst_test.c:1730: TBROK: Test haven't reported results!

Add a counter that is bumped every time a SILENT assertion finds the attribute and actually checks it, regardless of whether the check passes or fails (failures produce TFAIL on their own). Tests can reset it at the start, then report a TPASS summary or a TCONF based on the final count.

---

Notes:
v4:
- drop the silent_attr_exists() helper, bump the counter directly after the existence check in each silent assertion, which also keeps the cmp_silent counting consistent.

 include/tst_sysfs_assert.h | 11 +++++++++++
 lib/tst_sysfs_assert.c     | 20 ++++++++++++++++++++
 2 files changed, 31 insertions(+)
include/tst_sysfs_assert.h +11 -0
index b21c09849..d2a010d80 100644
@@ -649,4 +649,15 @@ long tst_sysfs_read_li(const char *file, const int lineno,
 unsigned long tst_sysfs_read_lx(const char *file, const int lineno,
 	const char *fmt, ...);
 
+/**
+ * tst_sysfs_cnt_reset() - Reset the checked-attribute counter.
+ */
+void tst_sysfs_cnt_reset(void);
+
+/**
+ * tst_sysfs_cnt_get() - Number of attributes the *_SILENT assertions
+ * found and checked (pass or fail) since the last reset.
+ */
+unsigned int tst_sysfs_cnt_get(void);
+
 #endif /* TST_SYSFS_ASSERT_H__ */
lib/tst_sysfs_assert.c +20 -0
index 98680e976..4a34acc35 100644
@@ -15,6 +15,8 @@
 #include "tst_test.h"
 #include "tst_sysfs_assert.h"
 
+static unsigned int tst_sysfs_cnt;
+
 static void build_path(char *path, size_t size, const char *fmt, va_list ap)
 {
 	vsnprintf(path, size, fmt, ap);
@@ -98,6 +100,8 @@ void tst_sysfs_assert_rangell_silent(const char *file, const int lineno,
 	if (access(path, F_OK))
 		return;
 
+	tst_sysfs_cnt++;
+
 	range_check(file, lineno, path, min, max);
 }
 
@@ -156,6 +160,8 @@ void tst_sysfs_assert_rangeuu_silent(const char *file, const int lineno,
 	if (access(path, F_OK))
 		return;
 
+	tst_sysfs_cnt++;
+
 	range_check_u(file, lineno, path, min, max);
 }
 
@@ -242,6 +248,8 @@ void tst_sysfs_assert_bool_silent(const char *file, const int lineno,
 	if (access(path, F_OK))
 		return;
 
+	tst_sysfs_cnt++;
+
 	bool_check(file, lineno, path);
 }
 
@@ -684,6 +692,8 @@ void tst_sysfs_assert_cmp_silent(const char *file, const int lineno,
 	if (access(path1, F_OK) || access(path2, F_OK))
 		return;
 
+	tst_sysfs_cnt++;
+
 	tst_sysfs_assert_cmp(file, lineno, path1, op, path2);
 }
 
@@ -869,3 +879,13 @@ unsigned long tst_sysfs_read_lx(const char *file, const int lineno,
 
 	return val;
 }
+
+void tst_sysfs_cnt_reset(void)
+{
+	tst_sysfs_cnt = 0;
+}
+
+unsigned int tst_sysfs_cnt_get(void)
+{
+	return tst_sysfs_cnt;
+}
Signature · 5 lines
2.56.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH v4 2/2 sys_hwmon01: always report a result

On machines where the hwmon devices expose no checkable attributes (e.g. a VM or a laptop where hwmon0 is just 'ACAD' with nothing but a name attribute), every *_SILENT assertion skips silently and the test finishes without reporting a single result, which the library turns into:

tst_test.c:1730: TBROK: Test haven't reported results!

Fix this by using the new tst_sysfs_cnt_*() helpers to count the attributes that actually exist and get validated, then reporting a TPASS summary at the end, or TCONF when hwmon devices are present but expose nothing we can check.

While at it, fix a preexisting off-by-one in the inN channel loop:
probe in0..in31 so that all channel types probe exactly MAX_CHANNELS indices.

---

Notes:
v4:
- no change.
v3:
- move the counter into the sysfs library as tst_sysfs_cnt_reset()/tst_sysfs_cnt_get(), drop the CHECK_* wrapper macros and the extra existence probe (Cyril)
v2:
- rename attr_exists() to sysfs_attr_exists() (Xiaochen)
- flatten the final result reporting with an early return (Xiaochen)
- fix a preexisting off-by-one in the inN channel loop (Xiaochen)

 .../kernel/sysfs/class/hwmon/sys_hwmon01.c    | 21 +++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
testcases/kernel/sysfs/class/hwmon/sys_hwmon01.c +17 -4
index 1bb574c23..482bd7a23 100644
@@ -131,7 +131,7 @@ static void check_hwmon_device(const char *hwmon)
 					PATH_CLASS_HWMON "/%s/update_interval", hwmon);
 
 	/* inN channels are numbered from 0, per the hwmon ABI */
-	for (i = 0; i <= MAX_CHANNELS; i++)
+	for (i = 0; i < MAX_CHANNELS; i++)
 		check_in_channel(hwmon, i);
 
 	for (i = 1; i <= MAX_CHANNELS; i++) {
@@ -147,7 +147,9 @@ static void do_test(void)
 {
 	DIR *d;
 	struct dirent *ent;
-	int found = 0;
+	int devices = 0;
+
+	tst_sysfs_cnt_reset();
 
 	if (!tst_sysfs_exists(PATH_CLASS_HWMON))
 		tst_brk(TCONF, PATH_CLASS_HWMON ": not present");
@@ -158,14 +160,25 @@ static void do_test(void)
 		if (strncmp(ent->d_name, "hwmon", 5))
 			continue;
 
-		found = 1;
+		devices++;
 		check_hwmon_device(ent->d_name);
 	}
 
 	SAFE_CLOSEDIR(d);
 
-	if (!found)
+	if (!devices) {
 		tst_res(TCONF, "No hwmon device found");
+		return;
+	}
+
+	if (!tst_sysfs_cnt_get()) {
+		tst_res(TCONF, "%d hwmon devices present, but no checkable attributes",
+				devices);
+		return;
+	}
+
+	tst_res(TPASS, "Validated %u attributes on %d hwmon devices",
+			tst_sysfs_cnt_get(), devices);
 }
 
 static struct tst_test test = {
Signature · 5 lines
2.56.0


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH v4 2/2] sys_hwmon01: always report a result

Hi!

Quoted 4 lines

v2:
- rename attr_exists() to sysfs_attr_exists() (Xiaochen)
- flatten the final result reporting with an early return (Xiaochen)
- fix a preexisting off-by-one in the inN channel loop (Xiaochen)

It's very minor however unrelated fixes should be ideally done in a separate patches.

Feel free to push with my Reviewed-by: Cyril Hrubis <[email protected]> with the off-by-one split into separate patch.

Signature · 5 lines
Cyril Hrubis
[email protected]

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

Re: [LTP] [PATCH v4 2/2] sys_hwmon01: always report a result

Quoted 8 lines

Hi!

Quoted 4 lines

v2:
- rename attr_exists() to sysfs_attr_exists() (Xiaochen)
- flatten the final result reporting with an early return (Xiaochen)
- fix a preexisting off-by-one in the inN channel loop (Xiaochen)

It's very minor however unrelated fixes should be ideally done in a separate patches.

+1

Quoted 2 lines

Feel free to push with my Reviewed-by: Cyril Hrubis <[email protected]> with the off-by-one split into separate patch.

Patchset merged, thanks!

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

Re: [LTP] [PATCH v4 2/2] sys_hwmon01: always report a result

Quoted 4 lines
Quoted 2 lines

Feel free to push with my Reviewed-by: Cyril Hrubis <[email protected]> with the off-by-one split into separate patch.

Patchset merged, thanks!

Oops, I fixed the off-by-one but pushed without creating a new commit, sorry about that.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp