From 106cad4a0c72abcb6c1b47658cb9ba9493c4ee4f Mon Sep 17 00:00:00 2001 From: ganchuantao Date: Sun, 7 Apr 2024 15:53:46 +0800 Subject: [PATCH 1/3] =?UTF-8?q?HiTraceChain=20=E6=8E=A5=E5=8F=A3=E9=80=82?= =?UTF-8?q?=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ganchuantao Change-Id: I43928657fc8559a34147b730d8f722ade69a2f1a --- BUILD.gn | 3 +++ src/threadpool.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) diff --git a/BUILD.gn b/BUILD.gn index f0c8acc..87da16f 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -187,6 +187,7 @@ if (defined(ohos_lite)) { defines += [ "PRINT_ERRNO_ABORT" ] } + defines += [ "ENABLE_HITRACE" ] if (enable_uv_statisic && is_ohos) { defines += [ "UV_STATISTIC" ] cflags += @@ -288,6 +289,7 @@ if (defined(ohos_lite)) { "src/unix/random-sysctl-linux.c", "src/unix/proctitle.c", ] + external_deps = [] if (use_ffrt) { external_deps += [ "ffrt:libffrt" ] } @@ -310,6 +312,7 @@ if (defined(ohos_lite)) { "src/win/trace_win.c", ] } + external_deps += [ "hitrace:libhitracechain" ] } else if (is_linux) { sources += nonwin_srcs + [ "src/unix/linux-core.c", diff --git a/src/threadpool.c b/src/threadpool.c index 25b9d19..d17494d 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -36,6 +36,8 @@ #endif #include +#include "hitrace/trace.h" + #define MAX_THREADPOOL_SIZE 1024 #define TASK_NUMBER_WARNING 50 #define UV_TRACE_NAME "UV_TRACE" @@ -724,7 +726,22 @@ void uv__work_done(uv_async_t* handle) { static void uv__queue_work(struct uv__work* w) { uv_work_t* req = container_of(w, uv_work_t, work_req); +#ifdef ENABLE_HITRACE + HiTraceIdStruct currentId = HiTraceChainGetId(); + if (req->reserved[1] != NULL) { + HiTraceChainSaveAndSetId((HiTraceIdStruct*)(req->reserved[1])); + HiTraceChainTracepoint(HITRACE_TP_SR, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv__queue_work"); + } +#endif + req->work_cb(req); + +#ifdef ENABLE_HITRACE + if (req->reserved[1] != NULL) { + HiTraceChainTracepoint(HITRACE_TP_SS, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv__queue_work"); + HiTraceChainRestoreId(¤tId); + } +#endif } @@ -737,7 +754,20 @@ static void uv__queue_done(struct uv__work* w, int err) { if (req->after_work_cb == NULL) return; +#ifdef ENABLE_HITRACE + HiTraceIdStruct currentId = HiTraceChainGetId(); + if (req->reserved[1] != NULL) { + HiTraceChainSaveAndSetId((HiTraceIdStruct*)(req->reserved[1])); + } +#endif + req->after_work_cb(req, err); + +#ifdef ENABLE_HITRACE + if (req->reserved[1] != NULL) { + HiTraceChainRestoreId(¤tId); + } +#endif } @@ -884,6 +914,17 @@ int uv_queue_work(uv_loop_t* loop, #ifdef ASYNC_STACKTRACE req->reserved[3] = (void*)CollectAsyncStack(); #endif + +#ifdef ENABLE_HITRACE + HiTraceIdStruct traceId = HiTraceChainGetId(); + req->reserved[1] = NULL; + if (traceId.valid == HITRACE_ID_VALID) { + req->reserved[1] = (HiTraceIdStruct*)malloc(sizeof(HiTraceIdStruct)); + *((HiTraceIdStruct*)(req->reserved[1])) = HiTraceChainCreateSpan(); + HiTraceChainTracepoint(HITRACE_TP_CS, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv_queue_work"); + } +#endif + uv__work_submit(loop, #ifdef USE_FFRT (uv_req_t*)req, @@ -896,6 +937,12 @@ int uv_queue_work(uv_loop_t* loop, #ifdef UV_STATISTIC uv_queue_statics(info); #endif + +#ifdef ENABLE_HITRACE + if (traceId.valid == HITRACE_ID_VALID && req->reserved[1] != NULL) { + HiTraceChainTracepoint(HITRACE_TP_CR, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv_queue_work"); + } +#endif return 0; } @@ -932,6 +979,17 @@ int uv_queue_work_with_qos(uv_loop_t* loop, info->builtin_return_address[2] = __builtin_return_address(2); (req->work_req).info = info; #endif + +#ifdef ENABLE_HITRACE + HiTraceIdStruct traceId = HiTraceChainGetId(); + req->reserved[1] = NULL; + if (traceId.valid == HITRACE_ID_VALID) { + req->reserved[1] = (HiTraceIdStruct*)malloc(sizeof(HiTraceIdStruct)); + *((HiTraceIdStruct*)(req->reserved[1])) = HiTraceChainCreateSpan(); + HiTraceChainTracepoint(HITRACE_TP_CS, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv_queue_work_with_qos"); + } +#endif + uv__work_submit_with_qos(loop, (uv_req_t*)req, &req->work_req, @@ -941,6 +999,13 @@ int uv_queue_work_with_qos(uv_loop_t* loop, #ifdef UV_STATISTIC uv_queue_statics(info); #endif + +#ifdef ENABLE_HITRACE + if (traceId.valid == HITRACE_ID_VALID && req->reserved[1] != NULL) { + HiTraceChainTracepoint(HITRACE_TP_CR, (HiTraceIdStruct*)(req->reserved[1]), "libuv::uv_queue_work_with_qos"); + } +#endif + return 0; #else return uv_queue_work(loop, req, work_cb, after_work_cb); -- Gitee From 2ecfa5fc3a6536e84e21901d560f75b7dcd4b08d Mon Sep 17 00:00:00 2001 From: ganchuantao Date: Tue, 9 Apr 2024 02:14:35 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ganchuantao --- BUILD.gn | 7 +++++-- src/threadpool.c | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/BUILD.gn b/BUILD.gn index 87da16f..f5bd10b 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -187,7 +187,10 @@ if (defined(ohos_lite)) { defines += [ "PRINT_ERRNO_ABORT" ] } - defines += [ "ENABLE_HITRACE" ] + if (is_ohos) { + defines += [ "ENABLE_HITRACE" ] + } + if (enable_uv_statisic && is_ohos) { defines += [ "UV_STATISTIC" ] cflags += @@ -300,6 +303,7 @@ if (defined(ohos_lite)) { ] external_deps += [ "hilog:libhilog", + "hitrace:libhitracechain", "hitrace:hitrace_meter", ] } @@ -312,7 +316,6 @@ if (defined(ohos_lite)) { "src/win/trace_win.c", ] } - external_deps += [ "hitrace:libhitracechain" ] } else if (is_linux) { sources += nonwin_srcs + [ "src/unix/linux-core.c", diff --git a/src/threadpool.c b/src/threadpool.c index d17494d..0f571a1 100644 --- a/src/threadpool.c +++ b/src/threadpool.c @@ -36,7 +36,9 @@ #endif #include +#ifdef ENABLE_HITRACE #include "hitrace/trace.h" +#endif #define MAX_THREADPOOL_SIZE 1024 #define TASK_NUMBER_WARNING 50 -- Gitee From 0d1784d7376e77606fb49025dc61386fa45c1a3e Mon Sep 17 00:00:00 2001 From: ganchuantao Date: Tue, 9 Apr 2024 09:31:12 +0000 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E5=8C=96=E6=A3=80=E6=9F=A5=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ganchuantao --- BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BUILD.gn b/BUILD.gn index f5bd10b..a63f85a 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -303,8 +303,8 @@ if (defined(ohos_lite)) { ] external_deps += [ "hilog:libhilog", - "hitrace:libhitracechain", "hitrace:hitrace_meter", + "hitrace:libhitracechain", ] } if (is_ohos && enable_async_stack) { -- Gitee