1 /*
2 * Copyright (c) 2022-2023 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 "action_cpu_lit.h"
17
18 #include "constants.h"
19 #include "thermal_hisysevent.h"
20 #include "thermal_service.h"
21
22 namespace OHOS {
23 namespace PowerMgr {
24 namespace {
25 }
26
ActionCpuLit(const std::string & actionName)27 ActionCpuLit::ActionCpuLit(const std::string& actionName)
28 {
29 actionName_ = actionName;
30 SocActionBase::SocSet.insert(actionName_);
31 }
32
InitParams(const std::string & params)33 void ActionCpuLit::InitParams(const std::string& params)
34 {
35 (void)params;
36 }
37
SetStrict(bool enable)38 void ActionCpuLit::SetStrict(bool enable)
39 {
40 isStrict_ = enable;
41 }
42
SetEnableEvent(bool enable)43 void ActionCpuLit::SetEnableEvent(bool enable)
44 {
45 enableEvent_ = enable;
46 }
47
AddActionValue(uint32_t actionId,std::string value)48 void ActionCpuLit::AddActionValue(uint32_t actionId, std::string value)
49 {
50 if (value.empty()) {
51 return;
52 }
53 if (actionId > 0) {
54 auto iter = policyActionMap_.find(actionId);
55 if (iter != policyActionMap_.end()) {
56 iter->second.uintDelayValue = static_cast<uint32_t>(strtol(value.c_str(),
57 nullptr, STRTOL_FORMART_DEC));
58 }
59 } else {
60 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
61 }
62 }
63
ExecuteInner()64 void ActionCpuLit::ExecuteInner()
65 {
66 auto tms = ThermalService::GetInstance();
67 THERMAL_RETURN_IF (tms == nullptr);
68 for (auto &policyAction : policyActionMap_) {
69 if (policyAction.second.isCompleted) {
70 valueList_.push_back(policyAction.second.uintDelayValue);
71 }
72 }
73
74 uint32_t value = GetActionValue();
75 if (value != lastValue_) {
76 SocLimitRequest(LIM_CPU_LIT_ID, value);
77 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
78 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
79 lastValue_ = value;
80 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
81 }
82 valueList_.clear();
83 }
84
ResetActionValue()85 void ActionCpuLit::ResetActionValue()
86 {
87 lastValue_ = 0;
88 }
89
GetActionValue()90 uint32_t ActionCpuLit::GetActionValue()
91 {
92 uint32_t value = FALLBACK_VALUE_UINT_SOC;
93 if (!valueList_.empty()) {
94 if (isStrict_) {
95 value = *min_element(valueList_.begin(), valueList_.end());
96 } else {
97 value = *max_element(valueList_.begin(), valueList_.end());
98 }
99 }
100 return value;
101 }
102 } // namespace PowerMgr
103 } // namespace OHOS
104