1 /*
2 * Copyright (c) 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 #include <gtest/gtest.h>
16 #include <iostream>
17 #include <map>
18 #include <sstream>
19 #include <unistd.h>
20 #include <vector>
21 #include "dump_utils.h"
22 #include "executor/memory/get_hardware_info.h"
23 #include "executor/memory/get_process_info.h"
24 #include "executor/memory/get_kernel_info.h"
25 #include "executor/memory/memory_info.h"
26 #include "executor/memory/memory_filter.h"
27 #include "executor/memory/memory_util.h"
28 #include "executor/memory/parse/parse_ashmem_info.h"
29 #include "executor/memory/parse/parse_meminfo.h"
30 #include "executor/memory/parse/parse_smaps_info.h"
31 #include "executor/memory/parse/parse_smaps_rollup_info.h"
32 #include "executor/memory/smaps_memory_info.h"
33 #include "hidumper_test_utils.h"
34 #include "memory_collector.h"
35 #include "string_ex.h"
36
37 using namespace std;
38 using namespace testing::ext;
39 namespace OHOS {
40 namespace HiviewDFX {
41 const int INIT_PID = 1;
42 const int INDEX = 1;
43 const uint64_t INVALID_PID = 0;
44 const int BUFFER_SIZE = 1024;
45 constexpr int TEST_PSS_VALUE = 100;
46 const std::vector<int> pssValues = {1000, 1050, 1020, 1030, 1010, 1020, 1040, 1050, 1030, 1060, 1010};
47 string NULL_STR = "";
48 const string FIRST_ROW_TMP = " Pss Shared Shared Private";
49 const string FIRST_ROW = FIRST_ROW_TMP +
50 " Private Swap SwapPss Heap Heap Heap ";
51 const string SECOND_ROW_TMP =
52 " Total Clean Dirty Clean Dirty";
53 const string SECOND_ROW = SECOND_ROW_TMP +
54 " Total Total Size Alloc Free ";
55 const string THIRD_ROW_TMP =
56 " ( kB ) ( kB ) ( kB ) ( kB ) ( kB )";
57 const string THIRD_ROW = THIRD_ROW_TMP +
58 " ( kB ) ( kB ) ( kB ) ( kB ) ( kB ) ";
59 const string FOURTH_ROW_TMP =
60 " ---------------------------------------------------------------------------";
61 const string FOURTH_ROW = FOURTH_ROW_TMP +
62 "---------------------------------------------------------------------------";
63 const vector<string> MEMEORY_TITLE_VEC = {FIRST_ROW, SECOND_ROW, THIRD_ROW, FOURTH_ROW};
64
65 const std::map<MemoryItemType, MemoryClass> TYPE_TO_CLASS_MAP = {
66 {MemoryItemType::MEMORY_ITEM_ENTITY_DB, MemoryClass::MEMORY_CLASS_DB},
67 {MemoryItemType::MEMORY_ITEM_ENTITY_DB_SHM, MemoryClass::MEMORY_CLASS_DB},
68 {MemoryItemType::MEMORY_ITEM_ENTITY_HAP, MemoryClass::MEMORY_CLASS_OTHER},
69 {MemoryItemType::MEMORY_ITEM_ENTITY_HSP, MemoryClass::MEMORY_CLASS_OTHER},
70 {MemoryItemType::MEMORY_ITEM_ENTITY_SO, MemoryClass::MEMORY_CLASS_SO},
71 {MemoryItemType::MEMORY_ITEM_ENTITY_SO1, MemoryClass::MEMORY_CLASS_SO},
72 {MemoryItemType::MEMORY_ITEM_ENTITY_TTF, MemoryClass::MEMORY_CLASS_TTF},
73 {MemoryItemType::MEMORY_ITEM_ENTITY_DEV_PARAMETER, MemoryClass::MEMORY_CLASS_DEV},
74 {MemoryItemType::MEMORY_ITEM_ENTITY_DEV_OTHER, MemoryClass::MEMORY_CLASS_DEV},
75 {MemoryItemType::MEMORY_ITEM_ENTITY_DATA_STORAGE, MemoryClass::MEMORY_CLASS_HAP},
76 {MemoryItemType::MEMORY_ITEM_ENTITY_DMABUF, MemoryClass::MEMORY_CLASS_DMABUF},
77 {MemoryItemType::MEMORY_ITEM_ENTITY_OTHER, MemoryClass::MEMORY_CLASS_OTHER},
78 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_INODE, MemoryClass::MEMORY_CLASS_OTHER},
79 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_ARKTS_CODE, MemoryClass::MEMORY_CLASS_OTHER},
80 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_ARKTS_HEAP, MemoryClass::MEMORY_CLASS_ARK_TS_HEAP},
81 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_GUARD, MemoryClass::MEMORY_CLASS_GUARD},
82 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_BSS, MemoryClass::MEMORY_CLASS_OTHER},
83 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_BRK, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
84 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
85 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC_META, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
86 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_JEMALLOC_TSD, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
87 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_META, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
88 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_MMAP, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
89 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_NATIVE_HEAP_OTHER, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
90 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_SIGNAL_STACK, MemoryClass::MEMORY_CLASS_STACK},
91 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_STACK, MemoryClass::MEMORY_CLASS_STACK},
92 {MemoryItemType::MEMORY_ITEM_TYPE_ANON_V8, MemoryClass::MEMORY_CLASS_OTHER},
93 {MemoryItemType::MEMORY_ITEM_TYPE_ANONYMOUS_OTHER, MemoryClass::MEMORY_CLASS_OTHER},
94 {MemoryItemType::MEMORY_ITEM_TYPE_CONTIGUOUS, MemoryClass::MEMORY_CLASS_OTHER},
95 {MemoryItemType::MEMORY_ITEM_TYPE_COPAGE, MemoryClass::MEMORY_CLASS_OTHER},
96 {MemoryItemType::MEMORY_ITEM_TYPE_FILE, MemoryClass::MEMORY_CLASS_OTHER},
97 {MemoryItemType::MEMORY_ITEM_TYPE_GUARD, MemoryClass::MEMORY_CLASS_OTHER},
98 {MemoryItemType::MEMORY_ITEM_TYPE_HEAP, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
99 {MemoryItemType::MEMORY_ITEM_TYPE_IO, MemoryClass::MEMORY_CLASS_OTHER},
100 {MemoryItemType::MEMORY_ITEM_TYPE_KSHARE, MemoryClass::MEMORY_CLASS_OTHER},
101 {MemoryItemType::MEMORY_ITEM_TYPE_MALLOC, MemoryClass::MEMORY_CLASS_NATIVE_HEAP},
102 {MemoryItemType::MEMORY_ITEM_TYPE_PREHISTORIC, MemoryClass::MEMORY_CLASS_OTHER},
103 {MemoryItemType::MEMORY_ITEM_TYPE_RESERVE, MemoryClass::MEMORY_CLASS_OTHER},
104 {MemoryItemType::MEMORY_ITEM_TYPE_SHMM, MemoryClass::MEMORY_CLASS_OTHER},
105 {MemoryItemType::MEMORY_ITEM_TYPE_STACK, MemoryClass::MEMORY_CLASS_STACK},
106 {MemoryItemType::MEMORY_ITEM_TYPE_UNKNOWN, MemoryClass::MEMORY_CLASS_OTHER},
107 {MemoryItemType::MEMORY_ITEM_TYPE_VNODES, MemoryClass::MEMORY_CLASS_OTHER},
108 {MemoryItemType::MEMORY_ITEM_TYPE_OTHER, MemoryClass::MEMORY_CLASS_OTHER},
109 {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GL, MemoryClass::MEMORY_CLASS_GRAPH},
110 {MemoryItemType::MEMORY_ITEM_TYPE_GRAPH_GRAPHICS, MemoryClass::MEMORY_CLASS_GRAPH},
111 };
112
113 const std::vector<std::pair<std::string, std::string>> TITLE_AND_VALUE = {
114 {"GL", "100"},
115 {"Graph", "100"},
116 {"ark ts heap", "100"},
117 {"guard", "100"},
118 {"native heap", "900"},
119 {".hap", "100"},
120 {"AnonPage other", "1100"},
121 {"stack", "300"},
122 {".db", "200"},
123 {".so", "200"},
124 {"dev", "200"},
125 {"dmabuf", "100"},
126 {".ttf", "100"},
127 {"FilePage other", "900"},
128 {"---------------", ""},
129 {"Total", "4500"},
130 {"\n", ""},
131 {"native heap:", ""},
132 {"heap", "100"},
133 {"jemalloc meta", "100"},
134 {"jemalloc heap", "100"},
135 {"brk heap", "100"},
136 {"musl heap", "100"},
137 {"mmap heap", "100"},
138 };
139 using ValueMap = std::map<std::string, uint64_t>;
140 using GroupMap = std::map<std::string, ValueMap>;
141 using StringMatrix = std::shared_ptr<std::vector<std::vector<std::string>>>;
142
143 class HidumperMemoryTest : public testing::Test {
144 public:
145 static void SetUpTestCase(void);
146 static void TearDownTestCase(void);
147 void SetUp();
148 void TearDown();
149 void CreateProcessMemoryDetail(ProcessMemoryDetail& processMemoryDetail);
150 void GetMemoryDetail(MemoryDetail& detail);
151 bool CheckMemoryPrint(const std::string& title, const std::string& value, const std::string& line);
152 };
153
SetUpTestCase(void)154 void HidumperMemoryTest::SetUpTestCase(void)
155 {
156 }
TearDownTestCase(void)157 void HidumperMemoryTest::TearDownTestCase(void)
158 {
159 }
SetUp(void)160 void HidumperMemoryTest::SetUp(void)
161 {
162 }
TearDown(void)163 void HidumperMemoryTest::TearDown(void)
164 {
165 }
166
CreateProcessMemoryDetail(ProcessMemoryDetail & processMemoryDetail)167 void HidumperMemoryTest::CreateProcessMemoryDetail(ProcessMemoryDetail& processMemoryDetail)
168 {
169 processMemoryDetail.pid = -1;
170 processMemoryDetail.name = "test";
171 std::vector<MemoryDetail> details;
172 for (int i = 0; i <= static_cast<int>(MemoryClass::MEMORY_CLASS_OTHER); i++) {
173 MemoryDetail detail;
174 detail.memoryClass = static_cast<MemoryClass>(i);
175 GetMemoryDetail(detail);
176 details.push_back(detail);
177 processMemoryDetail.totalRss += detail.totalRss;
178 processMemoryDetail.totalPss += detail.totalPss;
179 processMemoryDetail.totalSwapPss += detail.totalSwapPss;
180 processMemoryDetail.totalSwap += detail.totalSwap;
181 processMemoryDetail.totalAllPss += detail.totalAllPss;
182 processMemoryDetail.totalAllSwap += detail.totalAllSwap;
183 processMemoryDetail.totalSharedDirty += detail.totalSharedDirty;
184 processMemoryDetail.totalPrivateDirty += detail.totalPrivateDirty;
185 processMemoryDetail.totalSharedClean += detail.totalSharedClean;
186 processMemoryDetail.totalPrivateClean += detail.totalPrivateClean;
187 }
188 processMemoryDetail.details = details;
189 }
190
GetMemoryDetail(MemoryDetail & detail)191 void HidumperMemoryTest::GetMemoryDetail(MemoryDetail& detail)
192 {
193 std::vector<MemoryItem> items;
194 int first = 1; // 1 is the filePage of iNode
195 int second = 2; // 2 is the class of iNode
196 for (int j = 0; j <= static_cast<int>(MemoryItemType::MEMORY_ITEM_TYPE_OTHER); j++) {
197 MemoryItem item;
198 item.type = static_cast<MemoryItemType>(j);
199 MemoryClass tempClass = TYPE_TO_CLASS_MAP.find(item.type) != TYPE_TO_CLASS_MAP.end()
200 ? TYPE_TO_CLASS_MAP.at(item.type) : MemoryClass::MEMORY_CLASS_OTHER;
201 if (tempClass != detail.memoryClass) {
202 continue;
203 }
204 item.iNode = (detail.memoryClass == MemoryClass::MEMORY_CLASS_OTHER) && (j % second == 0) ? 0 : first;
205 item.rss = TEST_PSS_VALUE;
206 item.pss = TEST_PSS_VALUE;
207 item.swapPss = TEST_PSS_VALUE;
208 item.swap = TEST_PSS_VALUE;
209 item.allPss = TEST_PSS_VALUE;
210 item.allSwap = TEST_PSS_VALUE;
211 item.sharedDirty = TEST_PSS_VALUE;
212 tempClass == MemoryClass::MEMORY_CLASS_GRAPH ? item.privateDirty = 0 : item.privateDirty = TEST_PSS_VALUE;
213 item.sharedClean = TEST_PSS_VALUE;
214 item.privateClean = TEST_PSS_VALUE;
215 items.push_back(item);
216
217 detail.totalRss += item.rss;
218 detail.totalPss += item.pss;
219 detail.totalSwapPss += item.swapPss;
220 detail.totalSwap += item.swap;
221 detail.totalAllPss += item.allPss;
222 detail.totalAllSwap += item.allSwap;
223 detail.totalSharedDirty += item.sharedDirty;
224 detail.totalPrivateDirty += item.privateDirty;
225 detail.totalSharedClean += item.sharedClean;
226 detail.totalPrivateClean += item.privateClean;
227 }
228 detail.items = items;
229 }
230
CheckMemoryPrint(const std::string & title,const std::string & value,const std::string & line)231 bool HidumperMemoryTest::CheckMemoryPrint(const std::string& title, const std::string& value, const std::string& line)
232 {
233 if (line.find(title) == std::string::npos) {
234 std::cout << "data error, line:" << line << " title:" << title << std::endl;
235 return false;
236 }
237 std::string valueStr = "";
238 valueStr = line.substr(line.find(title) + title.length(), line.length());
239 if (title == "GL" || title == "Graph") {
240 std::string graphPss = HidumperTestUtils::GetInstance().GetValueInLine(valueStr, 0);
241 std::string graphPrivateDirty = HidumperTestUtils::GetInstance().GetValueInLine(valueStr, 4);
242 if (graphPss != value || graphPrivateDirty != value) {
243 std::cout << "graphPss:" << graphPss << " graphPrivateDirty:" << graphPrivateDirty << std::endl;
244 return false;
245 } else {
246 return true;
247 }
248 }
249 // [Pss Total] ... [SwapPss Total] is 0-6 line
250 for (int i = 0; i <= 6; i++) {
251 std::string tmp = HidumperTestUtils::GetInstance().GetValueInLine(valueStr, i);
252 if (!IsNumericStr(tmp)) {
253 continue;
254 }
255 if (tmp != value) {
256 std::cout << "tmp != value" << std::endl;
257 return false;
258 }
259 }
260 return true;
261 }
262
263 /**
264 * @tc.name: MemoryParse001
265 * @tc.desc: Test ParseMeminfo invalid ret.
266 * @tc.type: FUNC
267 */
268 HWTEST_F(HidumperMemoryTest, MemoryParse001, TestSize.Level1)
269 {
270 unique_ptr<OHOS::HiviewDFX::ParseMeminfo> parseMeminfo = make_unique<OHOS::HiviewDFX::ParseMeminfo>();
271 ValueMap result;
272 parseMeminfo->SetData("", result);
273 ASSERT_EQ(result.size(), 0);
274 }
275
276
277 /**
278 * @tc.name: ParseSmapsRollupInfo001
279 * @tc.desc: Test ParseSmapsRollupInfo invalid ret.
280 * @tc.type: FUNC
281 */
282 HWTEST_F(HidumperMemoryTest, ParseSmapsRollupInfo001, TestSize.Level1)
283 {
284 unique_ptr<OHOS::HiviewDFX::ParseSmapsRollupInfo> parseSmapsRollup =
285 make_unique<OHOS::HiviewDFX::ParseSmapsRollupInfo>();
286 MemInfoData::MemInfo memInfo;
287 parseSmapsRollup->GetValue("RSS", memInfo);
288 parseSmapsRollup->GetValue("PSS", memInfo);
289 parseSmapsRollup->GetValue("Size", memInfo);
290 ASSERT_TRUE(memInfo.rss == 0);
291 }
292
293 /**
294 * @tc.name: SmapsMemoryInfo001
295 * @tc.desc: Test SmapsMemoryInfo ret.
296 * @tc.type: FUNC
297 */
298 HWTEST_F(HidumperMemoryTest, SmapsMemoryInfo001, TestSize.Level1)
299 {
300 shared_ptr<OHOS::HiviewDFX::SmapsMemoryInfo> smapsMemoryInfo =
301 make_shared<OHOS::HiviewDFX::SmapsMemoryInfo>();
302 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
303 ASSERT_TRUE(smapsMemoryInfo->ShowMemorySmapsByPid(INIT_PID, result, true));
304 ASSERT_FALSE(smapsMemoryInfo->ShowMemorySmapsByPid(INVALID_PID, result, true));
305 }
306
307 /**
308 * @tc.name: GetHardwareInfo001
309 * @tc.desc: Test GetHardwareInfo ret.
310 * @tc.type: FUNC
311 */
312 HWTEST_F(HidumperMemoryTest, GetHardwareInfo001, TestSize.Level1)
313 {
314 unique_ptr<OHOS::HiviewDFX::GetHardwareInfo> getHardwareInfo =
315 make_unique<OHOS::HiviewDFX::GetHardwareInfo>();
316 getHardwareInfo->GetResverRegPath(NULL_STR);
317 vector<string> paths;
318 uint64_t value = getHardwareInfo->CalcHardware(paths);
319 ASSERT_TRUE(value == 0);
320 size_t groupSize = 0;
321 getHardwareInfo->GetGroupOfPaths(groupSize, groupSize, paths, paths);
322 ASSERT_TRUE(paths.size() == 0);
323 }
324
325 /**
326 * @tc.name: MemoryInfo001
327 * @tc.desc: Test MemoryInfo ret.
328 * @tc.type: FUNC
329 */
330 HWTEST_F(HidumperMemoryTest, MemoryInfo001, TestSize.Level1)
331 {
332 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
333 make_unique<OHOS::HiviewDFX::MemoryInfo>();
334 int value = static_cast<int>(memoryInfo->GetProcValue(INVALID_PID, NULL_STR));
335 ASSERT_EQ(value, 0);
336 value = static_cast<int>(memoryInfo->GetVss(INVALID_PID));
337 ASSERT_EQ(value, 0);
338 MemInfoData::MemUsage usage;
339 ASSERT_FALSE(memoryInfo->GetMemByProcessPid(INVALID_PID, usage));
340 #ifdef HIDUMPER_MEMMGR_ENABLE
341 memoryInfo->GetProcessAdjLabel(INVALID_PID);
342 #endif
343 }
344
345 /**
346 * @tc.name: MemoryInfo002
347 * @tc.desc: Test MemoryInfo ret.
348 * @tc.type: FUNC
349 */
350 HWTEST_F(HidumperMemoryTest, MemoryInfo002, TestSize.Level1)
351 {
352 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
353 make_unique<OHOS::HiviewDFX::MemoryInfo>();
354 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
355 ValueMap memInfo;
356 memoryInfo->GetPurgTotal(memInfo, result);
357 ASSERT_TRUE(memInfo.size() == 0);
358 }
359
360 /**
361 * @tc.name: MemoryInfo003
362 * @tc.desc: Test empty unique_ptr.
363 * @tc.type: FUNC
364 */
365 HWTEST_F(HidumperMemoryTest, MemoryInfo003, TestSize.Level1)
366 {
367 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
368 make_unique<OHOS::HiviewDFX::MemoryInfo>();
369 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
370 unique_ptr<MemoryDetail> nativeHeapDetail = nullptr;
371 memoryInfo->GetNativeHeap(nativeHeapDetail, result);
372 ASSERT_TRUE(result->size() == 0);
373
374 unique_ptr<MemoryDetail> anonPageDetail = make_unique<MemoryDetail>();
375 unique_ptr<MemoryDetail> filePageDetail = make_unique<MemoryDetail>();
376 unique_ptr<MemoryDetail> tempDetail = nullptr;
377 memoryInfo->UpdatePageDetail(anonPageDetail, filePageDetail, tempDetail);
378 ASSERT_TRUE(anonPageDetail);
379 ASSERT_TRUE(filePageDetail);
380
381 std::unique_ptr<MallHeapInfo> mallocHeapInfo = std::make_unique<MallHeapInfo>();
382 unique_ptr<MemoryDetail> detail = nullptr;
383 memoryInfo->SetDetailRet(MEMINFO_FILEPAGE_OTHER, detail, mallocHeapInfo, result);
384 ASSERT_TRUE(result->size() == 0);
385 unique_ptr<ProcessMemoryDetail> processMemoryDetail = nullptr;
386 memoryInfo->UpdateTotalDetail(processMemoryDetail, mallocHeapInfo, result);
387 ASSERT_TRUE(result->size() == 0);
388 }
389
390 /**
391 * @tc.name: MemoryInfo004
392 * @tc.desc: Test error GroupMap.
393 * @tc.type: FUNC
394 */
395 HWTEST_F(HidumperMemoryTest, MemoryInfo004, TestSize.Level1)
396 {
397 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
398 make_unique<OHOS::HiviewDFX::MemoryInfo>();
399 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
400 GroupMap groupMap;
401 ValueMap memInfo;
402 groupMap.insert(pair<string, ValueMap>("test", memInfo));
403 memoryInfo->GetPssTotal(groupMap, result);
404 ASSERT_TRUE(result->size() == 0);
405 }
406
407 /**
408 * @tc.name: MemoryInfo005
409 * @tc.desc: Test about ashmem.
410 * @tc.type: FUNC
411 */
412 HWTEST_F(HidumperMemoryTest, MemoryInfo005, TestSize.Level1)
413 {
414 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
415 make_unique<OHOS::HiviewDFX::MemoryInfo>();
416 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
417 memoryInfo->GetAshmem(INIT_PID, result, true);
418 ASSERT_TRUE(result->size() == 0);
419
420 FILE* file = popen("pidof render_service", "r");
421 char buffer[BUFFER_SIZE];
422 if (file) {
423 if (fgets(buffer, sizeof(buffer), file) != nullptr) {};
424 pclose(file);
425 }
426 int rsPid = strtol(buffer, nullptr, 10);
427 memoryInfo->GetAshmem(rsPid, result, true);
428 if (DumpUtils::IsHmKernel()) {
429 ASSERT_TRUE(result->size() != 0);
430 }
431 }
432
433 /**
434 * @tc.name: MemoryInfo006
435 * @tc.desc: Test CalculateStars ret.
436 * @tc.type: FUNC
437 */
438 HWTEST_F(HidumperMemoryTest, MemoryInfo006, TestSize.Level1)
439 {
440 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
441 make_unique<OHOS::HiviewDFX::MemoryInfo>();
442 int value = static_cast<int>(memoryInfo->CalculateStars(pssValues, INDEX));
443 ASSERT_TRUE(value != 0);
444 }
445
446 /**
447 * @tc.name: MemoryInfo007
448 * @tc.desc: Test GenerateTimestamps ret.
449 * @tc.type: FUNC
450 */
451 HWTEST_F(HidumperMemoryTest, MemoryInfo007, TestSize.Level1)
452 {
453 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
454 make_unique<OHOS::HiviewDFX::MemoryInfo>();
455 std::string timeStamps = memoryInfo->GenerateTimestamps(pssValues);
456 ASSERT_TRUE(timeStamps.size() != 0);
457 }
458
459 /**
460 * @tc.name: MemoryInfo008
461 * @tc.desc: Test GenerateLine ret.
462 * @tc.type: FUNC
463 */
464 HWTEST_F(HidumperMemoryTest, MemoryInfo008, TestSize.Level1)
465 {
466 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
467 make_unique<OHOS::HiviewDFX::MemoryInfo>();
468 std::string generateLine = memoryInfo->GenerateLine(pssValues, INDEX);
469 ASSERT_TRUE(generateLine.size() != 0);
470 }
471
472 /**
473 * @tc.name: MemoryInfo009
474 * @tc.desc: Test CalculateMaxIdex index.
475 * @tc.type: FUNC
476 */
477 HWTEST_F(HidumperMemoryTest, MemoryInfo009, TestSize.Level1)
478 {
479 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
480 make_unique<OHOS::HiviewDFX::MemoryInfo>();
481 int index = 0;
482 memoryInfo->CalculateMaxIdex(pssValues, &index);
483 ASSERT_TRUE(index != 0);
484 }
485
486 /**
487 * @tc.name: MemoryInfo010
488 * @tc.desc: Test PrintMemoryInfo prevLineCount.
489 * @tc.type: FUNC
490 */
491 HWTEST_F(HidumperMemoryTest, MemoryInfo010, TestSize.Level1)
492 {
493 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
494 make_unique<OHOS::HiviewDFX::MemoryInfo>();
495 int prevLineCount = 0;
496 memoryInfo->PrintMemoryInfo(pssValues, &prevLineCount);
497 ASSERT_TRUE(prevLineCount != 0);
498 prevLineCount = 1;
499 memoryInfo->PrintMemoryInfo(pssValues, &prevLineCount);
500 ASSERT_TRUE(prevLineCount != 0);
501 }
502
503 /**
504 * @tc.name: MemoryInfo011
505 * @tc.desc: Test PrintMemoryInfo GetScoreAdj.
506 * @tc.type: FUNC
507 */
508 HWTEST_F(HidumperMemoryTest, MemoryInfo011, TestSize.Level1)
509 {
510 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
511 make_unique<OHOS::HiviewDFX::MemoryInfo>();
512 int ret = memoryInfo->GetScoreAdj(1);
513 ASSERT_TRUE(ret != -1);
514 }
515
516 /**
517 * @tc.name: MemoryInfo012
518 * @tc.desc: Test about dmabuf.
519 * @tc.type: FUNC
520 */
521 HWTEST_F(HidumperMemoryTest, MemoryInfo012, TestSize.Level1)
522 {
523 unique_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
524 make_unique<OHOS::HiviewDFX::MemoryInfo>();
525 shared_ptr<vector<vector<string>>> result = make_shared<vector<vector<string>>>();
526 string path = "/proc/" + to_string(INIT_PID) + "/mm_dmabuf_info";
527 if (access(path.c_str(), F_OK) == 0) {
528 memoryInfo->GetDmaBuf(INIT_PID, result, true);
529 ASSERT_TRUE(result->size() == 0);
530 system("aa start -a com.ohos.contacts.MainAbility -b com.ohos.contacts");
531 sleep(3);
532 pid_t cocPid = HidumperTestUtils::GetInstance().GetPidByName("com.ohos.contacts");
533 ASSERT_TRUE(cocPid != -1);
534 memoryInfo->GetDmaBuf(cocPid, result, true);
535 ASSERT_TRUE(result->size() != 0);
536 }
537 }
538
539 /**
540 * @tc.name: GetProcessInfo001
541 * @tc.desc: Test GetProcessInfo ret.
542 * @tc.type: FUNC
543 */
544 HWTEST_F(HidumperMemoryTest, GetProcessInfo001, TestSize.Level1)
545 {
546 unique_ptr<OHOS::HiviewDFX::GetProcessInfo> getProcessInfo = make_unique<OHOS::HiviewDFX::GetProcessInfo>();
547 GroupMap groupMap;
548 ValueMap memInfo;
549 uint64_t pid = 0;
550 memInfo.insert(pair<string, uint64_t>("Name", pid));
551 groupMap.insert(pair<string, ValueMap>("test", memInfo));
552 int value = static_cast<int>(getProcessInfo->GetProcess(groupMap));
553 ASSERT_EQ(value, 0);
554 }
555
556 /**
557 * @tc.name: GetKernelInfo001
558 * @tc.desc: Test GetKernelInfo ret.
559 * @tc.type: FUNC
560 */
561 HWTEST_F(HidumperMemoryTest, GetKernelInfo001, TestSize.Level1)
562 {
563 unique_ptr<OHOS::HiviewDFX::GetKernelInfo> getGetKernelInfo = make_unique<OHOS::HiviewDFX::GetKernelInfo>();
564 ValueMap memInfo;
565 uint64_t value = 0;
566 ASSERT_TRUE(getGetKernelInfo->GetKernel(memInfo, value));
567 }
568
569
570 /**
571 * @tc.name: GraphicMemory001
572 * @tc.desc: Test GetGraphicMemory.
573 * @tc.type: FUNC
574 */
575 HWTEST_F(HidumperMemoryTest, GraphicMemory001, TestSize.Level1)
576 {
577 shared_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
578 make_shared<OHOS::HiviewDFX::MemoryInfo>();
579 MemInfoData::GraphicsMemory graphicsMemory;
580 FILE* file = popen("pidof render_service", "r");
581 char buffer[BUFFER_SIZE];
582 if (file) {
583 if (fgets(buffer, sizeof(buffer), file) != nullptr) {};
584 pclose(file);
585 }
586 int pid = strtol(buffer, nullptr, 10);
587 int ret = memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GRAPH);
588 ASSERT_TRUE(ret);
589 memoryInfo->GetGraphicsMemory(pid, graphicsMemory, GraphicType::GL);
590 ASSERT_TRUE(ret);
591 }
592
593 /**
594 * @tc.name: MemoryTitle001
595 * @tc.desc: Test memory title.
596 * @tc.type: FUNC
597 */
598 HWTEST_F(HidumperMemoryTest, MemoryTitle001, TestSize.Level1)
599 {
600 shared_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
601 make_shared<OHOS::HiviewDFX::MemoryInfo>();
602 StringMatrix result = std::make_shared<std::vector<std::vector<std::string>>>();
603 memoryInfo->InsertMemoryTitle(result);
604 ASSERT_TRUE(result->size() == MEMEORY_TITLE_VEC.size());
605 std::vector<std::string> memoryTitle;
606 for (size_t i = 0; i < result->size(); i++) {
607 std::string title = "";
608 std::vector<std::string> line = result->at(i);
609 for (size_t j = 0; j < line.size(); j++) {
610 std::string str = line[j];
611 title += str;
612 }
613 memoryTitle.push_back(title);
614 }
615 ASSERT_TRUE(memoryTitle == MEMEORY_TITLE_VEC);
616 }
617
618 /**
619 * @tc.name: CheckMemoryData001
620 * @tc.desc: Test check memory data.
621 * @tc.type: FUNC
622 */
623 HWTEST_F(HidumperMemoryTest, CheckMemoryData001, TestSize.Level1)
624 {
625 shared_ptr<OHOS::HiviewDFX::MemoryInfo> memoryInfo =
626 make_shared<OHOS::HiviewDFX::MemoryInfo>();
627 StringMatrix result = std::make_shared<std::vector<std::vector<std::string>>>();
628 ProcessMemoryDetail processMemoryDetail;
629 CreateProcessMemoryDetail(processMemoryDetail);
630 unique_ptr<MemoryDetail> nativeHeapDetail = {nullptr};
631 unique_ptr<ProcessMemoryDetail> processMemoryDetailPtr = make_unique<ProcessMemoryDetail>(processMemoryDetail);
632 memoryInfo->UpdateResult(-1, processMemoryDetailPtr, nativeHeapDetail, result);
633 memoryInfo->GetNativeHeap(nativeHeapDetail, result);
634 ASSERT_TRUE(result->size() == TITLE_AND_VALUE.size());
635 for (size_t i = 0; i < result->size(); i++) {
636 std::vector<std::string> line = result->at(i);
637 std::stringstream outputSs;
638 bool isFind = false;
639 for (size_t j = 0; j < line.size(); j++) {
640 std::string str = line[j];
641 if (str == "\n" || str == "native heap:" || str == "---------------") {
642 isFind = true;
643 break;
644 }
645 outputSs << str;
646 }
647 if (isFind) {
648 continue;
649 }
650 ASSERT_TRUE(CheckMemoryPrint(TITLE_AND_VALUE[i].first, TITLE_AND_VALUE[i].second, outputSs.str()));
651 }
652 }
653
654 /**
655 * @tc.name: ParseAshmemInfo001
656 * @tc.desc: Test error ashmemInfo.
657 * @tc.type: FUNC
658 */
659 HWTEST_F(HidumperMemoryTest, ParseAshmemInfo001, TestSize.Level1)
660 {
661 unique_ptr<ParseAshmemInfo> parseAshmeminfo = make_unique<ParseAshmemInfo>();
662 std::unordered_map<std::string, int64_t> ashmemOverviewMap;
663 parseAshmeminfo->UpdateAshmemOverviewMap("", ashmemOverviewMap);
664 parseAshmeminfo->UpdateAshmemOverviewMap("test", ashmemOverviewMap);
665 parseAshmeminfo->UpdateAshmemOverviewMap("test[", ashmemOverviewMap);
666 parseAshmeminfo->UpdateAshmemOverviewMap("test]", ashmemOverviewMap);
667 parseAshmeminfo->UpdateAshmemOverviewMap("test][", ashmemOverviewMap);
668 parseAshmeminfo->UpdateAshmemOverviewMap("test[]", ashmemOverviewMap);
669 parseAshmeminfo->UpdateAshmemOverviewMap("test[], physical size is testSize", ashmemOverviewMap);
670 parseAshmeminfo->UpdateAshmemOverviewMap("test[test], physical size is 12345678912345678912", ashmemOverviewMap);
671 ASSERT_TRUE(ashmemOverviewMap.empty());
672 }
673 } // namespace HiviewDFX
674 } // namespace OHOS