• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
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 
17 #include "napi_hidebug_vm.h"
18 
19 #include "error_code.h"
20 #include "hiappevent_util.h"
21 #include "napi_util.h"
22 #include "native_engine/native_engine.h"
23 
24 namespace OHOS {
25 namespace HiviewDFX {
26 namespace {
GetGcCount(napi_env env)27 napi_value GetGcCount(napi_env env)
28 {
29     NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
30     napi_value gcCount;
31     napi_create_bigint_uint64(env, engine->GetGCCount(), &gcCount);
32     return gcCount;
33 }
34 
GetGcTime(napi_env env)35 napi_value GetGcTime(napi_env env)
36 {
37     NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
38     napi_value gcTime;
39     napi_create_bigint_uint64(env, engine->GetGCDuration(), &gcTime);
40     return gcTime;
41 }
42 
GetGcBytesAllocated(napi_env env)43 napi_value GetGcBytesAllocated(napi_env env)
44 {
45     NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
46     napi_value gcBytesAllocated;
47     napi_create_bigint_uint64(env, engine->GetAccumulatedAllocateSize(), &gcBytesAllocated);
48     return gcBytesAllocated;
49 }
50 
GetGcBytesFreed(napi_env env)51 napi_value GetGcBytesFreed(napi_env env)
52 {
53     NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
54     napi_value gcBytesFreed;
55     napi_create_bigint_uint64(env, engine->GetAccumulatedFreeSize(), &gcBytesFreed);
56     return gcBytesFreed;
57 }
58 
GetFullGcLongTimeCount(napi_env env)59 napi_value GetFullGcLongTimeCount(napi_env env)
60 {
61     NativeEngine *engine = reinterpret_cast<NativeEngine*>(env);
62     napi_value fullGcLongTimeCount;
63     napi_create_bigint_uint64(env, engine->GetFullGCLongTimeCount(), &fullGcLongTimeCount);
64     return fullGcLongTimeCount;
65 }
66 
GetVmGcFunctionMap()67 inline std::map<std::string, napi_value (*)(napi_env value)> GetVmGcFunctionMap()
68 {
69     return {
70         {"ark.gc.gc-count", GetGcCount},
71         {"ark.gc.gc-time", GetGcTime},
72         {"ark.gc.gc-bytes-allocated", GetGcBytesAllocated},
73         {"ark.gc.gc-bytes-freed", GetGcBytesFreed},
74         {"ark.gc.fullgc-longtime-count", GetFullGcLongTimeCount},
75     };
76 }
77 }
78 
GetVMRuntimeStats(napi_env env,napi_callback_info info)79 napi_value GetVMRuntimeStats(napi_env env, napi_callback_info info)
80 {
81     ApiInvokeRecorder apiInvokeRecorder("getVMRuntimeStats");
82     napi_value vmRunTimeStats;
83     napi_create_object(env, &vmRunTimeStats);
84     auto vmGCFuncMap = GetVmGcFunctionMap();
85     for (const auto &[k, v] : vmGCFuncMap) {
86         napi_set_named_property(env, vmRunTimeStats, k.c_str(), v(env));
87     }
88     return vmRunTimeStats;
89 }
90 
GetVMRuntimeStat(napi_env env,napi_callback_info info)91 napi_value GetVMRuntimeStat(napi_env env, napi_callback_info info)
92 {
93     ApiInvokeRecorder apiInvokeRecorder("getVMRuntimeStat");
94     std::string param;
95     if (!GetTheOnlyStringParam(env, info, param)) {
96         std::string paramErrorMessage = "Invalid parameter, a string parameter required.";
97         napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
98         apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
99         return CreateUndefined(env);
100     }
101     auto vmGCFuncMap = GetVmGcFunctionMap();
102     auto target = vmGCFuncMap.find(param);
103     if (target == vmGCFuncMap.end()) {
104         std::string paramErrorMessage = "Invalid parameter, unknown property.";
105         napi_throw_error(env, std::to_string(ErrorCode::PARAMETER_ERROR).c_str(), paramErrorMessage.c_str());
106         apiInvokeRecorder.SetErrorCode(ErrorCode::PARAMETER_ERROR);
107         return CreateUndefined(env);
108     }
109     return target->second(env);
110 }
111 }
112 }