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_THREAD_GROUP_H
17 #define FFRT_THREAD_GROUP_H
18 
19 #include "eu/rtg_ioctl.h"
20 
21 namespace ffrt {
22 class ThreadGroup {
23 public:
Id()24     int Id() const
25     {
26         return tgid;
27     }
28 
Enabled()29     bool Enabled() const
30     {
31         return tgid >= 0 && RTGCtrl::Instance().Enabled();
32     }
33 
Init()34     bool Init()
35     {
36         if (Enabled()) {
37             return true;
38         }
39 
40         tgid = RTGCtrl::Instance().GetThreadGroup();
41         return tgid >= 0;
42     }
43 
Release()44     bool Release()
45     {
46         if (!Enabled()) {
47             return true;
48         }
49 
50         if (!RTGCtrl::Instance().PutThreadGroup(tgid)) {
51             return false;
52         }
53 
54         tgid = -1;
55         return true;
56     }
57 
Begin()58     void Begin()
59     {
60         if (!Enabled() || isBegin()) {
61             return;
62         }
63 
64         isbegin = true;
65         RTGCtrl::Instance().Begin(tgid);
66     }
67 
End()68     void End()
69     {
70         if (!Enabled() || !isBegin()) {
71             return;
72         }
73         RTGCtrl::Instance().End(tgid);
74         isbegin = false;
75     }
76 
Join()77     bool Join()
78     {
79         if (!Enabled()) {
80             return false;
81         }
82         return RTGCtrl::Instance().JoinThread(tgid, RTGCtrl::GetTID());
83     }
84 
Join(pid_t tid)85     bool Join(pid_t tid)
86     {
87         if (!Enabled()) {
88             return false;
89         }
90         return RTGCtrl::Instance().JoinThread(tgid, tid);
91     }
92 
Leave()93     bool Leave()
94     {
95         if (!Enabled()) {
96             return false;
97         }
98         return RTGCtrl::Instance().RemoveThread(tgid, RTGCtrl::GetTID());
99     }
100 
Leave(pid_t tid)101     bool Leave(pid_t tid)
102     {
103         if (!Enabled()) {
104             return false;
105         }
106         return RTGCtrl::Instance().RemoveThread(tgid, tid);
107     }
108 
UpdateFreq(int64_t freq)109     bool UpdateFreq(int64_t freq)
110     {
111         if (!Enabled()) {
112             return false;
113         }
114         return RTGCtrl::Instance().UpdatePerfFreq(tgid, freq);
115     }
116 
UpdateUitl(int64_t util)117     bool UpdateUitl(int64_t util)
118     {
119         if (!Enabled()) {
120             return false;
121         }
122         return RTGCtrl::Instance().UpdatePerfUtil(tgid, util);
123     }
124 
GetLoad()125     RTGLoadInfo GetLoad()
126     {
127         if (!Enabled()) {
128             return RTGLoadInfo();
129         }
130         return RTGCtrl::Instance().UpdateAndGetLoad(tgid);
131     }
132 
GetLoad(pid_t tid)133     RTGLoadInfo GetLoad(pid_t tid)
134     {
135         if (!Enabled()) {
136             return RTGLoadInfo();
137         }
138         return RTGCtrl::Instance().UpdateAndGetLoad(tgid, tid);
139     }
140 
SetWindowSize(uint64_t size)141     bool SetWindowSize(uint64_t size)
142     {
143         if (!Enabled()) {
144             return false;
145         }
146         return RTGCtrl::Instance().SetGroupWindowSize(tgid, size);
147     }
148 
SetInvalidInterval(uint64_t invalidMs)149     bool SetInvalidInterval(uint64_t invalidMs)
150     {
151         if (!Enabled()) {
152             return false;
153         }
154         return RTGCtrl::Instance().SetInvalidInterval(tgid, invalidMs);
155     }
156 
GetTID()157     static pid_t GetTID()
158     {
159         return RTGCtrl::GetTID();
160     }
161 
isBegin()162     bool isBegin()
163     {
164         return isbegin;
165     }
166 private:
167     int tgid = -1;
168     bool isbegin = false;
169 };
170 }; // namespace ffrt
171 
172 #endif