• 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 "hitrace/hitracec.h"
17 
18 #include <gtest/gtest.h>
19 
20 namespace OHOS {
21 namespace HiviewDFX {
22 using namespace testing::ext;
23 
24 #define HITRACE_DEBUG
25 #ifndef HITRACE_DEBUG
26 #define PRINT_ID(p)
27 #else
28 #define PRINT_ID(p)                                                                                                \
29     printf(#p " valid:%d, ver:%d, chain:0x%llx, flags:%x, span:0x%x, pspan:0x%x.\n", static_cast<int>((p)->valid), \
30            static_cast<int>((p)->ver), static_cast<long long>((p)->chainId), static_cast<int>((p)->flags),         \
31            static_cast<int>((p)->spanId), static_cast<int>((p)->parentSpanId))
32 #endif
33 
34 class HiTraceCTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase()42 void HiTraceCTest::SetUpTestCase()
43 {}
44 
TearDownTestCase()45 void HiTraceCTest::TearDownTestCase()
46 {}
47 
SetUp()48 void HiTraceCTest::SetUp()
49 {
50     HiTraceClearId();
51 }
52 
TearDown()53 void HiTraceCTest::TearDown()
54 {}
55 
56 /**
57  * @tc.name: Dfx_HiTraceCTest_IdTest_001
58  * @tc.desc: Get, set and clear trace id
59  * @tc.type: FUNC
60  * @tc.require: AR000CQVA0
61  */
62 HWTEST_F(HiTraceCTest, IdTest_001, TestSize.Level1)
63 {
64     /**
65      * @tc.steps: step1. get and validate trace id.
66      * @tc.expected: step1. trace id is invalid.
67      * @tc.steps: step2. construct trace id with chain id, span id, parent span id
68      *     and flags and set it into context, then get and validate it.
69      * @tc.expected: step2. trace id is valid with same chain id, span id, parent
70      *     span id and flags.
71      * @tc.steps: step3. construct invalid trace id and set into context, then get
72      *     and validate it.
73      * @tc.expected: step3. trace id is the same with step2.
74      * @tc.steps: step4. clear trace id, then get and validate it.
75      * @tc.expected: step4. trace id is invalid.
76      */
77     HiTraceIdStruct initId = HiTraceGetId();
78     EXPECT_EQ(0, HiTraceIsValid(&initId));
79     PRINT_ID(&initId);
80 
81     // set thread id
82     constexpr uint64_t chainId = 0xABCDEF;
83     constexpr uint64_t spanId = 0x12345;
84     constexpr uint64_t parentSpanId = 0x67890;
85     constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
86     HiTraceIdStruct setId;
87     HiTraceInitId(&setId);
88     HiTraceSetChainId(&setId, chainId);
89     HiTraceSetFlags(&setId, flags);
90     HiTraceSetSpanId(&setId, spanId);
91     HiTraceSetParentSpanId(&setId, parentSpanId);
92     PRINT_ID(&setId);
93 
94     HiTraceSetId(&setId);
95 
96     HiTraceIdStruct getId = HiTraceGetId();
97     EXPECT_EQ(1, HiTraceIsValid(&getId));
98     EXPECT_EQ(chainId, HiTraceGetChainId(&getId));
99     EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&getId));
100     EXPECT_EQ(spanId, HiTraceGetSpanId(&getId));
101     EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&getId));
102     PRINT_ID(&getId);
103 
104     // set invalid id
105     HiTraceIdStruct invalidId;
106     HiTraceInitId(&invalidId);
107 
108     HiTraceSetId(&invalidId);
109 
110     getId = HiTraceGetId();
111     EXPECT_EQ(1, HiTraceIsValid(&getId));
112     EXPECT_EQ(chainId, HiTraceGetChainId(&getId));
113     EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&getId));
114     EXPECT_EQ(spanId, HiTraceGetSpanId(&getId));
115     EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&getId));
116     PRINT_ID(&getId);
117 
118     // clear thread id
119     HiTraceClearId();
120 
121     HiTraceIdStruct clearId = HiTraceGetId();
122     EXPECT_EQ(0, HiTraceIsValid(&clearId));
123     PRINT_ID(&clearId);
124 }
125 
126 /**
127  * @tc.name: Dfx_HiTraceCTest_IntfTest_001
128  * @tc.desc: Interconversion between trace id and bytes array.
129  * @tc.type: FUNC
130  * @tc.require: AR000CQV9T
131  */
132 HWTEST_F(HiTraceCTest, IntfTest_001, TestSize.Level1)
133 {
134     /**
135      * @tc.steps: step1. construct trace id and validate it.
136      * @tc.expected: step1. trace id is valid.
137      * @tc.steps: step2. convert trace id to bytes array.
138      * @tc.expected: step2. convert success when array size >= id length.
139      * @tc.steps: step3. convert bytes array to trace id.
140      * @tc.expected: step3. convert success only when array size == id length.
141      * @tc.steps: step4. convert invalid id to bytes array.
142      * @tc.expected: step4. convert fail.
143      * @tc.steps: step5. convert invalid bytes array to id.
144      * @tc.expected: step5. convert fail.
145      */
146     // id to bytes
147     constexpr uint64_t chainId = 0xABCDEF;
148     constexpr uint64_t spanId = 0x12345;
149     constexpr uint64_t parentSpanId = 0x67890;
150     constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
151     HiTraceIdStruct id = {HITRACE_ID_VALID, HITRACE_VER_1, chainId, flags, spanId, parentSpanId};
152     EXPECT_EQ(1, HiTraceIsValid(&id));
153     PRINT_ID(&id);
154 
155     constexpr int idLen = sizeof(HiTraceIdStruct);
156     uint8_t bytes[idLen + 1];
157     int len = HiTraceIdToBytes(&id, bytes, idLen - 1);
158     EXPECT_EQ(0, len);
159     len = HiTraceIdToBytes(&id, bytes, idLen + 1);
160     EXPECT_EQ(idLen, len);
161     len = HiTraceIdToBytes(&id, bytes, idLen);
162     EXPECT_EQ(idLen, len);
163     PRINT_ID(reinterpret_cast<HiTraceIdStruct*>(bytes));
164 
165     // bytes to id
166     HiTraceIdStruct bytesToId = HiTraceBytesToId(bytes, idLen - 1);
167     EXPECT_EQ(0, HiTraceIsValid(&bytesToId));
168     bytesToId = HiTraceBytesToId(bytes, idLen + 1);
169     EXPECT_EQ(0, HiTraceIsValid(&bytesToId));
170     bytesToId = HiTraceBytesToId(bytes, idLen);
171     EXPECT_EQ(1, HiTraceIsValid(&bytesToId));
172     EXPECT_EQ(chainId, HiTraceGetChainId(&bytesToId));
173     EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceGetFlags(&bytesToId));
174     EXPECT_EQ(spanId, HiTraceGetSpanId(&bytesToId));
175     EXPECT_EQ(parentSpanId, HiTraceGetParentSpanId(&bytesToId));
176     PRINT_ID(&bytesToId);
177 
178     // set invalid id
179     HiTraceIdStruct invalidId;
180     HiTraceInitId(&invalidId);
181     EXPECT_EQ(0, HiTraceIdToBytes(&invalidId, bytes, idLen));
182     invalidId = HiTraceBytesToId(nullptr, idLen);
183     EXPECT_EQ(0, HiTraceIsValid(&invalidId));
184 }
185 
186 /**
187  * @tc.name: Dfx_HiTraceCTest_IntfTest_002
188  * @tc.desc: Start and stop trace.
189  * @tc.type: FUNC
190  * @tc.require: AR000CQV9T
191  */
192 HWTEST_F(HiTraceCTest, IntfTest_002, TestSize.Level1)
193 {
194     /**
195      * @tc.steps: step1. start trace with flags, get trace id and validit it.
196      * @tc.expected: step1. trace id and flags is valid.
197      * @tc.steps: step2. stop trace, get trace id and validit it.
198      * @tc.expected: step2. trace id is invalid.
199      */
200     // begin
201     HiTraceIdStruct beginId = HiTraceBegin("test", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_NO_BE_INFO);
202     EXPECT_EQ(1, HiTraceIsValid(&beginId));
203     EXPECT_EQ(1, HiTraceIsFlagEnabled(&beginId, HITRACE_FLAG_INCLUDE_ASYNC));
204     EXPECT_EQ(1, HiTraceIsFlagEnabled(&beginId, HITRACE_FLAG_NO_BE_INFO));
205     PRINT_ID(&beginId);
206 
207     // end
208     HiTraceEnd(&beginId);
209 
210     HiTraceIdStruct endId = HiTraceGetId();
211     EXPECT_EQ(0, HiTraceIsValid(&endId));
212     PRINT_ID(&endId);
213 }
214 
215 /**
216  * @tc.name: Dfx_HiTraceCTest_IntfTest_003
217  * @tc.desc: Start and stop trace with reentered.
218  * @tc.type: FUNC
219  * @tc.require: AR000CQV9T
220  */
221 HWTEST_F(HiTraceCTest, IntfTest_003, TestSize.Level1)
222 {
223     /**
224      * @tc.steps: step1. start trace twice and get 2nd trace id.
225      * @tc.expected: step1. 2nd trace is invalid.
226      * @tc.steps: step2. get trace id and check.
227      * @tc.expected: step2. trace id is valid and same with 1st id.
228      * @tc.steps: step3. set chain id with wrong id and get trace id.
229      * @tc.expected: step3. trace id is valid and same with 1st id.
230      * @tc.steps: step4. stop trace twice and get trace id.
231      * @tc.expected: step4. trace id is invalid.
232      */
233     HiTraceIdStruct beginId = HiTraceBegin("begin", HITRACE_FLAG_INCLUDE_ASYNC);
234     PRINT_ID(&beginId);
235 
236     // reenter begin
237     HiTraceIdStruct reBeginId = HiTraceBegin("reenter begin", HITRACE_FLAG_TP_INFO);
238     EXPECT_EQ(0, HiTraceIsValid(&reBeginId));
239     EXPECT_NE(HiTraceGetChainId(&reBeginId), HiTraceGetChainId(&beginId));
240     EXPECT_EQ(0, HiTraceIsFlagEnabled(&reBeginId, HITRACE_FLAG_INCLUDE_ASYNC));
241     EXPECT_EQ(0, HiTraceIsFlagEnabled(&reBeginId, HITRACE_FLAG_TP_INFO));
242     PRINT_ID(&reBeginId);
243 
244     // reenter end
245     HiTraceEnd(&reBeginId);
246 
247     HiTraceIdStruct endId = HiTraceGetId();
248     EXPECT_EQ(1, HiTraceIsValid(&endId));
249     EXPECT_EQ(HiTraceGetChainId(&endId), HiTraceGetChainId(&beginId));
250     EXPECT_EQ(1, HiTraceIsFlagEnabled(&endId, HITRACE_FLAG_INCLUDE_ASYNC));
251     EXPECT_EQ(0, HiTraceIsFlagEnabled(&endId, HITRACE_FLAG_TP_INFO));
252     PRINT_ID(&endId);
253 
254     // end with wrong chainId
255     HiTraceIdStruct wrongBeginId = beginId;
256     HiTraceSetChainId(&wrongBeginId, HiTraceGetChainId(&beginId) + 1);
257     HiTraceEnd(&wrongBeginId);
258 
259     HiTraceIdStruct wrongEndId = HiTraceGetId();
260     EXPECT_EQ(1, HiTraceIsValid(&wrongEndId));
261     EXPECT_EQ(HiTraceGetChainId(&wrongEndId), HiTraceGetChainId(&beginId));
262     EXPECT_EQ(1, HiTraceIsFlagEnabled(&wrongEndId, HITRACE_FLAG_INCLUDE_ASYNC));
263     PRINT_ID(&wrongEndId);
264 
265     // end
266     HiTraceEnd(&beginId);
267 
268     HiTraceIdStruct reEndId = HiTraceGetId();
269     EXPECT_EQ(0, HiTraceIsValid(&reEndId));
270     PRINT_ID(&reEndId);
271 
272     // end with invalid thread id
273     HiTraceEnd(&beginId);
274 
275     HiTraceIdStruct endInvalidId = HiTraceGetId();
276     EXPECT_EQ(0, HiTraceIsValid(&endInvalidId));
277     PRINT_ID(&endInvalidId);
278 }
279 
280 /**
281  * @tc.name: Dfx_HiTraceCTest_SpanTest_001
282  * @tc.desc: Create child and grand child span.
283  * @tc.type: FUNC
284  * @tc.require: AR000CQVA2
285  */
286 HWTEST_F(HiTraceCTest, SpanTest_001, TestSize.Level1)
287 {
288     /**
289      * @tc.steps: step1. start trace without HITRACE_FLAG_DONOT_CREATE_SPAN,
290      *     get and check flags.
291      * @tc.expected: step1. flags is same with set and span id is 0.
292      * @tc.steps: step2. create child id.
293      * @tc.expected: step2. child id has same span id with parent.
294      * @tc.steps: step3. set child id into context.
295      * @tc.steps: step4. create grand child id.
296      * @tc.expected: step4. grand child id has same span id with parent and child.
297      */
298     HiTraceIdStruct id = HiTraceBegin("test", 0);
299     EXPECT_EQ(0, HiTraceGetFlags(&id));
300     EXPECT_EQ(0UL, HiTraceGetSpanId(&id));
301     EXPECT_EQ(0UL, HiTraceGetParentSpanId(&id));
302     PRINT_ID(&id);
303 
304     // create child span
305     HiTraceIdStruct childId = HiTraceCreateSpan();
306     EXPECT_EQ(1, HiTraceIsValid(&childId));
307     EXPECT_EQ(HiTraceGetFlags(&childId), HiTraceGetFlags(&id));
308     EXPECT_EQ(HiTraceGetChainId(&childId), HiTraceGetChainId(&id));
309     EXPECT_EQ(HiTraceGetParentSpanId(&childId), HiTraceGetSpanId(&id));
310     PRINT_ID(&childId);
311 
312     // set child id to thread id
313     HiTraceSetId(&childId);
314 
315     // continue to create child span
316     HiTraceIdStruct grandChildId = HiTraceCreateSpan();
317     EXPECT_EQ(1, HiTraceIsValid(&grandChildId));
318     EXPECT_EQ(HiTraceGetFlags(&grandChildId), HiTraceGetFlags(&id));
319     EXPECT_EQ(HiTraceGetChainId(&grandChildId), HiTraceGetChainId(&id));
320     EXPECT_EQ(HiTraceGetParentSpanId(&grandChildId), HiTraceGetSpanId(&childId));
321     PRINT_ID(&grandChildId);
322 
323     // end
324     HiTraceEnd(&id);
325 }
326 
327 /**
328  * @tc.name: Dfx_HiTraceCTest_SpanTest_002
329  * @tc.desc: Start and stop trace with reentered.
330  * @tc.type: FUNC
331  * @tc.require: AR000CQVA2
332  */
333 HWTEST_F(HiTraceCTest, SpanTest_002, TestSize.Level1)
334 {
335     /**
336      * @tc.steps: step1. start trace with HITRACE_FLAG_DONOT_CREATE_SPAN,
337      *     get and check flags.
338      * @tc.expected: step1. HITRACE_FLAG_DONOT_CREATE_SPAN is enabled.
339      * @tc.steps: step2. create child id.
340      * @tc.expected: step2. child id is same with parent id.
341      */
342     // begin with "donot create span" flag
343     HiTraceIdStruct id = HiTraceBegin("test", HITRACE_FLAG_DONOT_CREATE_SPAN);
344     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_DONOT_CREATE_SPAN));
345     PRINT_ID(&id);
346 
347     // create child span
348     HiTraceIdStruct childId = HiTraceCreateSpan();
349     EXPECT_EQ(1, HiTraceIsValid(&childId));
350     EXPECT_EQ(HiTraceGetFlags(&childId), HiTraceGetFlags(&id));
351     EXPECT_EQ(HiTraceGetChainId(&childId), HiTraceGetChainId(&id));
352     EXPECT_EQ(HiTraceGetSpanId(&childId), HiTraceGetSpanId(&id));
353     EXPECT_EQ(HiTraceGetParentSpanId(&childId), HiTraceGetParentSpanId(&id));
354     PRINT_ID(&childId);
355 
356     // end
357     HiTraceEnd(&id);
358 }
359 
360 /**
361  * @tc.name: Dfx_HiTraceCTest_TracepointTest_001
362  * @tc.desc: Start trace with HITRACE_FLAG_TP_INFO flag.
363  * @tc.type: FUNC
364  * @tc.require: AR000CQVA3
365  */
366 HWTEST_F(HiTraceCTest, TracepointTest_001, TestSize.Level1)
367 {
368     /**
369      * @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO,
370      *     get and check flags.
371      * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
372      * @tc.steps: step2. add trace point info with id and check logs.
373      * @tc.expected: step2. trace point can be found in logs.
374      * @tc.steps: step2. add trace point info with null and check logs.
375      * @tc.expected: step2. trace point cannot be found in logs.
376      */
377     HiTraceIdStruct id = HiTraceBegin("test tp flag", HITRACE_FLAG_TP_INFO);
378     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
379     HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
380 
381     HiTraceTracepoint(HITRACE_TP_CS, nullptr, "client send msg content %d", 12);
382 
383     HiTraceEnd(&id);
384 }
385 
386 /**
387  * @tc.name: Dfx_HiTraceCTest_TracepointTest_002
388  * @tc.desc: Start trace without HITRACE_FLAG_TP_INFO flag.
389  * @tc.type: FUNC
390  * @tc.require: AR000CQVA3
391  */
392 HWTEST_F(HiTraceCTest, TracepointTest_002, TestSize.Level1)
393 {
394     /**
395      * @tc.steps: step1. start trace without HITRACE_FLAG_TP_INFO flag.
396      *     get and check flags.
397      * @tc.expected: step1. HITRACE_FLAG_TP_INFO is not enabled.
398      * @tc.steps: step2. add trace point info with id and check logs.
399      * @tc.expected: step2. trace point cannot be found in logs.
400      */
401     // begin with tp flag
402     HiTraceIdStruct id = HiTraceBegin("test no tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
403     EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
404     HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
405 
406     HiTraceEnd(&id);
407 }
408 
409 /**
410  * @tc.name: Dfx_HiTraceCTest_TracepointTest_003
411  * @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO flag.
412  * @tc.type: FUNC
413  * @tc.require: AR000CQVA3
414  */
415 HWTEST_F(HiTraceCTest, TracepointTest_003, TestSize.Level1)
416 {
417     /**
418      * @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO,
419      *     get and check flags.
420      * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
421      * @tc.steps: step2. add D2D trace point info with id and check logs.
422      * @tc.expected: step2. trace point can be found in logs.
423      * @tc.steps: step2. add D2D trace point info with null and check logs.
424      * @tc.expected: step2. trace point cannot be found in logs.
425      * @tc.steps: step3. add trace point info with id and check logs.
426      * @tc.expected: step3. trace point cannot be found in logs.
427      */
428     HiTraceIdStruct id = HiTraceBegin("test D2D tp flag", HITRACE_FLAG_D2D_TP_INFO);
429     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
430     HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
431     HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
432     HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
433     HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
434 
435     HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, nullptr, "cannot be found %d", 13);
436 
437     HiTraceTracepoint(HITRACE_TP_CS, &id, "cannot be found %d", 14);
438 
439     HiTraceEnd(&id);
440 }
441 
442 /**
443  * @tc.name: Dfx_HiTraceCTest_TracepointTest_004
444  * @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO flag.
445  * @tc.type: FUNC
446  * @tc.require: AR000CQVA3
447  */
448 HWTEST_F(HiTraceCTest, TracepointTest_004, TestSize.Level1)
449 {
450     /**
451      * @tc.steps: step1. start trace without HITRACE_FLAG_D2D_TP_INFO flag.
452      *     get and check flags.
453      * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
454      * @tc.steps: step2. add D2D trace point info with id and check logs.
455      * @tc.expected: step2. trace point cannot be found in logs.
456      */
457     HiTraceIdStruct id = HiTraceBegin("test no D2D tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
458     EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
459     HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "cannot be found %d", 12);
460     HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
461     HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
462     HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
463 
464     HiTraceEnd(&id);
465 }
466 
467 /**
468  * @tc.name: Dfx_HiTraceCTest_TracepointTest_005
469  * @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO and HITRACE_FLAG_TP_INFO flag.
470  * @tc.type: FUNC
471  * @tc.require: AR000CQVA3
472  */
473 HWTEST_F(HiTraceCTest, TracepointTest_005, TestSize.Level1)
474 {
475     /**
476      * @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO,
477      *     get and check flags.
478      * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
479      * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
480      * @tc.steps: step2. add D2D trace point info with id and check logs.
481      * @tc.expected: step2. trace point can be found in logs.
482      * @tc.steps: step3. add trace point info with id and check logs.
483      * @tc.expected: step3. trace point can be found in logs.
484      */
485     HiTraceIdStruct id = HiTraceBegin("test D2D | TP tp flag", HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO);
486     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
487     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
488     HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
489     HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
490     HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
491     HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
492 
493     HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
494 
495     HiTraceEnd(&id);
496 }
497 
498 /**
499  * @tc.name: Dfx_HiTraceCTest_TracepointTest_006
500  * @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO, but with HITRACE_FLAG_TP_INFO flag.
501  * @tc.type: FUNC
502  * @tc.require: AR000CQVA3
503  */
504 HWTEST_F(HiTraceCTest, TracepointTest_006, TestSize.Level1)
505 {
506     /**
507      * @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO flag.
508      *     get and check flags.
509      * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
510      * * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
511      * @tc.steps: step2. add D2D trace point info with id and check logs.
512      * @tc.expected: step2. trace point can be found in logs.
513      * @tc.steps: step2. add trace point info with id and check logs.
514      * @tc.expected: step2. trace point can be found in logs.
515      */
516     HiTraceIdStruct id = HiTraceBegin("test no D2D, but tp flag", HITRACE_FLAG_TP_INFO);
517     EXPECT_EQ(0, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
518     EXPECT_EQ(1, HiTraceIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
519     HiTraceTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
520     HiTraceTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
521     HiTraceTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
522     HiTraceTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
523 
524     HiTraceTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
525 
526     HiTraceEnd(&id);
527 }
528 
529 /**
530  * @tc.name: Dfx_HiTraceCTest_SyncAsyncTest_001
531  * @tc.desc: Start trace with SYNC or ASYNC.
532  * @tc.type: FUNC
533  * @tc.require: AR000CQ0G7
534  */
535 HWTEST_F(HiTraceCTest, SyncAsyncTest_001, TestSize.Level1)
536 {
537     /**
538      * @tc.steps: step1. start trace without HITRACE_FLAG_INCLUDE_ASYNC flag.
539      *    get and check flags.
540      * @tc.expected: step1. HITRACE_FLAG_INCLUDE_ASYNC is not enabled.
541      * @tc.steps: step2. start trace with HITRACE_FLAG_INCLUDE_ASYNC flag.
542      *    get and check flags.
543      * @tc.expected: step2. HITRACE_FLAG_INCLUDE_ASYNC is enabled.
544      */
545     // begin with sync flag
546     HiTraceIdStruct syncId = HiTraceBegin("test sync only", HITRACE_FLAG_TP_INFO);
547     EXPECT_EQ(0, HiTraceIsFlagEnabled(&syncId, HITRACE_FLAG_INCLUDE_ASYNC));
548     HiTraceTracepoint(HITRACE_TP_CS, &syncId, "client send msg: %s", "sync");
549 
550     HiTraceEnd(&syncId);
551 
552     // begin with async flag
553     HiTraceIdStruct asyncId = HiTraceBegin("test sync+async", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
554     EXPECT_EQ(1, HiTraceIsFlagEnabled(&asyncId, HITRACE_FLAG_INCLUDE_ASYNC));
555     HiTraceTracepoint(HITRACE_TP_CS, &asyncId, "client send msg: %s", "async");
556 
557     HiTraceEnd(&asyncId);
558 }
559 }  // namespace HiviewDFX
560 }  // namespace OHOS
561