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 633 : TraHandle TRACE_API(Create)(TracerType tracerType, const char* objName)
35 : {
36 633 : if (!AtraceCheckSupported()) {
37 24 : ADIAG_WAR("AtraceCreate is not supported on device side");
38 24 : return TRACE_UNSUPPORTED_HANDLE;
39 : }
40 609 : ADIAG_CHK_EXPR_ACTION(
41 : tracerType >= TRACER_TYPE_MAX, return TRACE_INVALID_HANDLE, "tracer type %d is invalid.", (int32_t)tracerType);
42 609 : ADIAG_CHK_NULL_PTR(objName, return TRACE_INVALID_HANDLE);
43 :
44 609 : TraceAttr attr = {0};
45 609 : attr.exitSave = false;
46 609 : TraHandle ret = TracerObjCreate(tracerType, objName, &attr);
47 609 : ADIAG_DBG("AtraceCreate [%s].", objName);
48 609 : 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 337 : TraHandle TRACE_API(CreateWithAttr)(TracerType tracerType, const char* objName, const TraceAttr* attr)
59 : {
60 337 : if (!AtraceCheckSupported()) {
61 24 : ADIAG_WAR("AtraceCreate is not supported on device side");
62 24 : return TRACE_UNSUPPORTED_HANDLE;
63 : }
64 313 : ADIAG_CHK_EXPR_ACTION(
65 : tracerType >= TRACER_TYPE_MAX, return TRACE_INVALID_HANDLE, "tracer type %d is invalid.", (int32_t)tracerType);
66 313 : ADIAG_CHK_NULL_PTR(objName, return TRACE_INVALID_HANDLE);
67 :
68 313 : TraHandle ret = TracerObjCreate(tracerType, objName, attr);
69 313 : ADIAG_DBG("AtraceCreateWithAttr [%s].", objName);
70 313 : 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 96 : TraHandle TRACE_API(GetHandle)(TracerType tracerType, const char* objName)
80 : {
81 96 : if (!AtraceCheckSupported()) {
82 24 : ADIAG_WAR("AtraceGetHandle is not supported on device side");
83 24 : return TRACE_UNSUPPORTED_HANDLE;
84 : }
85 72 : 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 91460 : TraStatus TRACE_API(Submit)(TraHandle handle, const void* buffer, uint32_t bufSize)
96 : {
97 91460 : 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 992 : void TRACE_API(Destroy)(TraHandle handle)
119 : {
120 992 : if (!AtraceCheckSupported()) {
121 24 : ADIAG_WAR("AtraceDestroy is not supported on device side");
122 24 : return;
123 : }
124 968 : 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 126 : TraStatus TRACE_API(Save)(TracerType tracerType, bool syncFlag)
134 : {
135 126 : if (!AtraceCheckSupported()) {
136 24 : ADIAG_WAR("AtraceSave is not supported on device side");
137 24 : return TRACE_UNSUPPORTED;
138 : }
139 102 : ADIAG_INF("AtraceSave start, tracerType: %d, syncFlag: %d", (int32_t)tracerType, (int32_t)syncFlag);
140 :
141 102 : TraStatus ret = TracerSave(tracerType, syncFlag);
142 102 : ADIAG_INF("AtraceSave end, ret=%d.", ret);
143 102 : return ret;
144 : }
145 :
146 : /**
147 : * @brief init atrace struct entry list
148 : * @return atrace entry list
149 : */
150 280 : void* TRACE_API(StructEntryListInit)(void) { return TracerStructEntryListInit(); }
151 :
152 : /**
153 : * @brief set atrace entry name
154 : * @param [in] entry: trace struct entry
155 : * @param [in] name: entry name
156 : * @return NA
157 : */
158 258 : void TRACE_API(StructEntryName)(TraceStructEntry* entry, const char* name)
159 : {
160 258 : return TracerStructEntryName(entry, name);
161 : }
162 :
163 : /**
164 : * @brief add atrace struct item
165 : * @param [in] entry: trace struct entry
166 : * @param [in] name: item name
167 : * @param [in] type: item type
168 : * @param [in] mode: item save mode
169 : * @param [in] bytes: bytes occupied by a single element
170 : * @param [in] length: bytes occupied by this item
171 : * @return NA
172 : */
173 716 : void TRACE_API(StructItemSet)(TraceStructEntry* entry, const char* name, uint8_t type, uint8_t mode, uint16_t length)
174 : {
175 716 : return TracerStructItemSet(entry, name, type, mode, length);
176 : }
177 :
178 : /**
179 : * @brief atrace struct entry exit
180 : * @param [in] entry: trace struct entry
181 : * @return NA
182 : */
183 279 : void TRACE_API(StructEntryExit)(TraceStructEntry* entry) { return TracerStructEntryExit(entry); }
184 :
185 22 : TraceStructEntry* TRACE_API(StructEntryCreate)(const char* name) { return TraceStructEntryCreate(name); }
186 :
187 22 : void TRACE_API(StructEntryDestroy)(TraceStructEntry* en) { TraceStructEntryDestroy(en); }
188 :
189 154 : void TRACE_API(StructItemFieldSet)(TraceStructEntry* en, const char* item, uint8_t type, uint8_t mode, uint16_t len)
190 : {
191 154 : TraceStructItemFieldSet(en, item, type, mode, len);
192 154 : }
193 :
194 66 : void TRACE_API(StructItemArraySet)(TraceStructEntry* en, const char* item, uint8_t type, uint8_t mode, uint16_t len)
195 : {
196 66 : TraceStructItemArraySet(en, item, type, mode, len);
197 66 : }
198 :
199 22 : void TRACE_API(StructSetAttr)(TraceStructEntry* en, uint8_t type, TraceAttr* attr)
200 : {
201 22 : TraceStructSetAttr(en, type, attr);
202 22 : }
203 :
204 : /*
205 : * @brief Create trace event.
206 : * @param [in] eventName: event name
207 : * @return event handle
208 : */
209 418 : TraEventHandle TRACE_API(EventCreate)(const char* eventName)
210 : {
211 418 : if (!AtraceCheckSupported()) {
212 22 : ADIAG_WAR("AtraceEventCreate is not supported on device side");
213 22 : return TRACE_UNSUPPORTED_HANDLE;
214 : }
215 396 : return TraceEventCreate(eventName);
216 : }
217 :
218 : /**
219 : * @brief Get event handle
220 : * @param [in] eventName: event name
221 : * @return event handle
222 : */
223 88 : TraEventHandle TRACE_API(EventGetHandle)(const char* eventName)
224 : {
225 88 : if (!AtraceCheckSupported()) {
226 22 : ADIAG_WAR("AtraceEventGetHandle is not supported on device side");
227 22 : return TRACE_UNSUPPORTED_HANDLE;
228 : }
229 66 : return TraceEventGetHandle(eventName);
230 : }
231 :
232 : /**
233 : * @brief Destroy event handle
234 : * @param [in] eventHandle: event handle
235 : * @return NA
236 : */
237 352 : void TRACE_API(EventDestroy)(TraEventHandle eventHandle)
238 : {
239 352 : if (!AtraceCheckSupported()) {
240 22 : ADIAG_WAR("AtraceEventDestroy is not supported on device side");
241 22 : return;
242 : }
243 330 : TraceEventDestroy(eventHandle);
244 : }
245 :
246 : /**
247 : * @brief Bind event handle with trace handle
248 : * @param [in] eventHandle: event handle
249 : * @param [in] handle: trace handle
250 : * @return TraStatus
251 : */
252 330 : TraStatus TRACE_API(EventBindTrace)(TraEventHandle eventHandle, TraHandle handle)
253 : {
254 330 : if (!AtraceCheckSupported()) {
255 22 : ADIAG_WAR("AtraceEventBindTrace is not supported on device side");
256 22 : return TRACE_UNSUPPORTED;
257 : }
258 308 : return TraceEventBindTrace(eventHandle, handle);
259 : }
260 :
261 : /**
262 : * @brief Set event attr
263 : * @param [in] eventHandle: event handle
264 : * @param [in] attr: event attribute
265 : * @return TraStatus
266 : */
267 220 : TraStatus TRACE_API(EventSetAttr)(TraEventHandle eventHandle, const TraceEventAttr* attr)
268 : {
269 220 : if (!AtraceCheckSupported()) {
270 22 : ADIAG_WAR("AtraceEventSetAttr is not supported on device side");
271 22 : return TRACE_UNSUPPORTED;
272 : }
273 198 : return TraceEventSetAttr(eventHandle, attr);
274 : }
275 :
276 : /**
277 : * @brief Report event and save the bound trace log to disk
278 : * @param [in] eventHandle: event handle
279 : * @return TraStatus
280 : */
281 1441990 : TraStatus TRACE_API(EventReport)(TraEventHandle eventHandle)
282 : {
283 1441990 : if (!AtraceCheckSupported()) {
284 0 : ADIAG_WAR("AtraceEventReport is not supported on device side");
285 0 : return TRACE_UNSUPPORTED;
286 : }
287 1441990 : return TraceEventReport(eventHandle);
288 : }
289 :
290 : /**
291 : * @brief Report event and save the bound trace log to disk sync
292 : * @param [in] eventHandle: event handle
293 : * @return TraStatus
294 : */
295 44 : TraStatus TRACE_API(EventReportSync)(TraEventHandle eventHandle)
296 : {
297 44 : if (!AtraceCheckSupported()) {
298 22 : ADIAG_WAR("AtraceEventReport is not supported on device side");
299 22 : return TRACE_UNSUPPORTED;
300 : }
301 22 : return TraceEventReport(eventHandle);
302 : }
303 :
304 : /**
305 : * @brief set global attribute
306 : * @param [in] attr: global attribute
307 : * @return TraStatus
308 : */
309 7 : TraStatus TRACE_API(SetGlobalAttr)(const TraceGlobalAttr* attr) { return TraceSetGlobalAttr(attr); }
|