1 /*
2  * Copyright (c) 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 #include "dump_manager_cpu_client.h"
16 #include <iservice_registry.h>
17 #include <string_ex.h>
18 #include <unistd.h>
19 #include "hilog_wrapper.h"
20 #include "dump_errors.h"
21 #include "inner/dump_service_id.h"
22 #include "dump_on_demand_load.h"
23 #include "dump_broker_cpu_proxy.h"
24 namespace OHOS {
25 namespace HiviewDFX {
DumpManagerCpuClient()26 DumpManagerCpuClient::DumpManagerCpuClient()
27 {
28 }
29 
~DumpManagerCpuClient()30 DumpManagerCpuClient::~DumpManagerCpuClient()
31 {
32     Reset();
33 }
34 
Request(DumpCpuData & dumpCpuData)35 int32_t DumpManagerCpuClient::Request(DumpCpuData &dumpCpuData)
36 {
37     if (Connect() != ERR_OK) {
38         DUMPER_HILOGE(MODULE_CPU_CLIENT, "debug|cpu connect error");
39         return DumpStatus::DUMP_FAIL;
40     }
41     int32_t ret = proxy_->Request(dumpCpuData);
42     return ret;
43 }
44 
GetCpuUsageByPid(int32_t pid,double & cpuUsage)45 int32_t DumpManagerCpuClient::GetCpuUsageByPid(int32_t pid, double &cpuUsage)
46 {
47     if (Connect() != ERR_OK) {
48         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu connect error");
49         return DumpStatus::DUMP_FAIL;
50     }
51     int32_t ret = proxy_->GetCpuUsageByPid(pid, cpuUsage);
52     return ret;
53 }
54 
Connect()55 ErrCode DumpManagerCpuClient::Connect()
56 {
57     std::lock_guard<std::mutex> lock(mutex_);
58     if (proxy_ != nullptr) {
59         return ERR_OK;
60     }
61     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
62     if (sam == nullptr) {
63         DUMPER_HILOGE(MODULE_CPU_CLIENT, "sam is null");
64         return ERROR_GET_SYSTEM_ABILITY_MANAGER;
65     }
66     sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_CPU_ABILITY_ID);
67     if (remoteObject == nullptr) {
68         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu remoteobject is null");
69         return ERROR_GET_DUMPER_SERVICE;
70     }
71     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DumpManagerCpuDeathRecipient());
72     if (deathRecipient_ == nullptr) {
73         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu deathRecipient_ is null");
74         return ERR_NO_MEMORY;
75     }
76     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
77         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu IsProxyObject is null");
78         return ERROR_ADD_DEATH_RECIPIENT;
79     }
80     proxy_ = iface_cast<IDumpCpuBroker>(remoteObject);
81     return ERR_OK;
82 }
83 
Reset()84 void DumpManagerCpuClient::Reset()
85 {
86     std::lock_guard<std::mutex> lock(mutex_);
87     if (proxy_ == nullptr) {
88         return;
89     }
90     auto serviceRemote = proxy_->AsObject();
91     if (serviceRemote != nullptr) {
92         serviceRemote->RemoveDeathRecipient(deathRecipient_);
93         proxy_ = nullptr;
94         DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
95     }
96 }
97 
ResetProxy(const wptr<IRemoteObject> & remote)98 void DumpManagerCpuClient::ResetProxy(const wptr<IRemoteObject>& remote)
99 {
100     std::lock_guard<std::mutex> lock(mutex_);
101     if (proxy_ == nullptr) {
102         return;
103     }
104     auto serviceRemote = proxy_->AsObject();
105     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
106         serviceRemote->RemoveDeathRecipient(deathRecipient_);
107         proxy_ = nullptr;
108         DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
109     }
110 }
111 
OnRemoteDied(const wptr<IRemoteObject> & remote)112 void DumpManagerCpuClient::DumpManagerCpuDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
113 {
114     if (remote == nullptr) {
115         return;
116     }
117     DumpManagerCpuClient::GetInstance().ResetProxy(remote);
118 }
119 } // namespace HiviewDFX
120 } // namespace OHOS
121