ff6046
From 60b831ef50e435b66ddd99e635a5112e121c7cb3 Mon Sep 17 00:00:00 2001
ff6046
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
ff6046
Date: Tue, 22 Jan 2019 15:43:07 +0100
ff6046
Subject: [PATCH] procfs-util: expose functionality to query total memory
ff6046
ff6046
procfs_memory_get_current is renamed to procfs_memory_get_used, because
ff6046
"current" can mean anything, including total memory, used memory, and free
ff6046
memory, as long as the value is up to date.
ff6046
ff6046
No functional change.
ff6046
ff6046
(cherry-picked from commit c482724aa5c5d0b1391fcf958a9a3ea6ce73a085)
ff6046
ff6046
Related: #1664976
ff6046
---
ff6046
 src/basic/procfs-util.c     | 9 +++++----
ff6046
 src/basic/procfs-util.h     | 5 ++++-
ff6046
 src/cgtop/cgtop.c           | 2 +-
ff6046
 src/core/cgroup.c           | 2 +-
ff6046
 src/test/test-procfs-util.c | 2 +-
ff6046
 5 files changed, 12 insertions(+), 8 deletions(-)
ff6046
ff6046
diff --git a/src/basic/procfs-util.c b/src/basic/procfs-util.c
ff6046
index a159e344b3..7aaf95bfce 100644
ff6046
--- a/src/basic/procfs-util.c
ff6046
+++ b/src/basic/procfs-util.c
ff6046
@@ -201,13 +201,11 @@ int procfs_cpu_get_usage(nsec_t *ret) {
ff6046
         return 0;
ff6046
 }
ff6046
 
ff6046
-int procfs_memory_get_current(uint64_t *ret) {
ff6046
+int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used) {
ff6046
         uint64_t mem_total = UINT64_MAX, mem_free = UINT64_MAX;
ff6046
         _cleanup_fclose_ FILE *f = NULL;
ff6046
         int r;
ff6046
 
ff6046
-        assert(ret);
ff6046
-
ff6046
         f = fopen("/proc/meminfo", "re");
ff6046
         if (!f)
ff6046
                 return -errno;
ff6046
@@ -262,6 +260,9 @@ int procfs_memory_get_current(uint64_t *ret) {
ff6046
         if (mem_free > mem_total)
ff6046
                 return -EINVAL;
ff6046
 
ff6046
-        *ret = (mem_total - mem_free) * 1024U;
ff6046
+        if (ret_total)
ff6046
+                *ret_total = mem_total * 1024U;
ff6046
+        if (ret_used)
ff6046
+                *ret_used = (mem_total - mem_free) * 1024U;
ff6046
         return 0;
ff6046
 }
ff6046
diff --git a/src/basic/procfs-util.h b/src/basic/procfs-util.h
ff6046
index f697ed92bc..5a44e9eff7 100644
ff6046
--- a/src/basic/procfs-util.h
ff6046
+++ b/src/basic/procfs-util.h
ff6046
@@ -11,4 +11,7 @@ int procfs_tasks_get_current(uint64_t *ret);
ff6046
 
ff6046
 int procfs_cpu_get_usage(nsec_t *ret);
ff6046
 
ff6046
-int procfs_memory_get_current(uint64_t *ret);
ff6046
+int procfs_memory_get(uint64_t *ret_total, uint64_t *ret_used);
ff6046
+static inline int procfs_memory_get_used(uint64_t *ret) {
ff6046
+        return procfs_memory_get(NULL, ret);
ff6046
+}
ff6046
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
ff6046
index 8dda08ab4c..792b13a43d 100644
ff6046
--- a/src/cgtop/cgtop.c
ff6046
+++ b/src/cgtop/cgtop.c
ff6046
@@ -297,7 +297,7 @@ static int process(
ff6046
         } else if (streq(controller, "memory")) {
ff6046
 
ff6046
                 if (is_root_cgroup(path)) {
ff6046
-                        r = procfs_memory_get_current(&g->memory);
ff6046
+                        r = procfs_memory_get_used(&g->memory);
ff6046
                         if (r < 0)
ff6046
                                 return r;
ff6046
                 } else {
ff6046
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
ff6046
index bb02436203..62ab41a288 100644
ff6046
--- a/src/core/cgroup.c
ff6046
+++ b/src/core/cgroup.c
ff6046
@@ -2402,7 +2402,7 @@ int unit_get_memory_current(Unit *u, uint64_t *ret) {
ff6046
 
ff6046
         /* The root cgroup doesn't expose this information, let's get it from /proc instead */
ff6046
         if (unit_has_root_cgroup(u))
ff6046
-                return procfs_memory_get_current(ret);
ff6046
+                return procfs_memory_get_used(ret);
ff6046
 
ff6046
         if ((u->cgroup_realized_mask & CGROUP_MASK_MEMORY) == 0)
ff6046
                 return -ENODATA;
ff6046
diff --git a/src/test/test-procfs-util.c b/src/test/test-procfs-util.c
ff6046
index 08af380cc7..1d0612985b 100644
ff6046
--- a/src/test/test-procfs-util.c
ff6046
+++ b/src/test/test-procfs-util.c
ff6046
@@ -18,7 +18,7 @@ int main(int argc, char *argv[]) {
ff6046
         assert_se(procfs_cpu_get_usage(&nsec) >= 0);
ff6046
         log_info("Current system CPU time: %s", format_timespan(buf, sizeof(buf), nsec/NSEC_PER_USEC, 1));
ff6046
 
ff6046
-        assert_se(procfs_memory_get_current(&v) >= 0);
ff6046
+        assert_se(procfs_memory_get_used(&v) >= 0);
ff6046
         log_info("Current memory usage: %s", format_bytes(buf, sizeof(buf), v));
ff6046
 
ff6046
         assert_se(procfs_tasks_get_current(&v) >= 0);