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