From b1b3d8d9f9282c56cef1bb58626770d96e7392b0 Mon Sep 17 00:00:00 2001 From: sannikov Date: Thu, 2 Mar 2023 00:42:12 +0300 Subject: [PATCH] [MM] fix clang-tidy issues in tests Signed-off-by: sannikov --- libpandabase/tests/base_mem_stats_test.cpp | 15 ++- libpandabase/tests/code_allocator_test.cpp | 21 ++- libpandabase/tests/hash_test.cpp | 45 +++++-- libpandabase/tests/pool_map_test.cpp | 31 +++-- runtime/tests/allocator_test_base.h | 7 +- runtime/tests/bump_allocator_test.cpp | 125 ++++++++++-------- runtime/tests/card_table_test.cpp | 101 ++++++++------ runtime/tests/collection_set_test.cpp | 29 ++-- runtime/tests/crossing_map_test.cpp | 18 ++- runtime/tests/freelist_allocator_test.cpp | 54 +++++--- runtime/tests/g1gc_fullgc_test.cpp | 65 +++++---- runtime/tests/g1gc_test.cpp | 34 ++--- runtime/tests/gc_log_test.cpp | 26 ++-- runtime/tests/gc_trigger_test.cpp | 6 +- runtime/tests/heap_space_test.cpp | 38 +++--- .../tests/humongous_obj_allocator_test.cpp | 20 ++- runtime/tests/internal_allocator_test.cpp | 44 ++++-- runtime/tests/malloc-proxy-allocator-test.cpp | 18 ++- runtime/tests/mark_word_test.cpp | 11 +- .../tests/mem_stats_additional_info_test.cpp | 15 ++- runtime/tests/mem_stats_gc_test.cpp | 19 +-- runtime/tests/mem_stats_gen_gc_test.cpp | 57 ++++---- runtime/tests/panda_smart_pointers_test.cpp | 11 +- runtime/tests/rem_set_test.cpp | 20 ++- runtime/tests/runslots_allocator_test.cpp | 35 ++--- runtime/tests/static_analyzer_test.cpp | 22 ++- runtime/tests/tlab_test.cpp | 30 ++++- 27 files changed, 563 insertions(+), 354 deletions(-) diff --git a/libpandabase/tests/base_mem_stats_test.cpp b/libpandabase/tests/base_mem_stats_test.cpp index 4ba9436a3..8b242d0ea 100644 --- a/libpandabase/tests/base_mem_stats_test.cpp +++ b/libpandabase/tests/base_mem_stats_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include "mem/code_allocator.h" #include "mem/pool_manager.h" #include "mem/base_mem_stats.h" @@ -25,7 +26,12 @@ class BaseMemStatsTest : public testing::Test { protected: void SetUp() override { - panda::mem::MemConfig::Initialize(128_MB, 64_MB, 64_MB, 32_MB, 0); + constexpr uint64_t OBJECT_POOL_SIZE = 128_MB; + constexpr uint64_t INTERNAL_SIZE = 64_MB; + constexpr uint64_t COMPILER_SIZE = 64_MB; + constexpr uint64_t CODE_SIZE = 32_MB; + constexpr uint64_t FRAMES_SIZE = 0; + panda::mem::MemConfig::Initialize(OBJECT_POOL_SIZE, INTERNAL_SIZE, COMPILER_SIZE, CODE_SIZE, FRAMES_SIZE); PoolManager::Initialize(); } @@ -57,13 +63,16 @@ TEST_F(BaseMemStatsTest, AllocationsOverAllocator) void *tmp; CodeAllocator ca(&stats); - uint8_t buff1[] = {0xCC}; - uint8_t buff2[] = {0xCC, 0xCC, 0xCC}; + static constexpr uint8_t ARR_FILLER = 0xCC; + std::array buff1 = {ARR_FILLER}; + std::array buff2 = {ARR_FILLER, ARR_FILLER, ARR_FILLER}; size_t size1 = sizeof(buff1); size_t size2 = sizeof(buff2); tmp = ca.AllocateCode(size1, static_cast(&buff1[0])); + ASSERT_NE(tmp, nullptr); tmp = ca.AllocateCode(sizeof(buff2), static_cast(&buff2[0])); + ASSERT_NE(tmp, nullptr); ASSERT_EQ(size1 + size2, stats.GetAllocated(SpaceType::SPACE_TYPE_CODE)); ASSERT_EQ(0, stats.GetFreed(SpaceType::SPACE_TYPE_CODE)); diff --git a/libpandabase/tests/code_allocator_test.cpp b/libpandabase/tests/code_allocator_test.cpp index d19481b80..3924b10c4 100644 --- a/libpandabase/tests/code_allocator_test.cpp +++ b/libpandabase/tests/code_allocator_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include "mem/code_allocator.h" #include "mem/pool_manager.h" #include "mem/base_mem_stats.h" @@ -24,9 +25,12 @@ namespace panda { class CodeAllocatorTest : public testing::Test { public: - CodeAllocatorTest() {} + CodeAllocatorTest() = default; - ~CodeAllocatorTest() + NO_COPY_SEMANTIC(CodeAllocatorTest); + NO_MOVE_SEMANTIC(CodeAllocatorTest); + + ~CodeAllocatorTest() override { Logger::Destroy(); } @@ -42,7 +46,12 @@ protected: void SetUp() override { - panda::mem::MemConfig::Initialize(0, 32_MB, 0, 32_MB, 0); + constexpr uint64_t OBJECT_POOL_SIZE = 0; + constexpr uint64_t INTERNAL_SIZE = 32_MB; + constexpr uint64_t COMPILER_SIZE = 0; + constexpr uint64_t CODE_SIZE = 32_MB; + constexpr uint64_t FRAMES_SIZE = 0; + panda::mem::MemConfig::Initialize(OBJECT_POOL_SIZE, INTERNAL_SIZE, COMPILER_SIZE, CODE_SIZE, FRAMES_SIZE); PoolManager::Initialize(); } @@ -57,11 +66,13 @@ TEST_F(CodeAllocatorTest, AllocateBuffTest) { BaseMemStats stats; CodeAllocator ca(&stats); - uint8_t buff[] = {0xCC, 0xCC}; + static constexpr uint8_t ARR_FILLER = 0xCC; + std::array buff = {ARR_FILLER, ARR_FILLER}; void *code_buff; code_buff = ca.AllocateCode(sizeof(buff), static_cast(&buff[0])); + Span code_buffer(static_cast(code_buff), sizeof(buff)); for (size_t i = 0; i < sizeof(buff); i++) { - ASSERT_EQ(static_cast(code_buff)[i], 0xCC); + ASSERT_EQ(code_buffer[i], ARR_FILLER); } ASSERT_TRUE(IsAligned(code_buff, 4 * SIZE_1K)); } diff --git a/libpandabase/tests/hash_test.cpp b/libpandabase/tests/hash_test.cpp index 0dce8e40d..e569d3c4b 100644 --- a/libpandabase/tests/hash_test.cpp +++ b/libpandabase/tests/hash_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include "utils/hash.h" @@ -25,16 +26,23 @@ namespace panda { class HashTest : public testing::Test { +private: + static constexpr unsigned FIXED_SEED = 0xDEADBEEF; + public: HashTest() { #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::time(NULL); #else - seed_ = 0xDEADBEEF; + seed_ = FIXED_SEED; #endif } - ~HashTest() {} + + NO_COPY_SEMANTIC(HashTest); + NO_MOVE_SEMANTIC(HashTest); + + ~HashTest() override = default; protected: template @@ -49,20 +57,29 @@ protected: static constexpr size_t KEY32INBYTES = 4; static constexpr size_t KEY8INBYTES = 1; + inline void InitRand() const + { + srand(seed_); + } + inline int GetRand() const + { + return rand(); // NOLINT(cert-msc50-cpp) + } + // Some platforms have this macro so do not redefine it. #ifndef PAGE_SIZE static constexpr size_t PAGE_SIZE = SIZE_1K * 4; #endif - +private: unsigned seed_; }; template void HashTest::OneObject32bitsHashTest() { - srand(seed_); + InitRand(); - uint32_t object32 = rand(); + uint32_t object32 = GetRand(); uint32_t first_hash = T::GetHash32(reinterpret_cast(&object32), KEY32INBYTES); uint32_t second_hash = T::GetHash32(reinterpret_cast(&object32), KEY32INBYTES); if (first_hash != second_hash) { @@ -70,7 +87,7 @@ void HashTest::OneObject32bitsHashTest() } ASSERT_EQ(first_hash, second_hash); - uint8_t object8 = rand(); + uint8_t object8 = GetRand(); first_hash = T::GetHash32(reinterpret_cast(&object8), KEY8INBYTES); second_hash = T::GetHash32(reinterpret_cast(&object8), KEY8INBYTES); if (first_hash != second_hash) { @@ -79,7 +96,7 @@ void HashTest::OneObject32bitsHashTest() ASSERT_EQ(first_hash, second_hash); // Set up 64 bits value and use only 40 bits from it - uint64_t object40 = rand(); + uint64_t object40 = GetRand(); first_hash = T::GetHash32(reinterpret_cast(&object40), KEY40INBYTES); second_hash = T::GetHash32(reinterpret_cast(&object40), KEY40INBYTES); if (first_hash != second_hash) { @@ -91,12 +108,13 @@ void HashTest::OneObject32bitsHashTest() template void HashTest::OneStringHashTest() { + // NOLINTNEXTLINE(modernize-avoid-c-arrays) char string[] = "Over 1000!\0"; // Dummy check. Don't ask me why... if (sizeof(char) != sizeof(uint8_t)) { return; } - uint8_t *mutf8_string = reinterpret_cast(string); + auto *mutf8_string = reinterpret_cast(string); uint32_t first_hash = T::GetHash32String(mutf8_string); uint32_t second_hash = T::GetHash32String(mutf8_string); ASSERT_EQ(first_hash, second_hash); @@ -105,9 +123,10 @@ void HashTest::OneStringHashTest() template void HashTest::StringMemHashTest() { + // NOLINTNEXTLINE(modernize-avoid-c-arrays) char string[] = "COULD YOU CREATE MORE COMPLEX TESTS,OK?\0"; size_t string_size = strlen(string); - uint8_t *mutf8_string = reinterpret_cast(string); + auto *mutf8_string = reinterpret_cast(string); uint32_t second_hash = T::GetHash32(mutf8_string, string_size); uint32_t first_hash = T::GetHash32String(mutf8_string); ASSERT_EQ(first_hash, second_hash); @@ -124,10 +143,10 @@ void HashTest::EndOfPageStringHashTest() panda::os::mem::MakeMemProtected(reinterpret_cast(reinterpret_cast(mem) + PAGE_SIZE), PAGE_SIZE); char *string = reinterpret_cast((reinterpret_cast(mem) + PAGE_SIZE) - sizeof(char) * string_size); - string[0] = 'O'; - string[1] = 'K'; - string[IMM_TWO] = '\0'; - uint8_t *mutf8_string = reinterpret_cast(string); + string[0] = 'O'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + string[1] = 'K'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + string[IMM_TWO] = '\0'; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + auto *mutf8_string = reinterpret_cast(string); uint32_t second_hash = T::GetHash32(mutf8_string, string_size - 1); uint32_t first_hash = T::GetHash32String(mutf8_string); ASSERT_EQ(first_hash, second_hash); diff --git a/libpandabase/tests/pool_map_test.cpp b/libpandabase/tests/pool_map_test.cpp index 5a288bb9f..6cbc64731 100644 --- a/libpandabase/tests/pool_map_test.cpp +++ b/libpandabase/tests/pool_map_test.cpp @@ -24,18 +24,24 @@ namespace panda { class PoolMapTest : public testing::Test { +private: + static constexpr unsigned FIXED_SEED = 0xDEADBEEF; + public: PoolMapTest() { #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::time(NULL); #else - seed_ = 0xDEADBEEF; + seed_ = FIXED_SEED; #endif - srand(seed_); + InitRand(); } - ~PoolMapTest() + NO_COPY_SEMANTIC(PoolMapTest); + NO_MOVE_SEMANTIC(PoolMapTest); + + ~PoolMapTest() override { ResetPoolMap(); } @@ -75,19 +81,19 @@ protected: SpaceType GetRandSpaceType() const { - int rand_index = rand() % ALL_SPACE_TYPES.size(); + int rand_index = GetRand() % ALL_SPACE_TYPES.size(); return ALL_SPACE_TYPES[rand_index]; } AllocatorType GetRandAllocatorType() const { - int rand_index = rand() % ALL_ALLOCATOR_TYPES.size(); + int rand_index = GetRand() % ALL_ALLOCATOR_TYPES.size(); return ALL_ALLOCATOR_TYPES[rand_index]; } size_t RandHeapAddr() const { - return AlignUp(rand() % PANDA_MAX_HEAP_SIZE, DEFAULT_ALIGNMENT_IN_BYTES); + return AlignUp(GetRand() % PANDA_MAX_HEAP_SIZE, DEFAULT_ALIGNMENT_IN_BYTES); } void CheckRandomPoolAddress(Pool pool, SpaceType space_type, AllocatorType allocator_type, uintptr_t allocator_addr) @@ -98,10 +104,19 @@ protected: ASSERT_EQ(ToUintPtr(GetAllocatorInfoForAddr(pool_addr).GetAllocatorHeaderAddr()), allocator_addr); } + inline void InitRand() const + { + srand(seed_); + } + inline int GetRand() const + { + return rand(); // NOLINT(cert-msc50-cpp) + } + private: void *RandAddrFromPool(Pool pool) const { - return ToVoidPtr(ToUintPtr(pool.GetMem()) + rand() % pool.GetSize()); + return ToVoidPtr(ToUintPtr(pool.GetMem()) + GetRand() % pool.GetSize()); } AllocatorInfo GetAllocatorInfoForAddr(const void *addr) const @@ -169,7 +184,7 @@ TEST_F(PoolMapTest, AddRemoveDifferentPoolsTest) static constexpr size_t ITERATIONS = 200; static constexpr uintptr_t POOL_START_ADDR = PANDA_POOL_ALIGNMENT_IN_BYTES; for (size_t i = 0; i < ITERATIONS; i++) { - size_t pool_size = AlignUp(rand() % MAX_POOL_SIZE, PANDA_POOL_ALIGNMENT_IN_BYTES); + size_t pool_size = AlignUp(GetRand() % MAX_POOL_SIZE, PANDA_POOL_ALIGNMENT_IN_BYTES); SpaceType space = GetRandSpaceType(); AllocatorType allocator = GetRandAllocatorType(); Pool pool(pool_size, ToVoidPtr(POOL_START_ADDR)); diff --git a/runtime/tests/allocator_test_base.h b/runtime/tests/allocator_test_base.h index 9a3fe900e..270b0f737 100644 --- a/runtime/tests/allocator_test_base.h +++ b/runtime/tests/allocator_test_base.h @@ -469,9 +469,8 @@ private: } }; -// NOLINTNEXTLINE(fuchsia-statically-constructed-objects) -template -std::unordered_set AllocatorTest::objects_set_; +template // NOLINT(fuchsia-statically-constructed-objects) +std::unordered_set AllocatorTest::objects_set_; // NOLINT(fuchsia-statically-constructed-objects) template template @@ -967,7 +966,7 @@ inline void AllocatorTest::AsanTest(size_t free_granularity, size_t p for (size_t i = 0; i < pools_count; i++) { AddMemoryPoolToAllocatorProtected(allocator); } - std::array allocated_elements; + std::array allocated_elements {}; // Allocations for (size_t i = 0; i < ALLOCATIONS_COUNT; ++i) { void *mem = allocator.Alloc(ALLOC_SIZE); diff --git a/runtime/tests/bump_allocator_test.cpp b/runtime/tests/bump_allocator_test.cpp index 6c73d0895..c17d9529a 100644 --- a/runtime/tests/bump_allocator_test.cpp +++ b/runtime/tests/bump_allocator_test.cpp @@ -26,6 +26,9 @@ using NonObjectBumpAllocator = BumpPointerAllocator; class BumpAllocatorTest : public testing::Test { +private: + static constexpr unsigned FIXED_SEED = 0x0BADDEAD; + public: BumpAllocatorTest() { @@ -33,14 +36,17 @@ public: #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::time(NULL); #else - seed_ = 0x0BADDEAD; + seed_ = FIXED_SEED; #endif srand(seed_); panda::mem::MemConfig::Initialize(0, 8_MB, 0, 0, 0); PoolManager::Initialize(); } - ~BumpAllocatorTest() + NO_COPY_SEMANTIC(BumpAllocatorTest); + NO_MOVE_SEMANTIC(BumpAllocatorTest); + + ~BumpAllocatorTest() override { for (auto i : allocated_mem_mmap_) { panda::os::mem::UnmapRaw(std::get<0>(i), std::get<1>(i)); @@ -53,6 +59,11 @@ public: // Logger::Destroy(); } + constexpr inline unsigned GetSeed() noexcept + { + return seed_; + } + protected: Arena *AllocateArena(size_t size) { @@ -65,6 +76,12 @@ protected: return arena; } + inline int GetRand() const + { + return rand(); // NOLINT(cert-msc50-cpp) + } + +private: std::vector> allocated_mem_mmap_; std::vector allocated_arenas_; unsigned seed_; @@ -81,35 +98,35 @@ TEST_F(BumpAllocatorTest, AlignedAlloc) mem::MemStatsType mem_stats; NonObjectBumpAllocator bp_allocator(pool, SpaceType::SPACE_TYPE_INTERNAL, &mem_stats); Alignment align = DEFAULT_ALIGNMENT; - std::array arr; + std::array arr {}; size_t mask = GetAlignmentInBytes(align) - 1; // Allocations - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < ARRAY_SIZE; ++i) { arr[i] = static_cast(bp_allocator.Alloc(sizeof(int), align)); - *arr[i] = rand() % std::numeric_limits::max(); + *arr[i] = GetRand() % std::numeric_limits::max(); } // Allocations checking - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < ARRAY_SIZE; ++i) { - ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align << ", seed:" << seed_; + ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align << ", seed:" << GetSeed(); ASSERT_EQ(reinterpret_cast(arr[i]) & mask, static_cast(0)) - << "value of i: " << i << ", align: " << align << ", seed:" << seed_; - ASSERT_EQ(*arr[i], rand() % std::numeric_limits::max()) - << "value of i: " << i << ", align: " << align << ", seed:" << seed_; + << "value of i: " << i << ", align: " << align << ", seed:" << GetSeed(); + ASSERT_EQ(*arr[i], GetRand() % std::numeric_limits::max()) + << "value of i: " << i << ", align: " << align << ", seed:" << GetSeed(); } static_assert(LOG_ALIGN_MAX != DEFAULT_ALIGNMENT, "We expect minimal alignment != DEFAULT_ALIGNMENT"); void *ptr; #ifndef NDEBUG EXPECT_DEATH_IF_SUPPORTED(ptr = bp_allocator.Alloc(sizeof(int), LOG_ALIGN_MAX), "alignment == DEFAULT_ALIGNMENT") - << ", seed:" << seed_; + << ", seed:" << GetSeed(); #endif ptr = bp_allocator.Alloc(SIZE_1M); ASSERT_EQ(ptr, nullptr) << "Here Alloc with allocation size = 1 MB should return nullptr" - << ", seed:" << seed_; + << ", seed:" << GetSeed(); } TEST_F(BumpAllocatorTest, CreateTLABAndAlloc) @@ -124,30 +141,30 @@ TEST_F(BumpAllocatorTest, CreateTLABAndAlloc) size_t mask = DEFAULT_ALIGNMENT_IN_BYTES - 1; - std::array tlab_elements; - std::array common_elements; + std::array tlab_elements {}; + std::array common_elements {}; auto pool = PoolManager::GetMmapMemPool()->AllocPool(TLAB_SIZE + COMMON_BUFFER_SIZE, SpaceType::SPACE_TYPE_INTERNAL, AllocatorType::BUMP_ALLOCATOR); mem::MemStatsType mem_stats; NonObjectBumpAllocator allocator(pool, SpaceType::SPACE_TYPE_OBJECT, &mem_stats, 1); { // Allocations in common buffer - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < COMMON_ALLOC_COUNT_SIZE; ++i) { common_elements[i] = static_cast(allocator.Alloc(sizeof(AllocType))); - ASSERT_TRUE(common_elements[i] != nullptr) << ", seed:" << seed_; - *common_elements[i] = rand() % std::numeric_limits::max(); + ASSERT_TRUE(common_elements[i] != nullptr) << ", seed:" << GetSeed(); + *common_elements[i] = GetRand() % std::numeric_limits::max(); } TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab != nullptr) << ", seed:" << seed_; - ASSERT_TRUE(allocator.CreateNewTLAB(TLAB_SIZE) == nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab != nullptr) << ", seed:" << GetSeed(); + ASSERT_TRUE(allocator.CreateNewTLAB(TLAB_SIZE) == nullptr) << ", seed:" << GetSeed(); // Allocations in TLAB - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < TLAB_ALLOC_COUNT_SIZE; ++i) { tlab_elements[i] = static_cast(tlab->Alloc(sizeof(AllocType))); - ASSERT_TRUE(tlab_elements[i] != nullptr) << ", seed:" << seed_; - *tlab_elements[i] = rand() % std::numeric_limits::max(); + ASSERT_TRUE(tlab_elements[i] != nullptr) << ", seed:" << GetSeed(); + *tlab_elements[i] = GetRand() % std::numeric_limits::max(); } // Check that we don't have memory in the buffer: @@ -155,44 +172,44 @@ TEST_F(BumpAllocatorTest, CreateTLABAndAlloc) ASSERT_TRUE(tlab->Alloc(sizeof(AllocType)) == nullptr); // Allocations checking in common buffer - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < COMMON_ALLOC_COUNT_SIZE; ++i) { - ASSERT_NE(common_elements[i], nullptr) << "value of i: " << i << ", seed:" << seed_; + ASSERT_NE(common_elements[i], nullptr) << "value of i: " << i << ", seed:" << GetSeed(); ASSERT_EQ(reinterpret_cast(common_elements[i]) & mask, static_cast(0)) - << "value of i: " << i << ", seed:" << seed_; - ASSERT_EQ(*common_elements[i], rand() % std::numeric_limits::max()) - << "value of i: " << i << ", seed:" << seed_; + << "value of i: " << i << ", seed:" << GetSeed(); + ASSERT_EQ(*common_elements[i], GetRand() % std::numeric_limits::max()) + << "value of i: " << i << ", seed:" << GetSeed(); } // Allocations checking in TLAB - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < TLAB_ALLOC_COUNT_SIZE; ++i) { - ASSERT_NE(tlab_elements[i], nullptr) << "value of i: " << i << ", seed:" << seed_; + ASSERT_NE(tlab_elements[i], nullptr) << "value of i: " << i << ", seed:" << GetSeed(); ASSERT_EQ(reinterpret_cast(tlab_elements[i]) & mask, static_cast(0)) - << "value of i: " << i << ", seed:" << seed_; - ASSERT_EQ(*tlab_elements[i], rand() % std::numeric_limits::max()) - << "value of i: " << i << ", seed:" << seed_; + << "value of i: " << i << ", seed:" << GetSeed(); + ASSERT_EQ(*tlab_elements[i], GetRand() % std::numeric_limits::max()) + << "value of i: " << i << ", seed:" << GetSeed(); } } allocator.Reset(); { TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab != nullptr) << ", seed:" << seed_; - ASSERT_TRUE(allocator.CreateNewTLAB(TLAB_SIZE) == nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab != nullptr) << ", seed:" << GetSeed(); + ASSERT_TRUE(allocator.CreateNewTLAB(TLAB_SIZE) == nullptr) << ", seed:" << GetSeed(); // Allocations in TLAB - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < TLAB_ALLOC_COUNT_SIZE; ++i) { tlab_elements[i] = static_cast(tlab->Alloc(sizeof(AllocType))); - ASSERT_TRUE(tlab_elements[i] != nullptr) << ", seed:" << seed_; - *tlab_elements[i] = rand() % std::numeric_limits::max(); + ASSERT_TRUE(tlab_elements[i] != nullptr) << ", seed:" << GetSeed(); + *tlab_elements[i] = GetRand() % std::numeric_limits::max(); } // Allocations in common buffer - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < COMMON_ALLOC_COUNT_SIZE; ++i) { common_elements[i] = static_cast(allocator.Alloc(sizeof(AllocType))); - ASSERT_TRUE(common_elements[i] != nullptr) << ", seed:" << seed_; - *common_elements[i] = rand() % std::numeric_limits::max(); + ASSERT_TRUE(common_elements[i] != nullptr) << ", seed:" << GetSeed(); + *common_elements[i] = GetRand() % std::numeric_limits::max(); } // Check that we don't have memory in the buffer: @@ -200,23 +217,23 @@ TEST_F(BumpAllocatorTest, CreateTLABAndAlloc) ASSERT_TRUE(tlab->Alloc(sizeof(AllocType)) == nullptr); // Allocations checking in TLAB - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < TLAB_ALLOC_COUNT_SIZE; ++i) { - ASSERT_NE(tlab_elements[i], nullptr) << "value of i: " << i << ", seed:" << seed_; + ASSERT_NE(tlab_elements[i], nullptr) << "value of i: " << i << ", seed:" << GetSeed(); ASSERT_EQ(reinterpret_cast(tlab_elements[i]) & mask, static_cast(0)) - << "value of i: " << i << ", seed:" << seed_; - ASSERT_EQ(*tlab_elements[i], rand() % std::numeric_limits::max()) - << "value of i: " << i << ", seed:" << seed_; + << "value of i: " << i << ", seed:" << GetSeed(); + ASSERT_EQ(*tlab_elements[i], GetRand() % std::numeric_limits::max()) + << "value of i: " << i << ", seed:" << GetSeed(); } // Allocations checking in common buffer - srand(seed_); + srand(GetSeed()); for (size_t i = 0; i < COMMON_ALLOC_COUNT_SIZE; ++i) { - ASSERT_NE(common_elements[i], nullptr) << "value of i: " << i << ", seed:" << seed_; + ASSERT_NE(common_elements[i], nullptr) << "value of i: " << i << ", seed:" << GetSeed(); ASSERT_EQ(reinterpret_cast(common_elements[i]) & mask, static_cast(0)) - << "value of i: " << i << ", seed:" << seed_; - ASSERT_EQ(*common_elements[i], rand() % std::numeric_limits::max()) - << "value of i: " << i << ", seed:" << seed_; + << "value of i: " << i << ", seed:" << GetSeed(); + ASSERT_EQ(*common_elements[i], GetRand() % std::numeric_limits::max()) + << "value of i: " << i << ", seed:" << GetSeed(); } } } @@ -232,19 +249,19 @@ TEST_F(BumpAllocatorTest, CreateTooManyTLABS) { for (size_t i = 0; i < TLAB_COUNT - 1; i++) { TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab != nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab != nullptr) << ", seed:" << GetSeed(); } TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab == nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab == nullptr) << ", seed:" << GetSeed(); } allocator.Reset(); { for (size_t i = 0; i < TLAB_COUNT - 1; i++) { TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab != nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab != nullptr) << ", seed:" << GetSeed(); } TLAB *tlab = allocator.CreateNewTLAB(TLAB_SIZE); - ASSERT_TRUE(tlab == nullptr) << ", seed:" << seed_; + ASSERT_TRUE(tlab == nullptr) << ", seed:" << GetSeed(); } } diff --git a/runtime/tests/card_table_test.cpp b/runtime/tests/card_table_test.cpp index fe062494e..86fd18963 100644 --- a/runtime/tests/card_table_test.cpp +++ b/runtime/tests/card_table_test.cpp @@ -30,24 +30,34 @@ namespace panda::mem::test { class CardTableTest : public testing::Test { -protected: - // static constexpr size_t kHeapSize = 0xffffffff; - static constexpr size_t K_ALLOC_COUNT = 1000; - // static constexpr size_t maxCardIndex = kHeapSize / ::panda::mem::CardTable::GetCardSize(); +private: + static constexpr unsigned FIXED_SEED = 123456; + static constexpr uint64_t HEAP_SIZE_LIMIT = 64_MB; + unsigned int seed_; std::mt19937 gen_; std::uniform_int_distribution addr_dis_; std::uniform_int_distribution card_index_dis_; mem::MemStatsType *mem_stats_; + InternalAllocatorPtr internal_allocator_; + panda::MTManagedThread *thread_; + std::unique_ptr card_table_; - CardTableTest() +protected: + // static constexpr size_t kHeapSize = 0xffffffff; + static constexpr size_t K_ALLOC_COUNT = 1000; + // static constexpr size_t maxCardIndex = kHeapSize / ::panda::mem::CardTable::GetCardSize(); + + CardTableTest() // NOLINT(cert-msc51-cpp) { #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::time(NULL); #else - seed_ = 123456; + seed_ = FIXED_SEED; #endif + gen_.seed(seed_); + mem_stats_ = nullptr; RuntimeOptions options; - options.SetHeapSizeLimit(64_MB); + options.SetHeapSizeLimit(HEAP_SIZE_LIMIT); options.SetShouldLoadBootPandaFiles(false); options.SetShouldInitializeIntrinsics(false); options.SetGcType("epsilon"); @@ -63,7 +73,10 @@ protected: card_table_->Initialize(); } - ~CardTableTest() + NO_COPY_SEMANTIC(CardTableTest); + NO_MOVE_SEMANTIC(CardTableTest); + + ~CardTableTest() override { card_table_.reset(nullptr); thread_->ManagedCodeEnd(); @@ -110,10 +123,10 @@ protected: return PoolManager::GetMmapMemPool()->GetMinObjectAddress() + GetRandomCardIndex() * CardTable::GetCardSize(); } - InternalAllocatorPtr internal_allocator_; - std::unique_ptr card_table_; - unsigned int seed_; - panda::MTManagedThread *thread_; + constexpr std::unique_ptr &GetCardTable() + { + return card_table_; + } }; TEST_F(CardTableTest, MarkTest) @@ -123,13 +136,13 @@ TEST_F(CardTableTest, MarkTest) for (size_t i = 0; i < K_ALLOC_COUNT; i++) { uintptr_t addr; addr = GetRandomAddress(); - if (!card_table_->IsMarked(addr)) { + if (!GetCardTable()->IsMarked(addr)) { ++marked_cnt; - card_table_->MarkCard(addr); + GetCardTable()->MarkCard(addr); } } - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { if (card->IsMarked()) { marked_cnt--; } @@ -142,18 +155,18 @@ TEST_F(CardTableTest, MarkAndClearAllTest) // std::set addrSet; size_t cnt = 0; - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { card->Mark(); cnt++; } - ASSERT_EQ(cnt, card_table_->GetCardsCount()); + ASSERT_EQ(cnt, GetCardTable()->GetCardsCount()); size_t cnt_cleared = 0; - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { card->Clear(); cnt_cleared++; } - ASSERT_EQ(cnt_cleared, card_table_->GetCardsCount()); + ASSERT_EQ(cnt_cleared, GetCardTable()->GetCardsCount()); } TEST_F(CardTableTest, ClearTest) @@ -167,12 +180,12 @@ TEST_F(CardTableTest, ClearTest) if (!addr_set.insert(addr).second) { continue; } - card_table_->MarkCard(addr); + GetCardTable()->MarkCard(addr); } size_t cleared_cnt = 0; // clear all marked and count them - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { if (card->IsMarked()) { card->Clear(); cleared_cnt++; @@ -181,7 +194,7 @@ TEST_F(CardTableTest, ClearTest) ASSERT_EQ(addr_set.size(), cleared_cnt); // check that there are no marked - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { ASSERT_EQ(card->IsMarked(), false); } } @@ -197,36 +210,36 @@ TEST_F(CardTableTest, ClearAllTest) if (!addr_set.insert(addr).second) { continue; } - card_table_->MarkCard(addr); + GetCardTable()->MarkCard(addr); } - card_table_->ClearAll(); - for (auto card : *card_table_) { + GetCardTable()->ClearAll(); + for (auto card : *GetCardTable()) { ASSERT_EQ(card->IsMarked(), false); } } -TEST_F(CardTableTest, double_initialization) +TEST_F(CardTableTest, double_initialization) // NOLINT(google-readability-avoid-underscore-in-googletest-name) { - EXPECT_DEATH(card_table_->Initialize(), ".*"); + EXPECT_DEATH(GetCardTable()->Initialize(), ".*"); } -TEST_F(CardTableTest, corner_cases) +TEST_F(CardTableTest, corner_cases) // NOLINT(google-readability-avoid-underscore-in-googletest-name) { // Mark 1st byte in the heap - ASSERT_EQ((*card_table_->begin())->IsMarked(), false); - card_table_->MarkCard(GetMinAddress()); - ASSERT_EQ((*card_table_->begin())->IsMarked(), true); + ASSERT_EQ((*GetCardTable()->begin())->IsMarked(), false); + GetCardTable()->MarkCard(GetMinAddress()); + ASSERT_EQ((*GetCardTable()->begin())->IsMarked(), true); // Mark last byte in the heap uintptr_t last = GetMinAddress() + GetPoolSize() - 1; - ASSERT_EQ(card_table_->IsMarked(last), false); - card_table_->MarkCard(last); - ASSERT_EQ(card_table_->IsMarked(last), true); + ASSERT_EQ(GetCardTable()->IsMarked(last), false); + GetCardTable()->MarkCard(last); + ASSERT_EQ(GetCardTable()->IsMarked(last), true); // Mark last byte of second card - uintptr_t second_last = GetMinAddress() + 2 * card_table_->GetCardSize() - 1; - ASSERT_EQ(card_table_->IsMarked(second_last), false); - card_table_->MarkCard(second_last); - ASSERT_EQ(((*card_table_->begin()) + 1)->IsMarked(), true); + uintptr_t second_last = GetMinAddress() + 2 * GetCardTable()->GetCardSize() - 1; + ASSERT_EQ(GetCardTable()->IsMarked(second_last), false); + GetCardTable()->MarkCard(second_last); + ASSERT_EQ(((*GetCardTable()->begin()) + 1)->IsMarked(), true); } TEST_F(CardTableTest, VisitMarked) @@ -236,21 +249,21 @@ TEST_F(CardTableTest, VisitMarked) while (marked_cnt < K_ALLOC_COUNT) { uintptr_t addr; addr = GetRandomAddress(); - if (!card_table_->IsMarked(addr)) { + if (!GetCardTable()->IsMarked(addr)) { ++marked_cnt; - card_table_->MarkCard(addr); + GetCardTable()->MarkCard(addr); } } PandaVector mem_ranges; - card_table_->VisitMarked([&mem_ranges](MemRange mem_range) { mem_ranges.emplace_back(mem_range); }, - CardTableProcessedFlag::VISIT_MARKED); + GetCardTable()->VisitMarked([&mem_ranges](MemRange mem_range) { mem_ranges.emplace_back(mem_range); }, + CardTableProcessedFlag::VISIT_MARKED); // Got ranges one by one PandaVector expected_ranges; - for (auto card : *card_table_) { + for (auto card : *GetCardTable()) { if (card->IsMarked()) { - expected_ranges.emplace_back(card_table_->GetMemoryRange(card)); + expected_ranges.emplace_back(GetCardTable()->GetMemoryRange(card)); } } diff --git a/runtime/tests/collection_set_test.cpp b/runtime/tests/collection_set_test.cpp index 0638faeb8..7f451b961 100644 --- a/runtime/tests/collection_set_test.cpp +++ b/runtime/tests/collection_set_test.cpp @@ -36,7 +36,10 @@ public: mem::InternalAllocator<>::InitInternalAllocatorFromRuntime(static_cast(allocator_)); } - ~CollectionSetTest() + NO_COPY_SEMANTIC(CollectionSetTest); + NO_MOVE_SEMANTIC(CollectionSetTest); + + ~CollectionSetTest() override { delete allocator_; PoolManager::Finalize(); @@ -45,6 +48,10 @@ public: InternalAllocator<>::ClearInternalAllocatorFromRuntime(); } + static constexpr uintptr_t NUM_0X1000 = 0x1000; + static constexpr uintptr_t NUM_0X2000 = 0x2000; + static constexpr uintptr_t NUM_0X3000 = 0x3000; + private: MemStatsType *mem_stats_; InternalAllocatorT *allocator_; @@ -52,7 +59,7 @@ private: TEST_F(CollectionSetTest, TestCtor) { - Region young_region(nullptr, 0x0, 0x1000); + Region young_region(nullptr, 0x0, NUM_0X1000); young_region.AddFlag(RegionFlag::IS_EDEN); PandaVector young_regions = {&young_region}; @@ -70,10 +77,10 @@ TEST_F(CollectionSetTest, TestCtor) TEST_F(CollectionSetTest, TestAddTenuredRegion) { - Region young_region(nullptr, 0x0, 0x1000); + Region young_region(nullptr, 0x0, NUM_0X1000); young_region.AddFlag(RegionFlag::IS_EDEN); PandaVector young_regions = {&young_region}; - Region tenured_region(nullptr, 0x1000, 0x2000); + Region tenured_region(nullptr, NUM_0X1000, NUM_0X2000); tenured_region.AddFlag(RegionFlag::IS_OLD); CollectionSet cs(std::move(young_regions)); @@ -92,10 +99,10 @@ TEST_F(CollectionSetTest, TestAddTenuredRegion) TEST_F(CollectionSetTest, TestAddHumongousRegion) { - Region young_region(nullptr, 0x0, 0x1000); + Region young_region(nullptr, 0x0, NUM_0X1000); young_region.AddFlag(RegionFlag::IS_EDEN); PandaVector young_regions = {&young_region}; - Region humongous_region(nullptr, 0x1000, 0x2000); + Region humongous_region(nullptr, NUM_0X1000, NUM_0X2000); humongous_region.AddFlag(RegionFlag::IS_OLD); humongous_region.AddFlag(RegionFlag::IS_LARGE_OBJECT); @@ -115,17 +122,17 @@ TEST_F(CollectionSetTest, TestAddHumongousRegion) TEST_F(CollectionSetTest, TestAddDifferentRegions) { - Region young_region(nullptr, 0x0, 0x1000); + Region young_region(nullptr, 0x0, NUM_0X1000); young_region.AddFlag(RegionFlag::IS_EDEN); PandaVector young_regions = {&young_region}; - Region tenured1_region(nullptr, 0x1000, 0x2000); + Region tenured1_region(nullptr, NUM_0X1000, NUM_0X2000); tenured1_region.AddFlag(RegionFlag::IS_OLD); - Region tenured2_region(nullptr, 0x1000, 0x2000); + Region tenured2_region(nullptr, NUM_0X1000, NUM_0X2000); tenured2_region.AddFlag(RegionFlag::IS_OLD); - Region humongous1_region(nullptr, 0x2000, 0x3000); + Region humongous1_region(nullptr, NUM_0X2000, NUM_0X3000); humongous1_region.AddFlag(RegionFlag::IS_OLD); humongous1_region.AddFlag(RegionFlag::IS_LARGE_OBJECT); - Region humongous2_region(nullptr, 0x2000, 0x3000); + Region humongous2_region(nullptr, NUM_0X2000, NUM_0X3000); humongous2_region.AddFlag(RegionFlag::IS_OLD); humongous2_region.AddFlag(RegionFlag::IS_LARGE_OBJECT); diff --git a/runtime/tests/crossing_map_test.cpp b/runtime/tests/crossing_map_test.cpp index 3c6395755..a8430ba3e 100644 --- a/runtime/tests/crossing_map_test.cpp +++ b/runtime/tests/crossing_map_test.cpp @@ -22,6 +22,9 @@ namespace panda::mem { class CrossingMapTest : public testing::Test { +private: + static constexpr unsigned FIXED_SEED = 0xDEADBEEF; + public: CrossingMapTest() { @@ -29,7 +32,7 @@ public: #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::time(NULL); #else - seed_ = 0xDEADBEEF; + seed_ = FIXED_SEED; #endif srand(seed_); panda::mem::MemConfig::Initialize(MEMORY_POOL_SIZE, MEMORY_POOL_SIZE, 0, 0, 0); @@ -42,7 +45,10 @@ public: crossing_map_->InitializeCrossingMapForMemory(ToVoidPtr(start_addr_), GetPoolSize()); } - ~CrossingMapTest() + NO_COPY_SEMANTIC(CrossingMapTest); + NO_MOVE_SEMANTIC(CrossingMapTest); + + ~CrossingMapTest() override { crossing_map_->RemoveCrossingMapForMemory(ToVoidPtr(start_addr_), GetPoolSize()); crossing_map_->Destroy(); @@ -63,7 +69,7 @@ protected: void *GetRandomObjAddr(size_t size) { ASSERT(size < GetPoolSize()); - uintptr_t rand_offset = rand() % (GetPoolSize() - size); + uintptr_t rand_offset = rand() % (GetPoolSize() - size); // NOLINT(cert-msc50-cpp) rand_offset = (rand_offset >> CrossingMap::CROSSING_MAP_OBJ_ALIGNMENT) << CrossingMap::CROSSING_MAP_OBJ_ALIGNMENT; return ToVoidPtr(start_addr_ + rand_offset); @@ -89,7 +95,7 @@ protected: return crossing_map_->GetMapNumFromAddr(addr); } - static constexpr size_t MIN_GAP_BETWEEN_OBJECTS = 1 << CrossingMap::CROSSING_MAP_OBJ_ALIGNMENT; + static constexpr size_t MIN_GAP_BETWEEN_OBJECTS = 1U << CrossingMap::CROSSING_MAP_OBJ_ALIGNMENT; static constexpr size_t MEMORY_POOL_SIZE = 64_MB; @@ -160,7 +166,7 @@ TEST_F(CrossingMapTest, BigSmallObjTest) TEST_F(CrossingMapTest, HugeObjTest) { - static constexpr size_t OBJ_SIZE = MEMORY_POOL_SIZE >> 1; + static constexpr size_t OBJ_SIZE = MEMORY_POOL_SIZE >> 1U; void *obj_addr = GetRandomObjAddr(OBJ_SIZE); GetCrossingMap()->AddObject(obj_addr, OBJ_SIZE); ASSERT_TRUE(GetCrossingMap()->FindFirstObject(obj_addr, obj_addr) == obj_addr) << " seed = " << GetSeed(); @@ -308,7 +314,7 @@ TEST_F(CrossingMapTest, InitializeCrosingMapForMemoryTest) GetRandomObjAddr((POOLS_SIZE * 2 + PANDA_POOL_ALIGNMENT_IN_BYTES) * POOL_COUNT + PANDA_POOL_ALIGNMENT_IN_BYTES); uintptr_t aligned_start_addr = AlignUp(ToUintPtr(start_addr), PANDA_POOL_ALIGNMENT_IN_BYTES); - std::array deleted_pools; + std::array deleted_pools {}; for (size_t i = 0; i < POOL_COUNT; i++) { void *pool_addr = ToVoidPtr(aligned_start_addr + i * (POOLS_SIZE * 2 + PANDA_POOL_ALIGNMENT_IN_BYTES)); GetCrossingMap()->InitializeCrossingMapForMemory(pool_addr, POOLS_SIZE * 2); diff --git a/runtime/tests/freelist_allocator_test.cpp b/runtime/tests/freelist_allocator_test.cpp index e7978d47b..8fb4a4adb 100644 --- a/runtime/tests/freelist_allocator_test.cpp +++ b/runtime/tests/freelist_allocator_test.cpp @@ -46,7 +46,10 @@ public: } } - ~FreeListAllocatorTest() + NO_COPY_SEMANTIC(FreeListAllocatorTest); + NO_MOVE_SEMANTIC(FreeListAllocatorTest); + + ~FreeListAllocatorTest() override { thread_->ManagedCodeEnd(); ClearPoolManager(); @@ -58,7 +61,6 @@ public: } protected: - panda::MTManagedThread *thread_; static constexpr size_t DEFAULT_POOL_SIZE_FOR_ALLOC = NonObjectFreeListAllocator::GetMinPoolSize(); static constexpr size_t DEFAULT_POOL_ALIGNMENT_FOR_ALLOC = FREELIST_DEFAULT_ALIGNMENT; static constexpr size_t POOL_HEADER_SIZE = sizeof(NonObjectFreeListAllocator::MemoryPoolHeader); @@ -102,6 +104,8 @@ protected: allocated_pools_by_pool_manager_.clear(); } +private: + panda::MTManagedThread *thread_; std::vector allocated_pools_by_pool_manager_; RuntimeOptions options_; bool crossingmap_manual_handling_ {false}; @@ -112,10 +116,12 @@ protected: TEST_F(FreeListAllocatorTest, SimpleAllocateDifferentObjSizeTest) { LOG(DEBUG, ALLOC) << "SimpleAllocateDifferentObjSizeTest"; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectFreeListAllocator allocator(mem_stats); AddMemoryPoolToAllocator(allocator); - for (size_t i = 23; i < 300; i++) { + constexpr size_t MIN_SIZE = 23; + constexpr size_t MAX_SIZE = 300; + for (size_t i = MIN_SIZE; i < MAX_SIZE; i++) { void *mem = allocator.Alloc(i); LOG(DEBUG, ALLOC) << "Allocate obj with size " << i << " at " << std::hex << mem; } @@ -124,7 +130,8 @@ TEST_F(FreeListAllocatorTest, SimpleAllocateDifferentObjSizeTest) TEST_F(FreeListAllocatorTest, AllocateWriteFreeTest) { - AllocateAndFree(FREELIST_ALLOCATOR_MIN_SIZE, 512); + static constexpr size_t ELEMENTS_COUNT = 512; + AllocateAndFree(FREELIST_ALLOCATOR_MIN_SIZE, ELEMENTS_COUNT); } TEST_F(FreeListAllocatorTest, AllocateRandomFreeTest) @@ -143,6 +150,7 @@ TEST_F(FreeListAllocatorTest, AllocateTooBigObjTest) TEST_F(FreeListAllocatorTest, AlignmentAllocTest) { static constexpr size_t POOLS_COUNT = 2; + // NOLINTNEXTLINE(readability-magic-numbers) AlignedAllocFreeTest(POOLS_COUNT); } @@ -187,7 +195,7 @@ TEST_F(FreeListAllocatorTest, AllocatedByFreeListAllocatorTest) TEST_F(FreeListAllocatorTest, FailedLinksTest) { static constexpr size_t MIN_ALLOC_SIZE = FREELIST_ALLOCATOR_MIN_SIZE; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectFreeListAllocator allocator(mem_stats); AddMemoryPoolToAllocator(allocator); std::pair pair; @@ -249,10 +257,10 @@ TEST_F(FreeListAllocatorTest, MaxAllocationSizeTest) { static constexpr size_t ALLOC_SIZE = MAX_ALLOC_SIZE; static constexpr size_t ALLOC_COUNT = 2; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectFreeListAllocator allocator(mem_stats); AddMemoryPoolToAllocator(allocator); - std::array memory_elements; + std::array memory_elements {}; for (size_t i = 0; i < ALLOC_COUNT; i++) { void *mem = allocator.Alloc(ALLOC_SIZE); ASSERT_TRUE(mem != nullptr); @@ -272,16 +280,16 @@ TEST_F(FreeListAllocatorTest, AllocateTheWholePoolFreeAndAllocateAgainTest) } else { min_size_power_of_two = ceil(std::log(FREELIST_ALLOCATOR_MIN_SIZE) / std::log(2)); } - if (((1 << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader)) < FREELIST_ALLOCATOR_MIN_SIZE) { + if (((1U << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader)) < FREELIST_ALLOCATOR_MIN_SIZE) { min_size_power_of_two++; } - size_t alloc_size = (1 << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader); + size_t alloc_size = (1U << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader); // To cover all memory we need to consider pool header size at first bytes of pool memory. - size_t first_alloc_size = (1 << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader) - POOL_HEADER_SIZE; + size_t first_alloc_size = (1U << min_size_power_of_two) - sizeof(freelist::MemoryBlockHeader) - POOL_HEADER_SIZE; if (first_alloc_size < FREELIST_ALLOCATOR_MIN_SIZE) { - first_alloc_size = (1 << (min_size_power_of_two + 1)) - sizeof(freelist::MemoryBlockHeader) - POOL_HEADER_SIZE; + first_alloc_size = (1U << (min_size_power_of_two + 1)) - sizeof(freelist::MemoryBlockHeader) - POOL_HEADER_SIZE; } - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectFreeListAllocator allocator(mem_stats); AddMemoryPoolToAllocator(allocator); std::vector memory_elements; @@ -415,20 +423,24 @@ TEST_F(FreeListAllocatorTest, AlignTest) NonObjectFreeListAllocator allocator(mem_stats.get()); AddMemoryPoolToAllocator(allocator); - auto *ptr1 = allocator.Alloc(1678U); - auto *ptr2 = allocator.Alloc(1678U); - auto *ptr3 = allocator.Alloc(1678U); - auto *ptr4 = allocator.Alloc(1678U); + static constexpr size_t ALLOC_SIZE_1 = 1678U; + auto *ptr1 = allocator.Alloc(ALLOC_SIZE_1); + auto *ptr2 = allocator.Alloc(ALLOC_SIZE_1); + auto *ptr3 = allocator.Alloc(ALLOC_SIZE_1); + auto *ptr4 = allocator.Alloc(ALLOC_SIZE_1); allocator.Free(ptr3); allocator.Free(ptr1); - auto *ptr5 = allocator.Alloc(1536U, GetLogAlignment(128U)); - ASSERT_FALSE(IsAligned(reinterpret_cast(ptr3), 128U)); - ASSERT_TRUE(IsAligned(reinterpret_cast(ptr5), 128U)); + static constexpr size_t ALLOC_SIZE_2 = 1536U; + static constexpr uint32_t ALIGNMENT_IN_BYTES = 128U; + auto *ptr5 = allocator.Alloc(ALLOC_SIZE_2, GetLogAlignment(ALIGNMENT_IN_BYTES)); + ASSERT_FALSE(IsAligned(reinterpret_cast(ptr3), ALIGNMENT_IN_BYTES)); + ASSERT_TRUE(IsAligned(reinterpret_cast(ptr5), ALIGNMENT_IN_BYTES)); allocator.Free(ptr5); - auto *ptr6 = allocator.Alloc(1272U); + static constexpr size_t ALLOC_SIZE_3 = 1272U; + auto *ptr6 = allocator.Alloc(ALLOC_SIZE_3); // allocations 5 and 6 should use the same memory block. But after free padding bits // should be cleared so pointers will be not equal. ASSERT_TRUE(ptr5 != ptr6); diff --git a/runtime/tests/g1gc_fullgc_test.cpp b/runtime/tests/g1gc_fullgc_test.cpp index 658dea11c..2064587e8 100644 --- a/runtime/tests/g1gc_fullgc_test.cpp +++ b/runtime/tests/g1gc_fullgc_test.cpp @@ -34,6 +34,7 @@ namespace panda::mem::test { +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) class G1GCFullGCTest : public testing::Test { public: using ObjVec = PandaVector; @@ -42,6 +43,8 @@ public: static constexpr GCTaskCause MIXED_G1_GC_CAUSE = GCTaskCause::YOUNG_GC_CAUSE; static constexpr GCTaskCause FULL_GC_CAUSE = GCTaskCause::EXPLICIT_CAUSE; + static constexpr uint32_t HEAP_SIZE_LIMIT = 33554432; + static constexpr uint32_t YOUNG_SPACE_SIZE = 4194304; enum class TargetSpace { YOUNG, TENURED, LARGE, HUMONG }; @@ -49,7 +52,7 @@ public: public: void GCStarted([[maybe_unused]] const GCTask &task, [[maybe_unused]] size_t heap_size) override { - count++; + count_++; } void GCFinished([[maybe_unused]] const GCTask &task, [[maybe_unused]] size_t heap_size_before_gc, @@ -57,23 +60,25 @@ public: { } - int count = 0; + private: + int count_ = 0; }; template class GCHook : public GCListener { public: - GCHook(F hook_arg) : hook(hook_arg) {}; + explicit GCHook(F hook_arg) : hook_(hook_arg) {}; void GCStarted([[maybe_unused]] const GCTask &task, [[maybe_unused]] size_t heap_size) override {} void GCFinished([[maybe_unused]] const GCTask &task, [[maybe_unused]] size_t heap_size_before_gc, [[maybe_unused]] size_t heap_size) override { - hook(); + hook_(); } - F hook; + private: + F hook_; }; void SetupRuntime(const std::string &gc_type_param) @@ -86,8 +91,8 @@ public: options.SetGcTriggerType("debug"); options.SetGcDebugTriggerStart(std::numeric_limits::max()); options.SetCompilerEnableJit(false); - options.SetHeapSizeLimit(33554432); - options.SetYoungSpaceSize(4194304); + options.SetHeapSizeLimit(HEAP_SIZE_LIMIT); + options.SetYoungSpaceSize(YOUNG_SPACE_SIZE); options.SetExplicitConcurrentGcEnabled(false); [[maybe_unused]] bool success = Runtime::Create(options); ASSERT(success); @@ -114,8 +119,8 @@ public: F space_checker, bool check_oom_in_tenured = false); void InitRoot(); - void MakeObjectsAlive(ObjVec objects, int every = 1); - void MakeObjectsPermAlive(ObjVec objects, int every = 1); + void MakeObjectsAlive(ObjVec const &objects, int every = 1); + void MakeObjectsPermAlive(ObjVec const &objects, int every = 1); void MakeObjectsGarbage(size_t start_idx, size_t after_end_idx, int every = 1); void DumpHandles(); void DumpAliveObjects(); @@ -127,20 +132,22 @@ public: void TearDown() override {} - panda::MTManagedThread *thread; + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + panda::MTManagedThread *thread {nullptr}; GCType gc_type; LanguageContext ctx {nullptr}; - ObjectAllocatorBase *object_allocator; + ObjectAllocatorBase *object_allocator {nullptr}; mem::InternalAllocatorPtr internal_allocator; - PandaVM *vm; - GC *gc; + PandaVM *vm {nullptr}; + GC *gc {nullptr}; std::vector handles; - MemStatsType *ms; - GCStats *gc_ms; - coretypes::Array *root = nullptr; + MemStatsType *ms {nullptr}; + GCStats *gc_ms {nullptr}; + coretypes::Array *root {nullptr}; size_t root_size = 0; - GCCounter *gccnt; + GCCounter *gccnt {nullptr}; + // NOLINTEND(misc-non-private-member-variables-in-classes) }; template @@ -169,9 +176,10 @@ G1GCFullGCTest::ObjVec G1GCFullGCTest::MakeAllocations(size_t min_size, size_t m size_t size = obj_templates[j].length() + sizeof(coretypes::String); if (check_oom_in_tenured) { // Leaving 5MB in tenured seems OK + constexpr size_t SPARE_MEM = 5000000; auto free = reinterpret_cast(object_allocator->GetHeapSpace())->GetCurrentFreeTenuredSize(); - if (size + 5000000 > free) { + if (size + SPARE_MEM > free) { return result; } } @@ -199,7 +207,7 @@ void G1GCFullGCTest::InitRoot() MakeObjectsPermAlive({root}); } -void G1GCFullGCTest::MakeObjectsAlive(ObjVec objects, int every) +void G1GCFullGCTest::MakeObjectsAlive(ObjVec const &objects, int every) { int cnt = every; for (auto *obj : objects) { @@ -228,7 +236,7 @@ void G1GCFullGCTest::MakeObjectsGarbage(size_t start_idx, size_t after_end_idx, } } -void G1GCFullGCTest::MakeObjectsPermAlive(ObjVec objects, int every) +void G1GCFullGCTest::MakeObjectsPermAlive(ObjVec const &objects, int every) { HanVec result; result.reserve(objects.size() / every); @@ -320,7 +328,8 @@ TEST_F(G1GCFullGCTest, TestIntensiveAlloc) { HandleScope scope(thread); PrepareTest(); - [[maybe_unused]] size_t bytes, raw_objects_size; + [[maybe_unused]] size_t bytes; + [[maybe_unused]] size_t raw_objects_size; [[maybe_unused]] size_t young_size = reinterpret_cast( @@ -346,7 +355,7 @@ TEST_F(G1GCFullGCTest, TestIntensiveAlloc) ObjVec ov1 = MakeAllocations(y_obj_size, y_obj_size, 1, &bytes, &raw_objects_size, y_space_check); allocated += bytes; - if (i++ % 100 == 0) { + if (i++ % PERCENT_100_U32 == 0) { gc->WaitForGCInManaged(GCTask(FULL_GC_CAUSE)); } } @@ -362,7 +371,7 @@ TEST_F(G1GCFullGCTest, TestIntensiveAlloc) GCHook gchook([&old_root_size, this, &gc_happened]() { MakeObjectsGarbage(old_root_size, this->root_size); old_root_size = this->root_size; - gc_happened = 1; + gc_happened = true; }); gc->AddListener(&gchook); while (allocated < 4 * heap_size) { @@ -393,7 +402,8 @@ TEST_F(G1GCFullGCTest, TestExplicitFullNearLimit) { HandleScope scope(thread); PrepareTest(); - [[maybe_unused]] size_t bytes, raw_objects_size; + [[maybe_unused]] size_t bytes; + [[maybe_unused]] size_t raw_objects_size; [[maybe_unused]] size_t young_size = reinterpret_cast( @@ -416,7 +426,7 @@ TEST_F(G1GCFullGCTest, TestExplicitFullNearLimit) // do explicit full and allocate the same size once again auto old_root_size = root_size; int i = 0; - while (t_free > 2.2 * young_size) { + while (t_free > 2.2 * young_size) { // NOLINT(readability-magic-numbers) ObjVec ov1 = MakeAllocations(y_obj_size, y_obj_size, 1, &bytes, &raw_objects_size, y_space_check); MakeObjectsAlive(ov1, 1); @@ -453,7 +463,8 @@ TEST_F(G1GCFullGCTest, TestOOMFullNearLimit) { HandleScope scope(thread); PrepareTest(); - [[maybe_unused]] size_t bytes, raw_objects_size; + [[maybe_unused]] size_t bytes; + [[maybe_unused]] size_t raw_objects_size; [[maybe_unused]] size_t young_size = reinterpret_cast( @@ -476,7 +487,7 @@ TEST_F(G1GCFullGCTest, TestOOMFullNearLimit) // then allocate the same size once again checking if we can handle it w/o OOM auto old_root_size = root_size; int i = 0; - while (t_free > 2.2 * young_size) { + while (t_free > 2.2 * young_size) { // NOLINT(readability-magic-numbers) ObjVec ov1 = MakeAllocations(y_obj_size, y_obj_size, 1, &bytes, &raw_objects_size, y_space_check); MakeObjectsAlive(ov1, 1); diff --git a/runtime/tests/g1gc_test.cpp b/runtime/tests/g1gc_test.cpp index b29253c8e..ccc67acb5 100644 --- a/runtime/tests/g1gc_test.cpp +++ b/runtime/tests/g1gc_test.cpp @@ -44,7 +44,10 @@ public: Runtime::Create(options); } - ~G1GCTest() + NO_COPY_SEMANTIC(G1GCTest); + NO_MOVE_SEMANTIC(G1GCTest); + + ~G1GCTest() override { Runtime::Destroy(); } @@ -58,7 +61,7 @@ public: options.SetRunGcInPlace(true); options.SetCompilerEnableJit(false); options.SetGcWorkersCount(0); - options.SetG1PromotionRegionAliveRate(100); + options.SetG1PromotionRegionAliveRate(PERCENT_100_U32); options.SetGcTriggerType("debug-never"); options.SetShouldLoadBootPandaFiles(false); options.SetShouldInitializeIntrinsics(false); @@ -409,7 +412,7 @@ TEST_F(G1GCTest, CheckRemsetToHumongousAfterReclaimHumongousObject) class NewObjectsListener : public GCListener { public: - NewObjectsListener(G1GCTest *test) : test_(test) {} + explicit NewObjectsListener(G1GCTest *test) : test_(test) {} void GCPhaseStarted(GCPhase phase) override { @@ -419,7 +422,7 @@ public: MTManagedThread *thread = MTManagedThread::GetCurrent(); // Allocate quite large object to make allocator to create a separate region - size_t nonmovable_len = 9 * DEFAULT_REGION_SIZE / 10; + size_t nonmovable_len = 9 * DEFAULT_REGION_SIZE / 10; // NOLINT(readability-magic-numbers) ObjectHeader *dummy = test_->AllocArray(nonmovable_len, ClassRoot::ARRAY_U8, true); Region *dummy_region = ObjectToRegion(dummy); EXPECT_TRUE(dummy_region->HasFlag(RegionFlag::IS_NONMOVABLE)); @@ -459,9 +462,9 @@ public: private: G1GCTest *test_; VMHandle nonmovable_; - uintptr_t nonmovable_mark_bitmap_addr_; + uintptr_t nonmovable_mark_bitmap_addr_ {}; VMHandle humongous_; - uintptr_t humongous_mark_bitmap_addr_; + uintptr_t humongous_mark_bitmap_addr_ {}; }; // Test the new objects created during concurrent marking are alive @@ -499,7 +502,7 @@ TEST_F(G1GCTest, TestNewObjectsSATB) class CollectionSetChecker : public GCListener { public: - CollectionSetChecker(ObjectAllocatorG1<> *allocator) : allocator_(allocator) {} + explicit CollectionSetChecker(ObjectAllocatorG1<> *allocator) : allocator_(allocator) {} void SetExpectedRegions(const std::initializer_list &expected_regions) { @@ -614,9 +617,10 @@ TEST_F(G1GCTest, TestMixedCollections) uint32_t garbage_rate = Runtime::GetOptions().GetG1RegionGarbageRateThreshold(); // The object will occupy more than half of region. // So expect the allocator allocates a separate young region for each object. - size_t big_len = garbage_rate * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); - size_t big_len1 = (garbage_rate + 1) * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); - size_t big_len2 = (garbage_rate + 2) * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); + size_t region_size = DEFAULT_REGION_SIZE / PERCENT_100_U32; + size_t big_len = garbage_rate * region_size + sizeof(coretypes::String); + size_t big_len1 = (garbage_rate + 1) * region_size + sizeof(coretypes::String); + size_t big_len2 = (garbage_rate + 2) * region_size + sizeof(coretypes::String); size_t small_len = DEFAULT_REGION_SIZE / 2 + sizeof(coretypes::String); Runtime *runtime = Runtime::GetCurrent(); @@ -773,7 +777,7 @@ public: static RuntimeOptions CreateOptions() { RuntimeOptions options = CreateDefaultOptions(); - options.SetG1PromotionRegionAliveRate(50); + options.SetG1PromotionRegionAliveRate(PROMOTE_RATE); return options; } @@ -810,8 +814,8 @@ TEST_F(G1GCPromotionTest, TestCorrectPromotionYoungRegion) VMHandle first_holder; VMHandle second_holder; VMHandle young; - std::array first_region_object_links; - std::array second_region_object_links; + std::array first_region_object_links {}; + std::array second_region_object_links {}; // Check Promotion for young region: first_holder = VMHandle(thread, AllocArray(HUMONGOUS_STRING_LEN, ClassRoot::ARRAY_STRING, false)); @@ -1108,7 +1112,7 @@ TEST_F(G1GCTest, NonMovableClearingDuringConcurrentPhaseTest) ScopedManagedCodeThread s(thread); [[maybe_unused]] HandleScope scope(thread); - size_t array_length = GetHumongousArrayLength(ClassRoot::ARRAY_STRING) - 50; + size_t array_length = GetHumongousArrayLength(ClassRoot::ARRAY_STRING) - 50; // NOLINT(readability-magic-numbers) coretypes::Array *first_non_movable_obj = nullptr; coretypes::Array *second_non_movable_obj = nullptr; uintptr_t prev_young_addr = 0; @@ -1248,7 +1252,7 @@ TEST_F(G1GCTest, HumongousClearingDuringConcurrentPhaseTest) class G1FullGCTest : public G1GCTest { public: - G1FullGCTest(uint32_t full_gc_region_fragmentation_rate = 0) + explicit G1FullGCTest(uint32_t full_gc_region_fragmentation_rate = 0) : G1GCTest(CreateOptions(full_gc_region_fragmentation_rate)) { } diff --git a/runtime/tests/gc_log_test.cpp b/runtime/tests/gc_log_test.cpp index 7f2c98510..08c224ffa 100644 --- a/runtime/tests/gc_log_test.cpp +++ b/runtime/tests/gc_log_test.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include #include #include #include @@ -36,9 +37,14 @@ namespace panda::mem { class GCTestLog : public testing::TestWithParam { public: - GCTestLog() {} + static constexpr uint32_t HEAP_SIZE_LIMIT = 15_MB; - ~GCTestLog() + GCTestLog() = default; + + NO_COPY_SEMANTIC(GCTestLog); + NO_MOVE_SEMANTIC(GCTestLog); + + ~GCTestLog() override { [[maybe_unused]] bool success = Runtime::Destroy(); ASSERT(success); @@ -46,7 +52,7 @@ public: } void SetupRuntime(const std::string &gc_type, bool small_heap_and_young_spaces = false, - size_t promotion_region_alive_rate = 100) const + size_t promotion_region_alive_rate = PERCENT_100_U32) const { panda::Logger::ComponentMask component_mask; component_mask.set(Logger::Component::GC); @@ -68,7 +74,7 @@ public: options.SetExplicitConcurrentGcEnabled(false); if (small_heap_and_young_spaces) { options.SetYoungSpaceSize(2_MB); - options.SetHeapSizeLimit(15_MB); + options.SetHeapSizeLimit(HEAP_SIZE_LIMIT); } [[maybe_unused]] bool success = Runtime::Create(options); ASSERT(success); @@ -144,8 +150,10 @@ public: static_assert(GCCollectionType::TENURED < GCCollectionType::FULL); protected: + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) std::string expected_log_; std::string log_; + // NOLINTEND(misc-non-private-member-variables-in-classes) private: const size_t number_of_gc_causes_ = 8; @@ -169,9 +177,9 @@ TEST_F(GCTestLog, GenGCYoungCauseFullCollectionLogTest) ObjectAllocator object_allocator; uint32_t garbage_rate = Runtime::GetOptions().GetG1RegionGarbageRateThreshold(); - size_t string_len = garbage_rate * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); + size_t string_len = garbage_rate * DEFAULT_REGION_SIZE / PERCENT_100_U32 + sizeof(coretypes::String); - VMHandle arrays[3]; + std::array, 3> arrays; { arrays[0] = VMHandle(thread, object_allocator.AllocArray(2, ClassRoot::ARRAY_STRING, false)); @@ -192,9 +200,9 @@ TEST_F(GCTestLog, G1GCMixedCollectionLogTest) SetupRuntime("g1-gc"); uint32_t garbage_rate = Runtime::GetOptions().GetG1RegionGarbageRateThreshold(); - size_t big_string_len = garbage_rate * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); - size_t big_string_len1 = (garbage_rate + 1) * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); - size_t big_string_len2 = (garbage_rate + 2) * DEFAULT_REGION_SIZE / 100 + sizeof(coretypes::String); + size_t big_string_len = garbage_rate * DEFAULT_REGION_SIZE / PERCENT_100_U32 + sizeof(coretypes::String); + size_t big_string_len1 = (garbage_rate + 1) * DEFAULT_REGION_SIZE / PERCENT_100_U32 + sizeof(coretypes::String); + size_t big_string_len2 = (garbage_rate + 2) * DEFAULT_REGION_SIZE / PERCENT_100_U32 + sizeof(coretypes::String); size_t small_len = DEFAULT_REGION_SIZE / 2 + sizeof(coretypes::String); Runtime *runtime = Runtime::GetCurrent(); diff --git a/runtime/tests/gc_trigger_test.cpp b/runtime/tests/gc_trigger_test.cpp index 637ef3de5..19b8fe198 100644 --- a/runtime/tests/gc_trigger_test.cpp +++ b/runtime/tests/gc_trigger_test.cpp @@ -35,7 +35,10 @@ public: thread_->ManagedCodeBegin(); } - ~GCTriggerTest() + NO_COPY_SEMANTIC(GCTriggerTest); + NO_MOVE_SEMANTIC(GCTriggerTest); + + ~GCTriggerTest() override { thread_->ManagedCodeEnd(); Runtime::Destroy(); @@ -60,6 +63,7 @@ protected: static constexpr size_t MAX_EXTRA_HEAP_SIZE = 8_MB; static constexpr uint32_t DEFAULT_INCREASE_MULTIPLIER = 3U; +private: MTManagedThread *thread_ = nullptr; }; diff --git a/runtime/tests/heap_space_test.cpp b/runtime/tests/heap_space_test.cpp index f0ee30c6c..414918eec 100644 --- a/runtime/tests/heap_space_test.cpp +++ b/runtime/tests/heap_space_test.cpp @@ -24,14 +24,12 @@ namespace panda::mem::test { class HeapSpaceTest : public testing::Test { public: - explicit HeapSpaceTest() { - // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL); - }; + HeapSpaceTest() = default; - ~HeapSpaceTest() - { - // Logger::Destroy(); - } + NO_COPY_SEMANTIC(HeapSpaceTest); + NO_MOVE_SEMANTIC(HeapSpaceTest); + + ~HeapSpaceTest() override = default; protected: static constexpr size_t DEFAULT_TEST_HEAP_SIZE = 64_MB; @@ -53,9 +51,10 @@ protected: } struct HeapSpaceHolder { - HeapSpaceHolder(size_t initial_heap_size = DEFAULT_TEST_HEAP_SIZE, - size_t max_heap_size = DEFAULT_TEST_HEAP_SIZE, uint32_t min_percentage = 30U, - uint32_t max_percentage = 70U) + explicit HeapSpaceHolder(size_t initial_heap_size = DEFAULT_TEST_HEAP_SIZE, + size_t max_heap_size = DEFAULT_TEST_HEAP_SIZE, + uint32_t min_percentage = 30U, // NOLINT(readability-magic-numbers) + uint32_t max_percentage = 70U) // NOLINT(readability-magic-numbers) { MemConfig::Initialize(max_heap_size, 0_MB, 0_MB, 0_MB, 0_MB, initial_heap_size); PoolManager::Initialize(); @@ -64,6 +63,9 @@ protected: min_percentage, max_percentage); } + NO_COPY_SEMANTIC(HeapSpaceHolder); + NO_MOVE_SEMANTIC(HeapSpaceHolder); + ~HeapSpaceHolder() noexcept { delete HeapSpaceTest::heap_space_; @@ -74,11 +76,11 @@ protected: }; struct GenerationalSpacesHolder { - GenerationalSpacesHolder(size_t init_young_size = DEFAULT_TEST_YOUNG_SIZE, - size_t young_size = DEFAULT_TEST_YOUNG_SIZE, - size_t initial_heap_size = DEFAULT_TEST_HEAP_SIZE, - size_t max_heap_size = DEFAULT_TEST_HEAP_SIZE, uint32_t min_percentage = 0U, - uint32_t max_percentage = PERCENT_100_U32) + explicit GenerationalSpacesHolder(size_t init_young_size = DEFAULT_TEST_YOUNG_SIZE, + size_t young_size = DEFAULT_TEST_YOUNG_SIZE, + size_t initial_heap_size = DEFAULT_TEST_HEAP_SIZE, + size_t max_heap_size = DEFAULT_TEST_HEAP_SIZE, uint32_t min_percentage = 0U, + uint32_t max_percentage = PERCENT_100_U32) { MemConfig::Initialize(max_heap_size, 0_MB, 0_MB, 0_MB, 0_MB, initial_heap_size); PoolManager::Initialize(); @@ -88,6 +90,9 @@ protected: min_percentage, max_percentage); } + NO_COPY_SEMANTIC(GenerationalSpacesHolder); + NO_MOVE_SEMANTIC(GenerationalSpacesHolder); + ~GenerationalSpacesHolder() noexcept { delete HeapSpaceTest::gen_spaces_; @@ -242,7 +247,8 @@ TEST_F(HeapSpaceTest, SharedPoolTest) TEST_F(HeapSpaceTest, ClampNewMaxSizeTest) { - HeapSpaceHolder hsh(10_MB, DEFAULT_TEST_HEAP_SIZE, 0U, PERCENT_100_U32); + constexpr uint64_t INITIAL_HEAP_SIZE = 10_MB; + HeapSpaceHolder hsh(INITIAL_HEAP_SIZE, DEFAULT_TEST_HEAP_SIZE, 0U, PERCENT_100_U32); auto pool_1 = heap_space_->TryAllocPool(4_MB, SpaceType::SPACE_TYPE_OBJECT, AllocatorType::FREELIST_ALLOCATOR, nullptr); diff --git a/runtime/tests/humongous_obj_allocator_test.cpp b/runtime/tests/humongous_obj_allocator_test.cpp index b3b898eac..eb3175316 100644 --- a/runtime/tests/humongous_obj_allocator_test.cpp +++ b/runtime/tests/humongous_obj_allocator_test.cpp @@ -33,11 +33,19 @@ public: HumongousObjAllocatorTest() { // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL); - panda::mem::MemConfig::Initialize(0, 1024_MB, 0, 0, 0); + constexpr size_t OBJECT_POOL_SIZE = 0; + constexpr size_t INTERNAL_SIZE = 1024_MB; + constexpr size_t COMPILER_SIZE = 0; + constexpr size_t CODE_SIZE = 0; + constexpr size_t FRAMES_SIZE = 0; + panda::mem::MemConfig::Initialize(OBJECT_POOL_SIZE, INTERNAL_SIZE, COMPILER_SIZE, CODE_SIZE, FRAMES_SIZE); PoolManager::Initialize(); } - ~HumongousObjAllocatorTest() + NO_COPY_SEMANTIC(HumongousObjAllocatorTest); + NO_MOVE_SEMANTIC(HumongousObjAllocatorTest); + + ~HumongousObjAllocatorTest() override { ClearPoolManager(); PoolManager::Finalize(); @@ -94,6 +102,7 @@ protected: allocated_pools_by_pool_manager_.clear(); } +private: std::vector allocated_pools_by_pool_manager_; // Mutex, which allows only one thread to add pool to the pool vector os::memory::Mutex pool_lock_; @@ -120,10 +129,11 @@ TEST_F(HumongousObjAllocatorTest, CheckIncorrectMemoryPoolReusageTest) TEST_F(HumongousObjAllocatorTest, SimpleAllocateDifferentObjSizeTest) { LOG(DEBUG, ALLOC) << "SimpleAllocateDifferentObjSizeTest"; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectHumongousObjAllocator allocator(mem_stats); std::vector values; - for (size_t i = 0; i < 20; i++) { + static constexpr size_t IT_BARRIER = 20; + for (size_t i = 0; i < IT_BARRIER; i++) { size_t pool_size = DEFAULT_POOL_SIZE_FOR_ALLOC + PAGE_SIZE * i; size_t alloc_size = pool_size - sizeof(POOL_HEADER_SIZE) - GetAlignmentInBytes(LOG_ALIGN_MAX); AddMemoryPoolToAllocator(allocator, pool_size); @@ -135,7 +145,7 @@ TEST_F(HumongousObjAllocatorTest, SimpleAllocateDifferentObjSizeTest) for (auto i : values) { allocator.Free(i); } - for (size_t i = 0; i < 20; i++) { + for (size_t i = 0; i < IT_BARRIER; i++) { void *mem = allocator.Alloc(MAX_ALLOC_SIZE); ASSERT_TRUE(mem != nullptr); } diff --git a/runtime/tests/internal_allocator_test.cpp b/runtime/tests/internal_allocator_test.cpp index ad2bfc5b9..929dbad95 100644 --- a/runtime/tests/internal_allocator_test.cpp +++ b/runtime/tests/internal_allocator_test.cpp @@ -34,7 +34,10 @@ public: allocator_ = new InternalAllocatorT(mem_stats_); } - ~InternalAllocatorTest() + NO_COPY_SEMANTIC(InternalAllocatorTest); + NO_MOVE_SEMANTIC(InternalAllocatorTest); + + ~InternalAllocatorTest() override { delete static_cast(allocator_); PoolManager::Finalize(); @@ -44,8 +47,7 @@ public: } protected: - mem::MemStatsType *mem_stats_; - InternalAllocatorPtr allocator_; + InternalAllocatorPtr allocator_; // NOLINT(misc-non-private-member-variables-in-classes) static constexpr size_t MEMORY_POOL_SIZE = 16_MB; @@ -68,6 +70,9 @@ protected: ASSERT(current_space_size <= max_space_size); return (max_space_size - current_space_size) >= InternalAllocator<>::RunSlotsAllocatorT::GetMinPoolSize(); } + +private: + mem::MemStatsType *mem_stats_; }; TEST_F(InternalAllocatorTest, AvoidInfiniteLoopTest) @@ -84,14 +89,24 @@ struct A { static size_t count_; A() { - value = ++count_; + value_ = ++count_; } + + NO_COPY_SEMANTIC(A); + NO_MOVE_SEMANTIC(A); + ~A() { --count_; } - uint8_t value; + constexpr inline uint8_t GetValue() const + { + return value_; + } + +private: + uint8_t value_; }; size_t A::count_ = 0; @@ -100,14 +115,15 @@ TEST_F(InternalAllocatorTest, NewDeleteArray) { constexpr size_t COUNT = 5; - auto arr = allocator_->New(COUNT); - ASSERT_NE(arr, nullptr); - ASSERT_EQ(ToUintPtr(arr) % DEFAULT_INTERNAL_ALIGNMENT_IN_BYTES, 0); + auto arr_tmp = allocator_->New(COUNT); // NOLINT(modernize-avoid-c-arrays) + ASSERT_NE(arr_tmp, nullptr); + ASSERT_EQ(ToUintPtr(arr_tmp) % DEFAULT_INTERNAL_ALIGNMENT_IN_BYTES, 0); ASSERT_EQ(A::count_, COUNT); + Span arr(arr_tmp, COUNT); for (uint8_t i = 1; i <= COUNT; ++i) { - ASSERT_EQ(arr[i - 1].value, i); + ASSERT_EQ(arr[i - 1].GetValue(), i); } - allocator_->DeleteArray(arr); + allocator_->DeleteArray(arr_tmp); ASSERT_EQ(A::count_, 0); } @@ -140,7 +156,7 @@ TEST_F(InternalAllocatorTest, AllocAlignmentTest) constexpr size_t N = RunSlots<>::MaxSlotSize() + DEFAULT_INTERNAL_ALIGNMENT_IN_BYTES; struct alignas(ALIGNMENT) S { - uint8_t a[N]; + std::array a; }; auto is_aligned = [](void *ptr) { return IsAligned(reinterpret_cast(ptr), ALIGNMENT); }; @@ -164,7 +180,7 @@ TEST_F(InternalAllocatorTest, AllocAlignmentTest) } { - auto *p = allocator_->New(1); + auto *p = allocator_->New(1); // NOLINT(modernize-avoid-c-arrays) ASSERT_TRUE(is_aligned(p)); allocator_->DeleteArray(p); } @@ -180,7 +196,7 @@ TEST_F(InternalAllocatorTest, AllocLocalAlignmentTest) constexpr size_t N = RunSlots<>::MaxSlotSize() + DEFAULT_INTERNAL_ALIGNMENT_IN_BYTES; struct alignas(ALIGNMENT) S { - uint8_t a[N]; + std::array a; }; auto is_aligned = [](void *ptr) { return IsAligned(reinterpret_cast(ptr), ALIGNMENT); }; @@ -210,7 +226,7 @@ TEST_F(InternalAllocatorTest, AllocLocalAlignmentTest) } { - auto *p = allocator_->NewLocal(1); + auto *p = allocator_->NewLocal(1); // NOLINT(modernize-avoid-c-arrays) ASSERT_TRUE(is_aligned(p)); allocator_->DeleteArray(p); } diff --git a/runtime/tests/malloc-proxy-allocator-test.cpp b/runtime/tests/malloc-proxy-allocator-test.cpp index 372ef0936..a07bbdf89 100644 --- a/runtime/tests/malloc-proxy-allocator-test.cpp +++ b/runtime/tests/malloc-proxy-allocator-test.cpp @@ -26,15 +26,12 @@ using MallocProxyNonObjectAllocator = MallocProxyAllocator { public: - MallocProxyAllocatorTest() - { - // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL); - } + MallocProxyAllocatorTest() = default; - ~MallocProxyAllocatorTest() - { - // Logger::Destroy(); - } + NO_COPY_SEMANTIC(MallocProxyAllocatorTest); + NO_MOVE_SEMANTIC(MallocProxyAllocatorTest); + + ~MallocProxyAllocatorTest() override = default; protected: static constexpr size_t SIZE_ALLOC = 1_KB; @@ -51,7 +48,7 @@ protected: TEST_F(MallocProxyAllocatorTest, SimpleTest) { static constexpr size_t SIZE = 23; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); MallocProxyNonObjectAllocator allocator(mem_stats); void *a1; a1 = allocator.Alloc(SIZE); @@ -72,8 +69,9 @@ TEST_F(MallocProxyAllocatorTest, AllocFreeTest) TEST_F(MallocProxyAllocatorTest, AdapterTest) { - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); MallocProxyNonObjectAllocator allocator(mem_stats); + // NOLINTNEXTLINE(readability-magic-numbers) std::array arr {{12, 14, 3, 5, 43, 12, 22, 42, 89, 10, 89, 32, 43, 12, 43, 12, 54, 89, 27, 84}}; std::vector v; diff --git a/runtime/tests/mark_word_test.cpp b/runtime/tests/mark_word_test.cpp index dc2dcd590..418a6f2ea 100644 --- a/runtime/tests/mark_word_test.cpp +++ b/runtime/tests/mark_word_test.cpp @@ -22,8 +22,11 @@ namespace panda { class MarkWordTest : public testing::Test { +private: + static constexpr uint32_t FIXED_SEED = 0xC0E67D50; + public: - MarkWordTest() {} + MarkWordTest() = default; protected: void SetUp() override @@ -49,12 +52,12 @@ protected: public: using MarkWordDistribution = std::uniform_int_distribution; - RandomTestValuesGetter() + RandomTestValuesGetter() // NOLINT(cert-msc51-cpp) { #ifdef PANDA_NIGHTLY_TEST_ON seed_ = std::random_device()(); #else - seed_ = 0xC0E67D50; + seed_ = FIXED_SEED; #endif gen_ = std::mt19937(seed_); @@ -142,7 +145,7 @@ protected: template class MarkWordWrapper { public: - MarkWordWrapper(bool is_marked_for_gc = false, bool is_read_barrier_set = false) + explicit MarkWordWrapper(bool is_marked_for_gc = false, bool is_read_barrier_set = false) { if (is_marked_for_gc) { mw_ = mw_.SetMarkedForGC(); diff --git a/runtime/tests/mem_stats_additional_info_test.cpp b/runtime/tests/mem_stats_additional_info_test.cpp index 0b07a4a1f..314882766 100644 --- a/runtime/tests/mem_stats_additional_info_test.cpp +++ b/runtime/tests/mem_stats_additional_info_test.cpp @@ -43,6 +43,9 @@ public: thread_->ManagedCodeBegin(); } + NO_COPY_SEMANTIC(MemStatsAdditionalInfoTest); + NO_MOVE_SEMANTIC(MemStatsAdditionalInfoTest); + ~MemStatsAdditionalInfoTest() override { thread_->ManagedCodeEnd(); @@ -50,7 +53,7 @@ public: } protected: - panda::MTManagedThread *thread_; + panda::MTManagedThread *thread_; // NOLINT(misc-non-private-member-variables-in-classes) }; TEST_F(MemStatsAdditionalInfoTest, HeapAllocatedMaxAndTotal) @@ -94,7 +97,7 @@ TEST_F(MemStatsAdditionalInfoTest, AdditionalStatistic) // allocated ASSERT_TRUE(statistics.find(string_class->GetName()) != std::string::npos); ASSERT_TRUE(statistics.find("footprint") != std::string::npos); - ASSERT_TRUE(statistics.find("1") != std::string::npos); + ASSERT_TRUE(statistics.find('1') != std::string::npos); #endif } @@ -102,18 +105,18 @@ TEST_F(MemStatsAdditionalInfoTest, AdditionalStatistic) TEST_F(MemStatsAdditionalInfoTest, GCPhaseTimeTest) { // pauses in milliseconds - constexpr uint64_t PAUSES[] = {20, 10, 30}; + constexpr uint PAUSES_COUNT = 3; + constexpr std::array PAUSES = {20, 10, 30}; constexpr uint64_t MIN_PAUSE = 10; constexpr uint64_t MAX_PAUSE = 30; constexpr uint64_t TOTAL_PAUSE = 60; - constexpr uint PAUSES_COUNT = 3; constexpr uint64_t AVG_PAUSE = TOTAL_PAUSE / PAUSES_COUNT; MemStatsAdditionalInfo stats; - for (uint i = 0; i < PAUSES_COUNT; i++) { + for (auto pause : PAUSES) { for (uint ph = 0; ph < static_cast(GCPhase::GC_PHASE_LAST); ph++) { stats.RecordGCPhaseStart(ToGCPhase(ph)); - std::this_thread::sleep_for(std::chrono::milliseconds(static_cast(PAUSES[i]))); + std::this_thread::sleep_for(std::chrono::milliseconds(pause)); stats.RecordGCPhaseEnd(); } } diff --git a/runtime/tests/mem_stats_gc_test.cpp b/runtime/tests/mem_stats_gc_test.cpp index 360af38f2..6fd381218 100644 --- a/runtime/tests/mem_stats_gc_test.cpp +++ b/runtime/tests/mem_stats_gc_test.cpp @@ -23,6 +23,8 @@ #include "runtime/handle_scope-inl.h" #include "runtime/mem/mem_stats.h" #include "runtime/mem/mem_stats_default.h" +#include +#include namespace panda::mem::test { @@ -34,7 +36,7 @@ public: options.SetShouldLoadBootPandaFiles(false); options.SetShouldInitializeIntrinsics(false); options.SetUseTlabForAllocations(false); - options.SetGcType(gc_type); + options.SetGcType(std::move(gc_type)); options.SetRunGcInPlace(true); options.SetExplicitConcurrentGcEnabled(false); bool success = Runtime::Create(options); @@ -53,7 +55,7 @@ public: ASSERT_TRUE(success) << "Cannot destroy Runtime"; } - panda::MTManagedThread *thread; + panda::MTManagedThread *thread {nullptr}; // NOLINT(misc-non-private-member-variables-in-classes) }; template @@ -91,7 +93,7 @@ void MemStatsGCTest::MemStatsTest(uint64_t tries, size_t object_size) uint64_t freed_objects = stats->GetTotalObjectsFreed(); uint64_t freed_bytes = stats->GetFreed(SpaceType::SPACE_TYPE_OBJECT); uint64_t diff_total = 0; - std::array *, OBJECT_COUNT> handlers; + std::array *, OBJECT_COUNT> handlers {}; for (size_t i = 0; i < tries; i++) { [[maybe_unused]] HandleScope scope(thread); for (uint64_t j = 0; j < OBJECT_COUNT; j++) { @@ -125,14 +127,13 @@ void MemStatsGCTest::MemStatsTest(uint64_t tries, size_t object_size) } } -constexpr size_t OBJECTS_SIZE[] = { +constexpr std::array OBJECTS_SIZE = { 32, // RunSlots: aligned & object_size = RunSlot size 72, // RunSlots: aligned & object_size != RunSlot size 129, // RunSlots: not aligned 512, // FreeList: aligned 1025 // FreeList: not aligned }; -constexpr size_t NUM_SIZES = sizeof(OBJECTS_SIZE) / sizeof(OBJECTS_SIZE[0]); TEST_F(MemStatsGCTest, GenGcTest) { @@ -140,8 +141,8 @@ TEST_F(MemStatsGCTest, GenGcTest) constexpr uint64_t TRIES = 4; SetupRuntime("gen-gc"); - for (size_t i = 0; i < NUM_SIZES; i++) { - MemStatsTest(TRIES, OBJECTS_SIZE[i]); + for (auto size : OBJECTS_SIZE) { + MemStatsTest(TRIES, size); } } @@ -151,8 +152,8 @@ TEST_F(MemStatsGCTest, StwGcTest) constexpr uint64_t TRIES = 10; SetupRuntime("stw"); - for (size_t i = 0; i < NUM_SIZES; i++) { - MemStatsTest(TRIES, OBJECTS_SIZE[i]); + for (auto size : OBJECTS_SIZE) { + MemStatsTest(TRIES, size); } } diff --git a/runtime/tests/mem_stats_gen_gc_test.cpp b/runtime/tests/mem_stats_gen_gc_test.cpp index e80b2cfd4..61ac70c81 100644 --- a/runtime/tests/mem_stats_gen_gc_test.cpp +++ b/runtime/tests/mem_stats_gen_gc_test.cpp @@ -26,6 +26,7 @@ #include "runtime/mem/runslots_allocator-inl.h" namespace panda::mem::test { +// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init) class MemStatsGenGCTest : public testing::Test { public: using ObjVec = PandaVector; @@ -102,7 +103,7 @@ public: { } - int count = 0; + int count = 0; // NOLINT(misc-non-private-member-variables-in-classes) }; struct MemOpReport { @@ -157,8 +158,8 @@ public: size_t *requested, F space_checker, bool check_oom_in_tenured); void InitRoot(); - void MakeObjectsAlive(ObjVec objects, int every = 1); - void MakeObjectsPermAlive(ObjVec objects, int every = 1); + void MakeObjectsAlive(ObjVec const &objects, int every = 1); + void MakeObjectsPermAlive(ObjVec const &objects, int every = 1); void MakeObjectsGarbage(size_t start_idx, size_t after_end_idx, int every = 1); void DumpHandles(); void DumpAliveObjects(); @@ -190,20 +191,22 @@ public: void TearDown() override {} - panda::MTManagedThread *thread; + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + panda::MTManagedThread *thread {nullptr}; GCType gc_type; LanguageContext ctx {nullptr}; - ObjectAllocatorBase *object_allocator; + ObjectAllocatorBase *object_allocator {nullptr}; mem::InternalAllocatorPtr internal_allocator; - PandaVM *vm; - GC *gc; + PandaVM *vm {nullptr}; + GC *gc {nullptr}; std::vector handles; - MemStatsType *ms; - GCStats *gc_ms; + MemStatsType *ms {nullptr}; + GCStats *gc_ms {nullptr}; coretypes::Array *root = nullptr; size_t root_size = 0; - GCCounter *gccnt; + GCCounter *gccnt {nullptr}; + // NOLINTEND(misc-non-private-member-variables-in-classes) }; template @@ -235,7 +238,7 @@ MemStatsGenGCTest::ObjVec MemStatsGenGCTest::MakeAllocationsWithRepeats(size_t m // Leaving 5MB in tenured seems OK auto free = reinterpret_cast(object_allocator->GetHeapSpace())->GetCurrentFreeTenuredSize(); - if (size + 5000000 > free) { + if (size + 5_MB > free) { // NOLINT(readability-magic-numbers) return result; } } @@ -269,7 +272,7 @@ void MemStatsGenGCTest::InitRoot() MakeObjectsPermAlive({root}); } -void MemStatsGenGCTest::MakeObjectsAlive(ObjVec objects, int every) +void MemStatsGenGCTest::MakeObjectsAlive(ObjVec const &objects, int every) { int cnt = every; for (auto *obj : objects) { @@ -297,7 +300,7 @@ void MemStatsGenGCTest::MakeObjectsGarbage(size_t start_idx, size_t after_end_id } } -void MemStatsGenGCTest::MakeObjectsPermAlive(ObjVec objects, int every) +void MemStatsGenGCTest::MakeObjectsPermAlive(ObjVec const &objects, int every) { HanVec result; result.reserve(objects.size() / every); @@ -393,11 +396,7 @@ template typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::MakeAllocations() { [[maybe_unused]] int gc_cnt = gccnt->count; - MemStatsGenGCTest::MemOpReport report; - report.allocated_count = 0; - report.allocated_bytes = 0; - report.saved_count = 0; - report.saved_bytes = 0; + MemStatsGenGCTest::MemOpReport report {0, 0, 0, 0}; size_t bytes = 0; [[maybe_unused]] size_t raw_objects_size; // currently not tracked by memstats size_t count = 0; @@ -410,7 +409,7 @@ typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::MakeAllocations() switch (gc_type) { case GCType::GEN_GC: { auto gen_alloc = reinterpret_cast *>(object_allocator); - count = 15; + count = 15; // NOLINT(readability-magic-numbers) if constexpr (SPACE == TargetSpace::YOUNG) { min_size = 0; max_size = gen_alloc->GetYoungAllocMaxSize(); @@ -442,7 +441,7 @@ typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::MakeAllocations() } case GCType::G1_GC: { auto g1_alloc = reinterpret_cast *>(object_allocator); - count = 15; + count = 15; // NOLINT(readability-magic-numbers) if constexpr (SPACE == TargetSpace::YOUNG) { min_size = 0; max_size = g1_alloc->GetYoungAllocMaxSize(); @@ -528,11 +527,7 @@ typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::MakeAllocations() typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::HelpAllocTenured() { - MemStatsGenGCTest::MemOpReport report; - report.allocated_count = 0; - report.allocated_bytes = 0; - report.saved_count = 0; - report.saved_bytes = 0; + MemStatsGenGCTest::MemOpReport report {0, 0, 0, 0}; auto old_root_size = root_size; @@ -574,20 +569,16 @@ typename MemStatsGenGCTest::MemOpReport MemStatsGenGCTest::HelpAllocTenured() template MemStatsGenGCTest::RealStatsLocations MemStatsGenGCTest::GetGenMemStatsDetails(T gms) { - RealStatsLocations loc; - loc.young_freed_objects_count = &gms->young_free_object_count_; - loc.young_freed_objects_size = &gms->young_free_object_size_; - loc.young_moved_objects_count = &gms->young_move_object_count_; - loc.young_moved_objects_size = &gms->young_move_object_size_; - loc.tenured_freed_objects_count = &gms->tenured_free_object_count_; - loc.tenured_freed_objects_size = &gms->tenured_free_object_size_; + RealStatsLocations loc {&gms->young_free_object_count_, &gms->young_free_object_size_, + &gms->young_move_object_count_, &gms->young_move_object_size_, + &gms->tenured_free_object_count_, &gms->tenured_free_object_size_}; return loc; } TEST_F(MemStatsGenGCTest, TrivialStatsGenGcTest) { for (int gctype_idx = 0; static_cast(gctype_idx) <= GCType::GCTYPE_LAST; ++gctype_idx) { - GCType gc_type_local = static_cast(gctype_idx); + auto gc_type_local = static_cast(gctype_idx); if (gc_type_local == GCType::INVALID_GC) { continue; } diff --git a/runtime/tests/panda_smart_pointers_test.cpp b/runtime/tests/panda_smart_pointers_test.cpp index ea67bc2f6..4174e1f7b 100644 --- a/runtime/tests/panda_smart_pointers_test.cpp +++ b/runtime/tests/panda_smart_pointers_test.cpp @@ -33,13 +33,16 @@ public: thread_->ManagedCodeBegin(); } + NO_COPY_SEMANTIC(PandaSmartPointersTest); + NO_MOVE_SEMANTIC(PandaSmartPointersTest); + ~PandaSmartPointersTest() override { thread_->ManagedCodeEnd(); Runtime::Destroy(); } -protected: +private: panda::MTManagedThread *thread_; }; @@ -59,13 +62,13 @@ TEST_F(PandaSmartPointersTest, MakePandaUniqueTest) int res = ReturnValueFromUniqPtr(std::move(uniq_ptr)); ASSERT_EQ(res, 5); - ASSERT_EQ(uniq_ptr.get(), nullptr); + ASSERT_EQ(uniq_ptr.get(), nullptr); // NOLINT(clang-analyzer-cplusplus.Move) // Unbounded array type static constexpr size_t SIZE = 3; - auto uniq_ptr_2 = MakePandaUnique(SIZE); + auto uniq_ptr_2 = MakePandaUnique(SIZE); // NOLINT(modernize-avoid-c-arrays) ASSERT_NE(uniq_ptr_2.get(), nullptr); for (size_t i = 0; i < SIZE; ++i) { @@ -76,7 +79,7 @@ TEST_F(PandaSmartPointersTest, MakePandaUniqueTest) for (size_t i = 0; i < SIZE; ++i) { ASSERT_EQ(uniq_ptr_3[i], i); } - ASSERT_EQ(uniq_ptr_2.get(), nullptr); + ASSERT_EQ(uniq_ptr_2.get(), nullptr); // NOLINT(clang-analyzer-cplusplus.Move) } } // namespace panda::mem::test diff --git a/runtime/tests/rem_set_test.cpp b/runtime/tests/rem_set_test.cpp index f29f1ccd2..732364e6b 100644 --- a/runtime/tests/rem_set_test.cpp +++ b/runtime/tests/rem_set_test.cpp @@ -46,14 +46,17 @@ public: size_t space_size = options_.GetHeapSizeLimit(); spaces_.young_space_.Initialize(space_size, space_size); spaces_.mem_space_.Initialize(space_size, space_size); - spaces_.InitializePercentages(0, 100); + spaces_.InitializePercentages(0, PERCENT_100_U32); spaces_.is_initialized_ = true; thread_ = panda::MTManagedThread::GetCurrent(); thread_->ManagedCodeBegin(); Init(); } - ~RemSetTest() + NO_COPY_SEMANTIC(RemSetTest); + NO_MOVE_SEMANTIC(RemSetTest); + + ~RemSetTest() override { thread_->ManagedCodeEnd(); card_table_ = nullptr; @@ -71,16 +74,19 @@ public: } protected: + // NOLINTBEGIN(misc-non-private-member-variables-in-classes) + ClassLinkerExtension *ext_ {nullptr}; + GenerationalSpaces spaces_; + // NOLINTEND(misc-non-private-member-variables-in-classes) +private: panda::MTManagedThread *thread_; RuntimeOptions options_; - ClassLinkerExtension *ext_; - GenerationalSpaces spaces_; PandaUniquePtr card_table_ {nullptr}; }; TEST_F(RemSetTest, AddRefTest) { - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectRegionAllocator allocator(mem_stats, &spaces_); auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(panda::Class)); cls->SetObjectSize(allocator.GetMaxRegularObjectSize()); @@ -114,7 +120,7 @@ TEST_F(RemSetTest, AddRefTest) TEST_F(RemSetTest, AddRefWithAddrTest) { - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectRegionAllocator allocator(mem_stats, &spaces_); auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(panda::Class)); cls->SetObjectSize(allocator.GetMaxRegularObjectSize()); @@ -146,7 +152,7 @@ TEST_F(RemSetTest, AddRefWithAddrTest) TEST_F(RemSetTest, TravelObjectToAddRefTest) { - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectRegionAllocator allocator(mem_stats, &spaces_); auto cls = ext_->CreateClass(nullptr, 0, 0, sizeof(panda::Class)); cls->SetObjectSize(allocator.GetMaxRegularObjectSize()); diff --git a/runtime/tests/runslots_allocator_test.cpp b/runtime/tests/runslots_allocator_test.cpp index ff1695d1f..70539526f 100644 --- a/runtime/tests/runslots_allocator_test.cpp +++ b/runtime/tests/runslots_allocator_test.cpp @@ -28,12 +28,12 @@ using RunSlotsType = RunSlots<>; class RunSlotsAllocatorTest : public AllocatorTest { public: - RunSlotsAllocatorTest() - { - // Logger::InitializeStdLogging(Logger::Level::DEBUG, Logger::Component::ALL); - } + RunSlotsAllocatorTest() = default; + + NO_COPY_SEMANTIC(RunSlotsAllocatorTest); + NO_MOVE_SEMANTIC(RunSlotsAllocatorTest); - ~RunSlotsAllocatorTest() + ~RunSlotsAllocatorTest() override { for (auto i : allocated_mem_mmap_) { panda::os::mem::UnmapRaw(std::get<0>(i), std::get<1>(i)); @@ -87,13 +87,14 @@ protected: auto runslots = reinterpret_cast(mem); runslots->Initialize(slots_size, ToUintPtr(mem), true); int i = 0; - while (runslots->PopFreeSlot()) { + while (runslots->PopFreeSlot() != nullptr) { i++; } - free(mem); + free(mem); // NOLINT(cppcoreguidelines-no-malloc) LOG(DEBUG, ALLOC) << "Iteration = " << i; } +private: std::vector> allocated_mem_mmap_; // Mutex, which allows only one thread to add pool to the pool vector os::memory::Mutex pool_lock_; @@ -113,7 +114,9 @@ TEST_F(RunSlotsAllocatorTest, SimpleAllocateDifferentObjSizeTest) mem::MemStatsType mem_stats; NonObjectAllocator allocator(&mem_stats); AddMemoryPoolToAllocator(allocator); - for (size_t i = 23; i < 300; i++) { + constexpr size_t MIN_SIZE = 23; + constexpr size_t MAX_SIZE = 300; + for (size_t i = MIN_SIZE; i < MAX_SIZE; i++) { void *mem = allocator.Alloc(i); LOG(DEBUG, ALLOC) << "Allocate obj with size " << i << " at " << std::hex << mem; } @@ -170,7 +173,8 @@ TEST_F(RunSlotsAllocatorTest, AllocateAllPossibleSizesFreeTest) TEST_F(RunSlotsAllocatorTest, AllocateWriteFreeTest) { - AllocateAndFree(sizeof(uint64_t), 512); + constexpr size_t CNT_OF_ELEMENTS = 512; + AllocateAndFree(sizeof(uint64_t), CNT_OF_ELEMENTS); } TEST_F(RunSlotsAllocatorTest, AllocateRandomFreeTest) @@ -210,7 +214,7 @@ TEST_F(RunSlotsAllocatorTest, AllocateVectorTest) TEST_F(RunSlotsAllocatorTest, AllocateReuse2) { // It's regression test - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectAllocator allocator(mem_stats); static constexpr size_t SIZE1 = 60; static constexpr size_t SIZE2 = 204; @@ -221,16 +225,17 @@ TEST_F(RunSlotsAllocatorTest, AllocateReuse2) constexpr char CHAR5 = 'e'; constexpr char CHAR6 = 'f'; AddMemoryPoolToAllocatorProtected(allocator); + // NOLINTNEXTLINE(readability-isolate-declaration) char *str_a, *str_b, *str_c, *str_d, *str_e, *str_f; auto fill_str = [](char *str, char c, size_t size) { for (size_t i = 0; i < size - 1; i++) { - str[i] = c; + str[i] = c; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) } - str[size - 1] = 0; + str[size - 1] = 0; // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) }; - auto check_str = [](char *str, char c, size_t size) { + auto check_str = [](char const *str, char c, size_t size) { for (size_t i = 0; i < size - 1; i++) { - if (str[i] != c) { + if (str[i] != c) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) return false; } } @@ -296,7 +301,7 @@ TEST_F(RunSlotsAllocatorTest, RunSlotsReusingTest) { static constexpr size_t SMALL_OBJ_SIZE = sizeof(uint32_t); static constexpr size_t BIG_OBJ_SIZE = 128; - mem::MemStatsType *mem_stats = new mem::MemStatsType(); + auto *mem_stats = new mem::MemStatsType(); NonObjectAllocator allocator(mem_stats); AddMemoryPoolToAllocatorProtected(allocator); // Alloc one big object. this must cause runslots init with it size diff --git a/runtime/tests/static_analyzer_test.cpp b/runtime/tests/static_analyzer_test.cpp index 772971583..3fa80385e 100644 --- a/runtime/tests/static_analyzer_test.cpp +++ b/runtime/tests/static_analyzer_test.cpp @@ -53,7 +53,10 @@ public: Runtime::Create(options); } - ~StaticAnalyzerTest() + NO_COPY_SEMANTIC(StaticAnalyzerTest); + NO_MOVE_SEMANTIC(StaticAnalyzerTest); + + ~StaticAnalyzerTest() override { Runtime::Destroy(); } @@ -103,15 +106,28 @@ TEST_F(StaticAnalyzerTest, TestArray) class ClearA { public: + ClearA() = default; + + DEFAULT_COPY_SEMANTIC(ClearA); + DEFAULT_MOVE_SEMANTIC(ClearA); + + virtual ~ClearA() = default; + virtual ObjectHeader *VirtualAllocString([[maybe_unused]] StaticAnalyzerTest &sat) { return nullptr; } - - virtual ~ClearA() = default; }; class TriggeredB : public ClearA { +public: + TriggeredB() = default; + + DEFAULT_COPY_SEMANTIC(TriggeredB); + DEFAULT_MOVE_SEMANTIC(TriggeredB); + + ~TriggeredB() override = default; + ObjectHeader *VirtualAllocString(StaticAnalyzerTest &sat) override { return sat.AllocString(); diff --git a/runtime/tests/tlab_test.cpp b/runtime/tests/tlab_test.cpp index 3b29636e4..c710538b9 100644 --- a/runtime/tests/tlab_test.cpp +++ b/runtime/tests/tlab_test.cpp @@ -25,6 +25,9 @@ namespace panda::mem { constexpr size_t TLAB_TEST_SIZE = 4_MB; class TLABTest : public testing::Test { +private: + static constexpr unsigned FIXED_SEED = 0xDEADBEEF; + public: TLABTest() { @@ -32,12 +35,15 @@ public: #ifdef PANDA_NIGHTLY_TEST_ON seed_ = time(NULL); #else - seed_ = 0x0BADDEAD; + seed_ = FIXED_SEED; #endif srand(seed_); } - ~TLABTest() + NO_COPY_SEMANTIC(TLABTest); + NO_MOVE_SEMANTIC(TLABTest); + + ~TLABTest() override { for (auto i : allocated_mem_mmap_) { panda::os::mem::UnmapRaw(std::get<0>(i), std::get<1>(i)); @@ -57,6 +63,16 @@ protected: return new_tlab; } + inline void InitRand() const + { + srand(seed_); + } + inline int GetRand() const + { + return rand(); // NOLINT(cert-msc50-cpp) + } + +private: std::vector> allocated_mem_mmap_; unsigned seed_; }; @@ -90,24 +106,24 @@ TEST_F(TLABTest, AlignedAlloc) constexpr size_t ARRAY_SIZE = 1024; TLAB *tlab = CreateNewTLAB(); Alignment align = DEFAULT_ALIGNMENT; - std::array arr; + std::array arr {}; size_t mask = GetAlignmentInBytes(align) - 1; // Allocations - srand(seed_); + InitRand(); for (size_t i = 0; i < ARRAY_SIZE; ++i) { arr[i] = static_cast(tlab->Alloc(sizeof(int))); - *arr[i] = rand() % std::numeric_limits::max(); + *arr[i] = GetRand() % std::numeric_limits::max(); } // Allocations checking - srand(seed_); + InitRand(); for (size_t i = 0; i < ARRAY_SIZE; ++i) { ASSERT_NE(arr[i], nullptr) << "value of i: " << i << ", align: " << align; ASSERT_EQ(reinterpret_cast(arr[i]) & mask, static_cast(0)) << "value of i: " << i << ", align: " << align; - ASSERT_EQ(*arr[i], rand() % std::numeric_limits::max()) << "value of i: " << i << ", align: " << align; + ASSERT_EQ(*arr[i], GetRand() % std::numeric_limits::max()) << "value of i: " << i << ", align: " << align; } void *ptr = tlab->Alloc(TLAB_TEST_SIZE); -- Gitee