The following pull request was submitted through Github.
It can be accessed and reviewed at: https://github.com/lxc/lxcfs/pull/399

This e-mail was sent by the LXC bot, direct replies will not reach the author
unless they happen to be subscribed to this list.

=== Description (from pull-request) ===
Stéphane has been running into issues with wrong sawp limits. The logic
he suggested does:
- if memory == memory+swap, we list both at the memlimit for total size
- if memory+swap is larger than memory, then we add the extra amount to
  total swap but still cap to physical swap limit

We came to the conclusion that this is currently the best approach as we
show the mem limit as both Memory and Swap so advertising twice as much
as allowed but there's no way around that really or we'd need to
dynamically update both MemTotal and SwapTotal based on what's used to
keep MemTotal+SwapTotal == limit that's certainly doable but it will be
somewhat racy and also likely confused userspace more than mis-reporting
swap.

Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
From 89662777a34fc3b275713d89f5ce3d5e2641ea73 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgra...@ubuntu.com>
Date: Fri, 17 Apr 2020 21:59:53 +0200
Subject: [PATCH] proc_fuse: improve swap calculation
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Stéphane has been running into issues with wrong sawp limits. The logic
he suggested does:
- if memory == memory+swap, we list both at the memlimit for total size
- if memory+swap is larger than memory, then we add the extra amount to
  total swap but still cap to physical swap limit

We came to the conclusion that this is currently the best approach as we
show the mem limit as both Memory and Swap so advertising twice as much
as allowed but there's no way around that really or we'd need to
dynamically update both MemTotal and SwapTotal based on what's used to
keep MemTotal+SwapTotal == limit that's certainly doable but it will be
somewhat racy and also likely confused userspace more than mis-reporting
swap.

Signed-off-by: Stéphane Graber <stgra...@ubuntu.com>
Signed-off-by: Christian Brauner <christian.brau...@ubuntu.com>
---
 src/proc_fuse.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index b2e348e..b2edf76 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -1019,7 +1019,7 @@ static int proc_meminfo_read(char *buf, size_t size, 
off_t offset,
        struct lxcfs_opts *opts = (struct lxcfs_opts 
*)fuse_get_context()->private_data;
        struct file_info *d = INTTYPE_TO_PTR(fi->fh);
        uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
-                hosttotal = 0;
+                hosttotal = 0, swtotal = 0;
        struct memory_stat mstat = {};
        size_t linelen = 0, total_len = 0;
        char *cache = d->buf;
@@ -1075,6 +1075,7 @@ 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);
                memswusage = memswusage / 1024;
+               swtotal = memswlimit;
        }
 
        if (safe_uint64(memusage_str, &memusage, 10) < 0)
@@ -1110,27 +1111,25 @@ static int proc_meminfo_read(char *buf, size_t size, 
off_t offset,
                        uint64_t hostswtotal = 0;
 
                        sscanf(line + STRLITERALLEN("SwapTotal:"), "%" PRIu64, 
&hostswtotal);
-                       if (hostswtotal < memswlimit)
-                               memswlimit = hostswtotal;
 
-                       if (memswlimit >= memlimit)
-                               memswlimit -= memlimit;
+                       /* Don't advertise more SWAP than the total memory 
allowed. */
+                       if (hostswtotal < swtotal)
+                               swtotal = hostswtotal;
 
-                       snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " 
kB\n", memswlimit);
+                       snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " 
kB\n", swtotal);
                        printme = lbuf;
                } else if (startswith(line, "SwapTotal:") && opts && 
opts->swap_off == true) {
                        snprintf(lbuf, 100, "SwapTotal:      %8" PRIu64 " 
kB\n", (uint64_t)0);
                        printme = lbuf;
                } else if (startswith(line, "SwapFree:") && memswlimit > 0 &&
-                          memswusage > 0 && opts && opts->swap_off == false) {
-                       uint64_t swaptotal = memswlimit,
-                                swapusage = memusage > memswusage
-                                                ? 0
-                                                : memswusage - memusage,
-                                swapfree = swapusage < swaptotal
-                                               ? swaptotal - swapusage
-                                               : 0;
-                       snprintf(lbuf, 100, "SwapFree:       %8" PRIu64 " 
kB\n", swapfree);
+                          opts && opts->swap_off == false) {
+                       uint64_t swfree = 0;
+                       uint64_t swusage = 0;
+
+                       swusage = memswusage - memusage;
+                       swfree = swtotal - swusage;
+
+                       snprintf(lbuf, 100, "SwapFree:       %8" PRIu64 " 
kB\n", swfree);
                        printme = lbuf;
                } else if (startswith(line, "SwapFree:") && opts && 
opts->swap_off == true) {
                        snprintf(lbuf, 100, "SwapFree:       %8" PRIu64 " 
kB\n", (uint64_t)0);
_______________________________________________
lxc-devel mailing list
lxc-devel@lists.linuxcontainers.org
http://lists.linuxcontainers.org/listinfo/lxc-devel

Reply via email to