system/core
修訂 | c4c05e3b25c4e2935f5214850b8ff3103f94dc25 (tree) |
---|---|
時間 | 2019-04-11 00:01:06 |
作者 | Sandeep Patil <sspatil@goog...> |
Commiter | Sandeep Patil |
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.
Fixes: 130177765
Test: Tested by deliberately killing specific process in ProcessRecord()
Change-Id: I701808c3226bb9b3a350ccf8e67fb29b59b0d4e0
Merged-In: I701808c3226bb9b3a350ccf8e67fb29b59b0d4e0
Signed-off-by: Sandeep Patil <sspatil@google.com>
@@ -14,11 +14,17 @@ | ||
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 | |
17 | +#include <android-base/file.h> | |
18 | +#include <android-base/parseint.h> | |
19 | +#include <android-base/stringprintf.h> | |
20 | +#include <android-base/strings.h> | |
17 | 21 | #include <dirent.h> |
18 | 22 | #include <errno.h> |
19 | 23 | #include <inttypes.h> |
20 | 24 | #include <linux/kernel-page-flags.h> |
21 | 25 | #include <linux/oom.h> |
26 | +#include <meminfo/procmeminfo.h> | |
27 | +#include <meminfo/sysmeminfo.h> | |
22 | 28 | #include <stdio.h> |
23 | 29 | #include <stdlib.h> |
24 | 30 | #include <sys/types.h> |
@@ -29,14 +35,6 @@ | ||
29 | 35 | #include <sstream> |
30 | 36 | #include <vector> |
31 | 37 | |
32 | -#include <android-base/file.h> | |
33 | -#include <android-base/parseint.h> | |
34 | -#include <android-base/stringprintf.h> | |
35 | -#include <android-base/strings.h> | |
36 | - | |
37 | -#include <meminfo/procmeminfo.h> | |
38 | -#include <meminfo/sysmeminfo.h> | |
39 | - | |
40 | 38 | using ::android::meminfo::MemUsage; |
41 | 39 | using ::android::meminfo::ProcMemInfo; |
42 | 40 |
@@ -460,8 +458,16 @@ int main(int argc, char* argv[]) { | ||
460 | 458 | auto mark_swap_usage = [&](pid_t pid) -> bool { |
461 | 459 | ProcessRecord proc(pid, show_wss, pgflags, pgflags_mask); |
462 | 460 | if (!proc.valid()) { |
463 | - std::cerr << "Failed to create process record for: " << pid << std::endl; | |
464 | - return false; | |
461 | + // Check to see if the process is still around, skip the process if the proc | |
462 | + // directory is inaccessible. It was most likely killed while creating the process | |
463 | + // record | |
464 | + std::string procdir = ::android::base::StringPrintf("/proc/%d", pid); | |
465 | + if (access(procdir.c_str(), F_OK | R_OK)) return true; | |
466 | + | |
467 | + // Warn if we failed to gather process stats even while it is still alive. | |
468 | + // Return success here, so we continue to print stats for other processes. | |
469 | + std::cerr << "warning: failed to create process record for: " << pid << std::endl; | |
470 | + return true; | |
465 | 471 | } |
466 | 472 | |
467 | 473 | // Skip processes with no memory mappings |
@@ -479,9 +485,9 @@ int main(int argc, char* argv[]) { | ||
479 | 485 | return true; |
480 | 486 | }; |
481 | 487 | |
482 | - // Get a list of all pids currently running in the system in | |
483 | - // 1st pass through all processes. Mark each swap offset used by the process as we find them | |
484 | - // for calculating proportional swap usage later. | |
488 | + // Get a list of all pids currently running in the system in 1st pass through all processes. | |
489 | + // Mark each swap offset used by the process as we find them for calculating proportional | |
490 | + // swap usage later. | |
485 | 491 | if (!read_all_pids(&pids, mark_swap_usage)) { |
486 | 492 | std::cerr << "Failed to read all pids from the system" << std::endl; |
487 | 493 | exit(EXIT_FAILURE); |