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