[lxc-devel] [lxcfs/master] Rework swap memory management
stgraber on Github
lxc-bot at linuxcontainers.org
Thu Nov 5 20:51:43 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 417 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20201105/1a696344/attachment.bin>
-------------- next part --------------
From 4032b735ba06488dd8703acd505cac07cf807b2e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:17:02 -0500
Subject: [PATCH 1/6] cgroups: Add get_memory_swappiness
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/cgroups/cgfsng.c | 7 +++++++
src/cgroups/cgroup.h | 2 ++
2 files changed, 9 insertions(+)
diff --git a/src/cgroups/cgfsng.c b/src/cgroups/cgfsng.c
index 824b0e7..d772f25 100644
--- a/src/cgroups/cgfsng.c
+++ b/src/cgroups/cgfsng.c
@@ -620,6 +620,12 @@ static int cgfsng_get_memory_max(struct cgroup_ops *ops, const char *cgroup,
return cgfsng_get_memory(ops, cgroup, "memory.max", value);
}
+static int cgfsng_get_memory_swappiness(struct cgroup_ops *ops, const char *cgroup,
+ char **value)
+{
+ return cgfsng_get_memory(ops, cgroup, "memory.swappiness", value);
+}
+
static int cgfsng_get_memory_swap_max(struct cgroup_ops *ops,
const char *cgroup, char **value)
{
@@ -1011,6 +1017,7 @@ struct cgroup_ops *cgfsng_ops_init(void)
cgfsng_ops->get_memory_stats_fd = cgfsng_get_memory_stats_fd;
cgfsng_ops->get_memory_stats = cgfsng_get_memory_stats;
cgfsng_ops->get_memory_max = cgfsng_get_memory_max;
+ cgfsng_ops->get_memory_swappiness = cgfsng_get_memory_swappiness;
cgfsng_ops->get_memory_swap_max = cgfsng_get_memory_swap_max;
cgfsng_ops->get_memory_current = cgfsng_get_memory_current;
cgfsng_ops->get_memory_swap_current = cgfsng_get_memory_swap_current;
diff --git a/src/cgroups/cgroup.h b/src/cgroups/cgroup.h
index ad4a3ae..f4b5d19 100644
--- a/src/cgroups/cgroup.h
+++ b/src/cgroups/cgroup.h
@@ -151,6 +151,8 @@ struct cgroup_ops {
const char *cgroup, char **value);
int (*get_memory_max)(struct cgroup_ops *ops, const char *cgroup,
char **value);
+ int (*get_memory_swappiness)(struct cgroup_ops *ops, const char *cgroup,
+ char **value);
int (*get_memory_swap_max)(struct cgroup_ops *ops, const char *cgroup,
char **value);
bool (*can_use_swap)(struct cgroup_ops *ops);
From 7cbfbc7406ae97b5ac5908cf550ddc7c81f27023 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:17:40 -0500
Subject: [PATCH 2/6] swap: Fix usage reporting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/proc_fuse.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 7bc1eb0..0ad21b9 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -407,7 +407,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
if (swtotal > 0) {
l = snprintf(d->buf + total_len, d->size - total_len,
"none%*svirtual\t\t%" PRIu64 "\t%" PRIu64 "\t0\n",
- 36, " ", swtotal, swfree);
+ 36, " ", swtotal, swusage);
total_len += l;
}
From 362d1193f6cb8038b2c8a89c69d0a3ef465caa9c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:20:26 -0500
Subject: [PATCH 3/6] swap: Only report usage when swappiness is 0
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/proc_fuse.c | 28 ++++++++++++++++++++++++----
1 file changed, 24 insertions(+), 4 deletions(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 0ad21b9..002aac9 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -318,13 +318,14 @@ static inline bool startswith(const char *line, const char *pref)
static int proc_swaps_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
- __do_free char *cgroup = NULL, *memusage_str = NULL, *memswusage_str = NULL;
+ __do_free char *cgroup = NULL, *memusage_str = NULL,
+ *memswusage_str = NULL, *memswpriority_str = NULL;
struct fuse_context *fc = fuse_get_context();
struct lxcfs_opts *opts = (struct lxcfs_opts *)fuse_get_context()->private_data;
bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memswlimit = 0, memlimit = 0, memusage = 0, memswusage = 0,
- swtotal = 0, swfree = 0, swusage = 0;
+ swtotal = 0, swfree = 0, swusage = 0, memswpriority = 1;
ssize_t total_len = 0;
ssize_t l = 0;
char *cache = d->buf;
@@ -380,6 +381,10 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
if (swtotal >= swusage)
swfree = swtotal - swusage;
}
+
+ ret = cgroup_ops->get_memory_swappiness(cgroup_ops, cgroup, &memswpriority_str);
+ if (ret >= 0)
+ safe_uint64(memswpriority_str, &memswpriority, 10);
}
}
@@ -404,6 +409,11 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
}
}
+ // When swappiness is 0, pretend we can't swap.
+ if (memswpriority == 0) {
+ swtotal = swusage;
+ }
+
if (swtotal > 0) {
l = snprintf(d->buf + total_len, d->size - total_len,
"none%*svirtual\t\t%" PRIu64 "\t%" PRIu64 "\t0\n",
@@ -1136,7 +1146,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
__do_free char *cgroup = NULL, *line = NULL, *memusage_str = NULL,
- *memswusage_str = NULL;
+ *memswusage_str = NULL, *memswpriority_str = NULL;
__do_free void *fopen_cache = NULL;
__do_fclose FILE *f = NULL;
struct fuse_context *fc = fuse_get_context();
@@ -1144,7 +1154,8 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap(), host_swap = false;
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
- hosttotal = 0, swfree = 0, swusage = 0, swtotal = 0;
+ hosttotal = 0, swfree = 0, swusage = 0, swtotal = 0,
+ memswpriority = 1;
struct memory_stat mstat = {};
size_t linelen = 0, total_len = 0;
char *cache = d->buf;
@@ -1209,6 +1220,10 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
swusage = (memswusage - memusage) / 1024;
}
}
+
+ ret = cgroup_ops->get_memory_swappiness(cgroup_ops, cgroup, &memswpriority_str);
+ if (ret >= 0)
+ safe_uint64(memswpriority_str, &memswpriority, 10);
}
f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
@@ -1257,6 +1272,11 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
swtotal = hostswtotal;
host_swap = true;
}
+
+ /* When swappiness is 0, pretend we can't swap. */
+ if (memswpriority == 0) {
+ swtotal = swusage;
+ }
}
snprintf(lbuf, 100, "SwapTotal: %8" PRIu64 " kB\n", swtotal);
From 33aa929ea96a9a4d6229e486a948999f55894d24 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:30:27 -0500
Subject: [PATCH 4/6] swap: Tweak meminfo logic
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/proc_fuse.c | 30 +++++++++---------------------
1 file changed, 9 insertions(+), 21 deletions(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 002aac9..4c555ec 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -1151,7 +1151,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
__do_fclose FILE *f = NULL;
struct fuse_context *fc = fuse_get_context();
struct lxcfs_opts *opts = (struct lxcfs_opts *)fuse_get_context()->private_data;
- bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap(), host_swap = false;
+ bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
hosttotal = 0, swfree = 0, swusage = 0, swtotal = 0,
@@ -1258,19 +1258,14 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
sscanf(line + STRLITERALLEN("SwapTotal:"), "%" PRIu64, &hostswtotal);
- /*
- * If swtotal is 0 it should mean that
- * memory.memsw.limit_in_bytes and
- * memory.limit_in_bytes are both unlimited or
- * both set to the same value. In both cases we
- * have no idea what the technical swap limit
- * is supposed to be (It's a shared limit
- * anyway.) so fallback to the host's values in
- * that case too.
- */
- if ((hostswtotal < swtotal) || swtotal == 0) {
+ /* The total amount of swap is always reported to be the
+ lesser of the RAM+SWAP limit or the SWAP device size.
+ This is because the kernel can swap as much as it
+ wants and not only up to swtotal. */
+
+ swtotal = memlimit + swtotal;
+ if (hostswtotal < swtotal) {
swtotal = hostswtotal;
- host_swap = true;
}
/* When swappiness is 0, pretend we can't swap. */
@@ -1283,14 +1278,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
printme = lbuf;
} else if (startswith(line, "SwapFree:")) {
if (wants_swap) {
- uint64_t hostswfree = 0;
-
- if (host_swap) {
- sscanf(line + STRLITERALLEN("SwapFree:"), "%" PRIu64, &hostswfree);
- swfree = hostswfree;
- } else if (swtotal >= swusage) {
- swfree = swtotal - swusage;
- }
+ swfree = swtotal - swusage;
}
snprintf(lbuf, 100, "SwapFree: %8" PRIu64 " kB\n", swfree);
From 3ce66074ff2fe79993d779df3bb7533dae017ef5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:43:01 -0500
Subject: [PATCH 5/6] swap: Make /proc/swaps match /proc/meminfo
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/proc_fuse.c | 47 ++++++++++++++++++++++++++++-------------------
1 file changed, 28 insertions(+), 19 deletions(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index 4c555ec..fc69789 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -325,11 +325,16 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memswlimit = 0, memlimit = 0, memusage = 0, memswusage = 0,
- swtotal = 0, swfree = 0, swusage = 0, memswpriority = 1;
+ swtotal = 0, swfree = 0, swusage = 0, memswpriority = 1
+ hostswtotal = 0, hostswfree = 0;
ssize_t total_len = 0;
ssize_t l = 0;
char *cache = d->buf;
int ret;
+ __do_free char *line = NULL;
+ __do_free void *fopen_cache = NULL;
+ __do_fclose FILE *f = NULL;
+ size_t linelen = 0;
if (offset) {
int left;
@@ -390,28 +395,32 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
total_len = snprintf(d->buf, d->size, "Filename\t\t\t\tType\t\tSize\tUsed\tPriority\n");
- /* When no mem + swap limit is specified or swapaccount=0*/
- if (!memswlimit) {
- __do_free char *line = NULL;
- __do_free void *fopen_cache = NULL;
- __do_fclose FILE *f = NULL;
- size_t linelen = 0;
+ /* Read host total and free values */
+ f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
+ if (!f)
+ return 0;
- f = fopen_cached("/proc/meminfo", "re", &fopen_cache);
- if (!f)
- return 0;
+ while (getline(&line, &linelen, f) != -1) {
+ if (startswith(line, "SwapTotal:"))
+ sscanf(line, "SwapTotal: %8" PRIu64 " kB", &hostswtotal);
+ else if (startswith(line, "SwapFree:"))
+ sscanf(line, "SwapFree: %8" PRIu64 " kB", &hostswfree);
+ }
- while (getline(&line, &linelen, f) != -1) {
- if (startswith(line, "SwapTotal:"))
- sscanf(line, "SwapTotal: %8" PRIu64 " kB", &swtotal);
- else if (startswith(line, "SwapFree:"))
- sscanf(line, "SwapFree: %8" PRIu64 " kB", &swfree);
+ if (wants_swap) {
+ /* The total amount of swap is always reported to be the
+ lesser of the RAM+SWAP limit or the SWAP device size.
+ This is because the kernel can swap as much as it
+ wants and not only up to swtotal. */
+ swtotal = memlimit / 1024 + swtotal;
+ if (hostswtotal < swtotal) {
+ swtotal = hostswtotal;
}
- }
- // When swappiness is 0, pretend we can't swap.
- if (memswpriority == 0) {
- swtotal = swusage;
+ /* When swappiness is 0, pretend we can't swap. */
+ if (memswpriority == 0) {
+ swtotal = swusage;
+ }
}
if (swtotal > 0) {
From 79af0cd194210970a086d543d86deab71f996c15 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Graber?= <stgraber at ubuntu.com>
Date: Thu, 5 Nov 2020 15:46:07 -0500
Subject: [PATCH 6/6] swap: Remove now unused variable
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
src/proc_fuse.c | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/src/proc_fuse.c b/src/proc_fuse.c
index fc69789..9bf963b 100644
--- a/src/proc_fuse.c
+++ b/src/proc_fuse.c
@@ -325,7 +325,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
bool wants_swap = opts && !opts->swap_off && liblxcfs_can_use_swap();
struct file_info *d = INTTYPE_TO_PTR(fi->fh);
uint64_t memswlimit = 0, memlimit = 0, memusage = 0, memswusage = 0,
- swtotal = 0, swfree = 0, swusage = 0, memswpriority = 1
+ swtotal = 0, swusage = 0, memswpriority = 1,
hostswtotal = 0, hostswfree = 0;
ssize_t total_len = 0;
ssize_t l = 0;
@@ -383,8 +383,6 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
swusage = 0;
else
swusage = (memswusage - memusage) / 1024;
- if (swtotal >= swusage)
- swfree = swtotal - swusage;
}
ret = cgroup_ops->get_memory_swappiness(cgroup_ops, cgroup, &memswpriority_str);
More information about the lxc-devel
mailing list