1 /*
2 * Copyright (c) 2022-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 "socperf_client.h"
17 #include <string> // for basic_string, to_string
18 #include <unistd.h> // for getpid, gettid
19
20 namespace OHOS {
21 namespace SOCPERF {
GetInstance()22 SocPerfClient& SocPerfClient::GetInstance()
23 {
24 static SocPerfClient instance;
25 return instance;
26 }
27
CheckClientValid()28 bool SocPerfClient::CheckClientValid()
29 {
30 std::lock_guard<std::mutex> lock(mutex_);
31 if (client) {
32 return true;
33 }
34
35 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
36 if (!samgr) {
37 SOC_PERF_LOGE("%{public}s, Failed to get SystemAbilityManager.", __func__);
38 return false;
39 }
40
41 sptr<IRemoteObject> object = samgr->CheckSystemAbility(SOC_PERF_SERVICE_SA_ID);
42 if (!object) {
43 SOC_PERF_LOGE("%{public}s, Failed to get SystemAbility[1906].", __func__);
44 return false;
45 }
46
47 client = iface_cast<ISocPerfService>(object);
48 if (!client || !client->AsObject()) {
49 SOC_PERF_LOGE("%{public}s, Failed to get SocPerfClient.", __func__);
50 return false;
51 }
52
53 recipient_ = new (std::nothrow) SocPerfDeathRecipient(*this);
54 if (!recipient_) {
55 return false;
56 }
57 client->AsObject()->AddDeathRecipient(recipient_);
58
59 return true;
60 }
61
ResetClient()62 void SocPerfClient::ResetClient()
63 {
64 std::lock_guard<std::mutex> lock(mutex_);
65 if (client && client->AsObject()) {
66 client->AsObject()->RemoveDeathRecipient(recipient_);
67 }
68 client = nullptr;
69 }
70
SocPerfDeathRecipient(SocPerfClient & socPerfClient)71 SocPerfClient::SocPerfDeathRecipient::SocPerfDeathRecipient(SocPerfClient &socPerfClient)
72 : socPerfClient_(socPerfClient) {}
73
~SocPerfDeathRecipient()74 SocPerfClient::SocPerfDeathRecipient::~SocPerfDeathRecipient() {}
75
OnRemoteDied(const wptr<IRemoteObject> & remote)76 void SocPerfClient::SocPerfDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
77 {
78 socPerfClient_.ResetClient();
79 }
80
AddPidAndTidInfo(const std::string & msg)81 std::string SocPerfClient::AddPidAndTidInfo(const std::string& msg)
82 {
83 std::string str;
84 int32_t pid = getpid();
85 int32_t tid = gettid();
86 str.append("pid=").append(std::to_string(pid)).append("|");
87 str.append("tid=").append(std::to_string(tid));
88 if (msg.size() > 0) {
89 str.append("|").append(msg);
90 }
91 return str;
92 }
93
PerfRequest(int32_t cmdId,const std::string & msg)94 void SocPerfClient::PerfRequest(int32_t cmdId, const std::string& msg)
95 {
96 if (!CheckClientValid()) {
97 return;
98 }
99 std::string newMsg = AddPidAndTidInfo(msg);
100 client->PerfRequest(cmdId, newMsg);
101 }
102
PerfRequestEx(int32_t cmdId,bool onOffTag,const std::string & msg)103 void SocPerfClient::PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg)
104 {
105 if (!CheckClientValid()) {
106 return;
107 }
108 std::string newMsg = AddPidAndTidInfo(msg);
109 client->PerfRequestEx(cmdId, onOffTag, newMsg);
110 }
111
PowerLimitBoost(bool onOffTag,const std::string & msg)112 void SocPerfClient::PowerLimitBoost(bool onOffTag, const std::string& msg)
113 {
114 if (!CheckClientValid()) {
115 return;
116 }
117 std::string newMsg = AddPidAndTidInfo(msg);
118 client->PowerLimitBoost(onOffTag, newMsg);
119 }
120
ThermalLimitBoost(bool onOffTag,const std::string & msg)121 void SocPerfClient::ThermalLimitBoost(bool onOffTag, const std::string& msg)
122 {
123 if (!CheckClientValid()) {
124 return;
125 }
126 std::string newMsg = AddPidAndTidInfo(msg);
127 client->ThermalLimitBoost(onOffTag, newMsg);
128 }
129
LimitRequest(int32_t clientId,const std::vector<int32_t> & tags,const std::vector<int64_t> & configs,const std::string & msg)130 void SocPerfClient::LimitRequest(int32_t clientId,
131 const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg)
132 {
133 if (!CheckClientValid()) {
134 return;
135 }
136 std::string newMsg = AddPidAndTidInfo(msg);
137 client->LimitRequest(clientId, tags, configs, newMsg);
138 }
139 } // namespace SOCPERF
140 } // namespace OHOS
141