HiTrace Js Api

Signed-off-by: xuyong <xuyong59@huawei.com>
This commit is contained in:
xuyong
2022-02-12 15:07:44 +08:00
parent cbd418744c
commit 6f7518a2da
5 changed files with 199 additions and 118 deletions
Executable → Regular
+4 -1
View File
@@ -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",
]
}
}
Executable → Regular
+21 -1
View File
@@ -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",
+2 -116
View File
@@ -21,111 +21,12 @@
#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 napi_value BytraceInit(napi_env env, napi_value exports);
EXTERN_C_END
/*
* Bytrace module definition
* bytrace module definition
*/
static napi_module bytrace_module = {
.nm_version = 1,
@@ -137,25 +38,10 @@ static napi_module bytrace_module = {
.reserved = {0}
};
/*
* 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(&bytrace_module);
napi_module_register(&hitracemeter_module);
}
+125
View File
@@ -0,0 +1,125 @@
/*
* 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 <cstdio>
#include <cstdlib>
#include <string>
#include <hilog/log.h>
#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;
}
extern 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;
}
extern 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;
}
extern 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
*/
extern 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
+47
View File
@@ -0,0 +1,47 @@
/*
* 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 <cstdio>
#include <cstdlib>
#include <string>
#include <hilog/log.h>
#include "bytrace.h"
#include "napi/native_api.h"
#include "napi/native_node_api.h"
EXTERN_C_START
extern napi_value BytraceInit(napi_env env, napi_value exports);
EXTERN_C_END
/*
* 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);
}