• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 <getopt.h>
18 #include <inttypes.h>
19 #include <linux/kernel-page-flags.h>
20 #include <stdlib.h>
21 
22 #include <iostream>
23 #include <map>
24 #include <set>
25 #include <unordered_map>
26 #include <vector>
27 
28 #include <android-base/parseint.h>
29 #include <android-base/strings.h>
30 #include <meminfo/procmeminfo.h>
31 #include <procinfo/process.h>
32 
33 #include <processrecord.h>
34 #include <smapinfo.h>
35 
36 using ::android::smapinfo::SortOrder;
37 
usage(int exit_status)38 [[noreturn]] static void usage(int exit_status) {
39     std::cerr << "Usage: " << getprogname() << " [ -W ] [ -v | -r | -p | -u | -s | -h ] [-d PID]"
40               << std::endl
41               << "    -v  Sort by VSS." << std::endl
42               << "    -r  Sort by RSS." << std::endl
43               << "    -p  Sort by PSS." << std::endl
44               << "    -u  Sort by USS." << std::endl
45               << "    -s  Sort by swap." << std::endl
46               << "        (Default sort order is PSS.)" << std::endl
47               << "    -R  Reverse sort order (default is descending)." << std::endl
48               << "    -c  Only show cached (storage backed) pages" << std::endl
49               << "    -C  Only show non-cached (ram/swap backed) pages" << std::endl
50               << "    -k  Only show pages collapsed by KSM" << std::endl
51               << "    -w  Display statistics for working set only." << std::endl
52               << "    -W  Reset working set of processes." << std::endl
53               << "    -o  Show and sort by oom score against lowmemorykiller thresholds."
54               << std::endl
55               << "    -d  Filter to descendants of specified process (can be repeated)" << std::endl
56               << "    -h  Display this help screen." << std::endl;
57     exit(exit_status);
58 }
59 
main(int argc,char * argv[])60 int main(int argc, char* argv[]) {
61     // Count all pages by default.
62     uint64_t pgflags = 0;
63     uint64_t pgflags_mask = 0;
64 
65     // Sort by PSS descending by default.
66     SortOrder sort_order = SortOrder::BY_PSS;
67     bool reverse_sort = false;
68 
69     bool get_oomadj = false;
70     bool get_wss = false;
71     bool reset_wss = false;
72 
73     std::vector<pid_t> descendant_filter;
74 
75     int opt;
76     while ((opt = getopt(argc, argv, "cCd:hkoprRsuvwW")) != -1) {
77         switch (opt) {
78             case 'c':
79                 pgflags = 0;
80                 pgflags_mask = (1 << KPF_SWAPBACKED);
81                 break;
82             case 'C':
83                 pgflags = (1 << KPF_SWAPBACKED);
84                 pgflags_mask = (1 << KPF_SWAPBACKED);
85                 break;
86             case 'd': {
87                 pid_t p;
88                 if (!android::base::ParseInt(optarg, &p)) {
89                     std::cerr << "Failed to parse pid '" << optarg << "'" << std::endl;
90                     usage(EXIT_FAILURE);
91                 }
92                 descendant_filter.push_back(p);
93                 break;
94             }
95             case 'h':
96                 usage(EXIT_SUCCESS);
97             case 'k':
98                 pgflags = (1 << KPF_KSM);
99                 pgflags_mask = (1 << KPF_KSM);
100                 break;
101             case 'o':
102                 sort_order = SortOrder::BY_OOMADJ;
103                 get_oomadj = true;
104                 break;
105             case 'p':
106                 sort_order = SortOrder::BY_PSS;
107                 break;
108             case 'r':
109                 sort_order = SortOrder::BY_RSS;
110                 break;
111             case 'R':
112                 reverse_sort = true;
113                 break;
114             case 's':
115                 sort_order = SortOrder::BY_SWAP;
116                 break;
117             case 'u':
118                 sort_order = SortOrder::BY_USS;
119                 break;
120             case 'v':
121                 sort_order = SortOrder::BY_VSS;
122                 break;
123             case 'w':
124                 get_wss = true;
125                 break;
126             case 'W':
127                 reset_wss = true;
128                 break;
129             default:
130                 usage(EXIT_FAILURE);
131         }
132     }
133 
134     std::set<pid_t> pids;
135     if (!::android::smapinfo::get_all_pids(&pids)) {
136         std::cerr << "Failed to get all pids." << std::endl;
137         exit(EXIT_FAILURE);
138     }
139 
140     if (descendant_filter.size()) {
141         // Map from parent pid to all of its children.
142         std::unordered_map<pid_t, std::vector<pid_t>> pid_tree;
143 
144         for (pid_t pid : pids) {
145             android::procinfo::ProcessInfo info;
146             std::string error;
147             if (!android::procinfo::GetProcessInfo(pid, &info, &error)) {
148                 std::cerr << "warning: failed to get process info for: " << pid << ": " << error
149                           << std::endl;
150                 continue;
151             }
152 
153             pid_tree[info.ppid].push_back(pid);
154         }
155 
156         std::set<pid_t> final_pids;
157         std::vector<pid_t>& frontier = descendant_filter;
158 
159         // Do a breadth-first walk of the process tree, starting from the pids we were given.
160         while (!frontier.empty()) {
161             pid_t pid = frontier.back();
162             frontier.pop_back();
163 
164             // It's possible for the pid we're looking at to already be in our list if one of the
165             // passed in processes descends from another, or if the same pid is passed twice.
166             auto [it, inserted] = final_pids.insert(pid);
167             if (inserted) {
168                 auto it = pid_tree.find(pid);
169                 if (it != pid_tree.end()) {
170                     // Add all of the children of |pid| to the list of nodes to visit.
171                     frontier.insert(frontier.end(), it->second.begin(), it->second.end());
172                 }
173             }
174         }
175 
176         pids = std::move(final_pids);
177     }
178 
179     if (reset_wss) {
180         for (pid_t pid : pids) {
181             if (!::android::meminfo::ProcMemInfo::ResetWorkingSet(pid)) {
182                 std::cerr << "Failed to reset working set of all processes" << std::endl;
183                 exit(EXIT_FAILURE);
184             }
185         }
186         // Other options passed to procrank are ignored if reset_wss is true.
187         return 0;
188     }
189 
190     bool success = ::android::smapinfo::run_procrank(pgflags, pgflags_mask, pids, get_oomadj,
191                                                      get_wss, sort_order, reverse_sort, nullptr,
192                                                      std::cout, std::cerr);
193     if (!success) {
194         exit(EXIT_FAILURE);
195     }
196     return 0;
197 }
198