1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
// Copyright 2021 The gVisor Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// All tests in this file rely on being about to mount and unmount cgroupfs,
// which isn't expected to work, or be safe on a general linux system.
#include <sys/mount.h>
#include <unistd.h>
#include "gtest/gtest.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "test/util/capability_util.h"
#include "test/util/cgroup_util.h"
#include "test/util/temp_path.h"
#include "test/util/test_util.h"
namespace gvisor {
namespace testing {
namespace {
using ::testing::_;
using ::testing::Gt;
std::vector<std::string> known_controllers = {"cpu", "cpuset", "cpuacct",
"memory"};
bool CgroupsAvailable() {
return IsRunningOnGvisor() && !IsRunningWithVFS1() &&
TEST_CHECK_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN));
}
TEST(Cgroup, MountSucceeds) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
EXPECT_NO_ERRNO(c.ContainsCallingProcess());
}
TEST(Cgroup, SeparateMounts) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
for (const auto& ctl : known_controllers) {
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(ctl));
EXPECT_NO_ERRNO(c.ContainsCallingProcess());
}
}
TEST(Cgroup, AllControllersImplicit) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
absl::flat_hash_map<std::string, CgroupsEntry> cgroups_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
for (const auto& ctl : known_controllers) {
EXPECT_TRUE(cgroups_entries.contains(ctl))
<< absl::StreamFormat("ctl=%s", ctl);
}
EXPECT_EQ(cgroups_entries.size(), known_controllers.size());
}
TEST(Cgroup, AllControllersExplicit) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("all"));
absl::flat_hash_map<std::string, CgroupsEntry> cgroups_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
for (const auto& ctl : known_controllers) {
EXPECT_TRUE(cgroups_entries.contains(ctl))
<< absl::StreamFormat("ctl=%s", ctl);
}
EXPECT_EQ(cgroups_entries.size(), known_controllers.size());
}
TEST(Cgroup, ProcsAndTasks) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
absl::flat_hash_set<pid_t> pids = ASSERT_NO_ERRNO_AND_VALUE(c.Procs());
absl::flat_hash_set<pid_t> tids = ASSERT_NO_ERRNO_AND_VALUE(c.Tasks());
EXPECT_GE(tids.size(), pids.size()) << "Found more processes than threads";
// Pids should be a strict subset of tids.
for (auto it = pids.begin(); it != pids.end(); ++it) {
EXPECT_TRUE(tids.contains(*it))
<< absl::StreamFormat("Have pid %d, but no such tid", *it);
}
}
TEST(Cgroup, ControllersMustBeInUniqueHierarchy) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
// Hierarchy #1: all controllers.
Cgroup all = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
// Hierarchy #2: memory.
//
// This should conflict since memory is already in hierarchy #1, and the two
// hierarchies have different sets of controllers, so this mount can't be a
// view into hierarchy #1.
EXPECT_THAT(m.MountCgroupfs("memory"), PosixErrorIs(EBUSY, _))
<< "Memory controller mounted on two hierarchies";
EXPECT_THAT(m.MountCgroupfs("cpu"), PosixErrorIs(EBUSY, _))
<< "CPU controller mounted on two hierarchies";
}
TEST(Cgroup, UnmountFreesControllers) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup all = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
// All controllers are now attached to all's hierarchy. Attempting new mount
// with any individual controller should fail.
EXPECT_THAT(m.MountCgroupfs("memory"), PosixErrorIs(EBUSY, _))
<< "Memory controller mounted on two hierarchies";
// Unmount the "all" hierarchy. This should enable any controller to be
// mounted on a new hierarchy again.
ASSERT_NO_ERRNO(m.Unmount(all));
EXPECT_NO_ERRNO(m.MountCgroupfs("memory"));
EXPECT_NO_ERRNO(m.MountCgroupfs("cpu"));
}
TEST(Cgroup, OnlyContainsControllerSpecificFiles) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup mem = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory"));
EXPECT_THAT(Exists(mem.Relpath("memory.usage_in_bytes")),
IsPosixErrorOkAndHolds(true));
// CPU files shouldn't exist in memory cgroups.
EXPECT_THAT(Exists(mem.Relpath("cpu.cfs_period_us")),
IsPosixErrorOkAndHolds(false));
EXPECT_THAT(Exists(mem.Relpath("cpu.cfs_quota_us")),
IsPosixErrorOkAndHolds(false));
EXPECT_THAT(Exists(mem.Relpath("cpu.shares")), IsPosixErrorOkAndHolds(false));
Cgroup cpu = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu"));
EXPECT_THAT(Exists(cpu.Relpath("cpu.cfs_period_us")),
IsPosixErrorOkAndHolds(true));
EXPECT_THAT(Exists(cpu.Relpath("cpu.cfs_quota_us")),
IsPosixErrorOkAndHolds(true));
EXPECT_THAT(Exists(cpu.Relpath("cpu.shares")), IsPosixErrorOkAndHolds(true));
// Memory files shouldn't exist in cpu cgroups.
EXPECT_THAT(Exists(cpu.Relpath("memory.usage_in_bytes")),
IsPosixErrorOkAndHolds(false));
}
TEST(Cgroup, InvalidController) {
SKIP_IF(!CgroupsAvailable());
TempPath mountpoint = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
std::string mopts = "this-controller-is-invalid";
EXPECT_THAT(
mount("none", mountpoint.path().c_str(), "cgroup", 0, mopts.c_str()),
SyscallFailsWithErrno(EINVAL));
}
TEST(Cgroup, MoptAllMustBeExclusive) {
SKIP_IF(!CgroupsAvailable());
TempPath mountpoint = ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir());
std::string mopts = "all,cpu";
EXPECT_THAT(
mount("none", mountpoint.path().c_str(), "cgroup", 0, mopts.c_str()),
SyscallFailsWithErrno(EINVAL));
}
TEST(MemoryCgroup, MemoryUsageInBytes) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory"));
EXPECT_THAT(c.ReadIntegerControlFile("memory.usage_in_bytes"),
IsPosixErrorOkAndHolds(Gt(0)));
}
TEST(CPUCgroup, ControlFilesHaveDefaultValues) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu"));
EXPECT_THAT(c.ReadIntegerControlFile("cpu.cfs_quota_us"),
IsPosixErrorOkAndHolds(-1));
EXPECT_THAT(c.ReadIntegerControlFile("cpu.cfs_period_us"),
IsPosixErrorOkAndHolds(100000));
EXPECT_THAT(c.ReadIntegerControlFile("cpu.shares"),
IsPosixErrorOkAndHolds(1024));
}
TEST(ProcCgroups, Empty) {
SKIP_IF(!CgroupsAvailable());
absl::flat_hash_map<std::string, CgroupsEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
// No cgroups mounted yet, we should have no entries.
EXPECT_TRUE(entries.empty());
}
TEST(ProcCgroups, ProcCgroupsEntries) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup mem = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory"));
absl::flat_hash_map<std::string, CgroupsEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
EXPECT_EQ(entries.size(), 1);
ASSERT_TRUE(entries.contains("memory"));
CgroupsEntry mem_e = entries["memory"];
EXPECT_EQ(mem_e.subsys_name, "memory");
EXPECT_GE(mem_e.hierarchy, 1);
// Expect a single root cgroup.
EXPECT_EQ(mem_e.num_cgroups, 1);
// Cgroups are currently always enabled when mounted.
EXPECT_TRUE(mem_e.enabled);
// Add a second cgroup, and check for new entry.
Cgroup cpu = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu"));
entries = ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
EXPECT_EQ(entries.size(), 2);
EXPECT_TRUE(entries.contains("memory")); // Still have memory entry.
ASSERT_TRUE(entries.contains("cpu"));
CgroupsEntry cpu_e = entries["cpu"];
EXPECT_EQ(cpu_e.subsys_name, "cpu");
EXPECT_GE(cpu_e.hierarchy, 1);
EXPECT_EQ(cpu_e.num_cgroups, 1);
EXPECT_TRUE(cpu_e.enabled);
// Separate hierarchies, since controllers were mounted separately.
EXPECT_NE(mem_e.hierarchy, cpu_e.hierarchy);
}
TEST(ProcCgroups, UnmountRemovesEntries) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup cg = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu,memory"));
absl::flat_hash_map<std::string, CgroupsEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
EXPECT_EQ(entries.size(), 2);
ASSERT_NO_ERRNO(m.Unmount(cg));
entries = ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
EXPECT_TRUE(entries.empty());
}
TEST(ProcPIDCgroup, Empty) {
SKIP_IF(!CgroupsAvailable());
absl::flat_hash_map<std::string, PIDCgroupEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
EXPECT_TRUE(entries.empty());
}
TEST(ProcPIDCgroup, Entries) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory"));
absl::flat_hash_map<std::string, PIDCgroupEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
EXPECT_EQ(entries.size(), 1);
PIDCgroupEntry mem_e = entries["memory"];
EXPECT_GE(mem_e.hierarchy, 1);
EXPECT_EQ(mem_e.controllers, "memory");
EXPECT_EQ(mem_e.path, "/");
Cgroup c1 = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu"));
entries = ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
EXPECT_EQ(entries.size(), 2);
EXPECT_TRUE(entries.contains("memory")); // Still have memory entry.
PIDCgroupEntry cpu_e = entries["cpu"];
EXPECT_GE(cpu_e.hierarchy, 1);
EXPECT_EQ(cpu_e.controllers, "cpu");
EXPECT_EQ(cpu_e.path, "/");
// Separate hierarchies, since controllers were mounted separately.
EXPECT_NE(mem_e.hierarchy, cpu_e.hierarchy);
}
TEST(ProcPIDCgroup, UnmountRemovesEntries) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup all = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs(""));
absl::flat_hash_map<std::string, PIDCgroupEntry> entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
EXPECT_GT(entries.size(), 0);
ASSERT_NO_ERRNO(m.Unmount(all));
entries = ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
EXPECT_TRUE(entries.empty());
}
TEST(ProcCgroup, PIDCgroupMatchesCgroups) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory"));
Cgroup c1 = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("cpu"));
absl::flat_hash_map<std::string, CgroupsEntry> cgroups_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
absl::flat_hash_map<std::string, PIDCgroupEntry> pid_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
CgroupsEntry cgroup_mem = cgroups_entries["memory"];
PIDCgroupEntry pid_mem = pid_entries["memory"];
EXPECT_EQ(cgroup_mem.hierarchy, pid_mem.hierarchy);
CgroupsEntry cgroup_cpu = cgroups_entries["cpu"];
PIDCgroupEntry pid_cpu = pid_entries["cpu"];
EXPECT_EQ(cgroup_cpu.hierarchy, pid_cpu.hierarchy);
EXPECT_NE(cgroup_mem.hierarchy, cgroup_cpu.hierarchy);
EXPECT_NE(pid_mem.hierarchy, pid_cpu.hierarchy);
}
TEST(ProcCgroup, MultiControllerHierarchy) {
SKIP_IF(!CgroupsAvailable());
Mounter m(ASSERT_NO_ERRNO_AND_VALUE(TempPath::CreateDir()));
Cgroup c = ASSERT_NO_ERRNO_AND_VALUE(m.MountCgroupfs("memory,cpu"));
absl::flat_hash_map<std::string, CgroupsEntry> cgroups_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcCgroupsEntries());
CgroupsEntry mem_e = cgroups_entries["memory"];
CgroupsEntry cpu_e = cgroups_entries["cpu"];
// Both controllers should have the same hierarchy ID.
EXPECT_EQ(mem_e.hierarchy, cpu_e.hierarchy);
absl::flat_hash_map<std::string, PIDCgroupEntry> pid_entries =
ASSERT_NO_ERRNO_AND_VALUE(ProcPIDCgroupEntries(getpid()));
// Expecting an entry listing both controllers, that matches the previous
// hierarchy ID. Note that the controllers are listed in alphabetical order.
PIDCgroupEntry pid_e = pid_entries["cpu,memory"];
EXPECT_EQ(pid_e.hierarchy, mem_e.hierarchy);
}
} // namespace
} // namespace testing
} // namespace gvisor
|