• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include "runtime/include/runtime.h"
18 #include "runtime/mem/gc/gc_trigger.h"
19 
20 namespace panda::test {
21 class GCTriggerTest : public testing::Test {
22 public:
GCTriggerTest()23     GCTriggerTest()
24     {
25         RuntimeOptions options;
26         options.SetShouldLoadBootPandaFiles(false);
27         options.SetShouldInitializeIntrinsics(false);
28         options.SetGcTriggerType("adaptive-heap-trigger");
29 
30         Runtime::Create(options);
31         thread_ = panda::MTManagedThread::GetCurrent();
32         thread_->ManagedCodeBegin();
33     }
34 
~GCTriggerTest()35     ~GCTriggerTest()
36     {
37         thread_->ManagedCodeEnd();
38         Runtime::Destroy();
39     }
40 
CreateGCTriggerHeap() const41     [[nodiscard]] mem::GCAdaptiveTriggerHeap *CreateGCTriggerHeap() const
42     {
43         return new mem::GCAdaptiveTriggerHeap(nullptr, nullptr, MIN_HEAP_SIZE,
44                                               mem::GCTriggerHeap::DEFAULT_PERCENTAGE_THRESHOLD,
45                                               DEFAULT_INCREASE_MULTIPLIER, MIN_EXTRA_HEAP_SIZE, MAX_EXTRA_HEAP_SIZE);
46     }
47 
GetTargetFootprint(const mem::GCAdaptiveTriggerHeap * trigger)48     static size_t GetTargetFootprint(const mem::GCAdaptiveTriggerHeap *trigger)
49     {
50         // Atomic with relaxed order reason: simple getter for test
51         return trigger->target_footprint_.load(std::memory_order_relaxed);
52     }
53 
54 protected:
55     static constexpr size_t MIN_HEAP_SIZE = 8_MB;
56     static constexpr size_t MIN_EXTRA_HEAP_SIZE = 1_MB;
57     static constexpr size_t MAX_EXTRA_HEAP_SIZE = 8_MB;
58     static constexpr uint32_t DEFAULT_INCREASE_MULTIPLIER = 3U;
59 
60     MTManagedThread *thread_ = nullptr;
61 };
62 
TEST_F(GCTriggerTest,ThresholdTest)63 TEST_F(GCTriggerTest, ThresholdTest)
64 {
65     static constexpr size_t BEFORE_HEAP_SIZE = 50_MB;
66     static constexpr size_t CURRENT_HEAP_SIZE = MIN_HEAP_SIZE;
67     static constexpr size_t FIRST_THRESHOLD = 2U * MIN_HEAP_SIZE;
68     static constexpr size_t HEAP_SIZE_AFTER_BASE_TRIGGER = (BEFORE_HEAP_SIZE + CURRENT_HEAP_SIZE) / 2U;
69     auto *trigger = CreateGCTriggerHeap();
70     GCTask task(GCTaskCause::HEAP_USAGE_THRESHOLD_CAUSE);
71 
72     trigger->ComputeNewTargetFootprint(task, BEFORE_HEAP_SIZE, CURRENT_HEAP_SIZE);
73 
74     ASSERT_EQ(GetTargetFootprint(trigger), FIRST_THRESHOLD);
75 
76     trigger->ComputeNewTargetFootprint(task, BEFORE_HEAP_SIZE, CURRENT_HEAP_SIZE);
77     ASSERT_EQ(GetTargetFootprint(trigger), HEAP_SIZE_AFTER_BASE_TRIGGER);
78     trigger->ComputeNewTargetFootprint(task, BEFORE_HEAP_SIZE, CURRENT_HEAP_SIZE);
79     ASSERT_EQ(GetTargetFootprint(trigger), HEAP_SIZE_AFTER_BASE_TRIGGER);
80     trigger->ComputeNewTargetFootprint(task, BEFORE_HEAP_SIZE, CURRENT_HEAP_SIZE);
81     ASSERT_EQ(GetTargetFootprint(trigger), HEAP_SIZE_AFTER_BASE_TRIGGER);
82     trigger->ComputeNewTargetFootprint(task, BEFORE_HEAP_SIZE, CURRENT_HEAP_SIZE);
83 
84     // Check that we could to avoid locale triggering
85     ASSERT_EQ(GetTargetFootprint(trigger), FIRST_THRESHOLD);
86 
87     delete trigger;
88 }
89 }  // namespace panda::test
90