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 #include <cstdio>
16 #include <cstdlib>
17 #include <cstring>
18 #include <mutex>
19 #include "workgroup_internal.h"
20 #include "dfx/log/ffrt_log_api.h"
21 #include "task_client_adapter.h"
22 
23 
24 #if (defined(QOS_WORKER_FRAME_RTG) || defined(QOS_FRAME_RTG))
25 constexpr int HWC_UID = 3039;
26 constexpr int ROOT_UID = 0;
27 constexpr int RS_RTG_ID = 10;
28 
29 namespace ffrt {
30 static int wgId = -1;
31 static WorkGroup* rsWorkGroup = nullptr;
32 static int wgCount = 0;
33 static std::mutex wgLock;
34 
35 #if (defined(QOS_WORKER_FRAME_RTG))
36 
WorkgroupInit(struct WorkGroup * wg,uint64_t interval,int rtgId)37 void WorkgroupInit(struct WorkGroup* wg, uint64_t interval, int rtgId)
38 {
39     wg->started = false;
40     wg->interval = interval;
41     wg->rtgId = rtgId;
42     wgId = rtgId;
43 
44     for (int i = 0; i < MAX_WG_THREADS; i++) {
45         wg->tids[i] = -1;
46     }
47 }
48 
FindThreadInWorkGroup(WorkGroup * workGroup,int tid)49 int FindThreadInWorkGroup(WorkGroup *workGroup, int tid)
50 {
51     if (workGroup == nullptr) {
52         FFRT_LOGE("[RSWorkGroup] find thread %{public}d in workGroup failed, workGroup is null", tid);
53         return -1;
54     }
55     for (int i = 0;i < MAX_WG_THREADS; i++) {
56         if (workGroup->tids[i] == tid) {
57             return i;
58         }
59     }
60     return -1;
61 }
62 
InsertThreadInWorkGroup(WorkGroup * workGroup,int tid)63 bool InsertThreadInWorkGroup(WorkGroup *workGroup, int tid)
64 {
65     if (workGroup == nullptr) {
66         FFRT_LOGE("[RSWorkGroup] join thread %{public}d into workGroup failed, workGroup is null", tid);
67         return false;
68     }
69     int targetIndex = -1;
70     for (int i = 0; i < MAX_WG_THREADS; i++) {
71         if (workGroup->tids[i] == -1) {
72             workGroup->tids[i] = tid;
73             targetIndex = i;
74             return true;
75         }
76     }
77     if (targetIndex == -1) {
78         FFRT_LOGE("[RSWorkGroup] join thread %{public}d into RSWorkGroup failed, max_thread_num: %{public}d",
79             tid, MAX_WG_THREADS);
80         return false;
81     }
82     return true;
83 }
84 
CreateRSWorkGroup(uint64_t interval)85 WorkGroup* CreateRSWorkGroup(uint64_t interval)
86 {
87     IntervalReply rs;
88     rs.rtgId = -1;
89     rs.tid = -1;
90     {
91         std::lock_guard<std::mutex> lck(wgLock);
92         if (rsWorkGroup == nullptr) {
93             CTC_QUERY_INTERVAL(QUERY_RENDER_SERVICE, rs);
94             if (rs.rtgId > 0) {
95                 rsWorkGroup = new struct WorkGroup();
96                 if (rsWorkGroup == nullptr) {
97                     FFRT_LOGE("[RSWorkGroup] rsWorkGroup malloc failed!");
98                     return nullptr;
99                 }
100                 WorkgroupInit(rsWorkGroup, interval, rs.rtgId);
101                 wgCount++;
102             }
103         }
104     }
105     return rsWorkGroup;
106 }
107 
LeaveRSWorkGroup(int tid)108 bool LeaveRSWorkGroup(int tid)
109 {
110     std::lock_guard<std::mutex> lck(wgLock);
111     if (rsWorkGroup == nullptr) {
112         FFRT_LOGI("[RSWorkGroup] LeaveRSWorkGroup rsWorkGroup is null ,tid:%{public}d", tid);
113         return false;
114     }
115     int existIndex = FindThreadInWorkGroup(rsWorkGroup, tid);
116     if (existIndex != -1) {
117         rsWorkGroup->tids[existIndex] = -1;
118     }
119     FFRT_LOGI("[RSWorkGroup] LeaveRSWorkGroup ,tid: %{public}d, existIndex: %{public}d", tid, existIndex);
120     return true;
121 }
122 
JoinRSWorkGroup(int tid)123 bool JoinRSWorkGroup(int tid)
124 {
125     std::lock_guard<std::mutex> lck(wgLock);
126     if (rsWorkGroup == nullptr) {
127         FFRT_LOGE("[RSWorkGroup] join thread %{public}d into RSWorkGroup failed; Create RSWorkGroup first", tid);
128         return false;
129     }
130     int existIndex = FindThreadInWorkGroup(rsWorkGroup, tid);
131     if (existIndex == -1) {
132         IntervalReply rs;
133         rs.rtgId = -1;
134         rs.tid = tid;
135         CTC_QUERY_INTERVAL(QUERY_RENDER_SERVICE, rs);
136         if (rs.rtgId > 0) {
137             bool success = InsertThreadInWorkGroup(rsWorkGroup, tid);
138             if (!success) {
139                 return false;
140             }
141         }
142     }
143     FFRT_LOGI("[RSWorkGroup] update thread %{public}d success", tid);
144     return true;
145 }
146 
DestoryRSWorkGroup()147 bool DestoryRSWorkGroup()
148 {
149     std::lock_guard<std::mutex> lck(wgLock);
150     if (rsWorkGroup != nullptr) {
151         delete rsWorkGroup;
152         rsWorkGroup = nullptr;
153         wgId = -1;
154         return true;
155     }
156     return false;
157 }
158 #endif
159 
160 #if defined(QOS_FRAME_RTG)
JoinWG(int tid)161 bool JoinWG(int tid)
162 {
163     if (wgId < 0) {
164         if (wgCount > 0) {
165             FFRT_LOGE("[WorkGroup] interval is unavailable");
166         }
167         return false;
168     }
169     int uid = getuid();
170     if (uid == RS_UID) {
171         return JoinRSWorkGroup(tid);
172     }
173     int addRet = AddThreadToRtgAdapter(tid, wgId, 0);
174     if (addRet == 0) {
175         FFRT_LOGI("[WorkGroup] update thread %{public}d success", tid);
176     } else {
177         FFRT_LOGE("[WorkGroup] update thread %{public}d failed, return %{public}d", tid, addRet);
178     }
179     return true;
180 }
181 
LeaveWG(int tid)182 bool LeaveWG(int tid)
183 {
184     int uid = getuid();
185     if (uid == RS_UID) {
186         return LeaveRSWorkGroup(tid);
187     }
188     return false;
189 }
190 
WorkgroupCreate(uint64_t interval)191 struct WorkGroup* WorkgroupCreate(uint64_t interval)
192 {
193     int rtgId = -1;
194     int uid = getuid();
195     int num = 0;
196 
197     if (uid == RS_UID) {
198         CreateRSWorkGroup(interval);
199         return rsWorkGroup;
200     }
201 
202     if (rtgId < 0) {
203         FFRT_LOGE("[WorkGroup] create rtg group %d failed", rtgId);
204         return nullptr;
205     }
206     FFRT_LOGI("[WorkGroup] create rtg group %d success", rtgId);
207 
208     WorkGroup* wg = nullptr;
209     wg = new struct WorkGroup();
210     if (wg == nullptr) {
211         FFRT_LOGE("[WorkGroup] workgroup malloc failed!");
212         return nullptr;
213     }
214     WorkgroupInit(wg, interval, rtgId);
215     {
216         std::lock_guard<std::mutex> lck(wgLock);
217         wgCount++;
218     }
219     return wg;
220 }
221 
WorkgroupClear(struct WorkGroup * wg)222 int WorkgroupClear(struct WorkGroup* wg)
223 {
224     int uid = getuid();
225     if (uid == RS_UID) {
226         return DestoryRSWorkGroup();
227     }
228     if (wg == nullptr) {
229         FFRT_LOGE("[WorkGroup] input workgroup is null");
230         return 0;
231     }
232     int ret = -1;
233     if (uid != RS_UID) {
234         ret = DestroyRtgGrpAdapter(wg->rtgId);
235         if (ret != 0) {
236             FFRT_LOGE("[WorkGroup] destroy rtg group failed");
237         } else {
238             std::lock_guard<std::mutex> lck(wgLock);
239             wgCount--;
240         }
241     }
242     delete wg;
243     wg = nullptr;
244     return ret;
245 }
246 
WorkgroupStartInterval(struct WorkGroup * wg)247 void WorkgroupStartInterval(struct WorkGroup* wg)
248 {
249     if (wg == nullptr) {
250         FFRT_LOGE("[WorkGroup] input workgroup is null");
251         return;
252     }
253 
254     if (wg->started) {
255         FFRT_LOGW("[WorkGroup] already start");
256         return;
257     }
258 
259     if (BeginFrameFreqAdapter(0) == 0) {
260         wg->started = true;
261     } else {
262         FFRT_LOGE("[WorkGroup] start rtg(%d) work interval failed", wg->rtgId);
263     }
264 }
265 
WorkgroupStopInterval(struct WorkGroup * wg)266 void WorkgroupStopInterval(struct WorkGroup* wg)
267 {
268     if (wg == nullptr) {
269         FFRT_LOGE("[WorkGroup] input workgroup is null");
270         return;
271     }
272 
273     if (!wg->started) {
274         FFRT_LOGW("[WorkGroup] already stop");
275         return;
276     }
277 
278     int ret = EndFrameFreqAdapter(0);
279     if (ret == 0) {
280         wg->started = false;
281     } else {
282         FFRT_LOGE("[WorkGroup] stop rtg(%d) work interval failed", wg->rtgId);
283     }
284 }
285 
WorkgroupJoin(struct WorkGroup * wg,int tid)286 void WorkgroupJoin(struct WorkGroup* wg, int tid)
287 {
288     if (wg == nullptr) {
289         FFRT_LOGE("[WorkGroup] input workgroup is null");
290         return;
291     }
292     int uid = getuid();
293     FFRT_LOGI("[WorkGroup] %s uid = %d rtgid = %d", __func__, uid, wg->rtgId);
294     if (uid == RS_UID) {
295         IntervalReply rs;
296         rs.tid = tid;
297         CTC_QUERY_INTERVAL(QUERY_RENDER_SERVICE, rs);
298         FFRT_LOGI("[WorkGroup] join thread %{public}ld", tid);
299         return;
300     }
301     int addRet = AddThreadToRtgAdapter(tid, wg->rtgId, 0);
302     if (addRet == 0) {
303         FFRT_LOGI("[WorkGroup] join thread %{public}ld success", tid);
304     } else {
305         FFRT_LOGE("[WorkGroup] join fail with %{public}d threads for %{public}d", addRet, tid);
306     }
307 }
308 
309 #endif /* QOS_FRAME_RTG */
310 }
311 
312 #endif
313