1 /** 2 * Copyright (c) 2021-2024 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 #ifndef PANDA_RUNTIME_TESTS_PYGOTE_SPACE_ALLOCATOR_TEST_H_ 16 #define PANDA_RUNTIME_TESTS_PYGOTE_SPACE_ALLOCATOR_TEST_H_ 17 18 #include <sys/mman.h> 19 #include <gtest/gtest.h> 20 21 #include "libpandabase/os/mem.h" 22 #include "libpandabase/utils/logger.h" 23 #include "runtime/mem/runslots_allocator-inl.h" 24 #include "runtime/mem/pygote_space_allocator-inl.h" 25 #include "runtime/include/object_header.h" 26 #include "runtime/mem/refstorage/global_object_storage.h" 27 28 namespace ark::mem { 29 30 class PygoteSpaceAllocatorTest : public testing::Test { 31 public: 32 NO_COPY_SEMANTIC(PygoteSpaceAllocatorTest); 33 NO_MOVE_SEMANTIC(PygoteSpaceAllocatorTest); 34 35 using PygoteAllocator = PygoteSpaceAllocator<ObjectAllocConfig>; 36 37 PygoteSpaceAllocatorTest() = default; 38 ~PygoteSpaceAllocatorTest() override = default; 39 40 protected: GetPygoteSpaceAllocator()41 PygoteAllocator *GetPygoteSpaceAllocator() 42 { 43 return thread_->GetVM()->GetGC()->GetObjectAllocator()->GetPygoteSpaceAllocator(); 44 } 45 GetObjectClass()46 Class *GetObjectClass() 47 { 48 auto runtime = ark::Runtime::GetCurrent(); 49 LanguageContext ctx = runtime->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY); 50 return runtime->GetClassLinker()->GetExtension(ctx)->GetClassRoot(ClassRoot::OBJECT); 51 } 52 PygoteFork()53 void PygoteFork() 54 { 55 thread_->ManagedCodeEnd(); 56 auto runtime = ark::Runtime::GetCurrent(); 57 runtime->PreZygoteFork(); 58 runtime->PostZygoteFork(); 59 thread_->ManagedCodeBegin(); 60 } 61 TriggerGc()62 void TriggerGc() 63 { 64 auto gc = thread_->GetVM()->GetGC(); 65 auto task = GCTask(GCTaskCause::EXPLICIT_CAUSE); 66 // trigger tenured gc 67 gc->WaitForGCInManaged(task); 68 gc->WaitForGCInManaged(task); 69 gc->WaitForGCInManaged(task); 70 } 71 72 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) 73 RuntimeOptions options_; 74 75 void InitAllocTest(); 76 77 void ForkedAllocTest(); 78 79 void NonMovableLiveObjectAllocTest(); 80 81 void NonMovableUnliveObjectAllocTest(); 82 83 void MovableLiveObjectAllocTest(); 84 85 void MovableUnliveObjectAllocTest(); 86 87 void MuchObjectAllocTest(); 88 89 protected: 90 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes) 91 ark::MTManagedThread *thread_ {nullptr}; 92 }; 93 94 } // namespace ark::mem 95 96 #endif // PANDA_RUNTIME_TESTS_PYGOTE_SPACE_ALLOCATOR_TEST_H_ 97