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 : #ifndef PROFILING_ADP_H
11 : #define PROFILING_ADP_H
12 :
13 : #include <string>
14 : #include <cstring>
15 : #include <ostream>
16 : #include <sstream>
17 : #include <memory>
18 : #include <unistd.h>
19 : #include <sys/syscall.h>
20 : #include <sys/types.h>
21 : #include "securec.h"
22 : #include "prof_api.h"
23 : #include "prof_dev_api.h"
24 : #include "dlog_pub.h"
25 : #include "common/type_def.h"
26 :
27 : #ifdef __cplusplus
28 : #ifndef LOG_CPP
29 : extern "C" {
30 : #endif
31 : #endif // __cplusplus
32 : LOG_FUNC_VISIBILITY int32_t __attribute__((weak)) CheckLogLevel(int32_t moduleId, int32_t logLevel);
33 : LOG_FUNC_VISIBILITY void __attribute((weak)) DlogRecord(int32_t moduleId, int32_t level, const char* fmt, ...);
34 : #ifdef __cplusplus
35 : #ifndef LOG_CPP
36 : }
37 : #endif // LOG_CPP
38 : #endif // __cplusplus
39 :
40 : namespace aicpu {
41 : constexpr uint8_t AICPU_PROF_VERSION = 0U;
42 : constexpr uint32_t MODEL_EXECUTE_START = 0U;
43 : constexpr uint32_t MODEL_EXECUTE_END = 1U;
44 :
45 : constexpr uint32_t PROFILING_FEATURE_SWITCH = 0U; // bit0 means profiling start or profiling stop
46 : constexpr uint32_t PROFILING_FEATURE_KERNEL_MODE = 1U; // bit1 means profiling mode of kernel
47 : constexpr uint32_t PROFILING_FEATURE_MODEL_MODE = 2U; // bit2 means profiling mode of model
48 :
49 : union ProfData {
50 148 : ProfData() {}
51 141 : ~ProfData() {}
52 : MsprofAicpuProfData aicpuProfData;
53 : MsprofDpProfData dPProfData;
54 : };
55 :
56 : struct ProfModelData {
57 7 : ProfModelData() {}
58 7 : ~ProfModelData() {}
59 : MsprofAicpuModelProfData aicpuModelProfData;
60 : };
61 :
62 22 : inline uint64_t ProfGetTid()
63 : {
64 22 : thread_local static const uint64_t TID = static_cast<uint64_t>(syscall(__NR_gettid));
65 22 : return TID;
66 : }
67 :
68 : #define AICPU_LOG_DEBUG(format, ...) \
69 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
70 : dlog_debug(static_cast<int32_t>(CCECPU), "[tid:%lu]:" format, ProfGetTid(), ##__VA_ARGS__); \
71 : }
72 : #define AICPU_LOG_INFO(format, ...) \
73 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
74 : dlog_info(static_cast<int32_t>(CCECPU), "[tid:%lu]:" format, ProfGetTid(), ##__VA_ARGS__); \
75 : }
76 : #define AICPU_LOG_WARN(format, ...) \
77 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
78 : dlog_warn(static_cast<int32_t>(CCECPU), "[tid:%lu]:" format, ProfGetTid(), ##__VA_ARGS__); \
79 : }
80 : #define AICPU_LOG_ERROR(format, ...) \
81 : if (&DlogRecord != nullptr) { \
82 : dlog_error(static_cast<int32_t>(CCECPU), "[tid:%lu]:" format, ProfGetTid(), ##__VA_ARGS__); \
83 : }
84 : #define AICPU_RUN_INFO(format, ...) \
85 : if ((&CheckLogLevel != nullptr) && (&DlogRecord != nullptr)) { \
86 : dlog_info( \
87 : static_cast<int32_t>(static_cast<uint32_t>(CCECPU) | static_cast<uint32_t>(RUN_LOG_MASK)), \
88 : "[tid:%lu]:" format, ProfGetTid(), ##__VA_ARGS__); \
89 : }
90 :
91 : #define AICPU_LOG_WHEN(cond, log, ...) \
92 : if (cond) { \
93 : AICPU_LOG_ERROR(log, ##__VA_ARGS__) \
94 : }
95 :
96 : #define AICPU_RCHECK(cond, ret, log, ...) \
97 : if (!(cond)) { \
98 : AICPU_LOG_ERROR(log, ##__VA_ARGS__) \
99 : return ret; \
100 : }
101 :
102 : class ProfMessage;
103 :
104 : #ifdef __cplusplus
105 : extern "C" {
106 : #endif
107 :
108 : /*****************************************************************************
109 : Prototype : IsProfOpen
110 : Description : judge if profiling is open
111 : Input : NA
112 : Output : NA
113 : Return Value : bool profiling flag
114 : 1.Date : 2021/10/09
115 : Modification : Created function
116 :
117 : *****************************************************************************/
118 : bool __attribute__((weak)) IsProfOpen();
119 :
120 : /*****************************************************************************
121 : Prototype : IsModelProfOpen
122 : Description : judge if model level profiling is open
123 : Input : NA
124 : Output : NA
125 : Return Value : bool profiling flag
126 : 1.Date : 2022/2/11
127 : Modification : Created function
128 :
129 : *****************************************************************************/
130 : bool __attribute__((weak)) IsModelProfOpen();
131 :
132 : /*****************************************************************************
133 : Prototype : NowMicros
134 : Description : it is used to get current micros
135 : Input : NA
136 : Output : NA
137 : Return Value : uint64_t current micros
138 : Calls : NA NA
139 : Called By : cp custcp
140 :
141 : History : NA
142 : 1.Date : 2019/4/12
143 : Modification : Created function
144 :
145 : *****************************************************************************/
146 : uint64_t __attribute__((weak)) NowMicros();
147 : /*****************************************************************************
148 : Prototype : GetSystemTick
149 : Description : it is used to get cpu tick
150 : Input : NA
151 : Output : NA
152 : Return Value : uint64_t tick
153 : Calls : NA
154 : Called By : cp custcp
155 :
156 : History : NA
157 : 1.Date : 2020/6/12
158 : Modification : Created function
159 :
160 : *****************************************************************************/
161 : uint64_t __attribute__((weak)) GetSystemTick();
162 : /*****************************************************************************
163 : Prototype : GetSystemTickFreq
164 : Description : it is used to get cpu tick freq
165 : Input : NA
166 : Output : NA
167 : Return Value : uint64_t tick freq
168 : Calls : NA
169 : Called By : cp custcp
170 :
171 : History : NA
172 : 1.Date : 2020/6/22
173 : Modification : Created function
174 :
175 : *****************************************************************************/
176 : uint64_t __attribute__((weak)) GetSystemTickFreq();
177 : /*****************************************************************************
178 : Prototype : GetMicrosAndSysTick
179 : Description : it is used to get now microsecond and cpu tick
180 : Input : uint64_t µs : now micros
181 : uint64_t &tick : system tick
182 : Output : NA
183 : Return Value : void
184 : Calls : NA
185 : Called By : cp custcp
186 :
187 : History : NA
188 : 1.Date : 2020/6/22
189 : Modification : Created function
190 :
191 : *****************************************************************************/
192 : void __attribute__((weak)) GetMicrosAndSysTick(uint64_t& micros, uint64_t& tick);
193 : /*****************************************************************************
194 : Prototype : InitProfiling
195 : Description : it is used to initialize the ProfilingAdp object and
196 : register instance to profiling
197 : Input : deviceId real device ID
198 : Input : hostPid host pid
199 : Output : NA
200 : Return Value : NA
201 : Calls : NA
202 : Called By : cp custcp
203 :
204 : History : NA
205 : 1.Date : 2019/4/12
206 : Modification : Created function
207 :
208 : *****************************************************************************/
209 : void __attribute__((weak)) InitProfiling(const uint32_t deviceId, const pid_t hostPid, const uint32_t channelId);
210 : /*****************************************************************************
211 : Prototype : IsProfilingValid
212 : Description : return weather profiling has been initialized
213 : Input : NA
214 : Output : NA
215 : Return Value : bool weather profiling has been initialized
216 : Calls : NA
217 : Called By : cp custcp modules who call send
218 :
219 : History : NA
220 : 1.Date : 2019/4/12
221 : Modification : Created function
222 :
223 : *****************************************************************************/
224 : bool __attribute__((weak)) IsProfilingValid();
225 : /*****************************************************************************
226 : Prototype : SendToProfiling
227 : Description : it is used to provide interface to DP module which can send data to profiling
228 : Input : const std::string& sendData : the data which it is need send to profiling
229 : std::string mark : the mark of dataset
230 : Output : NA
231 : Return Value : NA
232 : Calls : NA
233 : Called By : cp custcp
234 :
235 : History : NA
236 : 1.Date : 2019/4/12
237 : Modification : Created function
238 :
239 : *****************************************************************************/
240 : void __attribute__((weak)) SendToProfiling(const std::string& sendData, const std::string& mark);
241 : /*****************************************************************************
242 : Prototype : UpdateMode
243 : Description : it is used to provide interface to DP module which can update profiling mode
244 : Input : bool mode : OPEN or CLOSE
245 : Output : NA
246 : Return Value : NA
247 : Calls : NA
248 : Called By : cp custcp
249 :
250 : History : NA
251 : 1.Date : 2020/11/02
252 : Modification : Created function
253 :
254 : *****************************************************************************/
255 : void __attribute__((weak)) UpdateMode(const bool mode);
256 :
257 : /*****************************************************************************
258 : Prototype : UpdateModelMode
259 : Description : it is used to provide interface to set model level switch
260 : Input : const bool mode : profiling model level switch
261 : Output : NA
262 : Return Value : NA
263 : Calls : NA
264 : Called By : cp custcp
265 :
266 : History : NA
267 : 1.Date : 2022/2/11
268 : Modification : Created function
269 :
270 : *****************************************************************************/
271 : void __attribute__((weak)) UpdateModelMode(const bool mode);
272 :
273 : /*****************************************************************************
274 : Prototype : ReleaseProfiling
275 : Description : it is used to release all resource.
276 : register instance to profiling
277 : Input : NA
278 : Output : NA
279 : Return Value : NA
280 : Calls : NA
281 : Called By : cp custcp
282 :
283 : History : NA
284 : 1.Date : 2019/4/12
285 : Modification : Created function
286 :
287 : *****************************************************************************/
288 : void __attribute__((weak)) ReleaseProfiling(void);
289 :
290 : /*****************************************************************************
291 : Prototype : SetProfHandle
292 : Description : it is used to set prof handle
293 : Input : profMsg prof handle
294 : Output : NA
295 : Return Value : int32 0:succes, other: fail
296 : Calls : NA
297 : Called By : cp custcp
298 :
299 : History : NA
300 : 1.Date : 2020/9/18
301 : Modification : Created function
302 :
303 : *****************************************************************************/
304 : int32_t __attribute__((weak)) SetProfHandle(const std::shared_ptr<ProfMessage> profMsg);
305 :
306 : /*****************************************************************************
307 : Prototype : SetMsprofReporterCallback
308 : Description : it is used to set report callback function
309 : Input : report callback function
310 : Output : NA
311 : Return Value : int32 0:succes, other: fail
312 : Calls : NA
313 : Called By : cp custcp
314 :
315 : History : NA
316 : 1.Date : 2020/12/11
317 : Modification : Created function
318 :
319 : *****************************************************************************/
320 : int32_t __attribute__((weak)) SetMsprofReporterCallback(MsprofReporterCallback reportCallback);
321 :
322 : /*****************************************************************************
323 : Prototype : IsSupportedProfData
324 : Description : it is used to judge whether ProfData is supported
325 : Input : NA
326 : Output : NA
327 : Return Value : bool whether ProfData is supported
328 : Calls : NA
329 : Called By : cp custcp
330 :
331 : History : NA
332 : 1.Date : 2021/11/15
333 : Modification : Created function
334 :
335 : *****************************************************************************/
336 : bool __attribute__((weak)) IsSupportedProfData();
337 :
338 : /*****************************************************************************
339 : Prototype : GetProfHandle
340 : Description : it is used to get prof handle
341 : Input : NA
342 : Output : NA
343 : Return Value : ProfMessage prof handle
344 : Calls :
345 : Called By :
346 :
347 : History :
348 : 1.Date : 2020/9/18
349 : Modification : Created function
350 :
351 : *****************************************************************************/
352 : #ifndef __clang__
353 : std::shared_ptr<ProfMessage> __attribute__((weak)) GetProfHandle();
354 : #endif
355 :
356 : int32_t __attribute__((weak)) AicpuStartCallback();
357 :
358 : #ifdef __cplusplus
359 : }
360 : #endif
361 :
362 : /*****************************************************************************
363 : Prototype : GetProfHandle
364 : Description : it is used to get prof handle
365 : Input : NA
366 : Output : NA
367 : Return Value : ProfMessage prof handle
368 : Calls :
369 : Called By :
370 :
371 : History :
372 : 1.Date : 2020/9/18
373 : Modification : Created function
374 :
375 : *****************************************************************************/
376 : #ifdef __clang__
377 : std::shared_ptr<ProfMessage> __attribute__((weak)) GetProfHandle();
378 : #endif
379 :
380 : enum class ProfStatusCode : int32_t { PROFILINE_FAILED = -1, PROFILINE_SUCCESS = 0 };
381 :
382 : class ProfModelMessage : public std::basic_ostringstream<char_t> {
383 : public:
384 7 : explicit ProfModelMessage(const char_t* tag)
385 7 : : std::basic_ostringstream<char_t>(), tag_(tag), sendData_(), deviceId_(UINT16_MAX){};
386 7 : virtual ~ProfModelMessage() = default;
387 4 : ProfModelMessage* SetDataTagId(const uint16_t dataTagId)
388 : {
389 4 : sendData_.aicpuModelProfData.dataTag = dataTagId;
390 4 : return this;
391 : }
392 4 : ProfModelMessage* SetAicpuModelIterId(const uint16_t indexId)
393 : {
394 4 : sendData_.aicpuModelProfData.indexId = indexId;
395 4 : return this;
396 : }
397 4 : ProfModelMessage* SetAicpuModelTimeStamp(const uint64_t timeStamp)
398 : {
399 4 : sendData_.aicpuModelProfData.timeStamp = timeStamp;
400 4 : return this;
401 : }
402 4 : ProfModelMessage* SetAicpuModelId(const uint32_t modelId)
403 : {
404 4 : sendData_.aicpuModelProfData.modelId = modelId;
405 4 : return this;
406 : }
407 4 : ProfModelMessage* SetAicpuTagId(const uint16_t tagId)
408 : {
409 4 : sendData_.aicpuModelProfData.tagId = tagId;
410 4 : return this;
411 : }
412 4 : ProfModelMessage* SetEventId(const uint16_t eventId)
413 : {
414 4 : sendData_.aicpuModelProfData.eventId = eventId;
415 4 : return this;
416 : }
417 4 : ProfModelMessage* SetDeviceId(const uint32_t deviceId)
418 : {
419 4 : deviceId_ = deviceId;
420 4 : return this;
421 : }
422 : int32_t ReportProfModelMessage();
423 :
424 : int32_t SendProfModelMessageWithNewChannel();
425 :
426 : int32_t SendProfModelMessageWithOldChannel();
427 :
428 : void BuildProfModelAdditionalData(MsprofAdditionalInfo& reportData);
429 :
430 : private:
431 : ProfModelMessage(const ProfModelMessage&) = delete;
432 : ProfModelMessage& operator=(const ProfModelMessage&) = delete;
433 : const char_t* tag_;
434 : ProfModelData sendData_;
435 : uint32_t deviceId_;
436 : };
437 :
438 : class ProfMessage : public std::basic_ostringstream<char_t> {
439 : public:
440 : explicit ProfMessage(const char_t* tag);
441 : virtual ~ProfMessage();
442 139 : ProfMessage* SetAicpuMagicNumber(const uint16_t aicpuMagicNumber)
443 : {
444 139 : sendData_.aicpuProfData.magicNumber = aicpuMagicNumber;
445 139 : return this;
446 : }
447 139 : ProfMessage* SetAicpuDataTag(const uint16_t aicpuDataTag)
448 : {
449 139 : sendData_.aicpuProfData.dataTag = aicpuDataTag;
450 139 : return this;
451 : }
452 139 : ProfMessage* SetStreamId(const uint16_t streamId)
453 : {
454 139 : sendData_.aicpuProfData.streamId = streamId;
455 139 : return this;
456 : }
457 139 : ProfMessage* SetTaskId(const uint16_t taskId)
458 : {
459 139 : sendData_.aicpuProfData.taskId = taskId;
460 139 : return this;
461 : }
462 : ProfMessage* SetRunStartTime(const uint64_t runStartTime)
463 : {
464 : sendData_.aicpuProfData.runStartTime = runStartTime;
465 : return this;
466 : }
467 : ProfMessage* SetRunStartTick(const uint64_t runStartTick)
468 : {
469 : sendData_.aicpuProfData.runStartTick = runStartTick;
470 : return this;
471 : }
472 : ProfMessage* SetComputeStartTime(const uint64_t computeStartTime)
473 : {
474 : sendData_.aicpuProfData.computeStartTime = computeStartTime;
475 : return this;
476 : }
477 : ProfMessage* SetMemcpyStartTime(const uint64_t memcpyStartTime)
478 : {
479 : sendData_.aicpuProfData.memcpyStartTime = memcpyStartTime;
480 : return this;
481 : }
482 : ProfMessage* SetMemcpyEndTime(const uint64_t memcpyEndTime)
483 : {
484 : sendData_.aicpuProfData.memcpyEndTime = memcpyEndTime;
485 : return this;
486 : }
487 : ProfMessage* SetRunEndTime(const uint64_t runEndTime)
488 : {
489 : sendData_.aicpuProfData.runEndTime = runEndTime;
490 : return this;
491 : }
492 : ProfMessage* SetRunEndTick(const uint64_t runEndTick)
493 : {
494 : sendData_.aicpuProfData.runEndTick = runEndTick;
495 : return this;
496 : }
497 139 : ProfMessage* SetThreadId(const uint32_t threadId)
498 : {
499 139 : sendData_.aicpuProfData.threadId = threadId;
500 139 : return this;
501 : }
502 139 : ProfMessage* SetDeviceId(const uint32_t deviceId)
503 : {
504 139 : sendData_.aicpuProfData.deviceId = deviceId;
505 139 : return this;
506 : }
507 139 : ProfMessage* SetKernelType(const uint32_t aicpuKernelType)
508 : {
509 139 : sendData_.aicpuProfData.kernelType = aicpuKernelType;
510 139 : return this;
511 : }
512 139 : ProfMessage* SetSubmitTick(const uint64_t submitTick)
513 : {
514 139 : sendData_.aicpuProfData.submitTick = submitTick;
515 139 : return this;
516 : }
517 139 : ProfMessage* SetScheduleTick(const uint64_t scheduleTick)
518 : {
519 139 : sendData_.aicpuProfData.scheduleTick = scheduleTick;
520 139 : return this;
521 : }
522 139 : ProfMessage* SetTickBeforeRun(const uint64_t tickBeforeRun)
523 : {
524 139 : sendData_.aicpuProfData.tickBeforeRun = tickBeforeRun;
525 139 : return this;
526 : }
527 137 : ProfMessage* SetTickAfterRun(const uint64_t tickAfterRun)
528 : {
529 137 : sendData_.aicpuProfData.tickAfterRun = tickAfterRun;
530 137 : return this;
531 : }
532 136 : ProfMessage* SetDispatchTime(const uint32_t dispatchTime)
533 : {
534 136 : sendData_.aicpuProfData.dispatchTime = dispatchTime;
535 136 : return this;
536 : }
537 136 : ProfMessage* SetTotalTime(const uint32_t totalTime)
538 : {
539 136 : sendData_.aicpuProfData.totalTime = totalTime;
540 136 : return this;
541 : }
542 : ProfMessage* SetFFTSThreadId(const uint16_t fftsThreadId)
543 : {
544 : sendData_.aicpuProfData.fftsThreadId = fftsThreadId;
545 : return this;
546 : }
547 136 : ProfMessage* SetVersion(const uint8_t aicpuProfVersion)
548 : {
549 136 : sendData_.aicpuProfData.version = aicpuProfVersion;
550 136 : return this;
551 : }
552 1 : ProfMessage* SetDPMagicNumber(const uint16_t dPMagicNumber)
553 : {
554 1 : sendData_.dPProfData.magicNumber = dPMagicNumber;
555 1 : return this;
556 : }
557 1 : ProfMessage* SetDPDataTag(const uint16_t dPDataTag)
558 : {
559 1 : sendData_.dPProfData.dataTag = dPDataTag;
560 1 : return this;
561 : }
562 1 : ProfMessage* SetAction(const std::string& action)
563 : {
564 : const auto ret =
565 1 : strcpy_s(sendData_.dPProfData.action, static_cast<size_t>(MSPROF_DP_DATA_ACTION_LEN), action.c_str());
566 1 : if (ret != EOK) {
567 0 : AICPU_LOG_WARN("Copy action[%s] failed, ret=[%d].", action.c_str(), ret);
568 : }
569 1 : return this;
570 : }
571 1 : ProfMessage* SetSource(const std::string& source)
572 : {
573 : const auto ret =
574 1 : strcpy_s(sendData_.dPProfData.source, static_cast<size_t>(MSPROF_DP_DATA_SOURCE_LEN), source.c_str());
575 1 : if (ret != EOK) {
576 0 : AICPU_LOG_WARN("Copy source[%s] failed, ret=[%d].", source.c_str(), ret);
577 : }
578 1 : return this;
579 : }
580 1 : ProfMessage* SetIndex(const uint64_t queueIndex)
581 : {
582 1 : sendData_.dPProfData.index = queueIndex;
583 1 : return this;
584 : }
585 1 : ProfMessage* SetSize(const uint64_t size)
586 : {
587 1 : sendData_.dPProfData.size = size;
588 1 : return this;
589 : }
590 2 : ProfMessage* SetTimeStamp(const uint64_t timeStamp)
591 : {
592 2 : sendData_.dPProfData.timeStamp = timeStamp;
593 2 : return this;
594 : }
595 :
596 : private:
597 : ProfMessage(const ProfMessage&) = delete;
598 : ProfMessage& operator=(const ProfMessage&) = delete;
599 : const char_t* tag_;
600 : ProfData sendData_;
601 : };
602 : // Micros for calling the profiling interface friendly
603 : // eg. `PROF(CCECPU) << "My id: " << 5;` will send `[timestap] My id: 5` to profing data
604 : // which timestap is when this micros call
605 : #define PROF(tag) \
606 : if (aicpu::IsProfilingValid()) \
607 : aicpu::ProfMessage(#tag)
608 :
609 : } // namespace aicpu
610 : #endif
|