1 From 1e92c18e409f37f79a24f61623b9d9c4d84e3280 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber@ubuntu.com>
3 Date: Thu, 5 Nov 2020 15:30:27 -0500
4 Subject: [PATCH lxcfs 6/8] swap: Tweak meminfo logic
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 Signed-off-by: Stéphane Graber <stgraber@ubuntu.com>
11 src/proc_fuse.c | 30 +++++++++---------------------
12 1 file changed, 9 insertions(+), 21 deletions(-)
14 diff --git a/src/proc_fuse.c b/src/proc_fuse.c
15 index 002aac9..4c555ec 100644
18 @@ -1151,7 +1151,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
19 __do_fclose FILE *f = NULL;
20 struct fuse_context *fc = fuse_get_context();
21 struct lxcfs_opts *opts = (struct lxcfs_opts *)fuse_get_context()->private_data;
22 - bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap(), host_swap = false;
23 + bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
24 struct file_info *d = INTTYPE_TO_PTR(fi->fh);
25 uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
26 hosttotal = 0, swfree = 0, swusage = 0, swtotal = 0,
27 @@ -1258,19 +1258,14 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
29 sscanf(line + STRLITERALLEN("SwapTotal:"), "%" PRIu64, &hostswtotal);
32 - * If swtotal is 0 it should mean that
33 - * memory.memsw.limit_in_bytes and
34 - * memory.limit_in_bytes are both unlimited or
35 - * both set to the same value. In both cases we
36 - * have no idea what the technical swap limit
37 - * is supposed to be (It's a shared limit
38 - * anyway.) so fallback to the host's values in
41 - if ((hostswtotal < swtotal) || swtotal == 0) {
42 + /* The total amount of swap is always reported to be the
43 + lesser of the RAM+SWAP limit or the SWAP device size.
44 + This is because the kernel can swap as much as it
45 + wants and not only up to swtotal. */
47 + swtotal = memlimit + swtotal;
48 + if (hostswtotal < swtotal) {
49 swtotal = hostswtotal;
53 /* When swappiness is 0, pretend we can't swap. */
54 @@ -1283,14 +1278,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
56 } else if (startswith(line, "SwapFree:")) {
58 - uint64_t hostswfree = 0;
61 - sscanf(line + STRLITERALLEN("SwapFree:"), "%" PRIu64, &hostswfree);
62 - swfree = hostswfree;
63 - } else if (swtotal >= swusage) {
64 - swfree = swtotal - swusage;
66 + swfree = swtotal - swusage;
69 snprintf(lbuf, 100, "SwapFree: %8" PRIu64 " kB\n", swfree);