1 /*
2 * Copyright (c) 2022 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 <map>
19 #include <unistd.h>
20 #include "socperf_client.h"
21 #include "thermal_hisysevent.h"
22 #include "thermal_service.h"
23
24 namespace OHOS {
25 namespace PowerMgr {
26 namespace {
27 constexpr int32_t LIM_CPU_LIT_ID = 1001;
28 constexpr int32_t ACTION_TYPE_THERMAL_ID = 2;
29 auto g_service = DelayedSpSingleton<ThermalService>::GetInstance();
30 }
ActionCpuLit(const std::string & actionName)31 ActionCpuLit::ActionCpuLit(const std::string& actionName)
32 {
33 actionName_ = actionName;
34 }
35
InitParams(const std::string & params)36 void ActionCpuLit::InitParams(const std::string& params)
37 {
38 }
39
SetStrict(bool flag)40 void ActionCpuLit::SetStrict(bool flag)
41 {
42 flag_ = flag;
43 }
44
SetEnableEvent(bool enable)45 void ActionCpuLit::SetEnableEvent(bool enable)
46 {
47 enableEvent_ = enable;
48 }
49
AddActionValue(std::string value)50 void ActionCpuLit::AddActionValue(std::string value)
51 {
52 if (value.empty()) return;
53 valueList_.push_back(atoi(value.c_str()));
54 }
55
Execute()56 void ActionCpuLit::Execute()
57 {
58 THERMAL_RETURN_IF (g_service == nullptr);
59 uint32_t value;
60 std::string scene = g_service->GetScene();
61 auto iter = g_sceneMap.find(scene);
62 if (iter != g_sceneMap.end()) {
63 value = static_cast<uint32_t>(atoi(iter->second.c_str()));
64 if (value != lastValue_) {
65 CpuRuquest(value);
66 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
67 g_service->GetObserver()->SetDecisionValue(actionName_, iter->second);
68 lastValue_ = value;
69 valueList_.clear();
70 }
71 return;
72 }
73
74 if (valueList_.empty()) {
75 value = 0;
76 } else {
77 if (flag_) {
78 value = *max_element(valueList_.begin(), valueList_.end());
79 } else {
80 value = *min_element(valueList_.begin(), valueList_.end());
81 }
82 valueList_.clear();
83 }
84
85 THERMAL_HILOGD(COMP_SVC, "Enter value=%{public}d, lastValue_=%{public}d", value, lastValue_);
86 if (value != lastValue_) {
87 CpuRuquest(value);
88 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
89 g_service->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
90 lastValue_ = value;
91 }
92 }
93
CpuRuquest(uint32_t freq)94 int32_t ActionCpuLit::CpuRuquest(uint32_t freq)
95 {
96 std::vector<int32_t> tags;
97 std::vector<int64_t> configs;
98
99 if (!g_service->GetSimulationXml()) {
100 tags.push_back(LIM_CPU_LIT_ID);
101 configs.push_back(freq);
102 OHOS::SOCPERF::SocPerfClient::GetInstance().LimitRequest(ACTION_TYPE_THERMAL_ID, tags, configs, "");
103 } else {
104 auto thermalInterface = g_service->GetThermalInterface();
105 if (thermalInterface != nullptr) {
106 int32_t ret = thermalInterface->SetCpuFreq(freq);
107 if (ret != ERR_OK) {
108 return ret;
109 }
110 }
111 }
112
113 return ERR_OK;
114 }
115 } // namespace PowerMgr
116 } // namespace OHOS
117