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 : #ifndef DUMP_CONFIG_CONVERTER_H
12 : #define DUMP_CONFIG_CONVERTER_H
13 : #include <cstdint>
14 : #include <string>
15 : #include <vector>
16 : #include "adump_pub.h"
17 : #include "adump_api.h"
18 : #include "nlohmann/json.hpp"
19 :
20 : namespace Adx {
21 :
22 : const std::string ADUMP_DUMP = "dump";
23 : const std::string ADUMP_DUMP_SCENE = "dump_scene";
24 : const std::string ADUMP_DUMP_PATH = "dump_path";
25 : const std::string ADUMP_DUMP_MODE = "dump_mode";
26 : const std::string ADUMP_DUMP_DATA = "dump_data";
27 : const std::string ADUMP_DUMP_OP_SWITCH = "dump_op_switch";
28 : const std::string ADUMP_DUMP_LEVEL = "dump_level";
29 : const std::string ADUMP_DUMP_STATS = "dump_stats";
30 : const std::string ADUMP_DUMP_DEBUG = "dump_debug";
31 :
32 : const std::string ADUMP_DUMP_MODE_INPUT = "input";
33 : const std::string ADUMP_DUMP_MODE_OUTPUT = "output";
34 : const std::string ADUMP_DUMP_MODE_ALL = "all";
35 : const std::string ADUMP_DUMP_STATUS_SWITCH_ON = "on";
36 : const std::string ADUMP_DUMP_STATUS_SWITCH_OFF = "off";
37 : const std::string ADUMP_DUMP_MODEL_NAME = "model_name";
38 : const std::string ADUMP_DUMP_LAYER = "layer";
39 : const std::string ADUMP_DUMP_WATCHER_NODES = "watcher_nodes";
40 : const std::string ADUMP_DUMP_LIST = "dump_list";
41 : const std::string ADUMP_DUMP_STEP = "dump_step";
42 : const std::string ADUMP_DUMP_WATCHER = "watcher";
43 : const std::string ADUMP_DUMP_LEVEL_OP = "op";
44 : const std::string ADUMP_DUMP_LEVEL_KERNEL = "kernel";
45 : const std::string ADUMP_DUMP_LEVEL_ALL = "all";
46 : const std::string ADUMP_DUMP_DATA_TENSOR = "tensor";
47 : const std::string ADUMP_DUMP_DATA_OVERFLOW = "overflow";
48 : const std::string ADUMP_DUMP_DATA_STATS = "stats";
49 : const std::string ADUMP_DUMP_LITE_EXCEPTION = "lite_exception";
50 : const std::string ADUMP_DUMP_EXCEPTION_AIC_ERR_BRIEF = "aic_err_brief_dump";
51 : const std::string ADUMP_DUMP_EXCEPTION_AIC_ERR_NORM = "aic_err_norm_dump";
52 : const std::string ADUMP_DUMP_EXCEPTION_AIC_ERR_DETAIL = "aic_err_detail_dump";
53 : const std::string ADUMP_DUMP_OPTYPE_BLACKLIST = "optype_blacklist";
54 : const std::string ADUMP_DUMP_OPNAME_BLACKLIST = "opname_blacklist";
55 : const std::string ADUMP_DUMP_OPNAME_RANGE = "opname_range";
56 : const std::string ADUMP_DUMP_BLACKLIST_NAME = "name";
57 : const std::string ADUMP_DUMP_BLACKLIST_POS = "pos";
58 : const std::string ADUMP_DUMP_OPNAME_RANGE_BEGIN = "begin";
59 : const std::string ADUMP_DUMP_OPNAME_RANGE_END = "end";
60 : const std::string ADUMP_DUMP_KERNEL_DATA = "dump_kernel_data";
61 : const std::string ADUMP_DUMP_KERNEL_DATA_ALL = "all";
62 : const std::string ADUMP_DUMP_KERNEL_DATA_PRINTF = "printf";
63 : const std::string ADUMP_DUMP_KERNEL_DATA_TENSOR = "tensor";
64 : const std::string ADUMP_DUMP_KERNEL_DATA_ASSERT = "assert";
65 : const std::string ADUMP_DUMP_KERNEL_DATA_TIMESTAMP = "timestamp";
66 : const std::string ADUMP_DUMP_STATS_MAX = "Max";
67 : const std::string ADUMP_DUMP_STATS_MIN = "Min";
68 : const std::string ADUMP_DUMP_STATS_AVG = "Avg";
69 : const std::string ADUMP_DUMP_STATS_NAN = "Nan";
70 : const std::string ADUMP_DUMP_STATS_NEG_INF = "Negative Inf";
71 : const std::string ADUMP_DUMP_STATS_POS_INF = "Positive Inf";
72 : const std::string ADUMP_DUMP_STATS_L2NORM = "L2norm";
73 :
74 : constexpr int32_t MAX_DUMP_PATH_LENGTH = 512;
75 : constexpr int32_t MAX_IPV4_ADDRESS_VALUE = 255;
76 : constexpr int32_t MAX_IPV4_ADDRESS_LENGTH = 4;
77 : constexpr size_t MAX_DUMP_STEP_INTERVAL_COUNT = 100U;
78 : constexpr size_t MAX_BLACKLIST_SIZE = 100U;
79 : // 使能exception dump
80 : const std::string ADUMP_ENV_ASCEND_DUMP_SCENE = "ASCEND_DUMP_SCENE";
81 : // 设置dump路径(使能Kernel内DFX DUMP功能)
82 : const std::string ADUMP_ENV_ASCEND_DUMP_PATH = "ASCEND_DUMP_PATH";
83 : // 使能L1 exception dump(优先级低于ASCEND_DUMP_SCENE)
84 : const std::string ADUMP_ENV_NPU_COLLECT_PATH = "NPU_COLLECT_PATH";
85 : // 设置dump路径(使能Kernel内DFX DUMP功能,优先级低于ASCEND_DUMP_PATH)
86 : const std::string ADUMP_ENV_ASCEND_WORK_PATH = "ASCEND_WORK_PATH";
87 :
88 : struct RawDumpConfig
89 : {
90 : std::string dumpPath;
91 : std::string dumpMode;
92 : std::string dumpData;
93 : std::string dumpKernelData;
94 : std::vector<std::string> dumpStats;
95 : std::string dumpOpSwitch;
96 : std::string dumpLevel;
97 : std::string dumpDebug;
98 : std::string dumpScene;
99 : };
100 :
101 : struct AclDumpBlacklist
102 : {
103 : std::string name;
104 : std::vector<std::string> pos;
105 : };
106 :
107 : struct OpNameRange
108 : {
109 : std::string begin;
110 : std::string end;
111 : };
112 :
113 : struct AclModelDumpConfig
114 : {
115 : std::string modelName;
116 : std::vector<std::string> layer;
117 : std::vector<std::string> watcherNodes;
118 : bool isLayer = false;
119 : bool isModelName = false;
120 : bool isWatcherNodes = false;
121 : std::vector<AclDumpBlacklist> optypeBlacklist;
122 : std::vector<AclDumpBlacklist> opnameBlacklist;
123 : std::vector<OpNameRange> dumpOpNameRanges;
124 : };
125 :
126 : struct DumpEnvVariable {
127 : std::string ascendDumpScene;
128 : std::string ascendDumpPath;
129 : std::string npuCollectPath;
130 : std::string ascendWorkPath;
131 : };
132 :
133 : struct DumpDfxConfig {
134 : std::string dumpPath;
135 : std::vector<std::string> dfxTypes;
136 : };
137 :
138 : class DumpConfigConverter
139 : {
140 : public:
141 859 : DumpConfigConverter(const char *dumpConfigData, size_t dumpConfigSize, const char *dumpConfigPath = "null")
142 859 : : dumpConfigData_(dumpConfigData), dumpConfigSize_(dumpConfigSize), configPath_(dumpConfigPath) {}
143 859 : ~DumpConfigConverter() = default;
144 :
145 : int32_t Convert(DumpType &dumpType, DumpConfig &dumpConfig, bool &needDump, DumpDfxConfig &dumpDfxConfig);
146 : static bool EnableExceptionDumpWithEnv(DumpConfig &dumpConfig, DumpType &dumpType);
147 : static bool EnableKernelDfxDumpWithEnv(DumpDfxConfig &config);
148 : static std::string DumpTypeToStr(const DumpType dumpType);
149 :
150 : private:
151 : bool CheckDumpFieldTypes() const;
152 : bool CheckDumpFieldValues() const;
153 : bool CheckDumpConstraints() const;
154 : bool CheckDumpListFieldTypes() const;
155 :
156 : bool CheckDumpScene(std::string &dumpScene) const;
157 : bool CheckWatcherDumpScene(const std::string &dumpScene) const;
158 : bool CheckExceptionDumpScene(const std::string &dumpScene) const;
159 :
160 : bool CheckDumpPath() const;
161 : bool CheckDumpDebug(std::string &dumpDebug) const;
162 : bool CheckDumpStats() const;
163 : bool CheckDumpStep() const;
164 :
165 : bool CheckDumplist(const std::string &dumpLevel) const;
166 :
167 : // dump_list 校验子函数
168 : bool CheckModelNameAndLayer(const AclModelDumpConfig &item, const std::string &itemPath) const;
169 : bool CheckWatcherSceneConstraints(const AclModelDumpConfig &item, const std::string &itemPath,
170 : const std::string &dumpScene) const;
171 : bool CheckBlacklistSize(const AclModelDumpConfig &item, const std::string &itemPath) const;
172 : bool CheckBlacklistWithDumpLevel(const AclModelDumpConfig &item, const std::string &itemPath,
173 : const std::string &dumpLevel) const;
174 : bool CheckOpNameRange(const AclModelDumpConfig &item, const std::string &itemPath,
175 : const std::string &dumpLevel) const;
176 : bool CheckDumpListItems(const std::vector<AclModelDumpConfig> &dumpList, const std::string &dumpLevel,
177 : const std::string &dumpScene, bool isSwitchOff) const;
178 :
179 : bool CheckFieldValue(const nlohmann::json &js, const std::string &key) const;
180 : bool CheckKernelDataValues(const std::string &kernelData) const;
181 : bool CheckDumpStatsValues() const;
182 : bool CheckStringField(const nlohmann::json &js, const std::string &key, const std::string &basePath) const;
183 : bool CheckDumpListItemFieldTypes(const nlohmann::json &item, const std::string &basePath) const;
184 : bool CheckArrayOfString(const nlohmann::json &arr, const std::string &basePath) const;
185 : bool CheckArrayOfObject(const nlohmann::json &arr, const std::string &basePath) const;
186 : bool CheckArrayOfType(const nlohmann::json &arr, const std::string &basePath, bool isStringType) const;
187 : bool CheckBlacklistFieldTypes(const nlohmann::json &blacklistArray, const std::string &basePath) const;
188 : bool CheckBlacklistItemFieldTypes(const nlohmann::json &blacklistItem, const std::string &basePath) const;
189 : bool CheckOpnameRangeFieldTypes(const nlohmann::json &rangeArray, const std::string &basePath) const;
190 : bool CheckOpnameRangeItemFieldTypes(const nlohmann::json &rangeItem, const std::string &basePath) const;
191 :
192 : void ParseDfxTypesFromJson(const RawDumpConfig &rawDumpConfig, DumpDfxConfig &dumpDfxConfig) const;
193 : void EnsureDefaultDfxType(const DumpType &dumpType, DumpDfxConfig &dumpDfxConfig) const;
194 : DumpConfig ConvertDumpConfig(const RawDumpConfig &rawDumpConfig) const;
195 : DumpType ConvertDumpType(const RawDumpConfig &rawDumpConfig) const;
196 :
197 : std::string BuildIndexedPath(const std::string &base, size_t index) const;
198 : std::string BuildPath(const std::string &base, const std::string &key) const;
199 : void Split(const std::string &str, const char delim, std::vector<std::string> &elems) const;
200 : bool IsDigit(const std::string &str) const;
201 : bool IsValueValid(const std::string key, const std::string value, const std::string &errorPath = "") const;
202 : bool ConflictWith(const std::string key, const std::string value) const;
203 : bool CheckIpAddress(const std::string dumpPath) const;
204 : bool NeedDump(const RawDumpConfig &rawDumpConfig) const;
205 : static std::string TransOptionsToStr(const std::set<std::string> &options);
206 : static bool ConvertDumpScene(const std::string dumpScene, DumpType &dumpType);
207 : static bool GetEnvVariable(const std::string &env, std::string &value);
208 : static bool CheckDumpPath(const std::string ¶m, const std::string &dumpPath);
209 : static bool GetEnvDumpPath(const std::string &env, std::string &envPath);
210 : static void LoadDumpEnvVariables(DumpEnvVariable &dumpEnvVariable);
211 :
212 : nlohmann::json dumpJs_;
213 : const char *dumpConfigData_;
214 : size_t dumpConfigSize_;
215 : const char *configPath_;
216 : };
217 :
218 : } // namespace Adx
219 : #endif // DUMP_CONFIG_CONVERTER_H
|