1 /*
2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "socperf.h"
17
18 #include "parameters.h"
19 #include "socperf_trace.h"
20
21 namespace OHOS {
22 namespace SOCPERF {
23 namespace {
24 const int64_t TIME_INTERVAL = 8;
25 const int32_t CANCEL_CMDID_PREFIX = 100000;
26 const std::string DEFAULT_MODE = "default";
27 const std::string SPLIT_COLON = ":";
28 const int32_t DEVICEMODE_PARAM_NUMBER = 2;
29 const int32_t MODE_TYPE_INDEX = 0;
30 const int32_t MODE_NAME_INDEX = 1;
31 }
SocPerf()32 SocPerf::SocPerf()
33 {
34 }
35
~SocPerf()36 SocPerf::~SocPerf()
37 {
38 socperfThreadWrap_ = nullptr;
39 }
40
Init()41 bool SocPerf::Init()
42 {
43 if (!socPerfConfig_.Init()) {
44 SOC_PERF_LOGE("Failed to init SocPerf config");
45 return false;
46 }
47
48 if (!CreateThreadWraps()) {
49 SOC_PERF_LOGE("Failed to create threadwraps threads");
50 return false;
51 }
52 InitThreadWraps();
53 enabled_ = true;
54 return true;
55 }
56
CreateThreadWraps()57 bool SocPerf::CreateThreadWraps()
58 {
59 #ifdef SOCPERF_ADAPTOR_FFRT
60 socperfThreadWrap_ = std::make_shared<SocPerfThreadWrap>();
61 #else
62 auto runner = AppExecFwk::EventRunner::Create("socperf#runner");
63 if (!runner) {
64 SOC_PERF_LOGE("Failed to Create EventRunner");
65 return false;
66 }
67 socperfThreadWrap_ = std::make_shared<SocPerfThreadWrap>(runner);
68 #endif
69 if (!socperfThreadWrap_) {
70 SOC_PERF_LOGE("Failed to Create socPerfThreadWrap");
71 return false;
72 }
73 SOC_PERF_LOGD("Success to Create All threadWrap threads");
74 return true;
75 }
76
InitThreadWraps()77 void SocPerf::InitThreadWraps()
78 {
79 #ifdef SOCPERF_ADAPTOR_FFRT
80 socperfThreadWrap_->InitResourceNodeInfo();
81 #else
82 auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_INIT_RESOURCE_NODE_INFO);
83 socperfThreadWrap_->SendEvent(event);
84 #endif
85 }
86
PerfRequest(int32_t cmdId,const std::string & msg)87 void SocPerf::PerfRequest(int32_t cmdId, const std::string& msg)
88 {
89 if (!enabled_ || !perfRequestEnable_) {
90 SOC_PERF_LOGD("SocPerf disabled!");
91 return;
92 }
93 if (!CheckTimeInterval(true, cmdId)) {
94 SOC_PERF_LOGD("cmdId %{public}d can not trigger, because time interval", cmdId);
95 return;
96 }
97 if (socPerfConfig_.perfActionsInfo_.find(cmdId) == socPerfConfig_.perfActionsInfo_.end()) {
98 SOC_PERF_LOGD("Invalid PerfRequest cmdId[%{public}d]", cmdId);
99 return;
100 }
101
102 int32_t matchCmdId = MatchDeviceModeCmd(cmdId, false);
103 SOC_PERF_LOGD("cmdId[%{public}d]matchCmdId[%{public}d]msg[%{public}s]", cmdId, matchCmdId, msg.c_str());
104
105 std::string trace_str(__func__);
106 trace_str.append(",cmdId[").append(std::to_string(matchCmdId)).append("]");
107 trace_str.append(",msg[").append(msg).append("]");
108 SOCPERF_TRACE_BEGIN(trace_str);
109 DoFreqActions(socPerfConfig_.perfActionsInfo_[matchCmdId], EVENT_INVALID, ACTION_TYPE_PERF);
110 SOCPERF_TRACE_END();
111 UpdateCmdIdCount(cmdId);
112 }
113
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)114 void SocPerf::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
115 {
116 if (!enabled_ || !perfRequestEnable_) {
117 SOC_PERF_LOGD("SocPerf disabled!");
118 return;
119 }
120 if (socPerfConfig_.perfActionsInfo_.find(cmdId) == socPerfConfig_.perfActionsInfo_.end()) {
121 SOC_PERF_LOGD("Invalid PerfRequestEx cmdId[%{public}d]", cmdId);
122 return;
123 }
124 if (!CheckTimeInterval(onOffTag, cmdId)) {
125 SOC_PERF_LOGD("cmdId %{public}d can not trigger, because time interval", cmdId);
126 return;
127 }
128 int32_t matchCmdId = MatchDeviceModeCmd(cmdId, true);
129 SOC_PERF_LOGD("cmdId[%{public}d]matchCmdId[%{public}d]onOffTag[%{public}d]msg[%{public}s]",
130 cmdId, matchCmdId, onOffTag, msg.c_str());
131
132 std::string trace_str(__func__);
133 trace_str.append(",cmdId[").append(std::to_string(matchCmdId)).append("]");
134 trace_str.append(",onOff[").append(std::to_string(onOffTag)).append("]");
135 trace_str.append(",msg[").append(msg).append("]");
136 SOCPERF_TRACE_BEGIN(trace_str);
137 DoFreqActions(socPerfConfig_.perfActionsInfo_[matchCmdId], onOffTag ? EVENT_ON : EVENT_OFF, ACTION_TYPE_PERF);
138 SOCPERF_TRACE_END();
139 if (onOffTag) {
140 UpdateCmdIdCount(cmdId);
141 }
142 }
143
PowerLimitBoost(bool onOffTag,const std::string & msg)144 void SocPerf::PowerLimitBoost(bool onOffTag, const std::string& msg)
145 {
146 if (!enabled_) {
147 SOC_PERF_LOGD("SocPerf disabled!");
148 return;
149 }
150 SOC_PERF_LOGI("onOffTag[%{public}d]msg[%{public}s]", onOffTag, msg.c_str());
151
152 std::string trace_str(__func__);
153 trace_str.append(",onOff[").append(std::to_string(onOffTag)).append("]");
154 trace_str.append(",msg[").append(msg).append("]");
155 SOCPERF_TRACE_BEGIN(trace_str);
156 #ifdef SOCPERF_ADAPTOR_FFRT
157 socperfThreadWrap_->UpdatePowerLimitBoostFreq(onOffTag);
158 #else
159 auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_POWER_LIMIT_BOOST_FREQ, onOffTag ? 1 : 0);
160 socperfThreadWrap_->SendEvent(event);
161 #endif
162 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::RSS, "LIMIT_BOOST",
163 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
164 "CLIENT_ID", ACTION_TYPE_POWER,
165 "ON_OFF_TAG", onOffTag);
166 SOCPERF_TRACE_END();
167 }
168
ThermalLimitBoost(bool onOffTag,const std::string & msg)169 void SocPerf::ThermalLimitBoost(bool onOffTag, const std::string& msg)
170 {
171 if (!enabled_) {
172 SOC_PERF_LOGD("SocPerf disabled!");
173 return;
174 }
175 SOC_PERF_LOGI("onOffTag[%{public}d]msg[%{public}s]", onOffTag, msg.c_str());
176 std::string trace_str(__func__);
177 trace_str.append(",onOff[").append(std::to_string(onOffTag)).append("]");
178 trace_str.append(",msg[").append(msg).append("]");
179 SOCPERF_TRACE_BEGIN(trace_str);
180 #ifdef SOCPERF_ADAPTOR_FFRT
181 socperfThreadWrap_->UpdateThermalLimitBoostFreq(onOffTag);
182 #else
183 auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_THERMAL_LIMIT_BOOST_FREQ, onOffTag ? 1 : 0);
184 socperfThreadWrap_->SendEvent(event);
185 #endif
186 HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::RSS, "LIMIT_BOOST",
187 OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
188 "CLIENT_ID", ACTION_TYPE_THERMAL,
189 "ON_OFF_TAG", onOffTag);
190 SOCPERF_TRACE_END();
191 }
192
SendLimitRequestEventOff(std::shared_ptr<SocPerfThreadWrap> threadWrap,int32_t clientId,int32_t resId,int32_t eventId)193 void SocPerf::SendLimitRequestEventOff(std::shared_ptr<SocPerfThreadWrap> threadWrap,
194 int32_t clientId, int32_t resId, int32_t eventId)
195 {
196 auto iter = limitRequest_[clientId].find(resId);
197 if (iter != limitRequest_[clientId].end()
198 && limitRequest_[clientId][resId] != INVALID_VALUE) {
199 auto resAction = std::make_shared<ResAction>(
200 limitRequest_[clientId][resId], 0, clientId, EVENT_OFF, -1, MAX_INT_VALUE);
201 #ifdef SOCPERF_ADAPTOR_FFRT
202 threadWrap->UpdateLimitStatus(eventId, resAction, resId);
203 #else
204 auto event = AppExecFwk::InnerEvent::Get(eventId, resAction, resId);
205 threadWrap->SendEvent(event);
206 #endif
207 limitRequest_[clientId].erase(iter);
208 }
209 }
210
SendLimitRequestEventOn(std::shared_ptr<SocPerfThreadWrap> threadWrap,int32_t clientId,int32_t resId,int64_t resValue,int32_t eventId)211 void SocPerf::SendLimitRequestEventOn(std::shared_ptr<SocPerfThreadWrap> threadWrap,
212 int32_t clientId, int32_t resId, int64_t resValue, int32_t eventId)
213 {
214 if (resValue != INVALID_VALUE && resValue != RESET_VALUE) {
215 auto resAction = std::make_shared<ResAction>(resValue, 0, clientId, EVENT_ON, -1, MAX_INT_VALUE);
216 #ifdef SOCPERF_ADAPTOR_FFRT
217 threadWrap->UpdateLimitStatus(eventId, resAction, resId);
218 #else
219 auto event = AppExecFwk::InnerEvent::Get(eventId, resAction, resId);
220 threadWrap->SendEvent(event);
221 #endif
222 limitRequest_[clientId].insert(std::pair<int32_t, int32_t>(resId, resValue));
223 }
224 }
225
SendLimitRequestEvent(int32_t clientId,int32_t resId,int64_t resValue)226 void SocPerf::SendLimitRequestEvent(int32_t clientId, int32_t resId, int64_t resValue)
227 {
228 int32_t eventId = 0;
229 int32_t realResId = 0;
230 int32_t levelResId = 0;
231 if (resId > RES_ID_ADDITION) {
232 realResId = resId - RES_ID_ADDITION;
233 levelResId = resId;
234 eventId = INNER_EVENT_ID_DO_FREQ_ACTION_LEVEL;
235 } else {
236 realResId = resId;
237 levelResId = resId + RES_ID_ADDITION;
238 eventId = INNER_EVENT_ID_DO_FREQ_ACTION;
239 }
240
241 if (!socPerfConfig_.IsValidResId(realResId)) {
242 return;
243 }
244 std::lock_guard<std::mutex> lock(mutex_);
245 SendLimitRequestEventOff(socperfThreadWrap_, clientId, realResId, INNER_EVENT_ID_DO_FREQ_ACTION);
246 SendLimitRequestEventOff(socperfThreadWrap_, clientId, levelResId, INNER_EVENT_ID_DO_FREQ_ACTION_LEVEL);
247 SendLimitRequestEventOn(socperfThreadWrap_, clientId, resId, resValue, eventId);
248 }
249
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)250 void SocPerf::LimitRequest(int32_t clientId,
251 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
252 {
253 if (!enabled_) {
254 SOC_PERF_LOGE("SocPerf disabled!");
255 return;
256 }
257 if (tags.size() != configs.size()) {
258 SOC_PERF_LOGE("tags'size and configs' size must be the same!");
259 return;
260 }
261 if (clientId <= (int32_t)ACTION_TYPE_PERF || clientId >= (int32_t)ACTION_TYPE_MAX) {
262 SOC_PERF_LOGE("clientId must be between ACTION_TYPE_PERF and ACTION_TYPE_MAX!");
263 return;
264 }
265 std::string trace_str(__func__);
266 trace_str.append(",clientId[").append(std::to_string(clientId)).append("]");
267 trace_str.append(",msg[").append(msg).append("]");
268 for (int32_t i = 0; i < (int32_t)tags.size(); i++) {
269 trace_str.append(",tags[").append(std::to_string(tags[i])).append("]");
270 trace_str.append(",configs[").append(std::to_string(configs[i])).append("]");
271 SendLimitRequestEvent(clientId, tags[i], configs[i]);
272 }
273 SOCPERF_TRACE_BEGIN(trace_str);
274 SOC_PERF_LOGI("socperf limit %{public}s", trace_str.c_str());
275 SOCPERF_TRACE_END();
276 }
277
SetRequestStatus(bool status,const std::string & msg)278 void SocPerf::SetRequestStatus(bool status, const std::string& msg)
279 {
280 SOC_PERF_LOGI("requestEnable is changed to %{public}d, the reason is %{public}s", status, msg.c_str());
281 perfRequestEnable_ = status;
282 /* disable socperf sever, we should clear all alive request to avoid high freq for long time */
283 if (!perfRequestEnable_) {
284 ClearAllAliveRequest();
285 }
286 }
287
ClearAllAliveRequest()288 void SocPerf::ClearAllAliveRequest()
289 {
290 if (!enabled_) {
291 SOC_PERF_LOGE("SocPerf disabled!");
292 return;
293 }
294 #ifdef SOCPERF_ADAPTOR_FFRT
295 socperfThreadWrap_->ClearAllAliveRequest();
296 #else
297 auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_CLEAR_ALL_ALIVE_REQUEST);
298 socperfThreadWrap_->SendEvent(event);
299 #endif
300 }
301
SetThermalLevel(int32_t level)302 void SocPerf::SetThermalLevel(int32_t level)
303 {
304 if (!enabled_) {
305 SOC_PERF_LOGE("SocPerf disabled!");
306 return;
307 }
308 std::string trace_str(__func__);
309 trace_str.append(",level[").append(std::to_string(level)).append("]");
310 SOCPERF_TRACE_BEGIN(trace_str);
311 SOC_PERF_LOGI("ThermalLevel:%{public}d", level);
312 SOCPERF_TRACE_END();
313 thermalLvl_ = level;
314 socperfThreadWrap_->thermalLvl_ = level;
315 }
316
DoPerfRequestThremalLvl(int32_t cmdId,std::shared_ptr<Action> action,int32_t onOff)317 bool SocPerf::DoPerfRequestThremalLvl(int32_t cmdId, std::shared_ptr<Action> action, int32_t onOff)
318 {
319 if (socPerfConfig_.perfActionsInfo_[action->thermalCmdId_] == nullptr) {
320 SOC_PERF_LOGE("cmd %{public}d is not exist", action->thermalCmdId_);
321 return false;
322 }
323 // init DoFreqActions param
324 std::string thermalLvlTag = std::string("ThremalLvl_").append(std::to_string(action->thermalCmdId_))
325 .append("_").append(std::to_string(thermalLvl_));
326 std::shared_ptr<Actions> perfLvlActionCmd = std::make_shared<Actions>(cmdId, thermalLvlTag);
327 std::shared_ptr<Action> perfLvlAction = std::make_shared<Action>();
328 // perfrequest thermal level action's duration is same as trigger
329 perfLvlAction->duration = action->duration;
330 std::shared_ptr<Actions> cmdConfig = socPerfConfig_.perfActionsInfo_[action->thermalCmdId_];
331
332 // select the Nearest thermallevel action
333 std::shared_ptr<Action> actionConfig = *(cmdConfig->actionList.begin());
334 for (auto iter = cmdConfig->actionList.begin(); iter != cmdConfig->actionList.end(); iter++) {
335 if (perfLvlAction->thermalLvl_ <= (*iter)->thermalLvl_ && (*iter)->thermalLvl_ <= thermalLvl_) {
336 actionConfig = *iter;
337 }
338 }
339 if (thermalLvl_ < actionConfig->thermalLvl_) {
340 SOC_PERF_LOGD("thermal level is too low to trigger perf request level");
341 return false;
342 }
343
344 // fill in the item of perfLvlAction
345 perfLvlAction->thermalLvl_ = actionConfig->thermalLvl_;
346 perfLvlAction->thermalCmdId_ = INVALID_THERMAL_CMD_ID;
347 for (uint32_t i = 0; i < actionConfig->variable.size(); i++) {
348 perfLvlAction->variable.push_back(actionConfig->variable[i]);
349 }
350 perfLvlActionCmd->actionList.push_back(perfLvlAction);
351
352 // send cmd to socperf server wrapper
353 DoFreqActions(perfLvlActionCmd, onOff, ACTION_TYPE_PERFLVL);
354 return true;
355 }
356
DoFreqActions(std::shared_ptr<Actions> actions,int32_t onOff,int32_t actionType)357 void SocPerf::DoFreqActions(std::shared_ptr<Actions> actions, int32_t onOff, int32_t actionType)
358 {
359 std::shared_ptr<ResActionItem> header = nullptr;
360 std::shared_ptr<ResActionItem> curItem = nullptr;
361 auto now = std::chrono::system_clock::now();
362 int64_t curMs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
363 for (auto iter = actions->actionList.begin(); iter != actions->actionList.end(); iter++) {
364 std::shared_ptr<Action> action = *iter;
365 // process thermal level
366 if (action->thermalCmdId_ != INVALID_THERMAL_CMD_ID && thermalLvl_ > MIN_THERMAL_LVL) {
367 DoPerfRequestThremalLvl(actions->id, action, onOff);
368 }
369 for (int32_t i = 0; i < (int32_t)action->variable.size() - 1; i += RES_ID_AND_VALUE_PAIR) {
370 if (!socPerfConfig_.IsValidResId(action->variable[i])) {
371 continue;
372 }
373
374 if (onOff == EVENT_INVALID && action->duration == 0) {
375 continue;
376 }
377
378 auto resActionItem = std::make_shared<ResActionItem>(action->variable[i]);
379 int64_t endTime = action->duration == 0 ? MAX_INT_VALUE : curMs + action->duration;
380 resActionItem->resAction = std::make_shared<ResAction>(action->variable[i + 1], action->duration,
381 actionType, onOff, actions->id, endTime);
382 if (actions->interaction == false) {
383 resActionItem->resAction->interaction = false;
384 }
385 if (curItem) {
386 curItem->next = resActionItem;
387 } else {
388 header = resActionItem;
389 }
390 curItem = resActionItem;
391 }
392 }
393 #ifdef SOCPERF_ADAPTOR_FFRT
394 socperfThreadWrap_->DoFreqActionPack(header);
395 socperfThreadWrap_->PostDelayTask(header);
396 #else
397 auto event = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_DO_FREQ_ACTION_PACK, header);
398 socperfThreadWrap_->SendEvent(event);
399 std::shared_ptr<ResActionItem> queueHead = header;
400 while (queueHead) {
401 auto eventRes = AppExecFwk::InnerEvent::Get(INNER_EVENT_ID_DO_FREQ_ACTION_DELAYED, queueHead->resAction,
402 queueHead->resId);
403 socperfThreadWrap_->SendEvent(eventRes, queueHead->resAction->duration);
404 queueHead = queueHead->next;
405 }
406 #endif
407 }
408
RequestDeviceMode(const std::string & mode,bool status)409 void SocPerf::RequestDeviceMode(const std::string& mode, bool status)
410 {
411 SOC_PERF_LOGD("device mode %{public}s status changed to %{public}d", mode.c_str(), status);
412
413 if (mode.empty() || mode.length() > MAX_RES_MODE_LEN) {
414 return;
415 }
416
417 std::vector<std::string> modeParamList = Split(mode, SPLIT_COLON);
418 if (modeParamList.size() != DEVICEMODE_PARAM_NUMBER) {
419 SOC_PERF_LOGE("device mode %{public}s format is wrong.", mode.c_str());
420 return;
421 }
422
423 const std::string modeType = modeParamList[MODE_TYPE_INDEX];
424 const std::string modeName = modeParamList[MODE_NAME_INDEX];
425
426 auto iter = socPerfConfig_.sceneResourceInfo_.find(modeType);
427 if (iter == socPerfConfig_.sceneResourceInfo_.end()) {
428 SOC_PERF_LOGD("No matching device mode found.");
429 return;
430 }
431
432 const std::shared_ptr<SceneResNode> sceneResNode = iter->second;
433 const std::vector<std::shared_ptr<SceneItem>> items = sceneResNode->items;
434 const int32_t persistMode = sceneResNode->persistMode;
435
436 const std::string modeStr = MatchDeviceMode(modeName, status, items);
437 if (persistMode == REPORT_TO_PERFSO && socPerfConfig_.scenarioFunc_) {
438 const std::string msgStr = modeType + ":" + modeStr;
439 SOC_PERF_LOGD("send deviceMode to PerfScenario : %{public}s", msgStr.c_str());
440 socPerfConfig_.scenarioFunc_(msgStr);
441 }
442 }
443
MatchDeviceMode(const std::string & mode,bool status,const std::vector<std::shared_ptr<SceneItem>> & items)444 std::string SocPerf::MatchDeviceMode(const std::string& mode, bool status,
445 const std::vector<std::shared_ptr<SceneItem>>& items)
446 {
447 std::lock_guard<std::mutex> lock(mutexDeviceMode_);
448
449 if (!status) {
450 recordDeviceMode_.erase(mode);
451 return DEFAULT_MODE;
452 }
453
454 std::string itemName = DEFAULT_MODE;
455 for (const auto& iter : items) {
456 if (iter->name == mode) {
457 recordDeviceMode_.insert(mode);
458 if (iter->req == REPORT_TO_PERFSO) {
459 itemName = mode;
460 }
461 } else {
462 recordDeviceMode_.erase(iter->name);
463 }
464 }
465 return itemName;
466 }
467
MatchDeviceModeCmd(int32_t cmdId,bool isTagOnOff)468 int32_t SocPerf::MatchDeviceModeCmd(int32_t cmdId, bool isTagOnOff)
469 {
470 std::shared_ptr<Actions> actions = socPerfConfig_.perfActionsInfo_[cmdId];
471 if (actions->modeMap.empty() || (isTagOnOff && actions->isLongTimePerf)) {
472 return cmdId;
473 }
474
475 std::lock_guard<std::mutex> lock(mutexDeviceMode_);
476 if (recordDeviceMode_.empty()) {
477 return cmdId;
478 }
479
480 for (const auto& iter : actions->modeMap) {
481 auto deviceMode = recordDeviceMode_.find(iter->mode);
482 if (deviceMode != recordDeviceMode_.end()) {
483 int32_t deviceCmdId = iter->cmdId;
484 if (socPerfConfig_.perfActionsInfo_.find(deviceCmdId) == socPerfConfig_.perfActionsInfo_.end()) {
485 SOC_PERF_LOGW("Invaild actions cmdid %{public}d", deviceCmdId);
486 return cmdId;
487 }
488 if (isTagOnOff && socPerfConfig_.perfActionsInfo_[deviceCmdId]->isLongTimePerf) {
489 SOC_PERF_LOGD("long time perf not match cmdId %{public}d", deviceCmdId);
490 return cmdId;
491 }
492 return deviceCmdId;
493 }
494 }
495 return cmdId;
496 }
497
UpdateCmdIdCount(int32_t cmdId)498 void SocPerf::UpdateCmdIdCount(int32_t cmdId)
499 {
500 std::lock_guard<std::mutex> lock(mutexBoostCmdCount_);
501 if (boostCmdCount_.find(cmdId) == boostCmdCount_.end()) {
502 boostCmdCount_[cmdId] = 0;
503 }
504 boostCmdCount_[cmdId]++;
505 }
506
RequestCmdIdCount(const std::string & msg)507 std::string SocPerf::RequestCmdIdCount(const std::string &msg)
508 {
509 std::lock_guard<std::mutex> lock(mutexBoostCmdCount_);
510 std::stringstream ret;
511 for (const auto& pair : boostCmdCount_) {
512 if (ret.str().length() > 0) {
513 ret << ",";
514 }
515 ret << pair.first << ":" << pair.second;
516 }
517 return ret.str();
518 }
519
CheckTimeInterval(bool onOff,int32_t cmdId)520 bool SocPerf::CheckTimeInterval(bool onOff, int32_t cmdId)
521 {
522 std::lock_guard<std::mutex> lock(mutexBoostTime_);
523 auto now = std::chrono::system_clock::now();
524 uint64_t curMs = static_cast<uint64_t>(
525 std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count());
526 int32_t cancelCmdId = cmdId + CANCEL_CMDID_PREFIX;
527 int32_t recordCmdId = cmdId;
528 if (onOff) {
529 boostTime_[cancelCmdId] = 0;
530 }
531 if (!onOff) {
532 recordCmdId = cancelCmdId;
533 }
534 if (boostTime_.find(recordCmdId) == boostTime_.end()) {
535 boostTime_[recordCmdId] = curMs;
536 return true;
537 }
538 if (curMs - boostTime_[recordCmdId] > TIME_INTERVAL) {
539 boostTime_[recordCmdId] = curMs;
540 return true;
541 }
542 return false;
543 }
544 } // namespace SOCPERF
545 } // namespace OHOS
546