[lxc-devel] [lxcfs/master] Fix wrong calc of swaptoal and swapfree

yuwang888 on Github lxc-bot at linuxcontainers.org
Fri Oct 20 06:40:05 UTC 2017


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 683 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20171020/74cd7a20/attachment.bin>
-------------- next part --------------
From 4127e51bdc9dd46e8fa59be6a51c8a6e3333e313 Mon Sep 17 00:00:00 2001
From: "yuwang.yuwang" <yuwang.yuwang at alibaba-inc.com>
Date: Fri, 20 Oct 2017 14:28:03 +0800
Subject: [PATCH] Fix wrong calc of swaptoal and swapfree

it make the value of (memswlimit - memlimit) to be swaptotal,
it is wrong, because swapsize in cgroup/container can up to
[0,memswlimit], sometimes if the memsize(unless swap) of all tasks in
cgroup/container is very small, the swaptoal can to be memswlimit
so make the swaptotal to be min(host swtoal,memswlimit)

Signed-off-by: yuwang.yuwang <yuwang.yuwang at alibaba-inc.com>
---
 bindings.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/bindings.c b/bindings.c
index d7c2d1d..fe521a3 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3169,12 +3169,12 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			printme = lbuf;
 		} else if (startswith(line, "SwapTotal:") && memswlimit > 0) {
 			sscanf(line+sizeof("SwapTotal:")-1, "%lu", &hostswtotal);
-			if (hostswtotal < memswlimit - memlimit)
-				memswlimit = hostswtotal + memlimit;
-			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", memswlimit - memlimit);
+			if (hostswtotal < memswlimit)
+				memswlimit = hostswtotal;
+			snprintf(lbuf, 100, "SwapTotal:      %8lu kB\n", memswlimit);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0) {
-			unsigned long swaptotal = memswlimit - memlimit,
+			unsigned long swaptotal = memswlimit,
 					swapusage = memswusage - memusage,
 					swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
 			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", swapfree);


More information about the lxc-devel mailing list