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 HCCL_PROFILING_HANDLER_LITE_H
12 : #define HCCL_PROFILING_HANDLER_LITE_H
13 :
14 : #include <vector>
15 : #include <unordered_map>
16 : #include "hccl/hccl_types.h"
17 : #include "prof_common.h"
18 : #include "aprof_pub.h"
19 : #include "task_info.h"
20 : #include "profiling_common.h"
21 :
22 : extern "C" {
23 : __attribute__((weak)) int32_t
24 : MsprofReportBatchAdditionalInfo(uint32_t nonPersistantFlag, const VOID_PTR data, uint32_t length);
25 : __attribute__((weak)) int32_t
26 : AdprofReportBatchAdditionalInfo(uint32_t nonPersistantFlag, const void* data, uint32_t length);
27 : __attribute__((weak)) int32_t AdprofReportAdditionalInfo(uint32_t agingFlag, const void* data, uint32_t length);
28 : __attribute__((weak)) int32_t
29 : MsprofReportAdditionalInfo(uint32_t nonPersistantFlag, const VOID_PTR data, uint32_t length);
30 : __attribute__((weak)) int32_t AdprofCheckFeatureIsOn(uint64_t feature);
31 : __attribute__((weak)) int32_t MsprofRegisterCallback(uint32_t moduleId, ProfCommandHandle handle);
32 : __attribute__((weak)) uint64_t AdprofGetHashId(const char* hashInfo, size_t length);
33 : __attribute__((weak)) uint64_t MsprofStr2Id(const char* hashInfo, size_t length);
34 : };
35 :
36 : namespace aicpu {
37 10 : MAKE_ENUM(status_t, AICPU_ERROR_NONE = 0, AICPU_ERROR_FAILED = 1)
38 : status_t __attribute__((weak)) GetTaskAndStreamId(uint64_t& taskId, uint32_t& streamId);
39 : } // namespace aicpu
40 :
41 : namespace Hccl {
42 19 : MAKE_ENUM(MainStreamTaskType, HEAD = 0, TAIL = 1)
43 :
44 45 : MAKE_ENUM(ProfilingLevel, L0, L1)
45 :
46 : struct FlagTaskInfo {
47 : uint32_t taskId;
48 : MainStreamTaskType type;
49 : };
50 :
51 : class ProfilingHandlerLite {
52 : public:
53 : ~ProfilingHandlerLite();
54 : ProfilingHandlerLite(const ProfilingHandlerLite& that) = delete;
55 : ProfilingHandlerLite& operator=(const ProfilingHandlerLite& that) = delete;
56 : static ProfilingHandlerLite& GetInstance();
57 : HcclResult Init();
58 : void ReportHcclOpInfo(const DfxOpInfo& opInfo) const;
59 : void ReportHcclTaskDetails(const std::vector<TaskInfo*>& taskInfo) const;
60 : void ReportMainStreamTask(const FlagTaskInfo& flagTaskInfo) const;
61 : void SetCachedCclTag();
62 : void SetCachedGroupName(const std::string& groupName, u32 rankSize);
63 : uint64_t GetCachedAlgTypeHashId() const { return cachedAlgTypeHashId_; }
64 : void UpdateProfSwitch();
65 : void SetProL0On(bool val);
66 : void SetProL1On(bool val);
67 19 : inline bool GetProfL0State() const { return enableHcclL0_; }
68 8 : inline bool GetProfL1State() const { return enableHcclL1_; }
69 : uint64_t GetProfHashId(const char* name, uint32_t len) const;
70 :
71 : private:
72 : explicit ProfilingHandlerLite();
73 : void ReportAdditionInfo(const MsprofAdditionalInfo& reporterData) const;
74 :
75 : bool IsProfOn(uint64_t feature) const;
76 : bool IsProfSwitchOn(ProfilingLevel level);
77 : bool IsL1fromOffToOn();
78 :
79 : void DumpTaskDetails(const MsprofAicpuHcclTaskInfo& taskDetailsInfos, const TaskInfo* taskInfo) const;
80 : void GetTaskDetailInfos(const TaskInfo* it, MsprofAicpuHcclTaskInfo& taskDetailsInfos) const;
81 : void FillTaskParamDetails(const TaskInfo* it, MsprofAicpuHcclTaskInfo& taskDetailsInfos) const;
82 : bool FillBatchReporterData(
83 : uint32_t batchId, const MsprofAicpuHcclTaskInfo* taskInfos, MsprofAdditionalInfo& addInfo) const;
84 : void ReportHcclTaskDetailsLog(const std::vector<TaskInfo*>& taskInfo) const;
85 : bool ReportBatchAddInfo(
86 : uint32_t batchId, const MsprofAicpuHcclTaskInfo* taskInfos, MsprofAdditionalInfo* addInfoVec,
87 : uint32_t& addInfoIndx, uint32_t maxBatchNum, bool isLastBatch) const;
88 :
89 : private:
90 : static ProfilingHandlerLite instance_;
91 : bool enableHcclL0_{false};
92 : bool enableHcclL1_{false};
93 : bool initializedFlag_{false};
94 : uint64_t cachedGroupName_{INVALID_U64};
95 : uint64_t cachedAlgTypeHashId_{0};
96 : u32 cachedRankSize_{0};
97 : uint32_t cachedTid_{0};
98 : std::unordered_map<uint32_t, uint64_t> taskTypeHashCache_;
99 : std::unordered_map<std::string, uint64_t> cachedCclTag_;
100 : using ReportAdditionalInfoHandle = int32_t (*)(uint32_t, const void*, uint32_t);
101 : ReportAdditionalInfoHandle reportAdditionalInfo_{nullptr};
102 : using ReportBatchAdditionalInfoHandle = int32_t (*)(uint32_t, const void*, uint32_t);
103 : ReportBatchAdditionalInfoHandle reportBatchAdditionalInfo_{nullptr};
104 : using GetProfHashIdHandle = uint64_t (*)(const char*, size_t);
105 : GetProfHashIdHandle getProfHashId_{nullptr};
106 : };
107 : } // namespace Hccl
108 :
109 : #endif // HCCL_PROFILING_HANDLER_LITE_H
|