LCOV - code coverage report
Current view: top level - aicpu_prof - profiling_adp.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.4 % 368 340
Test Date: 2026-08-12 11:05:02 Functions: 86.3 % 51 44

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

Generated by: LCOV version 2.0-1