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 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
26 }
27
ActionCpuIsolate(const std::string & actionName)28 ActionCpuIsolate::ActionCpuIsolate(const std::string& actionName)
29 {
30 actionName_ = 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(std::string value)48 void ActionCpuIsolate::AddActionValue(std::string value)
49 {
50 if (value.empty()) {
51 return;
52 }
53 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
54 }
55
Execute()56 void ActionCpuIsolate::Execute()
57 {
58 THERMAL_RETURN_IF (g_service == nullptr);
59 uint32_t value = GetActionValue();
60 if (value != lastValue_) {
61 RequestCpuIsolate(value);
62 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
63 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
64 lastValue_ = value;
65 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
66 }
67 valueList_.clear();
68 }
69
GetActionValue()70 uint32_t ActionCpuIsolate::GetActionValue()
71 {
72 std::string scene = g_service->GetScene();
73 auto iter = g_sceneMap.find(scene);
74 if (iter != g_sceneMap.end()) {
75 return static_cast<uint32_t>(strtol(iter->second.c_str(), nullptr, STRTOL_FORMART_DEC));
76 }
77 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
78 if (!valueList_.empty()) {
79 if (isStrict_) {
80 value = *min_element(valueList_.begin(), valueList_.end());
81 } else {
82 value = *max_element(valueList_.begin(), valueList_.end());
83 }
84 }
85 return value;
86 }
87
RequestCpuIsolate(uint32_t isolateNum)88 void ActionCpuIsolate::RequestCpuIsolate(uint32_t isolateNum)
89 {
90 auto thermalInterface = g_service->GetThermalInterface();
91 if (thermalInterface != nullptr) {
92 int32_t ret = thermalInterface->IsolateCpu(isolateNum);
93 if (ret != ERR_OK) {
94 THERMAL_HILOGE(COMP_SVC, "failed to isolate cpu %u to thermal hdi", isolateNum);
95 return;
96 }
97 }
98 return;
99 }
100 } // namespace PowerMgr
101 } // namespace OHOS
102