LCOV - code coverage report
Current view: top level - legacy/ascend910/common/debug/profiling/inc - profiling_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.9 % 87 73
Test Date: 2026-08-18 17:47:01 Functions: 81.2 % 16 13

            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              : #ifndef COMMON_PROFILING_PROFILING_MANAGER_H
      12              : #define COMMON_PROFILING_PROFILING_MANAGER_H
      13              : 
      14              : #include "rt_external.h"
      15              : #include "adapter_prof.h"
      16              : #include "task_profiling_pub.h"
      17              : #include "profiler_base_pub.h"
      18              : #include "externalinput_pub.h"
      19              : 
      20              : #include "profiling_manager_pub.h"
      21              : #include "aprof_pub.h"
      22              : #include "dispatcher.h"
      23              : 
      24              : #include <string>
      25              : #include <cstdio>
      26              : #include <atomic>
      27              : 
      28              : namespace hccl {
      29              : using Prof_Status = uint32_t;
      30              : const Prof_Status SUCCESS = 0x0;
      31              : const Prof_Status FAILED = 0xFFFFFFFF;
      32              : 
      33              : struct EsLoopUpPara {
      34              :     s32 tag;
      35              :     void* srcAddr;
      36              :     void* dstAddr;
      37              :     u32 dataSize;
      38              : };
      39              : 
      40              : struct EsUpdatePara {
      41              :     s32 tag;
      42              :     char groupName[GROUP_NAME_MAX_LEN] = {0};
      43              :     void* srcAddr;
      44              :     void* dstAddr;
      45              :     u32 dataSize;
      46              : };
      47              : 
      48              : class ProfilingManager {
      49              : public:
      50              :     static constexpr u32 aging = 1;
      51              : 
      52              :     ProfilingManager();
      53              :     virtual ~ProfilingManager();
      54              :     static ProfilingManager& Instance();
      55              :     HcclResult CallMsprofRegFftsLaunch() const;
      56              :     HcclResult CallMsprofRegHcclOpApi() const;
      57              :     HcclResult CallMsprofRegHostApi() const;
      58              :     HcclResult RegEsTaskType(ProfTaskType taskType) const;
      59              :     HcclResult CallMsprofRegEsTaskTypeApi() const;
      60              :     HcclResult CallMsprofRegTaskTypeApi() const;
      61              :     HcclResult CallMsprofReportHostApi(
      62              :         HcclCMDType cmdType, uint64_t beginTime, u64 count, HcclDataType dataType, AlgType algType, uint64_t groupName,
      63              :         u32 numBlocks = 0) const;
      64              :     HcclResult
      65              :     CallMsprofReportHostHcclOpApi(uint64_t beginTime, uint64_t endTime, uint64_t itemId, uint32_t threadId) const;
      66              :     HcclResult CallMsprofReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType) const;
      67              :     HcclResult ReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType, uint32_t agingFlag) const;
      68              :     HcclResult CallEsMsprofReportTaskApi(bool isMainStrem, uint64_t beginTime, ProfTaskType taskType) const;
      69              :     HcclResult CallMsprofReportAdditionInfo(uint32_t type, uint64_t timeStamp, const void* data, int len) const;
      70              :     HcclResult CallMsprofReportMultiThreadInfo(const std::vector<uint32_t>& tidInfo) const;
      71              :     HcclResult CallMsprofReportContextIdInfo(u32 ctxIdMax) const;
      72              :     HcclResult CallMsprofReportHostAclApi(
      73              :         uint32_t type, uint64_t beginTime, uint64_t endTime, uint64_t itemId, uint32_t threadId) const;
      74              :     HcclResult
      75              :     CallMsprofReportHostNodeApi(uint64_t beginTime, uint64_t endTime, uint64_t itemId, uint32_t threadId) const;
      76              :     HcclResult
      77              :     CallMsprofReportHostNodeBasicInfo(uint64_t timeStamp, uint64_t itemId, uint32_t threadId, u32 numBlocks = 0) const;
      78              :     HcclResult CallMsprofReportHostHcclOpInfo(
      79              :         uint64_t timeStamp, uint32_t threadId, u64 count, HcclDataType dataType, std::string& algTypeStr,
      80              :         uint64_t groupName) const;
      81              :     HcclResult CallMsprofReportAdditionInfoForEsLookup(EsLoopUpPara& para, ProfTaskType type);
      82              :     HcclResult CallMsprofReportAdditionInfoForEsUpdate(const EsUpdatePara& para, ProfTaskType type);
      83              :     HcclResult
      84              :     ReportAdditionInfo(uint32_t type, uint64_t timeStamp, const void* data, int len, uint32_t agingFlag) const;
      85              :     HcclResult CallMsprofReportMc2CommInfo(uint64_t timeStamp, const void* data, int len);
      86              :     HcclResult CallMsprofReportEsAdditionInfo(uint32_t type, uint64_t timeStamp, const void* data, int len) const;
      87              : 
      88              :     Prof_Status CallMsprofReport(ReporterData& reporterData) const;
      89              :     Prof_Status GetHashKey(MsprofHashData& data) const;
      90              :     Prof_Status PluginInit() const;
      91              :     Prof_Status PluginUnInit() const;
      92              :     HcclResult ReportStoragedTaskApi();
      93              :     HcclResult ReportStoragedAdditionInfo();
      94              :     HcclResult ReportStoragedCompactInfo();
      95              :     HcclResult ClearStoragedProfilingInfo();
      96              :     HcclResult ReportStoragedFftsInfo();
      97              :     HcclResult ReportStoragedOpApi();
      98              :     HcclResult
      99              :     CallMsprofReportNodeInfo(uint64_t beginTime, uint64_t endTime, const std::string profName, uint32_t threadId);
     100            1 :     void SetMsprofReporterCallback(MsprofReporterCallback func)
     101              :     {
     102            1 :         reporterCallback_ = func;
     103            1 :         HCCL_INFO("[Check][Param]SetMsprofReporterCallback.");
     104            1 :     }
     105              : 
     106            2 :     void StartFftsLaunchSubscribe()
     107              :     {
     108            2 :         isFftsLaunchSubscribe_ = true;
     109            2 :         CallMsprofRegFftsLaunch();
     110            2 :         HCCL_RUN_INFO("StartFftsLaunchSubscribe:[%d]", isFftsLaunchSubscribe_);
     111            2 :     }
     112              : 
     113            2 :     void StartHostHcclOpSubscribe()
     114              :     {
     115            2 :         isHostHcclOpSubscribe_ = true;
     116            2 :         HCCL_RUN_INFO("StartHostHcclOpSubscribe:[%d]", isHostHcclOpSubscribe_);
     117            2 :     }
     118              : 
     119            0 :     void StartHostApiSubscribe()
     120              :     {
     121            0 :         isHostApiSubscribe_ = true;
     122            0 :         CallMsprofRegHostApi();
     123            0 :         ReportStoragedCompactInfo();
     124            0 :         HCCL_RUN_INFO("SetHostApiSubscribe:[%d]", isHostApiSubscribe_);
     125            0 :     }
     126              : 
     127            2 :     void StartTaskApiSubscribe()
     128              :     {
     129            2 :         isTaskApiSubscribe_ = true;
     130            2 :         CallMsprofRegTaskTypeApi();
     131            2 :         HCCL_RUN_INFO("SetTaskApiSubscribe:[%d]", isTaskApiSubscribe_);
     132            2 :     }
     133              : 
     134            2 :     void StartAdditionInfoSubscribe()
     135              :     {
     136            2 :         isAdditionInfoSubscribe_ = true;
     137            2 :         ReportStoragedAdditionInfo();
     138            2 :         HCCL_RUN_INFO("StartAdditionInfoSubscribe:[%d]", isAdditionInfoSubscribe_);
     139            2 :     }
     140              : 
     141            2 :     void StartSubscribe(uint64_t profconfig)
     142              :     {
     143              :         // profconfig同步到platform
     144            2 :         SetProfConfig(profconfig);
     145              :         // HostApi粒度的打点控制
     146            2 :         if ((profconfig & PROF_ACL_API_MASK) != 0) {
     147            0 :             StartHostApiSubscribe();
     148              :         }
     149              : 
     150              :         // aicpu模式下 开启L0就上报task打点; 其他场景开启L1才上报
     151            2 :         if ((GetExternalInputHcclAicpuUnfold() && (profconfig & PROF_TASK_TIME_MASK) != 0)
     152            2 :             || ((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
     153            2 :             StartTaskApiSubscribe();
     154              :         }
     155              : 
     156              :         // 集合通信算子粒度的打点 只有L0打开的时候才上报 L1打开的时候不上报; AICPU也不上报算子粒度的打点
     157            2 :         if (((profconfig & PROF_TASK_TIME_MASK) != 0) && ((profconfig & PROF_TASK_TIME_L1_MASK) == 0)) {
     158            2 :             StartHostHcclOpSubscribe();
     159              :         }
     160              : 
     161            2 :         if (GetExternalInputHcclEnableFfts()) {
     162              :             // FFTS打开的时候 L0和L1都上报FFTSLauch和contextID
     163            2 :             if (((profconfig & PROF_TASK_TIME_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
     164            2 :                 StartFftsLaunchSubscribe();
     165              :             }
     166              :         }
     167              :         // L1打开时, 上报task粒度的打点和子task的详细信息
     168            2 :         if (((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
     169            2 :             StartAdditionInfoSubscribe();
     170              :         } else {
     171            0 :             HCCL_RUN_INFO("[Profiling][CommandHandle] profSwitch is[%llu]", profconfig);
     172              :         }
     173            2 :     }
     174              : 
     175            2 :     void EsStartTaskApiSubscribe()
     176              :     {
     177            2 :         isTaskApiSubscribe_ = true;
     178            2 :         CallMsprofRegEsTaskTypeApi();
     179            2 :         HCCL_INFO("EsStartTaskApiSubscribe:[%d]", isTaskApiSubscribe_);
     180            2 :     }
     181              : 
     182            0 :     void EsStartAdditionInfoSubscribe()
     183              :     {
     184            0 :         isAdditionInfoSubscribe_ = true;
     185            0 :         HCCL_INFO("[Check][Param]EsStartAdditionInfoSubscribe.");
     186            0 :     }
     187              : 
     188            2 :     void EsStartSubscribe(uint64_t profconfig)
     189              :     {
     190            2 :         HCCL_INFO("[Profiling][CommandHandle] EsStartSubscribe profSwitch is[%llu]", profconfig);
     191              :         // profconfig同步到platform
     192            2 :         SetProfConfig(profconfig);
     193            2 :         if (((profconfig & PROF_TASK_TIME_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
     194            2 :             EsStartTaskApiSubscribe();
     195              :         }
     196              : 
     197            2 :         if (((profconfig & PROF_TASK_TIME_L1_MASK) != 0) || ((profconfig & PROF_HCCL_TRACE_MASK) != 0)) {
     198            0 :             EsStartAdditionInfoSubscribe();
     199              :         }
     200              : 
     201            2 :         HCCL_INFO("EsStartSubscribe");
     202            2 :     }
     203              : 
     204            3 :     void StopSubscribe(uint64_t profconfig)
     205              :     {
     206              :         // profconfig同步到platform
     207            3 :         SetProfConfig(profconfig);
     208            3 :         CallMsprofRegHcclOpApi();
     209            3 :         ReportStoragedOpApi();
     210            3 :         ReportStoragedTaskApi();
     211            3 :         isHostApiSubscribe_ = false;
     212            3 :         isHostHcclOpSubscribe_ = false;
     213            3 :         isTaskApiSubscribe_ = false;
     214            3 :         isAdditionInfoSubscribe_ = false;
     215            3 :         isFftsLaunchSubscribe_ = false;
     216            3 :         HCCL_RUN_INFO("[ProfilingManage]StopSubscribe.");
     217            3 :     }
     218              : 
     219            2 :     void EsStopSubscribe(uint64_t profconfig)
     220              :     {
     221              :         // profconfig同步到platform
     222            2 :         SetProfConfig(profconfig);
     223            2 :         isTaskApiSubscribe_ = false;
     224            2 :         isAdditionInfoSubscribe_ = false;
     225            2 :         HCCL_INFO("[Check][Param]EsStopSubscribe.");
     226            2 :     }
     227              : 
     228            0 :     bool GetFftsLaunchApiState() const { return isFftsLaunchSubscribe_; }
     229           75 :     bool GetAdditionInfoState() const { return isAdditionInfoSubscribe_; }
     230           26 :     bool GetTaskApiState() { return isTaskApiSubscribe_; }
     231          552 :     bool GetAllState()
     232              :     {
     233          552 :         return !isHostApiSubscribe_ && !isTaskApiSubscribe_ && !isAdditionInfoSubscribe_ && !isHostHcclOpSubscribe_
     234         1104 :                && !isFftsLaunchSubscribe_;
     235              :     }
     236              :     void SetFftsDispatcherMode();
     237              :     void ReSetFftsDispatcherMode();
     238              :     static void SetThreadCaptureStatus(s32 threadID, bool isCapture);
     239              :     static bool GetThreadCaptureStatus();
     240              :     static void DeleteThreadCaptureStatus(s32 threadID);
     241              : 
     242              : private:
     243              :     MsprofReporterCallback reporterCallback_;
     244              :     bool isHostApiSubscribe_ = false;
     245              :     bool isTaskApiSubscribe_ = false;
     246              :     bool isAdditionInfoSubscribe_ = false;
     247              :     bool isHostHcclOpSubscribe_ = false;
     248              :     bool isFftsLaunchSubscribe_ = false;
     249              :     static std::queue<MsprofApi> storageTaskApi_;
     250              :     static std::array<std::queue<MsprofAdditionalInfo>, MAX_MODULE_DEVICE_NUM> storageAdditionInfo_;
     251              :     static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportAddInfoMutex_;
     252              :     static std::array<std::queue<MsprofCompactInfo>, MAX_MODULE_DEVICE_NUM> storageCompactInfo_;
     253              :     static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportCompactInfoMutex_;
     254              :     static std::mutex reportDataQueueMutex_;
     255              :     static std::array<std::queue<MsprofAdditionalInfo>, MAX_MODULE_DEVICE_NUM> storageAdditionInfoFftsCapture_;
     256              :     static std::array<std::mutex, MAX_MODULE_DEVICE_NUM> reportAddInfoFftsCaptureMutex_;
     257              :     std::atomic<bool> isFftsDispatcher_{false};
     258              :     static std::unordered_map<s32, bool> captureStatusThreadIDMap_;
     259              :     static std::mutex captureStatusMapMutex_;
     260              :     static std::queue<MsprofApi> storageOpApi_;
     261              :     static std::mutex reportDataOpQueueMutex_;
     262              : };
     263              : } // namespace hccl
     264              : #endif // COMMON_PROFILING_PROFILING_MANAGER_H
        

Generated by: LCOV version 2.0-1