1 /*
2 * Copyright (C) 2021 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 <cstdio>
17 #include <functional>
18 #include <string>
19 #include <hilog/log.h>
20 #include "hitrace_meter.h"
21 #include "bytrace_napi_common.h"
22
23 using namespace OHOS::HiviewDFX;
24 namespace {
25 constexpr int FIRST_ARG_INDEX = 0;
26 constexpr int SECOND_ARG_INDEX = 1;
27 constexpr int THIRD_ARG_INDEX = 2;
28 constexpr int ARGC_NUMBER_TWO = 2;
29 constexpr int ARGC_NUMBER_THREE = 3;
30 constexpr uint64_t HITRACE_METER_TAG = 0xD002D33;
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HITRACE_METER_TAG, "HITRACE_METER_JS"};
32 using STR_NUM_PARAM_FUNC = std::function<bool(std::string, napi_value&)>;
33
ParseParams(napi_env & env,napi_callback_info & info,size_t & argc,napi_value * argv)34 napi_value ParseParams(napi_env& env, napi_callback_info& info, size_t& argc, napi_value* argv)
35 {
36 napi_value thisVar;
37 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
38 return nullptr;
39 }
40
TypeCheck(const napi_env & env,const napi_value & value,const napi_valuetype expectType)41 bool TypeCheck(const napi_env& env, const napi_value& value, const napi_valuetype expectType)
42 {
43 napi_valuetype valueType;
44 napi_status status = napi_typeof(env, value, &valueType);
45 if (status != napi_ok) {
46 HiLog::Error(LABEL, "Failed to get the type of the argument.");
47 return false;
48 }
49 if (valueType != expectType) {
50 HiLog::Error(LABEL, "Type of the parameter is invalid.");
51 return false;
52 }
53 return true;
54 }
55
ParseStringParam(const napi_env & env,const napi_value & value,std::string & dest)56 bool ParseStringParam(const napi_env& env, const napi_value& value, std::string& dest)
57 {
58 if (!TypeCheck(env, value, napi_string)) {
59 return false;
60 }
61 constexpr int nameMaxSize = 1024;
62 char buf[nameMaxSize] = {0};
63 size_t len = 0;
64 napi_get_value_string_utf8(env, value, buf, nameMaxSize, &len);
65 dest = std::string {buf};
66 return true;
67 }
68
ParseInt32Param(const napi_env & env,const napi_value & value,int & dest)69 bool ParseInt32Param(const napi_env& env, const napi_value& value, int& dest)
70 {
71 if (!TypeCheck(env, value, napi_number)) {
72 return false;
73 }
74 napi_get_value_int32(env, value, &dest);
75 return true;
76 }
77
ParseInt64Param(const napi_env & env,const napi_value & value,int64_t & dest)78 bool ParseInt64Param(const napi_env& env, const napi_value& value, int64_t& dest)
79 {
80 if (!TypeCheck(env, value, napi_number)) {
81 return false;
82 }
83 napi_get_value_int64(env, value, &dest);
84 return true;
85 }
86
ParseDoubleParam(const napi_env & env,const napi_value & value,double & dest)87 bool ParseDoubleParam(const napi_env& env, const napi_value& value, double& dest)
88 {
89 if (!TypeCheck(env, value, napi_number)) {
90 return false;
91 }
92 napi_get_value_double(env, value, &dest);
93 return true;
94 }
95
ParseUndefinedParam(const napi_env & env,const napi_value & value)96 bool ParseUndefinedParam(const napi_env& env, const napi_value& value)
97 {
98 return TypeCheck(env, value, napi_undefined);
99 }
100
JsStrNumParamsFunc(napi_env & env,napi_callback_info & info,STR_NUM_PARAM_FUNC nativeCall)101 bool JsStrNumParamsFunc(napi_env& env, napi_callback_info& info, STR_NUM_PARAM_FUNC nativeCall)
102 {
103 size_t argc = ARGC_NUMBER_TWO;
104 napi_value argv[ARGC_NUMBER_TWO];
105 ParseParams(env, info, argc, argv);
106 if (argc != ARGC_NUMBER_TWO) {
107 HiLog::Error(LABEL, "Wrong number of parameters.");
108 return false;
109 }
110 std::string name;
111 if (!ParseStringParam(env, argv[FIRST_ARG_INDEX], name)) {
112 return false;
113 }
114 if (!nativeCall(name, argv[SECOND_ARG_INDEX])) {
115 return false;
116 }
117 return true;
118 }
119 }
120
JSTraceStart(napi_env env,napi_callback_info info)121 static napi_value JSTraceStart(napi_env env, napi_callback_info info)
122 {
123 size_t argc = ARGC_NUMBER_THREE;
124 napi_value argv[ARGC_NUMBER_THREE];
125 ParseParams(env, info, argc, argv);
126 NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO || argc == ARGC_NUMBER_THREE, "Wrong number of arguments");
127 if (argc != ARGC_NUMBER_TWO && argc != ARGC_NUMBER_THREE) {
128 HiLog::Error(LABEL, "Wrong number of parameters.");
129 }
130 std::string name;
131 if (!ParseStringParam(env, argv[FIRST_ARG_INDEX], name)) {
132 return nullptr;
133 }
134 int taskId = 0;
135 if (!ParseInt32Param(env, argv[SECOND_ARG_INDEX], taskId)) {
136 return nullptr;
137 }
138 if (argc == ARGC_NUMBER_TWO) {
139 StartAsyncTrace(HITRACE_TAG_APP, name, taskId);
140 } else {
141 double limit = 0.0;
142 if (!ParseDoubleParam(env, argv[THIRD_ARG_INDEX], limit) && !ParseUndefinedParam(env, argv[THIRD_ARG_INDEX])) {
143 HiLog::Error(LABEL, "ParseDoubleParam false and ParseUndefinedParam false.");
144 return nullptr;
145 }
146 StartAsyncTrace(HITRACE_TAG_APP, name, taskId, limit);
147 }
148 return nullptr;
149 }
150
JSTraceFinish(napi_env env,napi_callback_info info)151 static napi_value JSTraceFinish(napi_env env, napi_callback_info info)
152 {
153 size_t argc = ARGC_NUMBER_TWO;
154 napi_value argv[ARGC_NUMBER_TWO];
155 napi_value thisVar;
156 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
157 NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments");
158 (void)JsStrNumParamsFunc(env, info, [&env] (std::string name, napi_value& nValue) -> bool {
159 int taskId = 0;
160 if (!ParseInt32Param(env, nValue, taskId)) {
161 return false;
162 }
163 FinishAsyncTrace(HITRACE_TAG_APP, name, taskId);
164 return true;
165 });
166 return nullptr;
167 }
168
JSTraceCount(napi_env env,napi_callback_info info)169 static napi_value JSTraceCount(napi_env env, napi_callback_info info)
170 {
171 size_t argc = ARGC_NUMBER_TWO;
172 napi_value argv[ARGC_NUMBER_TWO];
173 napi_value thisVar;
174 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
175 NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments");
176 (void)JsStrNumParamsFunc(env, info, [&env] (std::string name, napi_value& nValue) -> bool {
177 int64_t count = 0;
178 if (!ParseInt64Param(env, nValue, count)) {
179 return false;
180 }
181 CountTrace(HITRACE_TAG_APP, name, count);
182 return true;
183 });
184 return nullptr;
185 }
186
187 /*
188 * function for module exports
189 */
190 EXTERN_C_START
BytraceInit(napi_env env,napi_value exports)191 static napi_value BytraceInit(napi_env env, napi_value exports)
192 {
193 static napi_property_descriptor desc[] = {
194 DECLARE_NAPI_FUNCTION("startTrace", JSTraceStart),
195 DECLARE_NAPI_FUNCTION("finishTrace", JSTraceFinish),
196 DECLARE_NAPI_FUNCTION("traceByValue", JSTraceCount),
197 };
198 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
199 return exports;
200 }
201 EXTERN_C_END
202
203 /*
204 * bytrace module definition
205 */
206 static napi_module bytrace_module = {
207 .nm_version = 1,
208 .nm_flags = 0,
209 .nm_filename = "bytrace",
210 .nm_register_func = BytraceInit,
211 .nm_modname = "bytrace",
212 .nm_priv = ((void *)0),
213 .reserved = {0}
214 };
215
216 /*
217 * Module registration
218 */
RegisterModule(void)219 extern "C" __attribute__((constructor)) void RegisterModule(void)
220 {
221 napi_module_register(&bytrace_module);
222 }
223