From b7b89678eb8bef1272c5a4896a8a71f35dec45ef Mon Sep 17 00:00:00 2001 From: hxf Date: Mon, 5 May 2025 10:17:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BB=B6=E8=BF=9F=E4=BB=BB=E5=8A=A1dump?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E9=80=9A=E8=BF=87uid=E3=80=81workid=E6=8B=89?= =?UTF-8?q?=E8=B5=B7=E5=8D=95=E4=B8=AA=E4=BB=BB=E5=8A=A1=20Signed-off-by:?= =?UTF-8?q?=20hxf=20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/native/include/work_policy_manager.h | 15 +++++ .../native/include/work_scheduler_service.h | 1 + services/native/src/work_policy_manager.cpp | 33 ++++++++++ .../native/src/work_scheduler_service.cpp | 27 +++++++- .../test/src/work_policy_manager_test.cpp | 61 +++++++++++++++++++ .../test/src/workschedulerservice_test.cpp | 60 ++++++++++++++++++ 6 files changed, 195 insertions(+), 2 deletions(-) diff --git a/services/native/include/work_policy_manager.h b/services/native/include/work_policy_manager.h index d9eef0f..271423e 100644 --- a/services/native/include/work_policy_manager.h +++ b/services/native/include/work_policy_manager.h @@ -105,6 +105,14 @@ public: * @return The uid queue map. */ std::shared_ptr FindWorkStatus(WorkInfo& workInfo, int32_t uid); + /** + * @brief Find work status. + * + * @param uId The uId. + * @param workId The workId. + * @return The workStatus of workId. + */ + std::shared_ptr FindWorkStatus(int32_t uId, int32_t workId); /** * @brief Find SA. * @@ -225,6 +233,13 @@ public: * @param abilityName The abilityName. */ void DumpCheckIdeWorkToRun(const std::string &bundleName, const std::string &abilityName); + /** + * @brief Provide a command to exec onStart and onStop of the matched service extension. + * @param uId The uId. + * @param workId The workId. + * @param result The result. + */ + void DumpTriggerWork(int32_t uId, int32_t workId, std::string& result); /** * @brief Set cpu by dump. * diff --git a/services/native/include/work_scheduler_service.h b/services/native/include/work_scheduler_service.h index bd84725..6d4f685 100644 --- a/services/native/include/work_scheduler_service.h +++ b/services/native/include/work_scheduler_service.h @@ -354,6 +354,7 @@ private: int32_t SetTimer(); void CancelTimer(int32_t id); bool CheckCallingToken(); + void DumpTriggerWork(const std::string& uIdStr, const std::string& workIdStr, std::string& result); private: std::set whitelist_; diff --git a/services/native/src/work_policy_manager.cpp b/services/native/src/work_policy_manager.cpp index 9773cce..0a7880e 100644 --- a/services/native/src/work_policy_manager.cpp +++ b/services/native/src/work_policy_manager.cpp @@ -189,6 +189,16 @@ shared_ptr WorkPolicyManager::FindWorkStatus(WorkInfo& workInfo, int return nullptr; } +shared_ptr WorkPolicyManager::FindWorkStatus(int32_t uId, int32_t workId) +{ + WS_HILOGD("Find work status start."); + std::lock_guard lock(uidMapMutex_); + if (uidQueueMap_.count(uId) > 0) { + return uidQueueMap_.at(uId)->Find(WorkStatus::MakeWorkId(workId, uId)); + } + return nullptr; +} + shared_ptr WorkPolicyManager::FindSA(int32_t saId, int32_t uid) { WS_HILOGD("Find SA, saId:%{public}d, uid:%{public}d", saId, uid); @@ -737,6 +747,29 @@ void WorkPolicyManager::DumpCheckIdeWorkToRun(const std::string &bundleName, con SendIdeWorkRetriggerEvent(0); } +void WorkPolicyManager::DumpTriggerWork(int32_t uId, int32_t workId, std::string& result) +{ + std::lock_guard lock(ideDebugListMutex_); + std::shared_ptr workStatus = FindWorkStatus(uId, workId); + if (workStatus == nullptr) { + result.append("the work is not exist\n"); + return; + } + if (workStatus->IsRunning()) { + result.append("the work is running\n"); + return; + } + workStatus->MarkStatus(WorkStatus::Status::RUNNING); + bool ret = workConnManager_->StartWork(workStatus); + if (ret) { + result.append("the work trigger ok\n"); + AddWatchdogForWork(workStatus); + } else { + result.append("the work trigger error\n"); + workStatus->MarkStatus(WorkStatus::Status::WAIT_CONDITION); + } +} + void WorkPolicyManager::TriggerIdeWork() { std::lock_guard lock(ideDebugListMutex_); diff --git a/services/native/src/work_scheduler_service.cpp b/services/native/src/work_scheduler_service.cpp index 717c5f3..81649af 100644 --- a/services/native/src/work_scheduler_service.cpp +++ b/services/native/src/work_scheduler_service.cpp @@ -996,6 +996,8 @@ void WorkSchedulerService::DumpProcessForEngMode(std::vector &argsI DumpLoadSaWorks(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); } else if (argsInStr[DUMP_OPTION] == "-g") { DumpGetWorks(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); + } else if (argsInStr[DUMP_OPTION] == "-f") { + DumpTriggerWork(argsInStr[DUMP_PARAM_INDEX], argsInStr[DUMP_VALUE_INDEX], result); } else { result.append("Error params."); } @@ -1056,7 +1058,8 @@ void WorkSchedulerService::DumpUsage(std::string &result) .append(" -a: show all info.\n") .append(" -d event info: show the event info.\n") .append(" -d (eventType) (TypeValue): publish the event.\n") - .append(" -t (bundleName) (abilityName): trigger the work.\n") + .append(" -t (bundleName) (abilityName): trigger the bundleName all works.\n") + .append(" -f (uId) (workId): trigger the work.\n") .append(" -x (uid) (option): pause or resume the work.\n") .append(" -memory (number): set the available memory.\n") .append(" -watchdog_time (number): set watch dog time, default 120000.\n") @@ -1064,7 +1067,7 @@ void WorkSchedulerService::DumpUsage(std::string &result) .append(" -min_interval (number): set min interval time, set 0 means close test mode.\n") .append(" -cpu (number): set the usage cpu.\n") .append(" -count (number): set the max running task count.\n") - .append(" -s (number) (number): load or report sa.\n"); + .append(" -s (saId) (uId): load or report sa.\n"); } void WorkSchedulerService::DumpAllInfo(std::string &result) @@ -1121,6 +1124,26 @@ void WorkSchedulerService::DumpProcessWorks(const std::string &bundleName, const workPolicyManager_->DumpCheckIdeWorkToRun(bundleName, abilityName); } +void WorkSchedulerService::DumpTriggerWork(const std::string& uIdStr, const std::string& workIdStr, std::string& result) +{ + if (uIdStr.empty() || workIdStr.empty() || !std::all_of(uIdStr.begin(), uIdStr.end(), ::isdigit) + || !std::all_of(workIdStr.begin(), workIdStr.end(), ::isdigit)) { + result.append("param invalid\n"); + return; + } + int32_t uId = std::atoi(uIdStr.c_str()); + if (uId <= 0) { + result.append("uIdStr param invalid, uIdStr:" + uIdStr + "\n"); + return; + } + int32_t workId = std::atoi(workIdStr.c_str()); + if (workId <= 0) { + result.append("workIdStr param invalid, workIdStr:" + workIdStr + "\n"); + return; + } + workPolicyManager_->DumpTriggerWork(uId, workId, result); +} + void WorkSchedulerService::DumpRunningWorks(const std::string &uidStr, const std::string &option, std::string &result) { if (!std::all_of(uidStr.begin(), uidStr.end(), ::isdigit) || option.empty()) { diff --git a/services/test/src/work_policy_manager_test.cpp b/services/test/src/work_policy_manager_test.cpp index 200b699..1eea7a5 100644 --- a/services/test/src/work_policy_manager_test.cpp +++ b/services/test/src/work_policy_manager_test.cpp @@ -1049,5 +1049,66 @@ HWTEST_F(WorkPolicyManagerTest, GetDeepIdleWorks_001, TestSize.Level1) std::list> ret = workPolicyManager_->GetDeepIdleWorks(); EXPECT_TRUE(ret.size() > 0); } + +/** + * @tc.name: DumpTriggerWork_001 + * @tc.desc: Test WorkPolicyManagerTest DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, DumpTriggerWork_001, TestSize.Level1) +{ + int32_t uId = 1; + int32_t workId = 1; + std::string result; + workPolicyManager_->DumpTriggerWork(uId, workId, result); + EXPECT_EQ(result, "the work is not exist\n"); +} + +/** + * @tc.name: DumpTriggerWork_002 + * @tc.desc: Test WorkPolicyManagerTest DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, DumpTriggerWork_002, TestSize.Level1) +{ + workPolicyManager_->uidQueueMap_.clear(); + int32_t uId = 1; + int32_t workId = 1; + WorkInfo workinfo; + workinfo.SetWorkId(workId); + workinfo.RequestBatteryStatus(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW); + workinfo.RequestBatteryLevel(80); + std::shared_ptr workStatus = std::make_shared(workinfo, uId); + workStatus->MarkStatus(WorkStatus::RUNNING); + int32_t ret = workPolicyManager_->AddWork(workStatus, uId); + std::string result; + workPolicyManager_->DumpTriggerWork(uId, workId, result); + EXPECT_EQ(result, "the work is running\n"); +} + +/** + * @tc.name: DumpTriggerWork_003 + * @tc.desc: Test WorkPolicyManagerTest DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: I9J0A7 + */ +HWTEST_F(WorkPolicyManagerTest, DumpTriggerWork_003, TestSize.Level1) +{ + workPolicyManager_->uidQueueMap_.clear(); + int32_t uId = 1; + int32_t workId = 1; + WorkInfo workinfo; + workinfo.SetWorkId(workId); + workinfo.RequestBatteryStatus(WorkCondition::BatteryStatus::BATTERY_STATUS_LOW); + workinfo.RequestBatteryLevel(80); + std::shared_ptr workStatus = std::make_shared(workinfo, uId); + workStatus->MarkStatus(WorkStatus::WAIT_CONDITION); + int32_t ret = workPolicyManager_->AddWork(workStatus, uId); + std::string result; + workPolicyManager_->DumpTriggerWork(uId, workId, result); + EXPECT_EQ(result, "the work trigger error\n"); +} } } \ No newline at end of file diff --git a/services/test/src/workschedulerservice_test.cpp b/services/test/src/workschedulerservice_test.cpp index 4b20368..e566183 100644 --- a/services/test/src/workschedulerservice_test.cpp +++ b/services/test/src/workschedulerservice_test.cpp @@ -1209,5 +1209,65 @@ HWTEST_F(WorkSchedulerServiceTest, LoadMinRepeatTimeFromFile_002, TestSize.Level workSchedulerService_->LoadMinRepeatTimeFromFile(path); EXPECT_TRUE(workSchedulerService_->specialMap_.empty()); } + +/** + * @tc.name: DumpTriggerWork_001 + * @tc.desc: Test WorkSchedulerService DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: IBB60P + */ +HWTEST_F(WorkSchedulerServiceTest, DumpTriggerWork_001, TestSize.Level1) +{ + std::string uIdStr = ""; + std::string workIdStr = "123"; + std::string result; + workSchedulerService_->DumpTriggerWork(uIdStr, workIdStr, result); + EXPECT_EQ(result, "param invalid\n"); +} + +/** + * @tc.name: DumpTriggerWork_002 + * @tc.desc: Test WorkSchedulerService DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: IBB60P + */ +HWTEST_F(WorkSchedulerServiceTest, DumpTriggerWork_002, TestSize.Level1) +{ + std::string uIdStr = "0"; + std::string workIdStr = "123"; + std::string result; + workSchedulerService_->DumpTriggerWork(uIdStr, workIdStr, result); + EXPECT_EQ(result, "uIdStr param invalid, uIdStr:0\n"); +} + +/** + * @tc.name: DumpTriggerWork_003 + * @tc.desc: Test WorkSchedulerService DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: IBB60P + */ +HWTEST_F(WorkSchedulerServiceTest, DumpTriggerWork_003, TestSize.Level1) +{ + std::string uIdStr = "123"; + std::string workIdStr = "0"; + std::string result; + workSchedulerService_->DumpTriggerWork(uIdStr, workIdStr, result); + EXPECT_EQ(result, "workIdStr param invalid, workIdStr:0\n"); +} + +/** + * @tc.name: DumpTriggerWork_004 + * @tc.desc: Test WorkSchedulerService DumpTriggerWork. + * @tc.type: FUNC + * @tc.require: IBB60P + */ +HWTEST_F(WorkSchedulerServiceTest, DumpTriggerWork_004, TestSize.Level1) +{ + std::string uIdStr = "123"; + std::string workIdStr = "456"; + std::string result; + workSchedulerService_->DumpTriggerWork(uIdStr, workIdStr, result); + EXPECT_EQ(result, "the work is not exist\n"); +} } } \ No newline at end of file -- Gitee