• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 "mem_parser/pbreader_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 std::string PERMISSION_01 = "r--p";
29 std::string PATH = "/system/bin/hiprofilerd";
30 const uint64_t VARTUAL_SIZE_01 = 128;
31 const uint64_t RSS_01 = 112;
32 const uint64_t PSS_01 = 112;
33 const double RESIDE_01 = 87.5;
34 std::string PERMISSION_02 = "r-xp";
35 const uint64_t VARTUAL_SIZE_02 = 280;
36 const uint64_t RSS_02 = 280;
37 const uint64_t PSS_02 = 280;
38 const double RESIDE_02 = 100;
39 
40 class SmapsParserTest : public ::testing::Test {
41 public:
SetUp()42     void SetUp()
43     {
44         stream_.InitFilter();
45     }
46 
TearDown()47     void TearDown()
48     {
49         if (access(dbPath_.c_str(), F_OK) == 0) {
50             remove(dbPath_.c_str());
51         }
52     }
53 
SetSmapinfo(ProcessMemoryInfo * memInfo,bool isRepeated=false)54     void SetSmapinfo(ProcessMemoryInfo *memInfo, bool isRepeated = false)
55     {
56         SmapsInfo *smapsInfo = memInfo->add_smapinfo();
57         smapsInfo->set_start_addr("5589523000");
58         smapsInfo->set_end_addr("5589543000");
59         smapsInfo->set_permission(PERMISSION_01);
60         smapsInfo->set_path(PATH);
61         smapsInfo->set_size(VARTUAL_SIZE_01);
62         smapsInfo->set_rss(RSS_01);
63         smapsInfo->set_pss(PSS_01);
64         smapsInfo->set_reside(RESIDE_01);
65 
66         if (isRepeated) {
67             SmapsInfo *smapsInfo1 = memInfo->add_smapinfo();
68             smapsInfo1->set_start_addr("5589543000");
69             smapsInfo1->set_end_addr("5589589000");
70             smapsInfo1->set_permission(PERMISSION_02);
71             smapsInfo1->set_path(PATH);
72             smapsInfo1->set_size(VARTUAL_SIZE_02);
73             smapsInfo1->set_rss(RSS_02);
74             smapsInfo1->set_pss(PSS_02);
75             smapsInfo1->set_reside(RESIDE_02);
76         }
77     }
78 
79 public:
80     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
81     const std::string dbPath_ = "out.db";
82 };
83 /**
84  * @tc.name: ParseSmapsParse
85  * @tc.desc: Parse SmapsData object and export database
86  * @tc.type: FUNC
87  */
88 HWTEST_F(SmapsParserTest, ParseSmapsParse, TestSize.Level1)
89 {
90     TS_LOGI("test29-1");
91     PbreaderMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
92 
93     MemoryData tracePacket;
94     ProcessMemoryInfo *memInfo = tracePacket.add_processesinfo();
95     SmapsInfo *smapsInfo = memInfo->add_smapinfo();
96     EXPECT_TRUE(smapsInfo != nullptr);
97     int32_t size = memInfo->smapinfo_size();
98     EXPECT_TRUE(size == 1);
99     uint64_t timeStamp = 1616439852302;
100     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
101 
102     std::string memStrMsg = "";
103     tracePacket.SerializeToString(&memStrMsg);
104     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t *>(memStrMsg.data()), memStrMsg.size());
105     ProtoReader::MemoryData_Reader memData(memBytesView);
106     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
107     SmapsEvent.Finish();
108     stream_.traceDataCache_->ExportDatabase(dbPath_);
109 
110     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
111     memInfo->clear_smapinfo();
112 
113     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
114     EXPECT_TRUE(1 == eventCount);
115     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessMeasureData().Size() == 9);
116     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
117 }
118 /**
119  * @tc.name: ParseSmapsParseTestMeasureDataSize
120  * @tc.desc: Parse SmapsData object and count StatInfo
121  * @tc.type: FUNC
122  */
123 HWTEST_F(SmapsParserTest, ParseSmapsParseTestMeasureDataSize, TestSize.Level1)
124 {
125     TS_LOGI("test29-2");
126 
127     MemoryData tracePacket;
128     ProcessMemoryInfo *memInfo = tracePacket.add_processesinfo();
129     SetSmapinfo(memInfo);
130     std::string memStrMsg = "";
131     tracePacket.SerializeToString(&memStrMsg);
132     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t *>(memStrMsg.data()), memStrMsg.size());
133     ProtoReader::MemoryData_Reader memData(memBytesView);
134     uint64_t timeStamp = 1616439852302;
135     PbreaderMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
136     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
137     SmapsEvent.Finish();
138     stream_.traceDataCache_->ExportDatabase(dbPath_);
139 
140     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
141     memInfo->clear_smapinfo();
142 
143     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
144     EXPECT_TRUE(1 == eventCount);
145     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessMeasureData().Size() == 9);
146     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
147     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[0] == "0x5589523000");
148     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[0] == "0x5589543000");
149     uint64_t protection = stream_.traceDataCache_->GetDataIndex(PERMISSION_01);
150     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[0] == protection);
151     uint64_t pat = stream_.traceDataCache_->GetDataIndex(PATH);
152     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[0] == pat);
153     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[0] == VARTUAL_SIZE_01);
154     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[0] == RSS_01);
155     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[0] == PSS_01);
156     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[0] == RESIDE_01);
157     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
158 }
159 /**
160  * @tc.name: ParseSmapsParseTestMutiMeasureData
161  * @tc.desc: Parse muti SmapsData object and count StatInfo
162  * @tc.type: FUNC
163  */
164 HWTEST_F(SmapsParserTest, ParseSmapsParseTestMutiMeasureData, TestSize.Level1)
165 {
166     TS_LOGI("test29-3");
167 
168     MemoryData tracePacket;
169     ProcessMemoryInfo *memInfo = tracePacket.add_processesinfo();
170     SetSmapinfo(memInfo, true);
171     std::string memStrMsg = "";
172     tracePacket.SerializeToString(&memStrMsg);
173     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t *>(memStrMsg.data()), memStrMsg.size());
174     ProtoReader::MemoryData_Reader memData(memBytesView);
175     uint64_t timeStamp = 1616439852302;
176     PbreaderMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
177     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
178     SmapsEvent.Finish();
179     stream_.traceDataCache_->ExportDatabase(dbPath_);
180 
181     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
182     memInfo->clear_smapinfo();
183 
184     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
185     EXPECT_TRUE(1 == eventCount);
186     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[0] == "0x5589523000");
187     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[0] == "0x5589543000");
188     uint64_t protection = stream_.traceDataCache_->GetDataIndex(PERMISSION_01);
189     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[0] == protection);
190     uint64_t pathId = stream_.traceDataCache_->GetDataIndex(PATH);
191     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[0] == pathId);
192     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[0] == VARTUAL_SIZE_01);
193     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[0] == RSS_01);
194     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[0] == PSS_01);
195     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[0] == RESIDE_01);
196     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().StartAddrs()[1] == "0x5589543000");
197     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().EndAddrs()[1] == "0x5589589000");
198     protection = stream_.traceDataCache_->GetDataIndex(PERMISSION_02);
199     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().ProtectionIds()[1] == protection);
200     pathId = stream_.traceDataCache_->GetDataIndex(PATH);
201     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().PathIds()[1] == pathId);
202     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Sizes()[1] == VARTUAL_SIZE_02);
203     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Rss()[1] == RSS_02);
204     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Pss()[1] == PSS_02);
205     EXPECT_TRUE(stream_.traceDataCache_->GetConstSmapsData().Resides()[1] == RESIDE_02);
206 }
207 /**
208  * @tc.name: ParseMutiEmptySmapsDataAndCountStatInfo
209  * @tc.desc: Parse muti Empty SmapsData object and count StatInfo
210  * @tc.type: FUNC
211  */
212 HWTEST_F(SmapsParserTest, ParseMutiEmptySmapsDataAndCountStatInfo, TestSize.Level1)
213 {
214     TS_LOGI("test29-4");
215     PbreaderMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
216 
217     MemoryData tracePacket;
218     ProcessMemoryInfo *memInfo = tracePacket.add_processesinfo();
219     SmapsInfo *smapsInfo0 = memInfo->add_smapinfo();
220     EXPECT_TRUE(smapsInfo0 != nullptr);
221     int32_t size = memInfo->smapinfo_size();
222     EXPECT_TRUE(size == 1);
223     uint64_t timeStamp = 1616439852302;
224     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
225 
226     SmapsInfo *smapsInfo1 = memInfo->add_smapinfo();
227     EXPECT_TRUE(smapsInfo1 != nullptr);
228     size = memInfo->smapinfo_size();
229     EXPECT_TRUE(size == 2);
230 
231     std::string memStrMsg = "";
232     tracePacket.SerializeToString(&memStrMsg);
233     ProtoReader::BytesView memBytesView(reinterpret_cast<const uint8_t *>(memStrMsg.data()), memStrMsg.size());
234     ProtoReader::MemoryData_Reader memData(memBytesView);
235     SmapsEvent.ParseProcessInfo(&memData, timeStamp);
236     SmapsEvent.Finish();
237     stream_.traceDataCache_->ExportDatabase(dbPath_);
238 
239     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
240     memInfo->clear_smapinfo();
241 
242     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
243     EXPECT_TRUE(1 == eventCount);
244     EXPECT_EQ(stream_.traceDataCache_->GetConstProcessData().size(), 1);
245 }
246 /**
247  * @tc.name: ParseEmptySmapsData
248  * @tc.desc: Parse Empty SmapsData
249  * @tc.type: FUNC
250  */
251 HWTEST_F(SmapsParserTest, ParseEmptySmapsData, TestSize.Level1)
252 {
253     TS_LOGI("test29-5");
254     PbreaderMemParser SmapsEvent(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
255 
256     MemoryData tracePacket;
257     int32_t size = tracePacket.processesinfo_size();
258     EXPECT_TRUE(size == 0);
259     uint64_t timeStamp = 1616439852302;
260     BuiltinClocks clock = TS_CLOCK_BOOTTIME;
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     tracePacket.clear_processesinfo();
272 
273     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_SMAPS, STAT_EVENT_RECEIVED);
274     EXPECT_EQ(0, eventCount);
275 }
276 } // namespace TraceStreamer
277 } // namespace SysTuning
278