Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | #include <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #include <aidl/android/hardware/memtrack/DeviceInfo.h> |
| 20 | #include <aidl/android/hardware/memtrack/IMemtrack.h> |
| 21 | #include <aidl/android/hardware/memtrack/MemtrackType.h> |
| 22 | #include <android/binder_manager.h> |
| 23 | #include <android/binder_process.h> |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 24 | #include <vintf/VintfObject.h> |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 25 | |
| 26 | using aidl::android::hardware::memtrack::DeviceInfo; |
| 27 | using aidl::android::hardware::memtrack::IMemtrack; |
| 28 | using aidl::android::hardware::memtrack::MemtrackRecord; |
| 29 | using aidl::android::hardware::memtrack::MemtrackType; |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 30 | using android::vintf::KernelVersion; |
| 31 | using android::vintf::RuntimeInfo; |
| 32 | using android::vintf::VintfObject; |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 33 | |
| 34 | class MemtrackAidlTest : public testing::TestWithParam<std::string> { |
| 35 | public: |
| 36 | virtual void SetUp() override { |
| 37 | const auto instance = GetParam(); |
| 38 | ASSERT_TRUE(AServiceManager_isDeclared(instance.c_str())); |
| 39 | auto memtrackBinder = ndk::SpAIBinder(AServiceManager_waitForService(instance.c_str())); |
| 40 | memtrack_ = IMemtrack::fromBinder(memtrackBinder); |
| 41 | ASSERT_NE(memtrack_, nullptr); |
| 42 | } |
| 43 | |
| 44 | std::shared_ptr<IMemtrack> memtrack_; |
| 45 | }; |
| 46 | |
| 47 | TEST_P(MemtrackAidlTest, GetMemoryInvalidPid) { |
| 48 | int pid = -1; |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 49 | |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 50 | for (MemtrackType type : ndk::enum_range<MemtrackType>()) { |
| 51 | std::vector<MemtrackRecord> records; |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 52 | |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 53 | auto status = memtrack_->getMemory(pid, type, &records); |
| 54 | |
| 55 | EXPECT_EQ(status.getExceptionCode(), EX_ILLEGAL_ARGUMENT); |
| 56 | } |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | TEST_P(MemtrackAidlTest, GetMemoryInvalidType) { |
| 60 | int pid = 1; |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 61 | MemtrackType type = static_cast<MemtrackType>(-1); |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 62 | std::vector<MemtrackRecord> records; |
| 63 | |
| 64 | auto status = memtrack_->getMemory(pid, type, &records); |
| 65 | |
| 66 | EXPECT_EQ(status.getExceptionCode(), EX_UNSUPPORTED_OPERATION); |
| 67 | } |
| 68 | |
| 69 | TEST_P(MemtrackAidlTest, GetMemory) { |
| 70 | int pid = 1; |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 71 | for (MemtrackType type : ndk::enum_range<MemtrackType>()) { |
| 72 | std::vector<MemtrackRecord> records; |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 73 | |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 74 | auto status = memtrack_->getMemory(pid, type, &records); |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 75 | |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 76 | EXPECT_TRUE(status.isOk()); |
| 77 | } |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | TEST_P(MemtrackAidlTest, GetGpuDeviceInfo) { |
| 81 | std::vector<DeviceInfo> device_info; |
| 82 | |
| 83 | auto status = memtrack_->getGpuDeviceInfo(&device_info); |
| 84 | |
Kalesh Singh | a259c59 | 2021-02-02 15:43:58 -0500 | [diff] [blame] | 85 | // Devices with < 5.4 kernels aren't required to provide an implementation of |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 86 | // getGpuDeviceInfo(), and can return EX_UNSUPPORTED_OPERATION |
| 87 | if (status.getExceptionCode() == EX_UNSUPPORTED_OPERATION) { |
Kalesh Singh | a259c59 | 2021-02-02 15:43:58 -0500 | [diff] [blame] | 88 | KernelVersion min_kernel_version = KernelVersion(5, 4, 0); |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 89 | KernelVersion kernel_version = VintfObject::GetInstance() |
| 90 | ->getRuntimeInfo(RuntimeInfo::FetchFlag::CPU_VERSION) |
| 91 | ->kernelVersion(); |
| 92 | EXPECT_LT(kernel_version, min_kernel_version) |
Kalesh Singh | 1bcbf85 | 2021-02-12 11:29:50 -0500 | [diff] [blame] | 93 | << "Devices with 5.4 or later kernels must implement getGpuDeviceInfo()"; |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 94 | return; |
| 95 | } |
| 96 | |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 97 | EXPECT_TRUE(status.isOk()); |
Kalesh Singh | 11fdc97 | 2021-01-22 16:59:15 -0500 | [diff] [blame] | 98 | EXPECT_FALSE(device_info.empty()); |
| 99 | for (auto device : device_info) { |
| 100 | EXPECT_FALSE(device.name.empty()); |
| 101 | } |
Kalesh Singh | a851f3d | 2020-12-01 11:47:01 -0500 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(MemtrackAidlTest); |
| 105 | INSTANTIATE_TEST_SUITE_P(PerInstance, MemtrackAidlTest, |
| 106 | testing::ValuesIn(android::getAidlHalInstanceNames(IMemtrack::descriptor)), |
| 107 | android::PrintInstanceNameToString); |
| 108 | |
| 109 | int main(int argc, char** argv) { |
| 110 | ::testing::InitGoogleTest(&argc, argv); |
| 111 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 112 | ABinderProcess_startThreadPool(); |
| 113 | return RUN_ALL_TESTS(); |
| 114 | } |