1 From 018246ffa81294d6d6a9151e0310c82a3548fe2e Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= <f.gruenbichler@proxmox.com>
3 Date: Thu, 22 Dec 2016 13:12:04 +0100
4 Subject: [PATCH lxcfs] fix swap values with nested cgroups
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 the memory limit was already correctly set by looking at the
10 whole cgroup hierarchy and using the minimum value, refactor
11 that code to support arbitrary files in the memory cgroup
12 and reuse it for the memsw limit as well.
14 Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
16 bindings.c | 52 +++++++++++-----------------------------------------
17 1 file changed, 11 insertions(+), 41 deletions(-)
19 diff --git a/bindings.c b/bindings.c
20 index 93c5aaa..3516be6 100644
23 @@ -3046,12 +3046,12 @@ static int read_file(const char *path, char *buf, size_t size,
27 -static unsigned long get_memlimit(const char *cgroup)
28 +static unsigned long get_memlimit(const char *cgroup, const char *file)
30 char *memlimit_str = NULL;
31 unsigned long memlimit = -1;
33 - if (cgfs_get_value("memory", cgroup, "memory.limit_in_bytes", &memlimit_str))
34 + if (cgfs_get_value("memory", cgroup, file, &memlimit_str))
35 memlimit = strtoul(memlimit_str, NULL, 10);
38 @@ -3059,16 +3059,16 @@ static unsigned long get_memlimit(const char *cgroup)
42 -static unsigned long get_min_memlimit(const char *cgroup)
43 +static unsigned long get_min_memlimit(const char *cgroup, const char *file)
45 char *copy = strdupa(cgroup);
46 unsigned long memlimit = 0, retlimit;
48 - retlimit = get_memlimit(copy);
49 + retlimit = get_memlimit(copy, file);
51 while (strcmp(copy, "/") != 0) {
53 - memlimit = get_memlimit(copy);
54 + memlimit = get_memlimit(copy, file);
55 if (memlimit != -1 && memlimit < retlimit)
58 @@ -3083,8 +3083,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
59 struct file_info *d = (struct file_info *)fi->fh;
61 char *memusage_str = NULL, *memstat_str = NULL,
62 - *memswlimit_str = NULL, *memswusage_str = NULL,
63 - *memswlimit_default_str = NULL, *memswusage_default_str = NULL;
64 + *memswlimit_str = NULL, *memswusage_str = NULL;
65 unsigned long memlimit = 0, memusage = 0, memswlimit = 0, memswusage = 0,
66 cached = 0, hosttotal = 0, active_anon = 0, inactive_anon = 0,
67 active_file = 0, inactive_file = 0, unevictable = 0;
68 @@ -3113,7 +3112,7 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
69 return read_file("/proc/meminfo", buf, size, d);
72 - memlimit = get_min_memlimit(cg);
73 + memlimit = get_min_memlimit(cg, "memory.limit_in_bytes");
74 if (!cgfs_get_value("memory", cg, "memory.usage_in_bytes", &memusage_str))
76 if (!cgfs_get_value("memory", cg, "memory.stat", &memstat_str))
77 @@ -3124,20 +3123,9 @@ static int proc_meminfo_read(char *buf, size_t size, off_t offset,
78 if(cgfs_get_value("memory", cg, "memory.memsw.limit_in_bytes", &memswlimit_str) &&
79 cgfs_get_value("memory", cg, "memory.memsw.usage_in_bytes", &memswusage_str))
81 - /* If swapaccounting is turned on, then default value is assumed to be that of cgroup / */
82 - if (!cgfs_get_value("memory", "/", "memory.memsw.limit_in_bytes", &memswlimit_default_str))
84 - if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str))
87 - memswlimit = strtoul(memswlimit_str, NULL, 10);
88 + memswlimit = get_min_memlimit(cg, "memory.memsw.limit_in_bytes");
89 memswusage = strtoul(memswusage_str, NULL, 10);
91 - if (!strcmp(memswlimit_str, memswlimit_default_str))
93 - if (!strcmp(memswusage_str, memswusage_default_str))
96 memswlimit = memswlimit / 1024;
97 memswusage = memswusage / 1024;
99 @@ -3257,8 +3245,6 @@ err:
100 free(memswlimit_str);
101 free(memswusage_str);
103 - free(memswlimit_default_str);
104 - free(memswusage_default_str);
108 @@ -3859,8 +3845,7 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
109 struct fuse_context *fc = fuse_get_context();
110 struct file_info *d = (struct file_info *)fi->fh;
112 - char *memswlimit_str = NULL, *memlimit_str = NULL, *memusage_str = NULL, *memswusage_str = NULL,
113 - *memswlimit_default_str = NULL, *memswusage_default_str = NULL;
114 + char *memswlimit_str = NULL, *memlimit_str = NULL, *memusage_str = NULL, *memswusage_str = NULL;
115 unsigned long memswlimit = 0, memlimit = 0, memusage = 0, memswusage = 0, swap_total = 0, swap_free = 0;
116 ssize_t total_len = 0, rv = 0;
118 @@ -3885,32 +3870,19 @@ static int proc_swaps_read(char *buf, size_t size, off_t offset,
119 return read_file("/proc/swaps", buf, size, d);
120 prune_init_slice(cg);
122 - if (!cgfs_get_value("memory", cg, "memory.limit_in_bytes", &memlimit_str))
124 + memlimit = get_min_memlimit(cg, "memory.limit_in_bytes");
126 if (!cgfs_get_value("memory", cg, "memory.usage_in_bytes", &memusage_str))
129 - memlimit = strtoul(memlimit_str, NULL, 10);
130 memusage = strtoul(memusage_str, NULL, 10);
132 if (cgfs_get_value("memory", cg, "memory.memsw.usage_in_bytes", &memswusage_str) &&
133 cgfs_get_value("memory", cg, "memory.memsw.limit_in_bytes", &memswlimit_str)) {
135 - /* If swap accounting is turned on, then default value is assumed to be that of cgroup / */
136 - if (!cgfs_get_value("memory", "/", "memory.memsw.limit_in_bytes", &memswlimit_default_str))
138 - if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str))
141 - memswlimit = strtoul(memswlimit_str, NULL, 10);
142 + memswlimit = get_min_memlimit(cg, "memory.memsw.limit_in_bytes");
143 memswusage = strtoul(memswusage_str, NULL, 10);
145 - if (!strcmp(memswlimit_str, memswlimit_default_str))
147 - if (!strcmp(memswusage_str, memswusage_default_str))
150 swap_total = (memswlimit - memlimit) / 1024;
151 swap_free = (memswusage - memusage) / 1024;
153 @@ -3964,8 +3936,6 @@ err:
156 free(memswusage_str);
157 - free(memswusage_default_str);
158 - free(memswlimit_default_str);