Sandeep Patil | 54d8721 | 2018-08-29 17:10:47 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <errno.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <iomanip> |
| 22 | #include <iostream> |
| 23 | #include <sstream> |
| 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <meminfo/procmeminfo.h> |
| 28 | |
| 29 | using ProcMemInfo = ::android::meminfo::ProcMemInfo; |
| 30 | using MemUsage = ::android::meminfo::MemUsage; |
| 31 | |
| 32 | static void usage(const char* cmd) { |
| 33 | fprintf(stderr, |
| 34 | "Usage: %s [-i] [ -w | -W ] [ -p | -m ] [ -h ] pid\n" |
| 35 | " -i Uses idle page tracking for working set statistics.\n" |
| 36 | " -w Displays statistics for the working set only.\n" |
| 37 | " -W Resets the working set of the process.\n" |
| 38 | " -p Sort by PSS.\n" |
| 39 | " -u Sort by USS.\n" |
| 40 | " -m Sort by mapping order (as read from /proc).\n" |
| 41 | " -h Hide maps with no RSS.\n", |
| 42 | cmd); |
| 43 | } |
| 44 | |
| 45 | static void show_footer(uint32_t nelem, const std::string& padding) { |
| 46 | std::string elem(7, '-'); |
| 47 | |
| 48 | for (uint32_t i = 0; i < nelem; ++i) { |
| 49 | std::cout << std::setw(7) << elem << padding; |
| 50 | } |
| 51 | std::cout << std::endl; |
| 52 | } |
| 53 | |
| 54 | static void show_header(const std::vector<std::string>& header, const std::string& padding) { |
| 55 | if (header.empty()) return; |
| 56 | |
| 57 | for (size_t i = 0; i < header.size() - 1; ++i) { |
| 58 | std::cout << std::setw(7) << header[i] << padding; |
| 59 | } |
| 60 | std::cout << header.back() << std::endl; |
| 61 | show_footer(header.size() - 1, padding); |
| 62 | } |
| 63 | |
| 64 | static void scan_usage(std::stringstream& ss, const MemUsage& usage, const std::string& padding, |
| 65 | bool show_wss) { |
| 66 | // clear string stream first. |
| 67 | ss.str(""); |
| 68 | // TODO: use ::android::base::StringPrintf instead of <iomanip> here. |
| 69 | if (!show_wss) |
| 70 | ss << std::setw(6) << usage.vss/1024 << padding; |
| 71 | ss << std::setw(6) << usage.rss/1024 << padding << std::setw(6) |
| 72 | << usage.pss/1024 << padding << std::setw(6) << usage.uss/1024 << padding |
| 73 | << std::setw(6) << usage.shared_clean/1024 << padding << std::setw(6) |
| 74 | << usage.shared_dirty/1024 << padding << std::setw(6) |
| 75 | << usage.private_clean/1024 << padding << std::setw(6) |
| 76 | << usage.private_dirty/1024 << padding; |
| 77 | } |
| 78 | |
| 79 | static int show(ProcMemInfo& proc, bool hide_zeroes, bool show_wss) { |
| 80 | const std::vector<std::string> main_header = {"Vss", "Rss", "Pss", "Uss", "ShCl", |
| 81 | "ShDi", "PrCl", "PrDi", "Name"}; |
| 82 | const std::vector<std::string> wss_header = {"WRss", "WPss", "WUss", "WShCl", |
| 83 | "WShDi", "WPrCl", "WPrDi", "Name"}; |
| 84 | const std::vector<std::string>& header = show_wss ? wss_header : main_header; |
| 85 | |
| 86 | // Read process memory stats |
| 87 | const MemUsage& stats = show_wss ? proc.Wss() : proc.Usage(); |
| 88 | const std::vector<::android::meminfo::Vma>& maps = proc.Maps(); |
| 89 | |
| 90 | // following retains 'procmem' output so as to not break any scripts |
| 91 | // that rely on it. |
| 92 | std::string spaces = " "; |
| 93 | show_header(header, spaces); |
| 94 | const std::string padding = "K "; |
| 95 | std::stringstream ss; |
| 96 | for (auto& vma : maps) { |
| 97 | const MemUsage& vma_stats = show_wss ? vma.wss : vma.usage; |
| 98 | if (hide_zeroes && vma_stats.rss == 0) { |
| 99 | continue; |
| 100 | } |
| 101 | scan_usage(ss, vma_stats, padding, show_wss); |
| 102 | ss << vma.name << std::endl; |
| 103 | std::cout << ss.str(); |
| 104 | } |
| 105 | show_footer(header.size() - 1, spaces); |
| 106 | scan_usage(ss, stats, padding, show_wss); |
| 107 | ss << "TOTAL" << std::endl; |
| 108 | std::cout << ss.str(); |
| 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | int main(int argc, char* argv[]) { |
| 114 | bool use_pageidle = false; |
| 115 | bool hide_zeroes = false; |
| 116 | bool wss_reset = false; |
| 117 | bool show_wss = false; |
| 118 | int opt; |
| 119 | |
| 120 | while ((opt = getopt(argc, argv, "himpuWw")) != -1) { |
| 121 | switch (opt) { |
| 122 | case 'h': |
| 123 | hide_zeroes = true; |
| 124 | break; |
| 125 | case 'i': |
| 126 | use_pageidle = true; |
| 127 | break; |
| 128 | case 'm': |
| 129 | break; |
| 130 | case 'p': |
| 131 | break; |
| 132 | case 'u': |
| 133 | break; |
| 134 | case 'W': |
| 135 | wss_reset = true; |
| 136 | break; |
| 137 | case 'w': |
| 138 | show_wss = true; |
| 139 | break; |
| 140 | case '?': |
| 141 | usage(argv[0]); |
| 142 | return 0; |
| 143 | default: |
| 144 | abort(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | if (optind != (argc - 1)) { |
| 149 | fprintf(stderr, "Need exactly one pid at the end\n"); |
| 150 | usage(argv[0]); |
| 151 | exit(EXIT_FAILURE); |
| 152 | } |
| 153 | |
| 154 | pid_t pid = atoi(argv[optind]); |
| 155 | if (pid == 0) { |
| 156 | std::cerr << "Invalid process id" << std::endl; |
| 157 | exit(EXIT_FAILURE); |
| 158 | } |
| 159 | |
| 160 | bool need_wss = wss_reset || show_wss; |
| 161 | ProcMemInfo proc(pid, need_wss); |
| 162 | if (wss_reset) { |
| 163 | if (!proc.WssReset()) { |
| 164 | std::cerr << "Failed to reset working set of pid : " << pid << std::endl; |
| 165 | exit(EXIT_FAILURE); |
| 166 | } |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | return show(proc, hide_zeroes, show_wss); |
| 171 | } |