mirror of
https://github.com/systemd/systemd.git
synced 2025-03-13 00:58:27 +03:00
procfs-util: expose functionality to query total memory
procfs_memory_get_current is renamed to procfs_memory_get_used, because "current" can mean anything, including total memory, used memory, and free memory, as long as the value is up to date. No functional change.
This commit is contained in:
parent
04ba6ed167
commit
c482724aa5
@ -201,13 +201,11 @@ int procfs_cpu_get_usage(nsec_t *ret) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int procfs_memory_get_current(uint64_t *ret) {
|
int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
|
||||||
uint64_t mem_total = UINT64_MAX, mem_free = UINT64_MAX;
|
uint64_t mem_total = UINT64_MAX, mem_free = UINT64_MAX;
|
||||||
_cleanup_fclose_ FILE *f = NULL;
|
_cleanup_fclose_ FILE *f = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(ret);
|
|
||||||
|
|
||||||
f = fopen("/proc/meminfo", "re");
|
f = fopen("/proc/meminfo", "re");
|
||||||
if (!f)
|
if (!f)
|
||||||
return -errno;
|
return -errno;
|
||||||
@ -262,6 +260,9 @@ int procfs_memory_get_current(uint64_t *ret) {
|
|||||||
if (mem_free > mem_total)
|
if (mem_free > mem_total)
|
||||||
return -EINVAL;
|
return -EINVAL;
|
||||||
|
|
||||||
*ret = (mem_total - mem_free) * 1024U;
|
if (ret_total)
|
||||||
|
*ret_total = mem_total * 1024U;
|
||||||
|
if (ret_used)
|
||||||
|
*ret_used = (mem_total - mem_free) * 1024U;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,7 @@ int procfs_tasks_get_current(uint64_t *ret);
|
|||||||
|
|
||||||
int procfs_cpu_get_usage(nsec_t *ret);
|
int procfs_cpu_get_usage(nsec_t *ret);
|
||||||
|
|
||||||
int procfs_memory_get_current(uint64_t *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);
|
||||||
|
}
|
||||||
|
@ -291,7 +291,7 @@ static int process(
|
|||||||
} else if (streq(controller, "memory")) {
|
} else if (streq(controller, "memory")) {
|
||||||
|
|
||||||
if (is_root_cgroup(path)) {
|
if (is_root_cgroup(path)) {
|
||||||
r = procfs_memory_get_current(&g->memory);
|
r = procfs_memory_get_used(&g->memory);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
} else {
|
} else {
|
||||||
|
@ -2780,7 +2780,7 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) {
|
|||||||
|
|
||||||
/* The root cgroup doesn't expose this information, let's get it from /proc instead */
|
/* The root cgroup doesn't expose this information, let's get it from /proc instead */
|
||||||
if (unit_has_host_root_cgroup(u))
|
if (unit_has_host_root_cgroup(u))
|
||||||
return procfs_memory_get_current(ret);
|
return procfs_memory_get_used(ret);
|
||||||
|
|
||||||
if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
|
if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
|
||||||
return -ENODATA;
|
return -ENODATA;
|
||||||
|
@ -18,7 +18,7 @@ int main(int argc, char *argv[]) {
|
|||||||
assert_se(procfs_cpu_get_usage(&nsec) >= 0);
|
assert_se(procfs_cpu_get_usage(&nsec) >= 0);
|
||||||
log_info("Current system CPU time: %s", format_timespan(buf, sizeof(buf), nsec/NSEC_PER_USEC, 1));
|
log_info("Current system CPU time: %s", format_timespan(buf, sizeof(buf), nsec/NSEC_PER_USEC, 1));
|
||||||
|
|
||||||
assert_se(procfs_memory_get_current(&v) >= 0);
|
assert_se(procfs_memory_get_used(&v) >= 0);
|
||||||
log_info("Current memory usage: %s", format_bytes(buf, sizeof(buf), v));
|
log_info("Current memory usage: %s", format_bytes(buf, sizeof(buf), v));
|
||||||
|
|
||||||
assert_se(procfs_tasks_get_current(&v) >= 0);
|
assert_se(procfs_tasks_get_current(&v) >= 0);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user