diff --git a/interfaces/kits/BUILD.gn b/interfaces/kits/BUILD.gn old mode 100755 new mode 100644 index 7141781..8c4bba5 --- a/interfaces/kits/BUILD.gn +++ b/interfaces/kits/BUILD.gn @@ -15,6 +15,9 @@ import("//build/ohos.gni") group("jsapi_kits_target") { if (support_jsapi) { - deps = [ "js/napi:bytrace" ] + deps = [ + "js/napi:bytrace", + "js/napi:hitracemeter_napi", + ] } } diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn old mode 100755 new mode 100644 index 27537f3..2155262 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -15,7 +15,27 @@ import("//build/ohos.gni") import("//developtools/bytrace_standard/bytrace.gni") ohos_shared_library("bytrace") { - sources = [ "bytrace.cpp" ] + sources = [ + "bytrace.cpp", + "declare_napi.cpp", + ] + deps = [ + "${innerkits_path}/native:bytrace_core", + "//foundation/ace/napi:ace_napi", + ] + external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + + relative_install_dir = "module" + + subsystem_name = "developtools" + part_name = "bytrace_standard" +} + +ohos_shared_library("hitracemeter_napi") { + sources = [ + "declare_napi.cpp", + "hitracemeter.cpp", + ] deps = [ "${innerkits_path}/native:bytrace_core", "//foundation/ace/napi:ace_napi", diff --git a/interfaces/kits/js/napi/bytrace.cpp b/interfaces/kits/js/napi/bytrace.cpp old mode 100755 new mode 100644 index 3dc5b6d..c100a96 --- a/interfaces/kits/js/napi/bytrace.cpp +++ b/interfaces/kits/js/napi/bytrace.cpp @@ -18,114 +18,10 @@ #include #include #include "bytrace.h" -#include "napi/native_api.h" -#include "napi/native_node_api.h" - -using namespace OHOS::HiviewDFX; -namespace { -constexpr int ARGC_NUMBER_TWO = 2; -constexpr int NAME_MAX_SIZE = 1024; -} - -static napi_value JSTraceStart(napi_env env, napi_callback_info info) -{ - constexpr int ARGC_NUMBER_THREE = 3; - size_t argc = ARGC_NUMBER_THREE; - napi_value argv[ARGC_NUMBER_THREE]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO || argc == ARGC_NUMBER_THREE, "Wrong number of arguments"); - - napi_valuetype valueType; - NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); - char buf[NAME_MAX_SIZE] = {0}; - size_t len = 0; - napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); - std::string name = std::string{buf}; - - NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); - int taskId = 0; - napi_get_value_int32(env, argv[1], &taskId); - if (argc == ARGC_NUMBER_TWO) { - StartAsyncTrace(BYTRACE_TAG_APP, name, taskId); - } else { - NAPI_CALL(env, napi_typeof(env, argv[ARGC_NUMBER_TWO], &valueType)); - NAPI_ASSERT(env, valueType == napi_number, "Third arg type error, should is number"); - double limit = 0; - napi_get_value_double(env, argv[ARGC_NUMBER_TWO], &limit); - StartAsyncTrace(BYTRACE_TAG_APP, name, taskId, limit); - } - return nullptr; -} - -static napi_value JSTraceFinish(napi_env env, napi_callback_info info) -{ - size_t argc = ARGC_NUMBER_TWO; - napi_value argv[ARGC_NUMBER_TWO]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments"); - - napi_valuetype valueType; - NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); - char buf[NAME_MAX_SIZE] = {0}; - size_t len = 0; - napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); - std::string name = std::string{buf}; - - NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); - int taskId = 0; - napi_get_value_int32(env, argv[1], &taskId); - FinishAsyncTrace(BYTRACE_TAG_APP, name, taskId); - return nullptr; -} - -static napi_value JSTraceCount(napi_env env, napi_callback_info info) -{ - size_t argc = ARGC_NUMBER_TWO; - napi_value argv[ARGC_NUMBER_TWO]; - napi_value thisVar; - NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); - NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments"); - - napi_valuetype valueType; - NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); - NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); - char buf[NAME_MAX_SIZE] = {0}; - size_t len = 0; - napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); - std::string name = std::string{buf}; - - NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); - NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); - int64_t count = 0; - napi_get_value_int64(env, argv[1], &count); - CountTrace(BYTRACE_TAG_APP, name, count); - return nullptr; -} - -EXTERN_C_START -/* - * function for module exports - */ -static napi_value BytraceInit(napi_env env, napi_value exports) -{ - static napi_property_descriptor desc[] = { - DECLARE_NAPI_FUNCTION("startTrace", JSTraceStart), - DECLARE_NAPI_FUNCTION("finishTrace", JSTraceFinish), - DECLARE_NAPI_FUNCTION("traceByValue", JSTraceCount), - }; - NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); - return exports; -} -EXTERN_C_END +#include "declare_napi.h" /* - * Module definition + * bytrace module definition */ static napi_module bytrace_module = { .nm_version = 1, diff --git a/interfaces/kits/js/napi/declare_napi.cpp b/interfaces/kits/js/napi/declare_napi.cpp new file mode 100644 index 0000000..38eef46 --- /dev/null +++ b/interfaces/kits/js/napi/declare_napi.cpp @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "declare_napi.h" + +#include +#include +#include +#include +#include "bytrace.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +using namespace OHOS::HiviewDFX; +namespace { +constexpr int ARGC_NUMBER_TWO = 2; +constexpr int NAME_MAX_SIZE = 1024; +} + +static napi_value JSTraceStart(napi_env env, napi_callback_info info) +{ + constexpr int ARGC_NUMBER_THREE = 3; + size_t argc = ARGC_NUMBER_THREE; + napi_value argv[ARGC_NUMBER_THREE]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO || argc == ARGC_NUMBER_THREE, "Wrong number of arguments"); + + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); + char buf[NAME_MAX_SIZE] = {0}; + size_t len = 0; + napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); + std::string name = std::string{buf}; + + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); + int taskId = 0; + napi_get_value_int32(env, argv[1], &taskId); + if (argc == ARGC_NUMBER_TWO) { + StartAsyncTrace(BYTRACE_TAG_APP, name, taskId); + } else { + NAPI_CALL(env, napi_typeof(env, argv[ARGC_NUMBER_TWO], &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Third arg type error, should is number"); + double limit = 0; + napi_get_value_double(env, argv[ARGC_NUMBER_TWO], &limit); + StartAsyncTrace(BYTRACE_TAG_APP, name, taskId, limit); + } + return nullptr; +} + +static napi_value JSTraceFinish(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_NUMBER_TWO; + napi_value argv[ARGC_NUMBER_TWO]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments"); + + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); + char buf[NAME_MAX_SIZE] = {0}; + size_t len = 0; + napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); + std::string name = std::string{buf}; + + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); + int taskId = 0; + napi_get_value_int32(env, argv[1], &taskId); + FinishAsyncTrace(BYTRACE_TAG_APP, name, taskId); + return nullptr; +} + +static napi_value JSTraceCount(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_NUMBER_TWO; + napi_value argv[ARGC_NUMBER_TWO]; + napi_value thisVar; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + NAPI_ASSERT(env, argc == ARGC_NUMBER_TWO, "Wrong number of arguments"); + + napi_valuetype valueType; + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_string, "First arg type error, should is string"); + char buf[NAME_MAX_SIZE] = {0}; + size_t len = 0; + napi_get_value_string_utf8(env, argv[0], buf, NAME_MAX_SIZE, &len); + std::string name = std::string{buf}; + + NAPI_CALL(env, napi_typeof(env, argv[1], &valueType)); + NAPI_ASSERT(env, valueType == napi_number, "Second arg type error, should is number"); + int64_t count = 0; + napi_get_value_int64(env, argv[1], &count); + CountTrace(BYTRACE_TAG_APP, name, count); + return nullptr; +} + +EXTERN_C_START +/* + * function for module exports + */ +napi_value BytraceInit(napi_env env, napi_value exports) +{ + static napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("startTrace", JSTraceStart), + DECLARE_NAPI_FUNCTION("finishTrace", JSTraceFinish), + DECLARE_NAPI_FUNCTION("traceByValue", JSTraceCount), + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); + return exports; +} +EXTERN_C_END diff --git a/interfaces/kits/js/napi/declare_napi.h b/interfaces/kits/js/napi/declare_napi.h new file mode 100644 index 0000000..d1f83ae --- /dev/null +++ b/interfaces/kits/js/napi/declare_napi.h @@ -0,0 +1,26 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef DEVELOPTOOLS_INTERFACES_KITS_DECLARE_NAPI_H +#define DEVELOPTOOLS_INTERFACES_KITS_DECLARE_NAPI_H + +#include "napi/native_api.h" +#include "napi/native_node_api.h" + +EXTERN_C_START +napi_value BytraceInit(napi_env env, napi_value exports); +EXTERN_C_END + +#endif // DEVELOPTOOLS_INTERFACES_KITS_DECLARE_NAPI_H \ No newline at end of file diff --git a/interfaces/kits/js/napi/hitracemeter.cpp b/interfaces/kits/js/napi/hitracemeter.cpp new file mode 100644 index 0000000..2dd64c8 --- /dev/null +++ b/interfaces/kits/js/napi/hitracemeter.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include "bytrace.h" +#include "declare_napi.h" + +/* + * hiTraceMeter module definition + */ +static napi_module hitracemeter_module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = "hiTraceMeter", + .nm_register_func = BytraceInit, + .nm_modname = "hiTraceMeter", + .nm_priv = ((void *)0), + .reserved = {0} +}; + +/* + * Module registration + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&hitracemeter_module); +}