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 42 : bool ArgsParser::ParseArgs(const int32_t argc, const char_t * const argv[])
17 : {
18 42 : if ((argv == nullptr) || (argc <= 1)) {
19 1 : return false;
20 : }
21 :
22 400 : for (uint32_t i = 0; i < static_cast<uint32_t>(argc); ++i) {
23 728 : if (!ParseSinglePara(std::string(argv[i]))) {
24 5 : return false;
25 : }
26 : }
27 :
28 36 : return CheckRequiredParas();
29 : }
30 :
31 36 : bool ArgsParser::CheckRequiredParas() const
32 : {
33 36 : std::vector<bool> requiredParams = {withDeviceId_, withHostPid_, withPidSign_, withCustSoPath_,
34 72 : withAicpuPid_, withGrpNameNum_, withGrpNameList_};
35 242 : for (const auto &iter : requiredParams) {
36 217 : if (!iter) {
37 11 : return false;
38 : }
39 : }
40 :
41 25 : return true;
42 36 : }
43 :
44 368 : bool ArgsParser::ParseSinglePara(const std::string &singlePara)
45 : {
46 368 : const std::size_t offset = singlePara.find("=");
47 368 : if (offset == std::string::npos) {
48 37 : return true;
49 : }
50 :
51 331 : const std::string key = singlePara.substr(0U, offset+1UL);
52 331 : const std::string val = singlePara.substr(offset+1UL);
53 331 : const auto iter = argsParseFuncMap_.find(key);
54 331 : if (iter == argsParseFuncMap_.end()) {
55 : // Ignore unknown parameter
56 2 : return true;
57 : }
58 :
59 329 : const bool ret = (this->*(iter->second))(val);
60 329 : if (!ret) {
61 6 : return false;
62 : }
63 :
64 323 : return true;
65 331 : }
66 :
67 46 : bool ArgsParser::ParseDeviceId(const std::string ¶)
68 : {
69 46 : int32_t val = 0;
70 46 : if (!AicpuUtil::TransStrToInt(para, val)) {
71 1 : return false;
72 : }
73 :
74 45 : if ((val < 0) || (val >= AICPUFW_CHIP_NUM_MAX)) {
75 2 : return false;
76 : }
77 :
78 43 : deviceId_ = static_cast<uint32_t>(val);
79 43 : withDeviceId_ = true;
80 43 : return true;
81 : }
82 :
83 41 : bool ArgsParser::ParseHostPid(const std::string ¶)
84 : {
85 41 : int32_t val = 0;
86 41 : if (!AicpuUtil::TransStrToInt(para, val)) {
87 1 : return false;
88 : }
89 :
90 40 : if (val <= 0) {
91 2 : return false;
92 : }
93 :
94 38 : hostPid_ = static_cast<uint32_t>(val);
95 38 : withHostPid_ = true;
96 38 : return true;
97 : }
98 :
99 37 : bool ArgsParser::ParseSign(const std::string ¶)
100 : {
101 37 : pidSign_ = para;
102 37 : withPidSign_ = true;
103 37 : return true;
104 : }
105 :
106 38 : bool ArgsParser::ParseProfilingMode(const std::string ¶)
107 : {
108 38 : uint32_t val = 0U;
109 38 : if (!AicpuUtil::TransStrToUint(para, val)) {
110 1 : return false;
111 : }
112 :
113 37 : profilingMode_ = val;
114 37 : return true;
115 : }
116 :
117 34 : bool ArgsParser::ParseLogAndEventLevel(const std::string ¶)
118 : {
119 34 : int32_t val = 0;
120 34 : if (!AicpuUtil::TransStrToInt(para, val)) {
121 1 : return false;
122 : }
123 :
124 33 : if (val < 0) {
125 1 : return false;
126 : }
127 :
128 32 : logLevel_ = val % VALUE_FOR_CALCULATE_LOG_LEVEL;
129 32 : eventLevel_ = val / VALUE_FOR_CALCULATE_LOG_LEVEL;
130 32 : return true;
131 : }
132 :
133 2 : bool ArgsParser::ParseCcecpuLogLevel(const std::string ¶)
134 : {
135 2 : int32_t val = 0;
136 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
137 1 : return true;
138 : }
139 :
140 1 : ccecpulogLevel_ = val;
141 1 : return true;
142 : }
143 :
144 2 : bool ArgsParser::ParseAicpuLogLevel(const std::string ¶)
145 : {
146 2 : int32_t val = 0;
147 2 : if (!AicpuUtil::TransStrToInt(para, val)) {
148 1 : return true;
149 : }
150 :
151 1 : aicpulogLevel_ = val;
152 1 : return true;
153 : }
154 :
155 19 : bool ArgsParser::ParseVfId(const std::string ¶)
156 : {
157 19 : int32_t val = 0;
158 19 : if (!AicpuUtil::TransStrToInt(para, val)) {
159 1 : return false;
160 : }
161 :
162 18 : if ((val < 0) || (val > VF_ID_MAX)) {
163 3 : return false;
164 : }
165 :
166 15 : vfId_ = static_cast<uint32_t>(val);
167 15 : return true;
168 : }
169 :
170 29 : bool ArgsParser::ParseCustSoPath(const std::string ¶)
171 : {
172 29 : custSoPath_ = para;
173 29 : withCustSoPath_ = true;
174 29 : return true;
175 : }
176 :
177 30 : bool ArgsParser::ParseAicpuPid(const std::string ¶)
178 : {
179 30 : int32_t val = 0;
180 30 : if (!AicpuUtil::TransStrToInt(para, val)) {
181 1 : return false;
182 : }
183 :
184 29 : if (val <= 0) {
185 1 : return false;
186 : }
187 :
188 28 : aicpuPid_ = static_cast<uint32_t>(val);
189 28 : withAicpuPid_ = true;
190 28 : return true;
191 : }
192 :
193 32 : bool ArgsParser::ParseGrpNameNum(const std::string ¶)
194 : {
195 32 : int32_t val = 0;
196 32 : if (!AicpuUtil::TransStrToInt(para, val)) {
197 2 : return false;
198 : }
199 :
200 30 : if (val < 0) {
201 2 : return false;
202 : }
203 :
204 28 : grpNameNum_ = static_cast<uint32_t>(val);
205 28 : withGrpNameNum_ = true;
206 28 : return true;
207 : }
208 :
209 29 : bool ArgsParser::ParseGrpNameList(const std::string ¶)
210 : {
211 29 : grpNameList_.clear();
212 29 : ArgsParser::SplitGrpNameList(para, grpNameList_);
213 29 : withGrpNameList_ = true;
214 29 : return true;
215 : }
216 :
217 30 : void ArgsParser::SplitGrpNameList(const std::string &grpNamePara, std::vector<std::string> &grpNameList)
218 : {
219 30 : std::stringstream grpNameSteam(grpNamePara);
220 30 : std::string tmpGrpName;
221 88 : while (getline(grpNameSteam, tmpGrpName, ',')) {
222 58 : grpNameList.push_back(tmpGrpName);
223 : }
224 30 : }
225 :
226 10 : void ArgsParser::SplitControlCpuList(const std::string &cupListPara, std::vector<uint32_t> &controlCpuList)
227 : {
228 10 : std::stringstream cpuListSteam(cupListPara);
229 10 : std::string cpuName;
230 28 : while (getline(cpuListSteam, cpuName, ',')) {
231 19 : uint32_t val = 0U;
232 19 : if (!AicpuUtil::TransStrToUint(cpuName, val)) {
233 1 : controlCpuList.clear();
234 1 : return;
235 : }
236 18 : controlCpuList.push_back(val);
237 : }
238 11 : }
239 :
240 10 : bool ArgsParser::ParseCtrolCpuList(const std::string ¶)
241 : {
242 10 : controlCpuList_.clear();
243 10 : ArgsParser::SplitControlCpuList(para, controlCpuList_);
244 10 : withControlCpuList_ = true;
245 10 : return true;
246 : }
247 :
248 10 : bool ArgsParser::ParseTsdPid(const std::string ¶)
249 : {
250 10 : tsdPid_ = 0U;
251 10 : uint32_t val = 0U;
252 10 : if (!AicpuUtil::TransStrToUint(para, val)) {
253 1 : return true;
254 : }
255 9 : tsdPid_ = val;
256 9 : withTsdPid_ = true;
257 9 : return true;
258 : }
259 :
260 2 : std::string ArgsParser::GetParaParsedStr()
261 : {
262 2 : std::ostringstream oss;
263 2 : oss << "deviceId=" << deviceId_ << ", hostPid=" << hostPid_ << ", pidSign=" << pidSign_
264 2 : << ", profilingMode=" << profilingMode_ << ", vfId=" << vfId_ << ", logLevel=" << logLevel_
265 2 : << ", ccecpulogLevel=" << ccecpulogLevel_ << ", aicpulogLevel=" << aicpulogLevel_
266 2 : << ", aicpuPid=" << aicpuPid_ << ", grpNameNum=" << grpNameNum_ << ", grpNameList=[";
267 :
268 5 : for (const std::string &iter : grpNameList_) {
269 3 : oss << iter << ",";
270 : }
271 :
272 2 : oss << "], " << "custSoPath=" << custSoPath_ <<", ctrolCpuList=[";
273 4 : for (const uint32_t &iter : controlCpuList_) {
274 2 : oss << std::to_string(iter) << ",";
275 : }
276 2 : oss<<"]"<<", tsdPid=" << tsdPid_;
277 :
278 4 : return oss.str();
279 2 : }
280 : }
|