• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "socperf_client.h"
17 
18 namespace OHOS {
19 namespace SOCPERF {
GetInstance()20 SocPerfClient& SocPerfClient::GetInstance()
21 {
22     static SocPerfClient instance;
23     return instance;
24 }
25 
CheckClientValid()26 bool SocPerfClient::CheckClientValid()
27 {
28     if (client) {
29         return true;
30     }
31 
32     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33     if (!samgr) {
34         SOC_PERF_LOGE("Failed to get SystemAbilityManager.");
35         return false;
36     }
37 
38     sptr<IRemoteObject> object = samgr->GetSystemAbility(SOC_PERF_SERVICE_SA_ID);
39     if (!object) {
40         SOC_PERF_LOGE("Failed to get SystemAbility[1906] .");
41         return false;
42     }
43 
44     client = iface_cast<ISocPerfService>(object);
45     if (!client || !client->AsObject()) {
46         SOC_PERF_LOGE("Failed to get SocPerfClient.");
47         return false;
48     }
49 
50     recipient_ = new SocPerfDeathRecipient(*this);
51     if (!recipient_) {
52         return false;
53     }
54     client->AsObject()->AddDeathRecipient(recipient_);
55 
56     return true;
57 }
58 
ResetClient()59 void SocPerfClient::ResetClient()
60 {
61     if (client && client->AsObject()) {
62         client->AsObject()->RemoveDeathRecipient(recipient_);
63     }
64     client = nullptr;
65 }
66 
SocPerfDeathRecipient(SocPerfClient & socPerfClient)67 SocPerfClient::SocPerfDeathRecipient::SocPerfDeathRecipient(SocPerfClient &socPerfClient)
68     : socPerfClient_(socPerfClient) {};
69 
~SocPerfDeathRecipient()70 SocPerfClient::SocPerfDeathRecipient::~SocPerfDeathRecipient() {};
71 
OnRemoteDied(const wptr<IRemoteObject> & remote)72 void SocPerfClient::SocPerfDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
73 {
74     socPerfClient_.ResetClient();
75 }
76 
AddPidAndTidInfo(const std::string & msg)77 std::string SocPerfClient::AddPidAndTidInfo(const std::string& msg)
78 {
79     std::string str;
80     int pid = getpid();
81     int tid = gettid();
82     str.append("pid=").append(std::to_string(pid)).append("|");
83     str.append("tid=").append(std::to_string(tid));
84     if (msg.size() > 0) {
85         str.append("|").append(msg);
86     }
87     return str;
88 }
89 
PerfRequest(int cmdId,const std::string & msg)90 void SocPerfClient::PerfRequest(int cmdId, const std::string& msg)
91 {
92     if (!CheckClientValid()) {
93         return;
94     }
95     std::string newMsg = AddPidAndTidInfo(msg);
96     client->PerfRequest(cmdId, newMsg);
97 }
98 
PerfRequestEx(int cmdId,bool onOffTag,const std::string & msg)99 void SocPerfClient::PerfRequestEx(int cmdId, bool onOffTag, const std::string& msg)
100 {
101     if (!CheckClientValid()) {
102         return;
103     }
104     std::string newMsg = AddPidAndTidInfo(msg);
105     client->PerfRequestEx(cmdId, onOffTag, newMsg);
106 }
107 
PowerRequest(int cmdId,const std::string & msg)108 void SocPerfClient::PowerRequest(int cmdId, const std::string& msg)
109 {
110     if (!CheckClientValid()) {
111         return;
112     }
113     std::string newMsg = AddPidAndTidInfo(msg);
114     client->PowerRequest(cmdId, newMsg);
115 }
116 
PowerRequestEx(int cmdId,bool onOffTag,const std::string & msg)117 void SocPerfClient::PowerRequestEx(int cmdId, bool onOffTag, const std::string& msg)
118 {
119     if (!CheckClientValid()) {
120         return;
121     }
122     std::string newMsg = AddPidAndTidInfo(msg);
123     client->PowerRequestEx(cmdId, onOffTag, newMsg);
124 }
125 
PowerLimitBoost(bool onOffTag,const std::string & msg)126 void SocPerfClient::PowerLimitBoost(bool onOffTag, const std::string& msg)
127 {
128     if (!CheckClientValid()) {
129         return;
130     }
131     std::string newMsg = AddPidAndTidInfo(msg);
132     client->PowerLimitBoost(onOffTag, newMsg);
133 }
134 
ThermalRequest(int cmdId,const std::string & msg)135 void SocPerfClient::ThermalRequest(int cmdId, const std::string& msg)
136 {
137     if (!CheckClientValid()) {
138         return;
139     }
140     std::string newMsg = AddPidAndTidInfo(msg);
141     client->ThermalRequest(cmdId, newMsg);
142 }
143 
ThermalRequestEx(int cmdId,bool onOffTag,const std::string & msg)144 void SocPerfClient::ThermalRequestEx(int cmdId, bool onOffTag, const std::string& msg)
145 {
146     if (!CheckClientValid()) {
147         return;
148     }
149     std::string newMsg = AddPidAndTidInfo(msg);
150     client->ThermalRequestEx(cmdId, onOffTag, newMsg);
151 }
152 
ThermalLimitBoost(bool onOffTag,const std::string & msg)153 void SocPerfClient::ThermalLimitBoost(bool onOffTag, const std::string& msg)
154 {
155     if (!CheckClientValid()) {
156         return;
157     }
158     std::string newMsg = AddPidAndTidInfo(msg);
159     client->ThermalLimitBoost(onOffTag, newMsg);
160 }
161 } // namespace SOCPERF
162 } // namespace OHOS
163