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
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 ark::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_ = ark::MTManagedThread::GetCurrent();
43 thread_->ManagedCodeBegin();
44 }
45
~MemStatsAdditionalInfoTest()46 ~MemStatsAdditionalInfoTest() override
47 {
48 thread_->ManagedCodeEnd();
49 Runtime::Destroy();
50 }
51
52 NO_COPY_SEMANTIC(MemStatsAdditionalInfoTest);
53 NO_MOVE_SEMANTIC(MemStatsAdditionalInfoTest);
54
55 protected:
56 // NOLINTNEXTLINE(misc-non-private-member-variables-in-classes)
57 ark::MTManagedThread *thread_;
58 };
59
TEST_F(MemStatsAdditionalInfoTest,HeapAllocatedMaxAndTotal)60 TEST_F(MemStatsAdditionalInfoTest, HeapAllocatedMaxAndTotal)
61 {
62 static constexpr size_t BYTES_ALLOC1 = 2;
63 static constexpr size_t BYTES_ALLOC2 = 5;
64 static constexpr size_t RAW_ALLOC1 = 15;
65
66 std::string simpleString = "smallData";
67 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
68 coretypes::String *stringObject = coretypes::String::CreateFromMUtf8(
69 reinterpret_cast<const uint8_t *>(&simpleString[0]), simpleString.length(), ctx, thread_->GetVM());
70
71 size_t stringSize = stringObject->ObjectSize();
72
73 MemStatsAdditionalInfo stats;
74 stats.RecordAllocateObject(BYTES_ALLOC1, SpaceType::SPACE_TYPE_OBJECT);
75 stats.RecordAllocateObject(BYTES_ALLOC2, SpaceType::SPACE_TYPE_OBJECT);
76 stats.RecordAllocateRaw(RAW_ALLOC1, SpaceType::SPACE_TYPE_INTERNAL);
77
78 stats.RecordAllocateObject(stringSize, SpaceType::SPACE_TYPE_OBJECT);
79 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2 + stringSize, stats.GetAllocated(SpaceType::SPACE_TYPE_OBJECT));
80 stats.RecordFreeObject(stringSize, SpaceType::SPACE_TYPE_OBJECT);
81 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2, stats.GetFootprint(SpaceType::SPACE_TYPE_OBJECT));
82 ASSERT_EQ(BYTES_ALLOC1 + BYTES_ALLOC2 + stringSize, stats.GetAllocated(SpaceType::SPACE_TYPE_OBJECT));
83 ASSERT_EQ(stringSize, stats.GetFreed(SpaceType::SPACE_TYPE_OBJECT));
84 }
85
TEST_F(MemStatsAdditionalInfoTest,AdditionalStatistic)86 TEST_F(MemStatsAdditionalInfoTest, AdditionalStatistic)
87 {
88 PandaVM *vm = thread_->GetVM();
89 std::string simpleString = "smallData";
90 LanguageContext ctx = Runtime::GetCurrent()->GetLanguageContext(panda_file::SourceLang::PANDA_ASSEMBLY);
91 [[maybe_unused]] coretypes::String *stringObject = coretypes::String::CreateFromMUtf8(
92 reinterpret_cast<const uint8_t *>(&simpleString[0]), simpleString.length(), ctx, vm);
93 [[maybe_unused]] HandleScope<ObjectHeader *> scope(thread_);
94 [[maybe_unused]] VMHandle<ObjectHeader> handle(thread_, stringObject);
95 #ifndef NDEBUG
96 Class *stringClass = Runtime::GetCurrent()->GetClassLinker()->GetExtension(ctx)->GetClassRoot(ClassRoot::STRING);
97 auto statistics = thread_->GetVM()->GetMemStats()->GetStatistics();
98 // allocated
99 ASSERT_TRUE(statistics.find(stringClass->GetName()) != std::string::npos);
100 ASSERT_TRUE(statistics.find("footprint") != std::string::npos);
101 ASSERT_TRUE(statistics.find('1') != std::string::npos);
102 #endif
103 }
104
105 } // namespace ark::mem::test
106