1b0e7dd80Sopenharmony_ci/*
2b0e7dd80Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3b0e7dd80Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4b0e7dd80Sopenharmony_ci * you may not use this file except in compliance with the License.
5b0e7dd80Sopenharmony_ci * You may obtain a copy of the License at
6b0e7dd80Sopenharmony_ci *
7b0e7dd80Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8b0e7dd80Sopenharmony_ci *
9b0e7dd80Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10b0e7dd80Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11b0e7dd80Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12b0e7dd80Sopenharmony_ci * See the License for the specific language governing permissions and
13b0e7dd80Sopenharmony_ci * limitations under the License.
14b0e7dd80Sopenharmony_ci */
15b0e7dd80Sopenharmony_ci
16b0e7dd80Sopenharmony_ci#include "hitrace/hitracechainc.h"
17b0e7dd80Sopenharmony_ci
18b0e7dd80Sopenharmony_ci#include <cstdint>
19b0e7dd80Sopenharmony_ci#include <cstdio>
20b0e7dd80Sopenharmony_ci#include <gtest/gtest.h>
21b0e7dd80Sopenharmony_ci#include <sys/time.h>
22b0e7dd80Sopenharmony_ci
23b0e7dd80Sopenharmony_ci#include "gtest/gtest-message.h"
24b0e7dd80Sopenharmony_ci#include "gtest/gtest-test-part.h"
25b0e7dd80Sopenharmony_ci#include "gtest/gtest_pred_impl.h"
26b0e7dd80Sopenharmony_ci#include "gtest/hwext/gtest-tag.h"
27b0e7dd80Sopenharmony_ci
28b0e7dd80Sopenharmony_ci#define ARRAY_FIRST_INDEX 0
29b0e7dd80Sopenharmony_ci#define ARRAY_SECOND_INDEX 1
30b0e7dd80Sopenharmony_ci#define ARRAY_THIRD_INDEX 2
31b0e7dd80Sopenharmony_ci#define HASH_DATA_LENGTH 3
32b0e7dd80Sopenharmony_ci
33b0e7dd80Sopenharmony_ci#define DEVICE_CLIENT_SEND 12
34b0e7dd80Sopenharmony_ci#define PROCESS_CLIENT_SEND 22
35b0e7dd80Sopenharmony_ci#define THREAD_CLIENT_SEND 32
36b0e7dd80Sopenharmony_ci#define DEFAULT_CLIENT_SEND 42
37b0e7dd80Sopenharmony_ci
38b0e7dd80Sopenharmony_cinamespace OHOS {
39b0e7dd80Sopenharmony_cinamespace HiviewDFX {
40b0e7dd80Sopenharmony_ciusing namespace testing::ext;
41b0e7dd80Sopenharmony_ci
42b0e7dd80Sopenharmony_ci#define HITRACE_DEBUG
43b0e7dd80Sopenharmony_ci#ifndef HITRACE_DEBUG
44b0e7dd80Sopenharmony_ci#define PRINT_ID(p)
45b0e7dd80Sopenharmony_ci#else
46b0e7dd80Sopenharmony_ci#define PRINT_ID(p)                                                                                                \
47b0e7dd80Sopenharmony_ci    printf(#p " valid:%d, ver:%d, chain:0x%llx, flags:%x, span:0x%x, pspan:0x%x.\n", static_cast<int>((p)->valid), \
48b0e7dd80Sopenharmony_ci           static_cast<int>((p)->ver), static_cast<long long>((p)->chainId), static_cast<int>((p)->flags),         \
49b0e7dd80Sopenharmony_ci           static_cast<int>((p)->spanId), static_cast<int>((p)->parentSpanId))
50b0e7dd80Sopenharmony_ci#endif
51b0e7dd80Sopenharmony_ci
52b0e7dd80Sopenharmony_cistatic void HiTraceChainTracepointExWithArgsWrapper(HiTraceCommunicationMode mode, HiTraceTracepointType type,
53b0e7dd80Sopenharmony_ci    const HiTraceIdStruct* pId, const char* fmt, ...)
54b0e7dd80Sopenharmony_ci{
55b0e7dd80Sopenharmony_ci    va_list vaList;
56b0e7dd80Sopenharmony_ci    va_start(vaList, fmt);
57b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgs(mode, type, pId, fmt, vaList);
58b0e7dd80Sopenharmony_ci    va_end(vaList);
59b0e7dd80Sopenharmony_ci}
60b0e7dd80Sopenharmony_ci
61b0e7dd80Sopenharmony_cistatic void HiTraceChainTracepointWithArgsWrapper(HiTraceTracepointType type, const HiTraceIdStruct* pId,
62b0e7dd80Sopenharmony_ci    const char* fmt, ...)
63b0e7dd80Sopenharmony_ci{
64b0e7dd80Sopenharmony_ci    va_list vaList;
65b0e7dd80Sopenharmony_ci    va_start(vaList, fmt);
66b0e7dd80Sopenharmony_ci    HiTraceChainTracepointWithArgs(type, pId, fmt, vaList);
67b0e7dd80Sopenharmony_ci    va_end(vaList);
68b0e7dd80Sopenharmony_ci}
69b0e7dd80Sopenharmony_ci
70b0e7dd80Sopenharmony_cistatic uint64_t HashFunc(const void* pData, uint32_t dataLen)
71b0e7dd80Sopenharmony_ci{
72b0e7dd80Sopenharmony_ci    const uint64_t seed = 131;
73b0e7dd80Sopenharmony_ci    if ((!pData) || dataLen == 0) {
74b0e7dd80Sopenharmony_ci        return 0;
75b0e7dd80Sopenharmony_ci    }
76b0e7dd80Sopenharmony_ci    uint64_t hash = 0;
77b0e7dd80Sopenharmony_ci    uint64_t len = dataLen;
78b0e7dd80Sopenharmony_ci    const char* p = static_cast<const char*>(pData);
79b0e7dd80Sopenharmony_ci    for (; len > 0; --len) {
80b0e7dd80Sopenharmony_ci        hash = (hash * seed) + (*p++);
81b0e7dd80Sopenharmony_ci    }
82b0e7dd80Sopenharmony_ci    return hash;
83b0e7dd80Sopenharmony_ci}
84b0e7dd80Sopenharmony_ci
85b0e7dd80Sopenharmony_cistatic uint64_t GenerateChainId()
86b0e7dd80Sopenharmony_ci{
87b0e7dd80Sopenharmony_ci    const uint64_t randomNum = 269;
88b0e7dd80Sopenharmony_ci    uint64_t hashData[HASH_DATA_LENGTH];
89b0e7dd80Sopenharmony_ci    struct timeval tv;
90b0e7dd80Sopenharmony_ci    gettimeofday(&tv, nullptr);
91b0e7dd80Sopenharmony_ci    srand(tv.tv_sec);
92b0e7dd80Sopenharmony_ci    hashData[ARRAY_FIRST_INDEX] = tv.tv_sec;
93b0e7dd80Sopenharmony_ci    hashData[ARRAY_SECOND_INDEX] = tv.tv_usec;
94b0e7dd80Sopenharmony_ci    hashData[ARRAY_THIRD_INDEX] = randomNum;
95b0e7dd80Sopenharmony_ci    uint64_t hash = HashFunc(hashData, HASH_DATA_LENGTH * sizeof(uint64_t));
96b0e7dd80Sopenharmony_ci    return hash;
97b0e7dd80Sopenharmony_ci}
98b0e7dd80Sopenharmony_ci
99b0e7dd80Sopenharmony_cistatic uint64_t GenerateSpanId()
100b0e7dd80Sopenharmony_ci{
101b0e7dd80Sopenharmony_ci    const uint64_t randomNum = 269;
102b0e7dd80Sopenharmony_ci    uint64_t hashData[HASH_DATA_LENGTH];
103b0e7dd80Sopenharmony_ci    struct timeval tv;
104b0e7dd80Sopenharmony_ci    gettimeofday(&tv, nullptr);
105b0e7dd80Sopenharmony_ci    srand(tv.tv_sec);
106b0e7dd80Sopenharmony_ci    hashData[ARRAY_FIRST_INDEX] = randomNum;
107b0e7dd80Sopenharmony_ci    hashData[ARRAY_SECOND_INDEX] = tv.tv_sec;
108b0e7dd80Sopenharmony_ci    hashData[ARRAY_THIRD_INDEX] = tv.tv_usec;
109b0e7dd80Sopenharmony_ci    uint64_t hash = HashFunc(hashData, HASH_DATA_LENGTH * sizeof(uint64_t));
110b0e7dd80Sopenharmony_ci    return hash;
111b0e7dd80Sopenharmony_ci}
112b0e7dd80Sopenharmony_ci
113b0e7dd80Sopenharmony_cistatic uint64_t GenerateParentSpanId()
114b0e7dd80Sopenharmony_ci{
115b0e7dd80Sopenharmony_ci    const uint64_t randomNum = 269;
116b0e7dd80Sopenharmony_ci    uint64_t hashData[HASH_DATA_LENGTH];
117b0e7dd80Sopenharmony_ci    struct timeval tv;
118b0e7dd80Sopenharmony_ci    gettimeofday(&tv, nullptr);
119b0e7dd80Sopenharmony_ci    srand(tv.tv_sec);
120b0e7dd80Sopenharmony_ci    hashData[ARRAY_FIRST_INDEX] = tv.tv_usec;
121b0e7dd80Sopenharmony_ci    hashData[ARRAY_SECOND_INDEX] = randomNum;
122b0e7dd80Sopenharmony_ci    hashData[ARRAY_THIRD_INDEX] = tv.tv_sec;
123b0e7dd80Sopenharmony_ci    uint64_t hash = HashFunc(hashData, HASH_DATA_LENGTH * sizeof(uint64_t));
124b0e7dd80Sopenharmony_ci    return hash;
125b0e7dd80Sopenharmony_ci}
126b0e7dd80Sopenharmony_ci
127b0e7dd80Sopenharmony_ciclass HiTraceChainCTest : public testing::Test {
128b0e7dd80Sopenharmony_cipublic:
129b0e7dd80Sopenharmony_ci    static void SetUpTestCase();
130b0e7dd80Sopenharmony_ci    static void TearDownTestCase();
131b0e7dd80Sopenharmony_ci    void SetUp();
132b0e7dd80Sopenharmony_ci    void TearDown();
133b0e7dd80Sopenharmony_ci};
134b0e7dd80Sopenharmony_ci
135b0e7dd80Sopenharmony_civoid HiTraceChainCTest::SetUpTestCase()
136b0e7dd80Sopenharmony_ci{}
137b0e7dd80Sopenharmony_ci
138b0e7dd80Sopenharmony_civoid HiTraceChainCTest::TearDownTestCase()
139b0e7dd80Sopenharmony_ci{}
140b0e7dd80Sopenharmony_ci
141b0e7dd80Sopenharmony_civoid HiTraceChainCTest::SetUp()
142b0e7dd80Sopenharmony_ci{
143b0e7dd80Sopenharmony_ci    HiTraceChainClearId();
144b0e7dd80Sopenharmony_ci}
145b0e7dd80Sopenharmony_ci
146b0e7dd80Sopenharmony_civoid HiTraceChainCTest::TearDown()
147b0e7dd80Sopenharmony_ci{}
148b0e7dd80Sopenharmony_ci
149b0e7dd80Sopenharmony_ci/**
150b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_IdTest_001
151b0e7dd80Sopenharmony_ci * @tc.desc: Get, set and clear trace id
152b0e7dd80Sopenharmony_ci * @tc.type: FUNC
153b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA0
154b0e7dd80Sopenharmony_ci */
155b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, IdTest_001, TestSize.Level1)
156b0e7dd80Sopenharmony_ci{
157b0e7dd80Sopenharmony_ci    /**
158b0e7dd80Sopenharmony_ci     * @tc.steps: step1. get and validate trace id.
159b0e7dd80Sopenharmony_ci     * @tc.expected: step1. trace id is invalid.
160b0e7dd80Sopenharmony_ci     * @tc.steps: step2. construct trace id with chain id, span id, parent span id
161b0e7dd80Sopenharmony_ci     *     and flags and set it into context, then get and validate it.
162b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace id is valid with same chain id, span id, parent
163b0e7dd80Sopenharmony_ci     *     span id and flags.
164b0e7dd80Sopenharmony_ci     * @tc.steps: step3. construct invalid trace id and set into context, then get
165b0e7dd80Sopenharmony_ci     *     and validate it.
166b0e7dd80Sopenharmony_ci     * @tc.expected: step3. trace id is the same with step2.
167b0e7dd80Sopenharmony_ci     * @tc.steps: step4. clear trace id, then get and validate it.
168b0e7dd80Sopenharmony_ci     * @tc.expected: step4. trace id is invalid.
169b0e7dd80Sopenharmony_ci     */
170b0e7dd80Sopenharmony_ci    HiTraceIdStruct initId = HiTraceChainGetId();
171b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&initId));
172b0e7dd80Sopenharmony_ci    PRINT_ID(&initId);
173b0e7dd80Sopenharmony_ci
174b0e7dd80Sopenharmony_ci    // set thread id
175b0e7dd80Sopenharmony_ci    constexpr uint64_t chainId = 0xABCDEF;
176b0e7dd80Sopenharmony_ci    constexpr uint64_t spanId = 0x12345;
177b0e7dd80Sopenharmony_ci    constexpr uint64_t parentSpanId = 0x67890;
178b0e7dd80Sopenharmony_ci    constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
179b0e7dd80Sopenharmony_ci    HiTraceIdStruct setId;
180b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&setId);
181b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&setId, chainId);
182b0e7dd80Sopenharmony_ci    HiTraceChainSetFlags(&setId, flags);
183b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&setId, spanId);
184b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&setId, parentSpanId);
185b0e7dd80Sopenharmony_ci    PRINT_ID(&setId);
186b0e7dd80Sopenharmony_ci
187b0e7dd80Sopenharmony_ci    HiTraceChainSetId(&setId);
188b0e7dd80Sopenharmony_ci
189b0e7dd80Sopenharmony_ci    HiTraceIdStruct getId = HiTraceChainGetId();
190b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&getId));
191b0e7dd80Sopenharmony_ci    EXPECT_EQ(chainId, HiTraceChainGetChainId(&getId));
192b0e7dd80Sopenharmony_ci    EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceChainGetFlags(&getId));
193b0e7dd80Sopenharmony_ci    EXPECT_EQ(spanId, HiTraceChainGetSpanId(&getId));
194b0e7dd80Sopenharmony_ci    EXPECT_EQ(parentSpanId, HiTraceChainGetParentSpanId(&getId));
195b0e7dd80Sopenharmony_ci    PRINT_ID(&getId);
196b0e7dd80Sopenharmony_ci
197b0e7dd80Sopenharmony_ci    // set invalid id
198b0e7dd80Sopenharmony_ci    HiTraceIdStruct invalidId;
199b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&invalidId);
200b0e7dd80Sopenharmony_ci
201b0e7dd80Sopenharmony_ci    HiTraceChainSetId(&invalidId);
202b0e7dd80Sopenharmony_ci
203b0e7dd80Sopenharmony_ci    getId = HiTraceChainGetId();
204b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&getId));
205b0e7dd80Sopenharmony_ci    EXPECT_EQ(chainId, HiTraceChainGetChainId(&getId));
206b0e7dd80Sopenharmony_ci    EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceChainGetFlags(&getId));
207b0e7dd80Sopenharmony_ci    EXPECT_EQ(spanId, HiTraceChainGetSpanId(&getId));
208b0e7dd80Sopenharmony_ci    EXPECT_EQ(parentSpanId, HiTraceChainGetParentSpanId(&getId));
209b0e7dd80Sopenharmony_ci    PRINT_ID(&getId);
210b0e7dd80Sopenharmony_ci
211b0e7dd80Sopenharmony_ci    // clear thread id
212b0e7dd80Sopenharmony_ci    HiTraceChainClearId();
213b0e7dd80Sopenharmony_ci
214b0e7dd80Sopenharmony_ci    HiTraceIdStruct clearId = HiTraceChainGetId();
215b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&clearId));
216b0e7dd80Sopenharmony_ci    PRINT_ID(&clearId);
217b0e7dd80Sopenharmony_ci}
218b0e7dd80Sopenharmony_ci
219b0e7dd80Sopenharmony_ci/**
220b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_IntfTest_001
221b0e7dd80Sopenharmony_ci * @tc.desc: Interconversion between trace id and bytes array.
222b0e7dd80Sopenharmony_ci * @tc.type: FUNC
223b0e7dd80Sopenharmony_ci * @tc.require: AR000CQV9T
224b0e7dd80Sopenharmony_ci */
225b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, IntfTest_001, TestSize.Level1)
226b0e7dd80Sopenharmony_ci{
227b0e7dd80Sopenharmony_ci    /**
228b0e7dd80Sopenharmony_ci     * @tc.steps: step1. construct trace id and validate it.
229b0e7dd80Sopenharmony_ci     * @tc.expected: step1. trace id is valid.
230b0e7dd80Sopenharmony_ci     * @tc.steps: step2. convert trace id to bytes array.
231b0e7dd80Sopenharmony_ci     * @tc.expected: step2. convert success when array size >= id length.
232b0e7dd80Sopenharmony_ci     * @tc.steps: step3. convert bytes array to trace id.
233b0e7dd80Sopenharmony_ci     * @tc.expected: step3. convert success only when array size == id length.
234b0e7dd80Sopenharmony_ci     * @tc.steps: step4. convert invalid id to bytes array.
235b0e7dd80Sopenharmony_ci     * @tc.expected: step4. convert fail.
236b0e7dd80Sopenharmony_ci     * @tc.steps: step5. convert invalid bytes array to id.
237b0e7dd80Sopenharmony_ci     * @tc.expected: step5. convert fail.
238b0e7dd80Sopenharmony_ci     */
239b0e7dd80Sopenharmony_ci    // id to bytes
240b0e7dd80Sopenharmony_ci    constexpr uint64_t chainId = 0xABCDEF;
241b0e7dd80Sopenharmony_ci    constexpr uint64_t spanId = 0x12345;
242b0e7dd80Sopenharmony_ci    constexpr uint64_t parentSpanId = 0x67890;
243b0e7dd80Sopenharmony_ci    constexpr int flags = HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN;
244b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = {HITRACE_ID_VALID, HITRACE_VER_1, chainId, flags, spanId, parentSpanId};
245b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&id));
246b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
247b0e7dd80Sopenharmony_ci
248b0e7dd80Sopenharmony_ci    constexpr int idLen = sizeof(HiTraceIdStruct);
249b0e7dd80Sopenharmony_ci    uint8_t bytes[idLen + 1];
250b0e7dd80Sopenharmony_ci    int len = HiTraceChainIdToBytes(&id, bytes, idLen - 1);
251b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, len);
252b0e7dd80Sopenharmony_ci    len = HiTraceChainIdToBytes(&id, bytes, idLen + 1);
253b0e7dd80Sopenharmony_ci    EXPECT_EQ(idLen, len);
254b0e7dd80Sopenharmony_ci    len = HiTraceChainIdToBytes(&id, bytes, idLen);
255b0e7dd80Sopenharmony_ci    EXPECT_EQ(idLen, len);
256b0e7dd80Sopenharmony_ci    PRINT_ID(reinterpret_cast<HiTraceIdStruct*>(bytes));
257b0e7dd80Sopenharmony_ci
258b0e7dd80Sopenharmony_ci    // bytes to id
259b0e7dd80Sopenharmony_ci    HiTraceIdStruct bytesToId = HiTraceChainBytesToId(bytes, idLen - 1);
260b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&bytesToId));
261b0e7dd80Sopenharmony_ci    bytesToId = HiTraceChainBytesToId(bytes, idLen + 1);
262b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&bytesToId));
263b0e7dd80Sopenharmony_ci    bytesToId = HiTraceChainBytesToId(bytes, idLen);
264b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&bytesToId));
265b0e7dd80Sopenharmony_ci    EXPECT_EQ(chainId, HiTraceChainGetChainId(&bytesToId));
266b0e7dd80Sopenharmony_ci    EXPECT_EQ(HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_DONOT_CREATE_SPAN, HiTraceChainGetFlags(&bytesToId));
267b0e7dd80Sopenharmony_ci    EXPECT_EQ(spanId, HiTraceChainGetSpanId(&bytesToId));
268b0e7dd80Sopenharmony_ci    EXPECT_EQ(parentSpanId, HiTraceChainGetParentSpanId(&bytesToId));
269b0e7dd80Sopenharmony_ci    PRINT_ID(&bytesToId);
270b0e7dd80Sopenharmony_ci
271b0e7dd80Sopenharmony_ci    // set invalid id
272b0e7dd80Sopenharmony_ci    HiTraceIdStruct invalidId;
273b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&invalidId);
274b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIdToBytes(&invalidId, bytes, idLen));
275b0e7dd80Sopenharmony_ci    invalidId = HiTraceChainBytesToId(nullptr, idLen);
276b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&invalidId));
277b0e7dd80Sopenharmony_ci}
278b0e7dd80Sopenharmony_ci
279b0e7dd80Sopenharmony_ci/**
280b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_IntfTest_002
281b0e7dd80Sopenharmony_ci * @tc.desc: Start and stop trace.
282b0e7dd80Sopenharmony_ci * @tc.type: FUNC
283b0e7dd80Sopenharmony_ci * @tc.require: AR000CQV9T
284b0e7dd80Sopenharmony_ci */
285b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, IntfTest_002, TestSize.Level1)
286b0e7dd80Sopenharmony_ci{
287b0e7dd80Sopenharmony_ci    /**
288b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with flags, get trace id and validit it.
289b0e7dd80Sopenharmony_ci     * @tc.expected: step1. trace id and flags is valid.
290b0e7dd80Sopenharmony_ci     * @tc.steps: step2. stop trace, get trace id and validit it.
291b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace id is invalid.
292b0e7dd80Sopenharmony_ci     */
293b0e7dd80Sopenharmony_ci    // begin
294b0e7dd80Sopenharmony_ci    HiTraceIdStruct beginId = HiTraceChainBegin("test", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_NO_BE_INFO);
295b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&beginId));
296b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&beginId, HITRACE_FLAG_INCLUDE_ASYNC));
297b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&beginId, HITRACE_FLAG_NO_BE_INFO));
298b0e7dd80Sopenharmony_ci    PRINT_ID(&beginId);
299b0e7dd80Sopenharmony_ci
300b0e7dd80Sopenharmony_ci    // end
301b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&beginId);
302b0e7dd80Sopenharmony_ci
303b0e7dd80Sopenharmony_ci    HiTraceIdStruct endId = HiTraceChainGetId();
304b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&endId));
305b0e7dd80Sopenharmony_ci    PRINT_ID(&endId);
306b0e7dd80Sopenharmony_ci}
307b0e7dd80Sopenharmony_ci
308b0e7dd80Sopenharmony_ci/**
309b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_IntfTest_003
310b0e7dd80Sopenharmony_ci * @tc.desc: Start and stop trace with reentered.
311b0e7dd80Sopenharmony_ci * @tc.type: FUNC
312b0e7dd80Sopenharmony_ci * @tc.require: AR000CQV9T
313b0e7dd80Sopenharmony_ci */
314b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, IntfTest_003, TestSize.Level1)
315b0e7dd80Sopenharmony_ci{
316b0e7dd80Sopenharmony_ci    /**
317b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace twice and get 2nd trace id.
318b0e7dd80Sopenharmony_ci     * @tc.expected: step1. 2nd trace is invalid.
319b0e7dd80Sopenharmony_ci     * @tc.steps: step2. get trace id and check.
320b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace id is valid and same with 1st id.
321b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set chain id with wrong id and get trace id.
322b0e7dd80Sopenharmony_ci     * @tc.expected: step3. trace id is valid and same with 1st id.
323b0e7dd80Sopenharmony_ci     * @tc.steps: step4. stop trace twice and get trace id.
324b0e7dd80Sopenharmony_ci     * @tc.expected: step4. trace id is invalid.
325b0e7dd80Sopenharmony_ci     */
326b0e7dd80Sopenharmony_ci    HiTraceIdStruct beginId = HiTraceChainBegin("begin", HITRACE_FLAG_INCLUDE_ASYNC);
327b0e7dd80Sopenharmony_ci    PRINT_ID(&beginId);
328b0e7dd80Sopenharmony_ci
329b0e7dd80Sopenharmony_ci    // reenter begin
330b0e7dd80Sopenharmony_ci    HiTraceIdStruct reBeginId = HiTraceChainBegin("reenter begin", HITRACE_FLAG_TP_INFO);
331b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&reBeginId));
332b0e7dd80Sopenharmony_ci    EXPECT_NE(HiTraceChainGetChainId(&reBeginId), HiTraceChainGetChainId(&beginId));
333b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&reBeginId, HITRACE_FLAG_INCLUDE_ASYNC));
334b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&reBeginId, HITRACE_FLAG_TP_INFO));
335b0e7dd80Sopenharmony_ci    PRINT_ID(&reBeginId);
336b0e7dd80Sopenharmony_ci
337b0e7dd80Sopenharmony_ci    // reenter end
338b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&reBeginId);
339b0e7dd80Sopenharmony_ci
340b0e7dd80Sopenharmony_ci    HiTraceIdStruct endId = HiTraceChainGetId();
341b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&endId));
342b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&endId), HiTraceChainGetChainId(&beginId));
343b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&endId, HITRACE_FLAG_INCLUDE_ASYNC));
344b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&endId, HITRACE_FLAG_TP_INFO));
345b0e7dd80Sopenharmony_ci    PRINT_ID(&endId);
346b0e7dd80Sopenharmony_ci
347b0e7dd80Sopenharmony_ci    // end with wrong chainId
348b0e7dd80Sopenharmony_ci    HiTraceIdStruct wrongBeginId = beginId;
349b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&wrongBeginId, HiTraceChainGetChainId(&beginId) + 1);
350b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&wrongBeginId);
351b0e7dd80Sopenharmony_ci
352b0e7dd80Sopenharmony_ci    HiTraceIdStruct wrongEndId = HiTraceChainGetId();
353b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&wrongEndId));
354b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&wrongEndId), HiTraceChainGetChainId(&beginId));
355b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&wrongEndId, HITRACE_FLAG_INCLUDE_ASYNC));
356b0e7dd80Sopenharmony_ci    PRINT_ID(&wrongEndId);
357b0e7dd80Sopenharmony_ci
358b0e7dd80Sopenharmony_ci    // end
359b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&beginId);
360b0e7dd80Sopenharmony_ci
361b0e7dd80Sopenharmony_ci    HiTraceIdStruct reEndId = HiTraceChainGetId();
362b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&reEndId));
363b0e7dd80Sopenharmony_ci    PRINT_ID(&reEndId);
364b0e7dd80Sopenharmony_ci
365b0e7dd80Sopenharmony_ci    // end with invalid thread id
366b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&beginId);
367b0e7dd80Sopenharmony_ci
368b0e7dd80Sopenharmony_ci    HiTraceIdStruct endInvalidId = HiTraceChainGetId();
369b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsValid(&endInvalidId));
370b0e7dd80Sopenharmony_ci    PRINT_ID(&endInvalidId);
371b0e7dd80Sopenharmony_ci}
372b0e7dd80Sopenharmony_ci
373b0e7dd80Sopenharmony_ci/**
374b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_SpanTest_001
375b0e7dd80Sopenharmony_ci * @tc.desc: Create child and grand child span.
376b0e7dd80Sopenharmony_ci * @tc.type: FUNC
377b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA2
378b0e7dd80Sopenharmony_ci */
379b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, SpanTest_001, TestSize.Level1)
380b0e7dd80Sopenharmony_ci{
381b0e7dd80Sopenharmony_ci    /**
382b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without HITRACE_FLAG_DONOT_CREATE_SPAN,
383b0e7dd80Sopenharmony_ci     *     get and check flags.
384b0e7dd80Sopenharmony_ci     * @tc.expected: step1. flags is same with set and span id is 0.
385b0e7dd80Sopenharmony_ci     * @tc.steps: step2. create child id.
386b0e7dd80Sopenharmony_ci     * @tc.expected: step2. child id has same span id with parent.
387b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set child id into context.
388b0e7dd80Sopenharmony_ci     * @tc.steps: step4. create grand child id.
389b0e7dd80Sopenharmony_ci     * @tc.expected: step4. grand child id has same span id with parent and child.
390b0e7dd80Sopenharmony_ci     */
391b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test", 0);
392b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainGetFlags(&id));
393b0e7dd80Sopenharmony_ci    EXPECT_EQ(0UL, HiTraceChainGetSpanId(&id));
394b0e7dd80Sopenharmony_ci    EXPECT_EQ(0UL, HiTraceChainGetParentSpanId(&id));
395b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
396b0e7dd80Sopenharmony_ci
397b0e7dd80Sopenharmony_ci    // create child span
398b0e7dd80Sopenharmony_ci    HiTraceIdStruct childId = HiTraceChainCreateSpan();
399b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&childId));
400b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetFlags(&childId), HiTraceChainGetFlags(&id));
401b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&childId), HiTraceChainGetChainId(&id));
402b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&childId), HiTraceChainGetSpanId(&id));
403b0e7dd80Sopenharmony_ci    PRINT_ID(&childId);
404b0e7dd80Sopenharmony_ci
405b0e7dd80Sopenharmony_ci    // set child id to thread id
406b0e7dd80Sopenharmony_ci    HiTraceChainSetId(&childId);
407b0e7dd80Sopenharmony_ci
408b0e7dd80Sopenharmony_ci    // continue to create child span
409b0e7dd80Sopenharmony_ci    HiTraceIdStruct grandChildId = HiTraceChainCreateSpan();
410b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&grandChildId));
411b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetFlags(&grandChildId), HiTraceChainGetFlags(&id));
412b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&grandChildId), HiTraceChainGetChainId(&id));
413b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&grandChildId), HiTraceChainGetSpanId(&childId));
414b0e7dd80Sopenharmony_ci    PRINT_ID(&grandChildId);
415b0e7dd80Sopenharmony_ci
416b0e7dd80Sopenharmony_ci    // end
417b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
418b0e7dd80Sopenharmony_ci}
419b0e7dd80Sopenharmony_ci
420b0e7dd80Sopenharmony_ci/**
421b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_SpanTest_002
422b0e7dd80Sopenharmony_ci * @tc.desc: Start and stop trace with reentered.
423b0e7dd80Sopenharmony_ci * @tc.type: FUNC
424b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA2
425b0e7dd80Sopenharmony_ci */
426b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, SpanTest_002, TestSize.Level1)
427b0e7dd80Sopenharmony_ci{
428b0e7dd80Sopenharmony_ci    /**
429b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_DONOT_CREATE_SPAN,
430b0e7dd80Sopenharmony_ci     *     get and check flags.
431b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_DONOT_CREATE_SPAN is enabled.
432b0e7dd80Sopenharmony_ci     * @tc.steps: step2. create child id.
433b0e7dd80Sopenharmony_ci     * @tc.expected: step2. child id is same with parent id.
434b0e7dd80Sopenharmony_ci     */
435b0e7dd80Sopenharmony_ci    // begin with "donot create span" flag
436b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test", HITRACE_FLAG_DONOT_CREATE_SPAN);
437b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_DONOT_CREATE_SPAN));
438b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
439b0e7dd80Sopenharmony_ci
440b0e7dd80Sopenharmony_ci    // create child span
441b0e7dd80Sopenharmony_ci    HiTraceIdStruct childId = HiTraceChainCreateSpan();
442b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsValid(&childId));
443b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetFlags(&childId), HiTraceChainGetFlags(&id));
444b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&childId), HiTraceChainGetChainId(&id));
445b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetSpanId(&childId), HiTraceChainGetSpanId(&id));
446b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&childId), HiTraceChainGetParentSpanId(&id));
447b0e7dd80Sopenharmony_ci    PRINT_ID(&childId);
448b0e7dd80Sopenharmony_ci
449b0e7dd80Sopenharmony_ci    // end
450b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
451b0e7dd80Sopenharmony_ci}
452b0e7dd80Sopenharmony_ci
453b0e7dd80Sopenharmony_ci/**
454b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_001
455b0e7dd80Sopenharmony_ci * @tc.desc: Start trace with HITRACE_FLAG_TP_INFO flag.
456b0e7dd80Sopenharmony_ci * @tc.type: FUNC
457b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
458b0e7dd80Sopenharmony_ci */
459b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_001, TestSize.Level1)
460b0e7dd80Sopenharmony_ci{
461b0e7dd80Sopenharmony_ci    /**
462b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO,
463b0e7dd80Sopenharmony_ci     *     get and check flags.
464b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
465b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add trace point info with id and check logs.
466b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
467b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add trace point info with null and check logs.
468b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point cannot be found in logs.
469b0e7dd80Sopenharmony_ci     */
470b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test tp flag", HITRACE_FLAG_TP_INFO);
471b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
472b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
473b0e7dd80Sopenharmony_ci
474b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, nullptr, "client send msg content %d", 12);
475b0e7dd80Sopenharmony_ci
476b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
477b0e7dd80Sopenharmony_ci}
478b0e7dd80Sopenharmony_ci
479b0e7dd80Sopenharmony_ci/**
480b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_002
481b0e7dd80Sopenharmony_ci * @tc.desc: Start trace without HITRACE_FLAG_TP_INFO flag.
482b0e7dd80Sopenharmony_ci * @tc.type: FUNC
483b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
484b0e7dd80Sopenharmony_ci */
485b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_002, TestSize.Level1)
486b0e7dd80Sopenharmony_ci{
487b0e7dd80Sopenharmony_ci    /**
488b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without HITRACE_FLAG_TP_INFO flag.
489b0e7dd80Sopenharmony_ci     *     get and check flags.
490b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_TP_INFO is not enabled.
491b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add trace point info with id and check logs.
492b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point cannot be found in logs.
493b0e7dd80Sopenharmony_ci     */
494b0e7dd80Sopenharmony_ci    // begin with tp flag
495b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test no tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
496b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
497b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 12);
498b0e7dd80Sopenharmony_ci
499b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
500b0e7dd80Sopenharmony_ci}
501b0e7dd80Sopenharmony_ci
502b0e7dd80Sopenharmony_ci/**
503b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_003
504b0e7dd80Sopenharmony_ci * @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO flag.
505b0e7dd80Sopenharmony_ci * @tc.type: FUNC
506b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
507b0e7dd80Sopenharmony_ci */
508b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_003, TestSize.Level1)
509b0e7dd80Sopenharmony_ci{
510b0e7dd80Sopenharmony_ci    /**
511b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO,
512b0e7dd80Sopenharmony_ci     *     get and check flags.
513b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
514b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with id and check logs.
515b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
516b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with null and check logs.
517b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point cannot be found in logs.
518b0e7dd80Sopenharmony_ci     * @tc.steps: step3. add trace point info with id and check logs.
519b0e7dd80Sopenharmony_ci     * @tc.expected: step3. trace point cannot be found in logs.
520b0e7dd80Sopenharmony_ci     */
521b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test D2D tp flag", HITRACE_FLAG_D2D_TP_INFO);
522b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
523b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
524b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
525b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
526b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
527b0e7dd80Sopenharmony_ci
528b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, nullptr, "cannot be found %d", 13);
529b0e7dd80Sopenharmony_ci
530b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &id, "cannot be found %d", 14);
531b0e7dd80Sopenharmony_ci
532b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
533b0e7dd80Sopenharmony_ci}
534b0e7dd80Sopenharmony_ci
535b0e7dd80Sopenharmony_ci/**
536b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_004
537b0e7dd80Sopenharmony_ci * @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO flag.
538b0e7dd80Sopenharmony_ci * @tc.type: FUNC
539b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
540b0e7dd80Sopenharmony_ci */
541b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_004, TestSize.Level1)
542b0e7dd80Sopenharmony_ci{
543b0e7dd80Sopenharmony_ci    /**
544b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without HITRACE_FLAG_D2D_TP_INFO flag.
545b0e7dd80Sopenharmony_ci     *     get and check flags.
546b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
547b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with id and check logs.
548b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point cannot be found in logs.
549b0e7dd80Sopenharmony_ci     */
550b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test no D2D tp flag", HITRACE_FLAG_INCLUDE_ASYNC);
551b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
552b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "cannot be found %d", 12);
553b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "cannot be found %d", 22);
554b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "cannot be found %d", 32);
555b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "cannot be found %d", 42);
556b0e7dd80Sopenharmony_ci
557b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
558b0e7dd80Sopenharmony_ci}
559b0e7dd80Sopenharmony_ci
560b0e7dd80Sopenharmony_ci/**
561b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_005
562b0e7dd80Sopenharmony_ci * @tc.desc: Start trace with HITRACE_FLAG_D2D_TP_INFO and HITRACE_FLAG_TP_INFO flag.
563b0e7dd80Sopenharmony_ci * @tc.type: FUNC
564b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
565b0e7dd80Sopenharmony_ci */
566b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_005, TestSize.Level1)
567b0e7dd80Sopenharmony_ci{
568b0e7dd80Sopenharmony_ci    /**
569b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO,
570b0e7dd80Sopenharmony_ci     *     get and check flags.
571b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is enabled.
572b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
573b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with id and check logs.
574b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
575b0e7dd80Sopenharmony_ci     * @tc.steps: step3. add trace point info with id and check logs.
576b0e7dd80Sopenharmony_ci     * @tc.expected: step3. trace point can be found in logs.
577b0e7dd80Sopenharmony_ci     */
578b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test D2D | TP tp flag", HITRACE_FLAG_D2D_TP_INFO | HITRACE_FLAG_TP_INFO);
579b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
580b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
581b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
582b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
583b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
584b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
585b0e7dd80Sopenharmony_ci
586b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
587b0e7dd80Sopenharmony_ci
588b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
589b0e7dd80Sopenharmony_ci}
590b0e7dd80Sopenharmony_ci
591b0e7dd80Sopenharmony_ci/**
592b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_006
593b0e7dd80Sopenharmony_ci * @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO, but with HITRACE_FLAG_TP_INFO flag.
594b0e7dd80Sopenharmony_ci * @tc.type: FUNC
595b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
596b0e7dd80Sopenharmony_ci */
597b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_006, TestSize.Level1)
598b0e7dd80Sopenharmony_ci{
599b0e7dd80Sopenharmony_ci    /**
600b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO flag.
601b0e7dd80Sopenharmony_ci     *     get and check flags.
602b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
603b0e7dd80Sopenharmony_ci     * * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
604b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with id and check logs.
605b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
606b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add trace point info with id and check logs.
607b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
608b0e7dd80Sopenharmony_ci     */
609b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test no D2D, but tp flag", HITRACE_FLAG_TP_INFO);
610b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
611b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
612b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
613b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
614b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
615b0e7dd80Sopenharmony_ci    HiTraceChainTracepointEx(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
616b0e7dd80Sopenharmony_ci
617b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &id, "client send msg content %d", 13);
618b0e7dd80Sopenharmony_ci
619b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
620b0e7dd80Sopenharmony_ci}
621b0e7dd80Sopenharmony_ci
622b0e7dd80Sopenharmony_ci/**
623b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_TracepointTest_007
624b0e7dd80Sopenharmony_ci * @tc.desc: Start trace without HITRACE_FLAG_D2D_TP_INFO, but with HITRACE_FLAG_TP_INFO flag.
625b0e7dd80Sopenharmony_ci * @tc.type: FUNC
626b0e7dd80Sopenharmony_ci * @tc.require: AR000CQVA3
627b0e7dd80Sopenharmony_ci */
628b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, TracepointTest_007, TestSize.Level1)
629b0e7dd80Sopenharmony_ci{
630b0e7dd80Sopenharmony_ci    /**
631b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with HITRACE_FLAG_TP_INFO flag.
632b0e7dd80Sopenharmony_ci     *     get and check flags.
633b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_D2D_TP_INFO is not enabled.
634b0e7dd80Sopenharmony_ci     * * @tc.expected: step1. HITRACE_FLAG_TP_INFO is enabled.
635b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add D2D trace point info with id and check logs.
636b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
637b0e7dd80Sopenharmony_ci     * @tc.steps: step2. add trace point info with id and check logs.
638b0e7dd80Sopenharmony_ci     * @tc.expected: step2. trace point can be found in logs.
639b0e7dd80Sopenharmony_ci     */
640b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("test no D2D, but tp flag", HITRACE_FLAG_TP_INFO);
641b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_D2D_TP_INFO));
642b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&id, HITRACE_FLAG_TP_INFO));
643b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(HITRACE_CM_DEVICE, HITRACE_TP_CS, &id, "client send msg content %d", 12);
644b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(HITRACE_CM_PROCESS, HITRACE_TP_CS, &id, "client send msg content %d", 22);
645b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(HITRACE_CM_THREAD, HITRACE_TP_CS, &id, "client send msg content %d", 32);
646b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &id, "client send msg content %d", 42);
647b0e7dd80Sopenharmony_ci
648b0e7dd80Sopenharmony_ci    HiTraceChainTracepointWithArgsWrapper(HITRACE_TP_CS, &id, "client send msg content %d", 13);
649b0e7dd80Sopenharmony_ci
650b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
651b0e7dd80Sopenharmony_ci}
652b0e7dd80Sopenharmony_ci
653b0e7dd80Sopenharmony_ci/**
654b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_SyncAsyncTest_001
655b0e7dd80Sopenharmony_ci * @tc.desc: Start trace with SYNC or ASYNC.
656b0e7dd80Sopenharmony_ci * @tc.type: FUNC
657b0e7dd80Sopenharmony_ci * @tc.require: AR000CQ0G7
658b0e7dd80Sopenharmony_ci */
659b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, SyncAsyncTest_001, TestSize.Level1)
660b0e7dd80Sopenharmony_ci{
661b0e7dd80Sopenharmony_ci    /**
662b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without HITRACE_FLAG_INCLUDE_ASYNC flag.
663b0e7dd80Sopenharmony_ci     *    get and check flags.
664b0e7dd80Sopenharmony_ci     * @tc.expected: step1. HITRACE_FLAG_INCLUDE_ASYNC is not enabled.
665b0e7dd80Sopenharmony_ci     * @tc.steps: step2. start trace with HITRACE_FLAG_INCLUDE_ASYNC flag.
666b0e7dd80Sopenharmony_ci     *    get and check flags.
667b0e7dd80Sopenharmony_ci     * @tc.expected: step2. HITRACE_FLAG_INCLUDE_ASYNC is enabled.
668b0e7dd80Sopenharmony_ci     */
669b0e7dd80Sopenharmony_ci    // begin with sync flag
670b0e7dd80Sopenharmony_ci    HiTraceIdStruct syncId = HiTraceChainBegin("test sync only", HITRACE_FLAG_TP_INFO);
671b0e7dd80Sopenharmony_ci    EXPECT_EQ(0, HiTraceChainIsFlagEnabled(&syncId, HITRACE_FLAG_INCLUDE_ASYNC));
672b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &syncId, "client send msg: %s", "sync");
673b0e7dd80Sopenharmony_ci
674b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&syncId);
675b0e7dd80Sopenharmony_ci
676b0e7dd80Sopenharmony_ci    // begin with async flag
677b0e7dd80Sopenharmony_ci    HiTraceIdStruct asyncId = HiTraceChainBegin("test sync+async", HITRACE_FLAG_INCLUDE_ASYNC | HITRACE_FLAG_TP_INFO);
678b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&asyncId, HITRACE_FLAG_INCLUDE_ASYNC));
679b0e7dd80Sopenharmony_ci    HiTraceChainTracepoint(HITRACE_TP_CS, &asyncId, "client send msg: %s", "async");
680b0e7dd80Sopenharmony_ci
681b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&asyncId);
682b0e7dd80Sopenharmony_ci}
683b0e7dd80Sopenharmony_ci
684b0e7dd80Sopenharmony_ci/**
685b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_RestoreTest_001
686b0e7dd80Sopenharmony_ci * @tc.desc: Start normal trace.
687b0e7dd80Sopenharmony_ci * @tc.type: FUNC
688b0e7dd80Sopenharmony_ci */
689b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, RestoreTest_001, TestSize.Level1)
690b0e7dd80Sopenharmony_ci{
691b0e7dd80Sopenharmony_ci    /**
692b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without any flag.
693b0e7dd80Sopenharmony_ci     * @tc.expected: step1. no flag is enabled.
694b0e7dd80Sopenharmony_ci     * @tc.steps: step2. generate a temporary trace id.
695b0e7dd80Sopenharmony_ci     * @tc.expected: step2. a trace id is generated.
696b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set new trace id and save old trace id.
697b0e7dd80Sopenharmony_ci     * @tc.expected: step3. new trace id get into TLS.
698b0e7dd80Sopenharmony_ci     * @tc.steps: step4. store old trace id.
699b0e7dd80Sopenharmony_ci     * @tc.expected: step4. old trace id get into TLS.
700b0e7dd80Sopenharmony_ci     * @tc.steps: step5. end trace.
701b0e7dd80Sopenharmony_ci     * @tc.expected: step5. trace terminate.
702b0e7dd80Sopenharmony_ci     */
703b0e7dd80Sopenharmony_ci
704b0e7dd80Sopenharmony_ci    // begin trace
705b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("RestoreTest_001", HITRACE_FLAG_TP_INFO);
706b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
707b0e7dd80Sopenharmony_ci
708b0e7dd80Sopenharmony_ci    //generate new trace id
709b0e7dd80Sopenharmony_ci    HiTraceIdStruct newId{0};
710b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&newId);
711b0e7dd80Sopenharmony_ci    newId.valid=HITRACE_ID_VALID;
712b0e7dd80Sopenharmony_ci    uint64_t chainId = GenerateChainId();
713b0e7dd80Sopenharmony_ci    uint64_t spanId = GenerateSpanId();
714b0e7dd80Sopenharmony_ci    uint64_t parentSpanId = GenerateParentSpanId();
715b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&newId, chainId);
716b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&newId, spanId);
717b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&newId, parentSpanId);
718b0e7dd80Sopenharmony_ci    PRINT_ID(&newId);
719b0e7dd80Sopenharmony_ci
720b0e7dd80Sopenharmony_ci    // set new id and save old id
721b0e7dd80Sopenharmony_ci    HiTraceIdStruct oldId = HiTraceChainSaveAndSetId(&newId);
722b0e7dd80Sopenharmony_ci    PRINT_ID(&oldId);
723b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId = HiTraceChainGetId();
724b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
725b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
726b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
727b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
728b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
729b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
730b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
731b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
732b0e7dd80Sopenharmony_ci
733b0e7dd80Sopenharmony_ci    // restore old id
734b0e7dd80Sopenharmony_ci    HiTraceChainRestoreId(&oldId);
735b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId2 = HiTraceChainGetId();
736b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&id), HiTraceChainGetChainId(&currentId2));
737b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
738b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
739b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
740b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
741b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
742b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
743b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
744b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
745b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
746b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
747b0e7dd80Sopenharmony_ci
748b0e7dd80Sopenharmony_ci    // end trace
749b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
750b0e7dd80Sopenharmony_ci}
751b0e7dd80Sopenharmony_ci
752b0e7dd80Sopenharmony_ci/**
753b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_RestoreTest_002
754b0e7dd80Sopenharmony_ci * @tc.desc: Start trace with HITRACE_FLAG_INCLUDE_ASYNC.
755b0e7dd80Sopenharmony_ci * @tc.type: FUNC
756b0e7dd80Sopenharmony_ci */
757b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, RestoreTest_002, TestSize.Level1)
758b0e7dd80Sopenharmony_ci{
759b0e7dd80Sopenharmony_ci    /**
760b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with flag HITRACE_FLAG_INCLUDE_ASYNC.
761b0e7dd80Sopenharmony_ci     * @tc.expected: step1. flag HITRACE_FLAG_INCLUDE_ASYNC is enabled.
762b0e7dd80Sopenharmony_ci     * @tc.steps: step2. generate a temporary trace id.
763b0e7dd80Sopenharmony_ci     * @tc.expected: step2. a trace id is generated.
764b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set new trace id and save old trace id.
765b0e7dd80Sopenharmony_ci     * @tc.expected: step3. new trace id get into TLS.
766b0e7dd80Sopenharmony_ci     * @tc.steps: step4. store old trace id.
767b0e7dd80Sopenharmony_ci     * @tc.expected: step4. old trace id get into TLS.
768b0e7dd80Sopenharmony_ci     * @tc.steps: step5. end trace.
769b0e7dd80Sopenharmony_ci     * @tc.expected: step5. trace terminate.
770b0e7dd80Sopenharmony_ci     */
771b0e7dd80Sopenharmony_ci
772b0e7dd80Sopenharmony_ci    // begin trace
773b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("RestoreTest_002", HITRACE_FLAG_TP_INFO | HITRACE_FLAG_INCLUDE_ASYNC);
774b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
775b0e7dd80Sopenharmony_ci
776b0e7dd80Sopenharmony_ci    //generate new trace id
777b0e7dd80Sopenharmony_ci    HiTraceIdStruct newId{0};
778b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&newId);
779b0e7dd80Sopenharmony_ci    newId.valid=HITRACE_ID_VALID;
780b0e7dd80Sopenharmony_ci    uint64_t chainId = GenerateChainId();
781b0e7dd80Sopenharmony_ci    uint64_t spanId = GenerateSpanId();
782b0e7dd80Sopenharmony_ci    uint64_t parentSpanId = GenerateParentSpanId();
783b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&newId, chainId);
784b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&newId, spanId);
785b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&newId, parentSpanId);
786b0e7dd80Sopenharmony_ci    PRINT_ID(&newId);
787b0e7dd80Sopenharmony_ci
788b0e7dd80Sopenharmony_ci    // set new id and save old id
789b0e7dd80Sopenharmony_ci    HiTraceIdStruct oldId = HiTraceChainSaveAndSetId(&newId);
790b0e7dd80Sopenharmony_ci    PRINT_ID(&oldId);
791b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId = HiTraceChainGetId();
792b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
793b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
794b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
795b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
796b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
797b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
798b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
799b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
800b0e7dd80Sopenharmony_ci
801b0e7dd80Sopenharmony_ci    // restore old id
802b0e7dd80Sopenharmony_ci    HiTraceChainRestoreId(&oldId);
803b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId2 = HiTraceChainGetId();
804b0e7dd80Sopenharmony_ci    EXPECT_EQ(1, HiTraceChainIsFlagEnabled(&currentId2, HITRACE_FLAG_INCLUDE_ASYNC));
805b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
806b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
807b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
808b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
809b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
810b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
811b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
812b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
813b0e7dd80Sopenharmony_ci
814b0e7dd80Sopenharmony_ci    // end trace
815b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
816b0e7dd80Sopenharmony_ci}
817b0e7dd80Sopenharmony_ci
818b0e7dd80Sopenharmony_ci/**
819b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_RestoreTest_003
820b0e7dd80Sopenharmony_ci * @tc.desc: Start normal trace.
821b0e7dd80Sopenharmony_ci * @tc.type: FUNC
822b0e7dd80Sopenharmony_ci */
823b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, RestoreTest_003, TestSize.Level1)
824b0e7dd80Sopenharmony_ci{
825b0e7dd80Sopenharmony_ci    /**
826b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace with version HITRACE_VER_1.
827b0e7dd80Sopenharmony_ci     * @tc.expected: step1. flag HITRACE_VER_1 is enabled.
828b0e7dd80Sopenharmony_ci     * @tc.steps: step2. generate a temporary trace id.
829b0e7dd80Sopenharmony_ci     * @tc.expected: step2. a trace id is generated.
830b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set new trace id and save old trace id.
831b0e7dd80Sopenharmony_ci     * @tc.expected: step3. new trace id get into TLS.
832b0e7dd80Sopenharmony_ci     * @tc.steps: step4. store old trace id.
833b0e7dd80Sopenharmony_ci     * @tc.expected: step4. old trace id get into TLS.
834b0e7dd80Sopenharmony_ci     * @tc.steps: step5. end trace.
835b0e7dd80Sopenharmony_ci     * @tc.expected: step5. trace terminate.
836b0e7dd80Sopenharmony_ci     */
837b0e7dd80Sopenharmony_ci    // begin trace
838b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("RestoreTest_003", HITRACE_FLAG_TP_INFO);
839b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
840b0e7dd80Sopenharmony_ci
841b0e7dd80Sopenharmony_ci    //generate new trace id
842b0e7dd80Sopenharmony_ci    HiTraceIdStruct newId{0};
843b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&newId);
844b0e7dd80Sopenharmony_ci    newId.valid=HITRACE_ID_VALID;
845b0e7dd80Sopenharmony_ci    uint64_t chainId = GenerateChainId();
846b0e7dd80Sopenharmony_ci    uint64_t spanId = GenerateSpanId();
847b0e7dd80Sopenharmony_ci    uint64_t parentSpanId = GenerateParentSpanId();
848b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&newId, chainId);
849b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&newId, spanId);
850b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&newId, parentSpanId);
851b0e7dd80Sopenharmony_ci    PRINT_ID(&newId);
852b0e7dd80Sopenharmony_ci
853b0e7dd80Sopenharmony_ci    // set new id and save old id
854b0e7dd80Sopenharmony_ci    HiTraceIdStruct oldId = HiTraceChainSaveAndSetId(&newId);
855b0e7dd80Sopenharmony_ci    PRINT_ID(&oldId);
856b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId = HiTraceChainGetId();
857b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
858b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
859b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
860b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
861b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
862b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
863b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
864b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
865b0e7dd80Sopenharmony_ci
866b0e7dd80Sopenharmony_ci    // restore old id
867b0e7dd80Sopenharmony_ci    HiTraceChainRestoreId(&oldId);
868b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId2 = HiTraceChainGetId();
869b0e7dd80Sopenharmony_ci    EXPECT_EQ(HITRACE_VER_1, currentId2.ver);
870b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
871b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
872b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
873b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
874b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
875b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
876b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
877b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
878b0e7dd80Sopenharmony_ci
879b0e7dd80Sopenharmony_ci    // end trace
880b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
881b0e7dd80Sopenharmony_ci}
882b0e7dd80Sopenharmony_ci/**
883b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_RestoreTest_004
884b0e7dd80Sopenharmony_ci * @tc.desc: Start normal trace and create span.
885b0e7dd80Sopenharmony_ci * @tc.type: FUNC
886b0e7dd80Sopenharmony_ci */
887b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, RestoreTest_004, TestSize.Level1)
888b0e7dd80Sopenharmony_ci{
889b0e7dd80Sopenharmony_ci    /**
890b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without any flag, then create span.
891b0e7dd80Sopenharmony_ci     * @tc.expected: step1. no flag is enabled.
892b0e7dd80Sopenharmony_ci     * @tc.steps: step2. generate a temporary trace id.
893b0e7dd80Sopenharmony_ci     * @tc.expected: step2. a trace id is generated.
894b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set new trace id and save old trace id.
895b0e7dd80Sopenharmony_ci     * @tc.expected: step3. new trace id get into TLS.
896b0e7dd80Sopenharmony_ci     * @tc.steps: step4. store old trace id.
897b0e7dd80Sopenharmony_ci     * @tc.expected: step4. old trace id get into TLS.
898b0e7dd80Sopenharmony_ci     * @tc.steps: step5. end trace.
899b0e7dd80Sopenharmony_ci     * @tc.expected: step5. trace terminate.
900b0e7dd80Sopenharmony_ci     */
901b0e7dd80Sopenharmony_ci    // begin trace
902b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("RestoreTest_004", HITRACE_FLAG_TP_INFO);
903b0e7dd80Sopenharmony_ci    HiTraceChainCreateSpan();
904b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
905b0e7dd80Sopenharmony_ci
906b0e7dd80Sopenharmony_ci    //generate new trace id
907b0e7dd80Sopenharmony_ci    HiTraceIdStruct newId{0};
908b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&newId);
909b0e7dd80Sopenharmony_ci    newId.valid=HITRACE_ID_VALID;
910b0e7dd80Sopenharmony_ci    uint64_t chainId = GenerateChainId();
911b0e7dd80Sopenharmony_ci    uint64_t spanId = GenerateSpanId();
912b0e7dd80Sopenharmony_ci    uint64_t parentSpanId = GenerateParentSpanId();
913b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&newId, chainId);
914b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&newId, spanId);
915b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&newId, parentSpanId);
916b0e7dd80Sopenharmony_ci    PRINT_ID(&newId);
917b0e7dd80Sopenharmony_ci
918b0e7dd80Sopenharmony_ci    // set new id and save old id
919b0e7dd80Sopenharmony_ci    HiTraceIdStruct oldId = HiTraceChainSaveAndSetId(&newId);
920b0e7dd80Sopenharmony_ci    HiTraceChainCreateSpan();
921b0e7dd80Sopenharmony_ci    PRINT_ID(&oldId);
922b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId = HiTraceChainGetId();
923b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
924b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
925b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
926b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
927b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
928b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
929b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
930b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
931b0e7dd80Sopenharmony_ci
932b0e7dd80Sopenharmony_ci    // restore old id
933b0e7dd80Sopenharmony_ci    HiTraceChainRestoreId(&oldId);
934b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId2 = HiTraceChainGetId();
935b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&id), HiTraceChainGetChainId(&currentId2));
936b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
937b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
938b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
939b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
940b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
941b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
942b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
943b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
944b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
945b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
946b0e7dd80Sopenharmony_ci
947b0e7dd80Sopenharmony_ci    // end trace
948b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
949b0e7dd80Sopenharmony_ci}
950b0e7dd80Sopenharmony_ci
951b0e7dd80Sopenharmony_ci/**
952b0e7dd80Sopenharmony_ci * @tc.name: Dfx_HiTraceChainCTest_RestoreTest_005
953b0e7dd80Sopenharmony_ci * @tc.desc: Start normal trace.
954b0e7dd80Sopenharmony_ci * @tc.type: FUNC
955b0e7dd80Sopenharmony_ci */
956b0e7dd80Sopenharmony_ciHWTEST_F(HiTraceChainCTest, RestoreTest_005, TestSize.Level1)
957b0e7dd80Sopenharmony_ci{
958b0e7dd80Sopenharmony_ci    /**
959b0e7dd80Sopenharmony_ci     * @tc.steps: step1. start trace without any flag.
960b0e7dd80Sopenharmony_ci     * @tc.expected: step1. no flag is enabled.
961b0e7dd80Sopenharmony_ci     * @tc.steps: step2. generate a temporary trace id with HITRACE_ID_INVALID flag.
962b0e7dd80Sopenharmony_ci     * @tc.expected: step2. a trace id is generated.
963b0e7dd80Sopenharmony_ci     * @tc.steps: step3. set new trace id and save old trace id.
964b0e7dd80Sopenharmony_ci     * @tc.expected: step3. new trace is invalid.
965b0e7dd80Sopenharmony_ci     * @tc.steps: step4. store old trace id.
966b0e7dd80Sopenharmony_ci     * @tc.expected: step4. old trace id get into TLS.
967b0e7dd80Sopenharmony_ci     * @tc.steps: step5. end trace.
968b0e7dd80Sopenharmony_ci     * @tc.expected: step5. trace terminate.
969b0e7dd80Sopenharmony_ci     */
970b0e7dd80Sopenharmony_ci
971b0e7dd80Sopenharmony_ci    // begin trace
972b0e7dd80Sopenharmony_ci    HiTraceIdStruct id = HiTraceChainBegin("RestoreTest_005", HITRACE_FLAG_TP_INFO);
973b0e7dd80Sopenharmony_ci    PRINT_ID(&id);
974b0e7dd80Sopenharmony_ci
975b0e7dd80Sopenharmony_ci    //generate new trace id
976b0e7dd80Sopenharmony_ci    HiTraceIdStruct newId{0};
977b0e7dd80Sopenharmony_ci    HiTraceChainInitId(&newId);
978b0e7dd80Sopenharmony_ci    newId.valid=HITRACE_ID_INVALID;
979b0e7dd80Sopenharmony_ci    uint64_t chainId = GenerateChainId();
980b0e7dd80Sopenharmony_ci    uint64_t spanId = GenerateSpanId();
981b0e7dd80Sopenharmony_ci    uint64_t parentSpanId = GenerateParentSpanId();
982b0e7dd80Sopenharmony_ci    HiTraceChainSetChainId(&newId, chainId);
983b0e7dd80Sopenharmony_ci    HiTraceChainSetSpanId(&newId, spanId);
984b0e7dd80Sopenharmony_ci    HiTraceChainSetParentSpanId(&newId, parentSpanId);
985b0e7dd80Sopenharmony_ci    PRINT_ID(&newId);
986b0e7dd80Sopenharmony_ci
987b0e7dd80Sopenharmony_ci    // set new id and save old id
988b0e7dd80Sopenharmony_ci    HiTraceIdStruct oldId = HiTraceChainSaveAndSetId(&newId);
989b0e7dd80Sopenharmony_ci    PRINT_ID(&oldId);
990b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId = HiTraceChainGetId();
991b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
992b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
993b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
994b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
995b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
996b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
997b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
998b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
999b0e7dd80Sopenharmony_ci
1000b0e7dd80Sopenharmony_ci    // restore old id
1001b0e7dd80Sopenharmony_ci    HiTraceChainRestoreId(&oldId);
1002b0e7dd80Sopenharmony_ci    HiTraceIdStruct currentId2 = HiTraceChainGetId();
1003b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetChainId(&id), HiTraceChainGetChainId(&currentId2));
1004b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
1005b0e7dd80Sopenharmony_ci    EXPECT_EQ(HiTraceChainGetParentSpanId(&id), HiTraceChainGetParentSpanId(&currentId2));
1006b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
1007b0e7dd80Sopenharmony_ci            HITRACE_CM_DEVICE, HITRACE_TP_CS, &currentId, "client send msg %d", DEVICE_CLIENT_SEND);
1008b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
1009b0e7dd80Sopenharmony_ci            HITRACE_CM_PROCESS, HITRACE_TP_CS, &currentId, "client send msg %d", PROCESS_CLIENT_SEND);
1010b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
1011b0e7dd80Sopenharmony_ci            HITRACE_CM_THREAD, HITRACE_TP_CS, &currentId, "client send msg %d", THREAD_CLIENT_SEND);
1012b0e7dd80Sopenharmony_ci    HiTraceChainTracepointExWithArgsWrapper(
1013b0e7dd80Sopenharmony_ci            HITRACE_CM_DEFAULT, HITRACE_TP_CS, &currentId, "client send msg %d", DEFAULT_CLIENT_SEND);
1014b0e7dd80Sopenharmony_ci
1015b0e7dd80Sopenharmony_ci    // end trace
1016b0e7dd80Sopenharmony_ci    HiTraceChainEnd(&id);
1017b0e7dd80Sopenharmony_ci}
1018b0e7dd80Sopenharmony_ci}  // namespace HiviewDFX
1019b0e7dd80Sopenharmony_ci}  // namespace OHOS
1020