• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (proxy_ != nullptr) {
33         auto remoteObject = proxy_->AsObject();
34         if (remoteObject != nullptr) {
35             remoteObject->RemoveDeathRecipient(deathRecipient_);
36         }
37     }
38 }
39 
Request(DumpCpuData & dumpCpuData)40 int32_t DumpManagerCpuClient::Request(DumpCpuData &dumpCpuData)
41 {
42     if (Connect() != ERR_OK) {
43         DUMPER_HILOGE(MODULE_CPU_CLIENT, "debug|cpu connect error");
44         return DumpStatus::DUMP_FAIL;
45     }
46     int32_t ret = proxy_->Request(dumpCpuData);
47     return ret;
48 }
49 
GetCpuUsageByPid(int32_t pid,double & cpuUsage)50 int32_t DumpManagerCpuClient::GetCpuUsageByPid(int32_t pid, double &cpuUsage)
51 {
52     if (Connect() != ERR_OK) {
53         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu connect error");
54         return DumpStatus::DUMP_FAIL;
55     }
56     int32_t ret = proxy_->GetCpuUsageByPid(pid, cpuUsage);
57     return ret;
58 }
59 
Connect()60 ErrCode DumpManagerCpuClient::Connect()
61 {
62     std::lock_guard<std::mutex> lock(mutex_);
63     if (proxy_ != nullptr) {
64         return ERR_OK;
65     }
66     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
67     if (sam == nullptr) {
68         DUMPER_HILOGE(MODULE_CPU_CLIENT, "sam is null");
69         return ERROR_GET_SYSTEM_ABILITY_MANAGER;
70     }
71     sptr<IRemoteObject> remoteObject = sam->CheckSystemAbility(DFX_SYS_HIDUMPER_CPU_ABILITY_ID);
72     if (remoteObject == nullptr) {
73         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu remoteobject is null");
74         return ERROR_GET_DUMPER_SERVICE;
75     }
76     deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DumpManagerCpuDeathRecipient());
77     if (deathRecipient_ == nullptr) {
78         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu deathRecipient_ is null");
79         return ERR_NO_MEMORY;
80     }
81     if ((remoteObject->IsProxyObject()) && (!remoteObject->AddDeathRecipient(deathRecipient_))) {
82         DUMPER_HILOGE(MODULE_CPU_CLIENT, "cpu IsProxyObject is null");
83         return ERROR_ADD_DEATH_RECIPIENT;
84     }
85     proxy_ = iface_cast<IDumpCpuBroker>(remoteObject);
86     return ERR_OK;
87 }
88 
Reset()89 void DumpManagerCpuClient::Reset()
90 {
91     std::lock_guard<std::mutex> lock(mutex_);
92     if (proxy_ == nullptr) {
93         return;
94     }
95     auto serviceRemote = proxy_->AsObject();
96     if (serviceRemote != nullptr) {
97         serviceRemote->RemoveDeathRecipient(deathRecipient_);
98         proxy_ = nullptr;
99         DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
100     }
101 }
102 
ResetProxy(const wptr<IRemoteObject> & remote)103 void DumpManagerCpuClient::ResetProxy(const wptr<IRemoteObject>& remote)
104 {
105     std::lock_guard<std::mutex> lock(mutex_);
106     if (proxy_ == nullptr) {
107         return;
108     }
109     auto serviceRemote = proxy_->AsObject();
110     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
111         serviceRemote->RemoveDeathRecipient(deathRecipient_);
112         proxy_ = nullptr;
113         DUMPER_HILOGD(MODULE_CPU_CLIENT, "debug|disconnected");
114     }
115 }
116 
OnRemoteDied(const wptr<IRemoteObject> & remote)117 void DumpManagerCpuClient::DumpManagerCpuDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
118 {
119     if (remote == nullptr) {
120         return;
121     }
122     DumpManagerCpuClient::GetInstance().ResetProxy(remote);
123 }
124 } // namespace HiviewDFX
125 } // namespace OHOS
126