• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
JsStrNumParamsFunc(napi_env & env,napi_callback_info & info,STR_NUM_PARAM_FUNC nativeCall)96 bool JsStrNumParamsFunc(napi_env& env, napi_callback_info& info, STR_NUM_PARAM_FUNC nativeCall)
97 {
98     size_t argc = ARGC_NUMBER_TWO;
99     napi_value argv[ARGC_NUMBER_TWO];
100     ParseParams(env, info, argc, argv);
101     if (argc != ARGC_NUMBER_TWO) {
102         HiLog::Error(LABEL, "Wrong number of parameters.");
103         return false;
104     }
105     std::string name;
106     if (!ParseStringParam(env, argv[FIRST_ARG_INDEX], name)) {
107         return false;
108     }
109     if (!nativeCall(name, argv[SECOND_ARG_INDEX])) {
110         return false;
111     }
112     return true;
113 }
114 }
115 
JSTraceStart(napi_env env,napi_callback_info info)116 static napi_value JSTraceStart(napi_env env, napi_callback_info info)
117 {
118     size_t argc = ARGC_NUMBER_THREE;
119     napi_value argv[ARGC_NUMBER_THREE];
120     ParseParams(env, info, argc, argv);
121     NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO || argc == ARGC_NUMBER_THREE, "Wrong number of arguments");
122     if (argc != ARGC_NUMBER_TWO && argc != ARGC_NUMBER_THREE) {
123         HiLog::Error(LABEL, "Wrong number of parameters.");
124     }
125     std::string name;
126     if (!ParseStringParam(env, argv[FIRST_ARG_INDEX], name)) {
127         return nullptr;
128     }
129     int taskId = 0;
130     if (!ParseInt32Param(env, argv[SECOND_ARG_INDEX], taskId)) {
131         return nullptr;
132     }
133     if (argc == ARGC_NUMBER_TWO) {
134         StartAsyncTrace(HITRACE_TAG_APP, name, taskId);
135     } else {
136         double limit = 0.0;
137         if (!ParseDoubleParam(env, argv[THIRD_ARG_INDEX], limit)) {
138             return nullptr;
139         }
140         StartAsyncTrace(HITRACE_TAG_APP, name, taskId, limit);
141     }
142     return nullptr;
143 }
144 
JSTraceFinish(napi_env env,napi_callback_info info)145 static napi_value JSTraceFinish(napi_env env, napi_callback_info info)
146 {
147     size_t argc = ARGC_NUMBER_TWO;
148     napi_value argv[ARGC_NUMBER_TWO];
149     napi_value thisVar;
150     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
151     NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments");
152     (void)JsStrNumParamsFunc(env, info, [&env] (std::string name, napi_value& nValue) -> bool {
153         int taskId = 0;
154         if (!ParseInt32Param(env, nValue, taskId)) {
155             return false;
156         }
157         FinishAsyncTrace(HITRACE_TAG_APP, name, taskId);
158         return true;
159     });
160     return nullptr;
161 }
162 
JSTraceCount(napi_env env,napi_callback_info info)163 static napi_value JSTraceCount(napi_env env, napi_callback_info info)
164 {
165     size_t argc = ARGC_NUMBER_TWO;
166     napi_value argv[ARGC_NUMBER_TWO];
167     napi_value thisVar;
168     NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL));
169     NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments");
170     (void)JsStrNumParamsFunc(env, info, [&env] (std::string name, napi_value& nValue) -> bool {
171         int64_t count = 0;
172         if (!ParseInt64Param(env, nValue, count)) {
173             return false;
174         }
175         CountTrace(HITRACE_TAG_APP, name, count);
176         return true;
177     });
178     return nullptr;
179 }
180 
181 /*
182  * function for module exports
183  */
184 EXTERN_C_START
BytraceInit(napi_env env,napi_value exports)185 static napi_value BytraceInit(napi_env env, napi_value exports)
186 {
187     static napi_property_descriptor desc[] = {
188         DECLARE_NAPI_FUNCTION("startTrace", JSTraceStart),
189         DECLARE_NAPI_FUNCTION("finishTrace", JSTraceFinish),
190         DECLARE_NAPI_FUNCTION("traceByValue", JSTraceCount),
191     };
192     NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc));
193     return exports;
194 }
195 EXTERN_C_END
196 
197 /*
198  * bytrace module definition
199  */
200 static napi_module bytrace_module = {
201     .nm_version = 1,
202     .nm_flags = 0,
203     .nm_filename = "bytrace",
204     .nm_register_func = BytraceInit,
205     .nm_modname = "bytrace",
206     .nm_priv = ((void *)0),
207     .reserved = {0}
208 };
209 
210 /*
211  * Module registration
212  */
RegisterModule(void)213 extern "C" __attribute__((constructor)) void RegisterModule(void)
214 {
215     napi_module_register(&bytrace_module);
216 }
217