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 "dfx/watchdog/watchdog_util.h"
34 #include "dfx/trace_record/ffrt_trace_record.h"
35 #include "tm/cpu_task.h"
36 #include "sync/poller.h"
37
38 namespace ffrt {
39 #define OFFSETOF(TYPE, MEMBER) (reinterpret_cast<size_t>(&((reinterpret_cast<TYPE *>(0))->MEMBER)))
40
CheckOutsHandle(const ffrt_deps_t * outs)41 inline bool CheckOutsHandle(const ffrt_deps_t* outs)
42 {
43 if (outs == nullptr) {
44 return true;
45 }
46 for (uint32_t i = 0; i < outs->len; i++) {
47 if ((outs->items[i].type) == ffrt_dependence_task) {
48 FFRT_LOGE("handle can't be used as out dependence");
49 return false;
50 }
51 }
52 return true;
53 }
OutsDedup(std::vector<const void * > & outsNoDup,const ffrt_deps_t * outs)54 inline void OutsDedup(std::vector<const void *>& outsNoDup, const ffrt_deps_t* outs)
55 {
56 for (uint32_t i = 0; i < outs->len; i++) {
57 if (std::find(outsNoDup.begin(), outsNoDup.end(), outs->items[i].ptr) == outsNoDup.end()) {
58 outsNoDup.push_back(outs->items[i].ptr);
59 }
60 }
61 }
62
InsDedup(std::vector<CPUEUTask * > & in_handles,std::vector<const void * > & insNoDup,std::vector<const void * > & outsNoDup,const ffrt_deps_t * ins)63 inline void InsDedup(std::vector<CPUEUTask*> &in_handles, std::vector<const void *> &insNoDup,
64 std::vector<const void *> &outsNoDup, const ffrt_deps_t *ins)
65 {
66 for (uint32_t i = 0; i < ins->len; i++) {
67 if (std::find(outsNoDup.begin(), outsNoDup.end(), ins->items[i].ptr) == outsNoDup.end()) {
68 if ((ins->items[i].type) == ffrt_dependence_task) {
69 ((ffrt::CPUEUTask*)(ins->items[i].ptr))->IncDeleteRef();
70 in_handles.emplace_back((ffrt::CPUEUTask*)(ins->items[i].ptr));
71 }
72 insNoDup.push_back(ins->items[i].ptr);
73 }
74 }
75 }
76
77 class DependenceManager : public NonCopyable {
78 public:
79 static DependenceManager& Instance();
80
81 static void RegistInsCb(SingleInsCB<DependenceManager>::Instance &&cb);
82
83 virtual void onSubmit(bool has_handle, ffrt_task_handle_t &handle, ffrt_function_header_t *f,
84 const ffrt_deps_t *ins, const ffrt_deps_t *outs, const task_attr_private *attr) = 0;
85
onSubmitUV(ffrt_executor_task_t * task,const task_attr_private * attr)86 void onSubmitUV(ffrt_executor_task_t *task, const task_attr_private *attr)
87 {
88 FFRT_EXECUTOR_TASK_SUBMIT_MARKER(task);
89 FFRT_TRACE_SCOPE(1, onSubmitUV);
90 QoS qos = ((attr == nullptr || attr->qos_ == qos_inherit) ? QoS() : QoS(attr->qos_));
91 FFRTTraceRecord::TaskSubmit<ffrt_uv_task>(qos);
92 LinkedList* node = reinterpret_cast<LinkedList *>(&task->wq);
93 FFRTScheduler* sch = FFRTScheduler::Instance();
94 if (!sch->InsertNode(node, qos)) {
95 FFRT_LOGE("Submit UV task failed!");
96 return;
97 }
98 FFRTTraceRecord::TaskEnqueue<ffrt_uv_task>(qos);
99 }
100
101 virtual void onWait() = 0;
102 #ifdef QOS_DEPENDENCY
103 virtual void onWait(const ffrt_deps_t* deps, int64_t deadline = -1) = 0;
104 #else
105 virtual void onWait(const ffrt_deps_t* deps) = 0;
106 #endif
107
108 virtual int onExecResults(const ffrt_deps_t *deps) = 0;
109
110 virtual void onTaskDone(CPUEUTask* task) = 0;
111
Root()112 static inline CPUEUTask* Root()
113 {
114 // Within an ffrt process, different threads may have different QoS interval
115 thread_local static RootTaskCtxWrapper root_wraper;
116 return root_wraper.Root();
117 }
118
119 protected:
DependenceManager()120 DependenceManager() {}
~DependenceManager()121 virtual ~DependenceManager() {}
122 };
123
124 } // namespace ffrt
125 #endif
126