• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <cerrno>
18 #include <cstdio>
19 #include <filesystem>
20 #include <iostream>
21 #include <optional>
22 #include <random>
23 #include <string>
24 #include <vector>
25 
26 #include <android-base/file.h>
27 #include <android-base/strings.h>
28 using android::base::ReadFileToString;
29 using android::base::Split;
30 using android::base::WriteStringToFile;
31 
32 #include <gmock/gmock.h>
33 #include <gtest/gtest.h>
34 
35 namespace {
36 
37 const std::string CGROUP_V2_ROOT_PATH = "/sys/fs/cgroup";
38 
isMemcgV2Enabled()39 std::optional<bool> isMemcgV2Enabled() {
40     if (std::string proc_cgroups; ReadFileToString("/proc/cgroups", &proc_cgroups)) {
41         const std::vector<std::string> lines = Split(proc_cgroups, "\n");
42         for (const std::string& line : lines) {
43             if (line.starts_with("memory")) {
44                 const bool enabled = line.back() == '1';
45                 if (!enabled) return false;
46 
47                 const std::vector<std::string> memcg_tokens = Split(line, "\t");
48                 return memcg_tokens[1] == "0";  // 0 == default hierarchy == v2
49             }
50         }
51         // We know for sure it's not enabled, either because it is mounted as v1 (cgroups.json
52         // override) which would be detected above, or because it was intentionally disabled via
53         // kernel command line (cgroup_disable=memory), or because it's not built in to the kernel
54         // (CONFIG_MEMCG is not set).
55         return false;
56     }
57 
58     // Problems accessing /proc/cgroups (sepolicy?) Try checking the root cgroup.controllers file.
59     perror("Warning: Could not read /proc/cgroups");
60     if (std::string controllers;
61         ReadFileToString(CGROUP_V2_ROOT_PATH + "/cgroup.controllers", &controllers)) {
62         return controllers.find("memory") != std::string::npos;
63     }
64 
65     std::cerr << "Error: Could not read " << CGROUP_V2_ROOT_PATH
66               << "/cgroup.controllers: " << std::strerror(errno) << std::endl;
67     return std::nullopt;
68 }
69 
checkRootSubtreeState()70 std::optional<bool> checkRootSubtreeState() {
71     if (std::string controllers;
72         ReadFileToString(CGROUP_V2_ROOT_PATH + "/cgroup.subtree_control", &controllers)) {
73         return controllers.find("memory") != std::string::npos;
74     }
75     std::cerr << "Error: Could not read " << CGROUP_V2_ROOT_PATH
76               << "/cgroup.subtree_control: " << std::strerror(errno) << std::endl;
77     return std::nullopt;
78 }
79 
80 }  // anonymous namespace
81 
82 class MemcgV2SubdirTest : public testing::Test {
83   protected:
84     std::optional<std::string> mRandDir;
85 
SetUp()86     void SetUp() override {
87         std::optional<bool> memcgV2Enabled = isMemcgV2Enabled();
88         ASSERT_NE(memcgV2Enabled, std::nullopt);
89         if (!*memcgV2Enabled) GTEST_SKIP() << "Memcg v2 not enabled";
90 
91         mRootSubtreeState = checkRootSubtreeState();
92         ASSERT_NE(mRootSubtreeState, std::nullopt);
93 
94         if (!*mRootSubtreeState) {
95             ASSERT_TRUE(
96                     WriteStringToFile("+memory", CGROUP_V2_ROOT_PATH + "/cgroup.subtree_control"))
97                     << "Could not enable memcg under root: " << std::strerror(errno);
98         }
99 
100         // Make a new, temporary, randomly-named v2 cgroup in which we will attempt to activate
101         // memcg
102         std::random_device rd;
103         std::uniform_int_distribution dist(static_cast<int>('A'), static_cast<int>('Z'));
104         std::string randName = CGROUP_V2_ROOT_PATH + "/vts_libprocessgroup.";
105         for (int i = 0; i < 10; ++i) randName.append(1, static_cast<char>(dist(rd)));
106         ASSERT_TRUE(std::filesystem::create_directory(randName));
107         mRandDir = randName;  // For cleanup in TearDown
108 
109         std::string subtree_controllers;
110         ASSERT_TRUE(ReadFileToString(*mRandDir + "/cgroup.controllers", &subtree_controllers));
111         ASSERT_NE(subtree_controllers.find("memory"), std::string::npos)
112                 << "Memcg was not activated in child cgroup";
113     }
114 
TearDown()115     void TearDown() override {
116         if (mRandDir) {
117             if (!std::filesystem::remove(*mRandDir)) {
118                 std::cerr << "Could not remove temporary memcg v2 test directory" << std::endl;
119             }
120         }
121 
122         if (!*mRootSubtreeState) {
123             if (!WriteStringToFile("-memory", CGROUP_V2_ROOT_PATH + "/cgroup.subtree_control")) {
124                 std::cerr << "Could not disable memcg under root: " << std::strerror(errno)
125                           << std::endl;
126             }
127         }
128     }
129 
130   private:
131     std::optional<bool> mRootSubtreeState;
132 };
133 
134 
TEST_F(MemcgV2SubdirTest,CanActivateMemcgV2Subtree)135 TEST_F(MemcgV2SubdirTest, CanActivateMemcgV2Subtree) {
136     ASSERT_TRUE(WriteStringToFile("+memory", *mRandDir + "/cgroup.subtree_control"))
137             << "Could not enable memcg under child cgroup subtree";
138 }
139