1 /*
2 * Copyright (c) 2022-2024 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 "socperf_client.h"
17 #include <unistd.h> // for getpid, gettid
18 #include "iservice_registry.h"
19 #include "isoc_perf.h" // for ISocPerf
20 #include "socperf_log.h"
21 #include "system_ability_definition.h"
22
23 namespace {
24 OHOS::sptr<OHOS::SOCPERF::ISocPerf> client = nullptr;
25 }
26
27 namespace OHOS {
28 namespace SOCPERF {
29 const int32_t MAX_MODE_LEN = 64;
30
SocPerfClient()31 SocPerfClient::SocPerfClient()
32 {
33 }
34
~SocPerfClient()35 SocPerfClient::~SocPerfClient()
36 {
37 SOC_PERF_LOGI("SocPerfClient:~SocPerfClien");
38 ResetClient();
39 }
40
GetInstance()41 SocPerfClient& SocPerfClient::GetInstance()
42 {
43 static SocPerfClient instance;
44 return instance;
45 }
46
CheckClientValid()47 bool SocPerfClient::CheckClientValid()
48 {
49 if (client) {
50 return true;
51 }
52
53 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54 if (!samgr) {
55 SOC_PERF_LOGE("Failed to get SystemAbilityManager.");
56 return false;
57 }
58
59 sptr<IRemoteObject> object = samgr->CheckSystemAbility(SOC_PERF_SERVICE_SA_ID);
60 if (!object) {
61 SOC_PERF_LOGE("Failed to get SystemAbility[1906].");
62 return false;
63 }
64
65 client = iface_cast<ISocPerf>(object);
66 if (!client || !client->AsObject()) {
67 SOC_PERF_LOGE("Failed to get SocPerfClient.");
68 return false;
69 }
70
71 recipient_ = new (std::nothrow) SocPerfDeathRecipient(*this);
72 if (!recipient_) {
73 return false;
74 }
75 client->AsObject()->AddDeathRecipient(recipient_);
76 SOC_PERF_LOGI("SocPerfClient:new client");
77 return true;
78 }
79
ResetClient()80 void SocPerfClient::ResetClient()
81 {
82 std::lock_guard<std::mutex> lock(mutex_);
83 SOC_PERF_LOGI("SocPerfClient:ResetClient");
84 if (client && client->AsObject()) {
85 client->AsObject()->RemoveDeathRecipient(recipient_);
86 }
87 client = nullptr;
88 }
89
SocPerfDeathRecipient(SocPerfClient & socPerfClient)90 SocPerfClient::SocPerfDeathRecipient::SocPerfDeathRecipient(SocPerfClient &socPerfClient)
91 : socPerfClient_(socPerfClient) {}
92
~SocPerfDeathRecipient()93 SocPerfClient::SocPerfDeathRecipient::~SocPerfDeathRecipient() {}
94
OnRemoteDied(const wptr<IRemoteObject> & remote)95 void SocPerfClient::SocPerfDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
96 {
97 socPerfClient_.ResetClient();
98 }
99
AddPidAndTidInfo(const std::string & msg)100 std::string SocPerfClient::AddPidAndTidInfo(const std::string& msg)
101 {
102 std::string str;
103 int32_t pid = getpid();
104 int32_t tid = gettid();
105 str.append("pid=").append(std::to_string(pid)).append("|");
106 str.append("tid=").append(std::to_string(tid));
107 if (msg.size() > 0) {
108 str.append("|").append(msg);
109 }
110 return str;
111 }
112
PerfRequest(int32_t cmdId,const std::string & msg)113 void SocPerfClient::PerfRequest(int32_t cmdId, const std::string& msg)
114 {
115 std::lock_guard<std::mutex> lock(mutex_);
116 if (!CheckClientValid()) {
117 return;
118 }
119 std::string newMsg = AddPidAndTidInfo(msg);
120 client->PerfRequest(cmdId, newMsg);
121 }
122
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)123 void SocPerfClient::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
124 {
125 std::lock_guard<std::mutex> lock(mutex_);
126 if (!CheckClientValid()) {
127 return;
128 }
129 std::string newMsg = AddPidAndTidInfo(msg);
130 client->PerfRequestEx(cmdId, onOffTag, newMsg);
131 }
132
PowerLimitBoost(bool onOffTag,const std::string & msg)133 void SocPerfClient::PowerLimitBoost(bool onOffTag, const std::string& msg)
134 {
135 std::lock_guard<std::mutex> lock(mutex_);
136 if (!CheckClientValid()) {
137 return;
138 }
139 std::string newMsg = AddPidAndTidInfo(msg);
140 client->PowerLimitBoost(onOffTag, newMsg);
141 }
142
ThermalLimitBoost(bool onOffTag,const std::string & msg)143 void SocPerfClient::ThermalLimitBoost(bool onOffTag, const std::string& msg)
144 {
145 std::lock_guard<std::mutex> lock(mutex_);
146 if (!CheckClientValid()) {
147 return;
148 }
149 std::string newMsg = AddPidAndTidInfo(msg);
150 client->ThermalLimitBoost(onOffTag, newMsg);
151 }
152
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)153 void SocPerfClient::LimitRequest(int32_t clientId,
154 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
155 {
156 std::lock_guard<std::mutex> lock(mutex_);
157 if (!CheckClientValid()) {
158 return;
159 }
160 std::string newMsg = AddPidAndTidInfo(msg);
161 client->LimitRequest(clientId, tags, configs, newMsg);
162 }
163
SetRequestStatus(bool status,const std::string & msg)164 void SocPerfClient::SetRequestStatus(bool status, const std::string& msg)
165 {
166 std::lock_guard<std::mutex> lock(mutex_);
167 if (!CheckClientValid()) {
168 return;
169 }
170 std::string newMsg = AddPidAndTidInfo(msg);
171 client->SetRequestStatus(status, newMsg);
172 }
173
SetThermalLevel(int32_t level)174 void SocPerfClient::SetThermalLevel(int32_t level)
175 {
176 std::lock_guard<std::mutex> lock(mutex_);
177 if (!CheckClientValid()) {
178 return;
179 }
180 client->SetThermalLevel(level);
181 }
182
RequestDeviceMode(const std::string & mode,bool status)183 void SocPerfClient::RequestDeviceMode(const std::string& mode, bool status)
184 {
185 std::lock_guard<std::mutex> lock(mutex_);
186 if (!CheckClientValid() || mode.length() > MAX_MODE_LEN) {
187 return;
188 }
189 client->RequestDeviceMode(mode, status);
190 }
191
RequestCmdIdCount(const std::string & msg)192 std::string SocPerfClient::RequestCmdIdCount(const std::string& msg)
193 {
194 std::lock_guard<std::mutex> lock(mutex_);
195 if (!CheckClientValid()) {
196 return "";
197 }
198 std::string funcResult;
199 client->RequestCmdIdCount(msg, funcResult);
200 return funcResult;
201 }
202 } // namespace SOCPERF
203 } // namespace OHOS
204