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