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