From 18622e52b877b89ac6c0c9eab6e60be999d56107 Mon Sep 17 00:00:00 2001 From: hxf Date: Wed, 28 May 2025 20:14:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E4=BB=BB=E5=8A=A1=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ehisysevent=E6=89=93=E7=82=B9=E7=BB=B4=E6=B5=8B=20Signe?= =?UTF-8?q?d-off-by:=20hxf=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hisysevent.yaml | 18 +++ services/native/include/policy/cpu_policy.h | 11 +- .../native/include/policy/ipolicy_filter.h | 11 +- .../native/include/policy/memory_policy.h | 11 +- .../native/include/policy/power_mode_policy.h | 11 +- .../native/include/policy/thermal_policy.h | 11 +- services/native/include/work_conn_manager.h | 4 +- services/native/include/work_policy_manager.h | 4 +- .../native/src/conditions/screen_listener.cpp | 2 + services/native/src/policy/cpu_policy.cpp | 11 +- services/native/src/policy/memory_policy.cpp | 11 +- .../native/src/policy/power_mode_policy.cpp | 11 +- services/native/src/policy/thermal_policy.cpp | 11 +- services/native/src/work_conn_manager.cpp | 66 +++++----- services/native/src/work_policy_manager.cpp | 29 +++-- .../native/src/work_scheduler_service.cpp | 26 ++-- .../work_standby_state_change_callback.cpp | 11 +- services/test/BUILD.gn | 2 + services/test/src/policy/cpu_policy_test.cpp | 32 ++--- .../test/src/policy/memory_policy_test.cpp | 23 ++-- .../src/policy/power_mode_policy_test.cpp | 23 ++-- .../test/src/policy/thermal_policy_test.cpp | 23 ++-- .../test/src/work_policy_manager_test.cpp | 50 +++----- ...ork_standby_state_change_callback_test.cpp | 117 ++++++++++++++++++ services/test/src/work_status_test.cpp | 9 +- utils/native/BUILD.gn | 8 +- utils/native/include/work_sched_constants.h | 51 ++++---- .../include/work_sched_hisysevent_report.h | 56 +++++++++ .../native/include/work_sched_system_policy.h | 43 +++++++ .../src/work_sched_hisysevent_report.cpp | 78 ++++++++++++ 30 files changed, 521 insertions(+), 253 deletions(-) create mode 100644 services/test/src/work_standby_state_change_callback_test.cpp create mode 100644 utils/native/include/work_sched_hisysevent_report.h create mode 100644 utils/native/include/work_sched_system_policy.h create mode 100644 utils/native/src/work_sched_hisysevent_report.cpp diff --git a/hisysevent.yaml b/hisysevent.yaml index 707259c..0bec68a 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -49,3 +49,21 @@ WORK_STOP: WORKID: {type: STRING, desc: work id} REASON: {type: INT32, desc: work stop reason} DURATION: {type: INT32, desc: work running duration} + +DEEP_IDLE_STATE: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: deep idle state changed} + STATE: {type: INT32, desc: deep idle state} + +DEVICE_STANDBY_STATE: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: device standby state changed} + STATE: {type: INT32, desc: device standby state} + +SYSTEM_POLICY_LIMIT: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: system policy limit work start} + POLICY: {type: STRING, desc: work start delay reason} + +WORK_SCHEDULER_RUNTIME_EXCEPTION: + __BASE: {type: STATISTIC, level: MINOR, tag: PowerStats, desc: work scheduler service runtime exception} + MODULE_NAME: {type: STRING, desc: module name} + FUNC_NAME: {type: STRING, desc: function name} + EXCEPTION_INFO: {type: STRING, desc: exception info} diff --git a/services/native/include/policy/cpu_policy.h b/services/native/include/policy/cpu_policy.h index f2e66dc..de809f9 100644 --- a/services/native/include/policy/cpu_policy.h +++ b/services/native/include/policy/cpu_policy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -29,14 +29,7 @@ public: * * @return policyResult. */ - int32_t GetPolicyMaxRunning() override; - - /** - * @brief Get policyName. - * - * @return PolicyName. - */ - std::string GetPolicyName() override; + int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) override; private: int32_t GetCpuUsage(); std::shared_ptr workPolicyManager_; diff --git a/services/native/include/policy/ipolicy_filter.h b/services/native/include/policy/ipolicy_filter.h index c1944fa..ebb4b16 100644 --- a/services/native/include/policy/ipolicy_filter.h +++ b/services/native/include/policy/ipolicy_filter.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -16,6 +16,7 @@ #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_IPOLICY_FILTER_H #include +#include "work_sched_system_policy.h" namespace OHOS { namespace WorkScheduler { @@ -27,13 +28,7 @@ public: * * @return Res. */ - virtual int32_t GetPolicyMaxRunning(); - /** - * @brief Get policyName. - * - * @return PolicyName. - */ - virtual std::string GetPolicyName(); + virtual int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy); }; } // namespace WorkScheduler } // namespace OHOS diff --git a/services/native/include/policy/memory_policy.h b/services/native/include/policy/memory_policy.h index c9891e6..23eb300 100644 --- a/services/native/include/policy/memory_policy.h +++ b/services/native/include/policy/memory_policy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -31,14 +31,7 @@ public: * * @return Res. */ - int32_t GetPolicyMaxRunning() override; - - /** - * @brief Get policyName. - * - * @return PolicyName. - */ - std::string GetPolicyName() override; + int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) override; private: int32_t GetMemAvailable(); std::shared_ptr workPolicyManager_; diff --git a/services/native/include/policy/power_mode_policy.h b/services/native/include/policy/power_mode_policy.h index 59879c9..452c9b9 100644 --- a/services/native/include/policy/power_mode_policy.h +++ b/services/native/include/policy/power_mode_policy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -29,14 +29,7 @@ public: * * @return Res. */ - int32_t GetPolicyMaxRunning() override; - - /** - * @brief Get policyName. - * - * @return PolicyName. - */ - std::string GetPolicyName() override; + int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) override; private: std::shared_ptr workPolicyManager_; }; diff --git a/services/native/include/policy/thermal_policy.h b/services/native/include/policy/thermal_policy.h index 48d97f3..ebd8947 100644 --- a/services/native/include/policy/thermal_policy.h +++ b/services/native/include/policy/thermal_policy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -30,14 +30,7 @@ public: * * @return Res. */ - int32_t GetPolicyMaxRunning() override; - - /** - * @brief Get policyName. - * - * @return PolicyName. - */ - std::string GetPolicyName() override; + int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) override; private: std::shared_ptr workPolicyManager_; }; diff --git a/services/native/include/work_conn_manager.h b/services/native/include/work_conn_manager.h index b753c92..886d716 100644 --- a/services/native/include/work_conn_manager.h +++ b/services/native/include/work_conn_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -23,6 +23,7 @@ #include "work_scheduler_connection.h" #include "work_status.h" #include "ffrt.h" +#include "ability_manager_interface.h" namespace OHOS { using namespace Utils; @@ -56,6 +57,7 @@ private: void AddConnInfo(std::string &workId, sptr &connection); sptr GetConnInfo(std::string &workId); bool DisConnect(sptr connect); + sptr GetSystemAbilityManager(const std::string& moduleName); private: ffrt::mutex connMapMutex_; diff --git a/services/native/include/work_policy_manager.h b/services/native/include/work_policy_manager.h index 271423e..4023406 100644 --- a/services/native/include/work_policy_manager.h +++ b/services/native/include/work_policy_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -297,7 +297,7 @@ public: bool FindWork(int32_t uid); bool FindWork(const int32_t userId, const std::string &bundleName); private: - int32_t GetMaxRunningCount(std::string& policyName); + int32_t GetMaxRunningCount(WorkSchedSystemPolicy& systemPolicy); int32_t GetRunningCount(); void DumpConditionReadyQueue(std::string& result); void DumpUidQueueMap(std::string& result); diff --git a/services/native/src/conditions/screen_listener.cpp b/services/native/src/conditions/screen_listener.cpp index 4664796..9463321 100644 --- a/services/native/src/conditions/screen_listener.cpp +++ b/services/native/src/conditions/screen_listener.cpp @@ -28,6 +28,7 @@ #include "work_scheduler_service.h" #include "work_sched_constants.h" #include "conditions/timer_info.h" +#include "work_sched_hisysevent_report.h" namespace OHOS { namespace WorkScheduler { @@ -45,6 +46,7 @@ void ScreenEventSubscriber::OnReceiveEvent(const EventFwk::CommonEventData &data } if (action == EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED) { listener_.StopTimer(); + WorkSchedUtil::HiSysEventDeepIdleState(false); listener_.OnConditionChanged(WorkCondition::Type::DEEP_IDLE, std::make_shared(0, 0, false, std::string())); auto task = [weak = weak_from_this()]() { diff --git a/services/native/src/policy/cpu_policy.cpp b/services/native/src/policy/cpu_policy.cpp index 4b1089c..d0df1da 100644 --- a/services/native/src/policy/cpu_policy.cpp +++ b/services/native/src/policy/cpu_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -60,7 +60,7 @@ int32_t CpuPolicy::GetCpuUsage() return cpuUsage; } -int32_t CpuPolicy::GetPolicyMaxRunning() +int32_t CpuPolicy::GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) { int32_t cpuUsage = GetCpuUsage(); int32_t policyRes; @@ -73,13 +73,10 @@ int32_t CpuPolicy::GetPolicyMaxRunning() } else { policyRes = COUNT_CPU_MAX; } + systemPolicy.cpuUsage = cpuUsage; + systemPolicy.policyName = "CPU_POLICY"; WS_HILOGD("cpu_usage: %{public}d, policyRes: %{public}d", cpuUsage, policyRes); return policyRes; } - -std::string CpuPolicy::GetPolicyName() -{ - return "CPU_POLICY"; -} } // namespace WorkScheduler } // namespace OHOS \ No newline at end of file diff --git a/services/native/src/policy/memory_policy.cpp b/services/native/src/policy/memory_policy.cpp index 08052db..a6f6147 100644 --- a/services/native/src/policy/memory_policy.cpp +++ b/services/native/src/policy/memory_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -77,7 +77,7 @@ int32_t MemoryPolicy::GetMemAvailable() return memAvailable; } -int32_t MemoryPolicy::GetPolicyMaxRunning() +int32_t MemoryPolicy::GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) { int32_t memAvailable = GetMemAvailable(); WS_HILOGD("mem_available: %{public}d", memAvailable); @@ -87,13 +87,10 @@ int32_t MemoryPolicy::GetPolicyMaxRunning() if (memAvailable <= MEM_LOW) { return COUNT_MEMORY_LOW; } + systemPolicy.memAvailable = memAvailable; + systemPolicy.policyName = "MEMORY_POLICY"; WS_HILOGD("memory left normal"); return COUNT_MEMORY_NORMAL; } - -std::string MemoryPolicy::GetPolicyName() -{ - return "MEMORY_POLICY"; -} } // namespace WorkScheduler } // namespace OHOS \ No newline at end of file diff --git a/services/native/src/policy/power_mode_policy.cpp b/services/native/src/policy/power_mode_policy.cpp index b8f9d8e..f7993cd 100644 --- a/services/native/src/policy/power_mode_policy.cpp +++ b/services/native/src/policy/power_mode_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -40,7 +40,7 @@ PowerModePolicy::~PowerModePolicy() { } -int32_t PowerModePolicy::GetPolicyMaxRunning() +int32_t PowerModePolicy::GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) { int32_t res = COUNT_POWER_MODE_NORMAL; auto mode = PowerMgrClient::GetInstance().GetDeviceMode(); @@ -52,16 +52,13 @@ int32_t PowerModePolicy::GetPolicyMaxRunning() auto charge = BatterySrvClient::GetInstance().GetChargingStatus(); if (charge == BatteryChargeState::CHARGE_STATE_NONE || charge == BatteryChargeState::CHARGE_STATE_DISABLE) { res = COUNT_POWER_MODE_CRUCIAL; + systemPolicy.powerMode = static_cast(mode); + systemPolicy.policyName = "POWER_MODE_POLICY"; WS_HILOGI("charge: %{public}d, power mode: %{public}d, PolicyRes: %{public}d", charge, mode, res); } #endif WS_HILOGD("power mode: %{public}d, PolicyRes: %{public}d", mode, res); return res; } - -std::string PowerModePolicy::GetPolicyName() -{ - return "POWER_MODE_POLICY"; -} } // namespace WorkScheduler } // namespace OHOS \ No newline at end of file diff --git a/services/native/src/policy/thermal_policy.cpp b/services/native/src/policy/thermal_policy.cpp index 20e1542..2dfb331 100644 --- a/services/native/src/policy/thermal_policy.cpp +++ b/services/native/src/policy/thermal_policy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -36,7 +36,7 @@ ThermalPolicy::~ThermalPolicy() { } -int32_t ThermalPolicy::GetPolicyMaxRunning() +int32_t ThermalPolicy::GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) { auto& thermalMgrClient = ThermalMgrClient::GetInstance(); ThermalLevel thermalLevel = thermalMgrClient.GetThermalLevel(); @@ -48,14 +48,11 @@ int32_t ThermalPolicy::GetPolicyMaxRunning() } else { res = COUNT_THERMAL_NORMAL; } + systemPolicy.thermalLevel = static_cast(thermalLevel); + systemPolicy.policyName = "THERMAL_POLICY"; WS_HILOGD("ThermalLevel:%{public}d, PolicyRes:%{public}d", thermalLevel, res); return res; } - -std::string ThermalPolicy::GetPolicyName() -{ - return "THERMAL_POLICY"; -} } // namespace WorkScheduler } // namespace OHOS #endif // POWERMGR_THERMAL_MANAGER_ENABLE \ No newline at end of file diff --git a/services/native/src/work_conn_manager.cpp b/services/native/src/work_conn_manager.cpp index 121b9eb..d3a6276 100644 --- a/services/native/src/work_conn_manager.cpp +++ b/services/native/src/work_conn_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-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 @@ -27,6 +27,8 @@ #include "work_sched_utils.h" #include "errors.h" #include +#include "work_sched_constants.h" +#include "work_sched_hisysevent_report.h" #ifdef DEVICE_STANDBY_ENABLE #include "standby_service_client.h" @@ -67,6 +69,7 @@ bool WorkConnManager::StartWork(shared_ptr workStatus) if (conn) { WS_HILOGE("Work has started with id: %{public}s, bundleName: %{public}s, abilityName: %{public}s", workStatus->workId_.c_str(), workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str()); + WorkSchedUtil::HiSysEventException(CONNECT_ABILITY, __func__, "connect info has existed, connect failed"); RemoveConnInfo(workStatus->workId_); if (conn->IsConnected()) { conn->StopWork(); @@ -76,31 +79,20 @@ bool WorkConnManager::StartWork(shared_ptr workStatus) if (!workStatus->workInfo_->GetExtension()) { WS_HILOGE("%{public}s extension's type is not workScheduler, connect failed", workStatus->bundleName_.c_str()); + WorkSchedUtil::HiSysEventException(CONNECT_ABILITY, __func__, "app extension's type is not workScheduler"); return false; } WS_HILOGI("Begin to connect bundle:%{public}s, abilityName:%{public}s, workId:%{public}s", workStatus->bundleName_.c_str(), workStatus->abilityName_.c_str(), workStatus->workId_.c_str()); - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - WS_HILOGE("Failed to get system ability manager service."); + sptr abilityMgr_ = GetSystemAbilityManager(CONNECT_ABILITY); + if (abilityMgr_ == nullptr) { return false; } - sptr remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); - if (remoteObject == nullptr) { - WS_HILOGE("Failed to ability manager service."); - return false; - } - sptr abilityMgr_ = iface_cast(remoteObject); - if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) { - WS_HILOGE("Failed to get ability manager services object"); - return false; - } - sptr connection(new (std::nothrow) WorkSchedulerConnection(workStatus->workInfo_)); if (connection == nullptr) { WS_HILOGE("Failed to new connection."); + WorkSchedUtil::HiSysEventException(CONNECT_ABILITY, __func__, "create connection failed"); return false; } @@ -110,6 +102,7 @@ bool WorkConnManager::StartWork(shared_ptr workStatus) int32_t ret = abilityMgr_->ConnectAbility(want, connection, nullptr, workStatus->userId_); if (ret != ERR_OK) { WS_HILOGE("connect failed"); + WorkSchedUtil::HiSysEventException(CONNECT_ABILITY, __func__, "connect system ability failed"); return false; } AddConnInfo(workStatus->workId_, connection); @@ -122,25 +115,14 @@ bool WorkConnManager::StartWork(shared_ptr workStatus) bool WorkConnManager::DisConnect(sptr connect) { - sptr systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityManager == nullptr) { - WS_HILOGE("Failed to get system ability manager service."); - return false; - } - sptr remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); - if (remoteObject == nullptr) { - WS_HILOGE("Failed to ability manager service."); - return false; - } - sptr abilityMgr_ = iface_cast(remoteObject); - if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) { - WS_HILOGE("Failed to get ability manager services object."); + sptr abilityMgr_ = GetSystemAbilityManager(DISCONNECT_ABILITY); + if (abilityMgr_ == nullptr) { return false; } int32_t ret = abilityMgr_->DisconnectAbility(connect); if (ret != ERR_OK) { WS_HILOGE("disconnect failed"); + WorkSchedUtil::HiSysEventException(DISCONNECT_ABILITY, __func__, "disconnect system ability failed"); return false; } return true; @@ -231,5 +213,29 @@ void WorkConnManager::WriteStartWorkEvent(shared_ptr workStatus) workStatus->uid_, workStatus->bundleName_); #endif // DEVICE_STANDBY_ENABLE } + +sptr WorkConnManager::GetSystemAbilityManager(const std::string& moduleName) +{ + sptr systemAbilityManager = + SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemAbilityManager == nullptr) { + WS_HILOGE("Failed to get system ability manager service."); + WorkSchedUtil::HiSysEventException(moduleName, __func__, "get system ability manager failed"); + return nullptr; + } + sptr remoteObject = systemAbilityManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); + if (remoteObject == nullptr) { + WS_HILOGE("Failed to get system ability."); + WorkSchedUtil::HiSysEventException(moduleName, __func__, "get system ability failed"); + return nullptr; + } + sptr abilityMgr_ = iface_cast(remoteObject); + if ((abilityMgr_ == nullptr) || (abilityMgr_->AsObject() == nullptr)) { + WS_HILOGE("Failed to get ability manager services object"); + WorkSchedUtil::HiSysEventException(moduleName, __func__, "cast system ability failed"); + return nullptr; + } + return abilityMgr_; +} } // namespace WorkScheduler } // namespace OHOS diff --git a/services/native/src/work_policy_manager.cpp b/services/native/src/work_policy_manager.cpp index cf92cb4..848a29a 100644 --- a/services/native/src/work_policy_manager.cpp +++ b/services/native/src/work_policy_manager.cpp @@ -31,6 +31,7 @@ #include "work_sched_utils.h" #include "watchdog.h" #include "work_sched_data_manager.h" +#include "work_sched_hisysevent_report.h" #include using namespace std; @@ -300,7 +301,7 @@ void WorkPolicyManager::AddToReadyQueue(shared_ptr conditionReadyQueue_->Push(workStatusVector); } -int32_t WorkPolicyManager::GetMaxRunningCount(std::string& policyName) +int32_t WorkPolicyManager::GetMaxRunningCount(WorkSchedSystemPolicy& systemPolicy) { int32_t currentMaxRunning = GetDumpSetMaxRunningCount(); if (currentMaxRunning > 0 && currentMaxRunning <= DUMP_SET_MAX_COUNT_LIMIT) { @@ -308,10 +309,9 @@ int32_t WorkPolicyManager::GetMaxRunningCount(std::string& policyName) } currentMaxRunning = MAX_RUNNING_COUNT; for (auto policyFilter : policyFilters_) { - int32_t policyMaxRunning = policyFilter->GetPolicyMaxRunning(); + int32_t policyMaxRunning = policyFilter->GetPolicyMaxRunning(systemPolicy); if (policyMaxRunning < currentMaxRunning) { currentMaxRunning = policyMaxRunning; - policyName = policyFilter->GetPolicyName(); } } return currentMaxRunning; @@ -404,9 +404,9 @@ void WorkPolicyManager::CheckWorkToRun() WS_HILOGD("no condition ready work not running, return."); return; } - std::string policyName; + WorkSchedSystemPolicy systemPolicy; int32_t runningCount = GetRunningCount(); - int32_t allowRunningCount = GetMaxRunningCount(policyName); + int32_t allowRunningCount = GetMaxRunningCount(systemPolicy); if (runningCount < allowRunningCount || IsSpecialScene(topWork, runningCount)) { WS_HILOGD("running count < max running count"); if (topWork->workInfo_->IsSA()) { @@ -418,14 +418,15 @@ void WorkPolicyManager::CheckWorkToRun() } else { WS_HILOGD("trigger delay: %{public}d", DELAY_TIME_LONG); if (runningCount == MAX_RUNNING_COUNT) { - policyName = "OVER_LIMIT"; + systemPolicy.policyName = "OVER_LIMIT"; } - if (!policyName.empty()) { - topWork->delayReason_= policyName; + if (!systemPolicy.policyName.empty()) { + topWork->delayReason_= systemPolicy.policyName; WS_HILOGI("trigger delay, reason:%{public}s, runningCount:%{public}d allowRunningCount:%{public}d," - "bundleName:%{public}s, workId:%{public}s", policyName.c_str(), runningCount, allowRunningCount, - topWork->bundleName_.c_str(), topWork->workId_.c_str()); + "bundleName:%{public}s, workId:%{public}s", systemPolicy.GetInfo().c_str(), runningCount, + allowRunningCount, topWork->bundleName_.c_str(), topWork->workId_.c_str()); + WorkSchedUtil::HiSysEventSystemPolicyLimit(systemPolicy); } SendRetrigger(DELAY_TIME_LONG); } @@ -559,6 +560,7 @@ void WorkPolicyManager::WatchdogTimeOut(uint32_t watchdogId) std::shared_ptr workStatus = GetWorkFromWatchdog(watchdogId); if (workStatus == nullptr) { WS_HILOGE("watchdog:%{public}u time out error, workStatus is nullptr", watchdogId); + WorkSchedUtil::HiSysEventException(WATCHDOG_TIMEOUT, __func__, "get workstatus from watchdog is nullptr"); return; } WS_HILOGI("WatchdogTimeOut, watchId:%{public}u, bundleName:%{public}s, workId:%{public}s", @@ -659,10 +661,11 @@ void WorkPolicyManager::Dump(string& result) result.append("2. workPolicyManager uidQueueMap:\n"); DumpUidQueueMap(result); - std::string policyName; + WorkSchedSystemPolicy systemPolicy; result.append("3. GetMaxRunningCount:"); - result.append(to_string(GetMaxRunningCount(policyName)) - + (policyName.empty() ? "" : " reason: " + policyName) + "\n"); + int32_t maxRunningCount = GetMaxRunningCount(systemPolicy); + result.append(to_string(maxRunningCount) + + (maxRunningCount == MAX_RUNNING_COUNT ? "" : systemPolicy.GetInfo()) + "\n"); } uint32_t WorkPolicyManager::NewWatchdogId() diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index b8ef44d..b6c410a 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -76,6 +76,8 @@ #include "res_sched_client.h" #include "work_sched_data_manager.h" #include "work_sched_config.h" +#include "work_sched_constants.h" +#include "work_sched_hisysevent_report.h" #ifdef HICOLLIE_ENABLE #include "xcollie/xcollie.h" @@ -371,6 +373,7 @@ list> WorkSchedulerService::ReadPreinstalledWorks() CfgFiles *files = GetCfgFiles(PREINSTALLED_FILE_PATH); if (!files) { WS_HILOGE("GetCfgFiles failed"); + WorkSchedUtil::HiSysEventException(LOAD_WORK, __func__, "get cfg files failed"); return workInfos; } // china->base @@ -388,6 +391,7 @@ bool WorkSchedulerService::GetJsonFromFile(const char *filePath, Json::Value &ro std::string realPath; if (!WorkSchedUtils::ConvertFullPath(filePath, realPath)) { WS_HILOGE("Get real path failed %{private}s", filePath); + WorkSchedUtil::HiSysEventException(LOAD_WORK, __func__, "convert real path failed"); return false; } WS_HILOGD("Read from %{private}s", realPath.c_str()); @@ -400,6 +404,7 @@ bool WorkSchedulerService::GetJsonFromFile(const char *filePath, Json::Value &ro bool res = jsonReader->parse(data.c_str(), data.c_str() + data.length(), &root, &errs); if (!res || !errs.empty()) { WS_HILOGE("parse %{private}s json error", realPath.c_str()); + WorkSchedUtil::HiSysEventException(LOAD_WORK, __func__, "json parse failed"); return false; } WS_HILOGI("json parse success"); @@ -410,21 +415,21 @@ void WorkSchedulerService::OnStop() { WS_HILOGI("stop service."); std::lock_guard observerLock(observerMutex_); -#ifdef DEVICE_USAGE_STATISTICS_ENABLE + #ifdef DEVICE_USAGE_STATISTICS_ENABLE DeviceUsageStats::BundleActiveClient::GetInstance().UnRegisterAppGroupCallBack(groupObserver_); groupObserver_ = nullptr; g_hasGroupObserver = -1; -#endif -#ifdef DEVICE_STANDBY_ENABLE + #endif + #ifdef DEVICE_STANDBY_ENABLE DevStandbyMgr::StandbyServiceClient::GetInstance().UnsubscribeStandbyCallback(standbyStateObserver_); standbyStateObserver_ = nullptr; -#endif -#ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE + #endif + #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE ErrCode ret = BackgroundTaskMgr::BackgroundTaskMgrHelper::UnsubscribeBackgroundTask(*subscriber_); if (ret != ERR_OK) { WS_HILOGE("unscribe bgtask failed."); } -#endif + #endif eventRunner_.reset(); handler_.reset(); ready_ = false; @@ -434,6 +439,7 @@ bool WorkSchedulerService::Init(const std::shared_ptr& { if (!IsBaseAbilityReady()) { WS_HILOGE("request system service is not ready yet!"); + WorkSchedUtil::HiSysEventException(SERVICE_INIT, __func__, "request system service is not ready"); GetHandler()->SendEvent(InnerEvent::Get(WorkEventHandler::SERVICE_INIT_MSG, 0), INIT_DELAY); return false; } @@ -445,6 +451,7 @@ bool WorkSchedulerService::Init(const std::shared_ptr& InitWorkInner(); if (!Publish(wss)) { WS_HILOGE("OnStart register to system ability manager failed!"); + WorkSchedUtil::HiSysEventException(SERVICE_INIT, __func__, "register to system ability manager failed"); return false; } checkBundle_ = true; @@ -460,11 +467,12 @@ bool WorkSchedulerService::Init(const std::shared_ptr& bool WorkSchedulerService::InitBgTaskSubscriber() { -#ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE + #ifdef RESOURCESCHEDULE_BGTASKMGR_ENABLE subscriber_ = make_shared(); ErrCode ret = BackgroundTaskMgr::BackgroundTaskMgrHelper::SubscribeBackgroundTask(*subscriber_); if (ret != ERR_OK) { WS_HILOGE("SubscribeBackgroundTask failed."); + WorkSchedUtil::HiSysEventException(SERVICE_INIT, __func__, "subscribe background task failed"); return false; } this->QueryResAppliedUid(); @@ -1552,6 +1560,7 @@ bool WorkSchedulerService::LoadSa(std::shared_ptr workStatus, const sptr samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); if (samgr == nullptr) { WS_HILOGE("get SA manager failed."); + WorkSchedUtil::HiSysEventException(LOAD_SA, __func__, "get system ability manager failed"); return false; } int32_t saId = workStatus->workInfo_->GetSaId(); @@ -1559,11 +1568,13 @@ bool WorkSchedulerService::LoadSa(std::shared_ptr workStatus, const sptr object = samgr->CheckSystemAbility(saId); if (isResidentSa && object == nullptr) { WS_HILOGE("resident SA: %{public}d residentSA:%{public}d does not exist.", saId, isResidentSa); + WorkSchedUtil::HiSysEventException(LOAD_SA, __func__, "sa dose not exist"); return false; } else if (!isResidentSa && object == nullptr) { object = samgr->LoadSystemAbility(saId, TIME_OUT); if (object == nullptr) { WS_HILOGE("load SA: %{public}d residentSA:%{public}d failed.", saId, isResidentSa); + WorkSchedUtil::HiSysEventException(LOAD_SA, __func__, "load system ability failed"); return false; } WS_HILOGI("load SA: %{public}d residentSA:%{public}d successed.", saId, isResidentSa); @@ -1637,6 +1648,7 @@ void WorkSchedulerService::HandleDeepIdleMsg() } workQueueManager_->OnConditionChanged(WorkCondition::Type::DEEP_IDLE, std::make_shared(0, 0, true, std::string())); + WorkSchedUtil::HiSysEventDeepIdleState(true); } bool WorkSchedulerService::IsPreinstalledBundle(const std::string& checkBundleName) diff --git a/services/native/src/work_standby_state_change_callback.cpp b/services/native/src/work_standby_state_change_callback.cpp index d95cd1c..cc54ab4 100644 --- a/services/native/src/work_standby_state_change_callback.cpp +++ b/services/native/src/work_standby_state_change_callback.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-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 @@ -20,6 +20,7 @@ #include "work_scheduler_service.h" #include "work_policy_manager.h" #include "work_sched_data_manager.h" +#include "work_sched_hisysevent_report.h" namespace OHOS { namespace WorkScheduler { @@ -42,7 +43,13 @@ void WorkStandbyStateChangeCallback::OnDeviceIdleMode(bool napped, bool sleeping // (1, 0) or (0, 0) WS_HILOGI("Device standby exit sleeping state"); } - DelayedSingleton::GetInstance()->SetDeviceSleep(sleeping); + auto dataManager = DelayedSingleton::GetInstance(); + if (dataManager->GetDeviceSleep() == sleeping) { + WS_HILOGI("Device standby state is not changed"); + return; + } + WorkSchedUtil::HiSysEventDeviceStandbyState(sleeping); + dataManager->SetDeviceSleep(sleeping); workQueueManager_->OnConditionChanged(WorkCondition::Type::STANDBY, std::make_shared(0, 0, sleeping, std::string())); } diff --git a/services/test/BUILD.gn b/services/test/BUILD.gn index a468ef6..cc6ea79 100644 --- a/services/test/BUILD.gn +++ b/services/test/BUILD.gn @@ -61,6 +61,7 @@ ohos_unittest("WorkScheduleServiceTest") { "src/work_queue_test.cpp", "src/work_sched_config_test.cpp", "src/work_sched_data_manager_test.cpp", + "src/work_standby_state_change_callback_test.cpp", "src/work_status_test.cpp", "src/workschedulerservice_test.cpp", "src/zidl/work_scheduler_proxy_test.cpp", @@ -91,6 +92,7 @@ ohos_unittest("WorkScheduleServiceTest") { "jsoncpp:jsoncpp", "safwk:system_ability_fwk", "samgr:samgr_proxy", + "time_service:time_client", ] defines = [] if (bundle_active_enable) { diff --git a/services/test/src/policy/cpu_policy_test.cpp b/services/test/src/policy/cpu_policy_test.cpp index e633c3b..6f404ba 100644 --- a/services/test/src/policy/cpu_policy_test.cpp +++ b/services/test/src/policy/cpu_policy_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -91,8 +91,9 @@ HWTEST_F(CpuPolicyTest, getCpuUsage_003, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_001, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetCpuUsageByDump(15); - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(maxRunning, 3); } @@ -104,8 +105,9 @@ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_001, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_002, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetCpuUsageByDump(45); - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(maxRunning, 2); } @@ -117,8 +119,9 @@ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_002, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_003, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetCpuUsageByDump(55); - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(maxRunning, 1); } @@ -130,8 +133,9 @@ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_003, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_004, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetCpuUsageByDump(65); - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(maxRunning, 0); } @@ -143,8 +147,9 @@ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_004, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_005, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetCpuUsageByDump(120); - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3); } @@ -156,20 +161,9 @@ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_005, TestSize.Level1) */ HWTEST_F(CpuPolicyTest, getPolicyMaxRunning_006, TestSize.Level1) { - int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(); + WorkSchedSystemPolicy systemPolicy; + int32_t maxRunning = cpuPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_TRUE(maxRunning >= 0 && maxRunning <= 3); } - -/** - * @tc.name: GetPolicyName_001 - * @tc.desc: Test CpuPolicy GetPolicyName. - * @tc.type: FUNC - * @tc.require: I974IQ - */ -HWTEST_F(CpuPolicyTest, GetPolicyName_001, TestSize.Level1) -{ - std::string policyName = cpuPolicy_->GetPolicyName(); - EXPECT_TRUE(policyName == "CPU_POLICY"); -} } } \ No newline at end of file diff --git a/services/test/src/policy/memory_policy_test.cpp b/services/test/src/policy/memory_policy_test.cpp index 1f07b0f..6558303 100644 --- a/services/test/src/policy/memory_policy_test.cpp +++ b/services/test/src/policy/memory_policy_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -53,8 +53,9 @@ void MemoryPolicyTest::SetUpTestCase() */ HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; memoryPolicy_->workPolicyManager_->SetMemoryByDump(1 * 1024 * 1024); - int32_t ret = memoryPolicy_->GetPolicyMaxRunning(); + int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 1); } @@ -66,8 +67,9 @@ HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) */ HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; memoryPolicy_->workPolicyManager_->SetMemoryByDump(2 * 1024 * 1024); - int32_t ret = memoryPolicy_->GetPolicyMaxRunning(); + int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 2); } @@ -79,21 +81,10 @@ HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) */ HWTEST_F(MemoryPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; memoryPolicy_->workPolicyManager_->SetMemoryByDump(3 * 1024 * 1024); - int32_t ret = memoryPolicy_->GetPolicyMaxRunning(); + int32_t ret = memoryPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 3); } - -/** - * @tc.name: GetPolicyName_001 - * @tc.desc: Test MemoryPolicy GetPolicyName. - * @tc.type: FUNC - * @tc.require: I974IQ - */ -HWTEST_F(MemoryPolicyTest, GetPolicyName_001, TestSize.Level1) -{ - std::string ret = memoryPolicy_->GetPolicyName(); - EXPECT_EQ(ret, "MEMORY_POLICY"); -} } } diff --git a/services/test/src/policy/power_mode_policy_test.cpp b/services/test/src/policy/power_mode_policy_test.cpp index 7bcc7d1..ddeeae0 100644 --- a/services/test/src/policy/power_mode_policy_test.cpp +++ b/services/test/src/policy/power_mode_policy_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -63,8 +63,9 @@ void PowerModePolicyTest::SetUpTestCase() */ HWTEST_F(PowerModePolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; PowerMgr::PowerMgrClient::GetInstance().SetDeviceMode(PowerMgr::PowerMode::NORMAL_MODE); - int32_t ret = powerModePolicy_->GetPolicyMaxRunning(); + int32_t ret = powerModePolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 3); } @@ -76,8 +77,9 @@ HWTEST_F(PowerModePolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) */ HWTEST_F(PowerModePolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; PowerMgr::PowerMgrClient::GetInstance().SetDeviceMode(PowerMgr::PowerMode::PERFORMANCE_MODE); - int32_t ret = powerModePolicy_->GetPolicyMaxRunning(); + int32_t ret = powerModePolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 3); } @@ -89,21 +91,10 @@ HWTEST_F(PowerModePolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) */ HWTEST_F(PowerModePolicyTest, GetPolicyMaxRunning_003, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; PowerMgr::PowerMgrClient::GetInstance().SetDeviceMode(PowerMgr::PowerMode::POWER_MODE_MIN); - int32_t ret = powerModePolicy_->GetPolicyMaxRunning(); + int32_t ret = powerModePolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 3); } - -/** - * @tc.name: GetPolicyName_001 - * @tc.desc: Test PowerModePolicy GetPolicyName. - * @tc.type: FUNC - * @tc.require: I974IQ - */ -HWTEST_F(PowerModePolicyTest, GetPolicyName_001, TestSize.Level1) -{ - std::string ret = powerModePolicy_->GetPolicyName(); - EXPECT_EQ(ret, "POWER_MODE_POLICY"); -} } } diff --git a/services/test/src/policy/thermal_policy_test.cpp b/services/test/src/policy/thermal_policy_test.cpp index 2720b64..eade329 100644 --- a/services/test/src/policy/thermal_policy_test.cpp +++ b/services/test/src/policy/thermal_policy_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -68,8 +68,9 @@ void ThermalPolicyTest::SetUpTestCase() */ HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; MockProcess(PowerMgr::ThermalLevel::WARM); - int32_t ret = thermalPolicy_->GetPolicyMaxRunning(); + int32_t ret = thermalPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 0); } @@ -81,8 +82,9 @@ HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_001, TestSize.Level1) */ HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; MockProcess(PowerMgr::ThermalLevel::NORMAL); - int32_t ret = thermalPolicy_->GetPolicyMaxRunning(); + int32_t ret = thermalPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 1); } @@ -94,21 +96,10 @@ HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_002, TestSize.Level1) */ HWTEST_F(ThermalPolicyTest, GetPolicyMaxRunning_003, TestSize.Level1) { + WorkSchedSystemPolicy systemPolicy; MockProcess(PowerMgr::ThermalLevel::COOL); - int32_t ret = thermalPolicy_->GetPolicyMaxRunning(); + int32_t ret = thermalPolicy_->GetPolicyMaxRunning(systemPolicy); EXPECT_EQ(ret, 3); } - -/** - * @tc.name: GetPolicyName_001 - * @tc.desc: Test ThermalPolicy GetPolicyName. - * @tc.type: FUNC - * @tc.require: I974IQ - */ -HWTEST_F(ThermalPolicyTest, GetPolicyName_001, TestSize.Level1) -{ - std::string ret = thermalPolicy_->GetPolicyName(); - EXPECT_EQ(ret, "THERMAL_POLICY"); -} } } diff --git a/services/test/src/work_policy_manager_test.cpp b/services/test/src/work_policy_manager_test.cpp index 72b3145..b4b301e 100644 --- a/services/test/src/work_policy_manager_test.cpp +++ b/services/test/src/work_policy_manager_test.cpp @@ -44,17 +44,11 @@ public: explicit MockPolicyFilter(std::shared_ptr workPolicyManager){}; ~MockPolicyFilter(){}; int32_t maxRunningCount; - std::string policyName; - int32_t GetPolicyMaxRunning() override + int32_t GetPolicyMaxRunning(WorkSchedSystemPolicy& systemPolicy) override { return maxRunningCount; } - - std::string GetPolicyName() override - { - return policyName; - } }; std::shared_ptr WorkPolicyManagerTest::workPolicyManager_ = nullptr; @@ -788,8 +782,8 @@ HWTEST_F(WorkPolicyManagerTest, AddToReadyQueue_001, TestSize.Level1) HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_001, TestSize.Level1) { workPolicyManager_->dumpSetMaxRunningCount_ = 2; - std::string policyName; - int32_t ret = workPolicyManager_->GetMaxRunningCount(policyName); + WorkSchedSystemPolicy systemPolicy; + int32_t ret = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_TRUE(ret == workPolicyManager_->dumpSetMaxRunningCount_); } @@ -802,8 +796,8 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_001, TestSize.Level1) HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_002, TestSize.Level1) { workPolicyManager_->dumpSetMaxRunningCount_ = 0; - std::string policyName; - int32_t ret = workPolicyManager_->GetMaxRunningCount(policyName); + WorkSchedSystemPolicy systemPolicy; + int32_t ret = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_TRUE(ret >= workPolicyManager_->dumpSetMaxRunningCount_); } @@ -815,12 +809,11 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_002, TestSize.Level1) */ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_003, TestSize.Level1) { - std::string policyName; + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetMaxRunningCountByDump(5); - int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + int32_t result = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_EQ(5, result); - EXPECT_TRUE(policyName.empty()); workPolicyManager_->policyFilters_.clear(); } @@ -832,24 +825,21 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_003, TestSize.Level1) */ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_004, TestSize.Level1) { - std::string policyName; + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetMaxRunningCountByDump(0); auto filter1 = std::make_shared(workPolicyManager_); filter1->maxRunningCount = 2; - filter1->policyName = "policyA"; auto filter2 = std::make_shared(workPolicyManager_); filter2->maxRunningCount = 4; - filter2->policyName = "policyB"; workPolicyManager_->AddPolicyFilter(filter1); workPolicyManager_->AddPolicyFilter(filter2); - int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + int32_t result = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_EQ(2, result); - EXPECT_EQ("policyA", policyName); workPolicyManager_->policyFilters_.clear(); } @@ -861,29 +851,25 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_004, TestSize.Level1) */ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_005, TestSize.Level1) { - std::string policyName; + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetMaxRunningCountByDump(0); auto filter1 = std::make_shared(workPolicyManager_); filter1->maxRunningCount = 5; - filter1->policyName = "policyX"; auto filter2 = std::make_shared(workPolicyManager_); filter2->maxRunningCount = 3; - filter2->policyName = "policyY"; auto filter3 = std::make_shared(workPolicyManager_); filter3->maxRunningCount = 7; - filter3->policyName = "policyZ"; workPolicyManager_->AddPolicyFilter(filter1); workPolicyManager_->AddPolicyFilter(filter2); workPolicyManager_->AddPolicyFilter(filter3); - int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + int32_t result = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_EQ(3, result); - EXPECT_TRUE(policyName.empty()); workPolicyManager_->policyFilters_.clear(); } @@ -895,13 +881,12 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_005, TestSize.Level1) */ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_006, TestSize.Level1) { - std::string policyName; + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetMaxRunningCountByDump(0); - int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + int32_t result = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_EQ(3, result); - EXPECT_TRUE(policyName.empty()); workPolicyManager_->policyFilters_.clear(); } @@ -913,34 +898,29 @@ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_006, TestSize.Level1) */ HWTEST_F(WorkPolicyManagerTest, GetMaxRunningCount_007, TestSize.Level1) { - std::string policyName; + WorkSchedSystemPolicy systemPolicy; workPolicyManager_->SetMaxRunningCountByDump(0); auto filter1 = std::make_shared(workPolicyManager_); filter1->maxRunningCount = 10; - filter1->policyName = "policy1"; auto filter2 = std::make_shared(workPolicyManager_); filter2->maxRunningCount = 5; - filter2->policyName = "policy2"; auto filter3 = std::make_shared(workPolicyManager_); filter3->maxRunningCount = 8; - filter3->policyName = "policy3"; auto filter4 = std::make_shared(workPolicyManager_); filter4->maxRunningCount = 3; - filter4->policyName = "policy4"; workPolicyManager_->AddPolicyFilter(filter1); workPolicyManager_->AddPolicyFilter(filter2); workPolicyManager_->AddPolicyFilter(filter3); workPolicyManager_->AddPolicyFilter(filter4); - int32_t result = workPolicyManager_->GetMaxRunningCount(policyName); + int32_t result = workPolicyManager_->GetMaxRunningCount(systemPolicy); EXPECT_EQ(3, result); - EXPECT_TRUE(policyName.empty()); workPolicyManager_->policyFilters_.clear(); } diff --git a/services/test/src/work_standby_state_change_callback_test.cpp b/services/test/src/work_standby_state_change_callback_test.cpp new file mode 100644 index 0000000..d910b45 --- /dev/null +++ b/services/test/src/work_standby_state_change_callback_test.cpp @@ -0,0 +1,117 @@ +/* + * 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 +#include +#include "gmock/gmock.h" + +#include "work_queue_manager.h" +#include "work_scheduler_service.h" +#include "work_standby_state_change_callback.h" +#include "work_sched_data_manager.h" +#include "work_sched_utils.h" + +using namespace testing::ext; + +namespace OHOS { +namespace WorkScheduler { +class WorkStandbyStateChangeCallbackTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {} + static std::shared_ptr callback_; + static std::shared_ptr dataManager_; +}; + +std::shared_ptr WorkStandbyStateChangeCallbackTest::callback_ = nullptr; +std::shared_ptr WorkStandbyStateChangeCallbackTest::dataManager_ = nullptr; + +void WorkStandbyStateChangeCallbackTest::SetUpTestCase() +{ + std::shared_ptr workSchedulerService_ = std::make_shared(); + std::shared_ptr workQueueManger_ = std::make_shared(workSchedulerService_); + callback_ = std::make_shared(workQueueManger_); + + dataManager_ = DelayedSingleton::GetInstance(); +} + +/** + * @tc.name: OnDeviceIdleMode_001 + * @tc.desc: Test WorkStandbyStateChangeCallbackTest OnDeviceIdleMode. + * @tc.type: FUNC + * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICB1SP + */ +HWTEST_F(WorkStandbyStateChangeCallbackTest, OnDeviceIdleMode_001, TestSize.Level1) +{ + dataManager_->SetDeviceSleep(false); + callback_->OnDeviceIdleMode(true, false); + EXPECT_FALSE(dataManager_->GetDeviceSleep()); +} + +/** + * @tc.name: OnDeviceIdleMode_002 + * @tc.desc: Test WorkStandbyStateChangeCallbackTest OnDeviceIdleMode. + * @tc.type: FUNC + * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICB1SP + */ +HWTEST_F(WorkStandbyStateChangeCallbackTest, OnDeviceIdleMode_002, TestSize.Level1) +{ + dataManager_->SetDeviceSleep(false); + callback_->OnDeviceIdleMode(false, true); + EXPECT_TRUE(dataManager_->GetDeviceSleep()); +} + +/** + * @tc.name: OnDeviceIdleMode_003 + * @tc.desc: Test WorkStandbyStateChangeCallbackTest OnDeviceIdleMode. + * @tc.type: FUNC + * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICB1SP + */ +HWTEST_F(WorkStandbyStateChangeCallbackTest, OnDeviceIdleMode_003, TestSize.Level1) +{ + dataManager_->SetDeviceSleep(false); + callback_->OnDeviceIdleMode(false, false); + EXPECT_FALSE(dataManager_->GetDeviceSleep()); +} + +/** + * @tc.name: OnDeviceIdleMode_004 + * @tc.desc: Test WorkStandbyStateChangeCallbackTest OnDeviceIdleMode. + * @tc.type: FUNC + * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICB1SP + */ +HWTEST_F(WorkStandbyStateChangeCallbackTest, OnDeviceIdleMode_004, TestSize.Level1) +{ + dataManager_->SetDeviceSleep(false); + callback_->OnDeviceIdleMode(true, true); + EXPECT_TRUE(dataManager_->GetDeviceSleep()); +} + +/** + * @tc.name: OnDeviceIdleMode_005 + * @tc.desc: Test WorkStandbyStateChangeCallbackTest OnDeviceIdleMode. + * @tc.type: FUNC + * @tc.require: https://gitee.com/openharmony/resourceschedule_work_scheduler/issues/ICB1SP + */ +HWTEST_F(WorkStandbyStateChangeCallbackTest, OnDeviceIdleMode_005, TestSize.Level1) +{ + dataManager_->SetDeviceSleep(true); + callback_->OnDeviceIdleMode(false, true); + EXPECT_TRUE(dataManager_->GetDeviceSleep()); +} +} +} \ No newline at end of file diff --git a/services/test/src/work_status_test.cpp b/services/test/src/work_status_test.cpp index 4b5ef7e..5836eb3 100644 --- a/services/test/src/work_status_test.cpp +++ b/services/test/src/work_status_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024-2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -18,6 +18,7 @@ #include "work_status.h" #include "work_condition.h" +#include "work_sched_data_manager.h" #include "work_sched_hilog.h" #include "work_info.h" @@ -1019,6 +1020,8 @@ HWTEST_F(WorkStatusTest, IsSAReady_0014, TestSize.Level1) HWTEST_F(WorkStatusTest, ToString_001, TestSize.Level1) { workStatus_->conditionStatus_.clear(); + std::shared_ptr dataManager = DelayedSingleton::GetInstance(); + dataManager->SetDeviceSleep(false); workStatus_->ToString(WorkCondition::Type::TIMER); EXPECT_TRUE(workStatus_->conditionStatus_.empty()); } @@ -1032,6 +1035,8 @@ HWTEST_F(WorkStatusTest, ToString_001, TestSize.Level1) HWTEST_F(WorkStatusTest, ToString_002, TestSize.Level1) { workStatus_->conditionStatus_.clear(); + std::shared_ptr dataManager = DelayedSingleton::GetInstance(); + dataManager->SetDeviceSleep(false); workStatus_->conditionStatus_ = "TIMER&ready"; workStatus_->workInfo_->saId_ = 1000; workStatus_->workInfo_->residentSa_ = true; @@ -1048,6 +1053,8 @@ HWTEST_F(WorkStatusTest, ToString_002, TestSize.Level1) HWTEST_F(WorkStatusTest, ToString_003, TestSize.Level1) { workStatus_->conditionStatus_.clear(); + std::shared_ptr dataManager = DelayedSingleton::GetInstance(); + dataManager->SetDeviceSleep(false); workStatus_->conditionStatus_ = "TIMER&ready"; workStatus_->ToString(WorkCondition::Type::TIMER); EXPECT_FALSE(workStatus_->conditionStatus_.empty()); diff --git a/utils/native/BUILD.gn b/utils/native/BUILD.gn index aac6259..65630b8 100644 --- a/utils/native/BUILD.gn +++ b/utils/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022-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 @@ -25,7 +25,10 @@ ohos_source_set("workschedutils") { cfi_cross_dso = true debug = false } - sources = [ "${worksched_utils_path}/src/work_sched_utils.cpp" ] + sources = [ + "${worksched_utils_path}/src/work_sched_hisysevent_report.cpp", + "${worksched_utils_path}/src/work_sched_utils.cpp", + ] public_configs = [ ":utils_config" ] @@ -33,6 +36,7 @@ ohos_source_set("workschedutils") { "access_token:libtokenid_sdk", "c_utils:utils", "hilog:libhilog", + "hisysevent:libhisysevent", "init:libbegetutil", "ipc:ipc_single", "jsoncpp:jsoncpp", diff --git a/utils/native/include/work_sched_constants.h b/utils/native/include/work_sched_constants.h index dfa29c2..d4766c4 100644 --- a/utils/native/include/work_sched_constants.h +++ b/utils/native/include/work_sched_constants.h @@ -16,31 +16,40 @@ #ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_CONSTANTS_H #define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_CONSTANTS_H -namespace { +namespace OHOS { +namespace WorkScheduler { // services\native\src\work_policy_manager.cpp -constexpr int32_t MAX_RUNNING_COUNT = 3; -const int32_t STANDBY_MAX_RUNNING_COUNT = 2 * MAX_RUNNING_COUNT; -constexpr uint32_t MAX_WORK_COUNT_PER_UID = 10; -constexpr int32_t DELAY_TIME_LONG = 30000; -constexpr int32_t DELAY_TIME_SHORT = 5000; -constexpr uint32_t MAX_WATCHDOG_ID = 1000; -constexpr uint32_t INIT_WATCHDOG_ID = 1; -constexpr int32_t INIT_DUMP_SET_MEMORY = -1; -constexpr int32_t WATCHDOG_TIME = 2 * 60 * 1000; -constexpr int32_t MEDIUM_WATCHDOG_TIME = 10 * 60 * 1000; -constexpr int32_t LONG_WATCHDOG_TIME = 20 * 60 * 1000; -constexpr int32_t INIT_DUMP_SET_CPU = 0; -constexpr int32_t INVALID_VALUE = -1; -constexpr int32_t DUMP_SET_MAX_COUNT_LIMIT = 100; -static int32_t g_lastWatchdogTime = WATCHDOG_TIME; +inline constexpr int32_t MAX_RUNNING_COUNT = 3; +inline const int32_t STANDBY_MAX_RUNNING_COUNT = 2 * MAX_RUNNING_COUNT; +inline constexpr uint32_t MAX_WORK_COUNT_PER_UID = 10; +inline constexpr int32_t DELAY_TIME_LONG = 30000; +inline constexpr int32_t DELAY_TIME_SHORT = 5000; +inline constexpr uint32_t MAX_WATCHDOG_ID = 1000; +inline constexpr uint32_t INIT_WATCHDOG_ID = 1; +inline constexpr int32_t INIT_DUMP_SET_MEMORY = -1; +inline constexpr int32_t WATCHDOG_TIME = 2 * 60 * 1000; +inline constexpr int32_t MEDIUM_WATCHDOG_TIME = 10 * 60 * 1000; +inline constexpr int32_t LONG_WATCHDOG_TIME = 20 * 60 * 1000; +inline constexpr int32_t INIT_DUMP_SET_CPU = 0; +inline constexpr int32_t INVALID_VALUE = -1; +inline constexpr int32_t DUMP_SET_MAX_COUNT_LIMIT = 100; +inline static int32_t g_lastWatchdogTime = WATCHDOG_TIME; // services\native\src\work_status.cpp -const std::string OK = "1"; -const std::string NOT_OK = "0"; +inline const std::string OK = "1"; +inline const std::string NOT_OK = "0"; // watchdog timeout threshold -constexpr int32_t WATCHDOG_TIMEOUT_THRESHOLD_MS = 500; +inline constexpr int32_t WATCHDOG_TIMEOUT_THRESHOLD_MS = 500; // services\native\src\conditions\screen_listener.cpp -constexpr int MIN_DEEP_IDLE_SCREEN_OFF_TIME_MIN = 31 * 60 * 1000; -} +inline constexpr int MIN_DEEP_IDLE_SCREEN_OFF_TIME_MIN = 31 * 60 * 1000; + +inline const std::string CONNECT_ABILITY = "CONNECT_ABILITY"; +inline const std::string DISCONNECT_ABILITY = "DISCONNECT_ABILITY"; +inline const std::string WATCHDOG_TIMEOUT = "WATCHDOG_TIMEOUT"; +inline const std::string LOAD_WORK = "LOAD_WORK"; +inline const std::string SERVICE_INIT = "SERVICE_INIT"; +inline const std::string LOAD_SA = "LOAD_SA"; +} // namespace WorkScheduler +} // namespace OHOS #endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_CONSTANTS_H \ No newline at end of file diff --git a/utils/native/include/work_sched_hisysevent_report.h b/utils/native/include/work_sched_hisysevent_report.h new file mode 100644 index 0000000..0d56f02 --- /dev/null +++ b/utils/native/include/work_sched_hisysevent_report.h @@ -0,0 +1,56 @@ +/* + * 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 FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HISYSEVENT_REPORT_H +#define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HISYSEVENT_REPORT_H + +#include "work_sched_system_policy.h" + +namespace OHOS { +namespace WorkScheduler { +namespace WorkSchedUtil { + /** + * @brief Send a deep idle state changed event to the system. + * + * @param deepIdleState Whether the system is in deep idle state. + */ + void HiSysEventDeepIdleState(bool deepIdleState); + + /** + * @brief Send a device standby state changed event to the system. + * + * @param deviceStandbyState Whether the system is in device standby state. + */ + void HiSysEventDeviceStandbyState(bool deviceStandbyState); + + /** + * @brief Send a system policy limit event to the system. + * + * @param systemPolicy The system policy limit. + */ + void HiSysEventSystemPolicyLimit(WorkSchedSystemPolicy& systemPolicy); + + /** + * @brief Send a exception event to the system. + * + * @param moduleName the module name of the exception. + * @param funcName the function name of the exception. + * @param exceptionInfo the exception info. + */ + void HiSysEventException(const std::string& moduleName, const std::string& funcName, const std::string& exceptionInfo); +} // namespace WorkSchedUtil +} // namespace WorkScheduler +} // namespace OHOS +#endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_UTILS_HISYSEVENT_REPORT_H \ No newline at end of file diff --git a/utils/native/include/work_sched_system_policy.h b/utils/native/include/work_sched_system_policy.h new file mode 100644 index 0000000..3e952f3 --- /dev/null +++ b/utils/native/include/work_sched_system_policy.h @@ -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. + */ + +#ifndef FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_SYSTEM_POLICY_H +#define FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_SYSTEM_POLICY_H + +#include + +namespace OHOS { +namespace WorkScheduler { +struct WorkSchedSystemPolicy { + std::string policyName; + int32_t cpuUsage; + int32_t memAvailable; + // interfaces/inner_api/native/include/thermal_level_info.h + int32_t thermalLevel; + // interfaces/inner_api/native/include/power_mode_info.h + uint32_t powerMode; + + inline std::string GetInfo() const; +}; + +std::string WorkSchedSystemPolicy::GetInfo() const +{ + return "policyName: " + policyName + ", cpuUsage: " + std::to_string(cpuUsage) + + ", memAvailable: " + std::to_string(memAvailable) + ", thermalLevel: " + std::to_string(thermalLevel) + + ", powerMode: " + std::to_string(powerMode); +} +} // namespace WorkScheduler +} // namespace OHOS +#endif // FOUNDATION_RESOURCESCHEDULE_WORKSCHEDULER_SYSTEM_POLICY_H \ No newline at end of file diff --git a/utils/native/src/work_sched_hisysevent_report.cpp b/utils/native/src/work_sched_hisysevent_report.cpp new file mode 100644 index 0000000..7e12617 --- /dev/null +++ b/utils/native/src/work_sched_hisysevent_report.cpp @@ -0,0 +1,78 @@ +/* + * 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 +#include + +#include "work_sched_utils.h" +#include "work_sched_hisysevent_report.h" + +using namespace OHOS; + +namespace OHOS { +namespace WorkScheduler { +namespace WorkSchedUtil { +void HiSysEventDeepIdleState(bool deepIdleState) +{ + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::WORK_SCHEDULER, + "DEEP_IDLE_STATE", HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", deepIdleState); +} + +void HiSysEventDeviceStandbyState(bool deviceStandbyState) +{ + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::WORK_SCHEDULER, + "DEVICE_STANDBY_STATE", HiviewDFX::HiSysEvent::EventType::STATISTIC, "STATE", deviceStandbyState); +} + +void HiSysEventSystemPolicyLimit(WorkSchedSystemPolicy& systemPolicy) +{ + HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::WORK_SCHEDULER, + "SYSTEM_POLICY_LIMIT", HiviewDFX::HiSysEvent::EventType::STATISTIC, "POLICY", systemPolicy.GetInfo()); +} + +void HiSysEventException(const std::string& moduleName, const std::string& funcName, const std::string& exceptionInfo) +{ + struct HiSysEventParam params[] = { + { + .name = "MODULE_NAME", + .t = HISYSEVENT_STRING, + .v = { .s = const_cast(moduleName.c_str()) }, + .arraySize = 0 + }, + { + .name = "FUNC_NAME", + .t = HISYSEVENT_STRING, + .v = { .s = const_cast(funcName.c_str()) }, + .arraySize = 0 + }, + { + .name = "EXCEPTION_INFO", + .t = HISYSEVENT_STRING, + .v = { .s = const_cast(exceptionInfo.c_str()) }, + .arraySize = 0 + }, + }; + + OH_HiSysEvent_Write( + HiviewDFX::HiSysEvent::Domain::WORK_SCHEDULER, + "WORK_SCHEDULER_RUNTIME_EXCEPTION", + HISYSEVENT_STATISTIC, + params, + sizeof(params)/sizeof(params[0]) + ); +} +} // namespace WorkSchedUtil +} // namespace WorkScheduler +} // namespace OHOS \ No newline at end of file -- Gitee