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:
Zbigniew Jędrzejewski-Szmek 2019-01-22 15:43:07 +01:00
parent 04ba6ed167
commit c482724aa5
5 changed files with 12 additions and 8 deletions

View File

@ -201,13 +201,11 @@ int procfs_cpu_get_usage(nsec_t *ret) {
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;
_cleanup_fclose_ FILE *f = NULL;
int r;
assert(ret);
f = fopen("/proc/meminfo", "re");
if (!f)
return -errno;
@ -262,6 +260,9 @@ int procfs_memory_get_current(uint64_t *ret) {
if (mem_free > mem_total)
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;
}

View File

@ -11,4 +11,7 @@ int procfs_tasks_get_current(uint64_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);
}

View File

@ -291,7 +291,7 @@ static int process(
} else if (streq(controller, "memory")) {
if (is_root_cgroup(path)) {
r = procfs_memory_get_current(&g->memory);
r = procfs_memory_get_used(&g->memory);
if (r < 0)
return r;
} else {

View File

@ -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 */
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)
return -ENODATA;

View File

@ -18,7 +18,7 @@ int main(int argc, char *argv[]) {
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));
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));
assert_se(procfs_tasks_get_current(&v) >= 0);