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_broker_cpu_proxy.h"
16 #include <message_parcel.h>
17 #include "dump_errors.h"
18 #include "hidumper_cpu_service_ipc_interface_code.h"
19 #include "hilog_wrapper.h"
20 namespace OHOS {
21 namespace HiviewDFX {
Request(DumpCpuData & dumpCpuData)22 int32_t DumpBrokerCpuProxy::Request(DumpCpuData &dumpCpuData)
23 {
24     int32_t ret = -1;
25     sptr<IRemoteObject> remote = Remote();
26     if (remote == nullptr) {
27         return ret;
28     }
29     MessageParcel data;
30     MessageParcel reply;
31     MessageOption option;
32     if (!data.WriteInterfaceToken(DumpBrokerCpuProxy::GetDescriptor())) {
33         return ret;
34     }
35     if (!data.WriteParcelable(&dumpCpuData)) {
36         return ERROR_WRITE_PARCEL;
37     }
38     int res = remote->SendRequest(static_cast<int>(HidumperCpuServiceInterfaceCode::DUMP_REQUEST_CPUINFO),
39         data, reply, option);
40     if (res != ERR_OK) {
41         DUMPER_HILOGE(MODULE_CPU_ZIDL, "error|SendCpuRequest error code: %{public}d", res);
42         return ret;
43     }
44     std::shared_ptr<DumpCpuData> dumpCpuDataPtr(reply.ReadParcelable<DumpCpuData>());
45     if (dumpCpuDataPtr == nullptr) {
46         DUMPER_HILOGE(MODULE_CPU_ZIDL, "error|ReadParcelable error");
47         return ret;
48     }
49     if (!reply.ReadInt32(ret)) {
50         return ERROR_READ_PARCEL;
51     }
52     dumpCpuData = *dumpCpuDataPtr;
53     return ret;
54 }
55 
GetCpuUsageByPid(int32_t pid,double & cpuUsage)56 int32_t DumpBrokerCpuProxy::GetCpuUsageByPid(int32_t pid, double &cpuUsage)
57 {
58     int32_t ret = -1;
59     sptr<IRemoteObject> remoteObject = Remote();
60     if (remoteObject == nullptr) {
61         return ret;
62     }
63     MessageParcel data;
64     MessageParcel reply;
65     MessageOption option;
66     if (!data.WriteInterfaceToken(DumpBrokerCpuProxy::GetDescriptor())) {
67         return ret;
68     }
69     if (!data.WriteInt32(pid)) {
70         return ERROR_WRITE_PARCEL;
71     }
72     int res = remoteObject->SendRequest(static_cast<int>(HidumperCpuServiceInterfaceCode::DUMP_USAGE_ONLY),
73         data, reply, option);
74     if (res != ERR_OK) {
75         DUMPER_HILOGE(MODULE_CPU_SERVICE, "error|SendCpuRequest error code: %{public}d", res);
76         return ret;
77     }
78     if (!reply.ReadDouble(cpuUsage)) {
79         return ERROR_READ_PARCEL;
80     }
81     if (cpuUsage < 0) {
82         return ret;
83     }
84     return ERR_OK;
85 }
86 } // namespace HiviewDFX
87 } // namespace OHOS
88