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