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