| Yiwei Zhang | 9feb4ae | 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 { | 
 | 60 |         mGpuMem = std::make_unique<GpuMem>(); | 
 | 61 |         mTestableGpuMem = TestableGpuMem(mGpuMem.get()); | 
 | 62 |         mTestMap = bpf::BpfMap<uint64_t, uint64_t>(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, | 
 | 63 |                                                    BPF_F_NO_PREALLOC); | 
 | 64 |  | 
 | 65 |         EXPECT_LE(0, mTestMap.getMap().get()); | 
 | 66 |         EXPECT_TRUE(mTestMap.isValid()); | 
 | 67 |     } | 
 | 68 |  | 
 | 69 |     std::string dumpsys() { | 
 | 70 |         std::string result; | 
 | 71 |         Vector<String16> args; | 
 | 72 |         mGpuMem->dump(args, &result); | 
 | 73 |         return result; | 
 | 74 |     } | 
 | 75 |  | 
 | 76 |     std::unique_ptr<GpuMem> mGpuMem; | 
 | 77 |     TestableGpuMem mTestableGpuMem; | 
 | 78 |     bpf::BpfMap<uint64_t, uint64_t> mTestMap; | 
 | 79 | }; | 
 | 80 |  | 
 | 81 | TEST_F(GpuMemTest, validGpuMemTotalBpfPaths) { | 
 | 82 |     EXPECT_EQ(mTestableGpuMem.getGpuMemTraceGroup(), "gpu_mem"); | 
 | 83 |     EXPECT_EQ(mTestableGpuMem.getGpuMemTotalTracepoint(), "gpu_mem_total"); | 
 | 84 |     EXPECT_EQ(mTestableGpuMem.getGpuMemTotalProgPath(), | 
 | 85 |               "/sys/fs/bpf/prog_gpu_mem_tracepoint_gpu_mem_gpu_mem_total"); | 
 | 86 |     EXPECT_EQ(mTestableGpuMem.getGpuMemTotalMapPath(), "/sys/fs/bpf/map_gpu_mem_gpu_mem_total_map"); | 
 | 87 | } | 
 | 88 |  | 
 | 89 | TEST_F(GpuMemTest, bpfInitializationFailed) { | 
 | 90 |     EXPECT_EQ(dumpsys(), "Failed to initialize GPU memory eBPF\n"); | 
 | 91 | } | 
 | 92 |  | 
 | 93 | TEST_F(GpuMemTest, gpuMemTotalMapEmpty) { | 
 | 94 |     mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
 | 95 |  | 
 | 96 |     EXPECT_EQ(dumpsys(), "GPU memory total usage map is empty\n"); | 
 | 97 | } | 
 | 98 |  | 
 | 99 | TEST_F(GpuMemTest, globalMemTotal) { | 
 | 100 |     ASSERT_RESULT_OK(mTestMap.writeValue(TEST_GLOBAL_KEY, TEST_GLOBAL_VAL, BPF_ANY)); | 
 | 101 |     mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
 | 102 |  | 
 | 103 |     EXPECT_THAT(dumpsys(), HasSubstr(StringPrintf("Global total: %" PRIu64 "\n", TEST_GLOBAL_VAL))); | 
 | 104 | } | 
 | 105 |  | 
 | 106 | TEST_F(GpuMemTest, missingGlobalMemTotal) { | 
 | 107 |     ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY)); | 
 | 108 |     mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
 | 109 |  | 
 | 110 |     EXPECT_THAT(dumpsys(), HasSubstr("Global total: N/A")); | 
 | 111 | } | 
 | 112 |  | 
 | 113 | TEST_F(GpuMemTest, procMemTotal) { | 
 | 114 |     ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_1, TEST_PROC_VAL_1, BPF_ANY)); | 
 | 115 |     ASSERT_RESULT_OK(mTestMap.writeValue(TEST_PROC_KEY_2, TEST_PROC_VAL_2, BPF_ANY)); | 
 | 116 |     mTestableGpuMem.setGpuMemTotalMap(mTestMap); | 
 | 117 |  | 
 | 118 |     EXPECT_THAT(dumpsys(), | 
 | 119 |                 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n", | 
 | 120 |                                        (uint32_t)(TEST_PROC_KEY_1 >> 32)))); | 
 | 121 |     EXPECT_THAT(dumpsys(), | 
 | 122 |                 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_1, | 
 | 123 |                                        TEST_PROC_VAL_1))); | 
 | 124 |     EXPECT_THAT(dumpsys(), | 
 | 125 |                 HasSubstr(StringPrintf("Memory snapshot for GPU %u:\n", | 
 | 126 |                                        (uint32_t)(TEST_PROC_KEY_2 >> 32)))); | 
 | 127 |     EXPECT_THAT(dumpsys(), | 
 | 128 |                 HasSubstr(StringPrintf("Proc %u total: %" PRIu64 "\n", (uint32_t)TEST_PROC_KEY_2, | 
 | 129 |                                        TEST_PROC_VAL_2))); | 
 | 130 | } | 
 | 131 |  | 
 | 132 | } // namespace | 
 | 133 | } // namespace android |