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 423 : int32_t DumpSetting::Init(const DumpType type, const DumpConfig &dumpConfig)
31 : {
32 423 : if (type == DumpType::OPERATOR) {
33 399 : 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 399 : int32_t DumpSetting::DumpOperatorInit(const DumpConfig &dumpConfig)
44 : {
45 399 : bool dumpStatus = dumpStatus_;
46 399 : 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 393 : if (!dumpStatus) {
53 60 : dumpStatus_ = dumpStatus;
54 60 : return ADUMP_SUCCESS;
55 : }
56 :
57 333 : 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 327 : 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 321 : InitDumpData(dumpConfig.dumpData);
68 :
69 321 : InitDumpSwitch(dumpConfig.dumpSwitch & DUMP_SWITCH_MASK);
70 :
71 321 : if (!InitDumpStatsItem(dumpConfig.dumpStatsItem)) {
72 3 : IDE_LOGE("Dump stats config check failed.");
73 3 : return ADUMP_FAILED;
74 : }
75 318 : if (!AdumpDsmi::DrvGetPlatformType(platformType_)) {
76 0 : IDE_LOGE("Get platform type failed.");
77 0 : return ADUMP_FAILED;
78 : }
79 318 : dumpStatus_ = dumpStatus;
80 318 : IDE_LOGD("DumpOperatorInit finished.");
81 318 : 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
116 : {
117 48 : return dumpPath_.GetString();
118 : }
119 :
120 36 : const char* DumpSetting::GetDumpCPath() const
121 : {
122 36 : return dumpPath_.Empty() ? nullptr : dumpPath_.GetCString();
123 : }
124 :
125 51 : uint32_t DumpSetting::GetDumpMode() const
126 : {
127 51 : return dumpMode_;
128 : }
129 :
130 124 : bool DumpSetting::GetDumpStatus() const
131 : {
132 124 : if (dumpStatus_) {
133 75 : return ((dumpSwitch_ & OPERATOR_KERNEL_DUMP) != 0);
134 : }
135 49 : return dumpStatus_;
136 : }
137 :
138 45 : bool DumpSetting::GetDumpStatusEx() const
139 : {
140 45 : return dumpStatus_;
141 : }
142 :
143 69 : bool DumpSetting::IsDumpDataStats() const
144 : {
145 69 : return dumpData_.compare(DUMP_STATS_DATA) == 0;
146 : }
147 :
148 61 : bool DumpSetting::GetDumpDebugStatus() const
149 : {
150 61 : return dumpDebugStatus_;
151 : }
152 :
153 321 : const std::string DumpSetting::GetDumpData() const
154 : {
155 321 : return dumpData_;
156 : }
157 :
158 164 : uint64_t DumpSetting::GetDumpSwitch() const
159 : {
160 164 : return dumpSwitch_;
161 : }
162 :
163 198 : uint64_t DumpSetting::GetDumpStatsItem() const
164 : {
165 198 : return dumpStatsItem_;
166 : }
167 :
168 339 : PlatformType DumpSetting::GetPlatformType() const
169 : {
170 339 : return static_cast<PlatformType>(platformType_);
171 : }
172 :
173 500 : bool DumpSetting::InitDumpStatus(const std::string &dumpStatus, bool &status) const
174 : {
175 : static const std::map<std::string, bool> DUMP_STATUS_MAP = { { CONF_DUMP_STATUS_ON, true },
176 : { CONF_DUMP_STATUS_ON_CAPITAL, true },
177 : { CONF_DUMP_STATUS_OFF_CAPITAL, false },
178 518 : { CONF_DUMP_STATUS_OFF, false } };
179 :
180 500 : auto it = DUMP_STATUS_MAP.find(dumpStatus);
181 500 : if (it == DUMP_STATUS_MAP.cend()) {
182 10 : return false;
183 : }
184 :
185 490 : status = it->second;
186 490 : return true;
187 6 : }
188 :
189 345 : bool DumpSetting::InitDumpMode(const std::string &dumpMode)
190 : {
191 : static const std::map<std::string, uint32_t> DUMP_MODE_MAP = {
192 : { CONF_DUMP_MODE_INPUT, DUMP_MODE_INPUT },
193 : { CONF_DUMP_MODE_OUTPUT, DUMP_MODE_OUTPUT },
194 0 : { CONF_DUMP_MODE_ALL, DUMP_MODE_INPUT | DUMP_MODE_OUTPUT | DUMP_MODE_WORKSPACE }
195 360 : };
196 :
197 345 : auto it = DUMP_MODE_MAP.find(dumpMode);
198 345 : if (it == DUMP_MODE_MAP.cend()) {
199 6 : return false;
200 : }
201 339 : dumpMode_ = it->second;
202 339 : return true;
203 3 : }
204 :
205 339 : bool DumpSetting::InitDumpPath(const std::string &dumpPath)
206 : {
207 339 : if (dumpPath.empty()) {
208 9 : IDE_LOGE("Dump path is empty.");
209 9 : return false;
210 : }
211 :
212 330 : dumpPath_ = Path(dumpPath).Append(SysUtils::GetCurrentTime());
213 330 : IDE_LOGI("Init dump path: %s", dumpPath_.GetString().c_str());
214 330 : return true;
215 : }
216 :
217 321 : void DumpSetting::InitDumpData(const std::string &dumpData)
218 : {
219 321 : if (StrUtils::Trim(dumpData).compare(DUMP_STATS_DATA) == 0) {
220 72 : dumpData_ = DUMP_STATS_DATA;
221 : } else {
222 249 : dumpData_ = DUMP_TENSOR_DATA;
223 : }
224 321 : }
225 :
226 321 : bool DumpSetting::InitDumpStatsItem(const std::vector<std::string> &dumpStatsItem)
227 : {
228 : const std::unordered_map<std::string, uint64_t> statsItemMap = {
229 : {"Max", DUMP_STATS_MAX},
230 : {"Min", DUMP_STATS_MIN},
231 : {"Avg", DUMP_STATS_AVG},
232 : {"Nan", DUMP_STATS_NAN},
233 : {"Negative Inf", DUMP_STATS_NEG_INF},
234 : {"Positive Inf", DUMP_STATS_POS_INF},
235 : {"L2norm", DUMP_STATS_L2NORM}
236 2889 : };
237 321 : if (dumpStatsItem.empty()) {
238 315 : dumpStatsItem_ |= DUMP_STATS_MAX | DUMP_STATS_MIN | DUMP_STATS_AVG | DUMP_STATS_NAN | DUMP_STATS_NEG_INF |
239 : DUMP_STATS_POS_INF;
240 315 : IDE_LOGI("Dump stats set to default configuration[0x%llx].", dumpStatsItem_);
241 315 : return true;
242 : }
243 12 : for (auto &str : dumpStatsItem) {
244 9 : auto it = statsItemMap.find(StrUtils::Trim(str));
245 9 : if (it != statsItemMap.cend()) {
246 6 : dumpStatsItem_ |= it->second;
247 : } else {
248 3 : std::vector<std::string> keysVector;
249 24 : for (auto const& element : statsItemMap) {
250 21 : keysVector.push_back(element.first);
251 : }
252 3 : IDE_LOGE("Dump stats config[%s] is invalid, and expected %s.", str.c_str(),
253 : StrUtils::ToString(keysVector).c_str());
254 3 : return false;
255 3 : }
256 : }
257 3 : IDE_LOGI("Dump stats set configuration[0x%llx].", dumpStatsItem_);
258 3 : return true;
259 642 : }
260 :
261 398 : void DumpSetting::InitDumpSwitch(uint64_t dumpSwitch)
262 : {
263 398 : dumpSwitch_ = dumpSwitch;
264 398 : }
265 :
266 : } // namespace Adx
|