Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include <sstream>
12 : #include "aicpusd_util.h"
13 : #include "aicpusd_args_parser.h"
14 :
15 : namespace AicpuSchedule {
16 39 : bool ArgsParser::ParseArgs(const int32_t argc, const char_t* const argv[])
17 : {
18 39 : if ((argv == nullptr) || (argc <= 1)) {
19 1 : return false;
20 : }
21 :
22 318 : for (uint32_t i = 0; i < static_cast<uint32_t>(argc); ++i) {
23 570 : if (!ParseSinglePara(std::string(argv[i]))) {
24 5 : return false;
25 : }
26 : }
27 :
28 33 : return CheckRequiredParas();
29 : }
30 :
31 4 : bool ArgsParser::ParseArgs(const std::vector<std::string>& args)
32 : {
33 40 : for (const auto& iter : args) {
34 37 : if (!ParseSinglePara(iter)) {
35 1 : return false;
36 : }
37 : }
38 :
39 3 : return CheckRequiredParas();
40 : }
41 :
42 36 : bool ArgsParser::CheckRequiredParas() const
43 : {
44 36 : const bool requiredParams[] = {withDeviceId_, withHostPid_, withPidSign_, withGrpNameNum_, withGrpNameList_};
45 194 : for (uint32_t i = 0U; i < (sizeof(requiredParams) / sizeof(bool)); ++i) {
46 166 : if (!requiredParams[i]) {
47 8 : return false;
48 : }
49 : }
50 :
51 28 : return true;
52 : }
53 :
54 326 : bool ArgsParser::ParseSinglePara(const std::string& singlePara)
55 : {
56 326 : const std::size_t offset = singlePara.find("=");
57 326 : if (offset == std::string::npos) {
58 39 : return true;
59 : }
60 :
61 287 : const std::string key = singlePara.substr(0U, offset + 1UL);
62 287 : const std::string val = singlePara.substr(offset + 1UL);
63 287 : const auto iter = argsParseFuncMap_.find(key);
64 287 : if (iter == argsParseFuncMap_.end()) {
65 : // Ignore unknown parameter
66 2 : return true;
67 : }
68 :
69 285 : const bool ret = (this->*(iter->second))(val);
70 285 : if (!ret) {
71 7 : return false;
72 : }
73 :
74 278 : return true;
75 287 : }
76 :
77 47 : bool ArgsParser::ParseDeviceId(const std::string& para)
78 : {
79 47 : int32_t val = 0;
80 47 : if (!AicpuUtil::TransStrToInt(para, val)) {
81 1 : return false;
82 : }
83 :
84 46 : if ((val < 0) || (val >= AICPUFW_CHIP_NUM_MAX)) {
85 2 : return false;
86 : }
87 :
88 44 : deviceId_ = static_cast<uint32_t>(val);
89 44 : withDeviceId_ = true;
90 44 : return true;
91 : }
92 :
93 42 : bool ArgsParser::ParseHostPid(const std::string& para)
94 : {
95 42 : int32_t val = 0;
96 42 : if (!AicpuUtil::TransStrToInt(para, val)) {
97 1 : return false;
98 : }
99 :
100 41 : if (val <= 0) {
101 2 : return false;
102 : }
103 :
104 39 : hostPid_ = static_cast<uint32_t>(val);
105 39 : withHostPid_ = true;
106 39 : return true;
107 : }
108 :
109 40 : bool ArgsParser::ParseSign(const std::string& para)
110 : {
111 40 : pidSign_ = para;
112 40 : withPidSign_ = true;
113 40 : return true;
114 : }
115 :
116 39 : bool ArgsParser::ParseProfilingMode(const std::string& para)
117 : {
118 39 : uint32_t val = 0U;
119 39 : if (!AicpuUtil::TransStrToUint(para, val)) {
120 1 : return false;
121 : }
122 :
123 38 : if ((val != static_cast<uint32_t>(PROFILING_CLOSE)) && (val != static_cast<uint32_t>(PROFILING_OPEN))) {
124 2 : return false;
125 : }
126 :
127 36 : profilingMode_ = val;
128 36 : return true;
129 : }
130 :
131 33 : bool ArgsParser::ParseLogAndEventLevel(const std::string& para)
132 : {
133 33 : int32_t val = 0;
134 33 : if (!AicpuUtil::TransStrToInt(para, val)) {
135 1 : return false;
136 : }
137 :
138 32 : if (val < 0) {
139 1 : return false;
140 : }
141 :
142 31 : logLevel_ = val % VALUE_FOR_CALCULATE_LOG_LEVEL;
143 31 : eventLevel_ = val / VALUE_FOR_CALCULATE_LOG_LEVEL;
144 31 : return true;
145 : }
146 :
147 2 : bool ArgsParser::ParseCcecpuLogLevel(const std::string& para)
148 : {
149 2 : int32_t val = 0;
150 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
151 1 : return true;
152 : }
153 :
154 1 : ccecpulogLevel_ = val;
155 1 : return true;
156 : }
157 :
158 2 : bool ArgsParser::ParseAicpuLogLevel(const std::string& para)
159 : {
160 2 : int32_t val = 0;
161 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
162 1 : return true;
163 : }
164 :
165 1 : aicpulogLevel_ = val;
166 1 : return true;
167 : }
168 :
169 13 : bool ArgsParser::ParseVfId(const std::string& para)
170 : {
171 13 : int32_t val = 0;
172 13 : if (!AicpuUtil::TransStrToInt(para, val)) {
173 1 : return false;
174 : }
175 :
176 12 : if ((val < 0) || (val > VF_ID_MAX)) {
177 2 : return false;
178 : }
179 :
180 10 : vfId_ = static_cast<uint32_t>(val);
181 10 : return true;
182 : }
183 :
184 24 : bool ArgsParser::ParseDeviceMode(const std::string& para)
185 : {
186 24 : int32_t val = 0;
187 24 : if (!AicpuUtil::TransStrToInt(para, val)) {
188 1 : return false;
189 : }
190 :
191 23 : if ((val < 0) || (val > DEVICE_MODE_MAX)) {
192 3 : return false;
193 : }
194 :
195 20 : deviceMode_ = static_cast<uint32_t>(val);
196 20 : return true;
197 : }
198 :
199 3 : bool ArgsParser::ParseAicpuProcNum(const std::string& para)
200 : {
201 3 : uint32_t val = 0;
202 3 : if (!AicpuUtil::TransStrToUint(para, val)) {
203 1 : return false;
204 : }
205 2 : aicpuProcNum_ = val;
206 2 : return true;
207 : }
208 :
209 33 : bool ArgsParser::ParseGrpNameNum(const std::string& para)
210 : {
211 33 : int32_t val = 0;
212 33 : if (!AicpuUtil::TransStrToInt(para, val)) {
213 2 : return false;
214 : }
215 :
216 31 : if (val < 0) {
217 1 : return false;
218 : }
219 :
220 30 : grpNameNum_ = static_cast<uint32_t>(val);
221 30 : withGrpNameNum_ = true;
222 30 : return true;
223 : }
224 :
225 33 : bool ArgsParser::ParseGrpNameList(const std::string& para)
226 : {
227 33 : grpNameList_.clear();
228 33 : ArgsParser::SplitGrpNameList(para, grpNameList_);
229 33 : withGrpNameList_ = true;
230 33 : return true;
231 : }
232 :
233 4 : bool ArgsParser::ParseHostProcName(const std::string& para)
234 : {
235 4 : hostProcName_ = para;
236 4 : return true;
237 : }
238 :
239 1 : bool ArgsParser::ParseQsGrpNameList(const std::string& para)
240 : {
241 1 : qsGrpNameList_.clear();
242 1 : ArgsParser::SplitGrpNameList(para, qsGrpNameList_);
243 1 : return true;
244 : }
245 :
246 35 : void ArgsParser::SplitGrpNameList(const std::string& grpNamePara, std::vector<std::string>& grpNameList)
247 : {
248 35 : std::stringstream grpNameSteam(grpNamePara);
249 35 : std::string tmpGrpName;
250 98 : while (getline(grpNameSteam, tmpGrpName, ',')) {
251 63 : grpNameList.push_back(tmpGrpName);
252 : }
253 35 : }
254 :
255 27 : std::string ArgsParser::GetParaParsedStr()
256 : {
257 27 : std::ostringstream oss;
258 27 : oss << "deviceId=" << deviceId_ << ", hostPid=" << hostPid_ << ", pidSign=" << pidSign_
259 27 : << ", profilingMode=" << profilingMode_ << ", vfId=" << vfId_ << ", logLevel=" << logLevel_
260 27 : << ", ccecpulogLevel=" << ccecpulogLevel_ << ", aicpulogLevel=" << aicpulogLevel_
261 27 : << ", deviceMode=" << deviceMode_ << ", hostProcName=" << hostProcName_ << ", grpNameNum=" << grpNameNum_
262 27 : << ", aicpuProcNum=" << aicpuProcNum_ << ", grpNameList=[";
263 :
264 69 : for (const std::string& iter : grpNameList_) {
265 42 : oss << iter << ",";
266 : }
267 27 : oss << "]";
268 :
269 54 : return oss.str();
270 27 : }
271 : } // namespace AicpuSchedule
|