1 /*
2 * Copyright (c) 2023 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 #ifndef FFRT_DEPENDENCE_MANAGER_H
17 #define FFRT_DEPENDENCE_MANAGER_H
18 #include <unordered_map>
19 #include <vector>
20 #include <string>
21 #include <mutex>
22 #include <shared_mutex>
23 #include "internal_inc/types.h"
24 #include "internal_inc/osal.h"
25 #include "core/version_ctx.h"
26 #include "sched/execute_ctx.h"
27 #include "qos.h"
28 #include "ffrt_trace.h"
29 #include "sched/task_state.h"
30 #include "sched/scheduler.h"
31 #include "eu/execute_unit.h"
32 #include "core/entity.h"
33 #include "tm/cpu_task.h"
34 #ifdef FFRT_IO_TASK_SCHEDULER
35 #include "sync/poller.h"
36 #endif
37
38 namespace ffrt {
39 #define OFFSETOF(TYPE, MEMBER) (reinterpret_cast<size_t>(&((reinterpret_cast<TYPE *>(0))->MEMBER)))
40
outsDeDup(std::vector<const void * > & outsNoDup,const ffrt_deps_t * outs)41 inline bool outsDeDup(std::vector<const void *> &outsNoDup, const ffrt_deps_t *outs)
42 {
43 for (uint32_t i = 0; i < outs->len; i++) {
44 if (std::find(outsNoDup.begin(), outsNoDup.end(), outs->items[i].ptr) == outsNoDup.end()) {
45 if ((outs->items[i].type) == ffrt_dependence_task) {
46 FFRT_LOGE("handle can't be used as out dependence");
47 return false;
48 }
49 outsNoDup.push_back(outs->items[i].ptr);
50 }
51 }
52 return true;
53 }
54
insDeDup(std::vector<CPUEUTask * > & in_handles,std::vector<const void * > & insNoDup,std::vector<const void * > & outsNoDup,const ffrt_deps_t * ins)55 inline void insDeDup(std::vector<CPUEUTask*> &in_handles, std::vector<const void *> &insNoDup,
56 std::vector<const void *> &outsNoDup, const ffrt_deps_t *ins)
57 {
58 for (uint32_t i = 0; i < ins->len; i++) {
59 if (std::find(outsNoDup.begin(), outsNoDup.end(), ins->items[i].ptr) == outsNoDup.end()) {
60 if ((ins->items[i].type) == ffrt_dependence_task) {
61 ((ffrt::CPUEUTask*)(ins->items[i].ptr))->IncDeleteRef();
62 in_handles.emplace_back((ffrt::CPUEUTask*)(ins->items[i].ptr));
63 }
64 insNoDup.push_back(ins->items[i].ptr);
65 }
66 }
67 }
68
69 class DependenceManager : public NonCopyable {
70 public:
71 static DependenceManager& Instance();
72
73 static void RegistInsCb(SingleInsCB<DependenceManager>::Instance &&cb);
74
onSubmit(bool has_handle,ffrt_task_handle_t & handle,ffrt_function_header_t * f,const ffrt_deps_t * ins,const ffrt_deps_t * outs,const task_attr_private * attr)75 virtual void onSubmit(bool has_handle, ffrt_task_handle_t &handle, ffrt_function_header_t *f,
76 const ffrt_deps_t *ins, const ffrt_deps_t *outs, const task_attr_private *attr)
77 {
78 }
79
onSubmitUV(ffrt_executor_task_t * task,const task_attr_private * attr)80 virtual void onSubmitUV(ffrt_executor_task_t* task, const task_attr_private* attr)
81 {
82 }
83
onWait()84 virtual void onWait()
85 {
86 }
87
88 #ifdef QOS_DEPENDENCY
89 virtual void onWait(const ffrt_deps_t* deps, int64_t deadline = -1)
90 {
91 }
92 #else
onWait(const ffrt_deps_t * deps)93 virtual void onWait(const ffrt_deps_t* deps)
94 {
95 }
96 #endif
97
onTaskDone(CPUEUTask * task)98 virtual void onTaskDone(CPUEUTask* task)
99 {
100 }
101
Root()102 static inline CPUEUTask* Root()
103 {
104 // Within an ffrt process, different threads may have different QoS interval
105 thread_local static RootTaskCtxWrapper root_wrapper;
106 return root_wrapper.Root();
107 }
108
109 protected:
DependenceManager()110 DependenceManager() {}
~DependenceManager()111 virtual ~DependenceManager() {}
112 };
113 } // namespace ffrt
114 #endif
115