• 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 <fcntl.h>
17 #include <hwext/gtest-ext.h>
18 #include <hwext/gtest-tag.h>
19 #include <memory>
20 
21 #include "cpu_plugin_result.pb.h"
22 #include "cpu_plugin_result.pbreader.h"
23 #include "cpu_data_parser/pbreader_cpu_data_parser.h"
24 #include "parser/ptreader_parser/ptreader_parser.h"
25 #include "parser/common_types.h"
26 #include "trace_streamer_selector.h"
27 
28 using namespace testing::ext;
29 using namespace SysTuning::TraceStreamer;
30 
31 namespace SysTuning {
32 namespace TraceStreamer {
33 namespace PbReaderCpuDataParserUnitTest {
34 const uint64_t TS = 104;
35 const uint64_t TOTALLOAD_01 = 4;
36 const uint64_t USERLOAD_01 = 44;
37 const uint64_t SYSTEMLOAD_01 = 34;
38 const uint64_t PROCESS_NUM_01 = 204;
39 const uint64_t TOTALLOAD_02 = 5;
40 const uint64_t USERLOAD_02 = 45;
41 const uint64_t SYSTEMLOAD_02 = 35;
42 const uint64_t PROCESS_NUM_02 = 205;
43 const uint64_t TOTALLOAD_03 = 6;
44 const uint64_t USERLOAD_03 = 46;
45 const uint64_t SYSTEMLOAD_03 = 36;
46 const uint64_t PROCESS_NUM_03 = 206;
47 const uint64_t TOTALLOAD_04 = 6;
48 const uint64_t USERLOAD_04 = 46;
49 const uint64_t SYSTEMLOAD_04 = 36;
50 const uint64_t PROCESS_NUM_04 = 206;
51 
52 struct CpudataInfo {
53     uint64_t total_load;
54     uint64_t user_load;
55     uint64_t sys_load;
56     uint64_t process_num;
57 };
58 
59 class HtraceCpuDataParserTest : public ::testing::Test {
60 public:
SetUp()61     void SetUp()
62     {
63         stream_.InitFilter();
64     }
65 
TearDown() const66     void TearDown() const {}
67 
SetCpuData(CpudataInfo & cpudataInfo,bool isSetUsageInfo)68     std::string SetCpuData(CpudataInfo &cpudataInfo, bool isSetUsageInfo)
69     {
70         auto cpuInfo(std::make_unique<CpuData>());
71         cpuInfo->set_total_load(cpudataInfo.total_load);
72         cpuInfo->set_user_load(cpudataInfo.user_load);
73         cpuInfo->set_sys_load(cpudataInfo.sys_load);
74         cpuInfo->set_process_num(cpudataInfo.process_num);
75         if (isSetUsageInfo) {
76             CpuUsageInfo *cpuUsageInfo = new CpuUsageInfo();
77             cpuInfo->set_allocated_cpu_usage_info(cpuUsageInfo);
78         }
79 
80         std::string cpuData = "";
81         cpuInfo->SerializeToString(&cpuData);
82         return cpuData;
83     }
84 
85 public:
86     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
87 };
88 
89 /**
90  * @tc.name: ParseHtraceWithoutCpuDataData
91  * @tc.desc: Parse a cpu that does not contain any cpudata
92  * @tc.type: FUNC
93  */
94 HWTEST_F(HtraceCpuDataParserTest, ParseHtraceWithoutCpuData, TestSize.Level1)
95 {
96     TS_LOGI("test11-1");
97 
98     auto cpuInfo = std::make_unique<CpuData>();
99     std::string cpuData = "";
100     cpuInfo->SerializeToString(&cpuData);
101     ProtoReader::BytesView cpuInfoData(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
102     PbreaderCpuDataParser htraceCpuDataParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
103     htraceCpuDataParser.Parse(cpuInfoData, TS);
104     auto size = stream_.traceDataCache_->GetConstCpuUsageInfoData().Size();
105     EXPECT_FALSE(size);
106 }
107 
108 /**
109  * @tc.name: ParseHtraceWithOneCpuData
110  * @tc.desc: Parse a cpu with one cpudata
111  * @tc.type: FUNC
112  */
113 HWTEST_F(HtraceCpuDataParserTest, ParseHtraceWithOneCpuData, TestSize.Level1)
114 {
115     TS_LOGI("test11-2");
116 
117     CpudataInfo cpudataInfo = {TOTALLOAD_01, USERLOAD_01, SYSTEMLOAD_01, PROCESS_NUM_01};
118     std::string cpuData = SetCpuData(cpudataInfo, false);
119     ProtoReader::BytesView cpuInfoData(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
120     PbreaderCpuDataParser htraceCpuDataParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
121     htraceCpuDataParser.Parse(cpuInfoData, TS);
122     htraceCpuDataParser.Finish();
123     auto size = stream_.traceDataCache_->GetConstCpuUsageInfoData().Size();
124     EXPECT_FALSE(size);
125 }
126 
127 /**
128  * @tc.name: ParseHtraceWithTwoCpuData
129  * @tc.desc: Parse a cpu with two cpudata
130  * @tc.type: FUNC
131  */
132 HWTEST_F(HtraceCpuDataParserTest, ParseHtraceWithTwoCpuData, TestSize.Level1)
133 {
134     TS_LOGI("test11-3");
135 
136     CpudataInfo cpudataInfo01 = {TOTALLOAD_01, USERLOAD_01, SYSTEMLOAD_01, PROCESS_NUM_01};
137     std::string cpuData = SetCpuData(cpudataInfo01, true);
138     ProtoReader::BytesView cpuInfoData01(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
139     PbreaderCpuDataParser htraceCpuDataParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
140     htraceCpuDataParser.Parse(cpuInfoData01, TS);
141 
142     CpudataInfo cpudataInfo02 = {TOTALLOAD_02, USERLOAD_02, SYSTEMLOAD_02, PROCESS_NUM_02};
143     cpuData = SetCpuData(cpudataInfo02, true);
144     ProtoReader::BytesView cpuInfoData02(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
145     htraceCpuDataParser.Parse(cpuInfoData02, TS);
146     htraceCpuDataParser.Finish();
147 
148     auto cpuUsageInfoData = stream_.traceDataCache_->GetConstCpuUsageInfoData();
149     ASSERT_EQ(1, cpuUsageInfoData.Size());
150     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[0], TOTALLOAD_02);
151     EXPECT_EQ(cpuUsageInfoData.UserLoad()[0], USERLOAD_02);
152     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[0], SYSTEMLOAD_02);
153 }
154 
155 /**
156  * @tc.name: ParseHtraceWithThreeCpuData
157  * @tc.desc: Parse a cpu with Three cpudata
158  * @tc.type: FUNC
159  */
160 HWTEST_F(HtraceCpuDataParserTest, ParseHtraceWithThreeCpuData, TestSize.Level1)
161 {
162     TS_LOGI("test11-4");
163 
164     CpudataInfo cpudataInfo01 = {TOTALLOAD_01, USERLOAD_01, SYSTEMLOAD_01, PROCESS_NUM_01};
165     std::string cpuData = SetCpuData(cpudataInfo01, true);
166     ProtoReader::BytesView cpuInfoData01(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
167     PbreaderCpuDataParser htraceCpuDataParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
168     htraceCpuDataParser.Parse(cpuInfoData01, TS);
169 
170     CpudataInfo cpudataInfo02 = {TOTALLOAD_02, USERLOAD_02, SYSTEMLOAD_02, PROCESS_NUM_02};
171     cpuData = SetCpuData(cpudataInfo02, true);
172     ProtoReader::BytesView cpuInfoData02(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
173     htraceCpuDataParser.Parse(cpuInfoData02, TS);
174 
175     CpudataInfo cpudataInfo03 = {TOTALLOAD_03, USERLOAD_03, SYSTEMLOAD_03, PROCESS_NUM_03};
176     cpuData = SetCpuData(cpudataInfo03, true);
177     ProtoReader::BytesView cpuInfoData03(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
178     htraceCpuDataParser.Parse(cpuInfoData03, TS);
179     htraceCpuDataParser.Finish();
180 
181     auto cpuUsageInfoData = stream_.traceDataCache_->GetConstCpuUsageInfoData();
182     ASSERT_EQ(2, cpuUsageInfoData.Size());
183     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[0], TOTALLOAD_02);
184     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[1], TOTALLOAD_03);
185     EXPECT_EQ(cpuUsageInfoData.UserLoad()[0], USERLOAD_02);
186     EXPECT_EQ(cpuUsageInfoData.UserLoad()[1], USERLOAD_03);
187     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[0], SYSTEMLOAD_02);
188     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[1], SYSTEMLOAD_03);
189 }
190 
191 /**
192  * @tc.name: ParseHtraceWithMultipleCpuData
193  * @tc.desc: Parse a CpuData with Multiple CpuData
194  * @tc.type: FUNC
195  */
196 HWTEST_F(HtraceCpuDataParserTest, ParseHtraceWithMultipleCpuData, TestSize.Level1)
197 {
198     TS_LOGI("test11-5");
199 
200     CpudataInfo cpudataInfo01 = {TOTALLOAD_01, USERLOAD_01, SYSTEMLOAD_01, PROCESS_NUM_01};
201     std::string cpuData = SetCpuData(cpudataInfo01, true);
202     ProtoReader::BytesView cpuInfoData01(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
203     PbreaderCpuDataParser htraceCpuDataParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
204     htraceCpuDataParser.Parse(cpuInfoData01, TS);
205 
206     CpudataInfo cpudataInfo02 = {TOTALLOAD_02, USERLOAD_02, SYSTEMLOAD_02, PROCESS_NUM_02};
207     cpuData = SetCpuData(cpudataInfo02, true);
208     ProtoReader::BytesView cpuInfoData02(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
209     htraceCpuDataParser.Parse(cpuInfoData02, TS);
210 
211     CpudataInfo cpudataInfo03 = {TOTALLOAD_03, USERLOAD_03, SYSTEMLOAD_03, PROCESS_NUM_03};
212     cpuData = SetCpuData(cpudataInfo03, true);
213     ProtoReader::BytesView cpuInfoData03(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
214     htraceCpuDataParser.Parse(cpuInfoData03, TS);
215 
216     CpudataInfo cpudataInfo04 = {TOTALLOAD_04, USERLOAD_04, SYSTEMLOAD_04, PROCESS_NUM_04};
217     cpuData = SetCpuData(cpudataInfo04, true);
218     ProtoReader::BytesView cpuInfoData04(reinterpret_cast<const uint8_t *>(cpuData.data()), cpuData.size());
219     htraceCpuDataParser.Parse(cpuInfoData04, TS);
220     htraceCpuDataParser.Finish();
221 
222     auto cpuUsageInfoData = stream_.traceDataCache_->GetConstCpuUsageInfoData();
223     ASSERT_EQ(3, cpuUsageInfoData.Size());
224     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[0], TOTALLOAD_02);
225     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[1], TOTALLOAD_03);
226     EXPECT_EQ(cpuUsageInfoData.TotalLoad()[2], TOTALLOAD_04);
227     EXPECT_EQ(cpuUsageInfoData.UserLoad()[0], USERLOAD_02);
228     EXPECT_EQ(cpuUsageInfoData.UserLoad()[1], USERLOAD_03);
229     EXPECT_EQ(cpuUsageInfoData.UserLoad()[2], USERLOAD_04);
230     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[0], SYSTEMLOAD_02);
231     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[1], SYSTEMLOAD_03);
232     EXPECT_EQ(cpuUsageInfoData.SystemLoad()[2], SYSTEMLOAD_04);
233 }
234 } // namespace PbReaderCpuDataParserUnitTest
235 } // namespace TraceStreamer
236 } // namespace SysTuning
237