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