1 /*
2  * Copyright (C) 2013 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 <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/types.h>
21 
22 #include <filesystem>
23 #include <vector>
24 
25 #include <android-base/file.h>
26 #include <android-base/parseint.h>
27 #include <android-base/stringprintf.h>
28 #include <memtrack/memtrack.h>
29 
30 #define DIV_ROUND_UP(x, y) (((x) + (y)-1) / (y))
31 
getprocname(pid_t pid,std::string * name)32 static void getprocname(pid_t pid, std::string* name) {
33     std::string fname = ::android::base::StringPrintf("/proc/%d/cmdline", pid);
34     if (!::android::base::ReadFileToString(fname, name)) {
35         fprintf(stderr, "Failed to read cmdline from: %s\n", fname.c_str());
36         *name = "<unknown>";
37     }
38 }
39 
main(int,char **)40 int main(int /* argc */, char** /* argv */) {
41     int ret;
42     struct memtrack_proc* p;
43     std::vector<pid_t> pids;
44 
45     p = memtrack_proc_new();
46     if (p == nullptr) {
47         fprintf(stderr, "failed to create memtrack process handle\n");
48         exit(EXIT_FAILURE);
49     }
50 
51     for (auto& de : std::filesystem::directory_iterator("/proc")) {
52         if (!std::filesystem::is_directory(de.status())) {
53             continue;
54         }
55 
56         pid_t pid;
57         if (!::android::base::ParseInt(de.path().filename().string(), &pid)) {
58             continue;
59         }
60         pids.emplace_back(pid);
61     }
62 
63     for (auto& pid : pids) {
64         size_t v1;
65         size_t v2;
66         size_t v3;
67         size_t v4;
68         size_t v5;
69         size_t v6;
70         std::string cmdline;
71 
72         getprocname(pid, &cmdline);
73 
74         ret = memtrack_proc_get(p, pid);
75         if (ret) {
76             fprintf(stderr, "failed to get memory info for pid %d: %s (%d)\n", pid, strerror(-ret),
77                     ret);
78             continue;
79         }
80 
81         v1 = DIV_ROUND_UP(memtrack_proc_graphics_total(p), 1024);
82         v2 = DIV_ROUND_UP(memtrack_proc_graphics_pss(p), 1024);
83         v3 = DIV_ROUND_UP(memtrack_proc_gl_total(p), 1024);
84         v4 = DIV_ROUND_UP(memtrack_proc_gl_pss(p), 1024);
85         v5 = DIV_ROUND_UP(memtrack_proc_other_total(p), 1024);
86         v6 = DIV_ROUND_UP(memtrack_proc_other_pss(p), 1024);
87 
88         if (v1 | v2 | v3 | v4 | v5 | v6) {
89             fprintf(stdout, "%5d %6zu %6zu %6zu %6zu %6zu %6zu %s\n", pid, v1, v2, v3, v4, v5, v6,
90                     cmdline.c_str());
91         }
92     }
93 
94     memtrack_proc_destroy(p);
95 
96     return ret;
97 }
98