• 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 <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 
19 #include "filter_filter.h"
20 #include "measure_filter.h"
21 #include "process_filter.h"
22 #include "slice_filter.h"
23 #include "trace_streamer_selector.h"
24 
25 using namespace testing::ext;
26 using namespace SysTuning::TraceStreamer;
27 namespace SysTuning {
28 namespace TraceStreamer {
29 uint64_t TS_01 = 168758663018000;
30 uint32_t PID_01 = 2532;
31 uint32_t TGID_01 = 2519;
32 uint64_t TS_02 = 168758663028000;
33 uint32_t PID_02 = 2533;
34 uint32_t TGID_02 = 2529;
35 uint64_t TS_03 = 168758679303000;
36 uint64_t TS_04 = 168758682456000;
37 uint64_t TS_05 = 168758682466000;
38 uint64_t TS_06 = 168758679343000;
39 uint64_t TS_07 = 168758682456000;
40 uint64_t TS_08 = 168758682466000;
41 uint64_t TS_09 = 168758682476000;
42 uint64_t TS_10 = 168758679343000;
43 uint64_t TS_11 = 168758679344000;
44 uint64_t TS_12 = 168758689323000;
45 
46 class SliceFilterTest : public ::testing::Test {
47 public:
SetUp()48     void SetUp()
49     {
50         stream_.InitFilter();
51     }
52 
TearDown()53     void TearDown() {}
54 
55 public:
56     TraceStreamerSelector stream_;
57 };
58 
59 /**
60  * @tc.name: SliceTestOnceCall
61  * @tc.desc: Parse once method call stack
62  * @tc.type: FUNC
63  */
64 HWTEST_F(SliceFilterTest, SliceTestOnceCall, TestSize.Level1)
65 {
66     TS_LOGI("test28-1");
67     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
68     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("call_function_one");
69     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_01, PID_01, TGID_01, cat, splitStrIndex);
70     stream_.streamFilters_->sliceFilter_->EndSlice(TS_02, PID_01, TGID_01);
71     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
72     EXPECT_TRUE(slices->Size() == 1);
73     EXPECT_TRUE(slices->DursData()[0] == TS_02 - TS_01);
74 }
75 
76 /**
77  * @tc.name: SliceTestNestedTwoMethod
78  * @tc.desc: Parse Nested call stack of two methods
79  * @tc.type: FUNC
80  */
81 HWTEST_F(SliceFilterTest, SliceTestNestedTwoMethod, TestSize.Level1)
82 {
83     TS_LOGI("test28-2");
84     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
85     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("call_function_one");
86     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_01, PID_01, TGID_01, cat, splitStrIndex);
87     splitStrIndex = stream_.traceDataCache_->GetDataIndex("call_function_two");
88     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_02, PID_01, TGID_01, cat, splitStrIndex);
89     stream_.streamFilters_->sliceFilter_->EndSlice(TS_03, PID_01, TGID_01);
90     stream_.streamFilters_->sliceFilter_->EndSlice(TS_04, PID_01, TGID_01);
91     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
92     EXPECT_TRUE(slices->Size() == 2);
93     EXPECT_TRUE(slices->DursData()[0] == TS_04 - TS_01);
94     EXPECT_TRUE(slices->DursData()[1] == TS_03 - TS_02);
95     EXPECT_TRUE(slices->Depths()[1] == 1);
96 }
97 
98 /**
99  * @tc.name: SliceTestNestedTwoMethodStackAndOneMethodStack
100  * @tc.desc: Parse Nested call stack of two methods and one method call stack
101  * @tc.type: FUNC
102  */
103 HWTEST_F(SliceFilterTest, SliceTestNestedTwoMethodStackAndOneMethodStack, TestSize.Level1)
104 {
105     TS_LOGI("test28-3");
106     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
107     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_one");
108     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_01, PID_01, TGID_01, cat, splitStrIndex); // slice 0
109     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_one");
110     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_02, PID_02, TGID_02, cat, splitStrIndex);
111     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_two");
112     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_03, PID_01, TGID_01, cat, splitStrIndex); // slice 2
113     // end thread_one_call_function_two
114     stream_.streamFilters_->sliceFilter_->EndSlice(TS_04, PID_01, TGID_01);
115     // end thread_one_call_function_one
116     stream_.streamFilters_->sliceFilter_->EndSlice(TS_05, PID_01, TGID_01);
117     // end thread_two_call_function_one slice 1
118     stream_.streamFilters_->sliceFilter_->EndSlice(TS_06, PID_02, TGID_02);
119     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
120     EXPECT_TRUE(slices->Size() == 3);
121     EXPECT_TRUE(slices->DursData()[0] == TS_05 - TS_01); // slice 0
122     EXPECT_TRUE(slices->Depths()[0] == 0);
123     EXPECT_TRUE(slices->DursData()[1] == TS_06 - TS_02); // slice 1
124     EXPECT_TRUE(slices->Depths()[1] == 0);
125     EXPECT_TRUE(slices->DursData()[2] == TS_04 - TS_03); // slice 2
126     EXPECT_TRUE(slices->Depths()[2] == 1);
127 }
128 
129 /**
130  * @tc.name: SliceTestWithoutBeginSlice
131  * @tc.desc: Test EndSlice without BeginSlice
132  * @tc.type: FUNC
133  */
134 HWTEST_F(SliceFilterTest, SliceTestWithoutBeginSlice, TestSize.Level1)
135 {
136     TS_LOGI("test28-4");
137     stream_.streamFilters_->sliceFilter_->EndSlice(TS_01, PID_01, TGID_01);
138     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
139     EXPECT_TRUE(slices->Size() == 0);
140 }
141 
142 /**
143  * @tc.name: SliceTestWithMultiNestedCall
144  * @tc.desc: Parse multi nested call stack
145  * @tc.type: FUNC
146  */
147 HWTEST_F(SliceFilterTest, SliceTestWithMultiNestedCall, TestSize.Level1)
148 {
149     TS_LOGI("test28-5");
150     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
151     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_one");
152     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_01, PID_01, TGID_01, cat, splitStrIndex); // slice 0
153     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_one");
154     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_02, PID_02, TGID_02, cat, splitStrIndex);
155     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_two");
156     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_03, PID_01, TGID_01, cat, splitStrIndex); // slice 2
157     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_two");
158     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_04, PID_02, TGID_02, cat, splitStrIndex);
159     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_three");
160     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_05, PID_01, TGID_01, cat, splitStrIndex); // slice 4
161     splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_three");
162     stream_.streamFilters_->sliceFilter_->BeginSlice("comm", TS_06, PID_02, TGID_02, cat, splitStrIndex);
163     // end thread_one_call_function_three
164     stream_.streamFilters_->sliceFilter_->EndSlice(TS_07, PID_01, TGID_01);
165     // end thread_one_call_function_two
166     stream_.streamFilters_->sliceFilter_->EndSlice(TS_08, PID_01, TGID_01);
167     // end thread_one_call_function_one
168     stream_.streamFilters_->sliceFilter_->EndSlice(TS_09, PID_01, TGID_01);
169     // end thread_two_call_function_three slice 5
170     stream_.streamFilters_->sliceFilter_->EndSlice(TS_10, PID_02, TGID_02);
171     // end thread_two_call_function_two slice 3
172     stream_.streamFilters_->sliceFilter_->EndSlice(TS_11, PID_02, TGID_02);
173     // end thread_two_call_function_one slice 1
174     stream_.streamFilters_->sliceFilter_->EndSlice(TS_12, PID_02, TGID_02);
175     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
176     EXPECT_TRUE(slices->Size() == 6);
177     EXPECT_TRUE(slices->DursData()[0] == TS_09 - TS_01); // slice 0
178     EXPECT_TRUE(slices->Depths()[0] == 0);
179     EXPECT_TRUE(slices->DursData()[1] == TS_12 - TS_02); // slice 1
180     EXPECT_TRUE(slices->Depths()[1] == 0);
181     EXPECT_TRUE(slices->DursData()[2] == TS_08 - TS_03); // slice 2
182     EXPECT_TRUE(slices->Depths()[2] == 1);
183     EXPECT_TRUE(slices->DursData()[3] == TS_11 - TS_04); // slice 3
184     EXPECT_TRUE(slices->Depths()[3] == 1);
185     EXPECT_TRUE(slices->DursData()[4] == TS_07 - TS_05); // slice 4
186     EXPECT_TRUE(slices->DursData()[5] == TS_10 - TS_06); // slice 5
187 }
188 
189 /**
190  * @tc.name: AsyncTest
191  * @tc.desc: Test once asynchronous method call stack
192  * @tc.type: FUNC
193  */
194 HWTEST_F(SliceFilterTest, AsyncTest, TestSize.Level1)
195 {
196     TS_LOGI("test28-6");
197     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
198     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("async_call_function_one");
199     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_01, PID_01, TGID_01, cat, splitStrIndex); // slice 0
200     splitStrIndex = stream_.traceDataCache_->GetDataIndex("async_call_function_one");
201     // end thread_one_call_function_three
202     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_02, PID_01, TGID_01, cat, splitStrIndex);
203     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
204     EXPECT_TRUE(slices->Size() == 1);
205     EXPECT_TRUE(slices->DursData()[0] == TS_02 - TS_01); // slice 0
206 }
207 
208 /**
209  * @tc.name: FinishAsyncSliceWithoutStart
210  * @tc.desc: Finish async slice without start
211  * @tc.type: FUNC
212  */
213 HWTEST_F(SliceFilterTest, FinishAsyncSliceWithoutStart, TestSize.Level1)
214 {
215     TS_LOGI("test28-7");
216     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
217     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("async_call_function_one");
218     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_01, PID_01, TGID_01, cat, splitStrIndex);
219     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
220     EXPECT_TRUE(slices->Size() == 0);
221 }
222 
223 /**
224  * @tc.name: AsyncTestTwiceCallStack
225  * @tc.desc: Test Twice asynchronous call stack
226  * @tc.type: FUNC
227  */
228 HWTEST_F(SliceFilterTest, AsyncTestTwiceCallStack, TestSize.Level1)
229 {
230     TS_LOGI("test28-8");
231     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
232     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_one");
233     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_01, PID_01, TGID_01, cat, splitStrIndex); // slice 0
234     DataIndex index2 = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_one");
235     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_02, PID_02, TGID_02, cat, index2);
236     // end thread_one_call_function_three
237     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_03, PID_01, TGID_01, cat, splitStrIndex);
238     // end thread_two_call_function_three slice 5
239     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_04, PID_02, TGID_02, cat, index2);
240     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
241     EXPECT_TRUE(slices->Size() == 2);
242     EXPECT_TRUE(slices->DursData()[0] == TS_03 - TS_01); // slice 0
243     EXPECT_TRUE(slices->Depths()[0] == 0);
244     EXPECT_TRUE(slices->DursData()[1] == TS_04 - TS_02); // slice 1
245 }
246 
247 /**
248  * @tc.name: BeginAsyncSliceThreeTimes
249  * @tc.desc: Test asynchronous call three times
250  * @tc.type: FUNC
251  */
252 HWTEST_F(SliceFilterTest, BeginAsyncSliceThreeTimes, TestSize.Level1)
253 {
254     TS_LOGI("test28-9");
255     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
256     DataIndex splitStrIndex = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_one");
257     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_01, PID_01, TGID_01, cat, splitStrIndex); // slice 0
258     DataIndex index2 = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_one");
259     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_02, PID_02, TGID_02, cat, index2);
260     DataIndex index3 = stream_.traceDataCache_->GetDataIndex("thread_three_call_function_two");
261     DataIndex cat2 = stream_.traceDataCache_->GetDataIndex("Catalog2");
262     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_03, PID_01, TGID_01, cat2, index3); // slice 2
263     // end thread_one_call_function_three
264     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_04, PID_01, TGID_01, cat, splitStrIndex);
265     // end thread_one_call_function_two
266     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_05, PID_01, TGID_01, cat2, index3);
267     // end thread_two_call_function_three slice 5
268     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_06, PID_02, TGID_02, cat, index2);
269     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
270     EXPECT_TRUE(slices->Size() == 3);
271     EXPECT_TRUE(slices->DursData()[0] == TS_04 - TS_01); // slice 0
272     EXPECT_TRUE(slices->Depths()[0] == 0);
273     EXPECT_TRUE(slices->DursData()[1] == TS_06 - TS_02); // slice 1
274     EXPECT_TRUE(slices->Depths()[1] == 0);
275     EXPECT_TRUE(slices->DursData()[2] == TS_05 - TS_03); // slice 2
276 }
277 
278 /**
279  * @tc.name: BeginSliceMultiTimes
280  * @tc.desc: Test asynchronous call muti times
281  * @tc.type: FUNC
282  */
283 HWTEST_F(SliceFilterTest, BeginSliceMultiTimes, TestSize.Level1)
284 {
285     TS_LOGI("test28-10");
286     DataIndex cat = stream_.traceDataCache_->GetDataIndex("Catalog");
287     DataIndex index = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_one");
288     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_01, PID_01, TGID_01, cat, index); // slice 0
289     DataIndex index2 = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_one");
290     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_02, PID_02, TGID_02, cat, index2); // slice 1
291     DataIndex index3 = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_two");
292     DataIndex cat2 = stream_.traceDataCache_->GetDataIndex("Catalog2");
293     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_03, PID_01, TGID_01, cat2, index3); // slice 2
294     DataIndex index4 = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_two");
295     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_04, PID_02, TGID_02, cat2, index4); // slice 3
296     DataIndex index5 = stream_.traceDataCache_->GetDataIndex("thread_one_call_function_three");
297     DataIndex cat3 = stream_.traceDataCache_->GetDataIndex("Catalog3");
298     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_05, PID_01, TGID_01, cat3, index5); // slice 4
299     DataIndex index6 = stream_.traceDataCache_->GetDataIndex("thread_two_call_function_three");
300     stream_.streamFilters_->sliceFilter_->StartAsyncSlice(TS_06, PID_02, TGID_02, cat3, index6); // slice 5
301 
302     // end thread_one_call_function_three
303     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_07, PID_01, TGID_01, cat, index); // end slice 0
304     // end thread_one_call_function_two
305     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_08, PID_02, TGID_02, cat, index2); // end slice 1
306     // end thread_one_call_function_one
307     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_09, PID_01, TGID_01, cat2, index3); // end slice 2
308     // end thread_two_call_function_three slice 5
309     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_10, PID_02, TGID_02, cat2, index4); // end slice 3
310     // end thread_two_call_function_two slice 3
311     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_11, PID_01, TGID_01, cat3, index5); // end slice 4
312     // end thread_two_call_function_one slice 1
313     stream_.streamFilters_->sliceFilter_->FinishAsyncSlice(TS_12, PID_02, TGID_02, cat3, index6); // end slice 5
314 
315     auto slices = stream_.traceDataCache_->GetInternalSlicesData();
316     EXPECT_TRUE(slices->Size() == 6);
317     EXPECT_TRUE(slices->DursData()[0] == TS_07 - TS_01); // slice 0
318     EXPECT_TRUE(slices->Depths()[0] == 0);
319     EXPECT_TRUE(slices->DursData()[1] == TS_08 - TS_02); // slice 1
320     EXPECT_TRUE(slices->Depths()[1] == 0);
321     EXPECT_TRUE(slices->DursData()[2] == TS_09 - TS_03); // slice 2
322     EXPECT_TRUE(slices->Depths()[2] == 0);
323     EXPECT_TRUE(slices->DursData()[3] == TS_10 - TS_04); // slice 3
324     EXPECT_TRUE(slices->Depths()[3] == 0);
325     EXPECT_TRUE(slices->DursData()[4] == TS_11 - TS_05); // slice 4
326     EXPECT_TRUE(slices->DursData()[5] == TS_12 - TS_06); // slice 5
327 }
328 } // namespace TraceStreamer
329 } // namespace SysTuning
330