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