1 /*
2 * Copyright (c) 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 #include <gtest/gtest.h>
16 #include <map>
17 #include <unistd.h>
18 #include <vector>
19 #include <v1_0/imemory_tracker_interface.h>
20
21 #define private public
22 #include "executor/memory_dumper.h"
23 #undef private
24 #include "dump_client_main.h"
25 #include "hdf_base.h"
26 #include "executor/memory/memory_filter.h"
27 #include "executor/memory/memory_util.h"
28 #include "hidumper_test_utils.h"
29
30 using namespace testing::ext;
31 using namespace OHOS::HDI::Memorytracker::V1_0;
32 namespace OHOS {
33 namespace HiviewDFX {
34 class MemoryDumperTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void MemoryDumperTest::SetUpTestCase(void)
43 {
44 }
TearDownTestCase(void)45 void MemoryDumperTest::TearDownTestCase(void)
46 {
47 }
SetUp(void)48 void MemoryDumperTest::SetUp(void)
49 {
50 }
TearDown(void)51 void MemoryDumperTest::TearDown(void)
52 {
53 }
54
55 /**
56 * @tc.name: MemoryDumperTest001
57 * @tc.desc: Test MemoryDumper has correct group.
58 * @tc.type: FUNC
59 * @tc.require: issueI5NWZQ
60 */
61 HWTEST_F(MemoryDumperTest, MemoryDumperTest001, TestSize.Level3)
62 {
63 std::string cmd = "hidumper --mem";
64 std::string str = "Anonymous Page";
65 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
66 }
67
68 /**
69 * @tc.name: MemoryDumperTest002
70 * @tc.desc: Test MemoryDumper has DMA group.
71 * @tc.type: FUNC
72 * @tc.require: issueI5NX04
73 */
74 HWTEST_F(MemoryDumperTest, MemoryDumperTest002, TestSize.Level3)
75 {
76 sptr<IMemoryTrackerInterface> memtrack = IMemoryTrackerInterface::Get();
77 if (memtrack == nullptr) {
78 return;
79 }
80 std::string cmd = "hidumper --mem";
81 std::string str = "DMA";
82 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
83 }
84
85 /**
86 * @tc.name: MemoryDumperTest003
87 * @tc.desc: Test MemoryDumper has correct group.
88 * @tc.type: FUNC
89 * @tc.require: issueI5NWZQ
90 */
91 HWTEST_F(MemoryDumperTest, MemoryDumperTest003, TestSize.Level3)
92 {
93 std::string cmd = "hidumper --mem 1";
94 std::string str = "Total";
95 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
96 str = "native heap:";
97 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
98 str = "Purgeable:";
99 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
100 str = "DMA:";
101 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
102 }
103
104 /**
105 * @tc.name: MemoryDumperTest004
106 * @tc.desc: Test MemoryDumper has GPU group.
107 * @tc.type: FUNC
108 * @tc.require: issueI5NWZQ
109 */
110 HWTEST_F(MemoryDumperTest, MemoryDumperTest004, TestSize.Level3)
111 {
112 std::string cmd = "hidumper --mem";
113 std::string str = "GPU";
114 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
115 }
116
117 /**
118 * @tc.name: MemoryDumperTest005
119 * @tc.desc: Test MemoryDumper has Purgeable group.
120 * @tc.type: FUNC
121 * @tc.require: issueI5NWZQ
122 */
123 HWTEST_F(MemoryDumperTest, MemoryDumperTest005, TestSize.Level3)
124 {
125 std::string cmd = "hidumper --mem";
126 std::string str = "Total Purgeable";
127 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
128 }
129
130 /**
131 * @tc.name: MemoryDumperTest006
132 * @tc.desc: Test MemoryDumper has Dma/PurgSum/PurgPin group.
133 * @tc.type: FUNC
134 * @tc.require: issueI5NWZQ
135 */
136 HWTEST_F(MemoryDumperTest, MemoryDumperTest006, TestSize.Level3)
137 {
138 std::string cmd = "hidumper --mem";
139 std::string str = "Dma PurgSum PurgPin";
140 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
141 }
142
143 /**
144 * @tc.name: MemoryUtilTest001
145 * @tc.desc: Test IsNameLine has correct ret.
146 * @tc.type: FUNC
147 */
148 HWTEST_F(MemoryDumperTest, MemoryUtilTest001, TestSize.Level1)
149 {
150 const std::string valueLine = "Rss: 24 kB";
151 std::string name;
152 uint64_t iNode = 0;
153 ASSERT_FALSE(MemoryUtil::GetInstance().IsNameLine(valueLine, name, iNode));
154 ASSERT_EQ(name, "");
155 const std::string nameLine = "ffb84000-ffba5000 rw-p 00000000 00:00 0 [stack]";
156 ASSERT_TRUE(MemoryUtil::GetInstance().IsNameLine(nameLine, name, iNode));
157 ASSERT_EQ(name, "[stack]");
158 }
159
160 /**
161 * @tc.name: MemoryUtilTest002
162 * @tc.desc: Test GetTypeAndValue has correct ret.
163 * @tc.type: FUNC
164 */
165 HWTEST_F(MemoryDumperTest, MemoryUtilTest002, TestSize.Level1)
166 {
167 std::string type;
168 uint64_t value = 0;
169 const std::string illegalStr = "aaaaaa";
170 ASSERT_FALSE(MemoryUtil::GetInstance().GetTypeAndValue(illegalStr, type, value));
171 const std::string valueStr = "MemTotal: 2010244 kB";
172 ASSERT_TRUE(MemoryUtil::GetInstance().GetTypeAndValue(valueStr, type, value));
173 ASSERT_EQ(type, "MemTotal");
174 ASSERT_EQ(value, 2010244);
175 }
176
177 /**
178 * @tc.name: MemoryUtilTest003
179 * @tc.desc: Test RunCMD has correct ret.
180 * @tc.type: FUNC
181 */
182 HWTEST_F(MemoryDumperTest, MemoryUtilTest003, TestSize.Level1)
183 {
184 const std::string cmd = "ps -ef";
185 std::vector<std::string> vec;
186 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
187 ASSERT_GT(vec.size(), 0);
188 }
189 } // namespace HiviewDFX
190 } // namespace OHOS