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 #include <android/hardware/memtrack/1.0/IMemtrack.h>
18 #include <memtrack/memtrack.h>
19
20 #include <errno.h>
21 #include <malloc.h>
22 #include <vector>
23 #include <string.h>
24 #include <mutex>
25
26 #include <log/log.h>
27
28 using android::hardware::memtrack::V1_0::IMemtrack;
29 using android::hardware::memtrack::V1_0::MemtrackType;
30 using android::hardware::memtrack::V1_0::MemtrackRecord;
31 using android::hardware::memtrack::V1_0::MemtrackFlag;
32 using android::hardware::memtrack::V1_0::MemtrackStatus;
33 using android::hardware::hidl_vec;
34 using android::hardware::Return;
35
36 struct memtrack_proc_type {
37 MemtrackType type;
38 std::vector<MemtrackRecord> records;
39 };
40
41 struct memtrack_proc {
42 pid_t pid;
43 memtrack_proc_type types[static_cast<int>(MemtrackType::NUM_TYPES)];
44 };
45
46 //TODO(b/31632518)
get_instance()47 static android::sp<IMemtrack> get_instance() {
48 static android::sp<IMemtrack> module = IMemtrack::getService();
49 static bool logged = false;
50 if (module == nullptr && !logged) {
51 logged = true;
52 ALOGE("Couldn't load memtrack module");
53 }
54 return module;
55 }
56
memtrack_proc_new(void)57 memtrack_proc *memtrack_proc_new(void)
58 {
59 return new memtrack_proc();
60 }
61
memtrack_proc_destroy(memtrack_proc * p)62 void memtrack_proc_destroy(memtrack_proc *p)
63 {
64 delete(p);
65 }
66
memtrack_proc_get_type(memtrack_proc_type * t,pid_t pid,MemtrackType type)67 static int memtrack_proc_get_type(memtrack_proc_type *t,
68 pid_t pid, MemtrackType type)
69 {
70 int err = 0;
71 android::sp<IMemtrack> memtrack = get_instance();
72 if (memtrack == nullptr)
73 return -1;
74
75 Return<void> ret = memtrack->getMemory(pid, type,
76 [&t, &err](MemtrackStatus status, hidl_vec<MemtrackRecord> records) {
77 if (status != MemtrackStatus::SUCCESS) {
78 err = -1;
79 t->records.resize(0);
80 }
81 t->records.resize(records.size());
82 for (size_t i = 0; i < records.size(); i++) {
83 t->records[i].sizeInBytes = records[i].sizeInBytes;
84 t->records[i].flags = records[i].flags;
85 }
86 });
87 return ret.isOk() ? err : -1;
88 }
89
90 /* TODO: sanity checks on return values from HALs:
91 * make sure no records have invalid flags set
92 * - unknown flags
93 * - too many flags of a single category
94 * - missing ACCOUNTED/UNACCOUNTED
95 * make sure there are not overlapping SHARED and SHARED_PSS records
96 */
memtrack_proc_sanity_check(memtrack_proc *)97 static int memtrack_proc_sanity_check(memtrack_proc* /*p*/)
98 {
99 return 0;
100 }
101
memtrack_proc_get(memtrack_proc * p,pid_t pid)102 int memtrack_proc_get(memtrack_proc *p, pid_t pid)
103 {
104 if (!p) {
105 return -EINVAL;
106 }
107
108 p->pid = pid;
109 for (uint32_t i = 0; i < (uint32_t)MemtrackType::NUM_TYPES; i++) {
110 int ret = memtrack_proc_get_type(&p->types[i], pid, (MemtrackType)i);
111 if (ret != 0)
112 return ret;
113 }
114
115 return memtrack_proc_sanity_check(p);
116 }
117
memtrack_proc_sum(memtrack_proc * p,const std::vector<MemtrackType> & types,uint32_t flags)118 static ssize_t memtrack_proc_sum(memtrack_proc *p,
119 const std::vector<MemtrackType>& types, uint32_t flags)
120 {
121 ssize_t sum = 0;
122
123 for (size_t i = 0; i < types.size(); i++) {
124 memtrack_proc_type type = p->types[static_cast<int>(types[i])];
125 std::vector<MemtrackRecord> records = type.records;
126 for (size_t j = 0; j < records.size(); j++) {
127 if ((records[j].flags & flags) == flags) {
128 sum += records[j].sizeInBytes;
129 }
130 }
131 }
132
133 return sum;
134 }
135
memtrack_proc_graphics_total(memtrack_proc * p)136 ssize_t memtrack_proc_graphics_total(memtrack_proc *p)
137 {
138 std::vector<MemtrackType> types = {MemtrackType::GRAPHICS};
139 return memtrack_proc_sum(p, types, 0);
140 }
141
memtrack_proc_graphics_pss(memtrack_proc * p)142 ssize_t memtrack_proc_graphics_pss(memtrack_proc *p)
143 {
144 std::vector<MemtrackType> types = { MemtrackType::GRAPHICS };
145 return memtrack_proc_sum(p, types,
146 (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
147 }
148
memtrack_proc_gl_total(memtrack_proc * p)149 ssize_t memtrack_proc_gl_total(memtrack_proc *p)
150 {
151 std::vector<MemtrackType> types = { MemtrackType::GL };
152 return memtrack_proc_sum(p, types, 0);
153 }
154
memtrack_proc_gl_pss(memtrack_proc * p)155 ssize_t memtrack_proc_gl_pss(memtrack_proc *p)
156 {
157 std::vector<MemtrackType> types = { MemtrackType::GL };
158 return memtrack_proc_sum(p, types,
159 (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
160 }
161
memtrack_proc_other_total(memtrack_proc * p)162 ssize_t memtrack_proc_other_total(memtrack_proc *p)
163 {
164 std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
165 MemtrackType::CAMERA, MemtrackType::OTHER };
166 return memtrack_proc_sum(p, types, 0);
167 }
168
memtrack_proc_other_pss(memtrack_proc * p)169 ssize_t memtrack_proc_other_pss(memtrack_proc *p)
170 {
171 std::vector<MemtrackType> types = { MemtrackType::MULTIMEDIA,
172 MemtrackType::CAMERA, MemtrackType::OTHER };
173 return memtrack_proc_sum(p, types,
174 (uint32_t)MemtrackFlag::SMAPS_UNACCOUNTED);
175 }
176