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