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