1 /*
2 * Copyright (c) 2023 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 "napi/native_api.h"
17 #include <trace.h>
18 #include <thread>
19
20 #define FAIL (-1)
21 #define SUCCESS 0
22 #define PARAM_0 0
23 #define PARAM_1 1
24 #define PARAM_2 2
25 #define PARAM_3 3
26 #define PARAM_5 5
27 #define PARAM_10 10
28 static const int32_t TASK_ID = 111;
29 static const int32_t TASK_ID_TWO = 112;
30
OHHiTraceCountTrace(napi_env env, napi_callback_info info)31 static napi_value OHHiTraceCountTrace(napi_env env, napi_callback_info info)
32 {
33 int64_t count = PARAM_0;
34 OH_HiTrace_CountTrace("countTraceName", count++);
35 int returnValue = FAIL;
36 if (count == PARAM_1) {
37 returnValue = SUCCESS;
38 }
39 napi_value result = nullptr;
40 napi_create_int32(env, returnValue, &result);
41 return result;
42 }
43
OHHiTraceStartTrace(napi_env env, napi_callback_info info)44 static napi_value OHHiTraceStartTrace(napi_env env, napi_callback_info info)
45 {
46 int count = PARAM_0;
47 OH_HiTrace_StartTrace("traceName");
48 count++;
49 OH_HiTrace_FinishTrace();
50 count++;
51 int returnValue = FAIL;
52 if (count == PARAM_2) {
53 returnValue = SUCCESS;
54 }
55 napi_value result = nullptr;
56 napi_create_int32(env, returnValue, &result);
57 return result;
58 }
59
OHHiTraceStartAsyncTrace(napi_env env, napi_callback_info info)60 static napi_value OHHiTraceStartAsyncTrace(napi_env env, napi_callback_info info)
61 {
62 int count = PARAM_0;
63 OH_HiTrace_StartAsyncTrace("testAsync", TASK_ID);
64 count++;
65 OH_HiTrace_StartAsyncTrace("testAsync", TASK_ID_TWO);
66 OH_HiTrace_FinishAsyncTrace("testAsync", TASK_ID);
67 count++;
68 OH_HiTrace_FinishAsyncTrace("testAsync", TASK_ID_TWO);
69 int returnValue = FAIL;
70 if (count == PARAM_2) {
71 returnValue = SUCCESS;
72 }
73 napi_value result = nullptr;
74 napi_create_int32(env, returnValue, &result);
75 return result;
76 }
77
Begin(napi_env env, napi_callback_info info)78 static napi_value Begin(napi_env env, napi_callback_info info)
79 {
80 int count = PARAM_0;
81 HiTraceId hiTraceId = OH_HiTrace_BeginChain("hiTraceChainndktest", HITRACE_FLAG_DEFAULT);
82 count++;
83 OH_HiTrace_InitId(&hiTraceId);
84 OH_HiTrace_ClearId();
85 OH_HiTrace_SetId(&hiTraceId);
86 hiTraceId = OH_HiTrace_GetId();
87 OH_HiTrace_IsIdValid(&hiTraceId);
88 OH_HiTrace_EndChain();
89 count++;
90 HiTraceId traceId5 = OH_HiTrace_BeginChain("hitraceNdkTest5", HITRACE_FLAG_INCLUDE_ASYNC);
91 count++;
92 uint8_t pIdArray;
93 OH_HiTrace_IdToBytes(&traceId5, &pIdArray, sizeof(traceId5));
94 OH_HiTrace_IdFromBytes(&traceId5, &pIdArray, sizeof(traceId5));
95 int returnValue = FAIL;
96 if (count == PARAM_3) {
97 returnValue = SUCCESS;
98 }
99 napi_value result = nullptr;
100 napi_create_int32(env, returnValue, &result);
101
102 return result;
103 }
104
Beginflag(napi_env env, napi_callback_info info)105 static napi_value Beginflag(napi_env env, napi_callback_info info)
106 {
107 int count = PARAM_0;
108 HiTrace_Flag hitraceflag = HITRACE_FLAG_INCLUDE_ASYNC;
109 HiTraceId hiTraceId = OH_HiTrace_BeginChain("hiTraceChainndktest", hitraceflag);
110 count++;
111 OH_HiTrace_IsFlagEnabled(&hiTraceId, HITRACE_FLAG_INCLUDE_ASYNC);
112 count++;
113 OH_HiTrace_EnableFlag(&hiTraceId, HITRACE_FLAG_FAULT_TRIGGER);
114 count++;
115 OH_HiTrace_GetFlags(&hiTraceId);
116 count++;
117 OH_HiTrace_SetFlags(&hiTraceId, HITRACE_FLAG_DEFAULT);
118 OH_HiTrace_EndChain();
119 count++;
120 int returnValue = FAIL;
121 if (count == PARAM_5) {
122 returnValue = SUCCESS;
123 }
124 napi_value result = nullptr;
125 napi_create_int32(env, returnValue, &result);
126 return result;
127 }
128
BeginSpan(napi_env env, napi_callback_info info)129 static napi_value BeginSpan(napi_env env, napi_callback_info info)
130 {
131 int count = PARAM_0;
132 HiTrace_Flag hitraceflag = HITRACE_FLAG_INCLUDE_ASYNC;
133 HiTraceId hiTraceId = OH_HiTrace_BeginChain("hiTraceChainndktest", hitraceflag);
134 count++;
135 uint64_t chainId = 10000;
136 OH_HiTrace_SetChainId(&hiTraceId, chainId);
137 count++;
138 OH_HiTrace_GetChainId(&hiTraceId);
139 count++;
140 uint64_t spanId = 12345678;
141 OH_HiTrace_SetSpanId(&hiTraceId, spanId);
142 count++;
143 OH_HiTrace_GetSpanId(&hiTraceId);
144 count++;
145 uint64_t parentSpanId = 66666;
146 OH_HiTrace_SetParentSpanId(&hiTraceId, parentSpanId);
147 count++;
148 OH_HiTrace_GetParentSpanId(&hiTraceId);
149 count++;
150 OH_HiTrace_CreateSpan();
151 count++;
152 OH_HiTrace_Tracepoint(HITRACE_CM_DEFAULT, HITRACE_TP_CS, &hiTraceId, "hitracetest4");
153 count++;
154 OH_HiTrace_EndChain();
155 count++;
156 int returnValue = FAIL;
157 if (count == PARAM_10) {
158 returnValue = SUCCESS;
159 }
160 napi_value result = nullptr;
161 napi_create_int32(env, returnValue, &result);
162 return result;
163 }
164
165 EXTERN_C_START
Init(napi_env env, napi_value exports)166 static napi_value Init(napi_env env, napi_value exports)
167 {
168 napi_property_descriptor desc[] = {
169 {"oHHiTraceStartTrace", nullptr, OHHiTraceStartTrace, nullptr, nullptr, nullptr, napi_default, nullptr},
170 {"oHHiTraceStartAsyncTrace", nullptr, OHHiTraceStartAsyncTrace, nullptr, nullptr, nullptr, napi_default,
171 nullptr},
172 {"oHHiTraceCountTrace", nullptr, OHHiTraceCountTrace, nullptr, nullptr, nullptr, napi_default, nullptr},
173 {"begin", nullptr, Begin, nullptr, nullptr, nullptr, napi_default, nullptr},
174 {"beginflag", nullptr, Beginflag, nullptr, nullptr, nullptr, napi_default, nullptr},
175 {"beginSpan", nullptr, BeginSpan, nullptr, nullptr, nullptr, napi_default, nullptr},
176 };
177 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
178 return exports;
179 }
180
181 EXTERN_C_END
182
183 static napi_module demoModule = {
184 .nm_version = 1,
185 .nm_flags = 0,
186 .nm_filename = nullptr,
187 .nm_register_func = Init,
188 .nm_modname = "hitrace",
189 .nm_priv = ((void *)0),
190 .reserved = {0},
191 };
192
RegisterModule(void)193 extern "C" __attribute__((constructor)) void RegisterModule(void) { napi_module_register(&demoModule); }
194