From d4a6f9ffcf7b3888b33ebf2c4dce152e8c62b598 Mon Sep 17 00:00:00 2001 From: wangzhen Date: Fri, 22 Nov 2024 09:02:12 +0800 Subject: [PATCH] Apply ffrt task id Signed-off-by: wangzhen Change-Id: If247bc7127c91c39af73a7707ca8650ccab266a2 --- .../src/ability_manager_service.cpp | 4 +-- .../src/connection_state_manager.cpp | 2 +- .../ui_ability_lifecycle_manager.cpp | 2 +- services/appmgr/src/app_mgr_service_inner.cpp | 24 +++++++++--------- .../appmgr/src/killing_process_manager.cpp | 2 +- services/common/include/task_handler_wrap.h | 8 +----- services/common/src/ffrt_task_utils_wrap.h | 5 ++++ services/common/src/task_handler_wrap.cpp | 25 +++++++++++-------- 8 files changed, 38 insertions(+), 34 deletions(-) diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index c1a32d8699..e0208e4cfe 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -427,7 +427,7 @@ void AbilityManagerService::InitPushTask() auto task = []() { ApplicationUtil::AppFwkBootEventCallback(BOOTEVENT_BOOT_COMPLETED, "true", nullptr); }; - handler->SubmitTask(task, "BootCompletedDelayTask", BOOTEVENT_COMPLETED_DELAY_TIME); + handler->SubmitTaskJust(task, "BootCompletedDelayTask", BOOTEVENT_COMPLETED_DELAY_TIME); } else { WatchParameter(BOOTEVENT_BOOT_COMPLETED, ApplicationUtil::AppFwkBootEventCallback, nullptr); TAG_LOGI(AAFwkTag::ABILITYMGR, "init call, InitPushTask suc"); @@ -9350,7 +9350,7 @@ void AbilityManagerService::InitFocusListener() } }; if (taskHandler_) { - taskHandler_->SubmitTask(registerTask, "RegisterFocusListenerTask", REGISTER_FOCUS_DELAY); + taskHandler_->SubmitTaskJust(registerTask, "RegisterFocusListenerTask", REGISTER_FOCUS_DELAY); } } diff --git a/services/abilitymgr/src/connection_state_manager.cpp b/services/abilitymgr/src/connection_state_manager.cpp index 8d66745e1d..1c93c1d601 100644 --- a/services/abilitymgr/src/connection_state_manager.cpp +++ b/services/abilitymgr/src/connection_state_manager.cpp @@ -531,7 +531,7 @@ void ConnectionStateManager::InitAppStateObserver() } self->InitAppStateObserver(); }; - handler_->SubmitTask(initConnectionStateManagerTask, "InitConnectionStateManager", DELAY_TIME); + handler_->SubmitTaskJust(initConnectionStateManagerTask, "InitConnectionStateManager", DELAY_TIME); retry_++; } return; diff --git a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp index deef872678..ee3783054e 100644 --- a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp +++ b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp @@ -1459,7 +1459,7 @@ void UIAbilityLifecycleManager::DelayCompleteTerminate(const std::shared_ptrCompleteTerminate(abilityRecord); }; int killTimeout = AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() * KILL_TIMEOUT_MULTIPLE; - handler->SubmitTask(timeoutTask, "DELAY_KILL_PROCESS", killTimeout); + handler->SubmitTaskJust(timeoutTask, "DELAY_KILL_PROCESS", killTimeout); } void UIAbilityLifecycleManager::CompleteTerminate(const std::shared_ptr &abilityRecord) diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 1b0994f6e6..b66305bd84 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -972,7 +972,7 @@ void AppMgrServiceInner::LoadAbilityNoAppRecord(const std::shared_ptrSubmitTask([appRecord, abilityInfo, pThis = shared_from_this()]() { + otherTaskHandler_->SubmitTaskJust([appRecord, abilityInfo, pThis = shared_from_this()]() { pThis->OnAppStateChanged(appRecord, ApplicationState::APP_STATE_SET_COLD_START, false, false); pThis->SendAppStartupTypeEvent(appRecord, abilityInfo, AppStartType::COLD); }, "AppStateChangedNotify", FIRST_FRAME_NOTIFY_TASK_DELAY); @@ -1665,7 +1665,7 @@ void AppMgrServiceInner::SendProcessExitEventTask( auto sendEventTask = [inner = shared_from_this(), appRecord, exitTime, count] () { inner->SendProcessExitEventTask(appRecord, exitTime, count); }; - taskHandler_->SubmitTask(sendEventTask, PROCESS_EXIT_EVENT_TASK, KILL_PROCESS_DELAYTIME_MICRO_SECONDS); + taskHandler_->SubmitTaskJust(sendEventTask, PROCESS_EXIT_EVENT_TASK, KILL_PROCESS_DELAYTIME_MICRO_SECONDS); } void AppMgrServiceInner::SendProcessExitEvent(const std::shared_ptr &appRecord) @@ -4022,7 +4022,7 @@ void AppMgrServiceInner::TerminateApplication(const std::shared_ptrSubmitTask(timeoutTask, "DelayKillProcess", AMSEventHandler::KILL_PROCESS_TIMEOUT); + taskHandler_->SubmitTaskJust(timeoutTask, "DelayKillProcess", AMSEventHandler::KILL_PROCESS_TIMEOUT); } appRunningManager_->RemoveAppRunningRecordById(appRecord->GetRecordId()); if (!GetAppRunningStateByBundleName(appRecord->GetBundleName())) { @@ -5003,7 +5003,7 @@ void AppMgrServiceInner::KillApplicationByRecord(const std::shared_ptrSubmitTask(timeoutTask, "DelayKillProcess", AMSEventHandler::KILL_PROCESS_TIMEOUT); + taskHandler_->SubmitTaskJust(timeoutTask, "DelayKillProcess", AMSEventHandler::KILL_PROCESS_TIMEOUT); } void AppMgrServiceInner::SendHiSysEvent(int32_t innerEventId, std::shared_ptr appRecord) @@ -5603,7 +5603,7 @@ void AppMgrServiceInner::InitFocusListener() } }; if (taskHandler_) { - taskHandler_->SubmitTask(registerTask, "RegisterFocusListenerTask", REGISTER_FOCUS_DELAY); + taskHandler_->SubmitTaskJust(registerTask, "RegisterFocusListenerTask", REGISTER_FOCUS_DELAY); TAG_LOGI(AAFwkTag::APPMGR, "submit registerFocusListenerTask"); } } @@ -5795,7 +5795,7 @@ void AppMgrServiceInner::InitWindowPidVisibilityChangedListener() TAG_LOGE(AAFwkTag::APPMGR, "Task handler is nullptr."); return; } - taskHandler_->SubmitTask(registerTask, "RegisterPidVisibilityListener.", REGISTER_PID_VISIBILITY_DELAY); + taskHandler_->SubmitTaskJust(registerTask, "RegisterPidVisibilityListener.", REGISTER_PID_VISIBILITY_DELAY); } void AppMgrServiceInner::FreeWindowPidVisibilityChangedListener() @@ -5992,7 +5992,7 @@ void AppMgrServiceInner::AppRecoveryNotifyApp(int32_t pid, const std::string& bu } }; constexpr int32_t timeOut = 2000; - taskHandler_->SubmitTask(waitSaveTask, timeOutName, timeOut); + taskHandler_->SubmitTaskJust(waitSaveTask, timeOutName, timeOut); } void AppMgrServiceInner::ParseInfoToAppfreeze(const FaultData &faultData, int32_t pid, int32_t uid, @@ -6079,7 +6079,7 @@ int32_t AppMgrServiceInner::SubmitDfxFaultTask(const FaultData &faultData, const auto task = [pid, innerService = shared_from_this()]() { AppExecFwk::AppfreezeManager::GetInstance()->DeleteStack(pid); }; - dfxTaskHandler_->SubmitTask(task, "DeleteStack", delayTime); + dfxTaskHandler_->SubmitTaskJust(task, "DeleteStack", delayTime); return ERR_OK; } @@ -6173,7 +6173,7 @@ int32_t AppMgrServiceInner::KillFaultApp(int32_t pid, const std::string &bundleN }; constexpr int32_t waitTime = 3500; // wait 3.5s before kill application - taskHandler_->SubmitTask(killAppTask, "killAppTask", waitTime); + taskHandler_->SubmitTaskJust(killAppTask, "killAppTask", waitTime); return ERR_OK; } @@ -6286,7 +6286,7 @@ bool AppMgrServiceInner::SetAppFreezeFilter(int32_t pid) AppExecFwk::AppfreezeManager::GetInstance()->ResetAppfreezeState(pid, bundleName); }; constexpr int32_t waitTime = 120000; // wait 2min - dfxTaskHandler_->SubmitTask(resetAppfreezeTask, "resetAppfreezeTask", waitTime); + dfxTaskHandler_->SubmitTaskJust(resetAppfreezeTask, "resetAppfreezeTask", waitTime); return cancelResult; } TAG_LOGE(AAFwkTag::APPDFR, "SetAppFreezeFilter failed, pid %{public}d calling pid %{public}d", @@ -7077,7 +7077,7 @@ void AppMgrServiceInner::ClearResidentProcessAppRunningData(const std::shared_pt } restartResidentTaskList_.emplace_back(appRecord); TAG_LOGD(AAFwkTag::APPMGR, "Post restart resident process delay task."); - taskHandler_->SubmitTask(restartProcess, "RestartResidentProcessDelayTask", RESTART_INTERVAL_TIME); + taskHandler_->SubmitTaskJust(restartProcess, "RestartResidentProcessDelayTask", RESTART_INTERVAL_TIME); } } } @@ -8325,7 +8325,7 @@ bool AppMgrServiceInner::CleanAbilityByUserRequest(const sptr &to self->DecreaseWillKillPidsNum(); TAG_LOGD(AAFwkTag::APPMGR, "pid:%{public}d killed", targetPid); }; - delayKillTaskHandler_->SubmitTask(delayKillTask, "delayKillUIAbility", delayTime); + delayKillTaskHandler_->SubmitTaskJust(delayKillTask, "delayKillUIAbility", delayTime); return true; } diff --git a/services/appmgr/src/killing_process_manager.cpp b/services/appmgr/src/killing_process_manager.cpp index 96ff39a169..3c85f72f3d 100644 --- a/services/appmgr/src/killing_process_manager.cpp +++ b/services/appmgr/src/killing_process_manager.cpp @@ -60,7 +60,7 @@ void KillingProcessManager::AddKillingCallerKey(const std::string& callerKey) auto task = [callerKey] () { KillingProcessManager::GetInstance().RemoveKillingCallerKey(callerKey); }; - taskHandler->SubmitTask(task, "clearCallerKey", CLEAR_CALLER_KEY_DELAY_TIME); + taskHandler->SubmitTaskJust(task, "clearCallerKey", CLEAR_CALLER_KEY_DELAY_TIME); } void KillingProcessManager::RemoveKillingCallerKey(const std::string& callerKey) diff --git a/services/common/include/task_handler_wrap.h b/services/common/include/task_handler_wrap.h index 50862c802c..f16c248fb8 100644 --- a/services/common/include/task_handler_wrap.h +++ b/services/common/include/task_handler_wrap.h @@ -51,10 +51,7 @@ public: { return status_ && innerTaskHandle_; } - int32_t GetTaskId() const - { - return taskId_; - } + uint64_t GetTaskId() const; bool PrintTaskLog() const { return printTaskLog_; @@ -64,7 +61,6 @@ private: std::shared_ptr innerTaskHandle_; std::shared_ptr status_; - int32_t taskId_ = 0; bool printTaskLog_ = false; }; @@ -115,8 +111,6 @@ protected: } bool RemoveTask(const std::string &name, const TaskHandle &taskHandle); protected: - static std::atomic_int32_t g_taskId; - // this is used only for compatibility std::unordered_map tasks_; std::unique_ptr tasksMutex_; diff --git a/services/common/src/ffrt_task_utils_wrap.h b/services/common/src/ffrt_task_utils_wrap.h index 08a2286fa7..9b4266dae2 100644 --- a/services/common/src/ffrt_task_utils_wrap.h +++ b/services/common/src/ffrt_task_utils_wrap.h @@ -33,6 +33,11 @@ public: { return taskHandle_; } + + uint64_t GetId() + { + return taskHandle_.get_id(); + } private: ffrt::task_handle taskHandle_; }; diff --git a/services/common/src/task_handler_wrap.cpp b/services/common/src/task_handler_wrap.cpp index 8418871e69..d9d1211cc3 100644 --- a/services/common/src/task_handler_wrap.cpp +++ b/services/common/src/task_handler_wrap.cpp @@ -58,7 +58,13 @@ void TaskHandle::Sync() const handler->WaitTaskInner(innerTaskHandle_); } -std::atomic_int32_t TaskHandlerWrap::g_taskId = 0; +uint64_t TaskHandle::GetTaskId() const +{ + if (innerTaskHandle_) { + return innerTaskHandle_->GetId(); + } + return 0; +} std::shared_ptr TaskHandlerWrap::CreateQueueHandler(const std::string &queueName, TaskQoS queueQos) @@ -144,23 +150,22 @@ TaskHandle TaskHandlerWrap::SubmitTask(const std::function &task, const } TaskHandle result(shared_from_this(), nullptr); - result.taskId_ = ++g_taskId; result.printTaskLog_ = printTaskLog_; auto taskWrap = [result, task, taskName = taskAttr.taskName_]() { if (result.PrintTaskLog()) { - TAG_LOGW(AAFwkTag::DEFAULT, "begin execute task name: %{public}s, taskId: %{public}d", - taskName.c_str(), result.GetTaskId()); + TAG_LOGW(AAFwkTag::DEFAULT, "begin execute task name: %{public}s, taskId: %{public}" PRIu64"", + taskName.c_str(), ffrt::this_task::get_id()); } *result.status_ = TaskStatus::EXECUTING; task(); *result.status_ = TaskStatus::FINISHED; }; - if (printTaskLog_) { - TAG_LOGW(AAFwkTag::DEFAULT, "submitTask: %{public}s, taskId: %{public}d, queueName: %{public}s count: " - "%{public}" PRIu64"", taskAttr.taskName_.c_str(), result.taskId_, queueName_.c_str(), GetTaskCount()); - } result.innerTaskHandle_ = SubmitTaskInner(std::move(taskWrap), taskAttr); + if (printTaskLog_) { + TAG_LOGW(AAFwkTag::DEFAULT, "submitTask: %{public}s, taskId: %{public}" PRIu64", queueName: %{public}s count: " + "%{public}" PRIu64"", taskAttr.taskName_.c_str(), result.GetTaskId(), queueName_.c_str(), GetTaskCount()); + } return result; } @@ -176,8 +181,8 @@ bool TaskHandlerWrap::CancelTask(const std::string &name) auto taskHandle = it->second; tasks_.erase(it); if (printTaskLog_) { - TAG_LOGW(AAFwkTag::DEFAULT, "cancel task name: %{public}s, taskId: %{public}d, queueName: %{public}s", - name.c_str(), taskHandle.taskId_, queueName_.c_str()); + TAG_LOGW(AAFwkTag::DEFAULT, "cancel task name: %{public}s, taskId: %{public}" PRIu64", queueName: %{public}s", + name.c_str(), taskHandle.GetTaskId(), queueName_.c_str()); } return taskHandle.Cancel(); }