• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     MessageParcel data;
142     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
143         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed to write descriptor");
144         return RET_ERR;
145     }
146     WRITESTRING(data, name, RET_ERR);
147     MessageParcel reply;
148     MessageOption option;
149     sptr<IRemoteObject> remote = Remote();
150     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
151     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::STOP_HOOK_NAME),
152                                       data, reply, option);
153     if (ret != RET_OK) {
154         PROFILER_LOG_ERROR(LOG_CORE, "Stop failed");
155         return ret;
156     }
157     return RET_OK;
158 }
159 
Start(std::shared_ptr<NativeMemoryProfilerSaConfig> & config,std::string & replyStats)160 int32_t NativeMemoryProfilerSaProxy::Start(std::shared_ptr<NativeMemoryProfilerSaConfig>& config,
161                                            std::string& replyStats)
162 {
163     MessageParcel data;
164     if (!data.WriteInterfaceToken(NativeMemoryProfilerSaProxy::GetDescriptor())) {
165         PROFILER_LOG_ERROR(LOG_CORE, "Start failed to write descriptor");
166         return RET_ERR;
167     }
168     CHECK_TRUE(config->Marshalling(data), RET_ERR, "NativeMemoryProfilerSaConfig marshalling failed");
169 
170     MessageParcel reply;
171     MessageOption option;
172     sptr<IRemoteObject> remote = Remote();
173     CHECK_NOTNULL(remote, RET_ERR, "remote is nullptr");
174     int32_t ret = remote->SendRequest(static_cast<uint32_t>(NativeMemoryProfilerSaInterfaceCode::DUMP_SIMP_DATA),
175                                       data, reply, option);
176     if (ret != RET_OK) {
177         PROFILER_LOG_ERROR(LOG_CORE, "Start failed");
178         return ret;
179     }
180     READSTRING(reply, replyStats, RET_ERR);
181     return RET_OK;
182 }
183 } // namespace OHOS::Developtools::NativeDaemon