blob: 0bbf83d2b3034e7239809a2aedf7aa5835d26de2 [file] [log] [blame]
Ruchi Kandoi97b2f252016-09-29 13:55:10 -07001/*
2 * Copyright (C) 2016 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
17#define LOG_TAG "android.hardware.memtrack@1.0-impl"
Mark Salyzyn3ff52602017-01-10 10:16:48 -080018
19#include <log/log.h>
20
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070021#include <hardware/hardware.h>
22#include <hardware/memtrack.h>
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070023
24#include "Memtrack.h"
Mark Salyzyn3ff52602017-01-10 10:16:48 -080025
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070026namespace android {
27namespace hardware {
28namespace memtrack {
29namespace V1_0 {
30namespace implementation {
31
Connor O'Brien56dfb752017-01-20 18:26:52 -080032Memtrack::Memtrack(const memtrack_module_t *module) : mModule(module) {
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070033 if (mModule)
34 mModule->init(mModule);
35}
36
Steven Morelandd66d0dd2020-01-23 14:29:29 -080037Memtrack::~Memtrack() {}
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070038
39Return<void> Memtrack::getMemory(int32_t pid, MemtrackType type,
40 getMemory_cb _hidl_cb) {
41 hidl_vec<MemtrackRecord> records;
42 size_t temp = 0;
43 size_t *size = &temp;
44 int ret = 0;
45
46 if (mModule->getMemory == nullptr)
47 {
48 _hidl_cb(MemtrackStatus::SUCCESS, records);
49 return Void();
50 }
51 ret = mModule->getMemory(mModule, pid, static_cast<memtrack_type>(type),
52 NULL, size);
53 if (ret == 0)
54 {
55 memtrack_record *legacy_records = new memtrack_record[*size];
56 ret = mModule->getMemory(mModule, pid,
57 static_cast<memtrack_type>(type), legacy_records, size);
58 if (ret == 0)
59 {
60 records.resize(*size);
61 for(size_t i = 0; i < *size; i++)
62 {
63 records[i].sizeInBytes = legacy_records[i].size_in_bytes;
64 records[i].flags = legacy_records[i].flags;
65 }
66 }
67 delete[] legacy_records;
68 }
69 _hidl_cb(MemtrackStatus::SUCCESS, records);
70 return Void();
71}
72
73
Chris Phoenix5985ded2017-01-23 13:59:13 -080074IMemtrack* HIDL_FETCH_IMemtrack(const char* /* name */) {
Connor O'Brien56dfb752017-01-20 18:26:52 -080075 const hw_module_t* hw_module = nullptr;
76 const memtrack_module_t* memtrack_module = nullptr;
Chris Phoenix5985ded2017-01-23 13:59:13 -080077 int err = hw_get_module(MEMTRACK_HARDWARE_MODULE_ID, &hw_module);
Connor O'Brien56dfb752017-01-20 18:26:52 -080078 if (err) {
Chris Phoenix5985ded2017-01-23 13:59:13 -080079 ALOGE ("hw_get_module %s failed: %d", MEMTRACK_HARDWARE_MODULE_ID, err);
Connor O'Brien56dfb752017-01-20 18:26:52 -080080 return nullptr;
81 }
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070082
Connor O'Brien56dfb752017-01-20 18:26:52 -080083 if (!hw_module->methods || !hw_module->methods->open) {
84 memtrack_module = reinterpret_cast<const memtrack_module_t*>(hw_module);
85 } else {
Chris Phoenix5985ded2017-01-23 13:59:13 -080086 err = hw_module->methods->open(hw_module, MEMTRACK_HARDWARE_MODULE_ID,
Connor O'Brien56dfb752017-01-20 18:26:52 -080087 reinterpret_cast<hw_device_t**>(const_cast<memtrack_module_t**>(&memtrack_module)));
88 if (err) {
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070089 ALOGE("Passthrough failed to load legacy HAL.");
Connor O'Brien56dfb752017-01-20 18:26:52 -080090 return nullptr;
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070091 }
92 }
Connor O'Brien56dfb752017-01-20 18:26:52 -080093 return new Memtrack(memtrack_module);
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070094}
95
96} // namespace implementation
97} // namespace V1_0
98} // namespace memtrack
99} // namespace hardware
100} // namespace android