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 "dump.h"
12 :
13 : #include <mutex>
14 : #include <sstream>
15 : #include <set>
16 :
17 : #include "mmpa/mmpa_api.h"
18 : #include "adx_datadump_server.h"
19 : #include "adump_pub.h"
20 :
21 : #include "common/json_parser.h"
22 : #include "common/error_codes_inner.h"
23 :
24 : #include "common/log_inner.h"
25 : #include "utils/string_utils.h"
26 : #include "aclrt_impl/acl_rt_impl_base.h"
27 : #include "acl_rt_impl.h"
28 :
29 : namespace {
30 : bool aclmdlInitDumpFlag = false;
31 : std::mutex aclDumpMutex;
32 : constexpr int32_t ADX_ERROR_NONE = 0;
33 : }
34 :
35 : namespace acl {
36 92 : AclDump &AclDump::GetInstance()
37 : {
38 : static AclDump aclDumpProc;
39 92 : return aclDumpProc;
40 : }
41 :
42 31 : aclError AclDump::HandleDumpCommand(const char *configStr, size_t size, const char *configPath)
43 : {
44 31 : ACL_LOG_INFO("start to execute HandleDumpCommand.");
45 :
46 31 : int32_t adxRet = AdxDataDumpServerInit();
47 31 : if (adxRet != ADX_ERROR_NONE) {
48 1 : ACL_LOG_INNER_ERROR("[AdxDataDumpServer][Init]dump server run failed, adx errorCode = %d", adxRet);
49 1 : return ACL_ERROR_INTERNAL_ERROR;
50 : }
51 30 : acl::AclDump::GetInstance().SetAdxInitFromAclInitFlag(true);
52 :
53 : // base dump
54 : Adx::DumpConfigInfo configInfo;
55 30 : configInfo.dumpConfigPath = configPath;
56 30 : configInfo.dumpConfigData = configStr;
57 30 : configInfo.dumpConfigSize = size;
58 30 : adxRet = Adx::AdumpSetDumpConfig(configInfo);
59 30 : if (adxRet != ADX_ERROR_NONE) {
60 2 : auto ret =
61 2 : (adxRet == Adx::ADUMP_INPUT_FAILED) ? ACL_ERROR_INVALID_DUMP_CONFIG : ACL_ERROR_INTERNAL_ERROR;
62 2 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump config failed, adx errorCode = %d", adxRet);
63 2 : return ret;
64 : }
65 :
66 28 : return ACL_SUCCESS;
67 : }
68 :
69 33 : aclError AclDump::HandleDumpConfig(const char_t *const configPath)
70 : {
71 33 : ACL_LOG_INFO("start to execute HandleDumpConfig.");
72 33 : std::string configStr;
73 33 : const aclError ret = acl::JsonParser::GetConfigStrFromFile(configPath, configStr);
74 33 : if (ret != ACL_SUCCESS) {
75 0 : ACL_LOG_INNER_ERROR("Get config string from file[%s] failed, errorCode = %d",
76 : configPath, ret);
77 0 : return ret;
78 : }
79 : try {
80 33 : if (!configStr.empty()) {
81 31 : return HandleDumpCommand(configStr.c_str(), configStr.size(), configPath);
82 : }
83 0 : } catch (const nlohmann::json::exception &e) {
84 0 : ACL_LOG_INNER_ERROR("[Convert][DumpConfig]parse json for config failed, exception:%s.",
85 : e.what());
86 0 : return ACL_ERROR_INVALID_DUMP_CONFIG;
87 0 : }
88 2 : ACL_LOG_INFO("HandleDumpConfig end in HandleDumpConfig.");
89 2 : return ACL_SUCCESS;
90 33 : }
91 : } // namespace acl
92 :
93 : #ifdef __cplusplus
94 : extern "C" {
95 : #endif
96 :
97 12 : aclError aclmdlInitDumpImpl()
98 : {
99 12 : ACL_LOG_INFO("start to execute aclmdlInitDump.");
100 12 : if (!acl::GetAclInitFlag()) {
101 1 : acl::AclErrorLogManager::ReportInputError(
102 2 : "EP0008", std::vector<const char*>({"func", "reason"}),
103 1 : std::vector<const char*>(
104 1 : {"aclmdlInitDump", "aclInit must be executed before aclmdlInitDump is called"}));
105 1 : ACL_LOG_ERROR("[Check][AclInitFlag]aclInit must be executed before aclmdlInitDump is called");
106 1 : return ACL_ERROR_UNINITIALIZE;
107 : }
108 :
109 11 : const std::unique_lock<std::mutex> lk(aclDumpMutex);
110 11 : if (aclmdlInitDumpFlag) {
111 1 : acl::AclErrorLogManager::ReportInputError(
112 2 : "EP0008", std::vector<const char*>({"func", "reason"}),
113 2 : std::vector<const char*>({"aclmdlInitDump", "This API cannot be called repeatedly"}));
114 1 : ACL_LOG_ERROR("[Check][InitDumpFlag]This API cannot be called repeatedly");
115 1 : return ACL_ERROR_REPEAT_INITIALIZE;
116 : }
117 :
118 10 : const int32_t adxRet = AdxDataDumpServerInit();
119 10 : if (adxRet != ADX_ERROR_NONE) {
120 1 : ACL_LOG_CALL_ERROR("[AdxDataDumpServer][Init]dump server run failed, adx errorCode = %d", adxRet);
121 1 : return ACL_ERROR_INTERNAL_ERROR;
122 : }
123 :
124 9 : aclmdlInitDumpFlag = true;
125 9 : ACL_LOG_INFO("successfully initialized dump in aclmdlInitDump.");
126 9 : return ACL_SUCCESS;
127 11 : }
128 :
129 10 : aclError aclmdlSetDumpImpl(const char *dumpCfgPath)
130 : {
131 10 : ACL_LOG_INFO("start to execute aclmdlSetDump.");
132 10 : if (!acl::GetAclInitFlag()) {
133 1 : acl::AclErrorLogManager::ReportInputError(
134 2 : "EP0008", std::vector<const char*>({"func", "reason"}),
135 1 : std::vector<const char*>(
136 1 : {"aclmdlSetDump", "aclInit must be executed before aclmdlSetDumpImpl is called"}));
137 1 : ACL_LOG_ERROR("[Check][AclInitFlag]aclInit must be executed before aclmdlSetDumpImpl is called");
138 1 : return ACL_ERROR_UNINITIALIZE;
139 : }
140 :
141 9 : const std::unique_lock<std::mutex> lk(aclDumpMutex);
142 9 : if (!aclmdlInitDumpFlag) {
143 2 : acl::AclErrorLogManager::ReportInputError(
144 4 : "EP0008", std::vector<const char*>({"func", "reason"}),
145 2 : std::vector<const char*>(
146 2 : {"aclmdlSetDump", "aclmdlInitDump must be executed before aclmdlSetDumpImpl is called"}));
147 2 : ACL_LOG_ERROR("[Check][aclmdlInitDumpFlag]aclmdlInitDump must be executed before aclmdlSetDumpImpl is called");
148 2 : return ACL_ERROR_DUMP_NOT_RUN;
149 : }
150 :
151 7 : ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(dumpCfgPath);
152 6 : std::string configStr;
153 6 : aclError ret = acl::JsonParser::GetConfigStrFromFile(dumpCfgPath, configStr);
154 6 : if (ret != ACL_SUCCESS) {
155 1 : ACL_LOG_INNER_ERROR("Get config string from file[%s] failed, errorCode = %d",
156 : dumpCfgPath, ret);
157 1 : return ret;
158 : }
159 :
160 : // base dump
161 5 : if (!configStr.empty()) {
162 5 : ACL_LOG_INFO("Start to set dump.");
163 : Adx::DumpConfigInfo configInfo;
164 5 : configInfo.dumpConfigPath = dumpCfgPath;
165 5 : configInfo.dumpConfigData = configStr.c_str();
166 5 : configInfo.dumpConfigSize = configStr.size();
167 5 : const auto adxRet = Adx::AdumpSetDumpConfig(configInfo);
168 5 : if (adxRet != ADX_ERROR_NONE) {
169 2 : ret =
170 2 : (adxRet == Adx::ADUMP_INPUT_FAILED) ? ACL_ERROR_INVALID_DUMP_CONFIG : ACL_ERROR_INTERNAL_ERROR;
171 2 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump config failed, adx errorCode = %d", adxRet);
172 2 : return ret;
173 : }
174 : }
175 3 : ACL_LOG_INFO("set dump config successfully.");
176 3 : return ACL_SUCCESS;
177 9 : }
178 :
179 14 : aclError aclmdlFinalizeDumpImpl()
180 : {
181 14 : ACL_LOG_INFO("start to execute aclmdlFinalizeDump.");
182 14 : if (!acl::GetAclInitFlag()) {
183 1 : acl::AclErrorLogManager::ReportInputError(
184 2 : "EP0008", std::vector<const char*>({"func", "reason"}),
185 1 : std::vector<const char*>(
186 1 : {"aclmdlFinalizeDump", "aclInit must be executed before aclmdlFinalizeDump is called"}));
187 1 : ACL_LOG_ERROR("[Check][AclInitFlag]aclInit must be executed before aclmdlFinalizeDump is called");
188 1 : return ACL_ERROR_UNINITIALIZE;
189 : }
190 :
191 13 : const std::unique_lock<std::mutex> lk(aclDumpMutex);
192 13 : if (!aclmdlInitDumpFlag) {
193 1 : acl::AclErrorLogManager::ReportInputError(
194 2 : "EP0008", std::vector<const char*>({"func", "reason"}),
195 1 : std::vector<const char*>(
196 1 : {"aclmdlFinalizeDump", "aclmdlInitDump must be executed before aclmdlFinalizeDump is called"}));
197 1 : ACL_LOG_ERROR("[Check][aclmdlInitDumpFlag]aclmdlInitDump must be executed before aclmdlFinalizeDump is called");
198 1 : return ACL_ERROR_DUMP_NOT_RUN;
199 : }
200 :
201 : // close adump opened
202 12 : int32_t adxRet = Adx::AdumpUnSetDump();
203 12 : if (adxRet != ADX_ERROR_NONE) {
204 2 : ACL_LOG_INNER_ERROR("[Set][Dump]set dump off failed, adx errorCode = %d", adxRet);
205 2 : return ACL_ERROR_INTERNAL_ERROR;
206 : }
207 :
208 : // close dump server
209 10 : adxRet = AdxDataDumpServerUnInit();
210 10 : if (adxRet != ADX_ERROR_NONE) {
211 1 : ACL_LOG_CALL_ERROR("[AdxDataDumpServer][UnInit]generate dump file failed in disk, adx errorCode = %d", adxRet);
212 1 : return ACL_ERROR_INTERNAL_ERROR;
213 : }
214 :
215 9 : aclmdlInitDumpFlag = false;
216 9 : ACL_LOG_INFO("successfully execute aclmdlFinalizeDump, the dump task completed!");
217 9 : return ACL_SUCCESS;
218 13 : }
219 : #ifdef __cplusplus
220 : }
221 : #endif
|