lmkd/tests: handle the case when process exits before reaping starts am: 0cb4aae3e6

Original change: https://android-review.googlesource.com/c/platform/system/memory/lmkd/+/2267890

Change-Id: Ib225b60564bb0dd9518bcd9c8a202fc5d2c96b2f
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Suren Baghdasaryan 2022-10-26 16:14:23 +00:00 committed by Automerger Merge Worker
commit c7a3bf51b6
1 changed files with 24 additions and 3 deletions

View File

@ -38,10 +38,13 @@ using namespace android::base;
#define LMKD_LOGCAT_MARKER "lowmemorykiller" #define LMKD_LOGCAT_MARKER "lowmemorykiller"
#define LMKD_KILL_TEMPLATE "Kill \'[^']*\' \\\(%d\\)" #define LMKD_KILL_TEMPLATE "Kill \'[^']*\' \\\(%d\\)"
#define LMKD_REAP_TEMPLATE "Process %d was reaped" #define LMKD_REAP_TEMPLATE "Process %d was reaped"
#define LMKD_REAP_FAIL_TEMPLATE "process_mrelease %d failed"
#define LMKD_KILL_LINE_START LMKD_LOGCAT_MARKER ": Kill" #define LMKD_KILL_LINE_START LMKD_LOGCAT_MARKER ": Kill"
#define LMKD_REAP_LINE_START LMKD_LOGCAT_MARKER ": Process" #define LMKD_REAP_LINE_START LMKD_LOGCAT_MARKER ": Process"
#define LMKD_REAP_TIME_TEMPLATE LMKD_LOGCAT_MARKER ": Process %d was reaped in %ldms" #define LMKD_REAP_TIME_TEMPLATE LMKD_LOGCAT_MARKER ": Process %d was reaped in %ldms"
#define LMKD_REAP_MRELESE_ERR_MARKER ": process_mrelease"
#define LMKD_REAP_NO_PROCESS_TEMPLATE ": process_mrelease %d failed: No such process"
#define ONE_MB (1 << 20) #define ONE_MB (1 << 20)
@ -161,6 +164,12 @@ class LmkdTest : public ::testing::Test {
reap_pid == pid; reap_pid == pid;
} }
static bool ParseReapNoProcess(const std::string& line, pid_t pid) {
int reap_pid;
return sscanf(line.c_str(), LMKD_REAP_NO_PROCESS_TEMPLATE, &reap_pid) == 1 &&
reap_pid == pid;
}
private: private:
int sock; int sock;
uid_t uid; uid_t uid;
@ -187,8 +196,9 @@ TEST_F(LmkdTest, TargetReaping) {
FAIL() << "Target process " << pid << " was not killed"; FAIL() << "Target process " << pid << " was not killed";
} }
std::string regex = std::string regex = StringPrintf("((" LMKD_KILL_TEMPLATE ")|(" LMKD_REAP_TEMPLATE
StringPrintf("((" LMKD_KILL_TEMPLATE ")|(" LMKD_REAP_TEMPLATE "))", pid, pid); ")|(" LMKD_REAP_FAIL_TEMPLATE "))",
pid, pid, pid);
std::string logcat_out = ReadLogcat(LMKD_LOGCAT_MARKER ":I", regex); std::string logcat_out = ReadLogcat(LMKD_LOGCAT_MARKER ":I", regex);
// find kill report // find kill report
@ -202,7 +212,18 @@ TEST_F(LmkdTest, TargetReaping) {
// find reap duration report // find reap duration report
line_start = logcat_out.find(LMKD_REAP_LINE_START, line_end); line_start = logcat_out.find(LMKD_REAP_LINE_START, line_end);
ASSERT_TRUE(line_start != std::string::npos) << "Reaping time report is not found"; if (line_start == std::string::npos) {
// Target might have exited before reaping started
line_start = logcat_out.find(LMKD_REAP_MRELESE_ERR_MARKER, line_end);
ASSERT_TRUE(line_start != std::string::npos) << "Reaping time report is not found";
line_end = logcat_out.find('\n', line_start);
line = logcat_out.substr(line_start, line_end == std::string::npos ? std::string::npos
: line_end - line_start);
ASSERT_TRUE(ParseReapNoProcess(line, pid)) << "Failed to reap the target " << pid;
return;
}
line_end = logcat_out.find('\n', line_start); line_end = logcat_out.find('\n', line_start);
line = logcat_out.substr( line = logcat_out.substr(
line_start, line_end == std::string::npos ? std::string::npos : line_end - line_start); line_start, line_end == std::string::npos ? std::string::npos : line_end - line_start);