From 681cf0332bf56aadda322bc4ecd8201776e0b50f Mon Sep 17 00:00:00 2001 From: chenbushan Date: Sat, 13 Apr 2024 17:09:11 +0800 Subject: [PATCH 1/3] bugfix : add unitest for hdf_remote_service.c Signed-off-by: chenbushan Change-Id: I32ca1dbb2da7ebbdbfd4d268929c8695a2537552 --- adapter/uhdf2/ipc/BUILD.gn | 1 + adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp | 44 +++++++++------- adapter/uhdf2/pub_utils/BUILD.gn | 12 ++++- adapter/uhdf2/uhdf.gni | 7 +++ adapter/uhdf2/utils/BUILD.gn | 1 - adapter/uhdf2/utils/include/hdf_xcollie.h | 38 ++++++++++++++ adapter/uhdf2/utils/src/hdf_xcollie.cpp | 54 ++++++++++++++++++++ bundle.json | 3 +- 8 files changed, 139 insertions(+), 21 deletions(-) create mode 100644 adapter/uhdf2/utils/include/hdf_xcollie.h create mode 100644 adapter/uhdf2/utils/src/hdf_xcollie.cpp diff --git a/adapter/uhdf2/ipc/BUILD.gn b/adapter/uhdf2/ipc/BUILD.gn index 302894090..f88c06bed 100644 --- a/adapter/uhdf2/ipc/BUILD.gn +++ b/adapter/uhdf2/ipc/BUILD.gn @@ -23,6 +23,7 @@ config("libhdf_ipc_adapter_pub_config") { include_dirs = [ "$hdf_interface_path/inner_api/ipc", "$hdf_interface_path/inner_api/core", + "$hdf_uhdf_path/utils/include", ] } if (defined(ohos_lite)) { diff --git a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp index 46868ec40..d565d76b6 100644 --- a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp +++ b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp @@ -25,6 +25,7 @@ #include "hdf_object_manager.h" #include "hdf_sbuf_ipc.h" #include "hdf_remote_adapter.h" +#include "hdf_xcollie.h" #define HDF_LOG_TAG hdf_remote_adapter @@ -268,27 +269,34 @@ int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service) if (service == nullptr) { return HDF_ERR_INVALID_PARAM; } - - auto saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - const int32_t waitTimes = 50; - const int32_t sleepInterval = 20000; - int32_t timeout = waitTimes; - while (saManager == nullptr && (timeout > 0)) { - HDF_LOGI("waiting for samgr..."); - usleep(sleepInterval); + OHOS::sptr saManager; + { + OHOS::HdfXCollie hdfXCollie("HdfRemoteAdapterAddSa_" + OHOS::ToString(saId) + "_get_samgr", + OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - timeout--; - } + const int32_t waitTimes = 50; + const int32_t sleepInterval = 20000; + int32_t timeout = waitTimes; + while (saManager == nullptr && (timeout > 0)) { + HDF_LOGI("waiting for samgr..."); + usleep(sleepInterval); + saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + timeout--; + } - if (saManager == nullptr) { - HDF_LOGE("failed to get sa manager, waiting timeot"); - return HDF_FAILURE; + if (saManager == nullptr) { + HDF_LOGE("failed to get sa manager, waiting timeot"); + return HDF_FAILURE; + } + } + { + OHOS::HdfXCollie hdfXCollie("HdfRemoteAdapterAddSa_" + OHOS::ToString(saId) + "_add_sa", + OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); + struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); + int ret = saManager->AddSystemAbility(saId, holder->remote_); + (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); + HDF_LOGI("add sa %{public}d, ret = %{public}s", saId, (ret == 0) ? "succ" : "fail"); } - struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); - int ret = saManager->AddSystemAbility(saId, holder->remote_); - (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); - HDF_LOGI("add sa %{public}d, ret = %{public}s", saId, (ret == 0) ? "succ" : "fail"); - return HDF_SUCCESS; } diff --git a/adapter/uhdf2/pub_utils/BUILD.gn b/adapter/uhdf2/pub_utils/BUILD.gn index c0b61f924..da7803e2c 100644 --- a/adapter/uhdf2/pub_utils/BUILD.gn +++ b/adapter/uhdf2/pub_utils/BUILD.gn @@ -33,7 +33,10 @@ if (defined(ohos_lite)) { } } else { ohos_shared_library("libpub_utils") { - include_dirs = [ "$hdf_framework_path/utils/include" ] + include_dirs = [ + "$hdf_framework_path/utils/include", + "$hdf_uhdf_path/utils/include", + ] public_configs = [ ":lib_utils_pub_config" ] sources = [ "$hdf_framework_path/support/posix/src/osal_mem.c", @@ -41,12 +44,19 @@ if (defined(ohos_lite)) { "$hdf_framework_path/utils/src/hdf_cstring.c", "$hdf_framework_path/utils/src/hdf_sbuf.c", "$hdf_framework_path/utils/src/hdf_sbuf_impl_raw.c", + "$hdf_uhdf_path/utils/src/hdf_xcollie.cpp", ] external_deps = [ "c_utils:utils", "hilog:libhilog", ] + defines = [] + if (hicollie_enabled) { + external_deps += [ "hicollie:libhicollie" ] + defines += [ "HICOLLIE_ENABLE" ] + } + install_images = [ system_base_dir, "updater", diff --git a/adapter/uhdf2/uhdf.gni b/adapter/uhdf2/uhdf.gni index 18c8b4928..a877b1b01 100644 --- a/adapter/uhdf2/uhdf.gni +++ b/adapter/uhdf2/uhdf.gni @@ -13,3 +13,10 @@ hdf_framework_path = "//drivers/hdf_core/framework" hdf_uhdf_path = "//drivers/hdf_core/adapter/uhdf2" +declare_args() { + hicollie_enabled = true + if (defined(global_parts_info) && + !defined(global_parts_info.hiviewdfx_hicollie)) { + hicollie_enabled = false + } +} diff --git a/adapter/uhdf2/utils/BUILD.gn b/adapter/uhdf2/utils/BUILD.gn index 8d3c488fb..a2f95faa4 100644 --- a/adapter/uhdf2/utils/BUILD.gn +++ b/adapter/uhdf2/utils/BUILD.gn @@ -119,7 +119,6 @@ if (defined(ohos_lite)) { "$hdf_framework_path/core/shared/include", "$hdf_framework_path/include/core/", "$hdf_uhdf_path/osal/include", - "$hdf_uhdf_path/utils/include", ] public_configs = [ ":libhdf_utils_public_config" ] sources = [ diff --git a/adapter/uhdf2/utils/include/hdf_xcollie.h b/adapter/uhdf2/utils/include/hdf_xcollie.h new file mode 100644 index 000000000..d6281fae0 --- /dev/null +++ b/adapter/uhdf2/utils/include/hdf_xcollie.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 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 HDF_UHDF_UTILS_INCLUDE_HDF_XCOLLIE_H +#define HDF_UHDF_UTILS_INCLUDE_HDF_XCOLLIE_H + +#include + +namespace OHOS { +class HdfXCollie { +public: + HdfXCollie(const std::string& tag, uint32_t timeoutSeconds = DEFAULT_TIMEOUT_SECONDS, + std::function func = nullptr, void* arg = nullptr, uint32_t flag = HDF_XCOLLIE_FLAG_LOG); + ~HdfXCollie(); + + void CancelHdfXCollie(); + static const uint32_t HDF_XCOLLIE_FLAG_LOG = 1; // generate log file + static const uint32_t HDF_XCOLLIE_FLAG_RECOVERY = 2; // die when timeout + static const uint32_t DEFAULT_TIMEOUT_SECONDS = 10; +private: + int32_t id_; + std::string tag_; + bool isCanceled_; +}; +} +#endif // HDF_UHDF_UTILS_INCLUDE_HDF_XCOLLIE_H \ No newline at end of file diff --git a/adapter/uhdf2/utils/src/hdf_xcollie.cpp b/adapter/uhdf2/utils/src/hdf_xcollie.cpp new file mode 100644 index 000000000..5f362ea6c --- /dev/null +++ b/adapter/uhdf2/utils/src/hdf_xcollie.cpp @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2024 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 "hdf_xcollie.h" +#include "hdf_log.h" + +#ifdef HICOLLIE_ENABLE +#include "xcollie/xcollie.h" +#endif + +namespace OHOS { +HdfXCollie::HdfXCollie(const std::string& tag, uint32_t timeoutSeconds, + std::function func, void* arg, uint32_t flag) +{ + tag_ = tag; +#ifdef HICOLLIE_ENABLE + id_ = HiviewDFX::XCollie::GetInstance().SetTimer(tag_, timeoutSeconds, func, arg, flag); +#else + id_ = -1; +#endif + isCanceled_ = false; + HDF_LOGD("start HdfXCollie, tag:%{public}s,timeout:%{public}u,flag:%{public}u,id:%{public}d", + tag_.c_str(), timeoutSeconds, flag, id_); +} + +HdfXCollie::~HdfXCollie() +{ + CancelHdfXCollie(); +} + +void HdfXCollie::CancelHdfXCollie() +{ + if (!isCanceled_) { +#ifdef HICOLLIE_ENABLE + HiviewDFX::XCollie::GetInstance().CancelTimer(id_); +#endif + isCanceled_ = true; + HDF_LOGD("cancel HdfXCollie, tag:%{public}s,id:%{public}d", tag_.c_str(), id_); + } +} +} diff --git a/bundle.json b/bundle.json index 60a1d3949..c177d5d52 100644 --- a/bundle.json +++ b/bundle.json @@ -29,7 +29,8 @@ "ipc", "samgr", "selinux_adapter", - "hilog_lite" + "hilog_lite", + "hicollie" ], "third_party": [ "bounds_checking_function" -- Gitee From e1473d3d10493b74346fba28a353d0ccf2b1afa7 Mon Sep 17 00:00:00 2001 From: chenbushan Date: Thu, 18 Apr 2024 03:33:16 +0000 Subject: [PATCH 2/3] update adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp. Signed-off-by: chenbushan --- adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp index d565d76b6..0d8eb11a9 100644 --- a/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp +++ b/adapter/uhdf2/ipc/src/hdf_remote_adapter.cpp @@ -272,7 +272,7 @@ int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service) OHOS::sptr saManager; { OHOS::HdfXCollie hdfXCollie("HdfRemoteAdapterAddSa_" + OHOS::ToString(saId) + "_get_samgr", - OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); + OHOS::HdfXCollie::DEFAULT_TIMEOUT_SECONDS, nullptr, nullptr, OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); saManager = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); const int32_t waitTimes = 50; const int32_t sleepInterval = 20000; @@ -291,7 +291,7 @@ int HdfRemoteAdapterAddSa(int32_t saId, struct HdfRemoteService *service) } { OHOS::HdfXCollie hdfXCollie("HdfRemoteAdapterAddSa_" + OHOS::ToString(saId) + "_add_sa", - OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); + OHOS::HdfXCollie::DEFAULT_TIMEOUT_SECONDS, nullptr, nullptr, OHOS::HdfXCollie::HDF_XCOLLIE_FLAG_RECOVERY); struct HdfRemoteServiceHolder *holder = reinterpret_cast(service); int ret = saManager->AddSystemAbility(saId, holder->remote_); (void)OHOS::IPCSkeleton::GetInstance().SetMaxWorkThreadNum(g_remoteThreadMax++); -- Gitee From 137d8d059615864502321156d98736c0b4c615a4 Mon Sep 17 00:00:00 2001 From: chenbushan Date: Thu, 18 Apr 2024 15:13:43 +0000 Subject: [PATCH 3/3] update adapter/uhdf2/uhdf.gni. Signed-off-by: chenbushan --- adapter/uhdf2/uhdf.gni | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adapter/uhdf2/uhdf.gni b/adapter/uhdf2/uhdf.gni index a877b1b01..92db9f3d9 100644 --- a/adapter/uhdf2/uhdf.gni +++ b/adapter/uhdf2/uhdf.gni @@ -14,7 +14,7 @@ hdf_framework_path = "//drivers/hdf_core/framework" hdf_uhdf_path = "//drivers/hdf_core/adapter/uhdf2" declare_args() { - hicollie_enabled = true + hicollie_enabled = false if (defined(global_parts_info) && !defined(global_parts_info.hiviewdfx_hicollie)) { hicollie_enabled = false -- Gitee