1 /* 2 * Copyright (c) 2023-2025 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 #include <iostream> 16 #include "common.h" 17 #include "mem_profiler_collector.h" 18 #include "native_memory_profiler_sa_client_manager.h" 19 #include <thread> 20 #include <gtest/gtest.h> 21 22 using namespace testing::ext; 23 using namespace OHOS::HiviewDFX; 24 using namespace OHOS::HiviewDFX::UCollectUtil; 25 using namespace OHOS::HiviewDFX::UCollect; 26 using namespace OHOS::Developtools::NativeDaemon; 27 28 const static std::string NATIVE_DAEMON_NAME("native_daemon"); 29 int g_nativeDaemonPid = 0; 30 constexpr int WAIT_EXIT_MILLS = 50; 31 constexpr int FINAL_TIME = 1000; 32 constexpr int EXIT_TIME = 21000; 33 constexpr int DURATION = 10; 34 constexpr int INTERVAL = 1; 35 constexpr int MEM_SIZE = 1024; 36 37 class MemProfilerCollectorTest : public testing::Test { 38 public: SetUp()39 void SetUp() {}; TearDown()40 void TearDown() {}; SetUpTestCase()41 static void SetUpTestCase() {}; TearDownTestCase()42 static void TearDownTestCase() {}; 43 }; 44 45 /** 46 * @tc.name: MemProfilerCollectorTest001 47 * @tc.desc: used to test MemProfilerCollector.Start 48 * @tc.type: FUNC 49 */ 50 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest001, TestSize.Level1) 51 { 52 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 53 collector->Prepare(); 54 MemoryProfilerConfig memoryProfilerConfig = { 55 .type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 56 .pid = 0, 57 .duration = DURATION, 58 .sampleInterval = INTERVAL, 59 }; 60 collector->Start(memoryProfilerConfig); 61 int time = 0; 62 while (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 63 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 64 time += WAIT_EXIT_MILLS; 65 } 66 ASSERT_TRUE(time < FINAL_TIME); 67 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 68 } 69 70 /** 71 * @tc.name: MemProfilerCollectorTest002 72 * @tc.desc: used to test MemProfilerCollector.Stop 73 * @tc.type: FUNC 74 */ 75 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest002, TestSize.Level1) 76 { 77 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 78 collector->Prepare(); 79 MemoryProfilerConfig memoryProfilerConfig = { 80 .type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 81 .pid = 0, 82 .duration = DURATION, 83 .sampleInterval = INTERVAL, 84 }; 85 collector->Start(memoryProfilerConfig); 86 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 87 collector->Stop(0); 88 int time = 0; 89 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 90 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 91 time += WAIT_EXIT_MILLS; 92 } 93 ASSERT_FALSE(time < FINAL_TIME); 94 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 95 collector->Prepare(); 96 memoryProfilerConfig.type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_CALL_STACK; 97 collector->Start(memoryProfilerConfig); 98 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 99 collector->Stop(0); 100 time = 0; 101 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 102 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 103 time += WAIT_EXIT_MILLS; 104 } 105 ASSERT_FALSE(time < FINAL_TIME); 106 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 107 } 108 109 /** 110 * @tc.name: MemProfilerCollectorTest003 111 * @tc.desc: used to test MemProfilerCollector.Stop 112 * @tc.type: FUNC 113 */ 114 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest003, TestSize.Level1) 115 { 116 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 117 collector->Prepare(); 118 MemoryProfilerConfig memoryProfilerConfig = { 119 .type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 120 .pid = 0, 121 .duration = DURATION, 122 .sampleInterval = INTERVAL, 123 }; 124 collector->Start(0, memoryProfilerConfig); 125 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 126 collector->Stop(0); 127 int time = 0; 128 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 129 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 130 time += WAIT_EXIT_MILLS; 131 } 132 ASSERT_FALSE(time < FINAL_TIME); 133 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 134 collector->Prepare(); 135 memoryProfilerConfig.type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_CALL_STACK; 136 collector->Start(0, memoryProfilerConfig); 137 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 138 collector->Stop(0); 139 time = 0; 140 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 141 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 142 time += WAIT_EXIT_MILLS; 143 } 144 ASSERT_FALSE(time < FINAL_TIME); 145 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 146 } 147 148 /** 149 * @tc.name: MemProfilerCollectorTest004 150 * @tc.desc: used to test MemProfilerCollector.Stop 151 * @tc.type: FUNC 152 */ 153 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest004, TestSize.Level1) 154 { 155 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 156 collector->Prepare(); 157 MemoryProfilerConfig memoryProfilerConfig = { 158 .type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_LIBRARY, 159 .duration = DURATION, 160 .sampleInterval = INTERVAL, 161 }; 162 collector->Start(0, true, memoryProfilerConfig); 163 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 164 collector->Stop(0); 165 int time = 0; 166 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 167 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 168 time += WAIT_EXIT_MILLS; 169 } 170 ASSERT_FALSE(time < FINAL_TIME); 171 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 172 collector->Prepare(); 173 memoryProfilerConfig.type = NativeMemoryProfilerSaClientManager::NativeMemProfilerType::MEM_PROFILER_CALL_STACK; 174 collector->Start(0, memoryProfilerConfig); 175 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 176 collector->Stop(0); 177 time = 0; 178 while (COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 179 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 180 time += WAIT_EXIT_MILLS; 181 } 182 ASSERT_FALSE(time < FINAL_TIME); 183 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 184 } 185 186 /** 187 * @tc.name: MemProfilerCollectorTest005 188 * @tc.desc: used to test MemProfilerCollector.Start 189 * @tc.type: FUNC 190 */ 191 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest005, TestSize.Level1) 192 { 193 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 194 collector->Prepare(); 195 UCollectUtil::SimplifiedMemConfig simplifiedMemConfig = { 196 .largestSize = MEM_SIZE, 197 .secondLargestSize = MEM_SIZE, 198 .maxGrowthSize = MEM_SIZE, 199 .sampleSize = MEM_SIZE, 200 }; 201 collector->Start(0, 1, simplifiedMemConfig); 202 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 203 collector->Stop(0); 204 int time = 0; 205 while (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 206 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 207 time += WAIT_EXIT_MILLS; 208 } 209 ASSERT_TRUE(time < FINAL_TIME); 210 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 211 } 212 213 /** 214 * @tc.name: MemProfilerCollectorTest006 215 * @tc.desc: used to test MemProfilerCollector.StartPrintSimplifiedNmd 216 * @tc.type: FUNC 217 */ 218 HWTEST_F(MemProfilerCollectorTest, MemProfilerCollectorTest006, TestSize.Level1) 219 { 220 std::shared_ptr<MemProfilerCollector> collector = MemProfilerCollector::Create(); 221 collector->Prepare(); 222 std::vector<UCollectUtil::SimplifiedMemStats> simplifiedMemStats; 223 collector->StartPrintSimplifiedNmd(1, simplifiedMemStats); 224 std::this_thread::sleep_for(std::chrono::milliseconds(FINAL_TIME)); 225 collector->Stop(0); 226 int time = 0; 227 while (!COMMON::IsProcessExist(NATIVE_DAEMON_NAME, g_nativeDaemonPid) && time < FINAL_TIME) { 228 std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_EXIT_MILLS)); 229 time += WAIT_EXIT_MILLS; 230 } 231 ASSERT_TRUE(time < FINAL_TIME); 232 std::this_thread::sleep_for(std::chrono::milliseconds(EXIT_TIME)); 233 }