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 : /* WARNING: This is an internal header file. Any external reference to or inclusion of this file is prohibited.
12 : * It will be deprecated once the compatibility periods expires.
13 : * -------------------------------------------------------------------------------------------------------*/
14 : #ifndef ERROR_MANAGER_H_
15 : #define ERROR_MANAGER_H_
16 :
17 : #include <cinttypes>
18 : #include <map>
19 : #include <set>
20 : #include <string>
21 : #include <vector>
22 : #include <mutex>
23 : #include <cstring>
24 : #include <cstdlib>
25 :
26 : namespace error_message {
27 : using char_t = char;
28 : std::string TrimPath(const std::string& str);
29 : #ifdef __GNUC__
30 : int32_t FormatErrorMessage(char_t* str_dst, size_t dst_max, const char_t* format, ...)
31 : __attribute__((format(printf, 3, 4)));
32 :
33 : void ReportInnerError(
34 : const char_t* file_name, const char_t* func, uint32_t line, const std::string error_code, const char_t* format, ...)
35 : __attribute__((format(printf, 5, 6)));
36 : #else
37 : int32_t FormatErrorMessage(char_t* str_dst, size_t dst_max, const char_t* format, ...);
38 : void ReportInnerError(
39 : const char_t* file_name, const char_t* func, uint32_t line, const std::string error_code, const char_t* format,
40 : ...);
41 : #endif
42 : } // namespace error_message
43 :
44 : constexpr size_t const LIMIT_PER_MESSAGE = 1024U;
45 :
46 : ///
47 : /// @brief Report error message
48 : /// @param [in] key: vector parameter key
49 : /// @param [in] value: vector parameter value
50 : ///
51 : #define REPORT_INPUT_ERROR(error_code, key, value) \
52 : ErrorManager::GetInstance().ATCReportErrMessage(error_code, key, value)
53 :
54 : ///
55 : /// @brief Report error message
56 : /// @param [in] key: vector parameter key
57 : /// @param [in] value: vector parameter value
58 : ///
59 : #define REPORT_ENV_ERROR(error_code, key, value) ErrorManager::GetInstance().ATCReportErrMessage(error_code, key, value)
60 :
61 : #define REPORT_INNER_ERROR(error_code, fmt, ...) \
62 : error_message::ReportInnerError(__FILE__, &__FUNCTION__[0], __LINE__, (error_code), (fmt), ##__VA_ARGS__)
63 :
64 : #define REPORT_CALL_ERROR REPORT_INNER_ERROR
65 :
66 : namespace error_message {
67 : // first stage
68 : extern const std::string kInitialize;
69 : extern const std::string kModelCompile;
70 : extern const std::string kModelLoad;
71 : extern const std::string kModelExecute;
72 : extern const std::string kFinalize;
73 :
74 : // SecondStage
75 : // INITIALIZE
76 : extern const std::string kParser;
77 : extern const std::string kOpsProtoInit;
78 : extern const std::string kSystemInit;
79 : extern const std::string kEngineInit;
80 : extern const std::string kOpsKernelInit;
81 : extern const std::string kOpsKernelBuilderInit;
82 : // MODEL_COMPILE
83 : extern const std::string kPrepareOptimize;
84 : extern const std::string kOriginOptimize;
85 : extern const std::string kSubGraphOptimize;
86 : extern const std::string kMergeGraphOptimize;
87 : extern const std::string kPreBuild;
88 : extern const std::string kStreamAlloc;
89 : extern const std::string kMemoryAlloc;
90 : extern const std::string kTaskGenerate;
91 : // COMMON
92 : extern const std::string kOther;
93 :
94 : struct Context {
95 : uint64_t work_stream_id;
96 : std::string first_stage;
97 : std::string second_stage;
98 : std::string log_header;
99 : };
100 :
101 : enum class ErrorMsgMode : uint32_t {
102 : // 0:内置模式,推理采用线程粒度,训练采用session粒度,1:以进程为粒度
103 : INTERNAL_MODE = 0U,
104 : PROCESS_MODE = 1U,
105 : ERR_MSG_MODE_MAX = 2U
106 : };
107 :
108 : struct ErrorItem {
109 : std::string error_id;
110 : std::string error_title;
111 : std::string error_message;
112 : std::string possible_cause;
113 : std::string solution;
114 : // args_map作用于有error_id对应的json文件配置时,填充error_message对象
115 : std::map<std::string, std::string> args_map;
116 : std::string report_time;
117 :
118 : friend bool operator==(const ErrorItem& lhs, const ErrorItem& rhs) noexcept
119 : {
120 : return (lhs.error_id == rhs.error_id) && (lhs.error_message == rhs.error_message) &&
121 : (lhs.possible_cause == rhs.possible_cause) && (lhs.solution == rhs.solution);
122 : }
123 : };
124 : } // namespace error_message
125 :
126 : class ErrorManager {
127 : public:
128 : using ErrorItem = error_message::ErrorItem;
129 : /// @brief Obtain ErrorManager instance
130 : /// @return ErrorManager instance
131 : static ErrorManager& GetInstance();
132 :
133 : /// @brief init
134 : /// @return int 0(success) -1(fail)
135 : int32_t Init();
136 :
137 : int32_t Init(error_message::ErrorMsgMode error_mode);
138 :
139 : /// @brief init
140 : /// @param [in] path: current so path
141 : /// @return int 0(success) -1(fail)
142 : int32_t Init(const std::string path);
143 :
144 : int32_t ReportInterErrMessage(const std::string error_code, const std::string& error_msg);
145 :
146 : /// @brief Report error message
147 : /// @param [in] error_code: error code
148 : /// @param [in] args_map: parameter map
149 : /// @return int 0(success) -1(fail)
150 : int32_t ReportErrMessage(const std::string error_code, const std::map<std::string, std::string>& args_map);
151 :
152 : /// @brief Report user defined error message
153 : /// @param [in] error_code: user defined error code
154 : /// @param [in] errmsg: error message
155 : /// @return int 0(success) -1(fail)
156 : int32_t ReportErrMsgWithoutTpl(const std::string& error_code, const std::string& errmsg);
157 :
158 : /// @brief output error message
159 : /// @param [in] handle: print handle
160 : /// @return int 0(success) -1(fail)
161 : int32_t OutputErrMessage(int32_t handle);
162 :
163 : /// @brief output message
164 : /// @param [in] handle: print handle
165 : /// @return int 0(success) -1(fail)
166 : int32_t OutputMessage(int32_t handle);
167 : // 调用成功后会进行已有的错误信息的清理
168 : std::string GetErrorMessage();
169 :
170 : // 传递原始错误码信息
171 : int32_t SetRawErrorMessages(const std::vector<ErrorItem>& items);
172 :
173 : // 获取当前线程下原始上报的内部,外部错误信息,顺序为上报的原始顺序
174 : // 便于调用方进行自定制的加工,调用成功后会进行已有的错误信息的清理
175 : std::vector<ErrorItem> GetRawErrorMessages();
176 :
177 : std::string GetWarningMessage();
178 :
179 : /// @brief Report error message
180 : /// @param [in] key: vector parameter key
181 : /// @param [in] value: vector parameter value
182 : void ATCReportErrMessage(
183 : const std::string error_code, const std::vector<std::string>& key = {},
184 : const std::vector<std::string>& value = {});
185 :
186 : /// @brief report graph compile failed message such as error code and op_name in mstune case
187 : /// @param [in] graph_name: root graph name
188 : /// @param [in] msg: failed message map, key is error code, value is op_name
189 : /// @return int 0(success) -1(fail)
190 : int32_t ReportMstuneCompileFailedMsg(
191 : const std::string& root_graph_name, const std::map<std::string, std::string>& msg);
192 :
193 : /// @brief get graph compile failed message in mstune case
194 : /// @param [in] graph_name: graph name
195 : /// @param [out] msg_map: failed message map, key is error code, value is op_name list
196 : /// @return int 0(success) -1(fail)
197 : int32_t GetMstuneCompileFailedMsg(
198 : const std::string& graph_name, std::map<std::string, std::vector<std::string>>& msg_map);
199 :
200 : // @brief generate work_stream_id by current pid and tid, clear error_message stored by same work_stream_id
201 : // used in external api entrance, all sync api can use
202 : void GenWorkStreamIdDefault();
203 :
204 : // @brief generate work_stream_id by args sessionid and graphid, clear error_message stored by same work_stream_id
205 : // used in external api entrance
206 : void GenWorkStreamIdBySessionGraph(const uint64_t session_id, const uint64_t graph_id);
207 :
208 : void GenWorkStreamIdWithSessionIdGraphId(const uint64_t session_id, const uint64_t graph_id);
209 :
210 : const std::string& GetLogHeader();
211 :
212 : error_message::Context& GetErrorManagerContext();
213 :
214 : void SetErrorContext(error_message::Context error_context);
215 :
216 : void SetStage(const std::string& first_stage, const std::string& second_stage);
217 :
218 : // The default priority is 0 and a higher value indicates a higher priority
219 : int32_t ParseJsonFormatString(const void* const handle, uint32_t priority = 0);
220 :
221 : private:
222 : struct ErrorInfoConfig {
223 : std::string error_id;
224 : std::string error_title;
225 : std::string error_message;
226 : std::string possible_cause;
227 : std::string solution;
228 : std::vector<std::string> arg_list;
229 : uint32_t priority{0};
230 : };
231 3 : ErrorManager() = default;
232 3 : ~ErrorManager() = default;
233 :
234 : ErrorManager(const ErrorManager&) = delete;
235 : ErrorManager(ErrorManager&&) = delete;
236 : ErrorManager& operator=(const ErrorManager&) & = delete;
237 : ErrorManager& operator=(ErrorManager&&) & = delete;
238 :
239 : int32_t ParseJsonFile(const std::string path);
240 :
241 : static int32_t ReadJsonFile(const std::string& file_path, void* const handle);
242 :
243 : void ClassifyCompileFailedMsg(
244 : const std::map<std::string, std::string>& msg, std::map<std::string, std::vector<std::string>>& classified_msg);
245 :
246 : bool IsInnerErrorCode(const std::string& error_code) const;
247 :
248 : bool IsUserDefinedErrorCode(const std::string& error_code);
249 :
250 : bool IsParamCheckErrorId(const std::string& error_code) const;
251 :
252 : inline bool IsValidErrorCode(const std::string& error_codes) const
253 : {
254 : constexpr uint32_t kErrorCodeValidLength = 6U;
255 : return error_codes.size() == kErrorCodeValidLength;
256 : }
257 :
258 : std::vector<ErrorItem>& GetErrorMsgContainerByWorkId(uint64_t work_id);
259 : std::vector<ErrorItem>& GetWarningMsgContainerByWorkId(uint64_t work_id);
260 :
261 : std::vector<ErrorItem>& GetErrorMsgContainer(uint64_t work_stream_id);
262 : std::vector<ErrorItem>& GetWarningMsgContainer(uint64_t work_stream_id);
263 :
264 : void AssembleInnerErrorMessage(
265 : const std::vector<ErrorItem>& error_messages, const std::string& first_code,
266 : std::stringstream& err_stream) const;
267 :
268 : void ClearErrorMsgContainerByWorkId(const uint64_t work_stream_id);
269 : void ClearWarningMsgContainerByWorkId(const uint64_t work_stream_id);
270 :
271 : void ClearErrorMsgContainer(const uint64_t work_stream_id);
272 : void ClearWarningMsgContainer(const uint64_t work_stream_id);
273 :
274 : bool is_init_ = false;
275 : std::mutex mutex_;
276 : std::map<std::string, ErrorInfoConfig> error_map_;
277 : std::map<std::string, std::map<std::string, std::vector<std::string>>> compile_failed_msg_map_;
278 :
279 : std::map<uint64_t, std::vector<ErrorItem>> error_message_per_work_id_;
280 : std::map<uint64_t, std::vector<ErrorItem>> warning_messages_per_work_id_;
281 :
282 : thread_local static error_message::Context error_context_;
283 :
284 : error_message::ErrorMsgMode error_mode_ = error_message::ErrorMsgMode::INTERNAL_MODE;
285 : std::vector<ErrorItem> error_message_process_; // 进程粒度,所有的errmsg存到同一个vector
286 : std::vector<ErrorItem> warning_messages_process_; // 进程粒度,所有的warning msg存到同一个vector
287 : };
288 :
289 : #ifdef __cplusplus
290 : extern "C" {
291 : #endif
292 :
293 : #if defined(__GNUC__)
294 : #ifndef GE_FUNC_HOST_VISIBILITY
295 : #if defined(HOST_VISIBILITY)
296 : #define GE_FUNC_HOST_VISIBILITY __attribute__((visibility("default")))
297 : #else
298 : #define GE_FUNC_HOST_VISIBILITY
299 : #endif
300 : #endif // GE_FUNC_HOST_VISIBILITY
301 :
302 : #ifndef GE_FUNC_DEV_VISIBILITY
303 : #if defined(DEV_VISIBILITY)
304 : #define GE_FUNC_DEV_VISIBILITY __attribute__((visibility("default")))
305 : #else
306 : #define GE_FUNC_DEV_VISIBILITY
307 : #endif
308 : #endif // GE_FUNC_DEV_VISIBILITY
309 :
310 : #ifndef FORMAT_PRINTF
311 : #define FORMAT_PRINTF(format_idx, first_arg) __attribute__((format(printf, (format_idx), (first_arg))))
312 : #endif
313 :
314 : #else
315 : #ifndef GE_FUNC_HOST_VISIBILITY
316 : #define GE_FUNC_HOST_VISIBILITY
317 : #endif
318 :
319 : #ifndef GE_FUNC_DEV_VISIBILITY
320 : #define GE_FUNC_DEV_VISIBILITY
321 : #endif
322 :
323 : #ifndef FORMAT_PRINTF
324 : #define FORMAT_PRINTF(format_idx, first_arg)
325 : #endif
326 : #endif
327 :
328 : GE_FUNC_HOST_VISIBILITY GE_FUNC_DEV_VISIBILITY int32_t
329 : RegisterFormatErrorMessageForC(const char* error_msg, unsigned long error_msg_len);
330 :
331 : GE_FUNC_HOST_VISIBILITY GE_FUNC_DEV_VISIBILITY int32_t
332 : ReportPredefinedErrMsgForC(const char* error_code, const char** key, const char** value, unsigned long arg_num);
333 :
334 : #ifdef __GNUC__
335 : GE_FUNC_HOST_VISIBILITY GE_FUNC_DEV_VISIBILITY int32_t ReportInnerErrMsgForC(
336 : const char* file_name, const char* func, uint32_t line, const char* error_code, const char* format, ...)
337 : FORMAT_PRINTF(5, 6);
338 : #else
339 : GE_FUNC_HOST_VISIBILITY GE_FUNC_DEV_VISIBILITY int32_t ReportInnerErrMsgForC(
340 : const char* file_name, const char* func, uint32_t line, const char* error_code, const char* format, ...);
341 : #endif
342 :
343 : #ifdef __cplusplus
344 : }
345 : #endif
346 :
347 : #endif // ERROR_MANAGER_H_
|