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 : #include <set>
11 : #include <string>
12 : #include <mutex>
13 : #include <vector>
14 : #include <cctype>
15 : #include <cstdint>
16 : #include <sstream>
17 : #include "log/adx_log.h"
18 : #include "common/file_utils.h"
19 : #include "common/str_utils.h"
20 : #include "adump_api.h"
21 : #include "adump_pub.h"
22 : #include "adump_error_manager.h"
23 : #include "dump_config_converter.h"
24 : #include "common/json_parser.h"
25 : #include "common/path.h"
26 :
27 : namespace Adx {
28 : constexpr int32_t DECIMAL = 10;
29 : const std::map<std::string, std::set<std::string>> dumpValidOptions = {
30 : {ADUMP_DUMP_MODE, {ADUMP_DUMP_MODE_INPUT, ADUMP_DUMP_MODE_OUTPUT, ADUMP_DUMP_MODE_ALL}},
31 : {ADUMP_DUMP_DATA, {ADUMP_DUMP_DATA_TENSOR, ADUMP_DUMP_DATA_STATS}},
32 : {ADUMP_DUMP_KERNEL_DATA,
33 : {ADUMP_DUMP_KERNEL_DATA_ALL, ADUMP_DUMP_KERNEL_DATA_PRINTF, ADUMP_DUMP_KERNEL_DATA_TENSOR,
34 : ADUMP_DUMP_KERNEL_DATA_ASSERT, ADUMP_DUMP_KERNEL_DATA_TIMESTAMP}},
35 : {ADUMP_DUMP_OP_SWITCH, {ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_STATUS_SWITCH_OFF}},
36 : {ADUMP_DUMP_LEVEL, {ADUMP_DUMP_LEVEL_OP, ADUMP_DUMP_LEVEL_KERNEL, ADUMP_DUMP_LEVEL_ALL}},
37 : {ADUMP_DUMP_DEBUG, {ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_STATUS_SWITCH_OFF}},
38 : {ADUMP_DUMP_SCENE,
39 : {ADUMP_DUMP_LITE_EXCEPTION, ADUMP_DUMP_EXCEPTION_AIC_ERR_BRIEF, ADUMP_DUMP_EXCEPTION_AIC_ERR_NORM,
40 : ADUMP_DUMP_EXCEPTION_AIC_ERR_DETAIL, ADUMP_DUMP_WATCHER}},
41 : {ADUMP_DUMP_STATS,
42 : {ADUMP_DUMP_STATS_MAX, ADUMP_DUMP_STATS_MIN, ADUMP_DUMP_STATS_AVG, ADUMP_DUMP_STATS_NAN, ADUMP_DUMP_STATS_NEG_INF,
43 : ADUMP_DUMP_STATS_POS_INF, ADUMP_DUMP_STATS_L2NORM}},
44 : };
45 :
46 : const std::set<std::string> envDumpScenes = {
47 : ADUMP_DUMP_EXCEPTION_AIC_ERR_BRIEF, ADUMP_DUMP_EXCEPTION_AIC_ERR_NORM, ADUMP_DUMP_EXCEPTION_AIC_ERR_DETAIL};
48 :
49 333 : static void from_json(const nlohmann::json& js, RawDumpConfig& config)
50 : {
51 333 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_PATH, config.dumpPath);
52 333 : config.dumpMode = JsonParser::GetStringOrDefault(js, ADUMP_DUMP_MODE, ADUMP_DUMP_MODE_OUTPUT);
53 333 : config.dumpOpSwitch = JsonParser::GetStringOrDefault(js, ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_OFF);
54 333 : config.dumpDebug = JsonParser::GetStringOrDefault(js, ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_OFF);
55 333 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_SCENE, config.dumpScene);
56 333 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_DATA, config.dumpData);
57 333 : config.dumpKernelData = JsonParser::GetStringOrDefault(js, ADUMP_DUMP_KERNEL_DATA, ADUMP_DUMP_KERNEL_DATA_ALL);
58 333 : config.dumpLevel = JsonParser::GetStringOrDefault(js, ADUMP_DUMP_LEVEL, ADUMP_DUMP_LEVEL_ALL);
59 333 : if (JsonParser::ContainKey(js, ADUMP_DUMP_STATS)) {
60 36 : config.dumpStats = js.at(ADUMP_DUMP_STATS).get<std::vector<std::string>>();
61 : }
62 333 : }
63 :
64 27 : static void from_json(const nlohmann::json& js, OpNameRange& range)
65 : {
66 27 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_OPNAME_RANGE_BEGIN, range.begin);
67 27 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_OPNAME_RANGE_END, range.end);
68 27 : }
69 :
70 636 : static void from_json(const nlohmann::json& js, AclDumpBlacklist& blacklist)
71 : {
72 636 : JsonParser::GetStringIfExist(js, ADUMP_DUMP_BLACKLIST_NAME, blacklist.name);
73 636 : if (JsonParser::ContainKey(js, ADUMP_DUMP_BLACKLIST_POS)) {
74 15 : blacklist.pos = js.at(ADUMP_DUMP_BLACKLIST_POS).get<std::vector<std::string>>();
75 : }
76 636 : }
77 :
78 141 : static void from_json(const nlohmann::json& js, AclModelDumpConfig& info)
79 : {
80 141 : info.isLayer = false;
81 141 : if (JsonParser::GetStringIfExist(js, ADUMP_DUMP_MODEL_NAME, info.modelName)) {
82 123 : info.isModelName = true;
83 : }
84 141 : if (JsonParser::ContainKey(js, ADUMP_DUMP_LAYER)) {
85 18 : info.layer = js.at(ADUMP_DUMP_LAYER).get<std::vector<std::string>>();
86 18 : info.isLayer = true;
87 : }
88 141 : if (JsonParser::ContainKey(js, ADUMP_DUMP_WATCHER_NODES)) {
89 21 : info.watcherNodes = js.at(ADUMP_DUMP_WATCHER_NODES).get<std::vector<std::string>>();
90 21 : info.isWatcherNodes = true;
91 : }
92 141 : if (JsonParser::ContainKey(js, ADUMP_DUMP_OPTYPE_BLACKLIST)) {
93 24 : info.optypeBlacklist = js.at(ADUMP_DUMP_OPTYPE_BLACKLIST).get<std::vector<AclDumpBlacklist>>();
94 : }
95 141 : if (JsonParser::ContainKey(js, ADUMP_DUMP_OPNAME_BLACKLIST)) {
96 12 : info.opnameBlacklist = js.at(ADUMP_DUMP_OPNAME_BLACKLIST).get<std::vector<AclDumpBlacklist>>();
97 : }
98 141 : if (JsonParser::ContainKey(js, ADUMP_DUMP_OPNAME_RANGE)) {
99 27 : info.dumpOpNameRanges = js.at(ADUMP_DUMP_OPNAME_RANGE).get<std::vector<OpNameRange>>();
100 : }
101 141 : }
102 :
103 2094 : std::string DumpConfigConverter::BuildIndexedPath(const std::string& base, size_t index) const
104 : {
105 2094 : return base + "[" + std::to_string(index) + "]";
106 : }
107 :
108 2769 : std::string DumpConfigConverter::BuildPath(const std::string& base, const std::string& key) const
109 : {
110 2769 : return base.empty() ? key : base + "." + key;
111 : }
112 :
113 813 : bool DumpConfigConverter::CheckDumpFieldTypes() const
114 : {
115 : const std::vector<std::string> stringFields = {ADUMP_DUMP_PATH, ADUMP_DUMP_MODE, ADUMP_DUMP_OP_SWITCH,
116 : ADUMP_DUMP_LEVEL, ADUMP_DUMP_SCENE, ADUMP_DUMP_DATA,
117 8943 : ADUMP_DUMP_DEBUG, ADUMP_DUMP_KERNEL_DATA, ADUMP_DUMP_STEP};
118 :
119 15672 : for (const auto& field : stringFields) {
120 14106 : if (!CheckStringField(dumpJs_, field, "")) {
121 60 : return false;
122 : }
123 : }
124 :
125 753 : if (JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_STATS)) {
126 84 : std::string fieldPath = ADUMP_DUMP_STATS;
127 84 : if (!CheckArrayOfString(dumpJs_.at(ADUMP_DUMP_STATS), fieldPath)) {
128 24 : return false;
129 : }
130 84 : }
131 :
132 729 : if (JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_LIST)) {
133 279 : if (!CheckDumpListFieldTypes()) {
134 120 : return false;
135 : }
136 : }
137 :
138 609 : return true;
139 1626 : }
140 :
141 8043 : bool DumpConfigConverter::CheckStringField(
142 : const nlohmann::json& js, const std::string& key, const std::string& basePath) const
143 : {
144 8043 : if (!JsonParser::ContainKey(js, key)) {
145 5580 : return true;
146 : }
147 :
148 2463 : std::string fieldPath = BuildPath(basePath, key);
149 2463 : if (!js.at(key).is_string()) {
150 1218 : REPORT_EP0001_ITEM_ERROR("CheckFieldValue", fieldPath, ADUMP_REASON_ITEM_MUST_BE_STRING, configPath_);
151 87 : return false;
152 : }
153 :
154 2376 : return true;
155 2637 : }
156 :
157 609 : bool DumpConfigConverter::CheckDumpFieldValues() const
158 : {
159 : const std::vector<std::string> singleValueFields = {ADUMP_DUMP_MODE, ADUMP_DUMP_DATA, ADUMP_DUMP_OP_SWITCH,
160 4872 : ADUMP_DUMP_LEVEL, ADUMP_DUMP_DEBUG, ADUMP_DUMP_SCENE};
161 :
162 4698 : for (const auto& field : singleValueFields) {
163 3528 : if (!CheckFieldValue(dumpJs_, field)) {
164 48 : return false;
165 : }
166 : }
167 :
168 561 : std::string kernelData;
169 561 : if (JsonParser::GetStringIfExist(dumpJs_, ADUMP_DUMP_KERNEL_DATA, kernelData)) {
170 63 : if (!CheckKernelDataValues(kernelData)) {
171 18 : return false;
172 : }
173 : }
174 :
175 543 : if (JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_STATS)) {
176 60 : if (!CheckDumpStatsValues()) {
177 6 : return false;
178 : }
179 : }
180 :
181 537 : return true;
182 1218 : }
183 :
184 3528 : bool DumpConfigConverter::CheckFieldValue(const nlohmann::json& js, const std::string& key) const
185 : {
186 3528 : std::string value;
187 3528 : if (JsonParser::GetStringIfExist(js, key, value)) {
188 783 : return IsValueValid(key, value);
189 : }
190 :
191 2745 : return true;
192 3528 : }
193 :
194 63 : bool DumpConfigConverter::CheckKernelDataValues(const std::string& kernelData) const
195 : {
196 63 : std::vector<std::string> dfxTypes;
197 63 : Split(kernelData, ',', dfxTypes);
198 :
199 276 : for (const auto& dfxType : dfxTypes) {
200 168 : if (!IsValueValid(ADUMP_DUMP_KERNEL_DATA, dfxType)) {
201 18 : return false;
202 : }
203 : }
204 :
205 45 : return true;
206 63 : }
207 :
208 60 : bool DumpConfigConverter::CheckDumpStatsValues() const
209 : {
210 60 : const auto& statsArray = dumpJs_.at(ADUMP_DUMP_STATS);
211 :
212 123 : for (size_t i = 0; i < statsArray.size(); ++i) {
213 69 : std::string statValue = statsArray[i].get<std::string>();
214 69 : std::string indexedPath = BuildIndexedPath(ADUMP_DUMP_STATS, i);
215 69 : if (!IsValueValid(ADUMP_DUMP_STATS, statValue, indexedPath)) {
216 6 : return false;
217 : }
218 75 : }
219 :
220 54 : return true;
221 : }
222 :
223 279 : bool DumpConfigConverter::CheckDumpListFieldTypes() const
224 : {
225 279 : const auto& dumpListArray = dumpJs_.at(ADUMP_DUMP_LIST);
226 279 : if (!dumpListArray.is_array()) {
227 168 : REPORT_EP0001_ITEM_ERROR(
228 : "CheckDumpListFieldTypes", ADUMP_DUMP_LIST, ADUMP_REASON_ITEM_MUST_BE_ARRAY, configPath_);
229 12 : return false;
230 : }
231 :
232 429 : for (size_t i = 0; i < dumpListArray.size(); ++i) {
233 270 : std::string itemPath = BuildIndexedPath(ADUMP_DUMP_LIST, i);
234 270 : if (!dumpListArray[i].is_object()) {
235 168 : REPORT_EP0001_ITEM_ERROR(
236 : "CheckDumpListFieldTypes", itemPath, ADUMP_REASON_ITEM_MUST_BE_OBJECT, configPath_);
237 12 : return false;
238 : }
239 258 : if (!CheckDumpListItemFieldTypes(dumpListArray[i], itemPath)) {
240 96 : return false;
241 : }
242 270 : }
243 159 : return true;
244 48 : }
245 :
246 258 : bool DumpConfigConverter::CheckDumpListItemFieldTypes(const nlohmann::json& item, const std::string& basePath) const
247 : {
248 258 : if (!CheckStringField(item, ADUMP_DUMP_MODEL_NAME, basePath)) {
249 9 : return false;
250 : }
251 :
252 249 : if (JsonParser::ContainKey(item, ADUMP_DUMP_LAYER)) {
253 33 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_LAYER);
254 33 : if (!CheckArrayOfString(item.at(ADUMP_DUMP_LAYER), fieldPath)) {
255 15 : return false;
256 : }
257 33 : }
258 :
259 234 : if (JsonParser::ContainKey(item, ADUMP_DUMP_WATCHER_NODES)) {
260 36 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_WATCHER_NODES);
261 36 : if (!CheckArrayOfString(item.at(ADUMP_DUMP_WATCHER_NODES), fieldPath)) {
262 12 : return false;
263 : }
264 36 : }
265 :
266 222 : if (JsonParser::ContainKey(item, ADUMP_DUMP_OPTYPE_BLACKLIST)) {
267 51 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_OPTYPE_BLACKLIST);
268 51 : if (!CheckBlacklistFieldTypes(item.at(ADUMP_DUMP_OPTYPE_BLACKLIST), fieldPath)) {
269 27 : return false;
270 : }
271 51 : }
272 :
273 195 : if (JsonParser::ContainKey(item, ADUMP_DUMP_OPNAME_BLACKLIST)) {
274 21 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_OPNAME_BLACKLIST);
275 21 : if (!CheckBlacklistFieldTypes(item.at(ADUMP_DUMP_OPNAME_BLACKLIST), fieldPath)) {
276 9 : return false;
277 : }
278 21 : }
279 :
280 186 : if (JsonParser::ContainKey(item, ADUMP_DUMP_OPNAME_RANGE)) {
281 51 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_OPNAME_RANGE);
282 51 : if (!CheckOpnameRangeFieldTypes(item.at(ADUMP_DUMP_OPNAME_RANGE), fieldPath)) {
283 24 : return false;
284 : }
285 51 : }
286 :
287 162 : return true;
288 : }
289 :
290 306 : bool DumpConfigConverter::CheckArrayOfType(
291 : const nlohmann::json& arr, const std::string& basePath, bool isStringType) const
292 : {
293 306 : if (!arr.is_array()) {
294 630 : REPORT_EP0001_ITEM_ERROR("CheckArrayOfType", basePath, ADUMP_REASON_ITEM_MUST_BE_ARRAY, configPath_);
295 45 : return false;
296 : }
297 :
298 1089 : for (size_t i = 0; i < arr.size(); ++i) {
299 876 : std::string itemPath = BuildIndexedPath(basePath, i);
300 876 : bool typeMatch = isStringType ? arr[i].is_string() : arr[i].is_object();
301 876 : if (!typeMatch) {
302 : const std::string& reason =
303 48 : isStringType ? ADUMP_REASON_MEMBER_MUST_BE_STRING : ADUMP_REASON_MEMBER_MUST_BE_OBJECT;
304 624 : REPORT_EP0001_ITEM_ERROR("CheckArrayOfType", itemPath, reason, configPath_);
305 48 : return false;
306 48 : }
307 876 : }
308 213 : return true;
309 186 : }
310 :
311 183 : bool DumpConfigConverter::CheckArrayOfString(const nlohmann::json& arr, const std::string& basePath) const
312 : {
313 183 : return CheckArrayOfType(arr, basePath, true);
314 : }
315 :
316 123 : bool DumpConfigConverter::CheckArrayOfObject(const nlohmann::json& arr, const std::string& basePath) const
317 : {
318 123 : return CheckArrayOfType(arr, basePath, false);
319 : }
320 :
321 72 : bool DumpConfigConverter::CheckBlacklistFieldTypes(
322 : const nlohmann::json& blacklistArray, const std::string& basePath) const
323 : {
324 72 : if (!CheckArrayOfObject(blacklistArray, basePath)) {
325 15 : return false;
326 : }
327 :
328 693 : for (size_t i = 0; i < blacklistArray.size(); ++i) {
329 657 : std::string itemPath = BuildIndexedPath(basePath, i);
330 657 : if (!CheckBlacklistItemFieldTypes(blacklistArray[i], itemPath)) {
331 21 : return false;
332 : }
333 657 : }
334 36 : return true;
335 : }
336 :
337 657 : bool DumpConfigConverter::CheckBlacklistItemFieldTypes(
338 : const nlohmann::json& blacklistItem, const std::string& basePath) const
339 : {
340 657 : if (!CheckStringField(blacklistItem, ADUMP_DUMP_BLACKLIST_NAME, basePath)) {
341 6 : return false;
342 : }
343 :
344 651 : if (JsonParser::ContainKey(blacklistItem, ADUMP_DUMP_BLACKLIST_POS)) {
345 30 : std::string fieldPath = BuildPath(basePath, ADUMP_DUMP_BLACKLIST_POS);
346 30 : if (!CheckArrayOfString(blacklistItem.at(ADUMP_DUMP_BLACKLIST_POS), fieldPath)) {
347 15 : return false;
348 : }
349 30 : }
350 :
351 636 : return true;
352 : }
353 :
354 51 : bool DumpConfigConverter::CheckOpnameRangeFieldTypes(
355 : const nlohmann::json& rangeArray, const std::string& basePath) const
356 : {
357 51 : if (!CheckArrayOfObject(rangeArray, basePath)) {
358 12 : return false;
359 : }
360 :
361 66 : for (size_t i = 0; i < rangeArray.size(); ++i) {
362 39 : std::string itemPath = BuildIndexedPath(basePath, i);
363 39 : if (!CheckOpnameRangeItemFieldTypes(rangeArray[i], itemPath)) {
364 12 : return false;
365 : }
366 39 : }
367 27 : return true;
368 : }
369 :
370 39 : bool DumpConfigConverter::CheckOpnameRangeItemFieldTypes(
371 : const nlohmann::json& rangeItem, const std::string& basePath) const
372 : {
373 39 : if (!CheckStringField(rangeItem, ADUMP_DUMP_OPNAME_RANGE_BEGIN, basePath)) {
374 3 : return false;
375 : }
376 :
377 36 : if (!CheckStringField(rangeItem, ADUMP_DUMP_OPNAME_RANGE_END, basePath)) {
378 9 : return false;
379 : }
380 :
381 27 : return true;
382 : }
383 :
384 537 : bool DumpConfigConverter::CheckDumpConstraints() const
385 : {
386 537 : std::string dumpScene;
387 537 : if (!CheckDumpScene(dumpScene)) {
388 18 : return false;
389 : }
390 :
391 519 : if (!dumpScene.empty() && (dumpScene != ADUMP_DUMP_WATCHER)) {
392 45 : return true;
393 : }
394 :
395 474 : if (!CheckDumpPath()) {
396 60 : return false;
397 : }
398 :
399 414 : std::string dumpDebug;
400 414 : if (!CheckDumpDebug(dumpDebug)) {
401 6 : return false;
402 : }
403 :
404 408 : if (dumpDebug == ADUMP_DUMP_STATUS_SWITCH_ON) {
405 9 : return true;
406 : }
407 :
408 399 : if (!CheckDumpStep()) {
409 39 : return false;
410 : }
411 :
412 360 : std::string dumpLevel = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_LEVEL, ADUMP_DUMP_LEVEL_ALL);
413 360 : if (!CheckDumplist(dumpLevel)) {
414 63 : return false;
415 : }
416 :
417 297 : if (!CheckDumpStats()) {
418 18 : return false;
419 : };
420 279 : return true;
421 537 : }
422 :
423 537 : bool DumpConfigConverter::CheckDumpScene(std::string& dumpScene) const
424 : {
425 537 : if (!JsonParser::GetStringIfExist(dumpJs_, ADUMP_DUMP_SCENE, dumpScene)) {
426 441 : return true;
427 : }
428 :
429 96 : if (dumpScene == ADUMP_DUMP_WATCHER) {
430 39 : return CheckWatcherDumpScene(dumpScene);
431 : } else {
432 57 : return CheckExceptionDumpScene(dumpScene);
433 : }
434 : }
435 :
436 39 : bool DumpConfigConverter::CheckWatcherDumpScene(const std::string& dumpScene) const
437 : {
438 39 : if (ConflictWith(ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_ON)) {
439 30 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
440 : "CheckWatcherDumpScene", ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_SCENE, dumpScene,
441 : configPath_);
442 3 : return false;
443 : }
444 36 : if (ConflictWith(ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON)) {
445 30 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
446 : "CheckWatcherDumpScene", ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_SCENE, dumpScene,
447 : configPath_);
448 3 : return false;
449 : }
450 :
451 33 : std::string dumpMode = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_MODE, ADUMP_DUMP_MODE_OUTPUT);
452 33 : if (dumpMode != ADUMP_DUMP_MODE_OUTPUT) {
453 0 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
454 : "CheckWatcherDumpScene", ADUMP_DUMP_MODE, dumpMode, ADUMP_DUMP_SCENE, dumpScene, configPath_);
455 0 : return false;
456 : }
457 :
458 33 : return true;
459 45 : }
460 :
461 57 : bool DumpConfigConverter::CheckExceptionDumpScene(const std::string& dumpScene) const
462 : {
463 57 : if (ConflictWith(ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_ON)) {
464 60 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
465 : "CheckExceptionDumpScene", ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_SCENE, dumpScene,
466 : configPath_);
467 6 : return false;
468 : }
469 51 : if (ConflictWith(ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON)) {
470 60 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
471 : "CheckExceptionDumpScene", ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_SCENE, dumpScene,
472 : configPath_);
473 6 : return false;
474 : }
475 :
476 45 : return true;
477 24 : }
478 :
479 414 : bool DumpConfigConverter::CheckDumpDebug(std::string& dumpDebug) const
480 : {
481 414 : dumpDebug = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_DEBUG, ADUMP_DUMP_STATUS_SWITCH_OFF);
482 :
483 414 : if (dumpDebug == ADUMP_DUMP_STATUS_SWITCH_OFF) {
484 399 : return true;
485 : }
486 :
487 15 : if (ConflictWith(ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON)) {
488 60 : REPORT_EP0005_ITEM_VALUE_CANNOT_SET_WHEN_ITEM_VALUE(
489 : "CheckDumpDebug", ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_ON, ADUMP_DUMP_DEBUG,
490 : ADUMP_DUMP_STATUS_SWITCH_ON, configPath_);
491 6 : return false;
492 : }
493 :
494 9 : return true;
495 12 : }
496 :
497 333 : void DumpConfigConverter::ParseDfxTypesFromJson(const RawDumpConfig& rawDumpConfig, DumpDfxConfig& dumpDfxConfig) const
498 : {
499 333 : dumpDfxConfig.dumpPath = rawDumpConfig.dumpPath;
500 333 : if (JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_KERNEL_DATA)) {
501 45 : std::string kernelDumpData = JsonParser::GetCfgStrByKey(dumpJs_, ADUMP_DUMP_KERNEL_DATA);
502 45 : Split(kernelDumpData, ',', dumpDfxConfig.dfxTypes);
503 45 : }
504 333 : }
505 :
506 294 : void DumpConfigConverter::EnsureDefaultDfxType(const DumpType& dumpType, DumpDfxConfig& dumpDfxConfig) const
507 : {
508 294 : if (dumpType == DumpType::OPERATOR && dumpDfxConfig.dfxTypes.empty()) {
509 183 : dumpDfxConfig.dfxTypes.push_back(ADUMP_DUMP_KERNEL_DATA_ALL);
510 : }
511 294 : }
512 :
513 474 : bool DumpConfigConverter::CheckDumpPath() const
514 : {
515 474 : if (!JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_PATH)) {
516 84 : REPORT_EP0001_ITEM_ERROR("CheckDumpPath", ADUMP_DUMP_PATH, ADUMP_REASON_ITEM_NOT_FOUND, configPath_);
517 6 : return false;
518 : }
519 :
520 468 : std::string dumpPath = JsonParser::GetCfgStrByKey(dumpJs_, ADUMP_DUMP_PATH);
521 468 : if (dumpPath.empty()) {
522 84 : REPORT_EP0001_ITEM_ERROR("CheckDumpPath", ADUMP_DUMP_PATH, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
523 6 : return false;
524 : }
525 :
526 462 : if (dumpPath.length() > static_cast<size_t>(MAX_DUMP_PATH_LENGTH)) {
527 51 : REPORT_EP0003_INVALID_VALUE(
528 : "CheckDumpPath", dumpPath, ADUMP_DUMP_PATH, ADUMP_REASON_PATH_LENGTH_EXCEED_LIMIT, configPath_);
529 3 : return false;
530 : }
531 :
532 459 : const size_t colonPos = dumpPath.find_first_of(":");
533 459 : bool isCutDumpPathFlag = CheckIpAddress(dumpPath);
534 459 : if (isCutDumpPathFlag) {
535 9 : std::string pathAfterIp = dumpPath.substr(colonPos + 1U);
536 9 : if (!FileUtils::IsValidDirChar(pathAfterIp)) {
537 51 : REPORT_EP0003_INVALID_VALUE(
538 : "CheckDumpPath", pathAfterIp, ADUMP_DUMP_PATH, ADUMP_REASON_VALUE_INVALID_CHARS, configPath_);
539 3 : return false;
540 : }
541 9 : } else {
542 450 : Path path(dumpPath);
543 450 : if (!path.RealPath()) {
544 561 : REPORT_EP0003_INVALID_VALUE(
545 : "CheckDumpPath", dumpPath, ADUMP_DUMP_PATH, ADUMP_REASON_PATH_NOT_EXIST, configPath_);
546 33 : return false;
547 : }
548 417 : if (!path.IsDirectory()) {
549 102 : REPORT_EP0003_INVALID_VALUE(
550 : "CheckDumpPath", dumpPath, ADUMP_DUMP_PATH, ADUMP_REASON_PATH_NOT_DIRECTORY, configPath_);
551 6 : return false;
552 : }
553 411 : constexpr uint32_t accessMode = static_cast<uint32_t>(M_R_OK) | static_cast<uint32_t>(M_W_OK);
554 411 : if (!path.Asccess(accessMode)) {
555 51 : REPORT_EP0003_INVALID_VALUE(
556 : "CheckDumpPath", dumpPath, ADUMP_DUMP_PATH, ADUMP_REASON_PATH_NO_PERMISSION, configPath_);
557 3 : return false;
558 : }
559 450 : }
560 414 : return true;
561 588 : }
562 :
563 399 : bool DumpConfigConverter::CheckDumpStep() const
564 : {
565 399 : std::string dumpStep = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_STEP, "");
566 399 : if (dumpStep.empty()) {
567 348 : return true;
568 : }
569 :
570 51 : std::vector<std::string> matchVecs;
571 51 : Split(dumpStep, '|', matchVecs);
572 51 : if (matchVecs.size() > MAX_DUMP_STEP_INTERVAL_COUNT) {
573 153 : REPORT_EP0003_INVALID_VALUE(
574 : "CheckDumpStep", dumpStep, ADUMP_DUMP_STEP, ADUMP_REASON_DUMP_STEP_EXCEED_LIMIT, configPath_);
575 9 : return false;
576 : }
577 :
578 60 : for (size_t i = 0U; i < matchVecs.size(); ++i) {
579 48 : std::vector<std::string> steps;
580 48 : Split(matchVecs[i], '-', steps);
581 48 : if (steps.size() > 2U) {
582 9 : std::string indexedPath = BuildIndexedPath(ADUMP_DUMP_STEP, i);
583 153 : REPORT_EP0003_INVALID_VALUE(
584 : "CheckDumpStep", matchVecs[i], indexedPath, ADUMP_REASON_DUMP_STEP_INVALID_FORMAT, configPath_);
585 9 : return false;
586 9 : }
587 :
588 123 : for (const auto& step : steps) {
589 57 : if (!IsDigit(step)) {
590 12 : std::string indexedPath = BuildIndexedPath(ADUMP_DUMP_STEP, i);
591 204 : REPORT_EP0003_INVALID_VALUE(
592 : "CheckDumpStep", matchVecs[i], indexedPath, ADUMP_REASON_DUMP_STEP_NOT_DIGIT, configPath_);
593 12 : return false;
594 12 : }
595 : }
596 :
597 54 : if (std::strtol(steps[0U].c_str(), nullptr, DECIMAL) >
598 27 : std::strtol(steps[steps.size() - 1U].c_str(), nullptr, DECIMAL)) {
599 9 : std::string indexedPath = BuildIndexedPath(ADUMP_DUMP_STEP, i);
600 153 : REPORT_EP0003_INVALID_VALUE(
601 : "CheckDumpStep", matchVecs[i], indexedPath, ADUMP_REASON_DUMP_STEP_INVALID_RANGE, configPath_);
602 9 : return false;
603 9 : }
604 48 : }
605 12 : return true;
606 477 : }
607 :
608 360 : bool DumpConfigConverter::CheckDumplist(const std::string& dumpLevel) const
609 : {
610 360 : std::vector<AclModelDumpConfig> dumpList;
611 360 : if (JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_LIST)) {
612 141 : dumpList = dumpJs_.at(ADUMP_DUMP_LIST).get<std::vector<AclModelDumpConfig>>();
613 : }
614 360 : if (dumpList.empty()) {
615 219 : return true;
616 : }
617 :
618 : std::string dumpOpSwitch =
619 141 : JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_OP_SWITCH, ADUMP_DUMP_STATUS_SWITCH_OFF);
620 141 : std::string dumpScene = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_SCENE, "");
621 141 : bool isSwitchOff = (dumpOpSwitch == ADUMP_DUMP_STATUS_SWITCH_OFF);
622 :
623 141 : return CheckDumpListItems(dumpList, dumpLevel, dumpScene, isSwitchOff);
624 360 : }
625 :
626 114 : bool DumpConfigConverter::CheckModelNameAndLayer(const AclModelDumpConfig& item, const std::string& itemPath) const
627 : {
628 114 : if (item.isModelName && item.modelName.empty()) {
629 6 : std::string modelPath = BuildPath(itemPath, ADUMP_DUMP_MODEL_NAME);
630 84 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", modelPath, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
631 6 : return false;
632 6 : }
633 :
634 108 : if (item.isLayer && item.layer.empty()) {
635 3 : std::string layerPath = BuildPath(itemPath, ADUMP_DUMP_LAYER);
636 42 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", layerPath, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
637 3 : return false;
638 3 : }
639 :
640 105 : return true;
641 18 : }
642 132 : bool DumpConfigConverter::CheckWatcherSceneConstraints(
643 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpScene) const
644 : {
645 132 : if (dumpScene == ADUMP_DUMP_WATCHER) {
646 12 : if (!item.isWatcherNodes) {
647 0 : std::string watcherPath = BuildPath(itemPath, ADUMP_DUMP_WATCHER_NODES);
648 0 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", watcherPath, ADUMP_REASON_ITEM_NOT_FOUND, configPath_);
649 0 : return false;
650 0 : }
651 :
652 12 : if (item.watcherNodes.empty()) {
653 3 : std::string watcherPath = BuildPath(itemPath, ADUMP_DUMP_WATCHER_NODES);
654 42 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", watcherPath, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
655 3 : return false;
656 3 : }
657 :
658 9 : if (item.isModelName) {
659 3 : std::string modelPath = BuildPath(itemPath, ADUMP_DUMP_MODEL_NAME);
660 30 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_VALUE(
661 : "CheckDumpList", modelPath, ADUMP_DUMP_SCENE, ADUMP_DUMP_WATCHER, configPath_);
662 3 : return false;
663 3 : }
664 : } else {
665 120 : if (!item.watcherNodes.empty()) {
666 9 : std::string watcherPath = BuildPath(itemPath, ADUMP_DUMP_WATCHER_NODES);
667 90 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_NOT_VALUE(
668 : "CheckDumpList", watcherPath, ADUMP_DUMP_SCENE, ADUMP_DUMP_WATCHER, configPath_);
669 9 : return false;
670 9 : }
671 : }
672 :
673 117 : return true;
674 30 : }
675 :
676 117 : bool DumpConfigConverter::CheckBlacklistSize(const AclModelDumpConfig& item, const std::string& itemPath) const
677 : {
678 117 : if (item.optypeBlacklist.size() > MAX_BLACKLIST_SIZE) {
679 3 : std::string blacklistPath = BuildPath(itemPath, ADUMP_DUMP_OPTYPE_BLACKLIST);
680 51 : REPORT_EP0003_INVALID_VALUE(
681 : "CheckDumpList", std::to_string(item.optypeBlacklist.size()), blacklistPath,
682 : ADUMP_REASON_BLACKLIST_SIZE_EXCEED_LIMIT, configPath_);
683 3 : return false;
684 3 : }
685 :
686 114 : if (item.opnameBlacklist.size() > MAX_BLACKLIST_SIZE) {
687 3 : std::string blacklistPath = BuildPath(itemPath, ADUMP_DUMP_OPNAME_BLACKLIST);
688 51 : REPORT_EP0003_INVALID_VALUE(
689 : "CheckDumpList", std::to_string(item.opnameBlacklist.size()), blacklistPath,
690 : ADUMP_REASON_BLACKLIST_SIZE_EXCEED_LIMIT, configPath_);
691 3 : return false;
692 3 : }
693 :
694 111 : return true;
695 12 : }
696 111 : bool DumpConfigConverter::CheckBlacklistWithDumpLevel(
697 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpLevel) const
698 : {
699 111 : bool hasOptypeBlacklist = !item.optypeBlacklist.empty();
700 111 : bool hasOpnameBlacklist = !item.opnameBlacklist.empty();
701 :
702 111 : if (hasOptypeBlacklist && dumpLevel != ADUMP_DUMP_LEVEL_OP) {
703 9 : std::string blacklistPath = BuildPath(itemPath, ADUMP_DUMP_OPTYPE_BLACKLIST);
704 90 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_NOT_VALUE(
705 : "CheckDumpList", blacklistPath, ADUMP_DUMP_LEVEL, ADUMP_DUMP_LEVEL_OP, configPath_);
706 9 : return false;
707 9 : }
708 :
709 102 : if (hasOpnameBlacklist && dumpLevel != ADUMP_DUMP_LEVEL_OP) {
710 3 : std::string blacklistPath = BuildPath(itemPath, ADUMP_DUMP_OPNAME_BLACKLIST);
711 30 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_NOT_VALUE(
712 : "CheckDumpList", blacklistPath, ADUMP_DUMP_LEVEL, ADUMP_DUMP_LEVEL_OP, configPath_);
713 3 : return false;
714 3 : }
715 :
716 99 : return true;
717 24 : }
718 99 : bool DumpConfigConverter::CheckOpNameRange(
719 : const AclModelDumpConfig& item, const std::string& itemPath, const std::string& dumpLevel) const
720 : {
721 99 : if (item.dumpOpNameRanges.empty()) {
722 75 : return true;
723 : }
724 :
725 24 : std::string rangePath = BuildPath(itemPath, ADUMP_DUMP_OPNAME_RANGE);
726 :
727 24 : if (dumpLevel != ADUMP_DUMP_LEVEL_OP) {
728 60 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_NOT_VALUE(
729 : "CheckDumpList", rangePath, ADUMP_DUMP_LEVEL, ADUMP_DUMP_LEVEL_OP, configPath_);
730 6 : return false;
731 : }
732 :
733 18 : std::string modelPath = BuildPath(itemPath, ADUMP_DUMP_MODEL_NAME);
734 18 : if (!item.isModelName) {
735 84 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", modelPath, ADUMP_REASON_ITEM_NOT_FOUND, configPath_);
736 6 : return false;
737 : }
738 :
739 12 : if (item.modelName.empty()) {
740 0 : REPORT_EP0001_ITEM_ERROR("CheckDumpList", modelPath, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
741 0 : return false;
742 : }
743 :
744 15 : for (size_t j = 0U; j < item.dumpOpNameRanges.size(); ++j) {
745 12 : const auto& range = item.dumpOpNameRanges[j];
746 12 : std::string rangeIndexPath = BuildIndexedPath(rangePath, j);
747 :
748 12 : if (range.begin.empty() || range.end.empty()) {
749 9 : std::string rangeValue = "begin=" + range.begin + ", end=" + range.end;
750 153 : REPORT_EP0003_INVALID_VALUE(
751 : "CheckDumpList", rangeValue, rangeIndexPath, ADUMP_REASON_OPNAME_RANGE_INCOMPLETE, configPath_);
752 9 : return false;
753 9 : }
754 3 : IDE_LOGI("op name range begin[%s], op name range end[%s].", range.begin.c_str(), range.end.c_str());
755 12 : }
756 :
757 3 : return true;
758 66 : }
759 :
760 141 : bool DumpConfigConverter::CheckDumpListItems(
761 : const std::vector<AclModelDumpConfig>& dumpList, const std::string& dumpLevel, const std::string& dumpScene,
762 : bool isSwitchOff) const
763 : {
764 219 : for (size_t i = 0U; i < dumpList.size(); ++i) {
765 141 : const auto& item = dumpList[i];
766 141 : std::string itemPath = BuildIndexedPath(ADUMP_DUMP_LIST, i);
767 :
768 141 : if (isSwitchOff && !CheckModelNameAndLayer(item, itemPath)) {
769 9 : return false;
770 : }
771 :
772 132 : if (!CheckWatcherSceneConstraints(item, itemPath, dumpScene)) {
773 15 : return false;
774 : }
775 :
776 117 : if (!CheckBlacklistSize(item, itemPath)) {
777 6 : return false;
778 : }
779 :
780 111 : if (!CheckBlacklistWithDumpLevel(item, itemPath, dumpLevel)) {
781 12 : return false;
782 : }
783 :
784 99 : if (!CheckOpNameRange(item, itemPath, dumpLevel)) {
785 21 : return false;
786 : }
787 141 : }
788 :
789 78 : IDE_LOGI("end to check the validity of dump_list and dump_op_switch field.");
790 78 : return true;
791 : }
792 :
793 207 : void DumpConfigConverter::Split(const std::string& str, const char delim, std::vector<std::string>& elems) const
794 : {
795 207 : elems.clear();
796 207 : if (str.empty()) {
797 3 : elems.emplace_back("");
798 3 : return;
799 : }
800 :
801 204 : std::stringstream ss(str);
802 204 : std::string item;
803 :
804 1665 : while (getline(ss, item, delim)) {
805 1461 : elems.push_back(item);
806 : }
807 :
808 204 : const auto strSize = str.size();
809 204 : if ((strSize > 0U) && (str[strSize - 1U] == delim)) {
810 3 : elems.emplace_back("");
811 : }
812 204 : }
813 :
814 57 : bool DumpConfigConverter::IsDigit(const std::string& str) const
815 : {
816 57 : if (str.empty()) {
817 3 : return false;
818 : }
819 :
820 168 : for (const char& c : str) {
821 69 : if (isdigit(static_cast<int32_t>(c)) == 0) {
822 9 : return false;
823 : }
824 : }
825 45 : return true;
826 : }
827 :
828 297 : bool DumpConfigConverter::CheckDumpStats() const
829 : {
830 297 : if (!JsonParser::ContainKey(dumpJs_, ADUMP_DUMP_STATS)) {
831 246 : return true;
832 : }
833 51 : std::vector<std::string> dumpStats = dumpJs_.at(ADUMP_DUMP_STATS).get<std::vector<std::string>>();
834 51 : if (dumpStats.empty()) {
835 126 : REPORT_EP0001_ITEM_ERROR("CheckDumpStats", ADUMP_DUMP_STATS, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
836 9 : return false;
837 : }
838 :
839 42 : std::string dumpData = JsonParser::GetStringOrDefault(dumpJs_, ADUMP_DUMP_DATA, ADUMP_DUMP_DATA_TENSOR);
840 42 : if (dumpData == ADUMP_DUMP_DATA_TENSOR) {
841 90 : REPORT_EP0005_ITEM_CANNOT_SET_WHEN_ITEM_VALUE(
842 : "CheckDumpStats", ADUMP_DUMP_STATS, ADUMP_DUMP_DATA, ADUMP_DUMP_DATA_TENSOR, configPath_);
843 9 : return false;
844 : }
845 33 : return true;
846 87 : }
847 :
848 936 : bool DumpConfigConverter::IsValueValid(
849 : const std::string key, const std::string value, const std::string& errorPath) const
850 : {
851 936 : std::string reportPath = errorPath.empty() ? key : errorPath;
852 :
853 936 : if (value.empty()) {
854 84 : REPORT_EP0001_ITEM_ERROR("CheckItemValue", reportPath, ADUMP_REASON_ITEM_VALUE_EMPTY, configPath_);
855 6 : return false;
856 : }
857 :
858 930 : if (dumpValidOptions.at(key).find(value) != dumpValidOptions.at(key).end()) {
859 864 : return true;
860 : }
861 :
862 66 : std::string validOptionStr = TransOptionsToStr(dumpValidOptions.at(key));
863 1056 : REPORT_EP0002_ENUM_VALUE_INVALID("CheckItemValue", value, reportPath, validOptionStr, configPath_);
864 66 : return false;
865 1080 : }
866 :
867 69 : std::string DumpConfigConverter::TransOptionsToStr(const std::set<std::string>& options)
868 : {
869 69 : std::string optionStr;
870 336 : for (auto option : options) {
871 267 : optionStr += option + "/";
872 267 : }
873 69 : optionStr.pop_back();
874 69 : return optionStr;
875 0 : }
876 :
877 198 : bool DumpConfigConverter::ConflictWith(const std::string key, const std::string value) const
878 : {
879 198 : std::string itemValue;
880 198 : if (!JsonParser::GetStringIfExist(dumpJs_, key, itemValue)) {
881 174 : return false;
882 : }
883 24 : return itemValue == value;
884 198 : }
885 :
886 459 : bool DumpConfigConverter::CheckIpAddress(const std::string dumpPath) const
887 : {
888 : // check the valid of ipAddress in dump_path
889 459 : const size_t colonPos = dumpPath.find_first_of(":");
890 459 : if (colonPos != std::string::npos) {
891 27 : IDE_LOGI("dump_path field contains ip address.");
892 27 : if ((colonPos + 1U) == dumpPath.size()) {
893 51 : REPORT_EP0003_INVALID_VALUE(
894 : "CheckIpAddress", dumpPath, ADUMP_DUMP_PATH, ADUMP_REASON_PATH_INVALID, configPath_);
895 24 : return false;
896 : }
897 24 : const std::string ipAddress = dumpPath.substr(0U, colonPos);
898 24 : const std::vector<std::string> ipRet = StrUtils::Split(ipAddress, ".");
899 24 : if (ipRet.size() == static_cast<size_t>(MAX_IPV4_ADDRESS_LENGTH)) {
900 : try {
901 99 : for (const std::string& ret : ipRet) {
902 69 : const int32_t val = std::stoi(ret);
903 63 : if ((val < 0) || (val > MAX_IPV4_ADDRESS_VALUE)) {
904 6 : IDE_LOGW("ip address[%s] is invalid in dump_path field", ipAddress.c_str());
905 6 : return false;
906 : }
907 : }
908 6 : } catch (...) {
909 6 : IDE_LOGW("ip address[%s] can not convert to digital in dump_path field", ipAddress.c_str());
910 6 : return false;
911 6 : }
912 9 : IDE_LOGI("ip address[%s] is valid in dump_path field.", ipAddress.c_str());
913 9 : return true;
914 : }
915 45 : }
916 :
917 435 : IDE_LOGD("the dump_path field does not contain ip address or it does not comply with the ipv4 rule.");
918 435 : return false;
919 6 : }
920 :
921 294 : DumpConfig DumpConfigConverter::ConvertDumpConfig(const RawDumpConfig& rawDumpConfig) const
922 : {
923 294 : DumpConfig dumpConfig;
924 294 : dumpConfig.dumpStatus = ADUMP_DUMP_STATUS_SWITCH_ON;
925 294 : dumpConfig.dumpMode = rawDumpConfig.dumpMode;
926 294 : dumpConfig.dumpPath = rawDumpConfig.dumpPath;
927 294 : dumpConfig.dumpData = rawDumpConfig.dumpData;
928 294 : if (!rawDumpConfig.dumpScene.empty() && rawDumpConfig.dumpScene != ADUMP_DUMP_WATCHER) {
929 : // 优先级:ASCEND_DUMP_PATH > ASCEND_WORK_PATH > 配置文件 > "./"
930 45 : std::string envDumpPath;
931 45 : if (GetEnvDumpPath(ADUMP_ENV_ASCEND_WORK_PATH, envDumpPath)) {
932 3 : dumpConfig.dumpPath = envDumpPath;
933 : }
934 45 : if (GetEnvDumpPath(ADUMP_ENV_ASCEND_DUMP_PATH, envDumpPath)) {
935 3 : dumpConfig.dumpPath = envDumpPath;
936 : }
937 45 : }
938 294 : if (rawDumpConfig.dumpLevel == ADUMP_DUMP_LEVEL_OP) {
939 30 : dumpConfig.dumpSwitch = OPERATOR_OP_DUMP;
940 264 : } else if (rawDumpConfig.dumpLevel == ADUMP_DUMP_LEVEL_KERNEL) {
941 9 : dumpConfig.dumpSwitch = OPERATOR_KERNEL_DUMP;
942 : } else {
943 255 : dumpConfig.dumpSwitch = OPERATOR_OP_DUMP | OPERATOR_KERNEL_DUMP;
944 : }
945 690 : for (auto dumpStat : rawDumpConfig.dumpStats) {
946 51 : dumpConfig.dumpStatsItem.emplace_back(dumpStat);
947 51 : }
948 294 : return dumpConfig;
949 0 : }
950 :
951 294 : DumpType DumpConfigConverter::ConvertDumpType(const RawDumpConfig& rawDumpConfig) const
952 : {
953 : DumpType dumpType;
954 294 : if (ConvertDumpScene(rawDumpConfig.dumpScene, dumpType)) {
955 45 : return dumpType;
956 249 : } else if (rawDumpConfig.dumpDebug == ADUMP_DUMP_STATUS_SWITCH_ON) {
957 9 : return DumpType::OP_OVERFLOW;
958 : } else {
959 240 : return DumpType::OPERATOR;
960 : }
961 : }
962 :
963 330 : bool DumpConfigConverter::ConvertDumpScene(const std::string dumpScene, DumpType& dumpType)
964 : {
965 330 : if (dumpScene == ADUMP_DUMP_EXCEPTION_AIC_ERR_BRIEF || dumpScene == ADUMP_DUMP_LITE_EXCEPTION) {
966 48 : dumpType = DumpType::ARGS_EXCEPTION;
967 282 : } else if (dumpScene == ADUMP_DUMP_EXCEPTION_AIC_ERR_NORM) {
968 6 : dumpType = DumpType::EXCEPTION;
969 276 : } else if (dumpScene == ADUMP_DUMP_EXCEPTION_AIC_ERR_DETAIL) {
970 6 : dumpType = DumpType::AIC_ERR_DETAIL_DUMP;
971 : } else {
972 270 : return false;
973 : }
974 60 : return true;
975 : }
976 :
977 417 : std::string DumpConfigConverter::DumpTypeToStr(const DumpType dumpType)
978 : {
979 417 : if (dumpType == DumpType::ARGS_EXCEPTION) {
980 50 : return ADUMP_DUMP_EXCEPTION_AIC_ERR_BRIEF;
981 367 : } else if (dumpType == DumpType::EXCEPTION) {
982 6 : return ADUMP_DUMP_EXCEPTION_AIC_ERR_NORM;
983 361 : } else if (dumpType == DumpType::AIC_ERR_DETAIL_DUMP) {
984 4 : return ADUMP_DUMP_EXCEPTION_AIC_ERR_DETAIL;
985 357 : } else if (dumpType == DumpType::OP_OVERFLOW) {
986 18 : return ADUMP_DUMP_DATA_OVERFLOW;
987 339 : } else if (dumpType == DumpType::OPERATOR) {
988 336 : return ADUMP_DUMP_DATA_TENSOR;
989 : } else {
990 6 : return "unknown";
991 : }
992 : }
993 :
994 333 : bool DumpConfigConverter::NeedDump(const RawDumpConfig& rawDumpConfig) const
995 : {
996 333 : if (dumpJs_.contains("dump_list") && dumpJs_["dump_list"].is_array() && !dumpJs_["dump_list"].empty()) {
997 78 : IDE_LOGI("Dump list size: %zu", dumpJs_["dump_list"].size());
998 78 : return true;
999 : }
1000 255 : IDE_LOGI("Dump list is not an array or does not exist.");
1001 366 : if ((rawDumpConfig.dumpOpSwitch == ADUMP_DUMP_STATUS_SWITCH_ON) ||
1002 366 : (rawDumpConfig.dumpDebug == ADUMP_DUMP_STATUS_SWITCH_ON) || (!rawDumpConfig.dumpScene.empty())) {
1003 216 : return true;
1004 : }
1005 39 : IDE_LOGI("No dump config need to be set.");
1006 39 : return false;
1007 : }
1008 :
1009 859 : int32_t DumpConfigConverter::Convert(
1010 : DumpType& dumpType, DumpConfig& dumpConfig, bool& needDump, DumpDfxConfig& dumpDfxConfig)
1011 : {
1012 859 : nlohmann::json js;
1013 859 : std::string errMsg;
1014 859 : needDump = false;
1015 859 : int32_t ret = JsonParser::ParseJsonFromMemory(dumpConfigData_, dumpConfigSize_, js, errMsg);
1016 859 : if (ret != ADUMP_SUCCESS) {
1017 210 : REPORT_EP0004_PARSE_ERROR("Convert", errMsg, configPath_);
1018 21 : return ADUMP_FAILED;
1019 : }
1020 : try {
1021 838 : if (!JsonParser::ContainKey(js, ADUMP_DUMP)) {
1022 544 : return ADUMP_SUCCESS;
1023 : }
1024 831 : dumpJs_ = js.at(ADUMP_DUMP);
1025 831 : if (!dumpJs_.is_object()) {
1026 210 : REPORT_EP0001_ITEM_ERROR("Convert", ADUMP_DUMP, ADUMP_REASON_ITEM_MUST_BE_OBJECT, configPath_);
1027 15 : return ADUMP_FAILED;
1028 : }
1029 816 : if (dumpJs_.empty()) {
1030 3 : return ADUMP_SUCCESS;
1031 : }
1032 813 : if (!CheckDumpFieldTypes()) {
1033 204 : return ADUMP_FAILED;
1034 : }
1035 609 : if (!CheckDumpFieldValues()) {
1036 72 : return ADUMP_FAILED;
1037 : }
1038 537 : if (!CheckDumpConstraints()) {
1039 204 : return ADUMP_FAILED;
1040 : }
1041 333 : RawDumpConfig rawDumpConfig = js.at(ADUMP_DUMP);
1042 333 : ParseDfxTypesFromJson(rawDumpConfig, dumpDfxConfig);
1043 :
1044 333 : if (!NeedDump(rawDumpConfig)) {
1045 39 : return ADUMP_SUCCESS;
1046 : }
1047 294 : dumpConfig = ConvertDumpConfig(rawDumpConfig);
1048 294 : dumpType = ConvertDumpType(rawDumpConfig);
1049 294 : EnsureDefaultDfxType(dumpType, dumpDfxConfig);
1050 294 : needDump = true;
1051 294 : IDE_LOGI(
1052 : "Success to convert dumpType[%d], dumpPath[%s], dumpMode[%s], "
1053 : "dumpStatus[%s], dumpData[%s], dumpSwitch[%ld]",
1054 : dumpType, dumpConfig.dumpPath.c_str(), dumpConfig.dumpMode.c_str(), dumpConfig.dumpStatus.c_str(),
1055 : dumpConfig.dumpData.c_str(), dumpConfig.dumpSwitch);
1056 333 : } catch (const std::exception& e) {
1057 0 : REPORT_EP0004_PARSE_ERROR("Convert", std::string(e.what()), configPath_);
1058 0 : return ADUMP_FAILED;
1059 0 : }
1060 294 : return ADUMP_SUCCESS;
1061 931 : }
1062 :
1063 338 : bool DumpConfigConverter::GetEnvVariable(const std::string& env, std::string& value)
1064 : {
1065 338 : if (env.empty()) {
1066 0 : return false;
1067 : }
1068 338 : const char* val = std::getenv(env.c_str());
1069 338 : if (val != nullptr && val[0] != '\0') {
1070 107 : value = val;
1071 107 : IDE_LOGI("Get Env[%s] is [%s]", env.c_str(), val);
1072 107 : return true;
1073 : }
1074 231 : return false;
1075 : }
1076 :
1077 89 : bool DumpConfigConverter::CheckDumpPath(const std::string& param, const std::string& dumpPath)
1078 : {
1079 89 : Path path(dumpPath);
1080 89 : if (!path.CreateDirectory(true)) {
1081 0 : IDE_LOGW("Failed to create dump path [%s] for Env[%s]", dumpPath.c_str(), param.c_str());
1082 0 : return false;
1083 : }
1084 89 : if (!path.RealPath()) {
1085 0 : IDE_LOGW("The dump path [%s] for Env[%s] is not a real path", path.GetCString(), param.c_str());
1086 0 : return false;
1087 : }
1088 :
1089 89 : if (!path.IsDirectory()) {
1090 3 : IDE_LOGW("The dump path [%s] for Env[%s] is not a directory path", path.GetCString(), param.c_str());
1091 3 : return false;
1092 : }
1093 :
1094 86 : constexpr uint32_t accessMode = static_cast<uint32_t>(M_R_OK) | static_cast<uint32_t>(M_W_OK);
1095 86 : if (!path.Asccess(accessMode)) {
1096 0 : IDE_LOGW("The path [%s] for Env[%s] does not have read and write permission", path.GetCString(), param.c_str());
1097 0 : return false;
1098 : }
1099 :
1100 86 : IDE_LOGI("The real dump path [%s]", path.GetCString());
1101 86 : return true;
1102 89 : }
1103 :
1104 276 : bool DumpConfigConverter::GetEnvDumpPath(const std::string& env, std::string& envPath)
1105 : {
1106 276 : std::string tmpPath;
1107 276 : if (GetEnvVariable(env, tmpPath)) {
1108 89 : if (CheckDumpPath(env, tmpPath)) {
1109 86 : envPath = tmpPath;
1110 86 : return true;
1111 : }
1112 : }
1113 190 : return false;
1114 276 : }
1115 :
1116 62 : void DumpConfigConverter::LoadDumpEnvVariables(DumpEnvVariable& dumpEnvVariable)
1117 : {
1118 62 : std::string envAscendDumpScene;
1119 62 : if (GetEnvVariable(ADUMP_ENV_ASCEND_DUMP_SCENE, envAscendDumpScene)) {
1120 18 : if (envDumpScenes.find(envAscendDumpScene) != envDumpScenes.end()) {
1121 15 : dumpEnvVariable.ascendDumpScene = envAscendDumpScene;
1122 : } else {
1123 3 : std::string optionStr = TransOptionsToStr(envDumpScenes);
1124 3 : IDE_LOGW(
1125 : "Value[%s] of Env[ASCEND_DUMP_SCENE] is invalid, it must be %s", envAscendDumpScene.c_str(),
1126 : optionStr.c_str());
1127 3 : }
1128 : }
1129 :
1130 62 : (void)GetEnvDumpPath(ADUMP_ENV_ASCEND_DUMP_PATH, dumpEnvVariable.ascendDumpPath);
1131 62 : (void)GetEnvDumpPath(ADUMP_ENV_NPU_COLLECT_PATH, dumpEnvVariable.npuCollectPath);
1132 62 : (void)GetEnvDumpPath(ADUMP_ENV_ASCEND_WORK_PATH, dumpEnvVariable.ascendWorkPath);
1133 62 : }
1134 :
1135 36 : bool DumpConfigConverter::EnableExceptionDumpWithEnv(DumpConfig& dumpConfig, DumpType& dumpType)
1136 : {
1137 36 : DumpEnvVariable dumpEnvVariable;
1138 36 : LoadDumpEnvVariables(dumpEnvVariable);
1139 36 : if (ConvertDumpScene(dumpEnvVariable.ascendDumpScene, dumpType)) {
1140 39 : dumpConfig.dumpPath = !dumpEnvVariable.ascendDumpPath.empty() ?
1141 : dumpEnvVariable.ascendDumpPath :
1142 39 : (dumpEnvVariable.ascendWorkPath.empty() ? "./" : dumpEnvVariable.ascendWorkPath);
1143 15 : dumpConfig.dumpStatus = ADUMP_DUMP_STATUS_SWITCH_ON;
1144 15 : return true;
1145 21 : } else if (!dumpEnvVariable.npuCollectPath.empty()) {
1146 12 : dumpType = DumpType::EXCEPTION;
1147 12 : dumpConfig.dumpPath = dumpEnvVariable.npuCollectPath;
1148 12 : dumpConfig.dumpStatus = ADUMP_DUMP_STATUS_SWITCH_ON;
1149 12 : return true;
1150 : } else {
1151 9 : IDE_LOGI("No Env enable exception dump");
1152 9 : return false;
1153 : }
1154 36 : }
1155 :
1156 26 : bool DumpConfigConverter::EnableKernelDfxDumpWithEnv(DumpDfxConfig& config)
1157 : {
1158 26 : DumpEnvVariable dumpEnvVariable;
1159 26 : LoadDumpEnvVariables(dumpEnvVariable);
1160 26 : if (!dumpEnvVariable.ascendDumpPath.empty() || !dumpEnvVariable.ascendWorkPath.empty()) {
1161 : // enable all(default, RT_KERNEL_DFX_INFO_DEFAULT) with env variable
1162 20 : config.dfxTypes.push_back(ADUMP_DUMP_KERNEL_DATA_ALL);
1163 : std::string dumpPath =
1164 20 : dumpEnvVariable.ascendDumpPath.empty() ? dumpEnvVariable.ascendWorkPath : dumpEnvVariable.ascendDumpPath;
1165 60 : Path path = Path(dumpPath).Append("printf");
1166 20 : config.dumpPath = path.GetString();
1167 20 : return true;
1168 20 : }
1169 6 : return false;
1170 26 : }
1171 : } // namespace Adx
|