1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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
17 #include "native_memory_profiler_sa_proxy.h"
18 #include "parameters.h"
19 #include <sys/time.h>
20 #include "xcollie/xcollie.h"
21 #include "xcollie/xcollie_define.h"
22 #include "logging.h"
23
24 namespace OHOS::Developtools::NativeDaemon {
25 constexpr int32_t TIME_OUT = 15;
26 constexpr uint64_t S_TO_NS = 1000 * 1000 * 1000;
NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> & impl)27 NativeMemoryProfilerSaProxy::NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> &impl)
28 : IRemoteProxy<INativeMemoryProfilerSa>(impl) {}
29
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config)30 int32_t NativeMemoryProfilerSaProxy::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
31 {
32 MessageParcel data;
33 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
34 PROFILER_LOG_ERROR(LOG_CORE, "Start failed to write descriptor");
35 return RET_ERR;
36 }
37 CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
38
39 MessageParcel reply;
40 MessageOption option;
41 sptr<IRemoteObject> remote = Remote();
42 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
43 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::START),
44 data, reply, option);
45 if (ret != RET_OK) {
46 PROFILER_LOG_ERROR(LOG_CORE, "Start failed");
47 return ret;
48 }
49 return RET_OK;
50 }
51
DumpData(uint32_t fd,std::shared_ptr<NativeMemoryProfilerSaConfig> & config)52 int32_t NativeMemoryProfilerSaProxy::DumpData(uint32_t fd, std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
53 {
54 MessageParcel data;
55 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
56 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write descriptor");
57 return RET_ERR;
58 }
59
60 CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
61 if (!data.WriteFileDescriptor(fd)) {
62 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write fd");
63 return RET_ERR;
64 }
65
66 MessageParcel reply;
67 MessageOption option;
68 sptr<IRemoteObject> remote = Remote();
69 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
70 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_DATA),
71 data, reply, option);
72 if (ret != RET_OK) {
73 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed");
74 return ret;
75 }
76 return RET_OK;
77 }
78
Stop(uint32_t pid)79 int32_t NativeMemoryProfilerSaProxy::Stop(uint32_t pid)
80 {
81 struct timespec start = {};
82 clock_gettime(CLOCK_REALTIME, &start);
83 XCollieCallback callbackFunc = [](void *) {
84 OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0");
85 };
86 int setTimerRet = HiviewDFX::XCollie::GetInstance().SetTimer("Hiprofiler_Timeout", TIME_OUT, callbackFunc, nullptr,
87 HiviewDFX::XCOLLIE_FLAG_LOG);
88 if (setTimerRet == HiviewDFX::INVALID_ID) {
89 PROFILER_LOG_ERROR(LOG_CORE, "add hicollie failed for native daemon sa Stop");
90 return RET_ERR;
91 }
92 MessageParcel data;
93 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
94 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
95 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
96 return RET_ERR;
97 }
98 WRITEUINT32(data, pid, RET_ERR);
99 MessageParcel reply;
100 MessageOption option;
101 sptr<IRemoteObject> remote = Remote();
102 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
103 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_PID),
104 data, reply, option);
105 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
106 struct timespec end = {};
107 clock_gettime(CLOCK_REALTIME, &end);
108 uint64_t timeCost = (end.tv_sec - start.tv_sec) * S_TO_NS + (end.tv_nsec - start.tv_nsec);
109 PROFILER_LOG_INFO(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop cost time: %llu",
110 static_cast<unsigned long long>(timeCost));
111 if (ret != RET_OK) {
112 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
113 return ret;
114 }
115 return RET_OK;
116 }
117
Stop(const std::string & name)118 int32_t NativeMemoryProfilerSaProxy::Stop(const std::string& name)
119 {
120 MessageParcel data;
121 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
122 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
123 return RET_ERR;
124 }
125 WRITESTRING(data, name, RET_ERR);
126 MessageParcel reply;
127 MessageOption option;
128 sptr<IRemoteObject> remote = Remote();
129 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
130 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_NAME),
131 data, reply, option);
132 if (ret != RET_OK) {
133 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
134 return ret;
135 }
136 return RET_OK;
137 }
138 } // namespace OHOS::Developtools::NativeDaemon