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 ¶)
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 ¶)
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 ¶)
110 : {
111 40 : pidSign_ = para;
112 40 : withPidSign_ = true;
113 40 : return true;
114 : }
115 :
116 39 : bool ArgsParser::ParseProfilingMode(const std::string ¶)
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)) &&
124 38 : (val != static_cast<uint32_t>(PROFILING_OPEN))) {
125 2 : return false;
126 : }
127 :
128 36 : profilingMode_ = val;
129 36 : return true;
130 : }
131 :
132 33 : bool ArgsParser::ParseLogAndEventLevel(const std::string ¶)
133 : {
134 33 : int32_t val = 0;
135 33 : if (!AicpuUtil::TransStrToInt(para, val)) {
136 1 : return false;
137 : }
138 :
139 32 : if (val < 0) {
140 1 : return false;
141 : }
142 :
143 31 : logLevel_ = val % VALUE_FOR_CALCULATE_LOG_LEVEL;
144 31 : eventLevel_ = val / VALUE_FOR_CALCULATE_LOG_LEVEL;
145 31 : return true;
146 : }
147 :
148 2 : bool ArgsParser::ParseCcecpuLogLevel(const std::string ¶)
149 : {
150 2 : int32_t val = 0;
151 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
152 1 : return true;
153 : }
154 :
155 1 : ccecpulogLevel_ = val;
156 1 : return true;
157 : }
158 :
159 2 : bool ArgsParser::ParseAicpuLogLevel(const std::string ¶)
160 : {
161 2 : int32_t val = 0;
162 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
163 1 : return true;
164 : }
165 :
166 1 : aicpulogLevel_ = val;
167 1 : return true;
168 : }
169 :
170 13 : bool ArgsParser::ParseVfId(const std::string ¶)
171 : {
172 13 : int32_t val = 0;
173 13 : if (!AicpuUtil::TransStrToInt(para, val)) {
174 1 : return false;
175 : }
176 :
177 12 : if ((val < 0) || (val > VF_ID_MAX)) {
178 2 : return false;
179 : }
180 :
181 10 : vfId_ = static_cast<uint32_t>(val);
182 10 : return true;
183 : }
184 :
185 24 : bool ArgsParser::ParseDeviceMode(const std::string ¶)
186 : {
187 24 : int32_t val = 0;
188 24 : if (!AicpuUtil::TransStrToInt(para, val)) {
189 1 : return false;
190 : }
191 :
192 23 : if ((val < 0) || (val > DEVICE_MODE_MAX)) {
193 3 : return false;
194 : }
195 :
196 20 : deviceMode_ = static_cast<uint32_t>(val);
197 20 : return true;
198 : }
199 :
200 3 : bool ArgsParser::ParseAicpuProcNum(const std::string ¶)
201 : {
202 3 : uint32_t val = 0;
203 3 : if (!AicpuUtil::TransStrToUint(para, val)) {
204 1 : return false;
205 : }
206 2 : aicpuProcNum_ = val;
207 2 : return true;
208 : }
209 :
210 33 : bool ArgsParser::ParseGrpNameNum(const std::string ¶)
211 : {
212 33 : int32_t val = 0;
213 33 : if (!AicpuUtil::TransStrToInt(para, val)) {
214 2 : return false;
215 : }
216 :
217 31 : if (val < 0) {
218 1 : return false;
219 : }
220 :
221 30 : grpNameNum_ = static_cast<uint32_t>(val);
222 30 : withGrpNameNum_ = true;
223 30 : return true;
224 : }
225 :
226 33 : bool ArgsParser::ParseGrpNameList(const std::string ¶)
227 : {
228 33 : grpNameList_.clear();
229 33 : ArgsParser::SplitGrpNameList(para, grpNameList_);
230 33 : withGrpNameList_ = true;
231 33 : return true;
232 : }
233 :
234 4 : bool ArgsParser::ParseHostProcName(const std::string ¶)
235 : {
236 4 : hostProcName_ = para;
237 4 : return true;
238 : }
239 :
240 1 : bool ArgsParser::ParseQsGrpNameList(const std::string ¶)
241 : {
242 1 : qsGrpNameList_.clear();
243 1 : ArgsParser::SplitGrpNameList(para, qsGrpNameList_);
244 1 : return true;
245 : }
246 :
247 35 : void ArgsParser::SplitGrpNameList(const std::string &grpNamePara, std::vector<std::string> &grpNameList)
248 : {
249 35 : std::stringstream grpNameSteam(grpNamePara);
250 35 : std::string tmpGrpName;
251 98 : while (getline(grpNameSteam, tmpGrpName, ',')) {
252 63 : grpNameList.push_back(tmpGrpName);
253 : }
254 35 : }
255 :
256 27 : std::string ArgsParser::GetParaParsedStr()
257 : {
258 27 : std::ostringstream oss;
259 27 : oss << "deviceId=" << deviceId_ << ", hostPid=" << hostPid_ << ", pidSign=" << pidSign_
260 27 : << ", profilingMode=" << profilingMode_ << ", vfId=" << vfId_ << ", logLevel=" << logLevel_
261 27 : << ", ccecpulogLevel=" << ccecpulogLevel_ << ", aicpulogLevel=" << aicpulogLevel_
262 27 : << ", deviceMode=" << deviceMode_ << ", hostProcName=" << hostProcName_
263 27 : << ", grpNameNum=" << grpNameNum_ << ", aicpuProcNum=" << aicpuProcNum_
264 27 : << ", grpNameList=[";
265 :
266 69 : for (const std::string &iter : grpNameList_) {
267 42 : oss << iter << ",";
268 : }
269 27 : oss << "]";
270 :
271 54 : return oss.str();
272 27 : }
273 : }
|