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 #include "common.h"
24 #include "dfx_dump_catcher.h"
25 #include <iostream>
26 #include <sstream>
27
28 namespace OHOS::Developtools::NativeDaemon {
29 constexpr int32_t TIME_OUT = 15;
30 constexpr int32_t MAX_FRAME_NUM = 100;
31 constexpr uint64_t S_TO_NS = 1000 * 1000 * 1000;
NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> & impl)32 NativeMemoryProfilerSaProxy::NativeMemoryProfilerSaProxy(const sptr<IRemoteObject> &impl)
33 : IRemoteProxy<INativeMemoryProfilerSa>(impl) {}
34
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config)35 int32_t NativeMemoryProfilerSaProxy::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
36 {
37 MessageParcel data;
38 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
39 PROFILER_LOG_ERROR(LOG_CORE, "Start failed to write descriptor");
40 return RET_ERR;
41 }
42 CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
43
44 MessageParcel reply;
45 MessageOption option;
46 sptr<IRemoteObject> remote = Remote();
47 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
48 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::START),
49 data, reply, option);
50 if (ret != RET_OK) {
51 PROFILER_LOG_ERROR(LOG_CORE, "Start failed");
52 return ret;
53 }
54 return RET_OK;
55 }
56
DumpData(uint32_t fd,std::shared_ptr<NativeMemoryProfilerSaConfig> & config)57 int32_t NativeMemoryProfilerSaProxy::DumpData(uint32_t fd, std::shared_ptr<NativeMemoryProfilerSaConfig>& config)
58 {
59 MessageParcel data;
60 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
61 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write descriptor");
62 return RET_ERR;
63 }
64
65 CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
66 if (!data.WriteFileDescriptor(fd)) {
67 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed to write fd");
68 return RET_ERR;
69 }
70
71 MessageParcel reply;
72 MessageOption option;
73 sptr<IRemoteObject> remote = Remote();
74 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
75 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_DATA),
76 data, reply, option);
77 if (ret != RET_OK) {
78 PROFILER_LOG_ERROR(LOG_CORE, "DumpData failed");
79 return ret;
80 }
81 return RET_OK;
82 }
83
Stop(uint32_t pid)84 int32_t NativeMemoryProfilerSaProxy::Stop(uint32_t pid)
85 {
86 struct timespec start = {};
87 clock_gettime(CLOCK_REALTIME, &start);
88 XCollieCallback callbackFunc = [](void *) {
89 int pidVal = 0;
90 if (!COMMON::IsProcessExist("native_daemon", pidVal)) {
91 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop native_daemon doesn't exist!");
92 } else {
93 HiviewDFX::DfxDumpCatcher dumplog;
94 std::string msg = "";
95 if (!dumplog.DumpCatch(pidVal, 0, msg, MAX_FRAME_NUM)) {
96 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop DumpCatch failed!");
97 } else {
98 std::istringstream iss(msg);
99 std::string line;
100 while (std::getline(iss, line, '\n')) {
101 PROFILER_LOG_ERROR(LOG_CORE, "%s", line.c_str());
102 }
103 }
104 }
105 OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0");
106 };
107 int setTimerRet = HiviewDFX::XCollie::GetInstance().SetTimer("Hiprofiler_Timeout", TIME_OUT, callbackFunc, nullptr,
108 HiviewDFX::XCOLLIE_FLAG_LOG);
109 if (setTimerRet == HiviewDFX::INVALID_ID) {
110 PROFILER_LOG_ERROR(LOG_CORE, "add hicollie failed for native daemon sa Stop");
111 return RET_ERR;
112 }
113 MessageParcel data;
114 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
115 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
116 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
117 return RET_ERR;
118 }
119 WRITEUINT32(data, pid, RET_ERR);
120 MessageParcel reply;
121 MessageOption option;
122 sptr<IRemoteObject> remote = Remote();
123 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
124 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_PID),
125 data, reply, option);
126 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
127 struct timespec end = {};
128 clock_gettime(CLOCK_REALTIME, &end);
129 uint64_t timeCost = static_cast<uint64_t>((end.tv_sec - start.tv_sec) * S_TO_NS + (end.tv_nsec - start.tv_nsec));
130 PROFILER_LOG_INFO(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop cost time: %llu",
131 static_cast<unsigned long long>(timeCost));
132 if (ret != RET_OK) {
133 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
134 return ret;
135 }
136 return RET_OK;
137 }
138
Stop(const std::string & name)139 int32_t NativeMemoryProfilerSaProxy::Stop(const std::string& name)
140 {
141 struct timespec start = {};
142 clock_gettime(CLOCK_REALTIME, &start);
143 XCollieCallback callbackFunc = [](void *) {
144 int pidVal = 0;
145 if (!COMMON::IsProcessExist("native_daemon", pidVal)) {
146 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaProxy native_daemon doesn't exist!");
147 } else {
148 HiviewDFX::DfxDumpCatcher dumplog;
149 std::string msg = "";
150 if (!dumplog.DumpCatch(pidVal, 0, msg, MAX_FRAME_NUM)) {
151 PROFILER_LOG_ERROR(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop DumpCatch failed!");
152 } else {
153 std::istringstream iss(msg);
154 std::string line;
155 while (std::getline(iss, line, '\n')) {
156 PROFILER_LOG_DEBUG(LOG_CORE, "%s", line.c_str());
157 }
158 }
159 }
160 OHOS::system::SetParameter("hiviewdfx.hiprofiler.memprofiler.start", "0");
161 };
162 int setTimerRet = HiviewDFX::XCollie::GetInstance().SetTimer("Hiprofiler_Timeout", TIME_OUT, callbackFunc, nullptr,
163 HiviewDFX::XCOLLIE_FLAG_LOG);
164 if (setTimerRet == HiviewDFX::INVALID_ID) {
165 PROFILER_LOG_ERROR(LOG_CORE, "add hicollie failed for native daemon sa Stop");
166 return RET_ERR;
167 }
168 MessageParcel data;
169 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
170 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
171 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
172 return RET_ERR;
173 }
174 WRITESTRING(data, name, RET_ERR);
175 MessageParcel reply;
176 MessageOption option;
177 sptr<IRemoteObject> remote = Remote();
178 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
179 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_NAME),
180 data, reply, option);
181 HiviewDFX::XCollie::GetInstance().CancelTimer(setTimerRet);
182 struct timespec end = {};
183 clock_gettime(CLOCK_REALTIME, &end);
184 unsigned long long timeCost = static_cast<unsigned long long>((end.tv_sec - start.tv_sec) * S_TO_NS +
185 (end.tv_nsec - start.tv_nsec));
186 PROFILER_LOG_INFO(LOG_CORE, "NativeMemoryProfilerSaProxy::Stop cost time: %llu", timeCost);
187 if (ret != RET_OK) {
188 PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
189 return ret;
190 }
191 return RET_OK;
192 }
193
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config,std::string & replyStats)194 int32_t NativeMemoryProfilerSaProxy::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
195 std::string& replyStats)
196 {
197 MessageParcel data;
198 if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
199 PROFILER_LOG_ERROR(LOG_CORE, "Start failed to write descriptor");
200 return RET_ERR;
201 }
202 CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
203
204 MessageParcel reply;
205 MessageOption option;
206 sptr<IRemoteObject> remote = Remote();
207 CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
208 int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_SIMP_DATA),
209 data, reply, option);
210 if (ret != RET_OK) {
211 PROFILER_LOG_ERROR(LOG_CORE, "Start failed");
212 return ret;
213 }
214 READSTRING(reply, replyStats, RET_ERR);
215 return RET_OK;
216 }
217 } // namespace OHOS::Developtools::NativeDaemon