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