1 /*
2 * Copyright (c) 2021-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_gpu.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
ActionGpu(const std::string & actionName)27 ActionGpu::ActionGpu(const std::string& actionName)
28 {
29 actionName_ = actionName;
30 SocActionBase::SocSet.insert(actionName_);
31 }
32
InitParams(const std::string & params)33 void ActionGpu::InitParams(const std::string& params)
34 {
35 (void)params;
36 }
37
SetStrict(bool enable)38 void ActionGpu::SetStrict(bool enable)
39 {
40 isStrict_ = enable;
41 }
42
SetEnableEvent(bool enable)43 void ActionGpu::SetEnableEvent(bool enable)
44 {
45 enableEvent_ = enable;
46 }
47
AddActionValue(uint32_t actionId,std::string value)48 void ActionGpu::AddActionValue(uint32_t actionId, std::string value)
49 {
50 if (value.empty()) {
51 return;
52 }
53 if (actionId > 0) {
54 auto iter = policyActionMap_.find(actionId);
55 if (iter != policyActionMap_.end()) {
56 iter->second.uintDelayValue = static_cast<uint32_t>(strtol(value.c_str(),
57 nullptr, STRTOL_FORMART_DEC));
58 }
59 } else {
60 valueList_.push_back(static_cast<uint32_t>(strtol(value.c_str(), nullptr, STRTOL_FORMART_DEC)));
61 }
62 }
63
ResetActionValue()64 void ActionGpu::ResetActionValue()
65 {
66 lastValue_ = 0;
67 }
68
ExecuteInner()69 void ActionGpu::ExecuteInner()
70 {
71 auto tms = ThermalService::GetInstance();
72 THERMAL_RETURN_IF (tms == nullptr);
73 for (auto &policyAction : policyActionMap_) {
74 if (policyAction.second.isCompleted) {
75 valueList_.push_back(policyAction.second.uintDelayValue);
76 }
77 }
78
79 uint32_t value = GetActionValue();
80 if (value != lastValue_) {
81 SocLimitRequest(LIM_GPU_ID, value);
82 WriteActionTriggeredHiSysEvent(enableEvent_, actionName_, value);
83 tms->GetObserver()->SetDecisionValue(actionName_, std::to_string(value));
84 lastValue_ = value;
85 THERMAL_HILOGD(COMP_SVC, "action execute: {%{public}s = %{public}u}", actionName_.c_str(), lastValue_);
86 }
87 valueList_.clear();
88 }
89
GetActionValue()90 uint32_t ActionGpu::GetActionValue()
91 {
92 uint32_t value = FALLBACK_VALUE_UINT_SOC;
93 if (!valueList_.empty()) {
94 if (isStrict_) {
95 value = *min_element(valueList_.begin(), valueList_.end());
96 } else {
97 value = *max_element(valueList_.begin(), valueList_.end());
98 }
99 }
100 return value;
101 }
102 } // namespace PowermMgr
103 } // namespace OHOS
104