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