1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "session/host/include/ws_ffrt_helper.h"
17
18 #include <limits>
19 #include <shared_mutex>
20 #include <unordered_map>
21
22 #include <unistd.h>
23
24 #include "c/executor_task.h"
25 #include "ffrt.h"
26 #include "ffrt_inner.h"
27 #include "hitrace_meter.h"
28 #include "window_manager_hilog.h"
29
30 namespace OHOS::Rosen {
31 namespace {
32 constexpr int32_t FFRT_USER_INTERACTIVE_MAX_THREAD_NUM = 5;
33 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "WSFFRTHelper"};
34 const std::unordered_map<TaskQos, ffrt::qos> FFRT_QOS_MAP = {
35 { TaskQos::INHERIT, ffrt_qos_inherit },
36 { TaskQos::BACKGROUND, ffrt_qos_background },
37 { TaskQos::UTILITY, ffrt_qos_utility },
38 { TaskQos::DEFAULT, ffrt_qos_default },
39 { TaskQos::USER_INITIATED, ffrt_qos_user_initiated },
40 { TaskQos::DEADLINE_REQUEST, ffrt_qos_deadline_request },
41 { TaskQos::USER_INTERACTIVE, ffrt_qos_user_interactive },
42 };
43 }
44
45 class TaskHandleMap {
46 public:
SaveTask(const std::string & taskName,ffrt::task_handle handle)47 void SaveTask(const std::string& taskName, ffrt::task_handle handle)
48 {
49 std::unique_lock<std::shared_mutex> lock(mutex_);
50 taskMap_[taskName] = std::move(handle);
51 }
52
RemoveTask(const std::string & taskName)53 void RemoveTask(const std::string& taskName)
54 {
55 std::unique_lock<std::shared_mutex> lock(mutex_);
56 if (auto iter = taskMap_.find(taskName); iter != taskMap_.end()) {
57 if (iter->second != nullptr) {
58 auto ret = ffrt::skip(iter->second);
59 if (ret != 0) {
60 WLOGI("Failed to cancel task, taskName=%{public}s, retcode=%{public}d", taskName.c_str(), ret);
61 }
62 }
63 taskMap_.erase(iter);
64 } else {
65 WLOGI("Task is not existed, taskName=%{public}s", taskName.c_str());
66 }
67 }
68
IsTaskExisted(const std::string & taskName)69 bool IsTaskExisted(const std::string& taskName)
70 {
71 std::shared_lock<std::shared_mutex> lock(mutex_);
72 auto iter = taskMap_.find(taskName);
73 return iter != taskMap_.end() && iter->second != nullptr;
74 }
75
CountTask()76 std::size_t CountTask()
77 {
78 std::shared_lock<std::shared_mutex> lock(mutex_);
79 return taskMap_.size();
80 }
81
82 private:
83 std::unordered_map<std::string, ffrt::task_handle> taskMap_;
84 std::shared_mutex mutex_;
85 };
86
WSFFRTHelper()87 WSFFRTHelper::WSFFRTHelper() : taskHandleMap_(std::make_unique<TaskHandleMap>())
88 {
89 ffrtQueue_ = std::make_unique<ffrt::queue>(ffrt::queue_concurrent, "WSFFRTHelper",
90 ffrt::queue_attr().qos(ffrt_qos_user_interactive).max_concurrency(FFRT_USER_INTERACTIVE_MAX_THREAD_NUM));
91 TLOGI(WmsLogTag::WMS_MAIN, "FFRT user interactive qos max queue thread number: %{public}d",
92 FFRT_USER_INTERACTIVE_MAX_THREAD_NUM);
93 }
94
95 WSFFRTHelper::~WSFFRTHelper() = default;
96
SubmitTask(std::function<void ()> && task,const std::string & taskName,uint64_t delayTime,TaskQos qos)97 void WSFFRTHelper::SubmitTask(std::function<void()>&& task, const std::string& taskName, uint64_t delayTime,
98 TaskQos qos)
99 {
100 auto localTask = [task = std::move(task), taskName]() {
101 HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "f:%s", taskName.c_str());
102 task();
103 };
104 if (delayTime == 0 && ffrt_get_cur_task() != nullptr) {
105 localTask();
106 return;
107 }
108 ffrt::task_handle handle = ffrtQueue_->submit_h(std::move(localTask));
109 if (handle == nullptr) {
110 WLOGE("Failed to post task, taskName=%{public}s", taskName.c_str());
111 return;
112 }
113 taskHandleMap_->SaveTask(taskName, std::move(handle));
114 }
115
CancelTask(const std::string & taskName)116 void WSFFRTHelper::CancelTask(const std::string& taskName)
117 {
118 taskHandleMap_->RemoveTask(taskName);
119 }
120
IsTaskExisted(const std::string & taskName) const121 bool WSFFRTHelper::IsTaskExisted(const std::string& taskName) const
122 {
123 return taskHandleMap_->IsTaskExisted(taskName);
124 }
125
CountTask() const126 std::size_t WSFFRTHelper::CountTask() const
127 {
128 return taskHandleMap_->CountTask();
129 }
130 } // namespace OHOS::Rosen
131