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