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 : #include "aicpu_prof/profiling_adp.h"
12 : #include <vector>
13 : #include <string>
14 : #include <pthread.h>
15 : #include <securec.h>
16 : #include <thread>
17 : #include <atomic>
18 : #include <fstream>
19 : #include "common/type_def.h"
20 : #include "aicpu_context.h"
21 : #include "aicpu_prof.h"
22 : #include "prof_so_manager.h"
23 : #include "aprof_pub.h"
24 :
25 : namespace aicpu {
26 : namespace {
27 : thread_local std::shared_ptr<ProfMessage> g_prof(nullptr);
28 : std::atomic<bool> g_profFlag(false);
29 : std::atomic<bool> g_modelProfFlag(false);
30 : std::atomic<bool> g_profAdditionalFlag(false);
31 :
32 : #ifdef RUN_ON_AICPU
33 : inline void AsmCntvc(uint64_t &cntvct)
34 : {
35 : asm volatile("mrs %0, cntvct_el0" : "=r" (cntvct));
36 : }
37 :
38 : inline void AsmCntfrq(uint64_t &cntfrq)
39 : {
40 : asm volatile("mrs %0, cntfrq_el0" : "=r" (cntfrq));
41 : }
42 : #endif
43 : }
44 : constexpr int32_t MAX_INNER_SEND_DATA_LEN = 1024;
45 :
46 : #ifdef __cplusplus
47 : extern "C" {
48 : #endif
49 :
50 16 : bool IsProfOpen()
51 : {
52 16 : return g_profFlag;
53 : }
54 :
55 5 : bool IsModelProfOpen()
56 : {
57 5 : return g_modelProfFlag;
58 : }
59 :
60 : /*****************************************************************************
61 : Description : it is used to get current micros
62 : Input : NA
63 : Output : NA
64 : Return Value : current micros
65 : *****************************************************************************/
66 2 : uint64_t NowMicros()
67 : {
68 : #ifdef RUN_ON_AICPU
69 : uint64_t cntvct;
70 : uint64_t cntfrq;
71 : AsmCntvc(cntvct);
72 : AsmCntfrq(cntfrq);
73 : return static_cast<uint64_t>(cntvct * 1000000ULL / cntfrq); //lint !e573
74 : #else
75 : struct timespec ts;
76 2 : clock_gettime(CLOCK_REALTIME, &ts);
77 2 : return (static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL + // Seconds to nanos
78 2 : static_cast<uint64_t>(ts.tv_nsec)) / 1000ULL; // Nanos to micros
79 : #endif
80 : }
81 :
82 : /*****************************************************************************
83 : Prototype : GetSystemTick
84 : Description : it is used to get cpu tick
85 : Input : NA
86 : Output : NA
87 : Return Value : uint64_t tick
88 : *****************************************************************************/
89 7 : uint64_t GetSystemTick()
90 : {
91 : #ifdef RUN_ON_AICPU
92 : uint64_t cntvct;
93 : AsmCntvc(cntvct);
94 : return cntvct;
95 : #else
96 7 : return 0;
97 : #endif
98 : }
99 :
100 : /*****************************************************************************
101 : Prototype : GetSystemTickFreq
102 : Description : it is used to get cpu tick freq
103 : Input : NA
104 : Output : NA
105 : Return Value : uint64_t tick freq
106 : *****************************************************************************/
107 1 : uint64_t GetSystemTickFreq()
108 : {
109 : #ifdef RUN_ON_AICPU
110 : uint64_t cntfrq;
111 : AsmCntfrq(cntfrq);
112 : return cntfrq;
113 : #else
114 1 : return 0;
115 : #endif
116 : }
117 :
118 : /*****************************************************************************
119 : Prototype : GetMicrosAndSysTick
120 : Description : it is used to get now microsecond and cpu tick
121 : Input : uint64_t µs : now micros
122 : uint64_t &tick : system tick
123 : Output : NA
124 : Return Value : void
125 : *****************************************************************************/
126 1 : void GetMicrosAndSysTick(uint64_t µs, uint64_t &tick)
127 : {
128 : #ifdef RUN_ON_AICPU
129 : uint64_t cntvct;
130 : uint64_t cntfrq;
131 : AsmCntvc(cntvct);
132 : AsmCntfrq(cntfrq);
133 : micros = static_cast<uint64_t>(cntvct * 1000000ULL / cntfrq);
134 : tick = cntvct;
135 : #else
136 : struct timespec ts;
137 1 : clock_gettime(CLOCK_REALTIME, &ts);
138 1 : micros = (static_cast<uint64_t>(ts.tv_sec) * 1000000000ULL + // Seconds to nanos
139 1 : static_cast<uint64_t>(ts.tv_nsec)) / 1000ULL; // Nanos to micros
140 : // tick is not supported when not running on aicpu
141 1 : tick = 0;
142 : #endif
143 1 : }
144 :
145 : /*****************************************************************************
146 : Description : it is used to initialize the ProfilingAdp object and
147 : register instance to profiling
148 : Input : deviceId real device ID
149 : Input : hostPid host pid
150 : Input :channelId is used to distinguish aicpu process or cust-aicpu process
151 : Output : NA
152 : Return Value : NA
153 : *****************************************************************************/
154 3 : void InitProfiling(const uint32_t deviceId, const pid_t hostPid, const uint32_t channelId)
155 : {
156 3 : AICPU_LOG_INFO("Start initializing profiling.");
157 3 : const int32_t ret = ProfilingAdp::GetInstance().InitAicpuProfiling(deviceId, hostPid, channelId);
158 3 : AICPU_LOG_WHEN(ret != static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS),
159 : "Failed to init profiling(%d).", ret);
160 3 : }
161 :
162 : /*****************************************************************************
163 : Description : it is used to initialize the ProfilingAdp object data info
164 : Input : deviceId real device ID
165 : Input : hostPid host pid
166 : Output : NA
167 : Return Value : NA
168 : *****************************************************************************/
169 0 : void InitProfilingDataInfo(const uint32_t deviceId, const pid_t hostPid, const uint32_t channelId)
170 : {
171 0 : AICPU_LOG_INFO("Start initializing profiling data info.");
172 0 : ProfilingAdp::GetInstance().InitAicpuProfilingDataInfo(deviceId, hostPid, channelId);
173 0 : }
174 :
175 1 : void SetProfilingFlagForKFC(const uint32_t flag)
176 : {
177 1 : ProfilingAdp::GetInstance().SetAicpuProfilingFlagForKFC(flag);
178 1 : const int32_t retInit = ProfilingAdp::GetInstance().ProfilingModeOpenProcess();
179 1 : AICPU_LOG_WHEN(retInit != static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS),
180 : "Failed init profiling(%d).", retInit);
181 1 : }
182 1 : void LoadProfilingLib()
183 : {
184 1 : aicpu::ProfSoManager::GetInstance()->LoadSo();
185 1 : return;
186 : }
187 : /*****************************************************************************
188 : Description : return weather profiling has been initialized
189 : Input : NA
190 : Output : NA
191 : Return Value : bool weather profiling has been initialized
192 : *****************************************************************************/
193 0 : bool IsProfilingValid()
194 : {
195 0 : return ProfilingAdp::GetInstance().GetReportValid();
196 : }
197 :
198 : /*****************************************************************************
199 : Description : it is used to send data to profiling for ProfData
200 : Input : std::const string& sendData : the data which it is need send to profiling
201 : std::string mark : the mark of dataset
202 : Output : NA
203 : Return Value : NA
204 : *****************************************************************************/
205 : namespace {
206 12 : void SendToProfilingForSimplify(const char_t * const sendData, const std::string &mark)
207 : {
208 12 : if (g_profFlag) {
209 12 : const int32_t ret = ProfilingAdp::GetInstance().Send(sendData, mark);
210 12 : AICPU_LOG_WHEN(ret != static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS),
211 : "Failed send profiling data, ret[%d].", ret);
212 12 : AICPU_LOG_INFO("End of report profiling data");
213 : }
214 12 : }
215 : }
216 :
217 : /*****************************************************************************
218 : Description : it is used to provide interface to DP module which can send data to profiling
219 : Input : std::const string& sendData : the data which it is need send to profiling
220 : std::string mark : the mark of dataset
221 : Output : NA
222 : Return Value : NA
223 : *****************************************************************************/
224 7 : void SendToProfiling(const std::string &sendData, const std::string &mark)
225 : {
226 7 : SendToProfilingForSimplify(PtrToPtr<const std::string, const char_t>(&sendData), mark);
227 7 : }
228 :
229 20 : void UpdateMode(const bool mode)
230 : {
231 20 : AICPU_LOG_INFO("UpdateMode:profilingMode[%s]", mode ? "OPEN":"CLOSE");
232 20 : if (mode) {
233 20 : ProfilingAdp::GetInstance().UpdateModeProcess(mode);
234 : }
235 20 : g_profFlag = mode;
236 20 : }
237 :
238 5 : void UpdateModelMode(const bool mode)
239 : {
240 5 : AICPU_LOG_INFO("UpdateModelMode:module[%s].", mode ? "OPEN" : "CLOSE");
241 5 : g_modelProfFlag = mode;
242 5 : }
243 :
244 : /*****************************************************************************
245 : Description : it is used to provide interface to DP module which can flush data to profiling
246 : Input : std::const string& sendData : the data which it is need send to profiling
247 : std::string mark : the mark of dataset
248 : Output : NA
249 : Return Value : NA
250 : *****************************************************************************/
251 4 : void ReleaseProfiling()
252 : {
253 4 : int32_t ret = ProfilingAdp::GetInstance().UninitProcess();
254 4 : AICPU_LOG_WHEN(ret != static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS),
255 : "Failed unit profiling(%d).", ret);
256 4 : }
257 :
258 1 : int32_t SetProfHandle(const std::shared_ptr<ProfMessage> profMsg)
259 : {
260 1 : g_prof = profMsg;
261 1 : return 0;
262 : }
263 :
264 0 : int32_t SetMsprofReporterCallback(MsprofReporterCallback reportCallback)
265 : {
266 0 : AICPU_LOG_INFO("Start set reporter callback function");
267 0 : ProfilingAdp::GetInstance().SetReportCallbackValid(true, reportCallback);
268 0 : return static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS);
269 : }
270 :
271 0 : bool IsSupportedProfData()
272 : {
273 0 : return true;
274 : }
275 :
276 : #ifndef __clang__
277 1 : std::shared_ptr<ProfMessage> GetProfHandle()
278 : {
279 1 : return g_prof;
280 : }
281 : #endif
282 :
283 : #ifdef __cplusplus
284 : }
285 : #endif
286 :
287 : #ifdef __clang__
288 : std::shared_ptr<ProfMessage> GetProfHandle()
289 : {
290 : return g_prof;
291 : }
292 : #endif
293 :
294 : /*****************************************************************************
295 : Description : Construct an simple prof agent.
296 : Input : const char_t* tag
297 : Output : NA
298 : *****************************************************************************/
299 5 : ProfMessage::ProfMessage(const char_t *tag) : std::basic_ostringstream<char_t>(), tag_(tag), sendData_()
300 : {
301 5 : const auto ret = memset_s(&sendData_, sizeof(sendData_), 0x00, sizeof(sendData_));
302 5 : if (ret != EOK) {
303 0 : AICPU_LOG_ERROR("memset error ret:%d", ret);
304 : }
305 5 : AICPU_LOG_INFO("sendData_ is initialized");
306 5 : const std::string mark = tag;
307 5 : if (mark == "DP") {
308 2 : (void)SetTimeStamp(NowMicros());
309 : } else {
310 3 : AICPU_LOG_INFO("mark is [%s].", mark.c_str());
311 : }
312 5 : }
313 :
314 : /*****************************************************************************
315 : Description : Call prof interface at the end of the object declaration cycle
316 : Input : NA
317 : Output : NA
318 : *****************************************************************************/
319 5 : ProfMessage::~ProfMessage()
320 : {
321 5 : const std::string mark(tag_);
322 5 : if (mark == "AICPU") {
323 3 : SendToProfilingForSimplify(PtrToPtr<MsprofAicpuProfData, const char_t>(&sendData_.aicpuProfData), mark);
324 2 : } else if (mark == "DP") {
325 2 : SendToProfilingForSimplify(PtrToPtr<MsprofDpProfData, const char_t>(&sendData_.dPProfData), mark);
326 : } else {
327 0 : AICPU_LOG_INFO("Will not send data, mark=%s.", mark.c_str());
328 : }
329 5 : }
330 :
331 : /*****************************************************************************
332 : Description : it is used to set report callback function valid
333 : Input : bool flag : if it is true,it means can send data through profiling.
334 : otherwise,it must stop send data to profiling.
335 : MsprofReporterCallback reportCallback: the send report callback of profiling
336 : Output : NA
337 : Return Value : NA
338 : *****************************************************************************/
339 4 : void ProfilingAdp::SetReportCallbackValid(bool flag, MsprofReporterCallback profReportCallback)
340 : {
341 : (void)flag;
342 4 : AICPU_LOG_INFO("Start enable reporter callback.");
343 4 : reportCallback_ = profReportCallback;
344 4 : }
345 :
346 3 : MsprofReporterCallback ProfilingAdp::GetMsprofReporterCallback(void)
347 : {
348 3 : MsprofReporterCallback repCallBack = reportCallback_;
349 3 : return repCallBack;
350 : }
351 :
352 : /*****************************************************************************
353 : Description : the function need to
354 : Input : NA
355 : Output : NA
356 : Return Value : return a global and unique instance of class MapMarkAndGlobal
357 : *****************************************************************************/
358 7 : bool ProfilingAdp::GetReportValid(void)
359 : {
360 7 : const bool aviableFlag = initFlag_;
361 7 : return aviableFlag;
362 : }
363 :
364 : /*****************************************************************************
365 : Description : it is used to send data to profiling
366 : Input : ReporterData& data : it needs to send to profiling
367 : Output : NA
368 : Return Value : PROFILINE_SUCCESS - send data success
369 : others - send data failed
370 : *****************************************************************************/
371 8 : int32_t ProfilingAdp::SendProcess(ReporterData &data)
372 : {
373 8 : AICPU_LOG_INFO("Call SendProcess to report message.");
374 8 : int32_t ret = static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS);
375 8 : if (initFlag_) {
376 8 : if (reportCallback_ != nullptr) {
377 3 : AICPU_LOG_INFO("Start to report profiling data using report callback.");
378 3 : ret = reportCallback_(MSPROF_MODULE_DATA_PREPROCESS, MSPROF_REPORTER_REPORT,
379 : &data, static_cast<uint32_t>(sizeof(data)));
380 5 : } else if (isProfApiSo_ == true){
381 : ;
382 : } else {
383 5 : AICPU_LOG_INFO("Start to report profiling data using AdprofReportData, tag=%s, len=%zu.",
384 : data.tag, data.dataLen);
385 5 : ret = AdprofReportDataFunc(&data, static_cast<uint32_t>(sizeof(data)));
386 : }
387 : }
388 8 : return ret;
389 : }
390 :
391 4 : int32_t ProfilingAdp::SendAdditionalProcess(MsprofAdditionalInfo &additionalReportData)
392 : {
393 4 : int32_t ret = static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS);
394 4 : if (initFlag_) {
395 4 : if (isProfApiSo_ == true) {
396 0 : AICPU_LOG_INFO("Call MsprofReportAdditionalInfoFunc to report message.");
397 0 : ret = MsprofReportAdditionalInfoFunc(0U, static_cast<void *>(&additionalReportData),
398 : sizeof(additionalReportData));
399 : } else {
400 4 : AICPU_LOG_INFO("Call AdprofReportAdditionalInfo to report message.");
401 4 : ret = AdprofReportAdditionalInfoFunc(0U, static_cast<void *>(&additionalReportData),
402 : sizeof(additionalReportData));
403 : }
404 : }
405 4 : return ret;
406 : }
407 :
408 : /*****************************************************************************
409 : Description : it is used to uninit profiling
410 : Input : NA
411 : Output : NA
412 : Return Value : PROFILINE_SUCCESS - uninit success
413 : others - uninit failed
414 : *****************************************************************************/
415 6 : int32_t ProfilingAdp::UninitProcess()
416 : {
417 6 : if (!initFlag_) {
418 0 : AICPU_LOG_INFO("Profiling was not initialized.");
419 0 : return static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS);
420 : }
421 6 : int32_t retUnInit = 0;
422 6 : if (reportCallback_ != nullptr) {
423 : // rc场景下仅需执行reportCallback_函数的退出
424 3 : retUnInit = reportCallback_(MSPROF_MODULE_DATA_PREPROCESS, MSPROF_REPORTER_UNINIT, nullptr, 0U);
425 3 : } else if ((!g_profAdditionalFlag) && (isProfApiSo_ == false)) {
426 : // 非rc场景,老的HDC上报流程需要执行AdprofAicpuStopFunc保证退出时不会漏数据
427 3 : retUnInit = AdprofAicpuStopFunc();
428 : }
429 6 : if (retUnInit == static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS)) {
430 4 : aicpu::ProfSoManager::GetInstance()->UnloadSo();
431 4 : profilingFlag_ = 0;
432 4 : initFlag_ = false;
433 : }
434 6 : return retUnInit;
435 : }
436 :
437 : /*****************************************************************************
438 : Description : it is used to initialize the ProfilingAdp object and
439 : register instance to profiling
440 : Input : uint32_t deviceId: real device ID
441 : Input : pid_t hostPid: host pid
442 : Output : NA
443 : Return Value : PROFILINE_SUCCESS - init success
444 : others - init failed
445 : *****************************************************************************/
446 3 : int32_t ProfilingAdp::InitAicpuProfiling(const uint32_t deviceId, const pid_t hostPid, const uint32_t channelId)
447 : {
448 3 : InitAicpuProfilingDataInfo(deviceId, hostPid, channelId);
449 3 : ProfSoManager::GetInstance()->LoadSo();
450 3 : return ProfilingModeOpenProcess();
451 : }
452 :
453 : /*****************************************************************************
454 : Description : it is used to initialize the ProfilingAdp object and
455 : register instance to profiling
456 : Input : uint32_t deviceId: real device ID
457 : Input : pid_t hostPid: host pid
458 : Output : NA
459 : Return Value : NA
460 : *****************************************************************************/
461 3 : void ProfilingAdp::InitAicpuProfilingDataInfo(const uint32_t deviceId, const pid_t hostPid, const uint32_t channelId)
462 : {
463 3 : deviceId_ = deviceId;
464 3 : hostPid_ = hostPid;
465 3 : channelId_ = channelId;
466 3 : }
467 :
468 1 : void ProfilingAdp::SetAicpuProfilingFlagForKFC(const uint32_t flag)
469 : {
470 1 : profilingFlag_ = flag;
471 1 : }
472 :
473 4 : int32_t AicpuStartCallback()
474 : {
475 4 : AICPU_RUN_INFO("call AicpuStartCallback.");
476 4 : g_profAdditionalFlag = true;
477 4 : return 0;
478 : }
479 :
480 9 : int32_t ProfilingAdp::ProfilingModeOpenProcess()
481 : {
482 9 : int32_t retInit = 0;
483 9 : if (reportCallback_ != nullptr) {
484 1 : retInit = reportCallback_(MSPROF_MODULE_DATA_PREPROCESS, MSPROF_REPORTER_INIT, nullptr, 0U);
485 : } else {
486 8 : AICPU_LOG_INFO("call AdprofAicpuStartRegister. flag[%x], channelId[%u]", profilingFlag_, channelId_);
487 8 : AicpuStartPara regPara = {};
488 8 : regPara.devId = deviceId_;
489 8 : regPara.hostPid = static_cast<uint32_t>(hostPid_);
490 8 : regPara.channelId = channelId_;
491 8 : regPara.profConfig = profilingFlag_;
492 8 : if (isProfApiSo_ == true) {
493 0 : retInit = MsprofInitFunc(MSPROF_CTRL_INIT_AICPU, ®Para, sizeof(AicpuStartPara));
494 : } else {
495 8 : retInit = AdprofAicpuStartRegisterFunc(AicpuStartCallback, ®Para);
496 : }
497 : }
498 9 : return retInit;
499 : }
500 :
501 : /*****************************************************************************
502 : Description : it is used to initialize update mode
503 : Input : bool mode: true:open false:close
504 : Output : NA
505 : Return Value : NA
506 : *****************************************************************************/
507 20 : void ProfilingAdp::UpdateModeProcess(const bool mode)
508 : {
509 : (void)mode;
510 20 : initFlag_ = true;
511 20 : return;
512 : }
513 :
514 : /*****************************************************************************
515 : Description : it is used to new memory to store data
516 : Input : int32_t sendSize : the size of data
517 : const string& sendData : the source data
518 : ReporterData& reportData : the data which need to send
519 : Output : NA
520 : Return Value : if it is true,it means success to new memory.
521 : *****************************************************************************/
522 3 : bool ProfilingAdp::NewMemoryToStoreData(int32_t sendSize,
523 : const std::string &sendData,
524 : ReporterData &reportData) const //lint !e1072
525 : {
526 3 : const int32_t newLen = sendSize + 1;
527 3 : AICPU_RCHECK(newLen > 0, false, "Malloc size must be greater than zero, length is %d", newLen);
528 : uint8_t * const rdata = static_cast<uint8_t *>(
529 3 : malloc(sizeof(uint8_t) * static_cast<size_t>(newLen)));
530 3 : AICPU_RCHECK(rdata != nullptr, false, "Memory for new profiling data malloc failed");
531 : // met Exceptions, no need to checks for security functions
532 3 : auto ret = memset_s(rdata, static_cast<size_t>(newLen), 0, static_cast<size_t>(newLen));
533 3 : if (ret != EOK) {
534 1 : AICPU_LOG_ERROR("Set buffer failed, ret[%d]", ret);
535 1 : free(rdata);
536 1 : return false;
537 : }
538 2 : ret = memcpy_s(rdata, static_cast<size_t>(newLen),
539 2 : sendData.c_str(), static_cast<size_t>(sendSize));
540 2 : if (ret != EOK) {
541 0 : AICPU_LOG_ERROR("Copy send data to buffer failed, ret[%d]", ret);
542 0 : free(rdata);
543 0 : return false;
544 : }
545 :
546 2 : reportData.data = rdata;
547 :
548 2 : return true;
549 : }
550 :
551 : /*****************************************************************************
552 : Description : it is used to create the content
553 : Input : const string& sendData : the source data
554 : const string& mark : the mark of dataset
555 : char_t buffer[] : it is used to store data
556 : int32_t bufferlen : the size of buffer
557 : bool& newflag : if it is true,it means new memory to store data
558 : ReporterData& reportData : it is struct of profiling data package
559 : Output : NA
560 : Return Value : if it is true,it means success to create the content
561 : *****************************************************************************/
562 7 : bool ProfilingAdp::BuildSendContent(const std::string &sendData, const std::string &mark,
563 : char_t buffer[], const int32_t bufferlen,
564 : bool &newflag, ReporterData &reportData) const
565 : {
566 7 : const int32_t sendSize = static_cast<int32_t>(sendData.size());
567 7 : reportData.deviceId = static_cast<int32_t>(deviceId_);
568 7 : reportData.dataLen = static_cast<size_t>(sendSize) + static_cast<size_t>(1LU);
569 :
570 7 : if (sendSize < bufferlen) {
571 6 : errno_t err = EOK;
572 6 : err = memcpy_s(buffer, static_cast<size_t>(bufferlen), sendData.c_str(), static_cast<size_t>(sendSize));
573 6 : AICPU_RCHECK(err == EOK, false, "Copy send data to buffer failed");
574 6 : reportData.data = PtrToPtr<char_t, uint8_t>(buffer);
575 6 : newflag = false;
576 : } else {
577 1 : AICPU_RCHECK(NewMemoryToStoreData(sendSize, sendData, reportData), false,
578 : "Malloc new memory for send data failed");
579 1 : newflag = true;
580 : }
581 7 : reportData.data[sendSize] = static_cast<uint8_t>('\0');
582 7 : const errno_t retCpy = strncpy_s(reportData.tag, static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN + 1),
583 : mark.c_str(), static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN));
584 7 : if (retCpy != EOK) {
585 1 : AICPU_LOG_WARN("Copying reportData tag was not successful, retCpy=[%d].", retCpy);
586 : }
587 :
588 7 : reportData.tag[MSPROF_ENGINE_MAX_TAG_LEN] = '\0';
589 7 : return true;
590 : }
591 :
592 : /*****************************************************************************
593 : Description : it is used to create the content for AICPU
594 : Input : const ProfData& sendData : the source data
595 : const string& mark : the mark of dataset
596 : ReporterData& reportData : it is struct of profiling data package
597 : Output : NA
598 : Return Value : NA
599 : *****************************************************************************/
600 : template <typename T>
601 4 : void ProfilingAdp::BuildProfData(const T &sendData, const std::string &mark,
602 : ReporterData &reportData) const
603 : {
604 4 : reportData.deviceId = static_cast<int32_t>(deviceId_);
605 4 : reportData.dataLen = sizeof(sendData);
606 4 : reportData.data = reinterpret_cast<uint8_t *>(const_cast<T *>(&sendData));
607 4 : const errno_t retCpy = strncpy_s(reportData.tag, static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN + 1),
608 : mark.c_str(), static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN));
609 4 : if (retCpy != EOK) {
610 1 : AICPU_LOG_WARN("Copying reportData tag was not successful, retCpy=[%d].", retCpy);
611 : }
612 :
613 4 : reportData.tag[MSPROF_ENGINE_MAX_TAG_LEN] = '\0';
614 4 : }
615 :
616 1 : void ProfilingAdp::BuildProfAicpuAdditionalData(const MsprofAicpuProfData &sendData,
617 : MsprofAdditionalInfo &reportData) const
618 : {
619 1 : reportData.level = MSPROF_REPORT_AICPU_LEVEL;
620 1 : reportData.type = MSPROF_REPORT_AICPU_NODE_TYPE;
621 1 : reportData.threadId = sendData.threadId;
622 1 : reportData.timeStamp = GetSystemTick();
623 :
624 1 : auto data = reinterpret_cast<MsprofAicpuNodeAdditionalData *>(reportData.data);
625 1 : data->streamId = sendData.streamId;
626 1 : data->taskId = sendData.taskId;
627 1 : data->runStartTime = sendData.runStartTime;
628 1 : data->runStartTick = sendData.runStartTick;
629 1 : data->computeStartTime = sendData.computeStartTime;
630 1 : data->memcpyStartTime = sendData.memcpyStartTime;
631 1 : data->memcpyEndTime = sendData.memcpyEndTime;
632 1 : data->runEndTime = sendData.runEndTime;
633 1 : data->runEndTick = sendData.runEndTick;
634 1 : data->threadId = sendData.threadId;
635 1 : data->deviceId = sendData.deviceId;
636 1 : data->submitTick = sendData.submitTick;
637 1 : data->scheduleTick = sendData.scheduleTick;
638 1 : data->tickBeforeRun = sendData.tickBeforeRun;
639 1 : data->tickAfterRun = sendData.tickAfterRun;
640 1 : data->kernelType = sendData.kernelType;
641 1 : data->dispatchTime = sendData.dispatchTime;
642 1 : data->totalTime = sendData.totalTime;
643 1 : data->fftsThreadId = sendData.fftsThreadId;
644 1 : data->version = sendData.version;
645 :
646 1 : reportData.dataLen = sizeof(MsprofAicpuNodeAdditionalData);
647 1 : AICPU_LOG_INFO("aicpu prof message details: streamId[%hu], taskId[%hu], runStartTime[%llu], runStartTick[%llu], "
648 : "computeStartTime[%llu], memcpyStartTime[%llu], memcpyEndTime[%llu], runEndTime[%llu], "
649 : "runEndTick[%llu], threadId[%u], deviceId[%u], submitTick[%llu], scheduleTick[%llu], "
650 : "tickBeforeRun[%llu], tickAfterRun[%llu], kernelType[%u], dispatchTime[%u]us, totalTime[%u]us, "
651 : "fftsThreadId[%u], version[%u], dataLen[%u].", data->streamId, data->taskId, data->runStartTime,
652 : data->runStartTick, data->computeStartTime, data->memcpyStartTime, data->memcpyEndTime,
653 : data->runEndTime, data->runEndTick, data->threadId, data->deviceId, data->submitTick,
654 : data->scheduleTick, data->tickBeforeRun, data->tickAfterRun, data->kernelType, data->dispatchTime,
655 : data->totalTime, data->fftsThreadId, data->version, reportData.dataLen);
656 1 : }
657 :
658 2 : void ProfilingAdp::BuildProfDpAdditionalData(const MsprofDpProfData &sendData, MsprofAdditionalInfo &reportData) const
659 : {
660 2 : reportData.level = MSPROF_REPORT_AICPU_LEVEL;
661 2 : reportData.type = MSPROF_REPORT_AICPU_DP_TYPE;
662 2 : reportData.threadId = 0xffffffff;
663 2 : reportData.timeStamp = GetSystemTick();
664 :
665 2 : auto data = reinterpret_cast<MsprofAicpuDpAdditionalData *>(reportData.data);
666 4 : errno_t retCpy = strncpy_s(data->action, static_cast<size_t>(MSPROF_DP_DATA_ACTION_LEN),
667 2 : sendData.action, static_cast<size_t>(MSPROF_DP_DATA_ACTION_LEN));
668 2 : if (retCpy != EOK) {
669 1 : AICPU_LOG_WARN("Copying sendData.action tag was not successful, retCpy=[%d].", retCpy);
670 : }
671 4 : retCpy = strncpy_s(data->source, static_cast<size_t>(MSPROF_DP_DATA_SOURCE_LEN),
672 2 : sendData.source, static_cast<size_t>(MSPROF_DP_DATA_SOURCE_LEN));
673 2 : if (retCpy != EOK) {
674 1 : AICPU_LOG_WARN("Copying sendData.source tag was not successful, retCpy=[%d].", retCpy);
675 : }
676 2 : data->index = sendData.index;
677 2 : data->size = sendData.size;
678 :
679 2 : reportData.dataLen = sizeof(MsprofAicpuDpAdditionalData);
680 2 : AICPU_LOG_INFO("dp prof message details: action[%s], source[%s], index[%llu], size[%llu], dataLen[%u].",
681 : data->action, data->source, data->index, data->size, reportData.dataLen);
682 2 : }
683 :
684 1 : void ProfilingAdp::BuildProfMiAdditionalData(const std::string &sendData, MsprofAdditionalInfo &reportData) const
685 : {
686 1 : reportData.level = MSPROF_REPORT_AICPU_LEVEL;
687 1 : reportData.type = MSPROF_REPORT_AICPU_MI_TYPE;
688 1 : reportData.threadId = 0xffffffff;
689 1 : reportData.timeStamp = GetSystemTick();
690 :
691 1 : std::vector<std::string> data;
692 1 : std::string tmpSendData = sendData + ",";
693 1 : auto posComma = tmpSendData.find(",");
694 :
695 5 : while (posComma != tmpSendData.npos) {
696 4 : std::string temp = tmpSendData.substr(0, posComma);
697 4 : auto posColon = temp.find(":");
698 4 : data.push_back(temp.substr(posColon + 1, temp.size() - posColon));
699 4 : tmpSendData = tmpSendData.substr(posComma + 1, tmpSendData.size());
700 4 : posComma = tmpSendData.find(",");
701 4 : }
702 1 : if (data.size() != TOTAL_LEN) {
703 0 : AICPU_LOG_ERROR("Data format is not as expected.");
704 0 : return;
705 : }
706 :
707 1 : auto repData = reinterpret_cast<MsprofAicpuMiAdditionalData *>(reportData.data);
708 1 : repData->nodeTag = GET_NEXT_DEQUEUE_WAIT;
709 : try {
710 1 : repData->queueSize = stoul(data[QUEUE_SIZE]);
711 1 : repData->runStartTime = stoul(data[RUN_START_TIME]);
712 1 : repData->runEndTime = stoul(data[RUN_END_TIME]);
713 0 : } catch(...) {
714 0 : AICPU_LOG_ERROR("mindspore prof message details: %s", sendData.c_str());
715 0 : }
716 :
717 1 : reportData.dataLen = sizeof(MsprofAicpuMiAdditionalData);
718 1 : AICPU_LOG_INFO("mindspore prof message details: node[%u], queueSize[%llu], runStartTime[%llu], "
719 : "runEndTime[%llu], dataLen[%u].", repData->nodeTag, repData->queueSize,
720 : repData->runStartTime, repData->runEndTime, reportData.dataLen);
721 1 : }
722 :
723 2 : int32_t ProfilingAdp::SendProfDataWithNewChannel(const char_t * const sendData, std::string mark)
724 : {
725 2 : MsprofAdditionalInfo additionalReportData;
726 2 : if (mark == "AICPU") {
727 1 : BuildProfAicpuAdditionalData(*PtrToPtr<const char_t, const MsprofAicpuProfData>(sendData),
728 : additionalReportData);
729 1 : } else if (mark == "DP") {
730 1 : BuildProfDpAdditionalData(*PtrToPtr<const char_t, const MsprofDpProfData>(sendData), additionalReportData);
731 : } else {
732 0 : BuildProfMiAdditionalData(*PtrToPtr<const char_t, const std::string>(sendData), additionalReportData);
733 : }
734 2 : int32_t ret = SendAdditionalProcess(additionalReportData);
735 2 : return ret;
736 : }
737 :
738 10 : int32_t ProfilingAdp::SendProfDataWithOldChannel(const char_t * const sendData, std::string mark)
739 : {
740 : ReporterData reportData;
741 10 : bool needNewFlag = false;
742 10 : if (mark == "AICPU") {
743 2 : BuildProfData<MsprofAicpuProfData>(*PtrToPtr<const char_t, const MsprofAicpuProfData>(sendData), mark,
744 : reportData);
745 8 : } else if (mark == "DP") {
746 1 : BuildProfData<MsprofDpProfData>(*PtrToPtr<const char_t, const MsprofDpProfData>(sendData), mark,
747 : reportData);
748 : } else {
749 7 : AICPU_RCHECK((PtrToPtr<const char_t, const std::string>(sendData))->size() > 0U,
750 : static_cast<int32_t>(ProfStatusCode::PROFILINE_FAILED), "Check send size failed");
751 6 : char_t buffer[MAX_INNER_SEND_DATA_LEN] = {}; //lint !e813
752 6 : const bool ret = BuildSendContent(*PtrToPtr<const char_t, const std::string>(sendData),
753 : mark, &buffer[0], MAX_INNER_SEND_DATA_LEN, needNewFlag, reportData);
754 6 : AICPU_RCHECK(ret, static_cast<int32_t>(ProfStatusCode::PROFILINE_FAILED), "Build send content failed.");
755 : }
756 9 : int32_t ret = SendProcess(reportData);
757 9 : if (needNewFlag && (reportData.data != nullptr)) {
758 : // Newly created data memory needs to be released
759 1 : free(reportData.data);
760 1 : reportData.data = nullptr;
761 : }
762 9 : return ret;
763 : }
764 :
765 : /*****************************************************************************
766 : Description : it is used to provide interface to DP module which can send data to profiling
767 : Input : std::const string& sendData : the data which it is need send to profiling
768 : std::string mark : the mark of dataset
769 : Output : NA
770 : Return Value : PROFILINE_SUCCESS - send success
771 : others - end failed
772 : *****************************************************************************/
773 12 : int32_t ProfilingAdp::Send(const char_t * const sendData, std::string mark)
774 : {
775 12 : if (!GetReportValid()) {
776 0 : AICPU_LOG_WARN("Report invalid.");
777 0 : return static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS);
778 : }
779 :
780 12 : if (mark.empty()) {
781 1 : AICPU_LOG_WARN("No mark was set, use 'DEFAULT' as tag");
782 1 : mark = "DEFAULT";
783 : }
784 12 : int32_t ret = 0;
785 12 : if ((g_profAdditionalFlag && reportCallback_ == nullptr) || (isProfApiSo_ == true)) {
786 2 : ret = SendProfDataWithNewChannel(sendData, mark);
787 : } else {
788 10 : ret = SendProfDataWithOldChannel(sendData, mark);
789 : }
790 :
791 12 : if (ret == static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS) && mark == "AICPU") {
792 2 : counter.AicpuMesCountInc();
793 10 : } else if (ret == static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS) && mark == "DP") {
794 1 : counter.DpMesCountInc();
795 9 : } else if (ret == static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS)) {
796 3 : counter.MiMesCountInc();
797 : }
798 12 : return ret;
799 : }
800 :
801 2 : int32_t ProfModelMessage::SendProfModelMessageWithNewChannel()
802 : {
803 2 : MsprofAdditionalInfo additionalReportData;
804 2 : BuildProfModelAdditionalData(additionalReportData);
805 2 : int32_t ret = ProfilingAdp::GetInstance().SendAdditionalProcess(additionalReportData);
806 2 : return ret;
807 : }
808 :
809 2 : int32_t ProfModelMessage::SendProfModelMessageWithOldChannel()
810 : {
811 : ReporterData reportData;
812 2 : reportData.deviceId = static_cast<int32_t>(deviceId_);
813 2 : reportData.dataLen = sizeof(sendData_);
814 2 : reportData.data = PtrToPtr<void, uint8_t>(static_cast<void *>(&sendData_));
815 2 : AICPU_LOG_DEBUG("Report model profiling message, dev_id=%u, dataLen=%zu, timeStamp=%llu, tag_id=%u, "
816 : "index_id=%llu, model_id=%u, event_id=%llu, dataTag=%u.", deviceId_, reportData.dataLen,
817 : sendData_.aicpuModelProfData.timeStamp,
818 : static_cast<uint32_t>(sendData_.aicpuModelProfData.tagId),
819 : sendData_.aicpuModelProfData.indexId,
820 : sendData_.aicpuModelProfData.modelId,
821 : sendData_.aicpuModelProfData.eventId,
822 : static_cast<uint32_t>(sendData_.aicpuModelProfData.dataTag));
823 2 : const std::string tagStr(tag_);
824 2 : const errno_t retCpy = strncpy_s(reportData.tag, static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN + 1),
825 : tagStr.c_str(), static_cast<size_t>(MSPROF_ENGINE_MAX_TAG_LEN));
826 2 : if (retCpy != EOK) {
827 1 : AICPU_LOG_WARN("Copying reportData tag was not successful, retCpy=[%d].", retCpy);
828 : }
829 2 : reportData.tag[MSPROF_ENGINE_MAX_TAG_LEN] = '\0';
830 2 : int32_t ret = ProfilingAdp::GetInstance().SendProcess(reportData);
831 2 : return ret;
832 2 : }
833 :
834 4 : int32_t ProfModelMessage::ProfModelMessage::ReportProfModelMessage()
835 : {
836 4 : int32_t ret = 0;
837 4 : if ((g_profAdditionalFlag && ProfilingAdp::GetInstance().GetMsprofReporterCallback() == nullptr) || (ProfilingAdp::GetInstance().IsProfApiSo() == true)) {
838 2 : ret = SendProfModelMessageWithNewChannel();
839 : } else {
840 2 : ret = SendProfModelMessageWithOldChannel();
841 : }
842 :
843 4 : if (ret == static_cast<int32_t>(ProfStatusCode::PROFILINE_SUCCESS)) {
844 4 : ProfilingAdp::GetInstance().counter.ModelMesCountInc();
845 : }
846 4 : return ret;
847 : }
848 :
849 2 : void ProfModelMessage::BuildProfModelAdditionalData(MsprofAdditionalInfo &reportData)
850 : {
851 2 : reportData.level = MSPROF_REPORT_AICPU_LEVEL;
852 2 : reportData.type = MSPROF_REPORT_AICPU_MODEL_TYPE;
853 2 : reportData.threadId = 0xffffffff;
854 2 : reportData.timeStamp = sendData_.aicpuModelProfData.timeStamp;
855 :
856 2 : auto data = reinterpret_cast<MsprofAicpuModelAdditionalData *>(reportData.data);
857 2 : data->indexId = sendData_.aicpuModelProfData.indexId;
858 2 : data->modelId = sendData_.aicpuModelProfData.modelId;
859 2 : data->tagId = sendData_.aicpuModelProfData.tagId;
860 2 : data->eventId = sendData_.aicpuModelProfData.eventId;
861 :
862 2 : reportData.dataLen = sizeof(MsprofAicpuModelAdditionalData);
863 2 : AICPU_LOG_INFO("Model prof message details: indexId[%llu], modelId[%u], tagId[%hu], eventId[%llu], dataLen[%u].",
864 : data->indexId, data->modelId, data->tagId, data->eventId, reportData.dataLen);
865 2 : }
866 : } // namespace aicpu
|