Skip to content

Commit fadf3c4

Browse files
poetteringbluca
authored andcommitted
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. (cherry picked from commit 90ec8eb) (cherry picked from commit 5ee19fd) (cherry picked from commit 8e62340) (cherry picked from commit cb4f512)
1 parent 5d0fb94 commit fadf3c4

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/shared/psi-util.c

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,25 @@ int read_resource_pressure(const char *path, PressureType type, ResourcePressure
106106
}
107107

108108
int is_pressure_supported(void) {
109+
static thread_local int cached = -1;
109110
const char *p;
111+
int r;
110112

111-
/* The pressure files, both under /proc and in cgroups, will exist
112-
* even if the kernel has PSI support disabled; we have to read
113-
* the file to make sure it doesn't return -EOPNOTSUPP */
114-
FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
115-
int r;
113+
/* The pressure files, both under /proc/ and in cgroups, will exist even if the kernel has PSI
114+
* support disabled; we have to read the file to make sure it doesn't return -EOPNOTSUPP */
116115

116+
if (cached >= 0)
117+
return cached;
118+
119+
FOREACH_STRING(p, "/proc/pressure/cpu", "/proc/pressure/io", "/proc/pressure/memory") {
117120
r = read_virtual_file(p, 0, NULL, NULL);
118-
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
119-
return 0;
120-
if (r < 0)
121+
if (r < 0) {
122+
if (r == -ENOENT || ERRNO_IS_NOT_SUPPORTED(r))
123+
return (cached = false);
124+
121125
return r;
126+
}
122127
}
123128

124-
return 1;
129+
return (cached = true);
125130
}

0 commit comments

Comments
 (0)