basic/procfs-util: add procfs_cpu_get_ticks()

This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-08-21 09:17:51 +02:00
parent aa1045f862
commit efa45f5f47
3 changed files with 54 additions and 0 deletions
+33
View File
@@ -171,6 +171,39 @@ int procfs_cpu_get_usage(nsec_t *ret) {
return 0;
}
int procfs_cpu_get_ticks(ProcfsCpuTicks *ret) {
_cleanup_free_ char *first_line = NULL;
int r;
assert(ret);
r = read_one_line_file("/proc/stat", &first_line);
if (r < 0)
return r;
const char *p = first_word(first_line, "cpu");
if (!p)
return -EINVAL;
/* All eight fields exist on any kernel we support: iowait was added in 2.5.41, steal in 2.6.11.
* "guest" and "guest_nice" are not returned, the kernel already includes them in "user" and
* "nice". */
ProcfsCpuTicks t;
if (sscanf(p, "%"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64" %"SCNu64,
&t.user,
&t.nice,
&t.system,
&t.idle,
&t.iowait,
&t.irq,
&t.softirq,
&t.steal) < 8)
return -EINVAL;
*ret = t;
return 0;
}
int convert_meminfo_value_to_uint64_bytes(const char *s, uint64_t *ret) {
_cleanup_free_ char *w = NULL;
uint64_t v;
+13
View File
@@ -11,6 +11,19 @@ int procfs_tasks_get_current(uint64_t *ret);
int procfs_cpu_get_usage(nsec_t *ret);
typedef struct ProcfsCpuTicks {
uint64_t user;
uint64_t nice;
uint64_t system;
uint64_t idle;
uint64_t iowait;
uint64_t irq;
uint64_t softirq;
uint64_t steal;
} ProcfsCpuTicks;
int procfs_cpu_get_ticks(ProcfsCpuTicks *ret);
int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used);
static inline int procfs_memory_get_used(uint64_t *ret) {
return procfs_memory_get(NULL, ret);