diff --git a/frameworks/core/include/common_event_constant.h b/frameworks/core/include/common_event_constant.h index b10a129d32f2cf6aceafc7e74123dbddfa9e5626..32095c0c92d23814087355ecde391362bab8d20f 100644 --- a/frameworks/core/include/common_event_constant.h +++ b/frameworks/core/include/common_event_constant.h @@ -28,6 +28,8 @@ constexpr int8_t SUBSCRIBE_USER_SYSTEM_END = 99; constexpr int8_t UNDEFINED_PID = -1; constexpr int8_t MAX_HISTORY_SIZE = 100; constexpr int16_t MAX_SUBSCRIBER_NUM_PER_EVENT = 255; +constexpr uint32_t DEFAULT_MAX_SUBSCRIBER_NUM_ALL_APP = 2000; +constexpr double WARNING_REPORT_PERCENTAGE = 0.8; enum DumpEventType { ALL, diff --git a/services/include/common_event_subscriber_manager.h b/services/include/common_event_subscriber_manager.h index 356a7bf6d8d735b2268a12f43a2269f1e53d7b04..7845297d9d263add0b59f1aaf88d4d640453736a 100644 --- a/services/include/common_event_subscriber_manager.h +++ b/services/include/common_event_subscriber_manager.h @@ -16,11 +16,15 @@ #ifndef FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H #define FOUNDATION_EVENT_CESFWK_SERVICES_INCLUDE_COMMON_EVENT_SUBSCRIBER_MANAGER_H +#include +#include + #include "common_event_constant.h" #include "common_event_record.h" #include "common_event_subscribe_info.h" #include "event_log_wrapper.h" #include "iremote_object.h" +#include "parameter.h" #include "singleton.h" namespace OHOS { @@ -201,6 +205,12 @@ private: void SendSubscriberExceedMaximumHiSysEvent(int32_t userId, const std::string &eventName, uint32_t subscriberNum); + bool CheckSubscriberCountReachedMaxinum(); + + std::vector> GetTopSubscriberCounts(size_t topNum = 10); + + void PrintSubscriberCounts(std::vector> vtSubscriberCounts); + private: std::mutex mutex_; sptr death_; @@ -208,6 +218,7 @@ private: std::vector subscribers_; std::map frozenEvents_; const time_t FREEZE_EVENT_TIMEOUT = 30; // How long we keep records. Unit: second + std::map subscriberCounts_; }; } // namespace EventFwk } // namespace OHOS diff --git a/services/src/common_event_subscriber_manager.cpp b/services/src/common_event_subscriber_manager.cpp index 93bbacff43162e0ec0836ea6ccebe8e652c82bba..5aa3db4211e2e32f0e4a299d939e51866e25d286 100644 --- a/services/src/common_event_subscriber_manager.cpp +++ b/services/src/common_event_subscriber_manager.cpp @@ -13,8 +13,13 @@ * limitations under the License. */ -#include "common_event_subscriber_manager.h" +#include +#include +#include +#include +#include "ability_manager_client.h" +#include "common_event_subscriber_manager.h" #include "event_log_wrapper.h" #include "event_report.h" #include "hitrace_meter_adapter.h" @@ -23,6 +28,7 @@ namespace OHOS { namespace EventFwk { constexpr int32_t LENGTH = 80; +constexpr int32_t SIGNAL_KILL = 9; static constexpr int32_t SUBSCRIBE_EVENT_MAX_NUM = 512; CommonEventSubscriberManager::CommonEventSubscriberManager() @@ -255,6 +261,27 @@ bool CommonEventSubscriberManager::InsertSubscriberRecordLocked( std::lock_guard lock(mutex_); + pid_t pid = record->eventRecordInfo.pid; + + if (CheckSubscriberCountReachedMaxinum()) { + std::vector> vtSubscriberCounts = GetTopSubscriberCounts(1); + pid_t killedPid = (*vtSubscriberCounts.begin()).first; + + AAFwk::ExitReason reason = { AAFwk::REASON_RESOURCE_CONTROL, "Kill Reason: CES Register exceed limit"}; + AAFwk::AbilityManagerClient::GetInstance()->RecordProcessExitReason(killedPid, reason); + + int32_t killRes = kill(killedPid, SIGNAL_KILL); + if (killRes < 0) { + EVENT_LOGE("kill pid=%{public}d which has the most subscribers failed", killedPid); + } else { + EVENT_LOGI("kill pid=%{public}d which has the most subscribers successfully", killedPid); + } + + if (pid == killedPid) { + return false; + } + } + for (auto event : events) { auto infoItem = eventSubscribers_.find(event); if (infoItem != eventSubscribers_.end()) { @@ -272,6 +299,7 @@ bool CommonEventSubscriberManager::InsertSubscriberRecordLocked( } subscribers_.emplace_back(record); + subscriberCounts_[pid]++; return true; } @@ -293,6 +321,8 @@ int CommonEventSubscriberManager::RemoveSubscriberRecordLocked(const sptreventSubscribeInfo->GetMatchingSkills().GetEvents(); EVENT_LOGI("Unsubscribe subscriberID: %{public}s", (*it)->eventRecordInfo.subId.c_str()); subscribers_.erase(it); + pid_t pid = (*it)->eventRecordInfo.pid; + subscriberCounts_[pid] > 1 ? subscriberCounts_[pid]-- : subscriberCounts_.erase(pid); break; } } @@ -544,5 +574,60 @@ void CommonEventSubscriberManager::SendSubscriberExceedMaximumHiSysEvent(int32_t eventInfo.subscriberNum = subscriberNum; EventReport::SendHiSysEvent(SUBSCRIBER_EXCEED_MAXIMUM, eventInfo); } + +bool CommonEventSubscriberManager::CheckSubscriberCountReachedMaxinum() +{ + uint32_t subscriberCount = subscribers_.size(); + const int buffSize = 128; + char param[buffSize] = {0}; + GetParameter("ces_max_subscriber_num", "", param, buffSize - 1); + uint32_t maxSubscriberNum = atoi(param) == 0 ? DEFAULT_MAX_SUBSCRIBER_NUM_ALL_APP : atoi(param); + if (subscriberCount == (uint32_t)(maxSubscriberNum * WARNING_REPORT_PERCENTAGE)) { + EVENT_LOGW("subscribers reaches the alarm threshold"); + PrintSubscriberCounts(GetTopSubscriberCounts()); + return false; + } + if (subscriberCount == maxSubscriberNum) { + EVENT_LOGE("subscribers reaches the maxinum"); + PrintSubscriberCounts(GetTopSubscriberCounts()); + return true; + } + return false; +} + +std::vector> CommonEventSubscriberManager::GetTopSubscriberCounts(size_t topNum) +{ + topNum = subscriberCounts_.size() < topNum ? subscriberCounts_.size() : topNum; + + std::vector> vtSubscriberCounts; + std::set pidSet; + for (size_t i = 0; i < topNum; i++) { + std::pair curPair; + for (auto it = subscriberCounts_.begin(); it != subscriberCounts_.end(); it++) { + pid_t pid = it->first; + uint32_t count = it->second; + if (pidSet.find(pid) != pidSet.end()) { + continue; + } + if (curPair.second < count) { + curPair = std::make_pair(pid, count); + } + } + pidSet.insert(curPair.first); + vtSubscriberCounts.push_back(curPair); + } + + return vtSubscriberCounts; +} + +void CommonEventSubscriberManager::PrintSubscriberCounts(std::vector> vtSubscriberCounts) +{ + EVENT_LOGI("Start to print top App by subscribers in descending order"); + int index = 1; + for (auto vtIt = vtSubscriberCounts.begin(); vtIt != vtSubscriberCounts.end(); vtIt++) { + EVENT_LOGI("top%{public}d pid=%{public}d subscribers=%{public}d", index, vtIt->first, vtIt->second); + index++; + } +} } // namespace EventFwk } // namespace OHOS \ No newline at end of file diff --git a/services/test/unittest/BUILD.gn b/services/test/unittest/BUILD.gn index 197db3278a4ecfb7e7be156110778d53e2d4abfd..e2688c389b9ad47bf8575a90418a8c0e277aeeef 100644 --- a/services/test/unittest/BUILD.gn +++ b/services/test/unittest/BUILD.gn @@ -59,6 +59,7 @@ ohos_unittest("common_event_publish_system_event_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -90,6 +91,7 @@ ohos_unittest("common_event_dump_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -121,6 +123,7 @@ ohos_unittest("common_event_freeze_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -152,6 +155,7 @@ ohos_unittest("common_event_freeze_unit_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -183,6 +187,7 @@ ohos_unittest("ability_manager_death_recipient_unit_test") { "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -211,6 +216,7 @@ ohos_unittest("bundle_manager_death_recipient_unit_test") { "bundle_framework:appexecfwk_core", "c_utils:utils", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -254,6 +260,7 @@ ohos_unittest("static_subscriber_manager_unit_test") { "hicollie:libhicollie", "hilog:libhilog", "hitrace:hitrace_meter", + "init:libbegetutil", "ipc:ipc_core", "kv_store:distributeddata_inner", ] @@ -286,6 +293,7 @@ ohos_unittest("static_subscriber_connection_unit_test") { "c_utils:utils", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -317,6 +325,7 @@ ohos_unittest("common_event_sticky_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -351,6 +360,7 @@ ohos_unittest("common_event_subscribe_unit_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -382,6 +392,7 @@ ohos_unittest("common_event_unsubscribe_unit_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -413,6 +424,7 @@ ohos_unittest("common_event_publish_ordered_event_unit_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -444,6 +456,7 @@ ohos_unittest("common_event_publish_permission_event_unit_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -534,6 +547,7 @@ ohos_unittest("common_event_manager_service_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -572,6 +586,7 @@ ohos_unittest("common_event_manager_service_ability_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -608,6 +623,7 @@ ohos_unittest("bundle_manager_helper_other_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -644,6 +660,7 @@ ohos_unittest("bundle_manager_helper_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -680,6 +697,7 @@ ohos_unittest("common_event_manager_service_branch_test") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -716,6 +734,7 @@ ohos_unittest("common_event_manager_service_new_branch_test") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -757,6 +776,7 @@ ohos_unittest("common_event_control_manager_branch_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -791,6 +811,7 @@ ohos_unittest("common_event_control_manager_test") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -819,6 +840,7 @@ ohos_unittest("common_event_subscriber_manager_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -883,6 +905,7 @@ ohos_unittest("ability_manager_helper_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] @@ -912,6 +935,7 @@ ohos_unittest("ordered_event_handler_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } @@ -940,6 +964,7 @@ ohos_unittest("subscriber_deach_recipient_test") { "c_utils:utils", "eventhandler:libeventhandler", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } diff --git a/test/systemtest/common/acts/actsCESCESpublishInfoTest/BUILD.gn b/test/systemtest/common/acts/actsCESCESpublishInfoTest/BUILD.gn index 6fcf06a55bf7db9466643c22a83db0059dd24065..efdf3c93a66ac774f11370ddff580ffe758c7668 100644 --- a/test/systemtest/common/acts/actsCESCESpublishInfoTest/BUILD.gn +++ b/test/systemtest/common/acts/actsCESCESpublishInfoTest/BUILD.gn @@ -51,6 +51,7 @@ ohos_systemtest("ActsCESCESpublishInfoTest") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } diff --git a/test/systemtest/common/acts/actsCESDataTest/BUILD.gn b/test/systemtest/common/acts/actsCESDataTest/BUILD.gn index 8c2846aed542f7958ba3885af2cce7a4d3d65fb6..b393c370fb13faab3cf781de0cc9125efa1c6ab0 100644 --- a/test/systemtest/common/acts/actsCESDataTest/BUILD.gn +++ b/test/systemtest/common/acts/actsCESDataTest/BUILD.gn @@ -47,6 +47,7 @@ ohos_systemtest("ActsCESDataTest") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", ] } diff --git a/test/systemtest/common/acts/actsCESManagertest/BUILD.gn b/test/systemtest/common/acts/actsCESManagertest/BUILD.gn index 511770b222e11bfb5a445cb40234b845516f3b07..ca6dd0e17905230cb831a17e8c82510be6b96dad 100644 --- a/test/systemtest/common/acts/actsCESManagertest/BUILD.gn +++ b/test/systemtest/common/acts/actsCESManagertest/BUILD.gn @@ -52,6 +52,7 @@ ohos_systemtest("ActsCESManagertest") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ] diff --git a/test/systemtest/common/ces/common_event_services_publish_ordered_system_test/BUILD.gn b/test/systemtest/common/ces/common_event_services_publish_ordered_system_test/BUILD.gn index 3662e8eb2c5eb9616aeb681c5beb1d2f4dec9984..2d01a1cec4e9e2d88cb54426e9d3b80d08e5e96f 100644 --- a/test/systemtest/common/ces/common_event_services_publish_ordered_system_test/BUILD.gn +++ b/test/systemtest/common/ces/common_event_services_publish_ordered_system_test/BUILD.gn @@ -48,6 +48,7 @@ ohos_systemtest("CESPublishOrderedEventSystmTest") { "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", + "init:libbegetutil", "ipc:ipc_core", "samgr:samgr_proxy", ]