daandemeyer / rpms / systemd

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