• 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 #ifdef USE_OHOS_QOS
28 #include "qos.h"
29 #else
30 #include "staging_qos/sched/qos.h"
31 #endif
32 #include "dfx/trace/ffrt_trace.h"
33 #include "sched/scheduler.h"
34 #include "eu/execute_unit.h"
35 #include "core/entity.h"
36 #include "dfx/watchdog/watchdog_util.h"
37 #include "dfx/trace_record/ffrt_trace_record.h"
38 #include "tm/cpu_task.h"
39 
40 namespace ffrt {
41 #define OFFSETOF(TYPE, MEMBER) (reinterpret_cast<size_t>(&((reinterpret_cast<TYPE *>(0))->MEMBER)))
42 
CheckOutsHandle(const ffrt_deps_t * outs)43 inline bool CheckOutsHandle(const ffrt_deps_t* outs)
44 {
45     if (outs == nullptr) {
46         return true;
47     }
48     for (uint32_t i = 0; i < outs->len; i++) {
49         if ((outs->items[i].type) == ffrt_dependence_task) {
50             FFRT_LOGE("handle can't be used as out dependence");
51             return false;
52         }
53     }
54     return true;
55 }
OutsDedup(std::vector<const void * > & outsNoDup,const ffrt_deps_t * outs)56 inline void OutsDedup(std::vector<const void *>& outsNoDup, const ffrt_deps_t* outs)
57 {
58     for (uint32_t i = 0; i < outs->len; i++) {
59         if (std::find(outsNoDup.begin(), outsNoDup.end(), outs->items[i].ptr) == outsNoDup.end()) {
60             outsNoDup.push_back(outs->items[i].ptr);
61         }
62     }
63 }
64 
InsDedup(std::vector<CPUEUTask * > & in_handles,std::vector<const void * > & insNoDup,std::vector<const void * > & outsNoDup,const ffrt_deps_t * ins)65 inline void InsDedup(std::vector<CPUEUTask*> &in_handles, std::vector<const void *> &insNoDup,
66     std::vector<const void *> &outsNoDup, const ffrt_deps_t *ins)
67 {
68     for (uint32_t i = 0; i < ins->len; i++) {
69         if (std::find(outsNoDup.begin(), outsNoDup.end(), ins->items[i].ptr) == outsNoDup.end()) {
70             if ((ins->items[i].type == ffrt_dependence_task) && (ins->items[i].ptr != nullptr)) {
71                 static_cast<ffrt::CPUEUTask*>(const_cast<void*>(ins->items[i].ptr))->IncDeleteRef();
72                 in_handles.emplace_back(static_cast<ffrt::CPUEUTask*>(const_cast<void*>(ins->items[i].ptr)));
73             }
74             insNoDup.push_back(ins->items[i].ptr);
75         }
76     }
77 }
78 
79 class DependenceManager : public NonCopyable {
80 public:
81     static DependenceManager& Instance();
82 
83     static void RegistInsCb(SingleInsCB<DependenceManager>::Instance &&cb);
84 
85     virtual void onSubmit(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) = 0;
87 
88     void onSubmitUV(ffrt_executor_task_t *task, const task_attr_private *attr);
89 
90     void onSubmitIO(const ffrt_io_callable_t &work, const task_attr_private *attr);
91 
92     virtual void onWait() = 0;
93 
94     virtual void onWait(const ffrt_deps_t* deps) = 0;
95 
96     virtual int onExecResults(ffrt_task_handle_t handle) = 0;
97 
98     virtual void onTaskDone(CPUEUTask* task) = 0;
99 
100     virtual int onSkip(ffrt_task_handle_t handle);
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_wraper;
106         return root_wraper.Root();
107     }
108 
109 protected:
DependenceManager()110     DependenceManager() {}
~DependenceManager()111     virtual ~DependenceManager() {}
112 };
113 
114 } // namespace ffrt
115 #endif
116