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 #define LOG_TAG "memtrack"
17
18 #include <aidl/android/hardware/memtrack/DeviceInfo.h>
19 #include <aidl/android/hardware/memtrack/IMemtrack.h>
20 #include <aidl/android/hardware/memtrack/MemtrackRecord.h>
21 #include <aidl/android/hardware/memtrack/MemtrackType.h>
22
23 #include <android/binder_manager.h>
24 #include <memtrack/memtrack.h>
25
26 #include <errno.h>
27 #include <malloc.h>
28 #include <string.h>
29 #include <mutex>
30 #include <vector>
31
32 #include <log/log.h>
33
34 using aidl::android::hardware::memtrack::DeviceInfo;
35 using aidl::android::hardware::memtrack::IMemtrack;
36 using aidl::android::hardware::memtrack::MemtrackRecord;
37 using aidl::android::hardware::memtrack::MemtrackType;
38
39 const std::vector<MemtrackType> kMemtrackTypes{ndk::enum_range<MemtrackType>().begin(),
40 ndk::enum_range<MemtrackType>().end()};
41
42 struct memtrack_proc_type {
43 MemtrackType type;
44 std::vector<MemtrackRecord> records;
45 };
46
47 struct memtrack_proc {
48 pid_t pid;
49 std::vector<memtrack_proc_type> types;
50
memtrack_procmemtrack_proc51 memtrack_proc() {
52 types.resize(kMemtrackTypes.size());
53 }
54 };
55
get_memtrack_proxy_service()56 static std::shared_ptr<IMemtrack> get_memtrack_proxy_service() {
57 const char* kMemtrackProxyService = "memtrack.proxy";
58 static std::shared_ptr<IMemtrack> memtrack_proxy_service = nullptr;
59 if (!memtrack_proxy_service &&
60 !(memtrack_proxy_service = IMemtrack::fromBinder(
61 ndk::SpAIBinder(AServiceManager_checkService(kMemtrackProxyService))))) {
62 ALOGE("Unable to connect to %s\n", kMemtrackProxyService);
63 }
64 return memtrack_proxy_service;
65 }
66
memtrack_gpu_device_info(std::vector<DeviceInfo> * device_info)67 bool memtrack_gpu_device_info(std::vector<DeviceInfo>* device_info) {
68 std::shared_ptr<IMemtrack> memtrack_proxy_service = get_memtrack_proxy_service();
69 if (!memtrack_proxy_service) {
70 return false;
71 }
72
73 auto status = memtrack_proxy_service->getGpuDeviceInfo(device_info);
74 return status.isOk();
75 }
76
memtrack_proc_new(void)77 memtrack_proc *memtrack_proc_new(void)
78 {
79 return new memtrack_proc();
80 }
81
memtrack_proc_destroy(memtrack_proc * p)82 void memtrack_proc_destroy(memtrack_proc *p)
83 {
84 delete(p);
85 }
86
memtrack_proc_get_type(memtrack_proc_type * t,pid_t pid,MemtrackType type)87 static int memtrack_proc_get_type(memtrack_proc_type *t,
88 pid_t pid, MemtrackType type)
89 {
90 int err = 0;
91
92 std::shared_ptr<IMemtrack> memtrack_proxy_service = get_memtrack_proxy_service();
93 if (!memtrack_proxy_service) {
94 return -1;
95 }
96
97 std::vector<MemtrackRecord> records;
98 auto status = memtrack_proxy_service->getMemory(pid, type, &records);
99
100 if (!status.isOk()) {
101 return -1;
102 }
103
104 t->records.resize(records.size());
105 for (size_t i = 0; i < records.size(); i++) {
106 t->records[i].sizeInBytes = records[i].sizeInBytes;
107 t->records[i].flags = records[i].flags;
108 }
109
110 return err;
111 }
112
113 /* TODO: sanity checks on return values from HALs:
114 * make sure no records have invalid flags set
115 * - unknown flags
116 * - too many flags of a single category
117 * - missing ACCOUNTED/UNACCOUNTED
118 * make sure there are not overlapping SHARED and SHARED_PSS records
119 */
memtrack_proc_sanity_check(memtrack_proc *)120 static int memtrack_proc_sanity_check(memtrack_proc* /*p*/)
121 {
122 return 0;
123 }
124
memtrack_proc_get(memtrack_proc * p,pid_t pid)125 int memtrack_proc_get(memtrack_proc *p, pid_t pid)
126 {
127 if (!p) {
128 return -EINVAL;
129 }
130
131 p->pid = pid;
132 for (int i = 0; i < kMemtrackTypes.size(); i++) {
133 int ret = memtrack_proc_get_type(&p->types[i], pid, kMemtrackTypes[i]);
134 if (ret != 0)
135 return ret;
136 }
137
138 return memtrack_proc_sanity_check(p);
139 }
140
memtrack_proc_sum(memtrack_proc * p,const std::vector<MemtrackType> & types,uint32_t flags)141 static ssize_t memtrack_proc_sum(memtrack_proc *p,
142 const std::vector<MemtrackType>& types, uint32_t flags)
143 {
144 ssize_t sum = 0;
145
146 for (size_t i = 0; i < types.size(); i++) {
147 memtrack_proc_type type = p->types[static_cast<int>(types[i])];
148 std::vector<MemtrackRecord> records = type.records;
149 for (size_t j = 0; j < records.size(); j++) {
150 if ((records[j].flags & flags) == flags) {
151 sum += records[j].sizeInBytes;
152 }
153 }
154 }
155
156 return sum;
157 }
158
memtrack_proc_graphics_total(memtrack_proc * p)159 ssize_t memtrack_proc_graphics_total(memtrack_proc *p)
160 {
161 std::vector<MemtrackType> types = {MemtrackType::GRAPHICS};
162 return memtrack_proc_sum(p, types, 0);
163 }
164
memtrack_proc_graphics_pss(memtrack_proc * p)165 ssize_t memtrack_proc_graphics_pss(memtrack_proc *p)
166 {
167 std::vector<MemtrackType> types = { MemtrackType::GRAPHICS };
168 return memtrack_proc_sum(p, types,
169 (uint32_t)MemtrackRecord::FLAG_SMAPS_UNACCOUNTED);
170 }
171
memtrack_proc_gl_total(memtrack_proc * p)172 ssize_t memtrack_proc_gl_total(memtrack_proc *p)
173 {
174 std::vector<MemtrackType> types = { MemtrackType::GL };
175 return memtrack_proc_sum(p, types, 0);
176 }
177
memtrack_proc_gl_pss(memtrack_proc * p)178 ssize_t memtrack_proc_gl_pss(memtrack_proc *p)
179 {
180 std::vector<MemtrackType> types = { MemtrackType::GL };
181 return memtrack_proc_sum(p, types,
182 (uint32_t)MemtrackRecord::FLAG_SMAPS_UNACCOUNTED);
183 }
184
memtrack_proc_other_total(memtrack_proc * p)185 ssize_t memtrack_proc_other_total(memtrack_proc *p)
186 {
187 std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
188 MemtrackType::CAMERA, MemtrackType::OTHER };
189 return memtrack_proc_sum(p, types, 0);
190 }
191
memtrack_proc_other_pss(memtrack_proc * p)192 ssize_t memtrack_proc_other_pss(memtrack_proc *p)
193 {
194 std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
195 MemtrackType::CAMERA, MemtrackType::OTHER };
196 return memtrack_proc_sum(p, types,
197 (uint32_t)MemtrackRecord::FLAG_SMAPS_UNACCOUNTED);
198 }
199