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 "msprof_api_adapter.h"
12 : #include "common/bqs_log.h"
13 :
14 : namespace bqs {
15 : namespace {
16 : const std::string MsprofSoName = "libprofapi.so";
17 :
18 : const std::string MsprofInitFuncName = "MsprofInit";
19 : const std::string MsprofFinalizeFuncName = "MsprofFinalize";
20 : const std::string MsprofRegTypeInfoFuncName = "MsprofRegTypeInfo";
21 : const std::string MsprofRegisterCallbackFuncName = "MsprofRegisterCallback";
22 : const std::string MsprofReportApiFuncName = "MsprofReportApi";
23 : const std::string MsprofReportEventFuncName = "MsprofReportEvent";
24 : const std::string MsprofSysCycleTimeFuncName = "MsprofSysCycleTime";
25 :
26 : using MsprofInitFunc = int32_t (*)(uint32_t, void*, uint32_t);
27 : using MsprofFinalizeFunc = int32_t (*)();
28 : using MsprofRegTypeInfoFunc = int32_t (*)(uint16_t, uint32_t, const char*);
29 : using MsprofRegisterCallbackFunc = int32_t (*)(uint32_t, ProfCommandHandle);
30 : using MsprofReportApiFunc = int32_t (*)(uint32_t, const MsprofApi*);
31 : using MsprofReportEventFunc = int32_t (*)(uint32_t, const MsprofEvent*);
32 : using MsprofSysCycleTimeFunc = uint64_t (*)();
33 : } // namespace
34 :
35 8 : BqsMsprofApiAdapter::BqsMsprofApiAdapter()
36 : : SoManager(
37 : MsprofSoName,
38 : {MsprofInitFuncName, MsprofFinalizeFuncName, MsprofRegTypeInfoFuncName, MsprofRegisterCallbackFuncName,
39 72 : MsprofReportApiFuncName, MsprofReportEventFuncName, MsprofSysCycleTimeFuncName})
40 16 : {}
41 :
42 117 : BqsMsprofApiAdapter& BqsMsprofApiAdapter::GetInstance()
43 : {
44 117 : static BqsMsprofApiAdapter instance;
45 117 : return instance;
46 : }
47 :
48 16 : ProfStatus BqsMsprofApiAdapter::MsprofInit(uint32_t dataType, void* data, uint32_t dataLen) const
49 : {
50 16 : void* funcHandle = GetFuncHandle(MsprofInitFuncName);
51 16 : if (funcHandle == nullptr) {
52 1 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
53 : }
54 :
55 15 : const int32_t ret = (PtrToFunctionPtr<void, MsprofInitFunc>(funcHandle))(dataType, data, dataLen);
56 15 : if (ret != 0) {
57 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofInitFuncName.c_str(), ret);
58 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
59 : }
60 :
61 15 : return ProfStatus::PROF_SUCCESS;
62 : }
63 :
64 11 : ProfStatus BqsMsprofApiAdapter::MsprofFinalize() const
65 : {
66 11 : void* funcHandle = GetFuncHandle(MsprofFinalizeFuncName);
67 11 : if (funcHandle == nullptr) {
68 2 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
69 : }
70 :
71 9 : const int32_t ret = (PtrToFunctionPtr<void, MsprofFinalizeFunc>(funcHandle))();
72 9 : if (ret != 0) {
73 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofFinalizeFuncName.c_str(), ret);
74 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
75 : }
76 :
77 9 : return ProfStatus::PROF_SUCCESS;
78 : }
79 :
80 46 : ProfStatus BqsMsprofApiAdapter::MsprofRegTypeInfo(uint16_t level, uint32_t typeId, const char* typeName) const
81 : {
82 46 : void* funcHandle = GetFuncHandle(MsprofRegTypeInfoFuncName);
83 46 : if (funcHandle == nullptr) {
84 1 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
85 : }
86 :
87 45 : const int32_t ret = (PtrToFunctionPtr<void, MsprofRegTypeInfoFunc>(funcHandle))(level, typeId, typeName);
88 45 : if (ret != 0) {
89 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofRegTypeInfoFuncName.c_str(), ret);
90 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
91 : }
92 :
93 45 : return ProfStatus::PROF_SUCCESS;
94 : }
95 :
96 16 : ProfStatus BqsMsprofApiAdapter::MsprofRegisterCallback(uint32_t moduleId, ProfCommandHandle handle) const
97 : {
98 16 : void* funcHandle = GetFuncHandle(MsprofRegisterCallbackFuncName);
99 16 : if (funcHandle == nullptr) {
100 1 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
101 : }
102 :
103 15 : const int32_t ret = (PtrToFunctionPtr<void, MsprofRegisterCallbackFunc>(funcHandle))(moduleId, handle);
104 15 : if (ret != 0) {
105 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofRegisterCallbackFuncName.c_str(), ret);
106 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
107 : }
108 :
109 15 : return ProfStatus::PROF_SUCCESS;
110 : }
111 :
112 6 : ProfStatus BqsMsprofApiAdapter::MsprofReportApi(uint32_t agingFlag, const MsprofApi* api) const
113 : {
114 6 : void* funcHandle = GetFuncHandle(MsprofReportApiFuncName);
115 6 : if (funcHandle == nullptr) {
116 1 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
117 : }
118 :
119 5 : const int32_t ret = (PtrToFunctionPtr<void, MsprofReportApiFunc>(funcHandle))(agingFlag, api);
120 5 : if (ret != 0) {
121 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofReportApiFuncName.c_str(), ret);
122 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
123 : }
124 :
125 5 : return ProfStatus::PROF_SUCCESS;
126 : }
127 :
128 3 : ProfStatus BqsMsprofApiAdapter::MsprofReportEvent(uint32_t agingFlag, const MsprofEvent* event) const
129 : {
130 3 : void* funcHandle = GetFuncHandle(MsprofReportEventFuncName);
131 3 : if (funcHandle == nullptr) {
132 2 : return ProfStatus::PROF_MSPROF_API_NULLPTR;
133 : }
134 :
135 1 : const int32_t ret = (PtrToFunctionPtr<void, MsprofReportEventFunc>(funcHandle))(agingFlag, event);
136 1 : if (ret != 0) {
137 0 : BQS_LOG_ERROR("[Prof]Call %s failed, msprofRet=%d.", MsprofReportEventFuncName.c_str(), ret);
138 0 : return ProfStatus::PROF_MSPROF_INNER_ERROR;
139 : }
140 :
141 1 : return ProfStatus::PROF_SUCCESS;
142 : }
143 :
144 13 : uint64_t BqsMsprofApiAdapter::MsprofSysCycleTime() const
145 : {
146 13 : void* funcHandle = GetFuncHandle(MsprofSysCycleTimeFuncName);
147 13 : if (funcHandle == nullptr) {
148 1 : return 0UL;
149 : }
150 :
151 12 : return (PtrToFunctionPtr<void, MsprofSysCycleTimeFunc>(funcHandle))();
152 : }
153 : } // namespace bqs
|