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 16495 : DlProfFunction& DlProfFunction::GetInstance()
16 : {
17 16495 : static DlProfFunction hcclDlProfFunction;
18 16495 : return hcclDlProfFunction;
19 : }
20 :
21 12 : DlProfFunction::DlProfFunction() { DlProfFunctionStubInit(); }
22 :
23 12 : DlProfFunction::~DlProfFunction()
24 : {
25 12 : if (handle_ != nullptr) {
26 0 : (void)dlclose(handle_);
27 0 : handle_ = nullptr;
28 : }
29 12 : }
30 :
31 0 : int32_t MsprofRegisterCallbackStub([[maybe_unused]] uint32_t moduleId, [[maybe_unused]] ProfCommandHandle handle)
32 : {
33 0 : HCCL_WARNING("Entry MsprofRegisterCallbackStub");
34 0 : return 0;
35 : }
36 :
37 84 : int32_t MsprofRegTypeInfoStub(
38 : [[maybe_unused]] uint16_t level, [[maybe_unused]] uint32_t typeId, [[maybe_unused]] const char* typeName)
39 : {
40 84 : HCCL_WARNING("Entry MsprofRegTypeInfoStub");
41 84 : return 0;
42 : }
43 :
44 53 : int32_t MsprofReportApiStub([[maybe_unused]] uint32_t agingFlag, [[maybe_unused]] const MsprofApi* api)
45 : {
46 53 : HCCL_WARNING("Entry MsprofReportApiStub");
47 53 : return 0;
48 : }
49 :
50 1 : int32_t MsprofReportCompactInfoStub(
51 : [[maybe_unused]] uint32_t agingFlag, [[maybe_unused]] const VOID_PTR data, [[maybe_unused]] uint32_t length)
52 : {
53 1 : HCCL_WARNING("Entry MsprofReportCompactInfoStub");
54 1 : return 0;
55 : }
56 :
57 48 : int32_t MsprofReportAdditionalInfoStub(
58 : [[maybe_unused]] uint32_t agingFlag, [[maybe_unused]] const VOID_PTR data, [[maybe_unused]] uint32_t length)
59 : {
60 48 : HCCL_WARNING("Entry MsprofReportAdditionalInfoStub");
61 48 : return 0;
62 : }
63 :
64 15734 : uint64_t MsprofStr2IdStub([[maybe_unused]] const char* hashInfo, [[maybe_unused]] uint32_t length)
65 : {
66 15734 : HCCL_WARNING("Entry MsprofStr2IdStub");
67 15734 : return 0;
68 : }
69 :
70 99 : uint64_t MsprofSysCycleTimeStub()
71 : {
72 99 : HCCL_WARNING("Entry MsprofSysCycleTimeStub");
73 99 : return 0;
74 : }
75 :
76 12 : void DlProfFunction::DlProfFunctionStubInit()
77 : {
78 12 : dlMsprofRegisterCallback = (s32(*)(uint32_t, ProfCommandHandle))MsprofRegisterCallbackStub;
79 12 : dlMsprofRegTypeInfo = (s32(*)(uint16_t, uint32_t, const char*))MsprofRegTypeInfoStub;
80 12 : dlMsprofReportApi = (s32(*)(uint32_t, const MsprofApi*))MsprofReportApiStub;
81 12 : dlMsprofReportCompactInfo = (s32(*)(uint32_t, const VOID_PTR, uint32_t))MsprofReportCompactInfoStub;
82 12 : dlMsprofReportAdditionalInfo = (s32(*)(uint32_t, const VOID_PTR, uint32_t))MsprofReportAdditionalInfoStub;
83 12 : dlMsprofStr2Id = (uint64_t(*)(const char*, uint32_t))MsprofStr2IdStub;
84 12 : dlMsprofSysCycleTime = (uint64_t(*)(void))MsprofSysCycleTimeStub;
85 12 : }
86 :
87 0 : HcclResult DlProfFunction::DlProfFunctionInterInit()
88 : {
89 0 : dlMsprofRegisterCallback = (s32(*)(uint32_t, ProfCommandHandle))dlsym(handle_, "MsprofRegisterCallback");
90 0 : CHK_SMART_PTR_NULL(dlMsprofRegisterCallback);
91 :
92 0 : dlMsprofRegTypeInfo = (s32(*)(uint16_t, uint32_t, const char*))dlsym(handle_, "MsprofRegTypeInfo");
93 0 : CHK_SMART_PTR_NULL(dlMsprofRegTypeInfo);
94 :
95 0 : dlMsprofReportApi = (s32(*)(uint32_t, const MsprofApi*))dlsym(handle_, "MsprofReportApi");
96 0 : CHK_SMART_PTR_NULL(dlMsprofReportApi);
97 :
98 0 : dlMsprofReportCompactInfo = (s32(*)(uint32_t, const VOID_PTR, uint32_t))dlsym(handle_, "MsprofReportCompactInfo");
99 0 : CHK_SMART_PTR_NULL(dlMsprofReportCompactInfo);
100 :
101 : dlMsprofReportAdditionalInfo
102 0 : = (s32(*)(uint32_t, const VOID_PTR, uint32_t))dlsym(handle_, "MsprofReportAdditionalInfo");
103 0 : CHK_SMART_PTR_NULL(dlMsprofReportAdditionalInfo);
104 :
105 0 : dlMsprofStr2Id = (uint64_t(*)(const char*, uint32_t))dlsym(handle_, "MsprofStr2Id");
106 0 : CHK_SMART_PTR_NULL(dlMsprofStr2Id);
107 :
108 0 : dlMsprofSysCycleTime = (uint64_t(*)(void))dlsym(handle_, "MsprofSysCycleTime");
109 0 : CHK_SMART_PTR_NULL(dlMsprofSysCycleTime);
110 :
111 0 : return HCCL_SUCCESS;
112 : }
113 :
114 476 : HcclResult DlProfFunction::DlProfFunctionInit()
115 : {
116 476 : std::lock_guard<std::mutex> lock(handleMutex_);
117 476 : if (handle_ == nullptr) {
118 476 : handle_ = dlopen("libprofapi.so", RTLD_NOW);
119 : }
120 476 : if (handle_ != nullptr) {
121 0 : CHK_RET(DlProfFunctionInterInit());
122 : }
123 476 : return HCCL_SUCCESS;
124 476 : }
125 : } // namespace hccl
|