From 90ec8ebe33ec72ed6d9f451de9443d67dd351d72 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 27 Feb 2023 19:02:41 +0100 Subject: [PATCH] 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. Reference: https://github.com/systemd/systemd/pull/26610/commits/1ea90a9d409999159345ad8e193f7ef4cc7f634d Signed-off-by: zhaoxiaohu Signed-off-by: yuwang --- src/shared/psi-util.c | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/shared/psi-util.c b/src/shared/psi-util.c index 009095e..9045ae2 100644 --- a/src/shared/psi-util.c +++ b/src/shared/psi-util.c @@ -106,20 +106,25 @@ int read_resource_pressure(const char *path, PressureType type, ResourcePressure } int is_pressure_supported(void) { + static thread_local int cached = -1; const char *p; + 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 */ - FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") { - 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); } -- 2.33.0