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