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 <string> 20 21 #include "hidump_parser/pbreader_hidump_parser.h" 22 #include "hidump_plugin_result.pb.h" 23 #include "hidump_plugin_result.pbreader.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 class HidumpParserTest : public ::testing::Test { 34 public: SetUp()35 void SetUp() 36 { 37 stream_.InitFilter(); 38 } 39 TearDown()40 void TearDown() 41 { 42 if (access(dbPath_.c_str(), F_OK) == 0) { 43 remove(dbPath_.c_str()); 44 } 45 } 46 47 public: 48 SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {}; 49 const std::string dbPath_ = "../../../data/resource/out.db"; 50 }; 51 52 /** 53 * @tc.name: ParseEmptyHidumpInfo 54 * @tc.desc: Parse an empty HidumpInfo 55 * @tc.type: FUNC 56 */ 57 HWTEST_F(HidumpParserTest, ParseEmptyHidumpInfo, TestSize.Level1) 58 { 59 TS_LOGI("test7-1"); 60 HidumpInfo hidumpInfo; 61 PbreaderHidumpParser htraceHidumpParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 62 std::string hidumpDatas = ""; 63 hidumpInfo.SerializeToString(&hidumpDatas); 64 ProtoReader::BytesView hidumpInfoData(reinterpret_cast<const uint8_t *>(hidumpDatas.data()), hidumpDatas.size()); 65 htraceHidumpParser.Parse(hidumpInfoData); 66 auto size = stream_.traceDataCache_->GetConstHidumpData().Size(); 67 EXPECT_EQ(0, size); 68 } 69 70 /** 71 * @tc.name: ParseLegalHidumpInfo 72 * @tc.desc: Parse a legal HidumpInfo 73 * @tc.type: FUNC 74 */ 75 HWTEST_F(HidumpParserTest, ParseLegalHidumpInfo, TestSize.Level1) 76 { 77 TS_LOGI("test7-2"); 78 const uint32_t FPS = 120; 79 const uint32_t TV_SEC = 16326755; 80 const uint32_t TV_NSEC = 39656070; 81 82 FpsData_TimeSpec timeSpec; 83 timeSpec.set_tv_nsec(TV_NSEC); 84 timeSpec.set_tv_sec(TV_SEC); 85 86 HidumpInfo *hidumpInfo = new HidumpInfo(); 87 auto fpsData = hidumpInfo->add_fps_event(); 88 fpsData->set_fps(FPS); 89 fpsData->set_allocated_time(&timeSpec); 90 91 PbreaderHidumpParser htraceHidumpParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 92 std::string hidumpDatas = ""; 93 hidumpInfo->SerializeToString(&hidumpDatas); 94 ProtoReader::BytesView hidumpInfoData(reinterpret_cast<const uint8_t *>(hidumpDatas.data()), hidumpDatas.size()); 95 htraceHidumpParser.Parse(hidumpInfoData); 96 97 auto Fps = stream_.traceDataCache_->GetConstHidumpData().Fpss()[0]; 98 EXPECT_EQ(FPS, Fps); 99 auto TimeSpec = stream_.traceDataCache_->GetConstHidumpData().TimeStampData()[0]; 100 EXPECT_EQ((TV_NSEC + TV_SEC * SEC_TO_NS), TimeSpec); 101 auto Size = stream_.traceDataCache_->GetConstHidumpData().Size(); 102 EXPECT_EQ(1, Size); 103 } 104 105 /** 106 * @tc.name: ParseMultipleReasonableHidumpInfo 107 * @tc.desc: parse multiple reasonable HidumpInfo 108 * @tc.type: FUNC 109 */ 110 HWTEST_F(HidumpParserTest, ParseMultipleReasonableHidumpInfo, TestSize.Level1) 111 { 112 TS_LOGI("test7-3"); 113 const uint32_t FPS_00 = 120; 114 const uint32_t TV_SEC_00 = 1632675525; 115 const uint32_t TV_NSEC_00 = 996560700; 116 FpsData_TimeSpec timeSpecFirst; 117 timeSpecFirst.set_tv_nsec(TV_NSEC_00); 118 timeSpecFirst.set_tv_sec(TV_SEC_00); 119 120 const uint32_t FPS_01 = 60; 121 const uint32_t TV_SEC_01 = 1632675525; 122 const uint32_t TV_NSEC_01 = 996560700; 123 FpsData_TimeSpec timeSpecSecond; 124 timeSpecSecond.set_tv_nsec(TV_NSEC_01); 125 timeSpecSecond.set_tv_sec(TV_SEC_01); 126 127 const uint32_t FPS_02 = 90; 128 const uint32_t TV_SEC_02 = 1632688888; 129 const uint32_t TV_NSEC_02 = 996588888; 130 FpsData_TimeSpec timeSpecThird; 131 timeSpecThird.set_tv_nsec(TV_NSEC_02); 132 timeSpecThird.set_tv_sec(TV_SEC_02); 133 134 HidumpInfo *hidumpInfo = new HidumpInfo(); 135 auto fpsDataFirst = hidumpInfo->add_fps_event(); 136 fpsDataFirst->set_fps(FPS_00); 137 fpsDataFirst->set_allocated_time(&timeSpecFirst); 138 139 auto fpsDataSecond = hidumpInfo->add_fps_event(); 140 fpsDataSecond->set_fps(FPS_01); 141 fpsDataSecond->set_allocated_time(&timeSpecSecond); 142 143 auto fpsDataThird = hidumpInfo->add_fps_event(); 144 fpsDataThird->set_fps(FPS_02); 145 fpsDataThird->set_allocated_time(&timeSpecThird); 146 147 PbreaderHidumpParser htraceHidumpParser(stream_.traceDataCache_.get(), stream_.streamFilters_.get()); 148 std::string hidumpDatas = ""; 149 hidumpInfo->SerializeToString(&hidumpDatas); 150 ProtoReader::BytesView hidumpInfoData(reinterpret_cast<const uint8_t *>(hidumpDatas.data()), hidumpDatas.size()); 151 htraceHidumpParser.Parse(hidumpInfoData); 152 153 auto Fps_00 = stream_.traceDataCache_->GetConstHidumpData().Fpss()[0]; 154 auto Fps_01 = stream_.traceDataCache_->GetConstHidumpData().Fpss()[1]; 155 auto Fps_02 = stream_.traceDataCache_->GetConstHidumpData().Fpss()[2]; 156 EXPECT_EQ(FPS_00, Fps_00); 157 EXPECT_EQ(FPS_01, Fps_01); 158 EXPECT_EQ(FPS_02, Fps_02); 159 160 auto TimeSpec_00 = stream_.traceDataCache_->GetConstHidumpData().TimeStampData()[0]; 161 auto TimeSpec_01 = stream_.traceDataCache_->GetConstHidumpData().TimeStampData()[1]; 162 auto TimeSpec_02 = stream_.traceDataCache_->GetConstHidumpData().TimeStampData()[2]; 163 EXPECT_EQ((TV_NSEC_00 + TV_SEC_00 * SEC_TO_NS), TimeSpec_00); 164 EXPECT_EQ((TV_NSEC_01 + TV_SEC_01 * SEC_TO_NS), TimeSpec_01); 165 EXPECT_EQ((TV_NSEC_02 + TV_SEC_02 * SEC_TO_NS), TimeSpec_02); 166 167 auto Size = stream_.traceDataCache_->GetConstHidumpData().Size(); 168 EXPECT_EQ(3, Size); 169 } 170 } // namespace TraceStreamer 171 } // namespace SysTuning 172