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_LOAD_TRACKING_H
17 #define FFRT_LOAD_TRACKING_H
18 
19 #include <list>
20 #include <vector>
21 #include <thread>
22 #include <chrono>
23 #include <unordered_map>
24 
25 namespace ffrt {
26 class CPUEUTask;
27 class DefaultInterval;
28 
29 enum class TaskSwitchState {
30     BEGIN,
31     UPDATE,
32     END,
33 };
34 
35 struct TaskSwitchRecord {
36     uint64_t load;
37     TaskSwitchState state;
38     std::chrono::time_point<std::chrono::steady_clock> tp;
39 };
40 
41 template <typename T>
42 class LoadTracking {
43 public:
LoadTracking(DefaultInterval & it)44     LoadTracking(DefaultInterval& it) : it(it)
45     {
46     }
47 
48     virtual ~LoadTracking() = default;
49 
Begin()50     void Begin()
51     {
52         static_cast<T*>(this)->BeginImpl();
53     }
54 
End()55     void End()
56     {
57         static_cast<T*>(this)->EndImpl();
58     }
59 
Record(TaskSwitchState state)60     void Record(TaskSwitchState state)
61     {
62         static_cast<T*>(this)->RecordImpl(state);
63     }
64 
GetLoad()65     uint64_t GetLoad()
66     {
67         return static_cast<T*>(this)->GetLoadImpl();
68     }
69 
70 protected:
71     DefaultInterval& it;
72 };
73 
74 class KernelLoadTracking : public LoadTracking<KernelLoadTracking> {
75     friend class LoadTracking<KernelLoadTracking>;
76 
77 public:
KernelLoadTracking(DefaultInterval & it)78     KernelLoadTracking(DefaultInterval& it) : LoadTracking<KernelLoadTracking>(it)
79     {
80     }
81 
82 private:
83     void BeginImpl();
84     void EndImpl();
RecordImpl(TaskSwitchState state)85     void RecordImpl(TaskSwitchState state)
86     {
87         (void)state;
88     };
89     uint64_t GetLoadImpl();
90 };
91 }; // namespace ffrt
92 
93 #endif
94