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