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_med.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 
ActionCpuMed(const std::string & actionName)27 ActionCpuMed::ActionCpuMed(const std::string& actionName)
28 {
29     actionName_ = actionName;
30 }
31 
InitParams(const std::string & params)32 void ActionCpuMed::InitParams(const std::string& params)
33 {
34     (void)params;
35 }
36 
SetStrict(bool enable)37 void ActionCpuMed::SetStrict(bool enable)
38 {
39     isStrict_ = enable;
40 }
41 
SetEnableEvent(bool enable)42 void ActionCpuMed::SetEnableEvent(bool enable)
43 {
44     enableEvent_ = enable;
45 }
46 
AddActionValue(std::string value)47 void ActionCpuMed::AddActionValue(std::string value)
48 {
49     if (value.empty()) {
50         return;
51     }
52     valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
53 }
54 
Execute()55 void ActionCpuMed::Execute()
56 {
57     auto tms = ThermalService::GetInstance();
58     THERMAL_RETURN_IF (tms == nullptr);
59     uint32_t value = GetActionValue();
60     if (value != lastValue_) {
61         SocLimitRequest(LIM_CPU_MED_ID, value);
62         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
63         tms->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 ActionCpuMed::GetActionValue()
71 {
72     uint32_t value = FALLBACK_VALUE_UINT_SOC;
73     if (!valueList_.empty()) {
74         if (isStrict_) {
75             value = *min_element(valueList_.begin(), valueList_.end());
76         } else {
77             value = *max_element(valueList_.begin(), valueList_.end());
78         }
79     }
80     return value;
81 }
82 } // namespace PowerMgr
83 } // namespace OHOS
84