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 <unordered_map>
11 : #include "dump_setting.h"
12 : #include "log/hdc_log.h"
13 : #include "sys_utils.h"
14 : #include "str_utils.h"
15 : #include "common_utils.h"
16 : #include "adump_api.h"
17 :
18 : namespace Adx {
19 : namespace {
20 : constexpr char CONF_DUMP_STATUS_ON[] = "on";
21 : constexpr char CONF_DUMP_STATUS_OFF[] = "off";
22 : constexpr char CONF_DUMP_STATUS_ON_CAPITAL[] = "ON";
23 : constexpr char CONF_DUMP_STATUS_OFF_CAPITAL[] = "OFF";
24 :
25 : constexpr char CONF_DUMP_MODE_INPUT[] = "input";
26 : constexpr char CONF_DUMP_MODE_OUTPUT[] = "output";
27 : constexpr char CONF_DUMP_MODE_ALL[] = "all";
28 : } // namespace
29 :
30 444 : int32_t DumpSetting::Init(const DumpType type, const DumpConfig& dumpConfig)
31 : {
32 444 : if (type == DumpType::OPERATOR) {
33 420 : return DumpOperatorInit(dumpConfig);
34 24 : } else if (type == DumpType::OP_OVERFLOW) {
35 24 : return DumpOverflowInit(dumpConfig);
36 : } else {
37 0 : IDE_LOGE("Dump type(%d) is not valid.", static_cast<int32_t>(type));
38 : }
39 :
40 0 : return ADUMP_FAILED;
41 : }
42 :
43 420 : int32_t DumpSetting::DumpOperatorInit(const DumpConfig& dumpConfig)
44 : {
45 420 : bool dumpStatus = dumpStatus_;
46 420 : if (!InitDumpStatus(dumpConfig.dumpStatus, dumpStatus)) {
47 6 : IDE_LOGE("Dump status(%s) is not valid.", dumpConfig.dumpStatus.c_str());
48 6 : return ADUMP_FAILED;
49 : }
50 :
51 : // if disable dump, just return.
52 414 : if (!dumpStatus) {
53 60 : dumpStatus_ = dumpStatus;
54 60 : return ADUMP_SUCCESS;
55 : }
56 :
57 354 : if (!InitDumpMode(dumpConfig.dumpMode)) {
58 6 : IDE_LOGE("Dump mode(%s) is not valid.", dumpConfig.dumpMode.c_str());
59 6 : return ADUMP_FAILED;
60 : }
61 :
62 348 : if (!InitDumpPath(dumpConfig.dumpPath)) {
63 6 : IDE_LOGE("Dump path(%s) is not valid.", dumpConfig.dumpPath.c_str());
64 6 : return ADUMP_FAILED;
65 : }
66 :
67 342 : InitDumpData(dumpConfig.dumpData);
68 :
69 342 : InitDumpSwitch(dumpConfig.dumpSwitch & DUMP_SWITCH_MASK);
70 :
71 342 : if (!InitDumpStatsItem(dumpConfig.dumpStatsItem)) {
72 3 : IDE_LOGE("Dump stats config check failed.");
73 3 : return ADUMP_FAILED;
74 : }
75 339 : if (!AdumpDsmi::DrvGetPlatformType(platformType_)) {
76 0 : IDE_LOGE("Get platform type failed.");
77 0 : return ADUMP_FAILED;
78 : }
79 339 : dumpStatus_ = dumpStatus;
80 339 : IDE_LOGD("DumpOperatorInit finished.");
81 339 : return ADUMP_SUCCESS;
82 : }
83 :
84 24 : int32_t DumpSetting::DumpOverflowInit(const DumpConfig& dumpConfig)
85 : {
86 24 : bool dumpDebugStatus = dumpDebugStatus_;
87 24 : if (!InitDumpStatus(dumpConfig.dumpStatus, dumpDebugStatus)) {
88 3 : IDE_LOGE("Dump status(%s) is not valid.", dumpConfig.dumpStatus.c_str());
89 3 : return ADUMP_FAILED;
90 : }
91 :
92 : // if disable dump, just return.
93 21 : if (!dumpDebugStatus) {
94 9 : dumpDebugStatus_ = dumpDebugStatus;
95 9 : return ADUMP_SUCCESS;
96 : }
97 :
98 : // default all
99 24 : if (!InitDumpMode(CONF_DUMP_MODE_ALL)) {
100 0 : IDE_LOGE("Dump mode(%s) is not valid.", dumpConfig.dumpMode.c_str());
101 0 : return ADUMP_FAILED;
102 : }
103 :
104 12 : if (!InitDumpPath(dumpConfig.dumpPath)) {
105 3 : IDE_LOGE("Dump path(%s) is not valid.", dumpConfig.dumpPath.c_str());
106 3 : return ADUMP_FAILED;
107 : }
108 :
109 9 : InitDumpSwitch(dumpConfig.dumpSwitch & DUMP_SWITCH_MASK);
110 9 : dumpDebugStatus_ = dumpDebugStatus;
111 9 : IDE_LOGD("DumpOverflowInit finished.");
112 9 : return ADUMP_SUCCESS;
113 : }
114 :
115 48 : std::string DumpSetting::GetDumpPath() const { return dumpPath_.GetString(); }
116 :
117 36 : const char* DumpSetting::GetDumpCPath() const { return dumpPath_.Empty() ? nullptr : dumpPath_.GetCString(); }
118 :
119 51 : uint32_t DumpSetting::GetDumpMode() const { return dumpMode_; }
120 :
121 124 : bool DumpSetting::GetDumpStatus() const
122 : {
123 124 : if (dumpStatus_) {
124 75 : return ((dumpSwitch_ & OPERATOR_KERNEL_DUMP) != 0);
125 : }
126 49 : return dumpStatus_;
127 : }
128 :
129 45 : bool DumpSetting::GetDumpStatusEx() const { return dumpStatus_; }
130 :
131 69 : bool DumpSetting::IsDumpDataStats() const { return dumpData_.compare(DUMP_STATS_DATA) == 0; }
132 :
133 61 : bool DumpSetting::GetDumpDebugStatus() const { return dumpDebugStatus_; }
134 :
135 321 : const std::string DumpSetting::GetDumpData() const { return dumpData_; }
136 :
137 164 : uint64_t DumpSetting::GetDumpSwitch() const { return dumpSwitch_; }
138 :
139 978 : uint64_t DumpSetting::GetDumpStatsItem() const { return dumpStatsItem_; }
140 :
141 339 : PlatformType DumpSetting::GetPlatformType() const { return static_cast<PlatformType>(platformType_); }
142 :
143 531 : bool DumpSetting::InitDumpStatus(const std::string& dumpStatus, bool& status) const
144 : {
145 : static const std::map<std::string, bool> DUMP_STATUS_MAP = {
146 : {CONF_DUMP_STATUS_ON, true},
147 : {CONF_DUMP_STATUS_ON_CAPITAL, true},
148 : {CONF_DUMP_STATUS_OFF_CAPITAL, false},
149 549 : {CONF_DUMP_STATUS_OFF, false}};
150 :
151 531 : auto it = DUMP_STATUS_MAP.find(dumpStatus);
152 531 : if (it == DUMP_STATUS_MAP.cend()) {
153 10 : return false;
154 : }
155 :
156 521 : status = it->second;
157 521 : return true;
158 6 : }
159 :
160 366 : bool DumpSetting::InitDumpMode(const std::string& dumpMode)
161 : {
162 : static const std::map<std::string, uint32_t> DUMP_MODE_MAP = {
163 : {CONF_DUMP_MODE_INPUT, DUMP_MODE_INPUT},
164 : {CONF_DUMP_MODE_OUTPUT, DUMP_MODE_OUTPUT},
165 381 : {CONF_DUMP_MODE_ALL, DUMP_MODE_INPUT | DUMP_MODE_OUTPUT | DUMP_MODE_WORKSPACE}};
166 :
167 366 : auto it = DUMP_MODE_MAP.find(dumpMode);
168 366 : if (it == DUMP_MODE_MAP.cend()) {
169 6 : return false;
170 : }
171 360 : dumpMode_ = it->second;
172 360 : return true;
173 3 : }
174 :
175 360 : bool DumpSetting::InitDumpPath(const std::string& dumpPath)
176 : {
177 360 : if (dumpPath.empty()) {
178 9 : IDE_LOGE("Dump path is empty.");
179 9 : return false;
180 : }
181 :
182 351 : dumpPath_ = Path(dumpPath).Append(SysUtils::GetCurrentTime());
183 351 : IDE_LOGI("Init dump path: %s", dumpPath_.GetString().c_str());
184 351 : return true;
185 : }
186 :
187 342 : void DumpSetting::InitDumpData(const std::string& dumpData)
188 : {
189 342 : if (StrUtils::Trim(dumpData).compare(DUMP_STATS_DATA) == 0) {
190 93 : dumpData_ = DUMP_STATS_DATA;
191 : } else {
192 249 : dumpData_ = DUMP_TENSOR_DATA;
193 : }
194 342 : }
195 :
196 342 : bool DumpSetting::InitDumpStatsItem(const std::vector<std::string>& dumpStatsItem)
197 : {
198 : const std::unordered_map<std::string, uint64_t> statsItemMap = {
199 : {"Max", DUMP_STATS_MAX},
200 : {"Min", DUMP_STATS_MIN},
201 : {"Avg", DUMP_STATS_AVG},
202 : {"Nan", DUMP_STATS_NAN},
203 : {"Negative Inf", DUMP_STATS_NEG_INF},
204 : {"Positive Inf", DUMP_STATS_POS_INF},
205 3078 : {"L2norm", DUMP_STATS_L2NORM}};
206 342 : if (dumpStatsItem.empty()) {
207 336 : dumpStatsItem_ |=
208 : DUMP_STATS_MAX | DUMP_STATS_MIN | DUMP_STATS_AVG | DUMP_STATS_NAN | DUMP_STATS_NEG_INF | DUMP_STATS_POS_INF;
209 336 : IDE_LOGI("Dump stats set to default configuration[0x%llx].", dumpStatsItem_);
210 336 : return true;
211 : }
212 18 : for (auto& str : dumpStatsItem) {
213 9 : auto it = statsItemMap.find(StrUtils::Trim(str));
214 9 : if (it != statsItemMap.cend()) {
215 6 : dumpStatsItem_ |= it->second;
216 : } else {
217 3 : std::vector<std::string> keysVector;
218 24 : for (auto const& element : statsItemMap) {
219 21 : keysVector.push_back(element.first);
220 : }
221 3 : IDE_LOGE(
222 : "Dump stats config[%s] is invalid, and expected %s.", str.c_str(),
223 : StrUtils::ToString(keysVector).c_str());
224 3 : return false;
225 3 : }
226 : }
227 3 : IDE_LOGI("Dump stats set configuration[0x%llx].", dumpStatsItem_);
228 3 : return true;
229 684 : }
230 :
231 419 : void DumpSetting::InitDumpSwitch(uint64_t dumpSwitch) { dumpSwitch_ = dumpSwitch; }
232 :
233 : } // namespace Adx
|