• 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/hidebug.h"
17 
18 #include <memory>
19 #include <vector>
20 #include <unistd.h>
21 
22 #include "hidebug/hidebug_type.h"
23 #include "hidebug_native_interface.h"
24 #include "securec.h"
25 
OH_HiDebug_GetAppCpuUsage()26 double OH_HiDebug_GetAppCpuUsage()
27 {
28     return OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetCpuUsage();
29 }
30 
OH_HiDebug_GetSystemCpuUsage()31 double OH_HiDebug_GetSystemCpuUsage()
32 {
33     auto cpuUsageOptional = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetSystemCpuUsage();
34     if (cpuUsageOptional) {
35         return cpuUsageOptional.value();
36     }
37     return 0;
38 }
39 
OH_HiDebug_GetAppThreadCpuUsage()40 HiDebug_ThreadCpuUsagePtr OH_HiDebug_GetAppThreadCpuUsage()
41 {
42     auto threadMap = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetAppThreadCpuUsage();
43     HiDebug_ThreadCpuUsagePtr head = nullptr;
44     HiDebug_ThreadCpuUsagePtr prev = nullptr;
45     for (const auto[threadId, cpuUsage] : threadMap) {
46         HiDebug_ThreadCpuUsagePtr node = (HiDebug_ThreadCpuUsagePtr) malloc(sizeof(HiDebug_ThreadCpuUsage));
47         if (node == nullptr) {
48             continue;
49         }
50         node->threadId = threadId;
51         node->cpuUsage = cpuUsage;
52         node->next = nullptr;
53         if (prev == nullptr) {
54             head = node;
55         } else {
56             prev->next = node;
57         }
58         prev = node;
59     }
60     return head;
61 }
62 
OH_HiDebug_FreeThreadCpuUsage(HiDebug_ThreadCpuUsagePtr * threadCpuUsage)63 void OH_HiDebug_FreeThreadCpuUsage(HiDebug_ThreadCpuUsagePtr *threadCpuUsage)
64 {
65     if (threadCpuUsage == nullptr || *threadCpuUsage == nullptr) {
66         return;
67     }
68     HiDebug_ThreadCpuUsagePtr node = *threadCpuUsage;
69     while (node != nullptr) {
70         HiDebug_ThreadCpuUsagePtr next = node->next;
71         free(node);
72         node = next;
73     }
74     *threadCpuUsage = nullptr;
75 }
76 
OH_HiDebug_GetAppMemoryLimit(HiDebug_MemoryLimit * memoryLimit)77 void OH_HiDebug_GetAppMemoryLimit(HiDebug_MemoryLimit *memoryLimit)
78 {
79     if (!memoryLimit) {
80         return;
81     }
82     auto collectResult = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetAppMemoryLimit();
83     if (!collectResult) {
84         return;
85     }
86     memoryLimit->vssLimit = collectResult->vssLimit;
87     memoryLimit->rssLimit = collectResult->rssLimit;
88 }
89 
OH_HiDebug_GetAppNativeMemInfo(HiDebug_NativeMemInfo * nativeMemInfo)90 void OH_HiDebug_GetAppNativeMemInfo(HiDebug_NativeMemInfo *nativeMemInfo)
91 {
92     if (!nativeMemInfo) {
93         return;
94     }
95     auto nativeMemoryInfo = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetAppNativeMemInfo();
96     if (!nativeMemoryInfo) {
97         return;
98     }
99 
100     nativeMemInfo->pss = static_cast<uint32_t>(nativeMemoryInfo->pss);
101     nativeMemInfo->vss = static_cast<uint32_t>(nativeMemoryInfo->vss);
102     nativeMemInfo->rss = static_cast<uint32_t>(nativeMemoryInfo->rss);
103     nativeMemInfo->sharedDirty = static_cast<uint32_t>(nativeMemoryInfo->sharedDirty);
104     nativeMemInfo->privateDirty = static_cast<uint32_t>(nativeMemoryInfo->privateDirty);
105     nativeMemInfo->sharedClean = static_cast<uint32_t>(nativeMemoryInfo->sharedClean);
106     nativeMemInfo->privateClean = static_cast<uint32_t>(nativeMemoryInfo->privateClean);
107 }
108 
OH_HiDebug_GetSystemMemInfo(HiDebug_SystemMemInfo * systemMemInfo)109 void OH_HiDebug_GetSystemMemInfo(HiDebug_SystemMemInfo *systemMemInfo)
110 {
111     if (!systemMemInfo) {
112         return;
113     }
114     auto sysMemInfo = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetSystemMemInfo();
115     if (!sysMemInfo) {
116         return;
117     }
118 
119     systemMemInfo->totalMem = static_cast<uint32_t>(sysMemInfo->memTotal);
120     systemMemInfo->freeMem = static_cast<uint32_t>(sysMemInfo->memFree);
121     systemMemInfo->availableMem = static_cast<uint32_t>(sysMemInfo->memAvailable);
122 }
123 
OH_HiDebug_StartAppTraceCapture(HiDebug_TraceFlag flag,uint64_t tags,uint32_t limitSize,char * fileName,uint32_t length)124 HiDebug_ErrorCode OH_HiDebug_StartAppTraceCapture(HiDebug_TraceFlag flag,
125     uint64_t tags, uint32_t limitSize, char* fileName, uint32_t length)
126 {
127     if (fileName == nullptr) {
128         return HIDEBUG_INVALID_ARGUMENT;
129     }
130     auto& nativeInterface = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance();
131     std::string file;
132     auto ret = nativeInterface.StartAppTraceCapture(tags, flag, limitSize, file);
133     if (ret != HIDEBUG_SUCCESS) {
134         return ret;
135     }
136     if (strcpy_s(fileName, length, file.c_str()) != EOK) {
137         nativeInterface.StopAppTraceCapture();
138         return HIDEBUG_INVALID_ARGUMENT;
139     }
140     return HIDEBUG_SUCCESS;
141 }
142 
143 
OH_HiDebug_StopAppTraceCapture()144 HiDebug_ErrorCode OH_HiDebug_StopAppTraceCapture()
145 {
146     return OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().StopAppTraceCapture();
147 }
148 
OH_HiDebug_GetGraphicsMemory(uint32_t * value)149 HiDebug_ErrorCode OH_HiDebug_GetGraphicsMemory(uint32_t *value)
150 {
151     if (value == nullptr) {
152         return HIDEBUG_INVALID_ARGUMENT;
153     }
154     std::optional<int32_t> ret = OHOS::HiviewDFX::HidebugNativeInterface::GetInstance().GetGraphicsMemory();
155     if (!ret || ret < 0) {
156         return HIDEBUG_TRACE_ABNORMAL;
157     }
158     *value = static_cast<uint32_t>(ret.value());
159     return HIDEBUG_SUCCESS;
160 }