• 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_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 struct TaskCtx;
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 class UserSpaceLoadRecord {
42 public:
SetEnable(bool enable)43     void SetEnable(bool enable)
44     {
45         this->enable = enable;
46     }
47 
Enable()48     bool Enable() const
49     {
50         return enable;
51     }
52 
53     static void UpdateTaskSwitch(TaskCtx* prev, TaskCtx* next);
54 
55 private:
56     bool enable = false;
57 };
58 
59 template <typename T>
60 class LoadTracking {
61 public:
LoadTracking(DefaultInterval & it)62     LoadTracking(DefaultInterval& it) : it(it)
63     {
64     }
65 
66     virtual ~LoadTracking() = default;
67 
Begin()68     void Begin()
69     {
70         static_cast<T*>(this)->BeginImpl();
71     }
72 
End()73     void End()
74     {
75         static_cast<T*>(this)->EndImpl();
76     }
77 
Record(TaskSwitchState state)78     void Record(TaskSwitchState state)
79     {
80         static_cast<T*>(this)->RecordImpl(state);
81     }
82 
GetLoad()83     uint64_t GetLoad()
84     {
85         return static_cast<T*>(this)->GetLoadImpl();
86     }
87 
88 protected:
89     DefaultInterval& it;
90 };
91 
92 class KernelLoadTracking : public LoadTracking<KernelLoadTracking> {
93     friend class LoadTracking<KernelLoadTracking>;
94 
95 public:
KernelLoadTracking(DefaultInterval & it)96     KernelLoadTracking(DefaultInterval& it) : LoadTracking<KernelLoadTracking>(it)
97     {
98     }
99 
100 private:
101     void BeginImpl();
102     void EndImpl();
RecordImpl(TaskSwitchState state)103     void RecordImpl(TaskSwitchState state)
104     {
105         (void)state;
106     };
107     uint64_t GetLoadImpl();
108 };
109 
110 class UserSpaceLoadTracking : public LoadTracking<UserSpaceLoadTracking> {
111     friend class LoadTracking<UserSpaceLoadTracking>;
112     struct HistPoint;
113     using RecordList = typename std::list<TaskSwitchRecord>;
114 
115 public:
116     UserSpaceLoadTracking(DefaultInterval& it);
117 
118 private:
119     void BeginImpl();
120     void EndImpl();
121     void RecordImpl(TaskSwitchState state);
122     uint64_t GetLoadImpl();
123 
124     void RecordSwitchPoint(TaskSwitchState state, bool force = false);
125 
126     std::vector<HistPoint> CollectHistList();
127 
128     std::unordered_map<std::thread::id, RecordList> records;
129 };
130 }; // namespace ffrt
131 
132 #endif
133