From ba98e0e85709276a5bdf2f3812eaed30bb754e95 Mon Sep 17 00:00:00 2001 From: Sandeep Patil Date: Tue, 9 Apr 2019 12:31:36 -0700 Subject: [PATCH] libmeminfo/procrank: Ignore failures when process disappears. procrank currently fails if a process gets killed while it is reading the stats. This behavior is a regression from the previous version of procrank and is often undesired. Change procrank to silently ignore the process if it detects that it had been killed while reading the stats. If the process is still around, then print a warning about it and continue to read stats for other processes in the system. Bug: 130177765 Test: Tested by deliberately killing specific process in ProcessRecord() constructor Change-Id: I701808c3226bb9b3a350ccf8e67fb29b59b0d4e0 Signed-off-by: Sandeep Patil --- libmeminfo/tools/procrank.cpp | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/libmeminfo/tools/procrank.cpp b/libmeminfo/tools/procrank.cpp index 21a684c5c..5e89254b9 100644 --- a/libmeminfo/tools/procrank.cpp +++ b/libmeminfo/tools/procrank.cpp @@ -14,11 +14,17 @@ * limitations under the License. */ +#include +#include +#include +#include #include #include #include #include #include +#include +#include #include #include #include @@ -29,14 +35,6 @@ #include #include -#include -#include -#include -#include - -#include -#include - using ::android::meminfo::MemUsage; using ::android::meminfo::ProcMemInfo; @@ -460,8 +458,16 @@ int main(int argc, char* argv[]) { auto mark_swap_usage = [&](pid_t pid) -> bool { ProcessRecord proc(pid, show_wss, pgflags, pgflags_mask); if (!proc.valid()) { - std::cerr << "Failed to create process record for: " << pid << std::endl; - return false; + // Check to see if the process is still around, skip the process if the proc + // directory is inaccessible. It was most likely killed while creating the process + // record + std::string procdir = ::android::base::StringPrintf("/proc/%d", pid); + if (access(procdir.c_str(), F_OK | R_OK)) return true; + + // Warn if we failed to gather process stats even while it is still alive. + // Return success here, so we continue to print stats for other processes. + std::cerr << "warning: failed to create process record for: " << pid << std::endl; + return true; } // Skip processes with no memory mappings @@ -479,9 +485,9 @@ int main(int argc, char* argv[]) { return true; }; - // Get a list of all pids currently running in the system in - // 1st pass through all processes. Mark each swap offset used by the process as we find them - // for calculating proportional swap usage later. + // Get a list of all pids currently running in the system in 1st pass through all processes. + // Mark each swap offset used by the process as we find them for calculating proportional + // swap usage later. if (!read_all_pids(&pids, mark_swap_usage)) { std::cerr << "Failed to read all pids from the system" << std::endl; exit(EXIT_FAILURE);