diff --git a/adapter/uhdf2/ipc/BUILD.gn b/adapter/uhdf2/ipc/BUILD.gn index 302894090e5779db7df90df4fca3f2e691795a3e..f88c06bed8080140953ebad101bf8f91995621ef 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 46868ec40692c28930a447ae761e01ac13236bfd..0d8eb11a95dceb18b5fd38ff4c61498245a52b00 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::DEFAULT_TIMEOUT_SECONDS, nullptr, nullptr, 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::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++); + 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 c0b61f9241c6a62c795d28a631776e734b2675de..da7803e2c730b13c3987b99ae5ae40f74dc28f45 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 18c8b49280483ffac1cb9c835cae0dec53566af1..92db9f3d9fa0b9e9a3c3a0fc5cf0ce2832a336bd 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 = false + 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 8d3c488fbb04094b8a69d82828d285c8701c359c..a2f95faa4b3298ec32e60e2f9ae5ba96391bc18c 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 0000000000000000000000000000000000000000..d6281fae00a799a57175f1c0cfe78a8ff7fcbfaa --- /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 0000000000000000000000000000000000000000..5f362ea6ce2f288f4537367199c12bd7efa6561b --- /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 60a1d39495651e8d7c5bc27f12f2a5a1082ed596..c177d5d52a7704b250ca7d1032dc1ed83bbc983f 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"