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     static bool firstInit = [] {
90         int ret = ffrt_set_cpu_worker_max_num(ffrt_qos_user_interactive, FFRT_USER_INTERACTIVE_MAX_THREAD_NUM);
91         WLOGI("FFRT user interactive qos max thread number = %{public}d, retcode = %{public}d",
92             FFRT_USER_INTERACTIVE_MAX_THREAD_NUM, ret);
93         return true;
94     }();
95 }
96 
97 WSFFRTHelper::~WSFFRTHelper() = default;
98 
SubmitTask(std::function<void ()> && task,const std::string & taskName,uint64_t delayTime,TaskQos qos)99 void WSFFRTHelper::SubmitTask(std::function<void()>&& task, const std::string& taskName, uint64_t delayTime,
100     TaskQos qos)
101 {
102     auto localTask = [task = std::move(task), taskName]() {
103         HITRACE_METER_FMT(HITRACE_TAG_WINDOW_MANAGER, "f:%s", taskName.c_str());
104         task();
105     };
106     if (delayTime == 0 && ffrt_get_cur_task() != nullptr) {
107         localTask();
108         return;
109     }
110     ffrt::task_handle handle = ffrt::submit_h(std::move(localTask), {}, {}, ffrt::task_attr().delay(delayTime).
111         qos(FFRT_QOS_MAP.at(qos)));
112     if (handle == nullptr) {
113         WLOGE("Failed to post task, taskName = %{public}s", taskName.c_str());
114         return;
115     }
116     taskHandleMap_->SaveTask(taskName, std::move(handle));
117 }
118 
CancelTask(const std::string & taskName)119 void WSFFRTHelper::CancelTask(const std::string& taskName)
120 {
121     taskHandleMap_->RemoveTask(taskName);
122 }
123 
IsTaskExisted(const std::string & taskName) const124 bool WSFFRTHelper::IsTaskExisted(const std::string& taskName) const
125 {
126     return taskHandleMap_->IsTaskExisted(taskName);
127 }
128 
CountTask() const129 std::size_t WSFFRTHelper::CountTask() const
130 {
131     return taskHandleMap_->CountTask();
132 }
133 } // namespace OHOS::Rosen
134