From 07d320ce004e231b8047be6f6066e0ebd2f4872d Mon Sep 17 00:00:00 2001 From: zhou-liting125 Date: Thu, 20 Jan 2022 21:45:22 +0800 Subject: [PATCH] add bytrace Signed-off-by: zhou-liting125 --- interfaces/kits/js/napi/BUILD.gn | 5 ++ .../bytrace/include/rpc_bytrace_inner.h | 33 +++++++++++ .../c/adapter/bytrace/source/rpc_bytrace.c | 57 +++++++++++++++++++ .../bytrace/source/rpc_bytrace_inner.cpp | 57 +++++++++++++++++++ .../bytrace/source/rpc_bytrace_virtual.c | 42 ++++++++++++++ ipc/native/c/adapter/include/rpc_bytrace.h | 33 +++++++++++ .../src/napi/include/napi_remote_object.h | 3 + .../src/napi/src/napi_remote_object.cpp | 57 ++++++++++++++++++- 8 files changed, 284 insertions(+), 3 deletions(-) create mode 100644 ipc/native/c/adapter/bytrace/include/rpc_bytrace_inner.h create mode 100644 ipc/native/c/adapter/bytrace/source/rpc_bytrace.c create mode 100644 ipc/native/c/adapter/bytrace/source/rpc_bytrace_inner.cpp create mode 100644 ipc/native/c/adapter/bytrace/source/rpc_bytrace_virtual.c create mode 100644 ipc/native/c/adapter/include/rpc_bytrace.h diff --git a/interfaces/kits/js/napi/BUILD.gn b/interfaces/kits/js/napi/BUILD.gn index 6a39f85..8010262 100644 --- a/interfaces/kits/js/napi/BUILD.gn +++ b/interfaces/kits/js/napi/BUILD.gn @@ -25,10 +25,14 @@ ohos_shared_library("rpc") { "$SUBSYSTEM_DIR/utils/include", "//foundation/ace/napi/interfaces/kits", "//utils/system/safwk/native/include", + "$SUBSYSTEM_DIR/ipc/native/c/adapter/bytrace/include", + "$SUBSYSTEM_DIR/ipc/native/c/adapter/include", ] public_configs = [ ":rpc_public_config" ] sources = [ + "$SUBSYSTEM_DIR/ipc/native/c/adapter/bytrace/source/rpc_bytrace.c", + "$SUBSYSTEM_DIR/ipc/native/c/adapter/bytrace/source/rpc_bytrace_inner.cpp", "$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_ashmem.cpp", "$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_option.cpp", "$SUBSYSTEM_DIR/ipc/native/src/napi/src/napi_message_parcel.cpp", @@ -43,6 +47,7 @@ ohos_shared_library("rpc") { ] external_deps = [ + "bytrace_standard:bytrace_core", "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", ] diff --git a/ipc/native/c/adapter/bytrace/include/rpc_bytrace_inner.h b/ipc/native/c/adapter/bytrace/include/rpc_bytrace_inner.h new file mode 100644 index 0000000..3b99982 --- /dev/null +++ b/ipc/native/c/adapter/bytrace/include/rpc_bytrace_inner.h @@ -0,0 +1,33 @@ +/* + * 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 RPC_BYTRACE_INNER_H +#define RPC_BYTRACE_INNER_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void RpcStartTraceInner(uint64_t label, const char *value); +void RpcFinishTraceInner(uint64_t label); +void RpcStartAsyncTraceInner(uint64_t label, const char *value, int32_t TraceId); +void RpcFinishAsyncTraceInner(uint64_t label, const char *value, int32_t TraceId); +void RpcMiddleTraceInner(uint64_t label, const char *beforeValue, const char *afterValue); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/ipc/native/c/adapter/bytrace/source/rpc_bytrace.c b/ipc/native/c/adapter/bytrace/source/rpc_bytrace.c new file mode 100644 index 0000000..b8c95cd --- /dev/null +++ b/ipc/native/c/adapter/bytrace/source/rpc_bytrace.c @@ -0,0 +1,57 @@ +/* + * 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 "rpc_bytrace.h" +#include +#include +#include "rpc_bytrace_inner.h" + +static const uint64_t BYTRACE_TAG_RPC = (1ULL << 46); // RPC and IPC tag. +void RpcStartTrace(const char *value) +{ + if (value == NULL) { + return; + } + RpcStartTraceInner(BYTRACE_TAG_RPC, value); +} + +void RpcFinishTrace(void) +{ + RpcFinishTraceInner(BYTRACE_TAG_RPC); +} + +void RpcStartAsyncTrace(const char *value, int32_t TraceId) +{ + if (value == NULL) { + return; + } + RpcStartAsyncTraceInner(BYTRACE_TAG_RPC, value, TraceId); +} + +void RpcFinishAsyncTrace(const char *value, int32_t TraceId) +{ + if (value == NULL) { + return; + } + RpcFinishAsyncTraceInner(BYTRACE_TAG_RPC, value, TraceId); +} + +void RpcMiddleTrace(const char *beforeValue, const char *afterValue) +{ + if (beforeValue == NULL || afterValue == NULL) { + return; + } + RpcMiddleTraceInner(BYTRACE_TAG_RPC, beforeValue, afterValue); +} \ No newline at end of file diff --git a/ipc/native/c/adapter/bytrace/source/rpc_bytrace_inner.cpp b/ipc/native/c/adapter/bytrace/source/rpc_bytrace_inner.cpp new file mode 100644 index 0000000..8de7dd7 --- /dev/null +++ b/ipc/native/c/adapter/bytrace/source/rpc_bytrace_inner.cpp @@ -0,0 +1,57 @@ +/* + * 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 "rpc_bytrace_inner.h" +#include +#include +#include "bytrace.h" +using namespace std; + +void RpcStartTraceInner(uint64_t label, const char *value) +{ + if (value == nullptr) { + return; + } + StartTrace(label, value); +} + +void RpcFinishTraceInner(uint64_t label) +{ + FinishTrace(label); +} + +void RpcStartAsyncTraceInner(uint64_t label, const char *value, int32_t TraceId) +{ + if (value == nullptr) { + return; + } + StartAsyncTrace(label, value, TraceId); +} + +void RpcFinishAsyncTraceInner(uint64_t label, const char *value, int32_t TraceId) +{ + if (value == nullptr) { + return; + } + FinishAsyncTrace(label, value, TraceId); +} + +void RpcMiddleTraceInner(uint64_t label, const char *beforeValue, const char *afterValue) +{ + if (beforeValue == nullptr || afterValue == nullptr) { + return; + } + MiddleTrace(label, beforeValue, afterValue); +} \ No newline at end of file diff --git a/ipc/native/c/adapter/bytrace/source/rpc_bytrace_virtual.c b/ipc/native/c/adapter/bytrace/source/rpc_bytrace_virtual.c new file mode 100644 index 0000000..17aaf7a --- /dev/null +++ b/ipc/native/c/adapter/bytrace/source/rpc_bytrace_virtual.c @@ -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 "rpc_bytrace.h" + +void RpcStartTrace(const char *value) +{ + return; +} + +void RpcFinishTrace(void) +{ + return; +} + +void RpcStartAsyncTrace(const char *value, int32_t TraceId) +{ + return; +} + +void RpcFinishAsyncTrace(const char *value, int32_t TraceId) +{ + return; +} + +void RpcMiddleTrace(const char *beforeValue, const char *afterValue) +{ + return; +} \ No newline at end of file diff --git a/ipc/native/c/adapter/include/rpc_bytrace.h b/ipc/native/c/adapter/include/rpc_bytrace.h new file mode 100644 index 0000000..ada386a --- /dev/null +++ b/ipc/native/c/adapter/include/rpc_bytrace.h @@ -0,0 +1,33 @@ +/* + * 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 RPC_BYTRACE_H +#define RPC_BYTRACE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void RpcStartTrace(const char *value); +void RpcFinishTrace(void); +void RpcStartAsyncTrace(const char *value, int32_t TraceId); +void RpcFinishAsyncTrace(const char *value, int32_t TraceId); +void RpcMiddleTrace(const char *beforeValue, const char *afterValue); +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file diff --git a/ipc/native/src/napi/include/napi_remote_object.h b/ipc/native/src/napi/include/napi_remote_object.h index 4048d0b..0184917 100644 --- a/ipc/native/src/napi/include/napi_remote_object.h +++ b/ipc/native/src/napi/include/napi_remote_object.h @@ -21,6 +21,7 @@ #include "napi/native_node_api.h" #include "refbase.h" +constexpr size_t TRACESIZE = 64; namespace OHOS { EXTERN_C_START napi_value NAPIIPCSkeletonExport(napi_env env, napi_value exports); @@ -98,6 +99,8 @@ EXTERN_C_END napi_ref jsReplyRef; napi_ref callback; napi_env env; + char traceVaue[TRACESIZE]; + int32_t traceId; }; } // namespace OHOS #endif // NAPI_IPC_OHOS_REMOTE_OBJECT_H \ No newline at end of file diff --git a/ipc/native/src/napi/src/napi_remote_object.cpp b/ipc/native/src/napi/src/napi_remote_object.cpp index 4c71a5e..3c69246 100644 --- a/ipc/native/src/napi/src/napi_remote_object.cpp +++ b/ipc/native/src/napi/src/napi_remote_object.cpp @@ -29,8 +29,11 @@ #include "log_tags.h" #include "napi_message_option.h" #include "napi_message_parcel.h" +#include "rpc_bytrace.h" #include "string_ex.h" +static std::atomic bytraceId = 1000; + namespace OHOS { static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "napi_remoteObject" }; #ifndef TITLE @@ -1185,7 +1188,9 @@ void StubExecuteSendRequest(napi_env env, SendRequestParam *param) param->errCode = param->target->SendRequest(param->code, *(param->data.get()), *(param->reply.get()), param->option); DBINDER_LOGI("sendRequest done, errCode:%{public}d", param->errCode); - + if (param->traceId != 0) { + RpcFinishAsyncTrace(param->traceVaue, param->traceId); + } uv_loop_s *loop = nullptr; napi_get_uv_event_loop(env, &loop); uv_work_t *work = new uv_work_t; @@ -1245,7 +1250,18 @@ napi_value StubSendRequestAsync(napi_env env, sptr target, uint32 .jsReplyRef = nullptr, .callback = nullptr, .env = env, + .traceVaue = "", + .traceId = 0, }; + IPCObjectProxy *targetProxy = reinterpret_cast(target.GetRefPtr()); + if (targetProxy != nullptr) { + std::u16string remoteDescriptor = targetProxy->GetInterfaceDescriptor(); + if (sprintf_s(sendRequestParam->traceVaue, sizeof(sendRequestParam->traceVaue), "%s:%d", + Str16ToStr8(remoteDescriptor).c_str(), code) > 0) { + sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); + RpcStartAsyncTrace(sendRequestParam->traceVaue, sendRequestParam->traceId); + } + } napi_create_reference(env, argv[0], 1, &sendRequestParam->jsCodeRef); napi_create_reference(env, argv[1], 1, &sendRequestParam->jsDataRef); napi_create_reference(env, argv[2], 1, &sendRequestParam->jsReplyRef); @@ -1278,7 +1294,18 @@ napi_value StubSendRequestPromise(napi_env env, sptr target, uint .jsReplyRef = nullptr, .callback = nullptr, .env = env, + .traceVaue = "", + .traceId = 0, }; + IPCObjectProxy *targetProxy = reinterpret_cast(target.GetRefPtr()); + if (targetProxy != nullptr) { + std::u16string remoteDescriptor = targetProxy->GetInterfaceDescriptor(); + if (sprintf_s(sendRequestParam->traceVaue, sizeof(sendRequestParam->traceVaue), "%s:%d", + Str16ToStr8(remoteDescriptor).c_str(), code) > 0) { + sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); + RpcStartAsyncTrace(sendRequestParam->traceVaue, sendRequestParam->traceId); + } + } napi_create_reference(env, argv[0], 1, &sendRequestParam->jsCodeRef); napi_create_reference(env, argv[1], 1, &sendRequestParam->jsDataRef); napi_create_reference(env, argv[2], 1, &sendRequestParam->jsReplyRef); @@ -1393,6 +1420,9 @@ void ExecuteSendRequest(napi_env env, void *data) param->errCode = param->target->SendRequest(param->code, *(param->data.get()), *(param->reply.get()), param->option); DBINDER_LOGI("sendRequest done, errCode:%{public}d", param->errCode); + if (param->traceId != 0) { + RpcFinishAsyncTrace(param->traceVaue, param->traceId); + } } // This method runs on the main thread after 'ExecuteSendRequest' exits @@ -1435,6 +1465,7 @@ napi_value SendRequestAsync(napi_env env, sptr target, uint32_t c std::shared_ptr data, std::shared_ptr reply, MessageOption &option, napi_value *argv) { + napi_value result = nullptr; SendRequestParam *sendRequestParam = new SendRequestParam { .target = target, .code = code, @@ -1449,7 +1480,18 @@ napi_value SendRequestAsync(napi_env env, sptr target, uint32_t c .jsReplyRef = nullptr, .callback = nullptr, .env = env, + .traceVaue = "", + .traceId = 0, }; + IPCObjectProxy *targetProxy = reinterpret_cast(target.GetRefPtr()); + if (targetProxy != nullptr) { + std::u16string remoteDescriptor = targetProxy->GetInterfaceDescriptor(); + if (sprintf_s(sendRequestParam->traceVaue, sizeof(sendRequestParam->traceVaue), "%s:%d", + Str16ToStr8(remoteDescriptor).c_str(), code) > 0) { + sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); + RpcStartAsyncTrace(sendRequestParam->traceVaue, sendRequestParam->traceId); + } + } napi_create_reference(env, argv[0], 1, &sendRequestParam->jsCodeRef); napi_create_reference(env, argv[1], 1, &sendRequestParam->jsDataRef); napi_create_reference(env, argv[2], 1, &sendRequestParam->jsReplyRef); @@ -1459,7 +1501,6 @@ napi_value SendRequestAsync(napi_env env, sptr target, uint32_t c NAPI_CALL(env, napi_create_async_work(env, nullptr, resourceName, ExecuteSendRequest, SendRequestCbComplete, (void *)sendRequestParam, &sendRequestParam->asyncWork)); NAPI_CALL(env, napi_queue_async_work(env, sendRequestParam->asyncWork)); - napi_value result = nullptr; napi_get_undefined(env, &result); return result; } @@ -1485,7 +1526,18 @@ napi_value SendRequestPromise(napi_env env, sptr target, uint32_t .jsReplyRef = nullptr, .callback = nullptr, .env = env, + .traceVaue = "", + .traceId = 0, }; + IPCObjectProxy *targetProxy = reinterpret_cast(target.GetRefPtr()); + if (targetProxy != nullptr) { + std::u16string remoteDescriptor = targetProxy->GetInterfaceDescriptor(); + if (sprintf_s(sendRequestParam->traceVaue, sizeof(sendRequestParam->traceVaue), "%s:%d", + Str16ToStr8(remoteDescriptor).c_str(), code) > 0) { + sendRequestParam->traceId = bytraceId.fetch_add(1, std::memory_order_seq_cst); + RpcStartAsyncTrace(sendRequestParam->traceVaue, sendRequestParam->traceId); + } + } napi_create_reference(env, argv[0], 1, &sendRequestParam->jsCodeRef); napi_create_reference(env, argv[1], 1, &sendRequestParam->jsDataRef); napi_create_reference(env, argv[2], 1, &sendRequestParam->jsReplyRef); @@ -1533,7 +1585,6 @@ napi_value NAPI_RemoteProxy_sendRequest(napi_env env, napi_callback_info info) NAPI_ASSERT(env, proxyHolder != nullptr, "failed to get proxy holder"); sptr target = proxyHolder->object_; NAPI_ASSERT(env, target != nullptr, "invalid proxy object"); - if (argc == argcCallback) { napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr); napi_valuetype valuetype = napi_undefined; -- GitLab