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 <array>
17 #include <atomic>
18 #include <condition_variable>
19 #include <thread>
20
21 #include "gtest/gtest.h"
22 #include "iostream"
23 #include "runtime/handle_base-inl.h"
24 #include "runtime/include/coretypes/string.h"
25 #include "runtime/include/runtime.h"
26 #include "runtime/include/panda_vm.h"
27 #include "runtime/mem/gc/gc_phase.h"
28 #include "runtime/mem/mem_stats.h"
29 #include "runtime/mem/mem_stats_additional_info.h"
30
31 namespace panda::mem::test {
32
33 class MemStatsAdditionalInfoTest : public testing::Test {
34 public:
MemStatsAdditionalInfoTest()35 MemStatsAdditionalInfoTest()
36 {
37 RuntimeOptions options;
38 options.SetShouldLoadBootPandaFiles(false);
39 options.SetShouldInitializeIntrinsics(false);
40 options.SetGcType("epsilon");
41 Runtime::Create(options);
42 thread_ = panda::MTManagedThread::GetCurrent();
43 thread_->ManagedCodeBegin();
44 }
45
~MemStatsAdditionalInfoTest()46 ~MemStatsAdditionalInfoTest() override
47 {
48 thread_->ManagedCodeEnd();
49 Runtime::Destroy();
50 }
51
52 protected:
53 panda::MTManagedThread *thread_;
54 };
55
TEST_F(MemStatsAdditionalInfoTest,HeapAllocatedMaxAndTotal)56 TEST_F(MemStatsAdditionalInfoTest, HeapAllocatedMaxAndTotal)
57 {
58 static constexpr size_t BYTES_ALLOC1 = 2;
59 static constexpr size_t BYTES_ALLOC2 = 5;
60 static constexpr size_t RAW_ALLOC1 = 15;
61
62 std::string simple_string = "smallData";
63 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
64 coretypes::String *string_object = coretypes::String::CreateFromMUtf8(
65 reinterpret_cast<const uint8_t *>(&simple_string[0]), simple_string.length(), ctx, thread_->GetVM());
66
67 size_t string_size = string_object->ObjectSize();
68
69 MemStatsAdditionalInfo stats;
70 stats.RecordAllocateObject(BYTES_ALLOC1, SpaceType::SPACE_TYPE_OBJECT);
71 stats.RecordAllocateObject(BYTES_ALLOC2, SpaceType::SPACE_TYPE_OBJECT);
72 stats.RecordAllocateRaw(RAW_ALLOC1, SpaceType::SPACE_TYPE_INTERNAL);
73
74 stats.RecordAllocateObject(string_size, SpaceType::SPACE_TYPE_OBJECT);
75 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2 + string_size, stats.GetAllocated(SpaceType::SPACE_TYPE_OBJECT));
76 stats.RecordFreeObject(string_size, SpaceType::SPACE_TYPE_OBJECT);
77 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2, stats.GetFootprint(SpaceType::SPACE_TYPE_OBJECT));
78 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2 + string_size, stats.GetAllocated(SpaceType::SPACE_TYPE_OBJECT));
79 ASSERT_EQ(string_size, stats.GetFreed(SpaceType::SPACE_TYPE_OBJECT));
80 }
81
TEST_F(MemStatsAdditionalInfoTest,AdditionalStatistic)82 TEST_F(MemStatsAdditionalInfoTest, AdditionalStatistic)
83 {
84 PandaVM *vm = thread_->GetVM();
85 std::string simple_string = "smallData";
86 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
87 [[maybe_unused]] coretypes::String *string_object = coretypes::String::CreateFromMUtf8(
88 reinterpret_cast<const uint8_t *>(&simple_string[0]), simple_string.length(), ctx, vm);
89 [[maybe_unused]] HandleScope<ObjectHeader *> scope(thread_);
90 [[maybe_unused]] VMHandle<ObjectHeader> handle(thread_, string_object);
91 #ifndef NDEBUG
92 Class *stringClass = Runtime::GetCurrent()->GetClassLinker()->GetExtension(ctx)->GetClassRoot(ClassRoot::STRING);
93 auto statistics = thread_->GetVM()->GetMemStats()->GetStatistics(vm->GetHeapManager());
94 // allocated
95 ASSERT_TRUE(statistics.find(stringClass->GetName()) != std::string::npos);
96 ASSERT_TRUE(statistics.find("footprint") != std::string::npos);
97 ASSERT_TRUE(statistics.find("1") != std::string::npos);
98 #endif
99 }
100
101 // test correct pauses measurment
TEST_F(MemStatsAdditionalInfoTest,GCPhaseTimeTest)102 TEST_F(MemStatsAdditionalInfoTest, GCPhaseTimeTest)
103 {
104 // pauses in milliseconds
105 constexpr uint64_t PAUSES[] = {20, 10, 30};
106 constexpr uint64_t MIN_PAUSE = 10;
107 constexpr uint64_t MAX_PAUSE = 30;
108 constexpr uint64_t TOTAL_PAUSE = 60;
109 constexpr uint PAUSES_COUNT = 3;
110 constexpr uint64_t AVG_PAUSE = TOTAL_PAUSE / PAUSES_COUNT;
111
112 MemStatsAdditionalInfo stats;
113 for (uint i = 0; i < PAUSES_COUNT; i++) {
114 for (uint ph = 0; ph < static_cast<uint>(GCPhase::GC_PHASE_LAST); ph++) {
115 stats.RecordGCPhaseStart(ToGCPhase(ph));
116 std::this_thread::sleep_for(std::chrono::milliseconds(static_cast<long int>(PAUSES[i])));
117 stats.RecordGCPhaseEnd();
118 }
119 }
120
121 GCPhase phase;
122 for (uint ph = 0; ph < static_cast<uint>(GCPhase::GC_PHASE_LAST); ph++) {
123 phase = ToGCPhase(ph);
124 ASSERT_LE(MIN_PAUSE, stats.GetMinGCPhaseTime(phase));
125 ASSERT_LE(MAX_PAUSE, stats.GetMaxGCPhaseTime(phase));
126 ASSERT_LE(AVG_PAUSE, stats.GetAverageGCPhaseTime(phase));
127 ASSERT_LE(TOTAL_PAUSE, stats.GetTotalGCPhaseTime(phase));
128
129 ASSERT_LE(stats.GetMinGCPhaseTime(phase), stats.GetAverageGCPhaseTime(phase));
130 ASSERT_LE(stats.GetAverageGCPhaseTime(phase), stats.GetMaxGCPhaseTime(phase));
131 ASSERT_LE(stats.GetMaxGCPhaseTime(phase), stats.GetTotalGCPhaseTime(phase));
132 }
133
134 // test empty case
135 MemStatsAdditionalInfo stats_empty;
136 for (uint ph = 0; ph < static_cast<uint>(GCPhase::GC_PHASE_LAST); ph++) {
137 ASSERT_EQ(0, stats_empty.GetMinGCPhaseTime(ToGCPhase(ph)));
138 ASSERT_EQ(0, stats_empty.GetMaxGCPhaseTime(ToGCPhase(ph)));
139 ASSERT_EQ(0, stats_empty.GetAverageGCPhaseTime(ToGCPhase(ph)));
140 ASSERT_EQ(0, stats_empty.GetTotalGCPhaseTime(ToGCPhase(ph)));
141 }
142 }
143
144 } // namespace panda::mem::test
145