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_isolate.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
ActionCpuIsolate(const std::string & actionName)27 ActionCpuIsolate::ActionCpuIsolate(const std::string& actionName)
28 {
29 actionName_ = actionName;
30 SocActionBase::SocSet.insert(actionName_);
31 }
32
InitParams(const std::string & params)33 void ActionCpuIsolate::InitParams(const std::string& params)
34 {
35 (void)params;
36 }
37
SetStrict(bool enable)38 void ActionCpuIsolate::SetStrict(bool enable)
39 {
40 isStrict_ = enable;
41 }
42
SetEnableEvent(bool enable)43 void ActionCpuIsolate::SetEnableEvent(bool enable)
44 {
45 enableEvent_ = enable;
46 }
47
AddActionValue(uint32_t actionId,std::string value)48 void ActionCpuIsolate::AddActionValue(uint32_t actionId, std::string value)
49 {
50 if (value.empty()) {
51 return;
52 }
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 ActionCpuIsolate::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 SocIsolateRequest(value > 0);
78 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
79 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
80 lastValue_ = value;
81 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
82 }
83 valueList_.clear();
84 }
85
ResetActionValue()86 void ActionCpuIsolate::ResetActionValue()
87 {
88 lastValue_ = UINT_MAX;
89 }
90
GetActionValue()91 uint32_t ActionCpuIsolate::GetActionValue()
92 {
93 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
94 if (!valueList_.empty()) {
95 if (isStrict_) {
96 value = *min_element(valueList_.begin(), valueList_.end());
97 } else {
98 value = *max_element(valueList_.begin(), valueList_.end());
99 }
100 }
101 return value;
102 }
103 } // namespace PowerMgr
104 } // namespace OHOS
105