1 /* 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "mem/code_allocator.h" 17 #include "mem/pool_manager.h" 18 #include "mem/base_mem_stats.h" 19 20 #include "gtest/gtest.h" 21 22 namespace panda { 23 24 class BaseMemStatsTest : public testing::Test { 25 protected: SetUp()26 void SetUp() override 27 { 28 panda::mem::MemConfig::Initialize(128_MB, 64_MB, 64_MB, 32_MB); 29 PoolManager::Initialize(); 30 } 31 TearDown()32 void TearDown() override 33 { 34 PoolManager::Finalize(); 35 panda::mem::MemConfig::Finalize(); 36 } 37 }; 38 39 HWTEST_F(BaseMemStatsTest, CodeStatistic, testing::ext::TestSize.Level0) 40 { 41 static constexpr size_t N = 100; 42 BaseMemStats stats; 43 size_t sum = 0; 44 for (size_t i = 1; i < N; i++) { 45 sum += i; 46 stats.RecordAllocateRaw(i, SpaceType::SPACE_TYPE_CODE); 47 } 48 stats.RecordFreeRaw(N, SpaceType::SPACE_TYPE_CODE); 49 ASSERT_EQ(sum, stats.GetAllocated(SpaceType::SPACE_TYPE_CODE)); 50 ASSERT_EQ(N, stats.GetFreed(SpaceType::SPACE_TYPE_CODE)); 51 ASSERT_EQ(sum - N, stats.GetFootprint(SpaceType::SPACE_TYPE_CODE)); 52 } 53 54 HWTEST_F(BaseMemStatsTest, AllocationsOverAllocator, testing::ext::TestSize.Level0) 55 { 56 BaseMemStats stats; 57 void *tmp; 58 CodeAllocator ca(&stats); 59 60 uint8_t buff1[] = {0xCC}; 61 uint8_t buff2[] = {0xCC, 0xCC, 0xCC}; 62 size_t size1 = sizeof(buff1); 63 size_t size2 = sizeof(buff2); 64 65 tmp = ca.AllocateCode(size1, static_cast<void *>(&buff1[0])); 66 tmp = ca.AllocateCode(sizeof(buff2), static_cast<void *>(&buff2[0])); 67 68 ASSERT_EQ(size1 + size2, stats.GetAllocated(SpaceType::SPACE_TYPE_CODE)); 69 ASSERT_EQ(0U, stats.GetFreed(SpaceType::SPACE_TYPE_CODE)); 70 ASSERT_EQ(size1 + size2, stats.GetFootprint(SpaceType::SPACE_TYPE_CODE)); 71 72 stats.RecordFreeRaw(size2, SpaceType::SPACE_TYPE_CODE); 73 74 ASSERT_EQ(sizeof(buff1) + sizeof(buff2), stats.GetAllocated(SpaceType::SPACE_TYPE_CODE)); 75 ASSERT_EQ(sizeof(buff2), stats.GetFreed(SpaceType::SPACE_TYPE_CODE)); 76 ASSERT_EQ(sizeof(buff1), stats.GetFootprint(SpaceType::SPACE_TYPE_CODE)); 77 } 78 79 HWTEST_F(BaseMemStatsTest, HeapAllocateTest, testing::ext::TestSize.Level0) 80 { 81 BaseMemStats stats; 82 stats.RecordAllocateRaw(1U, SpaceType::SPACE_TYPE_CODE); 83 ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_CODE), 1U); 84 85 stats.RecordAllocateRaw(2U, SpaceType::SPACE_TYPE_INTERNAL); 86 ASSERT_EQ(stats.GetAllocated(SpaceType::SPACE_TYPE_INTERNAL), 2U); 87 88 stats.RecordFreeRaw(4U, SpaceType::SPACE_TYPE_CODE); 89 ASSERT_EQ(stats.GetFreed(SpaceType::SPACE_TYPE_CODE), 4U); 90 91 stats.RecordFreeRaw(5U, SpaceType::SPACE_TYPE_INTERNAL); 92 ASSERT_EQ(stats.GetFreed(SpaceType::SPACE_TYPE_INTERNAL), 5U); 93 94 ASSERT_EQ(0U, stats.GetAllocatedHeap()); 95 ASSERT_EQ(0U, stats.GetFreedHeap()); 96 } 97 98 } // namespace panda 99