]>
git.proxmox.com Git - systemd.git/blob - src/shared/psi-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include "alloc-util.h"
7 #include "errno-util.h"
8 #include "extract-word.h"
11 #include "parse-util.h"
13 #include "string-util.h"
14 #include "stat-util.h"
17 int read_resource_pressure(const char *path
, PressureType type
, ResourcePressure
*ret
) {
18 _cleanup_free_
char *line
= NULL
;
19 _cleanup_fclose_
FILE *f
= NULL
;
20 unsigned field_filled
= 0;
21 ResourcePressure rp
= {};
22 const char *t
, *cline
;
27 assert(IN_SET(type
, PRESSURE_TYPE_SOME
, PRESSURE_TYPE_FULL
));
30 if (type
== PRESSURE_TYPE_SOME
)
32 else if (type
== PRESSURE_TYPE_FULL
)
37 r
= fopen_unlocked(path
, "re", &f
);
42 _cleanup_free_
char *l
= NULL
;
45 r
= read_line(f
, LONG_LINE_MAX
, &l
);
62 /* extracts either avgX=Y.Z or total=X */
63 while ((r
= extract_first_word(&cline
, &word
, NULL
, 0)) > 0) {
64 _cleanup_free_
char *w
= word
;
67 if ((v
= startswith(w
, "avg10="))) {
68 if (field_filled
& (1U << 0))
71 field_filled
|= 1U << 0;
72 r
= parse_loadavg_fixed_point(v
, &rp
.avg10
);
73 } else if ((v
= startswith(w
, "avg60="))) {
74 if (field_filled
& (1U << 1))
77 field_filled
|= 1U << 1;
78 r
= parse_loadavg_fixed_point(v
, &rp
.avg60
);
79 } else if ((v
= startswith(w
, "avg300="))) {
80 if (field_filled
& (1U << 2))
83 field_filled
|= 1U << 2;
84 r
= parse_loadavg_fixed_point(v
, &rp
.avg300
);
85 } else if ((v
= startswith(w
, "total="))) {
86 if (field_filled
& (1U << 3))
89 field_filled
|= 1U << 3;
90 r
= safe_atou64(v
, &rp
.total
);
101 if (field_filled
!= 15U)
108 int is_pressure_supported(void) {
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") {
117 r
= read_virtual_file(p
, 0, NULL
, NULL
);
118 if (r
== -ENOENT
|| ERRNO_IS_NOT_SUPPORTED(r
))