From f5aa8eaf3ec5bdf053abe2e06b23af0247524663 Mon Sep 17 00:00:00 2001 From: wufarong Date: Tue, 10 Jun 2025 19:31:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E6=B5=81=E7=A8=8B=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0hitraceid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wufarong Change-Id: I91b02e258e0c56516cc8f68f8426d085b2dfc880 --- frameworks/ans/BUILD.gn | 2 + frameworks/core/BUILD.gn | 2 + frameworks/core/include/hitrace_util.h | 33 ++++++++++++++ frameworks/core/src/hitrace_util.cpp | 43 +++++++++++++++++++ frameworks/js/napi/BUILD.gn | 1 + frameworks/js/napi/src/manager/BUILD.gn | 1 + .../js/napi/src/manager/napi_publish.cpp | 3 ++ frameworks/js/napi/src/subscribe.cpp | 15 ++++++- frameworks/js/napi/src/subscribe/BUILD.gn | 1 + services/ans/BUILD.gn | 1 + .../include/advanced_notification_service.h | 2 + .../ans/include/notification_analytics_util.h | 2 + .../advanced_notification_publish.cpp | 20 +++++++++ .../common/notification_analytics_util.cpp | 13 ++++++ 14 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 frameworks/core/include/hitrace_util.h create mode 100644 frameworks/core/src/hitrace_util.cpp diff --git a/frameworks/ans/BUILD.gn b/frameworks/ans/BUILD.gn index 6a7fcd5cf..6e14f2b9e 100644 --- a/frameworks/ans/BUILD.gn +++ b/frameworks/ans/BUILD.gn @@ -76,6 +76,7 @@ ohos_shared_library("ans_innerkits") { "${core_path}/src/ans_image_util.cpp", "${core_path}/src/ans_notification.cpp", "${core_path}/src/dialog_status_data.cpp", + "${core_path}/src/hitrace_util.cpp", "${core_path}/src/listener/ans_subscriber_listener.cpp", "${core_path}/src/listener/swing_callback_service.cpp", "${core_path}/src/manager/ans_manager_death_recipient.cpp", @@ -162,6 +163,7 @@ ohos_shared_library("ans_innerkits") { "eventhandler:libeventhandler", "hicollie:libhicollie", "hilog:libhilog", + "hitrace:libhitracechain", "i18n:intl_util", "ipc:ipc_core", "samgr:samgr_proxy", diff --git a/frameworks/core/BUILD.gn b/frameworks/core/BUILD.gn index c90cde0df..792ee7b2c 100644 --- a/frameworks/core/BUILD.gn +++ b/frameworks/core/BUILD.gn @@ -30,6 +30,7 @@ ohos_shared_library("ans_core") { "${core_path}/src/ans_notification.cpp", "${core_path}/src/ans_subscriber_proxy.cpp", "${core_path}/src/ans_subscriber_stub.cpp", + "${core_path}/src/hitrace_util.cpp", "${frameworks_module_ans_path}/src/badge_number_callback_data.cpp", "${frameworks_module_ans_path}/src/enabled_notification_callback_data.cpp", "${frameworks_module_ans_path}/src/message_user.cpp", @@ -81,6 +82,7 @@ ohos_shared_library("ans_core") { "hicollie:libhicollie", "hilog:libhilog", "hitrace:hitrace_meter", + "hitrace:libhitracechain", "i18n:intl_util", "image_framework:image_native", "ipc:ipc_core", diff --git a/frameworks/core/include/hitrace_util.h b/frameworks/core/include/hitrace_util.h new file mode 100644 index 000000000..a0d0494a8 --- /dev/null +++ b/frameworks/core/include/hitrace_util.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2025 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 BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_HITRACE_UTIL_H +#define BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_HITRACE_UTIL_H + +#include "hitrace/hitracechain.h" + +namespace OHOS { + namespace Notification { + class TraceChainUtil { + public: + TraceChainUtil(); + ~TraceChainUtil(); + + private: + OHOS::HiviewDFX::HiTraceId traceId; + }; + +} // namespace Notification +} // namespace OHOS +#endif // BASE_NOTIFICATION_ANS_STANDARD_FRAMEWORKS_ANS_CORE_INCLUDE_HITRACE_UTIL_H \ No newline at end of file diff --git a/frameworks/core/src/hitrace_util.cpp b/frameworks/core/src/hitrace_util.cpp new file mode 100644 index 000000000..3828bbe13 --- /dev/null +++ b/frameworks/core/src/hitrace_util.cpp @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2025 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 "hitrace_util.h" + +#include "hitrace/hitracechain.h" + +namespace OHOS { +namespace Notification { +static const std::string TRACE_CHAIN_NAME = "distributed_notification_service"; + +TraceChainUtil::TraceChainUtil() +{ + auto oldTraceId = OHOS::HiviewDFX::HiTraceChain::GetId(); + if (oldTraceId.IsValid()) { + return; + } + traceId = OHOS::HiviewDFX::HiTraceChain::Begin(TRACE_CHAIN_NAME, HiTraceFlag::HITRACE_FLAG_INCLUDE_ASYNC | + HiTraceFlag::HITRACE_FLAG_NO_BE_INFO | + HiTraceFlag::HITRACE_FLAG_DONOT_CREATE_SPAN); +} + +TraceChainUtil::~TraceChainUtil() +{ + if (traceId.IsValid()) { + OHOS::HiviewDFX::HiTraceChain::End(traceId); + } +} + +} // namespace Notification +} // namespace OHOS \ No newline at end of file diff --git a/frameworks/js/napi/BUILD.gn b/frameworks/js/napi/BUILD.gn index 08cd1d7ca..7882efe71 100644 --- a/frameworks/js/napi/BUILD.gn +++ b/frameworks/js/napi/BUILD.gn @@ -128,6 +128,7 @@ ohos_shared_library("notification") { "ability_runtime:wantagent_innerkits", "c_utils:utils", "hilog:libhilog", + "hitrace:libhitracechain", "image_framework:image", "json:nlohmann_json_static", "libuv:uv", diff --git a/frameworks/js/napi/src/manager/BUILD.gn b/frameworks/js/napi/src/manager/BUILD.gn index 3444b0cec..fad2ff86d 100644 --- a/frameworks/js/napi/src/manager/BUILD.gn +++ b/frameworks/js/napi/src/manager/BUILD.gn @@ -115,6 +115,7 @@ ohos_shared_library("notificationmanager") { "ace_engine:ace_uicontent", "c_utils:utils", "hilog:libhilog", + "hitrace:libhitracechain", "image_framework:image", "ipc:ipc_single", "napi:ace_napi", diff --git a/frameworks/js/napi/src/manager/napi_publish.cpp b/frameworks/js/napi/src/manager/napi_publish.cpp index 6d26825a8..9f49651aa 100644 --- a/frameworks/js/napi/src/manager/napi_publish.cpp +++ b/frameworks/js/napi/src/manager/napi_publish.cpp @@ -17,6 +17,7 @@ #include "ans_inner_errors.h" #include "publish.h" +#include "hitrace_util.h" namespace OHOS { namespace NotificationNapi { @@ -24,6 +25,7 @@ namespace NotificationNapi { napi_value NapiPublish(napi_env env, napi_callback_info info) { ANS_LOGD("enter"); + TraceChainUtil traceChain = TraceChainUtil(); ParametersInfoPublish params; if (ParseParameters(env, info, params) == nullptr) { Common::NapiThrow(env, ERROR_PARAM_INVALID); @@ -149,6 +151,7 @@ napi_value NapiShowNotification(napi_env env, napi_callback_info info) napi_value NapiPublishAsBundle(napi_env env, napi_callback_info info) { ANS_LOGD("enter"); + TraceChainUtil traceChain = TraceChainUtil(); ParametersInfoPublish params; if (ParsePublishAsBundleParameters(env, info, params) == nullptr) { Common::NapiThrow(env, ERROR_PARAM_INVALID); diff --git a/frameworks/js/napi/src/subscribe.cpp b/frameworks/js/napi/src/subscribe.cpp index 685d2c7d1..0c822a57b 100644 --- a/frameworks/js/napi/src/subscribe.cpp +++ b/frameworks/js/napi/src/subscribe.cpp @@ -17,6 +17,7 @@ #include "ans_inner_errors.h" #include #include +#include "hitrace_util.h" namespace OHOS { namespace NotificationNapi { @@ -351,9 +352,19 @@ bool SubscriberInstance::HasOnBatchCancelCallback() void ThreadSafeOnConsumed(napi_env env, napi_value jsCallback, void* context, void* data) { - ANS_LOGI("OnConsumed thread safe start"); - auto dataWorkerData = reinterpret_cast(data); + auto additionalData = dataWorkerData->request->GetNotificationRequest().GetAdditionalData(); + if (additionalData && additionalData->HasParam("_oh_ans_sys_traceid")) { + std::stringstream sin(additionalData->GetStringParam("_oh_ans_sys_traceid")); + uint64_t chainId; + if (sin >> std::hex >> chainId) { + TraceChainUtil traceChainUtil = TraceChainUtil(); + OHOS::HiviewDFX::HiTraceId traceId = OHOS::HiviewDFX::HiTraceChain::GetId(); + traceId.SetChainId(chainId); + OHOS::HiviewDFX::HiTraceChain::SetId(traceId); + } + } + ANS_LOGI("OnConsumed thread safe start"); if (dataWorkerData == nullptr) { ANS_LOGE("dataWorkerData is null."); return; diff --git a/frameworks/js/napi/src/subscribe/BUILD.gn b/frameworks/js/napi/src/subscribe/BUILD.gn index e7f3b096c..8e2b91967 100644 --- a/frameworks/js/napi/src/subscribe/BUILD.gn +++ b/frameworks/js/napi/src/subscribe/BUILD.gn @@ -82,6 +82,7 @@ ohos_shared_library("notificationsubscribe") { "ability_runtime:wantagent_innerkits", "c_utils:utils", "hilog:libhilog", + "hitrace:libhitracechain", "image_framework:image", "ipc:ipc_single", "libuv:uv", diff --git a/services/ans/BUILD.gn b/services/ans/BUILD.gn index 87945431e..0f2947f8a 100644 --- a/services/ans/BUILD.gn +++ b/services/ans/BUILD.gn @@ -184,6 +184,7 @@ ohos_source_set("ans_service_sources") { "data_share:datashare_permission", "device_manager:devicemanagersdk", "ffrt:libffrt", + "hitrace:libhitracechain", "i18n:intl_util", "image_framework:image_native", "init:libbegetutil", diff --git a/services/ans/include/advanced_notification_service.h b/services/ans/include/advanced_notification_service.h index ef1bd9972..1263310fc 100644 --- a/services/ans/include/advanced_notification_service.h +++ b/services/ans/include/advanced_notification_service.h @@ -1669,6 +1669,8 @@ private: return true; } + void SetChainIdToExtraInfo(const sptr &request, OHOS::HiviewDFX::HiTraceId traceId); + private: static sptr instance_; static std::mutex instanceMutex_; diff --git a/services/ans/include/notification_analytics_util.h b/services/ans/include/notification_analytics_util.h index a7cce3a7b..5da7a4021 100644 --- a/services/ans/include/notification_analytics_util.h +++ b/services/ans/include/notification_analytics_util.h @@ -292,6 +292,8 @@ private: static void AddLocalLiveViewFailedNum(std::string bundle); static void MakeRequestBundle(const sptr& request); + + static std::string GetTraceIdStr(); }; } // namespace Notification } // namespace OHOS diff --git a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp index f1bc5ce08..6ae602d31 100644 --- a/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp +++ b/services/ans/src/advanced_notification_manager/advanced_notification_publish.cpp @@ -28,6 +28,8 @@ #include "notification_analytics_util.h" #include "os_account_manager.h" #include "os_account_manager_helper.h" +#include "string_wrapper.h" +#include "hitrace_util.h" namespace OHOS { namespace Notification { @@ -46,6 +48,8 @@ ErrCode AdvancedNotificationService::PublishWithMaxCapacity( ErrCode AdvancedNotificationService::Publish(const std::string &label, const sptr &request) { HITRACE_METER_NAME(HITRACE_TAG_NOTIFICATION, __PRETTY_FUNCTION__); + TraceChainUtil traceChain = TraceChainUtil(); + OHOS::HiviewDFX::HiTraceId traceId = OHOS::HiviewDFX::HiTraceChain::GetId(); ANS_LOGD("%{public}s", __FUNCTION__); if (!request) { @@ -53,6 +57,7 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt return ERR_ANS_INVALID_PARAM; } + SetChainIdToExtraInfo(request, traceId); if (request->GetDistributedCollaborate()) { return CollaboratePublish(request); } @@ -133,6 +138,21 @@ ErrCode AdvancedNotificationService::Publish(const std::string &label, const spt return result; } +void AdvancedNotificationService::SetChainIdToExtraInfo + (const sptr &request, OHOS::HiviewDFX::HiTraceId traceId) +{ + std::shared_ptr additionalData = request->GetAdditionalData(); + if (!additionalData) { + additionalData = std::make_shared(); + } + std::stringstream chainId; + chainId << std::hex << traceId.GetChainId(); + std::string hexTransId; + chainId >> std::hex >> hexTransId; + additionalData->SetParam("_oh_ans_sys_traceid", AAFwk::String::Box(hexTransId)); + request->SetAdditionalData(additionalData); +} + ErrCode AdvancedNotificationService::PublishNotificationForIndirectProxyWithMaxCapacity( const sptr& request) { diff --git a/services/ans/src/common/notification_analytics_util.cpp b/services/ans/src/common/notification_analytics_util.cpp index d0f9dc245..160a5cb00 100644 --- a/services/ans/src/common/notification_analytics_util.cpp +++ b/services/ans/src/common/notification_analytics_util.cpp @@ -32,6 +32,7 @@ #include "os_account_manager_helper.h" #include "notification_constant.h" #include "advanced_notification_inline.h" +#include "hitrace_util.h" namespace OHOS { namespace Notification { @@ -626,6 +627,7 @@ std::string NotificationAnalyticsUtil::BuildAnsData(const sptr( std::chrono::system_clock::now().time_since_epoch()).count(); ansData["time"] = now; + ansData["traceId"] = GetTraceIdStr(); ANS_LOGI("Ansdata built, the controlFlags is %{public}d, deviceStatus is %{public}s", controlFlags, GetDeviceStatus(request).c_str()); return ansData.dump(-1, ' ', false, nlohmann::json::error_handler_t::replace); @@ -921,6 +923,7 @@ std::string NotificationAnalyticsUtil::BuildExtraInfoWithReq(const HaMetaMessage std::chrono::system_clock::now().time_since_epoch()).count(); reason["time"] = now; + reason["traceId"] = GetTraceIdStr(); std::shared_ptr extraInfo = nullptr; if (request->GetUnifiedGroupInfo() != nullptr && request->GetUnifiedGroupInfo()->GetExtraInfo() != nullptr) { @@ -952,6 +955,16 @@ std::string NotificationAnalyticsUtil::BuildExtraInfoWithReq(const HaMetaMessage return wWrapper.ToString(); } +std::string NotificationAnalyticsUtil::GetTraceIdStr() +{ + OHOS::HiviewDFX::HiTraceId traceId = OHOS::HiviewDFX::HiTraceChain::GetId(); + std::stringstream chainId; + chainId << std::hex << traceId.GetChainId(); + std::string hexTransId; + chainId >> std::hex >> hexTransId; + return hexTransId; +} + void NotificationAnalyticsUtil::SetCommonWant(EventFwk::Want& want, const HaMetaMessage& message, std::string& extraInfo) { -- Gitee