]>
Commit | Line | Data |
---|---|---|
3b716f2b WB |
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 | |
5 | MIME-Version: 1.0 | |
6 | Content-Type: text/plain; charset=UTF-8 | |
7 | Content-Transfer-Encoding: 8bit | |
8 | ||
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. | |
13 | ||
14 | Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com> | |
15 | --- | |
16 | bindings.c | 52 +++++++++++----------------------------------------- | |
17 | 1 file changed, 11 insertions(+), 41 deletions(-) | |
18 | ||
19 | diff --git a/bindings.c b/bindings.c | |
20 | index 93c5aaa..3516be6 100644 | |
21 | --- a/bindings.c | |
22 | +++ b/bindings.c | |
23 | @@ -3046,12 +3046,12 @@ static int read_file(const char *path, char *buf, size_t size, | |
24 | * FUSE ops for /proc | |
25 | */ | |
26 | ||
27 | -static unsigned long get_memlimit(const char *cgroup) | |
28 | +static unsigned long get_memlimit(const char *cgroup, const char *file) | |
29 | { | |
30 | char *memlimit_str = NULL; | |
31 | unsigned long memlimit = -1; | |
32 | ||
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); | |
36 | ||
37 | free(memlimit_str); | |
38 | @@ -3059,16 +3059,16 @@ static unsigned long get_memlimit(const char *cgroup) | |
39 | return memlimit; | |
40 | } | |
41 | ||
42 | -static unsigned long get_min_memlimit(const char *cgroup) | |
43 | +static unsigned long get_min_memlimit(const char *cgroup, const char *file) | |
44 | { | |
45 | char *copy = strdupa(cgroup); | |
46 | unsigned long memlimit = 0, retlimit; | |
47 | ||
48 | - retlimit = get_memlimit(copy); | |
49 | + retlimit = get_memlimit(copy, file); | |
50 | ||
51 | while (strcmp(copy, "/") != 0) { | |
52 | copy = dirname(copy); | |
53 | - memlimit = get_memlimit(copy); | |
54 | + memlimit = get_memlimit(copy, file); | |
55 | if (memlimit != -1 && memlimit < retlimit) | |
56 | retlimit = memlimit; | |
57 | }; | |
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; | |
60 | char *cg; | |
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); | |
70 | prune_init_slice(cg); | |
71 | ||
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)) | |
75 | goto err; | |
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)) | |
80 | { | |
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)) | |
83 | - goto err; | |
84 | - if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str)) | |
85 | - goto err; | |
86 | - | |
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); | |
90 | ||
91 | - if (!strcmp(memswlimit_str, memswlimit_default_str)) | |
92 | - memswlimit = 0; | |
93 | - if (!strcmp(memswusage_str, memswusage_default_str)) | |
94 | - memswusage = 0; | |
95 | - | |
96 | memswlimit = memswlimit / 1024; | |
97 | memswusage = memswusage / 1024; | |
98 | } | |
99 | @@ -3257,8 +3245,6 @@ err: | |
100 | free(memswlimit_str); | |
101 | free(memswusage_str); | |
102 | free(memstat_str); | |
103 | - free(memswlimit_default_str); | |
104 | - free(memswusage_default_str); | |
105 | return rv; | |
106 | } | |
107 | ||
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; | |
111 | char *cg = NULL; | |
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; | |
117 | ssize_t l = 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); | |
121 | ||
122 | - if (!cgfs_get_value("memory", cg, "memory.limit_in_bytes", &memlimit_str)) | |
123 | - goto err; | |
124 | + memlimit = get_min_memlimit(cg, "memory.limit_in_bytes"); | |
125 | ||
126 | if (!cgfs_get_value("memory", cg, "memory.usage_in_bytes", &memusage_str)) | |
127 | goto err; | |
128 | ||
129 | - memlimit = strtoul(memlimit_str, NULL, 10); | |
130 | memusage = strtoul(memusage_str, NULL, 10); | |
131 | ||
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)) { | |
134 | ||
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)) | |
137 | - goto err; | |
138 | - if (!cgfs_get_value("memory", "/", "memory.memsw.usage_in_bytes", &memswusage_default_str)) | |
139 | - goto err; | |
140 | - | |
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); | |
144 | ||
145 | - if (!strcmp(memswlimit_str, memswlimit_default_str)) | |
146 | - memswlimit = 0; | |
147 | - if (!strcmp(memswusage_str, memswusage_default_str)) | |
148 | - memswusage = 0; | |
149 | - | |
150 | swap_total = (memswlimit - memlimit) / 1024; | |
151 | swap_free = (memswusage - memusage) / 1024; | |
152 | } | |
153 | @@ -3964,8 +3936,6 @@ err: | |
154 | free(memlimit_str); | |
155 | free(memusage_str); | |
156 | free(memswusage_str); | |
157 | - free(memswusage_default_str); | |
158 | - free(memswlimit_default_str); | |
159 | return rv; | |
160 | } | |
161 | ||
162 | -- | |
163 | 2.1.4 | |
164 |