• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 
19 #include "htrace_mem_parser.h"
20 #include "memory_plugin_result.pb.h"
21 #include "memory_plugin_result.pbreader.h"
22 #include "trace_streamer_selector.h"
23 
24 using namespace testing::ext;
25 using namespace SysTuning::TraceStreamer;
26 namespace SysTuning {
27 namespace TraceStreamer {
28 class SmapsParserTest : public ::testing::Test {
29 public:
SetUp()30     void SetUp()
31     {
32         stream_.InitFilter();
33     }
34 
TearDown()35     void TearDown()
36     {
37         if (access(dbPath_.c_str(), F_OK) == 0) {
38             remove(dbPath_.c_str());
39         }
40     }
41 
42 public:
43     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
44     const std::string dbPath_ = "../../../data/resource/out.db";
45 };
46 /**
47  * @tc.name: ParseSmapsParse
48  * @tc.desc: Parse SmapsData object and export database
49  * @tc.type: FUNC
50  */
51 HWTEST_F(SmapsParserTest, ParseSmapsParse, TestSize.Level1)
52 {
53     TS_LOGI("test29-1");
54     HtraceMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
55 
56     MemoryData tracePacket;
57     ProcessMemoryInfo* memInfo = tracePacket.add_processesinfo();
58     SmapsInfo* smapsInfo = memInfo->add_smapinfo();
59     EXPECT_TRUE(smapsInfo != nullptr);
60     int32_t size = memInfo->smapinfo_size();
61     EXPECT_TRUE(size == 1);
62     uint64_t timeStamp = 1616439852302;
63     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
64 
65     std::string memStrMsg = "";
66     tracePacket.SerializeToString(&memStrMsg);
67     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t*>(memStrMsg.data()), memStrMsg.size());
68     ProtoReader::MemoryData_Reader memData(memBytesView);
69     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
70     SmapsEvent.Finish();
71     stream_.traceDataCache_->ExportDatabase(dbPath_);
72 
73     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
74     memInfo->clear_smapinfo();
75 
76     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
77     EXPECT_TRUE(1 == eventCount);
78     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessMeasureData().Size() == MEM_MAX * 1);
79     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
80 }
81 /**
82  * @tc.name: ParseSmapsParseTestMeasureDataSize
83  * @tc.desc: Parse SmapsData object and count StatInfo
84  * @tc.type: FUNC
85  */
86 HWTEST_F(SmapsParserTest, ParseSmapsParseTestMeasureDataSize, TestSize.Level1)
87 {
88     TS_LOGI("test29-2");
89     HtraceMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
90 
91     MemoryData tracePacket;
92     ProcessMemoryInfo* memInfo = tracePacket.add_processesinfo();
93     SmapsInfo* SmapsInfo = memInfo->add_smapinfo();
94     EXPECT_TRUE(SmapsInfo != nullptr);
95     int32_t size = memInfo->smapinfo_size();
96     EXPECT_TRUE(size == 1);
97     uint64_t timeStamp = 1616439852302;
98     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
99     std::string startAddr = "5589523000";
100     std::string endAddr = "5589543000";
101     std::string permission = "r--p";
102     std::string path = "/system/bin/hiprofilerd";
103     uint64_t vartualSize = 128;
104     uint64_t rss = 112;
105     uint64_t pss = 112;
106     double reside = 87.5;
107     SmapsInfo->set_start_addr(startAddr);
108     SmapsInfo->set_end_addr(endAddr);
109     SmapsInfo->set_permission(permission);
110     SmapsInfo->set_path(path);
111     SmapsInfo->set_size(vartualSize);
112     SmapsInfo->set_rss(rss);
113     SmapsInfo->set_pss(pss);
114     SmapsInfo->set_reside(reside);
115 
116     std::string memStrMsg = "";
117     tracePacket.SerializeToString(&memStrMsg);
118     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t*>(memStrMsg.data()), memStrMsg.size());
119     ProtoReader::MemoryData_Reader memData(memBytesView);
120     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
121     SmapsEvent.Finish();
122     stream_.traceDataCache_->ExportDatabase(dbPath_);
123 
124     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
125     memInfo->clear_smapinfo();
126 
127     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
128     EXPECT_TRUE(1 == eventCount);
129     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessMeasureData().Size() == MEM_MAX * 1);
130     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
131 
132     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[0] == "0x5589523000");
133     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[0] == "0x5589543000");
134     uint64_t protection = stream_.traceDataCache_->GetDataIndex(permission);
135     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[0] == protection);
136     uint64_t pat = stream_.traceDataCache_->GetDataIndex(path);
137     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[0] == pat);
138     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[0] == vartualSize);
139     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[0] == rss);
140     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[0] == pss);
141     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[0] == reside);
142     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
143 }
144 /**
145  * @tc.name: ParseSmapsParseTestMutiMeasureData
146  * @tc.desc: Parse muti SmapsData object and count StatInfo
147  * @tc.type: FUNC
148  */
149 HWTEST_F(SmapsParserTest, ParseSmapsParseTestMutiMeasureData, TestSize.Level1)
150 {
151     TS_LOGI("test29-3");
152     HtraceMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
153 
154     MemoryData tracePacket;
155     ProcessMemoryInfo* memInfo = tracePacket.add_processesinfo();
156     SmapsInfo* smapsInfo0 = memInfo->add_smapinfo();
157     EXPECT_TRUE(smapsInfo0 != nullptr);
158     int32_t size = memInfo->smapinfo_size();
159     EXPECT_TRUE(size == 1);
160     uint64_t timeStamp = 1616439852302;
161     BuiltinClocks clock0 = TS_CLOCK_BOOTTIME;
162     std::string startAddr0 = "5589523000";
163     std::string endAddr0 = "5589543000";
164     std::string permission0 = "r--p";
165     std::string path0 = "/system/bin/hiprofilerd";
166     uint64_t vartualSize0 = 128;
167     uint64_t rss0 = 112;
168     uint64_t pss0 = 112;
169     double reside0 = 87.5;
170     smapsInfo0->set_start_addr(startAddr0);
171     smapsInfo0->set_end_addr(endAddr0);
172     smapsInfo0->set_permission(permission0);
173     smapsInfo0->set_path(path0);
174     smapsInfo0->set_size(vartualSize0);
175     smapsInfo0->set_rss(rss0);
176     smapsInfo0->set_pss(pss0);
177     smapsInfo0->set_reside(reside0);
178 
179     SmapsInfo* smapsInfo1 = memInfo->add_smapinfo();
180     EXPECT_TRUE(smapsInfo1 != nullptr);
181     size = memInfo->smapinfo_size();
182     EXPECT_TRUE(size == 2);
183     timeStamp = 1616439852302;
184     BuiltinClocks clock1 = TS_CLOCK_BOOTTIME;
185     std::string startAddr1 = "5589543000";
186     std::string endAddr1 = "5589589000";
187     std::string permission1 = "r-xp";
188     std::string path1 = "/system/bin/hiprofilerd";
189     uint64_t vartualSize1 = 280;
190     uint64_t rss1 = 280;
191     uint64_t pss1 = 280;
192     uint64_t reside1 = 100;
193     smapsInfo1->set_start_addr(startAddr1);
194     smapsInfo1->set_end_addr(endAddr1);
195     smapsInfo1->set_permission(permission1);
196     smapsInfo1->set_path(path1);
197     smapsInfo1->set_size(vartualSize1);
198     smapsInfo1->set_rss(rss1);
199     smapsInfo1->set_pss(pss1);
200     smapsInfo1->set_reside(reside1);
201 
202     std::string memStrMsg = "";
203     tracePacket.SerializeToString(&memStrMsg);
204     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t*>(memStrMsg.data()), memStrMsg.size());
205     ProtoReader::MemoryData_Reader memData(memBytesView);
206     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
207     SmapsEvent.Finish();
208     stream_.traceDataCache_->ExportDatabase(dbPath_);
209 
210     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
211     memInfo->clear_smapinfo();
212 
213     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
214     EXPECT_TRUE(1 == eventCount);
215 
216     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[0] == "0x5589523000");
217     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[0] == "0x5589543000");
218     uint64_t protection = stream_.traceDataCache_->GetDataIndex(permission0);
219     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[0] == protection);
220     uint64_t pathId = stream_.traceDataCache_->GetDataIndex(path0);
221     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[0] == pathId);
222     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[0] == vartualSize0);
223     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[0] == rss0);
224     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[0] == pss0);
225     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[0] == reside0);
226 
227     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[1] == "0x5589543000");
228     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[1] == "0x5589589000");
229     protection = stream_.traceDataCache_->GetDataIndex(permission1);
230     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[1] == protection);
231     pathId = stream_.traceDataCache_->GetDataIndex(path1);
232     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[1] == pathId);
233     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[1] == vartualSize1);
234     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[1] == rss1);
235     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[1] == pss1);
236     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[1] == reside1);
237 }
238 /**
239  * @tc.name: ParseMutiEmptySmapsDataAndCountStatInfo
240  * @tc.desc: Parse muti Empty SmapsData object and count StatInfo
241  * @tc.type: FUNC
242  */
243 HWTEST_F(SmapsParserTest, ParseMutiEmptySmapsDataAndCountStatInfo, TestSize.Level1)
244 {
245     TS_LOGI("test29-4");
246     HtraceMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
247 
248     MemoryData tracePacket;
249     ProcessMemoryInfo* memInfo = tracePacket.add_processesinfo();
250     SmapsInfo* smapsInfo0 = memInfo->add_smapinfo();
251     EXPECT_TRUE(smapsInfo0 != nullptr);
252     int32_t size = memInfo->smapinfo_size();
253     EXPECT_TRUE(size == 1);
254     uint64_t timeStamp = 1616439852302;
255     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
256 
257     SmapsInfo* smapsInfo1 = memInfo->add_smapinfo();
258     EXPECT_TRUE(smapsInfo1 != nullptr);
259     size = memInfo->smapinfo_size();
260     EXPECT_TRUE(size == 2);
261 
262     std::string memStrMsg = "";
263     tracePacket.SerializeToString(&memStrMsg);
264     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t*>(memStrMsg.data()), memStrMsg.size());
265     ProtoReader::MemoryData_Reader memData(memBytesView);
266     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
267     SmapsEvent.Finish();
268     stream_.traceDataCache_->ExportDatabase(dbPath_);
269 
270     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
271     memInfo->clear_smapinfo();
272 
273     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
274     EXPECT_TRUE(1 == eventCount);
275     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
276 }
277 /**
278  * @tc.name: ParseEmptySmapsData
279  * @tc.desc: Parse Empty SmapsData
280  * @tc.type: FUNC
281  */
282 HWTEST_F(SmapsParserTest, ParseEmptySmapsData, TestSize.Level1)
283 {
284     TS_LOGI("test29-5");
285     HtraceMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
286 
287     MemoryData tracePacket;
288     int32_t size = tracePacket.processesinfo_size();
289     EXPECT_TRUE(size == 0);
290     uint64_t timeStamp = 1616439852302;
291     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
292 
293     std::string memStrMsg = "";
294     tracePacket.SerializeToString(&memStrMsg);
295     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t*>(memStrMsg.data()), memStrMsg.size());
296     ProtoReader::MemoryData_Reader memData(memBytesView);
297     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
298     SmapsEvent.Finish();
299     stream_.traceDataCache_->ExportDatabase(dbPath_);
300 
301     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
302     tracePacket.clear_processesinfo();
303 
304     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
305     EXPECT_EQ(0, eventCount);
306 }
307 } // namespace TraceStreamer
308 } // namespace SysTuning
309