Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifdef ATRACE_API
12 : #include "atrace_api.h"
13 : #else
14 : #include "utrace_api.h"
15 : #endif
16 : #include "trace_attr.h"
17 : #include "tracer_core.h"
18 : #include "adiag_print.h"
19 : #include "adiag_list.h"
20 : #include "trace_event.h"
21 :
22 : #ifdef ATRACE_API
23 : #define TRACE_API(func) Atrace##func
24 : #else
25 : #define TRACE_API(func) Utrace##func
26 : #endif
27 :
28 : /**
29 : * @brief Create trace handle.
30 : * @param [in] tracerType: trace type
31 : * @param [in] objName: objName object name
32 : * @return atrace handle
33 : */
34 577 : TraHandle TRACE_API(Create)(TracerType tracerType, const char *objName)
35 : {
36 577 : if (!AtraceCheckSupported()) {
37 22 : ADIAG_WAR("AtraceCreate is not supported on device side");
38 22 : return TRACE_UNSUPPORTED_HANDLE;
39 : }
40 555 : ADIAG_CHK_EXPR_ACTION(tracerType >= TRACER_TYPE_MAX, return TRACE_INVALID_HANDLE,
41 : "tracer type %d is invalid.", (int32_t)tracerType);
42 555 : ADIAG_CHK_NULL_PTR(objName, return TRACE_INVALID_HANDLE);
43 :
44 555 : TraceAttr attr = {0};
45 555 : attr.exitSave = false;
46 555 : TraHandle ret = TracerObjCreate(tracerType, objName, &attr);
47 555 : ADIAG_DBG("AtraceCreate [%s].", objName);
48 555 : return ret;
49 : }
50 :
51 : /**
52 : * @brief Create trace handle.
53 : * @param [in] tracerType: trace type
54 : * @param [in] objName: object name
55 : * @param [in] attr: object attribute
56 : * @return atrace handle
57 : */
58 309 : TraHandle TRACE_API(CreateWithAttr)(TracerType tracerType, const char *objName, const TraceAttr *attr)
59 : {
60 309 : if (!AtraceCheckSupported()) {
61 22 : ADIAG_WAR("AtraceCreate is not supported on device side");
62 22 : return TRACE_UNSUPPORTED_HANDLE;
63 : }
64 287 : ADIAG_CHK_EXPR_ACTION(tracerType >= TRACER_TYPE_MAX, return TRACE_INVALID_HANDLE,
65 : "tracer type %d is invalid.", (int32_t)tracerType);
66 287 : ADIAG_CHK_NULL_PTR(objName, return TRACE_INVALID_HANDLE);
67 :
68 287 : TraHandle ret = TracerObjCreate(tracerType, objName, attr);
69 287 : ADIAG_DBG("AtraceCreateWithAttr [%s].", objName);
70 287 : return ret;
71 : }
72 :
73 : /**
74 : * @brief Get trace handle
75 : * @param [in] tracerType: trace type
76 : * @param [in] objName: objName object name
77 : * @return atrace handle
78 : */
79 88 : TraHandle TRACE_API(GetHandle)(TracerType tracerType, const char *objName)
80 : {
81 88 : if (!AtraceCheckSupported()) {
82 22 : ADIAG_WAR("AtraceGetHandle is not supported on device side");
83 22 : return TRACE_UNSUPPORTED_HANDLE;
84 : }
85 66 : return TracerObjGet(tracerType, objName);
86 : }
87 :
88 : /**
89 : * @brief Submit trace info
90 : * @param [in] handle: trace handle
91 : * @param [in] buffer: trace info buffer
92 : * @param [in] bufSize: size of buffer
93 : * @return TraStatus
94 : */
95 83154 : TraStatus TRACE_API(Submit)(TraHandle handle, const void *buffer, uint32_t bufSize)
96 : {
97 83154 : return TracerObjSubmit(handle, 0, buffer, bufSize);
98 : }
99 :
100 : /**
101 : * @brief Submit trace info by buffer type
102 : * @param [in] handle: trace handle
103 : * @param [in] bufferType: buffer type
104 : * @param [in] buffer: trace info buffer
105 : * @param [in] bufSize: size of buffer
106 : * @return TraStatus
107 : */
108 0 : TraStatus TRACE_API(SubmitByType)(TraHandle handle, uint8_t bufferType, const void *buffer, uint32_t bufSize)
109 : {
110 0 : return TracerObjSubmit(handle, bufferType, buffer, bufSize);
111 : }
112 :
113 : /**
114 : * @brief Destroy trace handle
115 : * @param [in] handle: trace handle
116 : * @return NA
117 : */
118 906 : void TRACE_API(Destroy)(TraHandle handle)
119 : {
120 906 : if (!AtraceCheckSupported()) {
121 22 : ADIAG_WAR("AtraceDestroy is not supported on device side");
122 22 : return;
123 : }
124 884 : TracerObjDestroy(handle);
125 : }
126 :
127 : /**
128 : * @brief Save trace info for all handle of tracerType
129 : * @param [in] tracerType: trace type to be saved
130 : * @param [in] syncFlag: synchronous or asynchronous
131 : * @return TraStatus
132 : */
133 116 : TraStatus TRACE_API(Save)(TracerType tracerType, bool syncFlag)
134 : {
135 116 : if (!AtraceCheckSupported()) {
136 22 : ADIAG_WAR("AtraceSave is not supported on device side");
137 22 : return TRACE_UNSUPPORTED;
138 : }
139 94 : ADIAG_INF("AtraceSave start, tracerType: %d, syncFlag: %d", (int32_t)tracerType, (int32_t)syncFlag);
140 :
141 94 : TraStatus ret = TracerSave(tracerType, syncFlag);
142 94 : ADIAG_INF("AtraceSave end, ret=%d.", ret);
143 94 : return ret;
144 : }
145 :
146 : /**
147 : * @brief init atrace struct entry list
148 : * @return atrace entry list
149 : */
150 256 : void *TRACE_API(StructEntryListInit)(void)
151 : {
152 256 : return TracerStructEntryListInit();
153 : }
154 :
155 : /**
156 : * @brief set atrace entry name
157 : * @param [in] entry: trace struct entry
158 : * @param [in] name: entry name
159 : * @return NA
160 : */
161 236 : void TRACE_API(StructEntryName)(TraceStructEntry *entry, const char *name)
162 : {
163 236 : return TracerStructEntryName(entry, name);
164 : }
165 :
166 : /**
167 : * @brief add atrace struct item
168 : * @param [in] entry: trace struct entry
169 : * @param [in] name: item name
170 : * @param [in] type: item type
171 : * @param [in] mode: item save mode
172 : * @param [in] bytes: bytes occupied by a single element
173 : * @param [in] length: bytes occupied by this item
174 : * @return NA
175 : */
176 658 : void TRACE_API(StructItemSet)(TraceStructEntry *entry, const char *name, uint8_t type, uint8_t mode, uint16_t length)
177 : {
178 658 : return TracerStructItemSet(entry, name, type, mode, length);
179 : }
180 :
181 : /**
182 : * @brief atrace struct entry exit
183 : * @param [in] entry: trace struct entry
184 : * @return NA
185 : */
186 255 : void TRACE_API(StructEntryExit)(TraceStructEntry *entry)
187 : {
188 255 : return TracerStructEntryExit(entry);
189 : }
190 :
191 20 : TraceStructEntry *TRACE_API(StructEntryCreate)(const char *name)
192 : {
193 20 : return TraceStructEntryCreate(name);
194 : }
195 :
196 20 : void TRACE_API(StructEntryDestroy)(TraceStructEntry *en)
197 : {
198 20 : TraceStructEntryDestroy(en);
199 20 : }
200 :
201 140 : void TRACE_API(StructItemFieldSet)(TraceStructEntry *en, const char *item, uint8_t type, uint8_t mode, uint16_t len)
202 : {
203 140 : TraceStructItemFieldSet(en, item, type, mode, len);
204 140 : }
205 :
206 60 : void TRACE_API(StructItemArraySet)(TraceStructEntry *en, const char *item, uint8_t type, uint8_t mode, uint16_t len)
207 : {
208 60 : TraceStructItemArraySet(en, item, type, mode, len);
209 60 : }
210 :
211 20 : void TRACE_API(StructSetAttr)(TraceStructEntry *en, uint8_t type, TraceAttr *attr)
212 : {
213 20 : TraceStructSetAttr(en, type, attr);
214 20 : }
215 :
216 : /*
217 : * @brief Create trace event.
218 : * @param [in] eventName: event name
219 : * @return event handle
220 : */
221 380 : TraEventHandle TRACE_API(EventCreate)(const char *eventName)
222 : {
223 380 : if (!AtraceCheckSupported()) {
224 20 : ADIAG_WAR("AtraceEventCreate is not supported on device side");
225 20 : return TRACE_UNSUPPORTED_HANDLE;
226 : }
227 360 : return TraceEventCreate(eventName);
228 : }
229 :
230 : /**
231 : * @brief Get event handle
232 : * @param [in] eventName: event name
233 : * @return event handle
234 : */
235 80 : TraEventHandle TRACE_API(EventGetHandle)(const char *eventName)
236 : {
237 80 : if (!AtraceCheckSupported()) {
238 20 : ADIAG_WAR("AtraceEventGetHandle is not supported on device side");
239 20 : return TRACE_UNSUPPORTED_HANDLE;
240 : }
241 60 : return TraceEventGetHandle(eventName);
242 : }
243 :
244 :
245 : /**
246 : * @brief Destroy event handle
247 : * @param [in] eventHandle: event handle
248 : * @return NA
249 : */
250 320 : void TRACE_API(EventDestroy)(TraEventHandle eventHandle)
251 : {
252 320 : if (!AtraceCheckSupported()) {
253 20 : ADIAG_WAR("AtraceEventDestroy is not supported on device side");
254 20 : return;
255 : }
256 300 : TraceEventDestroy(eventHandle);
257 : }
258 :
259 : /**
260 : * @brief Bind event handle with trace handle
261 : * @param [in] eventHandle: event handle
262 : * @param [in] handle: trace handle
263 : * @return TraStatus
264 : */
265 300 : TraStatus TRACE_API(EventBindTrace)(TraEventHandle eventHandle, TraHandle handle)
266 : {
267 300 : if (!AtraceCheckSupported()) {
268 20 : ADIAG_WAR("AtraceEventBindTrace is not supported on device side");
269 20 : return TRACE_UNSUPPORTED;
270 : }
271 280 : return TraceEventBindTrace(eventHandle, handle);
272 : }
273 :
274 : /**
275 : * @brief Set event attr
276 : * @param [in] eventHandle: event handle
277 : * @param [in] attr: event attribute
278 : * @return TraStatus
279 : */
280 200 : TraStatus TRACE_API(EventSetAttr)(TraEventHandle eventHandle, const TraceEventAttr *attr)
281 : {
282 200 : if (!AtraceCheckSupported()) {
283 20 : ADIAG_WAR("AtraceEventSetAttr is not supported on device side");
284 20 : return TRACE_UNSUPPORTED;
285 : }
286 180 : return TraceEventSetAttr(eventHandle, attr);
287 : }
288 :
289 : /**
290 : * @brief Report event and save the bound trace log to disk
291 : * @param [in] eventHandle: event handle
292 : * @return TraStatus
293 : */
294 1310900 : TraStatus TRACE_API(EventReport)(TraEventHandle eventHandle)
295 : {
296 1310900 : if (!AtraceCheckSupported()) {
297 0 : ADIAG_WAR("AtraceEventReport is not supported on device side");
298 0 : return TRACE_UNSUPPORTED;
299 : }
300 1310900 : return TraceEventReport(eventHandle);
301 : }
302 :
303 : /**
304 : * @brief Report event and save the bound trace log to disk sync
305 : * @param [in] eventHandle: event handle
306 : * @return TraStatus
307 : */
308 40 : TraStatus TRACE_API(EventReportSync)(TraEventHandle eventHandle)
309 : {
310 40 : if (!AtraceCheckSupported()) {
311 20 : ADIAG_WAR("AtraceEventReport is not supported on device side");
312 20 : return TRACE_UNSUPPORTED;
313 : }
314 20 : return TraceEventReport(eventHandle);
315 : }
316 :
317 : #ifdef UTRACE_API
318 : /**
319 : * @brief set global attribute
320 : * @param [in] attr: global attribute
321 : * @return TraStatus
322 : */
323 7 : TraStatus UtraceSetGlobalAttr(const TraceGlobalAttr *attr)
324 : {
325 7 : return TraceSetGlobalAttr(attr);
326 : }
327 : #endif
|