[lxc-devel] [lxcfs/master] proc_fuse: more swap tweaks

brauner on Github lxc-bot at linuxcontainers.org
Tue Jun 9 13:39:29 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 429 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200609/be635692/attachment.bin>
-------------- next part --------------
From b91ec30a9fb50c11b45c57e3c61e5f9a1593f832 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 9 Jun 2020 15:38:15 +0200
Subject: [PATCH] proc_fuse: more swap tweaks

Essentially, we stop being clever. This is just a nightmare.

Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
 src/proc_fuse.c | 57 +++++++++++++++++++------------------------------
 1 file changed, 22 insertions(+), 35 deletions(-)

diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 9b60be4..4c51385 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -1053,12 +1053,18 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 
 	prune_init_slice(cgroup);
 
-	memlimit = get_min_memlimit(cgroup, false);
-
+	/* memory limits */
 	ret = cgroup_ops->get_memory_current(cgroup_ops, cgroup, &memusage_str);
 	if (ret < 0)
 		return read_file_fuse("/proc/meminfo", buf, size, d);
 
+	if (safe_uint64(memusage_str, &memusage, 10) < 0)
+		lxcfs_error("Failed to convert memusage %s", memusage_str);
+	memusage /= 1024;
+
+	memlimit = get_min_memlimit(cgroup, false);
+	memlimit /= 1024;
+
 	if (!cgroup_parse_memory_stat(cgroup, &mstat))
 		return read_file_fuse("/proc/meminfo", buf, size, d);
 
@@ -1067,11 +1073,12 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 	 * turned off for current kernel.
 	 */
 	if (wants_swap) {
+		/* swap limits */
 		ret = cgroup_ops->get_memory_swap_max(cgroup_ops, cgroup, &memswlimit_str);
 		if (ret >= 0)
 			ret = cgroup_ops->get_memory_swap_current(cgroup_ops, cgroup, &memswusage_str);
 		if (ret >= 0) {
-			struct sysinfo info;
+			uint64_t swusage = 0;
 
 			memswlimit = get_min_memlimit(cgroup, true);
 			memswlimit = memswlimit / 1024;
@@ -1079,23 +1086,23 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			if (safe_uint64(memswusage_str, &memswusage, 10) < 0)
 				lxcfs_error("Failed to convert memswusage %s", memswusage_str);
 
-			ret = sysinfo(&info);
-			if (!ret) {
-				if (info.totalswap < memswlimit)
-					memswlimit = info.totalswap;
-				swfree = info.freeswap;
-			}
-
 			memswusage = memswusage / 1024;
 			swtotal = memswlimit;
+			if (memswusage > memusage)
+				swusage = memswusage - memusage;
+			else
+				swusage = memswusage;
+
+			/* Simply show 0. Maybe someone has a smarter idea. */
+			if (memswlimit < swusage) {
+				swtotal = 0;
+				wants_swap = false;
+			} else {
+				swfree = swtotal - swusage;
+			}
 		}
 	}
 
-	if (safe_uint64(memusage_str, &memusage, 10) < 0)
-		lxcfs_error("Failed to convert memusage %s", memusage_str);
-	memlimit /= 1024;
-	memusage /= 1024;
-
 	f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
 	if (!f)
 		return read_file_fuse("/proc/meminfo", buf, size, d);
@@ -1124,26 +1131,6 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " kB\n", swtotal);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:")) {
-			if (swtotal > 0 && wants_swap) {
-				uint64_t swusage = 0;
-
-				swusage = memswusage - memusage;
-				/*
-				 * Free swap has already been capped to the
-				 * system limit above.
-				 */
-				if (swtotal >= swusage)
-					swfree = swtotal - swusage;
-
-				/*
-				 * If swap total is not at least as big as what
-				 * swap free indicates, swap usage calculation
-				 * will be wrong. In that case, simply show
-				 * swap free as 0.
-				 */
-				if (swtotal < swfree)
-					swfree = 0;
-			}
 			snprintf(lbuf, 100, "SwapFree:       %8" PRIu64 " kB\n", swfree);
 			printme = lbuf;
 		} else if (startswith(line, "Slab:")) {


More information about the lxc-devel mailing list