From a42207e2e33710f3aa4c72d660f623f8754b81ab Mon Sep 17 00:00:00 2001 From: Sandeep Patil Date: Wed, 17 Apr 2019 11:38:15 -0700 Subject: [PATCH] procrank: do not keep process maps around procrank's usage in its output shows 20x increase from last year. This is because it is keeping the process maps around until termination. Fix that by getting rid of ProcMemInfo objects when done parsing /proc//maps,pagemap. Note that the total allocations do not change and have not necessarily regressed from Pie. Bug: 130672819 Test: adb shell procrank | grep 'procrank\|cmdline' Change-Id: Ib7bf960ed1d053347fcfc0c8aee9019607a1eb01 Signed-off-by: Sandeep Patil --- libmeminfo/tools/procrank.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/libmeminfo/tools/procrank.cpp b/libmeminfo/tools/procrank.cpp index 5e89254b9..cb3757dba 100644 --- a/libmeminfo/tools/procrank.cpp +++ b/libmeminfo/tools/procrank.cpp @@ -42,7 +42,6 @@ struct ProcessRecord { public: ProcessRecord(pid_t pid, bool get_wss = false, uint64_t pgflags = 0, uint64_t pgflags_mask = 0) : pid_(-1), - procmem_(nullptr), oomadj_(OOM_SCORE_ADJ_MAX + 1), cmdline_(""), proportional_swap_(0), @@ -79,15 +78,15 @@ struct ProcessRecord { // The .c_str() assignment below then takes care of trimming the cmdline at the first // 0x00. This is how original procrank worked (luckily) cmdline_.resize(strlen(cmdline_.c_str())); - procmem_ = std::move(procmem); + usage_or_wss_ = get_wss ? procmem->Wss() : procmem->Usage(); + swap_offsets_ = procmem->SwapOffsets(); pid_ = pid; } bool valid() const { return pid_ != -1; } void CalculateSwap(const uint16_t* swap_offset_array, float zram_compression_ratio) { - const std::vector& swp_offs = procmem_->SwapOffsets(); - for (auto& off : swp_offs) { + for (auto& off : swap_offsets_) { proportional_swap_ += getpagesize() / swap_offset_array[off]; unique_swap_ += swap_offset_array[off] == 1 ? getpagesize() : 0; zswap_ = proportional_swap_ * zram_compression_ratio; @@ -103,18 +102,19 @@ struct ProcessRecord { uint64_t zswap() const { return zswap_; } // Wrappers to ProcMemInfo - const std::vector& SwapOffsets() const { return procmem_->SwapOffsets(); } - const MemUsage& Usage() const { return procmem_->Usage(); } - const MemUsage& Wss() const { return procmem_->Wss(); } + const std::vector& SwapOffsets() const { return swap_offsets_; } + const MemUsage& Usage() const { return usage_or_wss_; } + const MemUsage& Wss() const { return usage_or_wss_; } private: pid_t pid_; - std::unique_ptr procmem_; int32_t oomadj_; std::string cmdline_; uint64_t proportional_swap_; uint64_t unique_swap_; uint64_t zswap_; + MemUsage usage_or_wss_; + std::vector swap_offsets_; }; // Show working set instead of memory consumption @@ -171,7 +171,7 @@ static bool read_all_pids(std::vector* pids, std::functiond_name, &pid)) continue; if (!for_each_pid(pid)) return false; - pids->push_back(pid); + pids->emplace_back(pid); } return true; @@ -471,7 +471,7 @@ int main(int argc, char* argv[]) { } // Skip processes with no memory mappings - uint64_t vss = proc.Usage().vss; + uint64_t vss = show_wss ? proc.Wss().vss : proc.Usage().vss; if (vss == 0) return true; // collect swap_offset counts from all processes in 1st pass @@ -481,7 +481,7 @@ int main(int argc, char* argv[]) { return false; } - procs.push_back(std::move(proc)); + procs.emplace_back(std::move(proc)); return true; };