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