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