blob: b93227f1128f67194fc3fdbb2105c31dd129df45 [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"
18#include <hardware/hardware.h>
19#include <hardware/memtrack.h>
Yifan Hongf9d30342016-11-30 13:45:34 -080020#include <android/log.h>
Ruchi Kandoi97b2f252016-09-29 13:55:10 -070021
22#include "Memtrack.h"
23namespace android {
24namespace hardware {
25namespace memtrack {
26namespace V1_0 {
27namespace implementation {
28
29Memtrack::Memtrack(memtrack_module_t *module) : mModule(module) {
30 if (mModule)
31 mModule->init(mModule);
32}
33
34Memtrack::~Memtrack() {
35 delete(mModule);
36}
37
38Return<void> Memtrack::getMemory(int32_t pid, MemtrackType type,
39 getMemory_cb _hidl_cb) {
40 hidl_vec<MemtrackRecord> records;
41 size_t temp = 0;
42 size_t *size = &temp;
43 int ret = 0;
44
45 if (mModule->getMemory == nullptr)
46 {
47 _hidl_cb(MemtrackStatus::SUCCESS, records);
48 return Void();
49 }
50 ret = mModule->getMemory(mModule, pid, static_cast<memtrack_type>(type),
51 NULL, size);
52 if (ret == 0)
53 {
54 memtrack_record *legacy_records = new memtrack_record[*size];
55 ret = mModule->getMemory(mModule, pid,
56 static_cast<memtrack_type>(type), legacy_records, size);
57 if (ret == 0)
58 {
59 records.resize(*size);
60 for(size_t i = 0; i < *size; i++)
61 {
62 records[i].sizeInBytes = legacy_records[i].size_in_bytes;
63 records[i].flags = legacy_records[i].flags;
64 }
65 }
66 delete[] legacy_records;
67 }
68 _hidl_cb(MemtrackStatus::SUCCESS, records);
69 return Void();
70}
71
72
73IMemtrack* HIDL_FETCH_IMemtrack(const char* name) {
74 int ret = 0;
75 const hw_module_t* hw_module = NULL;
76 memtrack_module_t *memtrack_module = NULL;
77
78 ret = hw_get_module(name, &hw_module);
79 if (ret == 0 && hw_module->methods->open > 0)
80 {
81 ret = hw_module->methods->open(hw_module, name,
82 reinterpret_cast<hw_device_t**>(&memtrack_module));
83 if (ret == 0)
84 return new Memtrack(memtrack_module);
85 else {
86 ALOGE("Passthrough failed to load legacy HAL.");
87 }
88 }
89 else {
90 ALOGE ("hw_get_module %s failed: %d", name, ret);
91 }
92 return nullptr;
93}
94
95} // namespace implementation
96} // namespace V1_0
97} // namespace memtrack
98} // namespace hardware
99} // namespace android