1 /*
2 * Copyright (C) 2022 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 #include <sys/mman.h>
17 #include <sys/syscall.h>
18 #include <sstream>
19 #include <string>
20
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/stringprintf.h>
24 #include <cutils/properties.h>
25 #include <gtest/gtest.h>
26 #include <liblmkd_utils.h>
27 #include <log/log_properties.h>
28 #include <private/android_filesystem_config.h>
29 #include <stdlib.h>
30
31 using namespace android::base;
32
33 #ifndef __NR_process_mrelease
34 #define __NR_process_mrelease 448
35 #endif
36
37 #define INKERNEL_MINFREE_PATH "/sys/module/lowmemorykiller/parameters/minfree"
38
39 #define LMKD_LOGCAT_MARKER "lowmemorykiller"
40 #define LMKD_KILL_TEMPLATE "Kill \'[^']*\' \\\(%d\\)"
41 #define LMKD_REAP_TEMPLATE "Process %d was reaped"
42 #define LMKD_REAP_FAIL_TEMPLATE "process_mrelease %d failed"
43
44 #define LMKD_KILL_LINE_START LMKD_LOGCAT_MARKER ": Kill"
45 #define LMKD_REAP_LINE_START LMKD_LOGCAT_MARKER ": Process"
46 #define LMKD_REAP_TIME_TEMPLATE LMKD_LOGCAT_MARKER ": Process %d was reaped in %ldms"
47 #define LMKD_REAP_MRELESE_ERR_MARKER ": process_mrelease"
48 #define LMKD_REAP_NO_PROCESS_TEMPLATE ": process_mrelease %d failed: No such process"
49
50 #define ONE_MB (1 << 20)
51
52 // Test constant parameters
53 #define OOM_ADJ_MAX 1000
54 #define ALLOC_STEP (5 * ONE_MB)
55 #define ALLOC_DELAY 200
56
57 // used to create ptr aliasing and prevent compiler optimizing the access
58 static volatile void* gptr;
59
60 class LmkdTest : public ::testing::Test {
61 public:
SetUp()62 virtual void SetUp() {
63 // test requirements
64 if (getuid() != static_cast<unsigned>(AID_ROOT)) {
65 GTEST_SKIP() << "Must be root, skipping test";
66 }
67
68 if (!__android_log_is_debuggable()) {
69 GTEST_SKIP() << "Must be userdebug build, skipping test";
70 }
71
72 if (!access(INKERNEL_MINFREE_PATH, W_OK)) {
73 GTEST_SKIP() << "Must not have kernel lowmemorykiller driver,"
74 << " skipping test";
75 }
76
77 // should be able to turn on lmkd debug information
78 if (!property_get_bool("ro.lmk.debug", true)) {
79 GTEST_SKIP() << "Can't run with ro.lmk.debug property set to 'false', skipping test";
80 }
81
82 // setup lmkd connection
83 ASSERT_FALSE((sock = lmkd_connect()) < 0)
84 << "Failed to connect to lmkd process, err=" << strerror(errno);
85
86 // enable ro.lmk.debug if not already enabled
87 if (!property_get_bool("ro.lmk.debug", false)) {
88 EXPECT_EQ(property_set("ro.lmk.debug", "true"), 0);
89 EXPECT_EQ(lmkd_update_props(sock), UPDATE_PROPS_SUCCESS)
90 << "Failed to reinitialize lmkd";
91 }
92
93 uid = getuid();
94 }
95
TearDown()96 virtual void TearDown() {
97 // drop lmkd connection
98 close(sock);
99 }
100
SetupChild(pid_t pid,int oomadj)101 void SetupChild(pid_t pid, int oomadj) {
102 struct lmk_procprio params;
103
104 params.pid = pid;
105 params.uid = uid;
106 params.oomadj = oomadj;
107 params.ptype = PROC_TYPE_APP;
108 ASSERT_FALSE(lmkd_register_proc(sock, ¶ms) < 0)
109 << "Failed to communicate with lmkd, err=" << strerror(errno);
110 GTEST_LOG_(INFO) << "Target process " << pid << " launched";
111 if (property_get_bool("ro.config.low_ram", false)) {
112 ASSERT_FALSE(create_memcg(uid, pid) != 0)
113 << "Target process " << pid << " failed to create a cgroup";
114 }
115 }
116
SendProcsPrioRequest(struct lmk_procs_prio procs_prio_request,int procs_count)117 void SendProcsPrioRequest(struct lmk_procs_prio procs_prio_request, int procs_count) {
118 ASSERT_FALSE(lmkd_register_procs(sock, &procs_prio_request, procs_count) < 0)
119 << "Failed to communicate with lmkd, err=" << strerror(errno);
120 }
121
SendGetKillCountRequest(struct lmk_getkillcnt * get_kill_cnt_request)122 void SendGetKillCountRequest(struct lmk_getkillcnt* get_kill_cnt_request) {
123 ASSERT_GE(lmkd_get_kill_count(sock, get_kill_cnt_request), 0)
124 << "Failed fetching lmkd kill count";
125 }
126
ExecCommand(const std::string & command)127 static std::string ExecCommand(const std::string& command) {
128 FILE* fp = popen(command.c_str(), "r");
129 std::string content;
130 ReadFdToString(fileno(fp), &content);
131 pclose(fp);
132 return content;
133 }
134
ReadLogcat(const std::string & tag,const std::string & regex)135 static std::string ReadLogcat(const std::string& tag, const std::string& regex) {
136 std::string cmd = "logcat -d -b all";
137 if (!tag.empty()) {
138 cmd += " -s \"" + tag + "\"";
139 }
140 if (!regex.empty()) {
141 cmd += " -e \"" + regex + "\"";
142 }
143 return ExecCommand(cmd);
144 }
145
ConsumeMemory(size_t total_size,size_t step_size,size_t step_delay)146 static size_t ConsumeMemory(size_t total_size, size_t step_size, size_t step_delay) {
147 volatile void* ptr;
148 size_t allocated_size = 0;
149
150 while (allocated_size < total_size) {
151 ptr = mmap(NULL, step_size, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
152 if (ptr != MAP_FAILED) {
153 // create ptr aliasing to prevent compiler optimizing the access
154 gptr = ptr;
155 // make data non-zero
156 memset((void*)ptr, (int)(allocated_size + 1), step_size);
157 allocated_size += step_size;
158 }
159 usleep(step_delay);
160 }
161 return allocated_size;
162 }
163
ParseProcSize(const std::string & line,long & rss,long & swap)164 static bool ParseProcSize(const std::string& line, long& rss, long& swap) {
165 size_t pos = line.find("to free");
166 if (pos == std::string::npos) {
167 return false;
168 }
169 return sscanf(line.c_str() + pos, "to free %ldkB rss, %ldkB swap", &rss, &swap) == 2;
170 }
171
ParseReapTime(const std::string & line,pid_t pid,long & reap_time)172 static bool ParseReapTime(const std::string& line, pid_t pid, long& reap_time) {
173 int reap_pid;
174 return sscanf(line.c_str(), LMKD_REAP_TIME_TEMPLATE, &reap_pid, &reap_time) == 2 &&
175 reap_pid == pid;
176 }
177
ParseReapNoProcess(const std::string & line,pid_t pid)178 static bool ParseReapNoProcess(const std::string& line, pid_t pid) {
179 int reap_pid;
180 return sscanf(line.c_str(), LMKD_REAP_NO_PROCESS_TEMPLATE, &reap_pid) == 1 &&
181 reap_pid == pid;
182 }
183
getLmkdTestUid() const184 uid_t getLmkdTestUid() const { return uid; }
185
186 private:
187 int sock;
188 uid_t uid;
189 };
190
TEST_F(LmkdTest,TargetReaping)191 TEST_F(LmkdTest, TargetReaping) {
192 // test specific requirements
193 if (syscall(__NR_process_mrelease, -1, 0) && errno == ENOSYS) {
194 GTEST_SKIP() << "Must support process_mrelease syscall, skipping test";
195 }
196
197 // for a child to act as a target process
198 pid_t pid = fork();
199 ASSERT_FALSE(pid < 0) << "Failed to spawn a child process, err=" << strerror(errno);
200 if (pid != 0) {
201 // parent
202 waitpid(pid, NULL, 0);
203 } else {
204 // child
205 SetupChild(getpid(), OOM_ADJ_MAX);
206 // allocate memory until killed
207 ConsumeMemory((size_t)-1, ALLOC_STEP, ALLOC_DELAY);
208 // should not reach here, child should be killed by OOM
209 FAIL() << "Target process " << pid << " was not killed";
210 }
211
212 std::string regex = StringPrintf("((" LMKD_KILL_TEMPLATE ")|(" LMKD_REAP_TEMPLATE
213 ")|(" LMKD_REAP_FAIL_TEMPLATE "))",
214 pid, pid, pid);
215 std::string logcat_out = ReadLogcat(LMKD_LOGCAT_MARKER ":I", regex);
216
217 // find kill report
218 size_t line_start = logcat_out.find(LMKD_KILL_LINE_START);
219 ASSERT_TRUE(line_start != std::string::npos) << "Kill report is not found";
220 size_t line_end = logcat_out.find('\n', line_start);
221 std::string line = logcat_out.substr(
222 line_start, line_end == std::string::npos ? std::string::npos : line_end - line_start);
223 long rss, swap;
224 ASSERT_TRUE(ParseProcSize(line, rss, swap)) << "Kill report format is invalid";
225
226 // find reap duration report
227 line_start = logcat_out.find(LMKD_REAP_LINE_START);
228 if (line_start == std::string::npos) {
229 // Target might have exited before reaping started
230 line_start = logcat_out.find(LMKD_REAP_MRELESE_ERR_MARKER);
231
232 ASSERT_TRUE(line_start != std::string::npos) << "Reaping time report is not found";
233
234 line_end = logcat_out.find('\n', line_start);
235 line = logcat_out.substr(line_start, line_end == std::string::npos ? std::string::npos
236 : line_end - line_start);
237 ASSERT_TRUE(ParseReapNoProcess(line, pid)) << "Failed to reap the target " << pid;
238 return;
239 }
240 line_end = logcat_out.find('\n', line_start);
241 line = logcat_out.substr(
242 line_start, line_end == std::string::npos ? std::string::npos : line_end - line_start);
243 long reap_time;
244 ASSERT_TRUE(ParseReapTime(line, pid, reap_time) && reap_time >= 0)
245 << "Reaping time report format is invalid";
246
247 // occasionally the reaping happens quickly enough that it's reported as 0ms
248 if (reap_time > 0) {
249 double reclaim_speed = ((double)rss + swap) / reap_time;
250 GTEST_LOG_(INFO) << "Reclaim speed " << reclaim_speed << "kB/ms (" << rss << "kB rss + "
251 << swap << "kB swap) / " << reap_time << "ms";
252 }
253 }
254
255 /*
256 * Verify that the `PROCS_PRIO` cmd is able to receive a batch of processes and adjust their
257 * those processes' OOM score.
258 */
TEST_F(LmkdTest,batch_procs_oom_score_adj)259 TEST_F(LmkdTest, batch_procs_oom_score_adj) {
260 struct ChildProcessInfo {
261 pid_t pid;
262 int original_oom_score;
263 int req_new_oom_score;
264 };
265
266 struct ChildProcessInfo children_info[PROCS_PRIO_MAX_RECORD_COUNT];
267
268 for (unsigned int i = 0; i < PROCS_PRIO_MAX_RECORD_COUNT; i++) {
269 children_info[i].pid = fork();
270 if (children_info[i].pid < 0) {
271 for (const auto child : children_info)
272 if (child.pid >= 0) kill(child.pid, SIGKILL);
273 FAIL() << "Failed forking process in iteration=" << i;
274 } else if (children_info[i].pid == 0) {
275 /*
276 * Keep the children alive, the parent process will kill it
277 * once we are done with it.
278 */
279 while (true) {
280 sleep(20);
281 }
282 }
283 }
284
285 struct lmk_procs_prio procs_prio_request;
286 const uid_t parent_uid = getLmkdTestUid();
287
288 for (unsigned int i = 0; i < PROCS_PRIO_MAX_RECORD_COUNT; i++) {
289 if (children_info[i].pid < 0) continue;
290
291 const std::string process_oom_path =
292 "proc/" + std::to_string(children_info[i].pid) + "/oom_score_adj";
293 std::string curr_oom_score;
294 if (!ReadFileToString(process_oom_path, &curr_oom_score) || curr_oom_score.empty()) {
295 for (const auto child : children_info)
296 if (child.pid >= 0) kill(child.pid, SIGKILL);
297 FAIL() << "Failed reading original oom score for child process: "
298 << children_info[i].pid;
299 }
300
301 children_info[i].original_oom_score = atoi(curr_oom_score.c_str());
302 children_info[i].req_new_oom_score =
303 ((unsigned int)children_info[i].original_oom_score != i) ? i : (i + 10);
304 procs_prio_request.procs[i] = {.pid = children_info[i].pid,
305 .uid = parent_uid,
306 .oomadj = children_info[i].req_new_oom_score,
307 .ptype = proc_type::PROC_TYPE_APP};
308 }
309
310 /*
311 * Submit batching, then send a new/different request and wait for LMKD
312 * to respond to it. This ensures that LMKD has finished the batching
313 * request and we can now read/validate the new OOM scores.
314 */
315 SendProcsPrioRequest(procs_prio_request, PROCS_PRIO_MAX_RECORD_COUNT);
316 struct lmk_getkillcnt kill_cnt_req = {.min_oomadj = -1000, .max_oomadj = 1000};
317 SendGetKillCountRequest(&kill_cnt_req);
318
319 for (auto child_info : children_info) {
320 if (child_info.pid < 0) continue;
321 const std::string process_oom_path =
322 "proc/" + std::to_string(child_info.pid) + "/oom_score_adj";
323 std::string curr_oom_score;
324 if (!ReadFileToString(process_oom_path, &curr_oom_score) || curr_oom_score.empty()) {
325 for (const auto child : children_info)
326 if (child.pid >= 0) kill(child.pid, SIGKILL);
327 FAIL() << "Failed reading new oom score for child process: " << child_info.pid;
328 }
329 kill(child_info.pid, SIGKILL);
330
331 const int actual_new_oom_score = atoi(curr_oom_score.c_str());
332 ASSERT_EQ(child_info.req_new_oom_score, actual_new_oom_score)
333 << "Child with pid=" << child_info.pid << " didn't update its OOM score";
334 }
335 }
336
main(int argc,char ** argv)337 int main(int argc, char** argv) {
338 ::testing::InitGoogleTest(&argc, argv);
339 InitLogging(argv, StderrLogger);
340 return RUN_ALL_TESTS();
341 }
342