blob: c5e74c1035d08f7bbe64bd15fa595e0e15571459 [file] [log] [blame]
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -07001/*
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
Mark Salyzynb4c16852017-01-10 13:19:54 -080026#include <log/log.h>
Yifan Hongb1007492016-12-02 11:28:04 -080027
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -070028using android::hardware::memtrack::V1_0::IMemtrack;
29using android::hardware::memtrack::V1_0::MemtrackType;
30using android::hardware::memtrack::V1_0::MemtrackRecord;
31using android::hardware::memtrack::V1_0::MemtrackFlag;
32using android::hardware::memtrack::V1_0::MemtrackStatus;
33using android::hardware::hidl_vec;
Connor O'Brienbbcf7192017-02-27 12:21:39 -080034using android::hardware::Return;
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -070035
36struct memtrack_proc_type {
37 MemtrackType type;
38 std::vector<MemtrackRecord> records;
39};
40
41struct memtrack_proc {
42 pid_t pid;
43 memtrack_proc_type types[static_cast<int>(MemtrackType::NUM_TYPES)];
44};
45
46//TODO(b/31632518)
47static android::sp<IMemtrack> get_instance() {
Chris Phoenixbb1083e2017-01-23 14:00:27 -080048 static android::sp<IMemtrack> module = IMemtrack::getService();
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -070049 if (module == nullptr) {
50 ALOGE("Couldn't load memtrack module");
51 }
52 return module;
53}
54
55memtrack_proc *memtrack_proc_new(void)
56{
57 return new memtrack_proc();
58}
59
60void memtrack_proc_destroy(memtrack_proc *p)
61{
62 delete(p);
63}
64
65static 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
Connor O'Brienbbcf7192017-02-27 12:21:39 -080073 Return<void> ret = memtrack->getMemory(pid, type,
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -070074 [&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 });
Connor O'Brienbbcf7192017-02-27 12:21:39 -080085 return ret.isOk() ? err : -1;
Ruchi Kandoi6e5739d2016-10-03 16:56:02 -070086}
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 */
95static int memtrack_proc_sanity_check(memtrack_proc* /*p*/)
96{
97 return 0;
98}
99
100int 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
116static 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
134ssize_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
140ssize_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
147ssize_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
153ssize_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
160ssize_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
167ssize_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}