• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_resource.h"
17 
18 #include "string_operation.h"
19 #include "thermal_hisysevent.h"
20 #include "thermal_service.h"
21 
22 namespace OHOS {
23 namespace PowerMgr {
24 namespace {
25 constexpr int32_t RESID_IDX = 0;
26 constexpr int32_t FALLBACK_IDX = 1;
27 }
ActionSocPerfResource(const std::string & actionName)28 ActionSocPerfResource::ActionSocPerfResource(const std::string& actionName)
29 {
30     actionName_ = actionName;
31     SocActionBase::SocSet.insert(actionName_);
32 }
33 
InitParams(const std::string & params)34 void ActionSocPerfResource::InitParams(const std::string& params)
35 {
36     std::vector<std::string> paramList;
37     StringOperation::SplitString(params, paramList, "|");
38     int32_t paramNum = static_cast<int32_t>(paramList.size());
39     if (paramNum > FALLBACK_IDX) {
40         StrToInt(paramList[RESID_IDX], resId_);
41         StringOperation::StrToUint(paramList[FALLBACK_IDX].c_str(), fallbackValue_);
42     } else if (paramNum > RESID_IDX) {
43         StrToInt(paramList[RESID_IDX], resId_);
44     }
45 }
46 
SetStrict(bool enable)47 void ActionSocPerfResource::SetStrict(bool enable)
48 {
49     isStrict_ = enable;
50 }
51 
SetEnableEvent(bool enable)52 void ActionSocPerfResource::SetEnableEvent(bool enable)
53 {
54     enableEvent_ = enable;
55 }
56 
AddActionValue(uint32_t actionId,std::string value)57 void ActionSocPerfResource::AddActionValue(uint32_t actionId, std::string value)
58 {
59     if (value.empty()) {
60         THERMAL_HILOGD(COMP_SVC, "value empty");
61         return;
62     }
63     uint32_t newValue = 0;
64     if (!StringOperation::StrToUint(value, newValue)) {
65         return;
66     }
67     if (actionId > 0) {
68         auto iter = policyActionMap_.find(actionId);
69         if (iter != policyActionMap_.end()) {
70             iter->second.uintDelayValue = newValue;
71         }
72     } else {
73         valueList_.push_back(newValue);
74     }
75 }
76 
ExecuteInner()77 void ActionSocPerfResource::ExecuteInner()
78 {
79     auto tms = ThermalService::GetInstance();
80     THERMAL_RETURN_IF (tms == nullptr);
81     for (auto &policyAction : policyActionMap_) {
82         if (policyAction.second.isCompleted) {
83             valueList_.push_back(policyAction.second.uintDelayValue);
84         }
85     }
86 
87     uint32_t value = GetActionValue();
88     if (value != lastValue_) {
89         SocLimitRequest(resId_, value);
90         WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
91         if (tms->GetObserver() != nullptr) {
92             tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
93         }
94         lastValue_ = value;
95         THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
96     }
97     valueList_.clear();
98 }
99 
ResetActionValue()100 void ActionSocPerfResource::ResetActionValue()
101 {
102     lastValue_ = 0;
103 }
104 
GetActionValue()105 uint32_t ActionSocPerfResource::GetActionValue()
106 {
107     uint32_t value = fallbackValue_;
108     if (!valueList_.empty()) {
109         if (isStrict_) {
110             value = *min_element(valueList_.begin(), valueList_.end());
111         } else {
112             value = *max_element(valueList_.begin(), valueList_.end());
113         }
114     }
115     return value;
116 }
117 } // namespace PowerMgr
118 } // namespace OHOS