[lxc-devel] [lxcfs/master] handle swap reporting better when swap accounting is enabled

KellenRenshaw on Github lxc-bot at linuxcontainers.org
Fri Jan 24 20:41:05 UTC 2020


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 576 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200124/1b6347a3/attachment.bin>
-------------- next part --------------
From 56a6b30b18aadd839a49eefb716a41d7fcda85d9 Mon Sep 17 00:00:00 2001
From: Kellen Renshaw <kellen.renshaw at canonical.com>
Date: Fri, 24 Jan 2020 13:28:43 -0700
Subject: [PATCH 1/2] bindings: Adjusts the logic for calculating SwapFree to
 better handle conditions where swap usage is reported to be <0.

Signed-off-by: Kellen Renshaw <kellen.renshaw at canonical.com>
---
 bindings.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/bindings.c b/bindings.c
index d11f0e5..3e939f5 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3599,8 +3599,8 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0 && opts && opts->swap_off == false) {
 			unsigned long swaptotal = memswlimit,
-					swapusage = memswusage - memusage,
-					swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
+					swapusage = memusage > memswusage ? 0 : memswusage - memusage,
+					swapfree = swaptotal - swapusage;
 			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", swapfree);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && opts && opts->swap_off == true) {

From b6ae646f1fad3ac8fa5d7af114c0362b88f878f9 Mon Sep 17 00:00:00 2001
From: Kellen Renshaw <kellen.renshaw at canonical.com>
Date: Fri, 24 Jan 2020 13:38:21 -0700
Subject: [PATCH 2/2] Restored the previous logic involving swaptotal, since it
 protects against swapusage > swaptotal

Signed-off-by: Kellen Renshaw <kellen.renshaw at canonical.com>
---
 bindings.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bindings.c b/bindings.c
index 3e939f5..4a8a421 100644
--- a/bindings.c
+++ b/bindings.c
@@ -3600,7 +3600,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
 		} else if (startswith(line, "SwapFree:") && memswlimit > 0 && memswusage > 0 && opts && opts->swap_off == false) {
 			unsigned long swaptotal = memswlimit,
 					swapusage = memusage > memswusage ? 0 : memswusage - memusage,
-					swapfree = swaptotal - swapusage;
+					swapfree = swapusage < swaptotal ? swaptotal - swapusage : 0;
 			snprintf(lbuf, 100, "SwapFree:       %8lu kB\n", swapfree);
 			printme = lbuf;
 		} else if (startswith(line, "SwapFree:") && opts && opts->swap_off == true) {


More information about the lxc-devel mailing list