summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2023-02-27 19:02:41 +0100
committerLuca Boccassi <luca.boccassi@gmail.com>2023-02-28 12:16:59 +0000
commit90ec8ebe33ec72ed6d9f451de9443d67dd351d72 (patch)
tree0e0713f48ee5b1845ad83b2d6fdb017e9f403e11 /src/basic
parent9897f5ddeae5ad4eafae15c8dd63056622458be5 (diff)
downloadsystemd-90ec8ebe33ec72ed6d9f451de9443d67dd351d72.tar.gz
psi-util: fix error handling
We checked ERRNO_IS_NOT_SUPPORTED on a possible positive non-error code, which isn't right. Fix that. Also add caching, since we are about to call this more often.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/psi-util.c24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/basic/psi-util.c b/src/basic/psi-util.c
index 8bdd0d4a85..a3b553cb44 100644
--- a/src/basic/psi-util.c
+++ b/src/basic/psi-util.c
@@ -106,18 +106,24 @@ int read_resource_pressure(const char *path, PressureType type, ResourcePressure
}
int is_pressure_supported(void) {
- /* The pressure files, both under /proc and in cgroups, will exist
- * even if the kernel has PSI support disabled; we have to read
- * the file to make sure it doesn't return -EOPNOTSUPP */
- FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
- int r;
+ static thread_local int cached = -1;
+ int r;
+
+ /* The pressure files, both under /proc/ and in cgroups, will exist even if the kernel has PSI
+ * support disabled; we have to read the file to make sure it doesn't return -EOPNOTSUPP */
+
+ if (cached >= 0)
+ return cached;
+ FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
r = read_virtual_file(p, 0, NULL, NULL);
- if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
- return 0;
- if (r < 0)
+ if (r < 0) {
+ if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
+ return (cached = false);
+
return r;
+ }
}
- return 1;
+ return (cached = true);
}