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 "dlprof_function_v2.h"
12 : #include "log.h"
13 :
14 : namespace Hccl {
15 : #define UNUSED(x) (void)(x)
16 :
17 604 : DlProfFunction& DlProfFunction::GetInstance()
18 : {
19 604 : static DlProfFunction hcclDlProfFunction;
20 604 : return hcclDlProfFunction;
21 : }
22 :
23 1 : DlProfFunction::DlProfFunction() { DlProfFunctionStubInit(); }
24 :
25 1 : DlProfFunction::~DlProfFunction()
26 : {
27 1 : if (handle_ != nullptr) {
28 0 : (void)dlclose(handle_);
29 0 : handle_ = nullptr;
30 : }
31 1 : }
32 :
33 1 : static int32_t MsprofRegisterCallbackStub(uint32_t moduleId, ProfCommandHandle handle)
34 : {
35 : UNUSED(moduleId);
36 : UNUSED(handle);
37 3 : HCCL_WARNING("Entry MsprofRegisterCallbackStub");
38 1 : return 0;
39 : }
40 :
41 152 : static int32_t MsprofRegTypeInfoStub(uint16_t level, uint32_t typeId, const char* typeName)
42 : {
43 : UNUSED(level);
44 : UNUSED(typeId);
45 : UNUSED(typeName);
46 456 : HCCL_WARNING("Entry MsprofRegTypeInfoStub");
47 152 : return 0;
48 : }
49 :
50 15 : static int32_t MsprofReportApiStub(uint32_t agingFlag, const MsprofApi* api)
51 : {
52 : UNUSED(agingFlag);
53 : UNUSED(api);
54 45 : HCCL_WARNING("Entry MsprofReportApiStub");
55 15 : return 0;
56 : }
57 :
58 12 : static int32_t MsprofReportCompactInfoStub(uint32_t agingFlag, const void* data, uint32_t length)
59 : {
60 : UNUSED(agingFlag);
61 : UNUSED(data);
62 : UNUSED(length);
63 36 : HCCL_WARNING("Entry MsprofReportCompactInfoStub");
64 12 : return 0;
65 : }
66 :
67 11 : static int32_t MsprofReportAdditionalInfoStub(uint32_t agingFlag, const void* data, uint32_t length)
68 : {
69 : UNUSED(agingFlag);
70 : UNUSED(data);
71 : UNUSED(length);
72 33 : HCCL_WARNING("Entry MsprofReportAdditionalInfoStub");
73 11 : return 0;
74 : }
75 :
76 0 : static int32_t MsprofReportBatchAdditionalInfoStub(uint32_t agingFlag, const void* data, uint32_t length)
77 : {
78 : UNUSED(agingFlag);
79 : UNUSED(data);
80 : UNUSED(length);
81 0 : HCCL_WARNING("Entry MsprofReportBatchAdditionalInfoStub");
82 0 : return 0;
83 : }
84 :
85 178 : static uint64_t MsprofStr2IdStub(const char* hashInfo, uint32_t length)
86 : {
87 : UNUSED(hashInfo);
88 : UNUSED(length);
89 534 : HCCL_WARNING("Entry MsprofStr2IdStub");
90 178 : return 0;
91 : }
92 :
93 107 : static uint64_t MsprofSysCycleTimeStub()
94 : {
95 321 : HCCL_WARNING("Entry MsprofSysCycleTimeStub");
96 107 : return 0;
97 : }
98 :
99 1 : void DlProfFunction::DlProfFunctionStubInit()
100 : {
101 1 : dlMsprofRegisterCallback = static_cast<int32_t (*)(uint32_t, ProfCommandHandle)>(MsprofRegisterCallbackStub);
102 1 : dlMsprofRegTypeInfo = static_cast<int32_t (*)(uint16_t, uint32_t, const char*)>(MsprofRegTypeInfoStub);
103 1 : dlMsprofReportApi = static_cast<int32_t (*)(uint32_t, const MsprofApi*)>(MsprofReportApiStub);
104 1 : dlMsprofReportCompactInfo = static_cast<int32_t (*)(uint32_t, const void*, uint32_t)>(MsprofReportCompactInfoStub);
105 : dlMsprofReportAdditionalInfo
106 1 : = static_cast<int32_t (*)(uint32_t, const void*, uint32_t)>(MsprofReportAdditionalInfoStub);
107 : dlMsprofReportBatchAdditionalInfo
108 1 : = static_cast<int32_t (*)(uint32_t, const void*, uint32_t)>(MsprofReportBatchAdditionalInfoStub);
109 1 : dlMsprofStr2Id = static_cast<uint64_t (*)(const char*, uint32_t)>(MsprofStr2IdStub);
110 1 : dlMsprofSysCycleTime = static_cast<uint64_t (*)(void)>(MsprofSysCycleTimeStub);
111 1 : }
112 :
113 0 : HcclResult DlProfFunction::DlProfFunctionInterInit()
114 : {
115 0 : dlMsprofRegisterCallback = (int32_t(*)(uint32_t, ProfCommandHandle))dlsym(handle_, "MsprofRegisterCallback");
116 0 : CHK_PTR_NULL(dlMsprofRegisterCallback);
117 :
118 0 : dlMsprofRegTypeInfo = (int32_t(*)(uint16_t, uint32_t, const char*))dlsym(handle_, "MsprofRegTypeInfo");
119 0 : CHK_PTR_NULL(dlMsprofRegTypeInfo);
120 :
121 0 : dlMsprofReportApi = (int32_t(*)(uint32_t, const MsprofApi*))dlsym(handle_, "MsprofReportApi");
122 0 : CHK_PTR_NULL(dlMsprofReportApi);
123 :
124 : dlMsprofReportCompactInfo
125 0 : = (int32_t(*)(uint32_t, const VOID_PTR, uint32_t))dlsym(handle_, "MsprofReportCompactInfo");
126 0 : CHK_PTR_NULL(dlMsprofReportCompactInfo);
127 :
128 : dlMsprofReportAdditionalInfo
129 0 : = (int32_t(*)(uint32_t, const VOID_PTR, uint32_t))dlsym(handle_, "MsprofReportAdditionalInfo");
130 0 : CHK_PTR_NULL(dlMsprofReportAdditionalInfo);
131 :
132 : dlMsprofReportBatchAdditionalInfo
133 0 : = (int32_t(*)(uint32_t, const VOID_PTR, uint32_t))dlsym(handle_, "MsprofReportBatchAdditionalInfo");
134 0 : if (dlMsprofReportBatchAdditionalInfo == nullptr) {
135 0 : HCCL_INFO("[DlProfFunction] MsprofReportBatchAdditionalInfo not found, batch report disabled");
136 : }
137 :
138 0 : dlMsprofStr2Id = (uint64_t(*)(const char*, uint32_t))dlsym(handle_, "MsprofStr2Id");
139 0 : CHK_PTR_NULL(dlMsprofStr2Id);
140 :
141 0 : dlMsprofSysCycleTime = (uint64_t(*)(void))dlsym(handle_, "MsprofSysCycleTime");
142 0 : CHK_PTR_NULL(dlMsprofSysCycleTime);
143 :
144 0 : return HCCL_SUCCESS;
145 : }
146 :
147 41 : HcclResult DlProfFunction::DlProfFunctionInit()
148 : {
149 41 : if (initializedFlag_) {
150 40 : return HCCL_SUCCESS;
151 : }
152 :
153 1 : std::lock_guard<std::mutex> lock(handleMutex_);
154 1 : if (handle_ == nullptr) {
155 1 : handle_ = dlopen("libprofapi.so", RTLD_NOW);
156 : }
157 1 : if (handle_ != nullptr) {
158 0 : CHK_RET(DlProfFunctionInterInit());
159 : }
160 1 : initializedFlag_ = true;
161 1 : return HCCL_SUCCESS;
162 1 : }
163 : } // namespace Hccl
|