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

Configure Feed

Select the types of activity you want to include in your feed.

selftests: cgroup: Add cg_read_key_long_poll() to poll a cgroup key with retries

Introduce a new helper function `cg_read_key_long_poll()` in
cgroup_util.h. This function polls the specified key in a cgroup file
until it matches the expected value or the retry limit is reached,
with configurable wait intervals between retries.

This helper is particularly useful for handling asynchronously updated
cgroup statistics (e.g., memory.stat), where immediate reads may
observe stale values, especially on busy systems. It allows tests and
other utilities to handle such cases more flexibly.

Signed-off-by: Guopeng Zhang <zhangguopeng@kylinos.cn>
Suggested-by: Michal Koutný <mkoutny@suse.com>
Reviewed-by: Shakeel Butt <shakeel.butt@linux.dev>
Acked-by: Michal Koutný <mkoutny@suse.com>
Signed-off-by: Tejun Heo <tj@kernel.org>

authored by

Guopeng Zhang and committed by
Tejun Heo
311ead1b 3309b63a

+26
+21
tools/testing/selftests/cgroup/lib/cgroup_util.c
··· 168 168 return atol(ptr + strlen(key)); 169 169 } 170 170 171 + long cg_read_key_long_poll(const char *cgroup, const char *control, 172 + const char *key, long expected, int retries, 173 + useconds_t wait_interval_us) 174 + { 175 + long val = -1; 176 + int i; 177 + 178 + for (i = 0; i < retries; i++) { 179 + val = cg_read_key_long(cgroup, control, key); 180 + if (val < 0) 181 + return val; 182 + 183 + if (val == expected) 184 + break; 185 + 186 + usleep(wait_interval_us); 187 + } 188 + 189 + return val; 190 + } 191 + 171 192 long cg_read_lc(const char *cgroup, const char *control) 172 193 { 173 194 char buf[PAGE_SIZE];
+5
tools/testing/selftests/cgroup/lib/include/cgroup_util.h
··· 17 17 #define CG_NAMED_NAME "selftest" 18 18 #define CG_PATH_FORMAT (!cg_test_v1_named ? "0::%s" : (":name=" CG_NAMED_NAME ":%s")) 19 19 20 + #define DEFAULT_WAIT_INTERVAL_US (100 * 1000) /* 100 ms */ 21 + 20 22 /* 21 23 * Checks if two given values differ by less than err% of their sum. 22 24 */ ··· 66 64 extern long cg_read_long(const char *cgroup, const char *control); 67 65 extern long cg_read_long_fd(int fd); 68 66 long cg_read_key_long(const char *cgroup, const char *control, const char *key); 67 + long cg_read_key_long_poll(const char *cgroup, const char *control, 68 + const char *key, long expected, int retries, 69 + useconds_t wait_interval_us); 69 70 extern long cg_read_lc(const char *cgroup, const char *control); 70 71 extern int cg_write(const char *cgroup, const char *control, char *buf); 71 72 extern int cg_open(const char *cgroup, const char *control, int flags);