1 /*
2 * Copyright (c) 2024 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
16 #include "hidebug_native_interface.h"
17
18 #include <chrono>
19 #include <fstream>
20 #include <memory>
21 #include <vector>
22 #include <unistd.h>
23
24 #include "dump_usage.h"
25 #include "hidebug_app_thread_cpu.h"
26 #include "hilog/log.h"
27 #include "client/cpu_collector.h"
28 #include "client/memory_collector.h"
29 #include "utility/memory_collector.h"
30
31 namespace OHOS {
32 namespace HiviewDFX {
33
34 #undef LOG_DOMAIN
35 #define LOG_DOMAIN 0xD002D0A
36 #undef LOG_TAG
37 #define LOG_TAG "HiDebug_Native_Interface"
38
39 class HidebugNativeInterfaceImpl : public HidebugNativeInterface {
40 public:
41 HidebugNativeInterfaceImpl() = default;
42 HidebugNativeInterfaceImpl(const HidebugNativeInterfaceImpl&) = delete;
43 HidebugNativeInterfaceImpl& operator =(const HidebugNativeInterfaceImpl&) = delete;
44 double GetCpuUsage() override;
45 std::map<uint32_t, double> GetAppThreadCpuUsage() override;
46 HiDebug_ErrorCode StartAppTraceCapture(uint64_t tags, uint32_t flag,
47 uint32_t limitsize, std::string &file) override;
48 HiDebug_ErrorCode StopAppTraceCapture() override;
49 int GetMemoryLeakResource(const std::string& type, int32_t value, bool enabledDebugLog) override;
50 std::optional<double> GetSystemCpuUsage() override;
51 std::optional<MemoryLimit> GetAppMemoryLimit() override;
52 std::optional<HiDebug_NativeMemInfo> GetAppNativeMemInfo() override;
53 std::optional<SysMemory> GetSystemMemInfo() override;
54 bool IsDebuggerConnected() override;
55 private:
56 /**
57 * GetElapsedNanoSecondsSinceBoot
58 *
59 * @return NanoSecondsSinceBoot
60 */
61 int64_t GetElapsedNanoSecondsSinceBoot();
62 constexpr static int SECOND_TO_NANOSECOND = 1 * 1000 * 1000 * 1000;
63 constexpr static int CPU_USAGE_VALIDITY = 2 * SECOND_TO_NANOSECOND; // 2s
64 static inline int64_t lastCpuUsageGetTime_ = 0;
65 static inline double lastCpuUsage_ = 0;
66 static inline HidebugAppThreadCpu threadCpu_;
67 };
68
CreateInstance()69 std::unique_ptr<HidebugNativeInterface> HidebugNativeInterface::CreateInstance()
70 {
71 return std::make_unique<HidebugNativeInterfaceImpl>();
72 }
73
GetCpuUsage()74 double HidebugNativeInterfaceImpl::GetCpuUsage()
75 {
76 std::unique_ptr<DumpUsage> dumpUsage = std::make_unique<DumpUsage>();
77 pid_t pid = getprocpid();
78 float tmpCpuUsage = dumpUsage->GetCpuUsage(pid);
79 double cpuUsage = static_cast<double>(tmpCpuUsage);
80 return cpuUsage;
81 }
82
GetAppThreadCpuUsage()83 std::map<uint32_t, double> HidebugNativeInterfaceImpl::GetAppThreadCpuUsage()
84 {
85 auto collectResult = threadCpu_.CollectThreadStatInfos();
86 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
87 HILOG_ERROR(LOG_CORE, "GetAppThreadCpuUsage fail, ret: %{public}d", static_cast<int>(collectResult.retCode));
88 return {};
89 }
90 std::map<uint32_t, double> threadMap;
91 for (const auto &threadCpuStatInfo : collectResult.data) {
92 threadMap[threadCpuStatInfo.tid] = threadCpuStatInfo.cpuUsage;
93 }
94 return threadMap;
95 }
96
StartAppTraceCapture(uint64_t tags,uint32_t flag,uint32_t limitsize,std::string & file)97 HiDebug_ErrorCode HidebugNativeInterfaceImpl::StartAppTraceCapture(uint64_t tags, uint32_t flag,
98 uint32_t limitsize, std::string &file)
99 {
100 auto ret = StartCaptureAppTrace((TraceFlag)flag, tags, limitsize, file);
101 if (ret == RET_SUCC) {
102 return HIDEBUG_SUCCESS;
103 }
104 if (ret == RET_FAIL_INVALID_ARGS) {
105 return HIDEBUG_INVALID_ARGUMENT;
106 }
107 if (ret == RET_STARTED) {
108 return HIDEBUG_TRACE_CAPTURED_ALREADY;
109 }
110 if (ret == RET_FAIL_MKDIR || ret == RET_FAIL_SETACL || ret == RET_FAIL_EACCES || ret == RET_FAIL_ENOENT) {
111 return HIDEBUG_NO_PERMISSION;
112 }
113 return HIDEBUG_TRACE_ABNORMAL;
114 }
115
StopAppTraceCapture()116 HiDebug_ErrorCode HidebugNativeInterfaceImpl::StopAppTraceCapture()
117 {
118 auto ret = StopCaptureAppTrace();
119 if (ret == RET_SUCC) {
120 return HIDEBUG_SUCCESS;
121 }
122 if (ret == RET_STOPPED) {
123 return HIDEBUG_NO_TRACE_RUNNING;
124 }
125 return HIDEBUG_TRACE_ABNORMAL;
126 }
127
GetSystemCpuUsage()128 std::optional<double> HidebugNativeInterfaceImpl::GetSystemCpuUsage()
129 {
130 HILOG_INFO(LOG_CORE, "GetSystemCpuUsage");
131 int64_t now = GetElapsedNanoSecondsSinceBoot();
132 if (lastCpuUsageGetTime_ > 0 && now <= lastCpuUsageGetTime_ + CPU_USAGE_VALIDITY) {
133 HILOG_WARN(LOG_CORE, "GetSystemCpuUsage too frequently, return the last result");
134 return lastCpuUsage_;
135 }
136 std::shared_ptr<UCollectClient::CpuCollector> collector = UCollectClient::CpuCollector::Create();
137 if (!collector) {
138 HILOG_ERROR(LOG_CORE, "GetSystemCpuUsage Failed, return the last result");
139 return std::nullopt;
140 }
141 auto collectResult = collector->GetSysCpuUsage();
142 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
143 HILOG_ERROR(LOG_CORE, "GetSystemCpuUsage Failed, retCode: %{public}d, return the last result",
144 static_cast<int>(collectResult.retCode));
145 return std::nullopt;
146 }
147 lastCpuUsage_ = collectResult.data;
148 lastCpuUsageGetTime_ = GetElapsedNanoSecondsSinceBoot();
149 return lastCpuUsage_;
150 }
151
GetAppMemoryLimit()152 std::optional<MemoryLimit> HidebugNativeInterfaceImpl::GetAppMemoryLimit()
153 {
154 auto collector = UCollectUtil::MemoryCollector::Create();
155 if (!collector) {
156 HILOG_ERROR(LOG_CORE, "GetAppMemoryLimit Failed");
157 return {};
158 }
159 auto collectResult = collector->CollectMemoryLimit();
160 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
161 HILOG_ERROR(LOG_CORE, "GetAppMemoryLimit Failed, retCode: %{public}d", static_cast<int>(collectResult.retCode));
162 return {};
163 }
164
165 MemoryLimit memoryLimit;
166 memoryLimit.vssLimit = collectResult.data.vssLimit;
167 memoryLimit.rssLimit = collectResult.data.rssLimit;
168
169 return memoryLimit;
170 }
171
GetAppNativeMemInfo()172 std::optional<HiDebug_NativeMemInfo> HidebugNativeInterfaceImpl::GetAppNativeMemInfo()
173 {
174 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
175 if (!collector) {
176 HILOG_ERROR(LOG_CORE, "GetAppNativeMemInfo Failed");
177 return {};
178 }
179 int pid = getprocpid();
180 auto collectResult = collector->CollectProcessMemory(pid);
181 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
182 HILOG_ERROR(LOG_CORE, "CollectProcessMemory Failed,retCode = %{public}d",
183 static_cast<int>(collectResult.retCode));
184 return {};
185 }
186
187 HiDebug_NativeMemInfo nativeMemInfo;
188 int32_t pssInfo = collectResult.data.pss + collectResult.data.swapPss;
189 nativeMemInfo.pss = static_cast<uint32_t>(pssInfo);
190 nativeMemInfo.rss = collectResult.data.rss;
191 nativeMemInfo.sharedDirty = collectResult.data.sharedDirty;
192 nativeMemInfo.privateDirty = collectResult.data.privateDirty;
193 nativeMemInfo.sharedClean = static_cast<uint32_t>(collectResult.data.sharedClean);
194 nativeMemInfo.privateClean = collectResult.data.privateClean;
195
196 auto collectVss = collector->CollectProcessVss(pid);
197 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
198 HILOG_ERROR(LOG_CORE, "CollectProcessVss Failed,retCode = %{public}d", static_cast<int>(collectResult.retCode));
199 return {};
200 }
201 nativeMemInfo.vss = static_cast<uint32_t>(collectVss.data);
202 return nativeMemInfo;
203 }
204
GetSystemMemInfo()205 std::optional<SysMemory> HidebugNativeInterfaceImpl::GetSystemMemInfo()
206 {
207 std::shared_ptr<UCollectUtil::MemoryCollector> collector = UCollectUtil::MemoryCollector::Create();
208 if (!collector) {
209 HILOG_ERROR(LOG_CORE, "GetSystemMemInfo Failed");
210 return {};
211 }
212 auto collectResult = collector->CollectSysMemory();
213 if (collectResult.retCode != UCollect::UcError::SUCCESS) {
214 HILOG_ERROR(LOG_CORE, "GetSystemMemInfo Failed,retCode = %{public}d",
215 static_cast<int>(collectResult.retCode));
216 return {};
217 }
218
219 SysMemory sysMemInfo;
220 sysMemInfo.memTotal = collectResult.data.memTotal;
221 sysMemInfo.memFree = collectResult.data.memFree;
222 sysMemInfo.memAvailable = collectResult.data.memAvailable;
223 return sysMemInfo;
224 }
225
GetMemoryLeakResource(const std::string & type,int32_t value,bool enabledDebugLog)226 int HidebugNativeInterfaceImpl::GetMemoryLeakResource(const std::string& type,
227 int32_t value, bool enabledDebugLog)
228 {
229 HILOG_DEBUG(LOG_CORE, "GetMemoryLeakResource");
230 auto memoryCollect = UCollectClient::MemoryCollector::Create();
231 if (!memoryCollect) {
232 HILOG_ERROR(LOG_CORE, "GetMemoryLeakResource Failed, return result");
233 return MemoryState::MEMORY_FAILED;
234 }
235 UCollectClient::MemoryCaller memoryCaller;
236 memoryCaller.pid = getprocpid();
237 memoryCaller.resourceType = type;
238 memoryCaller.limitValue = value;
239 memoryCaller.enabledDebugLog = enabledDebugLog;
240 auto result = memoryCollect->SetAppResourceLimit(memoryCaller);
241 if (result.retCode != UCollect::UcError::SUCCESS) {
242 HILOG_ERROR(LOG_CORE, "GetMemoryLeakResource Failed, retCode: %{public}d, return the last result",
243 static_cast<int>(result.retCode));
244 return MemoryState::MEMORY_FAILED;
245 }
246 HILOG_DEBUG(LOG_CORE, "GetMemoryLeakResource Success, retCode: %{public}d", static_cast<int>(result.retCode));
247 return MemoryState::MEMORY_SUCCESS;
248 }
249
IsDebuggerConnected()250 bool HidebugNativeInterfaceImpl::IsDebuggerConnected()
251 {
252 HILOG_DEBUG(LOG_CORE, "IsDebuggerConnected");
253 std::ifstream file("/proc/self/status");
254 if (!file.is_open()) {
255 HILOG_ERROR(LOG_CORE, "IsDebuggerConnected:: open status file failed!");
256 return false;
257 }
258 std::string line;
259 while (std::getline(file, line)) {
260 if (line.find("TracerPid:") != std::string::npos) {
261 std::string pidStr = line.substr(line.find(":") + 1);
262 return std::atoi(pidStr.c_str()) != 0;
263 }
264 }
265 HILOG_ERROR(LOG_CORE, "IsDebuggerConnected:: no find the TracerPid:");
266 return false;
267 }
268
GetElapsedNanoSecondsSinceBoot()269 int64_t HidebugNativeInterfaceImpl::GetElapsedNanoSecondsSinceBoot()
270 {
271 struct timespec times = {0, 0};
272 clock_gettime(CLOCK_MONOTONIC, ×);
273 return times.tv_sec * SECOND_TO_NANOSECOND + times.tv_nsec;
274 }
275 }
276 }
277