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 std::string cmd = "hidumper --mem";
77 std::string str = "DMA";
78 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
79 }
80
81 /**
82 * @tc.name: MemoryDumperTest003
83 * @tc.desc: Test MemoryDumper has correct group.
84 * @tc.type: FUNC
85 * @tc.require: issueI5NWZQ
86 */
87 HWTEST_F(MemoryDumperTest, MemoryDumperTest003, TestSize.Level3)
88 {
89 std::string cmd = "hidumper --mem 1";
90 std::string str = "Total";
91 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
92 str = "native heap:";
93 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
94 str = "Purgeable:";
95 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
96 str = "DMA:";
97 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
98 }
99
100 /**
101 * @tc.name: MemoryDumperTest004
102 * @tc.desc: Test MemoryDumper has GPU group.
103 * @tc.type: FUNC
104 * @tc.require: issueI5NWZQ
105 */
106 HWTEST_F(MemoryDumperTest, MemoryDumperTest004, TestSize.Level3)
107 {
108 std::string cmd = "hidumper --mem";
109 std::string str = "GPU";
110 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
111 }
112
113 /**
114 * @tc.name: MemoryDumperTest005
115 * @tc.desc: Test MemoryDumper has Purgeable group.
116 * @tc.type: FUNC
117 * @tc.require: issueI5NWZQ
118 */
119 HWTEST_F(MemoryDumperTest, MemoryDumperTest005, TestSize.Level3)
120 {
121 std::string cmd = "hidumper --mem";
122 std::string str = "Total Purgeable";
123 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
124 }
125
126 /**
127 * @tc.name: MemoryDumperTest006
128 * @tc.desc: Test MemoryDumper has Dma/PurgSum/PurgPin group.
129 * @tc.type: FUNC
130 * @tc.require: issueI5NWZQ
131 */
132 HWTEST_F(MemoryDumperTest, MemoryDumperTest006, TestSize.Level3)
133 {
134 std::string cmd = "hidumper --mem";
135 std::string str = "Dma PurgSum PurgPin";
136 ASSERT_TRUE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
137 }
138
139 /**
140 * @tc.name: MemoryDumperTest007
141 * @tc.desc: Test zip memory not contain "Total Memory Usage by PID".
142 * @tc.type: FUNC
143 * @tc.require: issueI5NWZQ
144 */
145 HWTEST_F(MemoryDumperTest, MemoryDumperTest007, TestSize.Level3)
146 {
147 std::string cmd = "hidumper --mem --zip";
148 std::string str = "Total Memory Usage by PID";
149 ASSERT_FALSE(HidumperTestUtils::GetInstance().IsExistInCmdResult(cmd, str));
150 }
151
152 /**
153 * @tc.name: MemoryUtilTest001
154 * @tc.desc: Test IsNameLine has correct ret.
155 * @tc.type: FUNC
156 */
157 HWTEST_F(MemoryDumperTest, MemoryUtilTest001, TestSize.Level1)
158 {
159 const std::string valueLine = "Rss: 24 kB";
160 std::string name;
161 uint64_t iNode = 0;
162 ASSERT_FALSE(MemoryUtil::GetInstance().IsNameLine(valueLine, name, iNode));
163 ASSERT_EQ(name, "");
164 const std::string nameLine = "ffb84000-ffba5000 rw-p 00000000 00:00 0 [stack]";
165 ASSERT_TRUE(MemoryUtil::GetInstance().IsNameLine(nameLine, name, iNode));
166 ASSERT_EQ(name, "[stack]");
167 }
168
169 /**
170 * @tc.name: MemoryUtilTest002
171 * @tc.desc: Test GetTypeAndValue has correct ret.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(MemoryDumperTest, MemoryUtilTest002, TestSize.Level1)
175 {
176 std::string type;
177 uint64_t value = 0;
178 const std::string illegalStr = "aaaaaa";
179 ASSERT_FALSE(MemoryUtil::GetInstance().GetTypeAndValue(illegalStr, type, value));
180 const std::string valueStr = "MemTotal: 2010244 kB";
181 ASSERT_TRUE(MemoryUtil::GetInstance().GetTypeAndValue(valueStr, type, value));
182 ASSERT_EQ(type, "MemTotal");
183 ASSERT_EQ(value, 2010244);
184 }
185
186 /**
187 * @tc.name: MemoryUtilTest003
188 * @tc.desc: Test RunCMD has correct ret.
189 * @tc.type: FUNC
190 */
191 HWTEST_F(MemoryDumperTest, MemoryUtilTest003, TestSize.Level1)
192 {
193 const std::string cmd = "ps -ef";
194 std::vector<std::string> vec;
195 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
196 ASSERT_GT(vec.size(), 0);
197 }
198
199 /**
200 * @tc.name: MemoryUtilTest004
201 * @tc.desc: Test hidumper some cmd.
202 * @tc.type: FUNC
203 */
204 HWTEST_F(MemoryDumperTest, MemoryUtilTest004, TestSize.Level1)
205 {
206 std::string cmd = "hidumper --mem-smaps 1";
207 std::vector<std::string> vec;
208 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
209 cmd = "hidumper --mem-smaps 1 -v";
210 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
211 cmd = "hidumper --net 1";
212 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
213 cmd = "hidumper --storage 1";
214 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
215 }
216
217 /**
218 * @tc.name: MemoryUtilTest005
219 * @tc.desc: Test no such pid.
220 * @tc.type: FUNC
221 */
222 HWTEST_F(MemoryDumperTest, MemoryUtilTest005, TestSize.Level1)
223 {
224 std::string cmd = "hidumper --mem-smaps 100000";
225 std::vector<std::string> vec;
226 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
227 cmd = "hidumper --mem-smaps 100000 -v";
228 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
229 cmd = "hidumper --cpuusage 100000";
230 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
231 cmd = "hidumper --mem 100000";
232 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
233 cmd = "hidumper -p 100000";
234 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
235 cmd = "hidumper --storage 100000";
236 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
237 cmd = "hidumper --net 100000";
238 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
239 cmd = "hidumper --mem-jsheap 100000";
240 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
241 cmd = "hidumper --mem-jsheap 0";
242 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
243 cmd = "hidumper --ipc --stat 100000";
244 ASSERT_TRUE(MemoryUtil::GetInstance().RunCMD(cmd, vec));
245 }
246 } // namespace HiviewDFX
247 } // namespace OHOS