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