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 : std::string dumpPath;
90 : std::string dumpMode;
91 : std::string dumpData;
92 : std::string dumpKernelData;
93 : std::vector<std::string> dumpStats;
94 : std::string dumpOpSwitch;
95 : std::string dumpLevel;
96 : std::string dumpDebug;
97 : std::string dumpScene;
98 : };
99 :
100 : struct AclDumpBlacklist {
101 : std::string name;
102 : std::vector<std::string> pos;
103 : };
104 :
105 : struct OpNameRange {
106 : std::string begin;
107 : std::string end;
108 : };
109 :
110 : struct AclModelDumpConfig {
111 : std::string modelName;
112 : std::vector<std::string> layer;
113 : std::vector<std::string> watcherNodes;
114 : bool isLayer = false;
115 : bool isModelName = false;
116 : bool isWatcherNodes = false;
117 : std::vector<AclDumpBlacklist> optypeBlacklist;
118 : std::vector<AclDumpBlacklist> opnameBlacklist;
119 : std::vector<OpNameRange> dumpOpNameRanges;
120 : };
121 :
122 : struct DumpEnvVariable {
123 : std::string ascendDumpScene;
124 : std::string ascendDumpPath;
125 : std::string npuCollectPath;
126 : std::string ascendWorkPath;
127 : };
128 :
129 : struct DumpDfxConfig {
130 : std::string dumpPath;
131 : std::vector<std::string> dfxTypes;
132 : };
133 :
134 : class DumpConfigConverter {
135 : public:
136 859 : DumpConfigConverter(const char* dumpConfigData, size_t dumpConfigSize, const char* dumpConfigPath = "null")
137 859 : : dumpConfigData_(dumpConfigData), dumpConfigSize_(dumpConfigSize), configPath_(dumpConfigPath)
138 859 : {}
139 859 : ~DumpConfigConverter() = default;
140 :
141 : int32_t Convert(DumpType& dumpType, DumpConfig& dumpConfig, bool& needDump, DumpDfxConfig& dumpDfxConfig);
142 : static bool EnableExceptionDumpWithEnv(DumpConfig& dumpConfig, DumpType& dumpType);
143 : static bool EnableKernelDfxDumpWithEnv(DumpDfxConfig& config);
144 : static std::string DumpTypeToStr(const DumpType dumpType);
145 :
146 : private:
147 : bool CheckDumpFieldTypes() const;
148 : bool CheckDumpFieldValues() const;
149 : bool CheckDumpConstraints() const;
150 : bool CheckDumpListFieldTypes() const;
151 :
152 : bool CheckDumpScene(std::string& dumpScene) const;
153 : bool CheckWatcherDumpScene(const std::string& dumpScene) const;
154 : bool CheckExceptionDumpScene(const std::string& dumpScene) const;
155 :
156 : bool CheckDumpPath() const;
157 : bool CheckDumpDebug(std::string& dumpDebug) const;
158 : bool CheckDumpStats() const;
159 : bool CheckDumpStep() const;
160 :
161 : bool CheckDumplist(const std::string& dumpLevel) const;
162 :
163 : // dump_list 校验子函数
164 : bool CheckModelNameAndLayer(const AclModelDumpConfig& item, const std::string& itemPath) const;
165 : bool CheckWatcherSceneConstraints(
166 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpScene) const;
167 : bool CheckBlacklistSize(const AclModelDumpConfig& item, const std::string& itemPath) const;
168 : bool CheckBlacklistWithDumpLevel(
169 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpLevel) const;
170 : bool CheckOpNameRange(
171 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpLevel) const;
172 : bool CheckDumpListItems(
173 : const std::vector<AclModelDumpConfig>& dumpList, const std::string& dumpLevel, const std::string& dumpScene,
174 : bool isSwitchOff) const;
175 :
176 : bool CheckFieldValue(const nlohmann::json& js, const std::string& key) const;
177 : bool CheckKernelDataValues(const std::string& kernelData) const;
178 : bool CheckDumpStatsValues() const;
179 : bool CheckStringField(const nlohmann::json& js, const std::string& key, const std::string& basePath) const;
180 : bool CheckDumpListItemFieldTypes(const nlohmann::json& item, const std::string& basePath) const;
181 : bool CheckArrayOfString(const nlohmann::json& arr, const std::string& basePath) const;
182 : bool CheckArrayOfObject(const nlohmann::json& arr, const std::string& basePath) const;
183 : bool CheckArrayOfType(const nlohmann::json& arr, const std::string& basePath, bool isStringType) const;
184 : bool CheckBlacklistFieldTypes(const nlohmann::json& blacklistArray, const std::string& basePath) const;
185 : bool CheckBlacklistItemFieldTypes(const nlohmann::json& blacklistItem, const std::string& basePath) const;
186 : bool CheckOpnameRangeFieldTypes(const nlohmann::json& rangeArray, const std::string& basePath) const;
187 : bool CheckOpnameRangeItemFieldTypes(const nlohmann::json& rangeItem, const std::string& basePath) const;
188 :
189 : void ParseDfxTypesFromJson(const RawDumpConfig& rawDumpConfig, DumpDfxConfig& dumpDfxConfig) const;
190 : void EnsureDefaultDfxType(const DumpType& dumpType, DumpDfxConfig& dumpDfxConfig) const;
191 : DumpConfig ConvertDumpConfig(const RawDumpConfig& rawDumpConfig) const;
192 : DumpType ConvertDumpType(const RawDumpConfig& rawDumpConfig) const;
193 :
194 : std::string BuildIndexedPath(const std::string& base, size_t index) const;
195 : std::string BuildPath(const std::string& base, const std::string& key) const;
196 : void Split(const std::string& str, const char delim, std::vector<std::string>& elems) const;
197 : bool IsDigit(const std::string& str) const;
198 2601 : bool IsValueValid(const std::string key, const std::string value, const std::string& errorPath = "") const;
199 : bool ConflictWith(const std::string key, const std::string value) const;
200 : bool CheckIpAddress(const std::string dumpPath) const;
201 : bool NeedDump(const RawDumpConfig& rawDumpConfig) const;
202 : static std::string TransOptionsToStr(const std::set<std::string>& options);
203 : static bool ConvertDumpScene(const std::string dumpScene, DumpType& dumpType);
204 : static bool GetEnvVariable(const std::string& env, std::string& value);
205 : static bool CheckDumpPath(const std::string& param, const std::string& dumpPath);
206 : static bool GetEnvDumpPath(const std::string& env, std::string& envPath);
207 : static void LoadDumpEnvVariables(DumpEnvVariable& dumpEnvVariable);
208 :
209 : nlohmann::json dumpJs_;
210 : const char* dumpConfigData_;
211 : size_t dumpConfigSize_;
212 : const char* configPath_;
213 : };
214 :
215 : } // namespace Adx
216 : #endif // DUMP_CONFIG_CONVERTER_H
|