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