• 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 <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <string>
20 #include <unordered_map>
21 
22 
23 #include "htrace_mem_parser.h"
24 #include "parser/common_types.h"
25 #include "trace_streamer_selector.h"
26 
27 using namespace testing::ext;
28 using namespace SysTuning::TraceStreamer;
29 
30 namespace SysTuning {
31 namespace TraceStreamer {
32 class HtraceMemParserTest : public ::testing::Test {
33 public:
SetUp()34     void SetUp()
35     {
36         stream_.InitFilter();
37     }
38 
TearDown()39     void TearDown()
40     {
41         if (access(dbPath_.c_str(), F_OK) == 0) {
42             remove(dbPath_.c_str());
43         }
44     }
45 
46 public:
47     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
48     const std::string dbPath_ = "/data/resource/out.db";
49 };
50 
51 /**
52  * @tc.name: ParseMemParse
53  * @tc.desc: Parse MemoryData object and export database
54  * @tc.type: FUNC
55  */
56 HWTEST_F(HtraceMemParserTest, ParseMemParse, TestSize.Level1)
57 {
58     TS_LOGI("test7-1");
59     HtraceMemParser* memParser = new HtraceMemParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
60 
61     MemoryData tracePacket;
62     ProcessMemoryInfo* memoryInfo = tracePacket.add_processesinfo();
63     EXPECT_TRUE(memoryInfo != nullptr);
64     int size = tracePacket.processesinfo_size();
65     EXPECT_TRUE(size == 1);
66     uint64_t timeStamp = 1616439852302;
67     BuiltinClocks clock = TS_CLOCK_REALTIME;
68 
69     memParser->Parse(tracePacket, timeStamp, clock);
70     memParser->Finish();
71     stream_.traceDataCache_->ExportDatabase(dbPath_);
72 
73     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
74     tracePacket.clear_processesinfo();
75     delete memParser;
76 
77     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_MEMORY, STAT_EVENT_RECEIVED);
78     EXPECT_TRUE(1 == eventCount);
79 }
80 
81 /**
82  * @tc.name: ParseMemParseTestMeasureDataSize
83  * @tc.desc: Parse MemoryData object and count StatInfo
84  * @tc.type: FUNC
85  */
86 HWTEST_F(HtraceMemParserTest, ParseMemParseTestMeasureDataSize, TestSize.Level1)
87 {
88     TS_LOGI("test7-1");
89     HtraceMemParser* memParser = new HtraceMemParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
90 
91     MemoryData tracePacket;
92     ProcessMemoryInfo* memoryInfo = tracePacket.add_processesinfo();
93     EXPECT_TRUE(memoryInfo != nullptr);
94     int size = tracePacket.processesinfo_size();
95     EXPECT_TRUE(size == 1);
96     uint64_t timeStamp = 1616439852302;
97     BuiltinClocks clock = TS_CLOCK_REALTIME;
98     uint32_t pid = 12;
99     int64_t memKb = 1024;
100     int64_t memRssKb = 512;
101     int64_t memAnonKb = 128;
102     int64_t memFileKb = 2048;
103     memoryInfo->set_pid(pid);
104     memoryInfo->set_name("Process1");
105     memoryInfo->set_vm_size_kb(memKb);
106     memoryInfo->set_vm_rss_kb(memRssKb);
107     memoryInfo->set_rss_anon_kb(memAnonKb);
108     memoryInfo->set_rss_file_kb(memFileKb);
109 
110     memParser->Parse(tracePacket, timeStamp, clock);
111     memParser->Finish();
112     stream_.traceDataCache_->ExportDatabase(dbPath_);
113 
114     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
115     tracePacket.clear_processesinfo();
116     delete memParser;
117 
118     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_MEMORY, STAT_EVENT_RECEIVED);
119     EXPECT_TRUE(1 == eventCount);
120 
121     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessData(1).pid_ == pid);
122     EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().Size() == MEM_MAX * 1);
123 
124     for (auto i = 0; i < MEM_MAX; i++) {
125         if (stream_.traceDataCache_->GetConstMeasureData().filterIdDeque_[i] == memParser->memNameDictMap_.at(MEM_VM_SIZE)) {
126             EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().valuesDeque_[i] == memKb);
127         } else if (stream_.traceDataCache_->GetConstMeasureData().filterIdDeque_[i] == memParser->memNameDictMap_.at(MEM_VM_RSS)) {
128             EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().valuesDeque_[i] == memRssKb);
129         } else if (stream_.traceDataCache_->GetConstMeasureData().filterIdDeque_[i] == memParser->memNameDictMap_.at(MEM_VM_ANON)) {
130             EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().valuesDeque_[i] == memAnonKb);
131         } else if (stream_.traceDataCache_->GetConstMeasureData().filterIdDeque_[i] == memParser->memNameDictMap_.at(MEM_RSS_FILE)) {
132             EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().valuesDeque_[i] == memFileKb);
133         }
134     }
135 }
136 
137 /**
138  * @tc.name: ParseMemParseTestMutiMeasureData
139  * @tc.desc: Parse muti MemoryData object and count StatInfo
140  * @tc.type: FUNC
141  */
142 HWTEST_F(HtraceMemParserTest, ParseMemParseTestMutiMeasureData, TestSize.Level1)
143 {
144     TS_LOGI("test7-1");
145     HtraceMemParser* memParser = new HtraceMemParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
146 
147     MemoryData tracePacket;
148     ProcessMemoryInfo* memoryInfo = tracePacket.add_processesinfo();
149     EXPECT_TRUE(memoryInfo != nullptr);
150     int size = tracePacket.processesinfo_size();
151     EXPECT_TRUE(size == 1);
152     uint64_t timeStamp = 1616439852302;
153     BuiltinClocks clock = TS_CLOCK_REALTIME;
154     uint32_t pid = 12;
155     memoryInfo->set_pid(12);
156     memoryInfo->set_name("Process1");
157     memoryInfo->set_vm_size_kb(1024);
158     memoryInfo->set_vm_rss_kb(512);
159     memoryInfo->set_rss_anon_kb(128);
160     memoryInfo->set_rss_file_kb(128);
161 
162     ProcessMemoryInfo* memoryInfo2 = tracePacket.add_processesinfo();
163     EXPECT_TRUE(memoryInfo2 != nullptr);
164     size = tracePacket.processesinfo_size();
165     EXPECT_TRUE(size == 2);
166     timeStamp = 1616439852402;
167     uint32_t pid2 = 13;
168     memoryInfo2->set_pid(pid2);
169     memoryInfo2->set_name("Process2");
170     memoryInfo2->set_vm_size_kb(1024);
171     memoryInfo2->set_vm_rss_kb(512);
172     memoryInfo2->set_rss_anon_kb(128);
173     memoryInfo2->set_rss_file_kb(128);
174 
175     memParser->Parse(tracePacket, timeStamp, clock);
176     memParser->Finish();
177     stream_.traceDataCache_->ExportDatabase(dbPath_);
178 
179     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
180     tracePacket.clear_processesinfo();
181     delete memParser;
182 
183     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_MEMORY, STAT_EVENT_RECEIVED);
184     EXPECT_TRUE(1 == eventCount);
185 
186     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessData(1).pid_ == pid);
187     EXPECT_TRUE(stream_.traceDataCache_->GetConstProcessData(2).pid_ == pid2);
188 }
189 
190 /**
191  * @tc.name: ParseMultiEmptyProcessMemoryInfo
192  * @tc.desc: Parse muti Empty ProcessMemoryInfo object and count StatInfo
193  * @tc.type: FUNC
194  */
195 HWTEST_F(HtraceMemParserTest, ParseMultiEmptyProcessMemoryInfo, TestSize.Level1)
196 {
197     TS_LOGI("test7-1");
198     HtraceMemParser* memParser = new HtraceMemParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
199 
200     MemoryData tracePacket;
201     ProcessMemoryInfo* memoryInfo = tracePacket.add_processesinfo();
202     EXPECT_TRUE(memoryInfo != nullptr);
203     int size = tracePacket.processesinfo_size();
204     EXPECT_TRUE(size == 1);
205     uint64_t timeStamp = 1616439852302;
206     BuiltinClocks clock = TS_CLOCK_REALTIME;
207 
208     ProcessMemoryInfo* memoryInfo2 = tracePacket.add_processesinfo();
209     EXPECT_TRUE(memoryInfo2 != nullptr);
210     size = tracePacket.processesinfo_size();
211     EXPECT_TRUE(size == 2);
212 
213     memParser->Parse(tracePacket, timeStamp, clock);
214     memParser->Finish();
215     stream_.traceDataCache_->ExportDatabase(dbPath_);
216 
217     EXPECT_TRUE(access(dbPath_.c_str(), F_OK) == 0);
218     tracePacket.clear_processesinfo();
219     delete memParser;
220 
221     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_MEMORY, STAT_EVENT_RECEIVED);
222     EXPECT_TRUE(1 == eventCount);
223 
224     EXPECT_TRUE(stream_.traceDataCache_->GetConstMeasureData().Size() == MEM_MAX * 2);
225 }
226 
227 /**
228  * @tc.name: ParseEmptyMemoryData
229  * @tc.desc: Parse Empty MemoryData
230  * @tc.type: FUNC
231  */
232 HWTEST_F(HtraceMemParserTest, ParseEmptyMemoryData, TestSize.Level1)
233 {
234     TS_LOGI("test7-1");
235     HtraceMemParser* memParser = new HtraceMemParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
236 
237     MemoryData tracePacket;
238     int size = tracePacket.processesinfo_size();
239     EXPECT_TRUE(size == 0);
240     uint64_t timeStamp = 1616439852302;
241     BuiltinClocks clock = TS_CLOCK_REALTIME;
242 
243     memParser->Parse(tracePacket, timeStamp, clock);
244     memParser->Finish();
245     delete memParser;
246 
247     auto eventCount = stream_.traceDataCache_->GetConstStatAndInfo().GetValue(TRACE_MEMORY, STAT_EVENT_RECEIVED);
248     EXPECT_TRUE(0 == eventCount);
249 }
250 } // namespace TraceStreamer
251 } // namespace SysTuning
252