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