summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSandeep Patil <sspatil@google.com>2019-04-09 12:31:36 -0700
committerSandeep Patil <sspatil@google.com>2019-04-09 12:31:36 -0700
commita1dbb456dc44a28c21c6a0e7ad63c4093da99cb8 (patch)
treee3a2442e6e4fa17b172aa95eb519450a057bcd5f
parent364ee4aa456576b8b13da1511c4306498d7762fa (diff)
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 <sspatil@google.com>
-rw-r--r--libmeminfo/tools/procrank.cpp32
1 files changed, 19 insertions, 13 deletions
diff --git a/libmeminfo/tools/procrank.cpp b/libmeminfo/tools/procrank.cpp
index 21a684c5c3..5e89254b93 100644
--- a/libmeminfo/tools/procrank.cpp
+++ b/libmeminfo/tools/procrank.cpp
@@ -14,11 +14,17 @@
* limitations under the License.
*/
+#include <android-base/file.h>
+#include <android-base/parseint.h>
+#include <android-base/stringprintf.h>
+#include <android-base/strings.h>
#include <dirent.h>
#include <errno.h>
#include <inttypes.h>
#include <linux/kernel-page-flags.h>
#include <linux/oom.h>
+#include <meminfo/procmeminfo.h>
+#include <meminfo/sysmeminfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
@@ -29,14 +35,6 @@
#include <sstream>
#include <vector>
-#include <android-base/file.h>
-#include <android-base/parseint.h>
-#include <android-base/stringprintf.h>
-#include <android-base/strings.h>
-
-#include <meminfo/procmeminfo.h>
-#include <meminfo/sysmeminfo.h>
-
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);