• 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 "commonlibrary/ets_utils/tools/log.h"
17 #include "native_engine.h"
18 
19 namespace OHOS::JsSysModule::Dfx {
20     constexpr int NUMBER_OF_PARAMETER_TWO = 2;
DumpHeapSnapshot(napi_env env,napi_callback_info info)21     static napi_value DumpHeapSnapshot(napi_env env, napi_callback_info info)
22     {
23         size_t argc = NUMBER_OF_PARAMETER_TWO;
24         size_t requireArgc = NUMBER_OF_PARAMETER_TWO;
25         napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
26         NAPI_ASSERT(env, argc <= requireArgc, "Wrong number of arguments");
27         napi_value argv[NUMBER_OF_PARAMETER_TWO] = { 0 };
28         napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
29         std::string tempStr = "";
30         size_t tempStrsize = 0;
31         if (napi_get_value_string_utf8(env, argv[0], nullptr, 0, &tempStrsize) != napi_ok) {
32             HILOG_ERROR("can not get argv[0] size");
33             return nullptr;
34         }
35         tempStr.reserve(tempStrsize + 1);
36         tempStr.resize(tempStrsize);
37         if (napi_get_value_string_utf8(env, argv[0], tempStr.data(), tempStrsize + 1, &tempStrsize) != napi_ok) {
38             HILOG_ERROR("can not get argv[0] value");
39             return nullptr;
40         }
41         std::string pathStr = tempStr;
42         bool isVmMode = true;
43         napi_get_value_bool(env, argv[1], &isVmMode);
44         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
45         engine->DumpHeapSnapshot(pathStr, isVmMode);
46         napi_value result = nullptr;
47         NAPI_CALL(env, napi_get_undefined(env, &result));
48         return result;
49     }
50 
BuildNativeAndJsStackTrace(napi_env env,napi_callback_info info)51     static napi_value BuildNativeAndJsStackTrace(napi_env env, napi_callback_info info)
52     {
53         napi_value result = nullptr;
54         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
55         std::string stackTraceResult = "";
56         bool temp = engine->BuildNativeAndJsStackTrace(stackTraceResult);
57         NAPI_CALL(env, napi_create_string_utf8(env, stackTraceResult.c_str(), stackTraceResult.size(), &result));
58         if (temp) {
59             return result;
60         } else {
61             return nullptr;
62         }
63     }
64 
StartHeapTracking(napi_env env,napi_callback_info info)65     static napi_value StartHeapTracking(napi_env env, napi_callback_info info)
66     {
67         size_t argc = NUMBER_OF_PARAMETER_TWO;
68         size_t requireArgc = NUMBER_OF_PARAMETER_TWO;
69         napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
70         NAPI_ASSERT(env, argc <= requireArgc, "Wrong number of arguments");
71         napi_value argv[NUMBER_OF_PARAMETER_TWO] = { 0 };
72         napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
73         double timeInterval = 0;
74         if (napi_get_value_double(env, argv[0], &timeInterval) != napi_ok) {
75             HILOG_ERROR("can not get argv[0] value");
76             return nullptr;
77         }
78         bool isVmMode = true;
79         if (napi_get_value_bool(env, argv[1], &isVmMode) != napi_ok) {
80             HILOG_ERROR("can not get argv[1] value");
81             return nullptr;
82         }
83         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
84         auto startResult = engine->StartHeapTracking(timeInterval, isVmMode);
85         napi_value result = nullptr;
86         NAPI_CALL(env, napi_get_boolean(env, startResult, &result));
87         return result;
88     }
89 
StopHeapTracking(napi_env env,napi_callback_info info)90     static napi_value StopHeapTracking(napi_env env, napi_callback_info info)
91     {
92         size_t argc = 1;
93         size_t requireArgc = 1;
94         napi_get_cb_info(env, info, &argc, nullptr, nullptr, nullptr);
95         NAPI_ASSERT(env, argc <= requireArgc, "Wrong number of arguments");
96         napi_value argv = nullptr;
97         napi_get_cb_info(env, info, &argc, &argv, nullptr, nullptr);
98         std::string tempStr = "";
99         size_t tempStrsize = 0;
100         if (napi_get_value_string_utf8(env, argv, nullptr, 0, &tempStrsize) != napi_ok) {
101             HILOG_ERROR("can not get argv size");
102             return nullptr;
103         }
104         tempStr.reserve(tempStrsize);
105         tempStr.resize(tempStrsize);
106         if (napi_get_value_string_utf8(env, argv, tempStr.data(), tempStrsize + 1, &tempStrsize) != napi_ok) {
107             HILOG_ERROR("can not get argv value");
108             return nullptr;
109         }
110         std::string filePath = tempStr;
111         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
112         auto stopResult = engine->StopHeapTracking(filePath);
113         napi_value result = nullptr;
114         NAPI_CALL(env, napi_get_boolean(env, stopResult, &result));
115         return result;
116     }
117 
PrintStatisticResult(napi_env env,napi_callback_info info)118     static napi_value PrintStatisticResult(napi_env env, napi_callback_info info)
119     {
120         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
121         engine->PrintStatisticResult();
122         napi_value result = nullptr;
123         NAPI_CALL(env, napi_get_undefined(env, &result));
124         return result;
125     }
126 
StartRuntimeStat(napi_env env,napi_callback_info info)127     static napi_value StartRuntimeStat(napi_env env, napi_callback_info info)
128     {
129         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
130         engine->StartRuntimeStat();
131         napi_value result = nullptr;
132         NAPI_CALL(env, napi_get_undefined(env, &result));
133         return result;
134     }
135 
StopRuntimeStat(napi_env env,napi_callback_info info)136     static napi_value StopRuntimeStat(napi_env env, napi_callback_info info)
137     {
138         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
139         engine->StopRuntimeStat();
140         napi_value result = nullptr;
141         NAPI_CALL(env, napi_get_undefined(env, &result));
142         return result;
143     }
144 
GetArrayBufferSize(napi_env env,napi_callback_info info)145     static napi_value GetArrayBufferSize(napi_env env, napi_callback_info info)
146     {
147         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
148         auto value = static_cast<uint32_t>(engine->GetArrayBufferSize());
149         napi_value result = nullptr;
150         NAPI_CALL(env, napi_create_uint32(env, value, &result));
151         return result;
152     }
153 
GetHeapTotalSize(napi_env env,napi_callback_info info)154     static napi_value GetHeapTotalSize(napi_env env, napi_callback_info info)
155     {
156         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
157         auto value = static_cast<uint32_t>(engine->GetHeapTotalSize());
158         napi_value result = nullptr;
159         NAPI_CALL(env, napi_create_uint32(env, value, &result));
160         return result;
161     }
162 
GetHeapUsedSize(napi_env env,napi_callback_info info)163     static napi_value GetHeapUsedSize(napi_env env, napi_callback_info info)
164     {
165         NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
166         auto value = static_cast<uint32_t>(engine->GetHeapUsedSize());
167         napi_value result = nullptr;
168         NAPI_CALL(env, napi_create_uint32(env, value, &result));
169         return result;
170     }
171 
DfxInit(napi_env env,napi_value exports)172     static napi_value DfxInit(napi_env env, napi_value exports)
173     {
174         static napi_property_descriptor desc[] = {
175             DECLARE_NAPI_FUNCTION("dumpHeapSnapshot", DumpHeapSnapshot),
176             DECLARE_NAPI_FUNCTION("buildNativeAndJsStackTrace", BuildNativeAndJsStackTrace),
177             DECLARE_NAPI_FUNCTION("startHeapTracking", StartHeapTracking),
178             DECLARE_NAPI_FUNCTION("stopHeapTracking", StopHeapTracking),
179             DECLARE_NAPI_FUNCTION("printStatisticResult", PrintStatisticResult),
180             DECLARE_NAPI_FUNCTION("startRuntimeStat", StartRuntimeStat),
181             DECLARE_NAPI_FUNCTION("stopRuntimeStat", StopRuntimeStat),
182             DECLARE_NAPI_FUNCTION("getArrayBufferSize", GetArrayBufferSize),
183             DECLARE_NAPI_FUNCTION("getHeapTotalSize", GetHeapTotalSize),
184             DECLARE_NAPI_FUNCTION("getHeapUsedSize", GetHeapUsedSize),
185         };
186         NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
187         return exports;
188     }
189 
190     // dfx module define
191     static napi_module dfxModule = {
192         .nm_version = 1,
193         .nm_flags = 0,
194         .nm_filename = nullptr,
195         .nm_register_func = DfxInit,
196         .nm_modname = "dfx",
197         .nm_priv = reinterpret_cast<void*>(0),
198         .reserved = {0},
199     };
200 
201     // dfx module register
202     extern "C"
RegisterModule()203     __attribute__((constructor)) void RegisterModule()
204     {
205         napi_module_register(&dfxModule);
206     }
207 } // namespace OHOS::JsSysModule::Dfx
208