| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright 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 |  | 
|  | 17 | #undef LOG_TAG | 
|  | 18 | #define LOG_TAG "gpuservice_unittest" | 
|  | 19 |  | 
|  | 20 | #include <android-base/stringprintf.h> | 
|  | 21 | #include <bpf/BpfMap.h> | 
|  | 22 | #include <gmock/gmock.h> | 
|  | 23 | #include <gpumem/GpuMem.h> | 
|  | 24 | #include <gtest/gtest.h> | 
|  | 25 | #include <inttypes.h> | 
|  | 26 | #include <utils/String16.h> | 
|  | 27 | #include <utils/Vector.h> | 
|  | 28 |  | 
|  | 29 | #include "TestableGpuMem.h" | 
|  | 30 |  | 
|  | 31 | namespace android { | 
|  | 32 | namespace { | 
|  | 33 |  | 
|  | 34 | using base::StringPrintf; | 
|  | 35 | using testing::HasSubstr; | 
|  | 36 |  | 
|  | 37 | constexpr uint32_t TEST_MAP_SIZE = 10; | 
|  | 38 | constexpr uint64_t TEST_GLOBAL_KEY = 0; | 
|  | 39 | constexpr uint64_t TEST_GLOBAL_VAL = 123; | 
|  | 40 | constexpr uint64_t TEST_PROC_KEY_1 = 1; | 
|  | 41 | constexpr uint64_t TEST_PROC_VAL_1 = 234; | 
|  | 42 | constexpr uint64_t TEST_PROC_KEY_2 = 4294967298; // (1 << 32) + 2 | 
|  | 43 | constexpr uint64_t TEST_PROC_VAL_2 = 345; | 
|  | 44 |  | 
|  | 45 | class GpuMemTest : public testing::Test { | 
|  | 46 | public: | 
|  | 47 | GpuMemTest() { | 
|  | 48 | const ::testing::TestInfo* const test_info = | 
|  | 49 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 50 | ALOGD("**** Setting up for %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | ~GpuMemTest() { | 
|  | 54 | const ::testing::TestInfo* const test_info = | 
|  | 55 | ::testing::UnitTest::GetInstance()->current_test_info(); | 
|  | 56 | ALOGD("**** Tearing down after %s.%s\n", test_info->test_case_name(), test_info->name()); | 
|  | 57 | } | 
|  | 58 |  | 
|  | 59 | void SetUp() override { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 60 | SKIP_IF_BPF_NOT_SUPPORTED; | 
|  | 61 | ASSERT_EQ(0, bpf::setrlimitForTest()); | 
|  | 62 |  | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 63 | mGpuMem = std::make_unique<GpuMem>(); | 
|  | 64 | mTestableGpuMem = TestableGpuMem(mGpuMem.get()); | 
| Yiwei Zhang | a265b40 | 2020-06-25 22:02:29 -0700 | [diff] [blame] | 65 | mTestableGpuMem.setInitialized(); | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 66 | errno = 0; | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 67 | mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, | 
|  | 68 | BPF_F_NO_PREALLOC); | 
|  | 69 |  | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 70 | EXPECT_EQ(0, errno); | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 71 | EXPECT_LE(0, mTestMap.getMap().get()); | 
|  | 72 | EXPECT_TRUE(mTestMap.isValid()); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | std::string dumpsys() { | 
|  | 76 | std::string result; | 
|  | 77 | Vector<String16> args; | 
|  | 78 | mGpuMem->dump(args, &result); | 
|  | 79 | return result; | 
|  | 80 | } | 
|  | 81 |  | 
|  | 82 | std::unique_ptr<GpuMem> mGpuMem; | 
|  | 83 | TestableGpuMem mTestableGpuMem; | 
|  | 84 | bpf::BpfMap<uint64_t, uint64_t> mTestMap; | 
|  | 85 | }; | 
|  | 86 |  | 
|  | 87 | TEST_F(GpuMemTest, validGpuMemTotalBpfPaths) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 88 | SKIP_IF_BPF_NOT_SUPPORTED; | 
|  | 89 |  | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 90 | EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem"); | 
|  | 91 | EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total"); | 
|  | 92 | EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(), | 
|  | 93 | "/sys/fs/bpf/prog_gpu_mem_tracepoint_gpu_mem_gpu_mem_total"); | 
|  | 94 | EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map"); | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | TEST_F(GpuMemTest, bpfInitializationFailed) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 98 | SKIP_IF_BPF_NOT_SUPPORTED; | 
|  | 99 |  | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 100 | EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n"); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | TEST_F(GpuMemTest, gpuMemTotalMapEmpty) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 104 | SKIP_IF_BPF_NOT_SUPPORTED; | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 105 | mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
|  | 106 |  | 
|  | 107 | EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n"); | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | TEST_F(GpuMemTest, globalMemTotal) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 111 | SKIP_IF_BPF_NOT_SUPPORTED; | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 112 | ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY)); | 
|  | 113 | mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
|  | 114 |  | 
|  | 115 | EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL))); | 
|  | 116 | } | 
|  | 117 |  | 
|  | 118 | TEST_F(GpuMemTest, missingGlobalMemTotal) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 119 | SKIP_IF_BPF_NOT_SUPPORTED; | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 120 | ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY)); | 
|  | 121 | mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
|  | 122 |  | 
|  | 123 | EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A")); | 
|  | 124 | } | 
|  | 125 |  | 
|  | 126 | TEST_F(GpuMemTest, procMemTotal) { | 
| Yiwei Zhang | 2083e3e | 2020-06-12 15:30:54 -0700 | [diff] [blame] | 127 | SKIP_IF_BPF_NOT_SUPPORTED; | 
| Yiwei Zhang | c5feac2 | 2020-04-30 10:48:35 -0700 | [diff] [blame] | 128 | ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY)); | 
|  | 129 | ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY)); | 
|  | 130 | mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
|  | 131 |  | 
|  | 132 | EXPECT_THAT(dumpsys(), | 
|  | 133 | HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n", | 
|  | 134 | (uint32_t)(TEST_PROC_KEY_1 >> 32)))); | 
|  | 135 | EXPECT_THAT(dumpsys(), | 
|  | 136 | HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1, | 
|  | 137 | TEST_PROC_VAL_1))); | 
|  | 138 | EXPECT_THAT(dumpsys(), | 
|  | 139 | HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n", | 
|  | 140 | (uint32_t)(TEST_PROC_KEY_2 >> 32)))); | 
|  | 141 | EXPECT_THAT(dumpsys(), | 
|  | 142 | HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2, | 
|  | 143 | TEST_PROC_VAL_2))); | 
|  | 144 | } | 
|  | 145 |  | 
|  | 146 | } // namespace | 
|  | 147 | } // namespace android |