1 /*
2 * Copyright (c) 2025 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_socperf.h"
17
18 #include "string_operation.h"
19 #include "thermal_hisysevent.h"
20 #include "thermal_service.h"
21
22 namespace OHOS {
23 namespace PowerMgr {
ActionSocPerf(const std::string & actionName)24 ActionSocPerf::ActionSocPerf(const std::string& actionName)
25 {
26 actionName_ = actionName;
27 SocActionBase::SocSet.insert(actionName_);
28 }
29
InitParams(const std::string & params)30 void ActionSocPerf::InitParams(const std::string& params)
31 {
32 StrToInt(params, cmdId_);
33 }
34
SetStrict(bool enable)35 void ActionSocPerf::SetStrict(bool enable)
36 {
37 isStrict_ = enable;
38 }
39
SetEnableEvent(bool enable)40 void ActionSocPerf::SetEnableEvent(bool enable)
41 {
42 enableEvent_ = enable;
43 }
44
AddActionValue(uint32_t actionId,std::string value)45 void ActionSocPerf::AddActionValue(uint32_t actionId, std::string value)
46 {
47 if (value.empty()) {
48 THERMAL_HILOGD(COMP_SVC, "value empty");
49 return;
50 }
51 uint32_t newValue = 0;
52 if (!StringOperation::StrToUint(value, newValue)) {
53 return;
54 }
55 if (actionId > 0) {
56 auto iter = policyActionMap_.find(actionId);
57 if (iter != policyActionMap_.end()) {
58 iter->second.uintDelayValue = newValue;
59 }
60 } else {
61 valueList_.push_back(newValue);
62 }
63 }
64
ExecuteInner()65 void ActionSocPerf::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 #ifdef SOC_PERF_ENABLE
78 SocPerfRequestEx(cmdId_, value > 0);
79 #endif
80 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
81 if (tms->GetObserver() != nullptr) {
82 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
83 }
84 lastValue_ = value;
85 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
86 }
87 valueList_.clear();
88 }
89
ResetActionValue()90 void ActionSocPerf::ResetActionValue()
91 {
92 lastValue_ = 0;
93 }
94
GetActionValue()95 uint32_t ActionSocPerf::GetActionValue()
96 {
97 uint32_t value = FALLBACK_VALUE_UINT_ZERO;
98 if (!valueList_.empty()) {
99 if (isStrict_) {
100 value = *min_element(valueList_.begin(), valueList_.end());
101 } else {
102 value = *max_element(valueList_.begin(), valueList_.end());
103 }
104 }
105 return value;
106 }
107 } // namespace PowerMgr
108 } // namespace OHOS
109