1 /*
2 * Copyright (c) 2022 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 "napi/native_api.h"
17 #include <hidebug/hidebug.h>
18 #include <hidebug/hidebug_type.h>
19 #include <cstdlib>
20
GetTotalMem(napi_env env,napi_callback_info info)21 static napi_value GetTotalMem(napi_env env, napi_callback_info info)
22 {
23 napi_value totalMem;
24 HiDebug_SystemMemInfo sysMemInfo;
25 OH_HiDebug_GetSystemMemInfo(&sysMemInfo);
26 napi_create_uint32(env, sysMemInfo.totalMem, &totalMem);
27 return totalMem;
28 }
29
GetFreeMem(napi_env env,napi_callback_info info)30 static napi_value GetFreeMem(napi_env env, napi_callback_info info)
31 {
32 napi_value freeMem;
33 HiDebug_SystemMemInfo sysMemInfo;
34 OH_HiDebug_GetSystemMemInfo(&sysMemInfo);
35 napi_create_uint32(env, sysMemInfo.freeMem, &freeMem);
36 return freeMem;
37 }
38
GetAvailableMem(napi_env env,napi_callback_info info)39 static napi_value GetAvailableMem(napi_env env, napi_callback_info info)
40 {
41 napi_value availableMem;
42 HiDebug_SystemMemInfo sysMemInfo;
43 OH_HiDebug_GetSystemMemInfo(&sysMemInfo);
44 napi_create_uint32(env, sysMemInfo.availableMem, &availableMem);
45 return availableMem;
46 }
47
GetPss(napi_env env,napi_callback_info info)48 static napi_value GetPss(napi_env env, napi_callback_info info)
49 {
50 napi_value pss;
51 HiDebug_NativeMemInfo nativeMemInfo;
52 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
53 napi_create_uint32(env, nativeMemInfo.pss, &pss);
54 return pss;
55 }
56
GetVss(napi_env env,napi_callback_info info)57 static napi_value GetVss(napi_env env, napi_callback_info info)
58 {
59 napi_value vss;
60 HiDebug_NativeMemInfo nativeMemInfo;
61 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
62 napi_create_uint32(env, nativeMemInfo.vss, &vss);
63 return vss;
64 }
65
GetRss(napi_env env,napi_callback_info info)66 static napi_value GetRss(napi_env env, napi_callback_info info)
67 {
68 napi_value rss;
69 HiDebug_NativeMemInfo nativeMemInfo;
70 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
71 napi_create_uint32(env, nativeMemInfo.rss, &rss);
72 return rss;
73 }
74
GetSharedDirty(napi_env env,napi_callback_info info)75 static napi_value GetSharedDirty(napi_env env, napi_callback_info info)
76 {
77 napi_value sharedDirty;
78 HiDebug_NativeMemInfo nativeMemInfo;
79 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
80 napi_create_uint32(env, nativeMemInfo.sharedDirty, &sharedDirty);
81 return sharedDirty;
82 }
83
GetPrivateDirty(napi_env env,napi_callback_info info)84 static napi_value GetPrivateDirty(napi_env env, napi_callback_info info)
85 {
86 napi_value privateDirty;
87 HiDebug_NativeMemInfo nativeMemInfo;
88 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
89 napi_create_uint32(env, nativeMemInfo.privateDirty, &privateDirty);
90 return privateDirty;
91 }
92
GetSharedClean(napi_env env,napi_callback_info info)93 static napi_value GetSharedClean(napi_env env, napi_callback_info info)
94 {
95 napi_value sharedClean;
96 HiDebug_NativeMemInfo nativeMemInfo;
97 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
98 napi_create_uint32(env, nativeMemInfo.sharedClean, &sharedClean);
99 return sharedClean;
100 }
101
GetPrivateClean(napi_env env,napi_callback_info info)102 static napi_value GetPrivateClean(napi_env env, napi_callback_info info)
103 {
104 napi_value privateClean;
105 HiDebug_NativeMemInfo nativeMemInfo;
106 OH_HiDebug_GetAppNativeMemInfo(&nativeMemInfo);
107 napi_create_uint32(env, nativeMemInfo.privateClean, &privateClean);
108 return privateClean;
109 }
110
GetRssLimit(napi_env env,napi_callback_info info)111 static napi_value GetRssLimit(napi_env env, napi_callback_info info)
112 {
113 napi_value rssLimit;
114 HiDebug_MemoryLimit memoryLimit;
115 OH_HiDebug_GetAppMemoryLimit(&memoryLimit);
116 napi_create_bigint_uint64(env, memoryLimit.rssLimit, &rssLimit);
117 return rssLimit;
118 }
119
GetVssLimit(napi_env env,napi_callback_info info)120 static napi_value GetVssLimit(napi_env env, napi_callback_info info)
121 {
122 napi_value vssLimit;
123 HiDebug_MemoryLimit memoryLimit;
124 OH_HiDebug_GetAppMemoryLimit(&memoryLimit);
125 napi_create_bigint_uint64(env, memoryLimit.vssLimit, &vssLimit);
126 return vssLimit;
127 }
128
GetSysCpuUsage(napi_env env,napi_callback_info info)129 static napi_value GetSysCpuUsage(napi_env env, napi_callback_info info)
130 {
131 napi_value sysCpuUsage;
132 double cpuUsage = OH_HiDebug_GetSystemCpuUsage();
133 napi_create_double(env, cpuUsage, &sysCpuUsage);
134 return sysCpuUsage;
135 }
136
GetAppThreadCpuUsage(napi_env env,napi_callback_info info)137 static napi_value GetAppThreadCpuUsage(napi_env env, napi_callback_info info)
138 {
139 napi_value res;
140 napi_create_array(env, &res);
141 size_t idx = 0;
142 HiDebug_ThreadCpuUsagePtr threadCpuUsage = OH_HiDebug_GetAppThreadCpuUsage();
143 HiDebug_ThreadCpuUsagePtr curThreadCpuUsage = threadCpuUsage;
144 while (curThreadCpuUsage != nullptr) {
145 napi_value obj = nullptr;
146 napi_create_array(env, &obj);
147 auto threadIdValue = curThreadCpuUsage->threadId;
148 auto cpuUsageValue = curThreadCpuUsage->cpuUsage;
149
150 napi_value threadId;
151 napi_create_uint32(env, threadIdValue, &threadId);
152 napi_set_named_property(env, obj, "threadId", threadId);
153
154 napi_value cpuUsage;
155 napi_create_double(env, cpuUsageValue, &cpuUsage);
156 napi_set_named_property(env, obj, "cpuUsage", cpuUsage);
157
158 napi_set_element(env, res, idx, obj);
159 idx++;
160 curThreadCpuUsage = curThreadCpuUsage->next;
161 }
162 OH_HiDebug_FreeThreadCpuUsage(&threadCpuUsage);
163 return res;
164 }
165
GetAppCpuUsage(napi_env env,napi_callback_info info)166 static napi_value GetAppCpuUsage(napi_env env, napi_callback_info info)
167 {
168 napi_value appCpuUsage;
169 double cpuUsage = OH_HiDebug_GetAppCpuUsage();
170 napi_create_double(env, cpuUsage, &appCpuUsage);
171 return appCpuUsage;
172 }
173
StartAppTraceCapture(napi_env env,napi_callback_info info)174 static napi_value StartAppTraceCapture(napi_env env, napi_callback_info info)
175 {
176 napi_value ret;
177 size_t argc = 3; // arg total:3
178 napi_value args[3] = {nullptr}; // arg total:3
179
180 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
181
182 napi_valuetype valuetype0;
183 napi_typeof(env, args[0], &valuetype0);
184 napi_valuetype valuetype1;
185 napi_typeof(env, args[1], &valuetype1);
186 napi_valuetype valuetype2;
187 napi_typeof(env, args[2], &valuetype2); // arg number:2
188
189 uint32_t flag;
190 napi_get_value_uint32(env, args[0], &flag);
191 uint64_t tags = HIDEBUG_TRACE_TAG_ARK;
192 uint32_t limitSize;
193 napi_get_value_uint32(env, args[1], &limitSize);
194 uint32_t length;
195 napi_get_value_uint32(env, args[2], &length); // arg number:2
196 char fileName[length];
197
198 HiDebug_ErrorCode errorCode = OH_HiDebug_StartAppTraceCapture(HiDebug_TraceFlag(flag),
199 tags, limitSize, fileName, length);
200 napi_create_int32(env, errorCode, &ret);
201 return ret;
202 }
203
GetAppTraceCaptureFile(napi_env env,napi_callback_info info)204 static napi_value GetAppTraceCaptureFile(napi_env env, napi_callback_info info)
205 {
206 napi_value ret;
207 size_t argc = 3; // arg total:3
208 napi_value args[3] = {nullptr}; // arg total:3
209
210 napi_get_cb_info(env, info, &argc, args, nullptr, nullptr);
211
212 napi_valuetype valuetype0;
213 napi_typeof(env, args[0], &valuetype0);
214 napi_valuetype valuetype1;
215 napi_typeof(env, args[1], &valuetype1);
216 napi_valuetype valuetype2;
217 napi_typeof(env, args[2], &valuetype2); // arg number:2
218
219 uint32_t flag;
220 napi_get_value_uint32(env, args[0], &flag);
221 uint64_t tags = HIDEBUG_TRACE_TAG_ARK;
222 uint32_t limitSize;
223 napi_get_value_uint32(env, args[1], &limitSize);
224 uint32_t length;
225 napi_get_value_uint32(env, args[2], &length); // arg number:2
226 char fileName[length];
227
228 OH_HiDebug_StartAppTraceCapture(HiDebug_TraceFlag(flag), tags, limitSize, fileName, length);
229 napi_create_string_utf8(env, fileName, length, &ret);
230 return ret;
231 }
232
StartAppTraceCaptureTag(napi_env env,napi_callback_info info)233 static napi_value StartAppTraceCaptureTag(napi_env env, napi_callback_info info)
234 {
235 napi_value ret;
236
237 HiDebug_TraceFlag flag = HIDEBUG_TRACE_FLAG_MAIN_THREAD;
238 uint64_t tags = 0;
239 uint32_t limitSize = 1024 * 1024;
240 uint32_t length = 256;
241
242 char fileName[length];
243
244 HiDebug_ErrorCode errorCode = OH_HiDebug_StartAppTraceCapture(flag, tags, limitSize, fileName, length);
245 napi_create_int32(env, errorCode, &ret);
246 return ret;
247 }
248
StopAppTraceCapture(napi_env env,napi_callback_info info)249 static napi_value StopAppTraceCapture(napi_env env, napi_callback_info info)
250 {
251 napi_value ret;
252 HiDebug_ErrorCode errorCode = OH_HiDebug_StopAppTraceCapture();
253 napi_create_int32(env, errorCode, &ret);
254 return ret;
255 }
256
getGraphicsMemory(napi_env env,napi_callback_info info)257 static napi_value getGraphicsMemory(napi_env env, napi_callback_info info)
258 {
259 uint32_t value = 0;
260 napi_value sum;
261 HiDebug_ErrorCode errCode = OH_HiDebug_GetGraphicsMemory(&value);
262 napi_create_double(env, errCode, &sum);
263 return sum;
264 }
265
getGraphicsMemoryArray(napi_env env,napi_callback_info info)266 static napi_value getGraphicsMemoryArray(napi_env env, napi_callback_info info)
267 {
268 uint32_t arr[5] = {1, 2, 3, 4, 5};
269 uint32_t *value = arr;
270 napi_value sum;
271 HiDebug_ErrorCode errCode = OH_HiDebug_GetGraphicsMemory(value);
272 napi_create_double(env, errCode, &sum);
273 return sum;
274 }
275
getGraphicsMemoryNULL(napi_env env,napi_callback_info info)276 static napi_value getGraphicsMemoryNULL(napi_env env, napi_callback_info info)
277 {
278 napi_value sum;
279 HiDebug_ErrorCode errCode = OH_HiDebug_GetGraphicsMemory(NULL);
280 napi_create_double(env, errCode, &sum);
281 return sum;
282 }
283
284 EXTERN_C_START
Init(napi_env env,napi_value exports)285 static napi_value Init(napi_env env, napi_value exports)
286 {
287 napi_property_descriptor desc[] = {
288 { "getTotalMem", nullptr, GetTotalMem, nullptr, nullptr, nullptr, napi_default, nullptr },
289 { "getFreeMem", nullptr, GetFreeMem, nullptr, nullptr, nullptr, napi_default, nullptr },
290 { "getAvailableMem", nullptr, GetAvailableMem, nullptr, nullptr, nullptr, napi_default, nullptr },
291 { "getPss", nullptr, GetPss, nullptr, nullptr, nullptr, napi_default, nullptr },
292 { "getVss", nullptr, GetVss, nullptr, nullptr, nullptr, napi_default, nullptr },
293 { "getRss", nullptr, GetRss, nullptr, nullptr, nullptr, napi_default, nullptr },
294 { "getSharedDirty", nullptr, GetSharedDirty, nullptr, nullptr, nullptr, napi_default, nullptr },
295 { "getPrivateDirty", nullptr, GetPrivateDirty, nullptr, nullptr, nullptr, napi_default, nullptr },
296 { "getSharedClean", nullptr, GetSharedClean, nullptr, nullptr, nullptr, napi_default, nullptr },
297 { "getPrivateClean", nullptr, GetPrivateClean, nullptr, nullptr, nullptr, napi_default, nullptr },
298 { "getRssLimit", nullptr, GetRssLimit, nullptr, nullptr, nullptr, napi_default, nullptr },
299 { "getVssLimit", nullptr, GetVssLimit, nullptr, nullptr, nullptr, napi_default, nullptr },
300 { "getSysCpuUsage", nullptr, GetSysCpuUsage, nullptr, nullptr, nullptr, napi_default, nullptr },
301 { "getAppThreadCpuUsage", nullptr, GetAppThreadCpuUsage, nullptr, nullptr, nullptr, napi_default, nullptr },
302 { "getAppCpuUsage", nullptr, GetAppCpuUsage, nullptr, nullptr, nullptr, napi_default, nullptr },
303 { "startAppTraceCapture", nullptr, StartAppTraceCapture, nullptr, nullptr, nullptr, napi_default, nullptr },
304 { "getAppTraceCaptureFile", nullptr, GetAppTraceCaptureFile, nullptr, nullptr, nullptr, napi_default, nullptr },
305 { "startAppTraceCaptureTag", nullptr,
306 StartAppTraceCaptureTag, nullptr, nullptr, nullptr, napi_default, nullptr },
307 { "stopAppTraceCapture", nullptr, StopAppTraceCapture, nullptr, nullptr, nullptr, napi_default, nullptr },
308 {"getGraphicsMemory", nullptr, getGraphicsMemory, nullptr, nullptr, nullptr, napi_default, nullptr},
309 { "getGraphicsMemoryNULL", nullptr, getGraphicsMemoryNULL, nullptr, nullptr, nullptr, napi_default, nullptr },
310 { "getGraphicsMemoryArray", nullptr, getGraphicsMemoryArray, nullptr, nullptr, nullptr, napi_default, nullptr },
311 };
312 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
313 return exports;
314 }
315 EXTERN_C_END
316
317 static napi_module demoModule = {
318 .nm_version = 1,
319 .nm_flags = 0,
320 .nm_filename = nullptr,
321 .nm_register_func = Init,
322 .nm_modname = "hidebug",
323 .nm_priv = ((void*)0),
324 .reserved = { 0 },
325 };
326
RegisterEntryModule(void)327 extern "C" __attribute__((constructor)) void RegisterEntryModule(void)
328 {
329 napi_module_register(&demoModule);
330 }
331