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 WORKGROUP_INCLUDE
17 #define WORKGROUP_INCLUDE
18 
19 #include <unistd.h>
20 #include <sys/syscall.h>
21 #include <sys/types.h>
22 #include <cstdbool>
23 #include <chrono>
24 #include <fcntl.h>
25 #include <string>
26 
27 constexpr int RS_UID = 1003;
28 constexpr int MAX_WG_THREADS = 32;
29 #define MAX_FRAME_BUFFER 6
30 #define gettid() syscall(SYS_gettid)
31 namespace ffrt {
32 enum WgType {
33     TYPE_DEFAULT = 0,
34     TYPE_RS = 1,
35     TYPE_MAX
36 };
37 
38 struct WorkGroup {
39     bool started;
40     int rtgId;
41     int tids[MAX_WG_THREADS];
42     uint64_t interval;
43     WgType type;
44 };
45 
46 #if (defined(QOS_FRAME_RTG))
47 
48 struct WorkGroup* WorkgroupCreate(uint64_t interval);
49 int WorkgroupClear(struct WorkGroup* wg);
50 bool JoinWG(int tid);
51 bool LeaveWG(int tid);
52 
53 #else
54 
55 #ifdef QOS_WORKER_FRAME_RTG
56     WorkGroup* CreateRSWorkGroup(uint64_t interval);
57     bool JoinRSWorkGroup(int tid);
58     bool LeaveRSWorkGroup(int tid);
59     bool DestoryRSWorkGroup();
60 #endif
61 
WorkgroupCreate(uint64_t interval)62 inline struct WorkGroup* WorkgroupCreate(uint64_t interval __attribute__((unused)))
63 {
64 #ifdef QOS_WORKER_FRAME_RTG
65     int uid = getuid();
66     if (uid == RS_UID) {
67         return CreateRSWorkGroup(interval);
68     }
69 #endif
70     struct WorkGroup* wg = new (std::nothrow) struct WorkGroup();
71     if (wg == nullptr) {
72         return nullptr;
73     }
74     return wg;
75 }
76 
WorkgroupClear(struct WorkGroup * wg)77 inline int WorkgroupClear(struct WorkGroup* wg)
78 {
79 #ifdef QOS_WORKER_FRAME_RTG
80     int uid = getuid();
81     if (uid == RS_UID) {
82         return DestoryRSWorkGroup();
83     }
84 #endif
85     delete wg;
86     wg = nullptr;
87     return 0;
88 }
89 
JoinWG(int tid)90 inline bool JoinWG(int tid)
91 {
92 #ifdef QOS_WORKER_FRAME_RTG
93     int uid = getuid();
94     if (uid == RS_UID) {
95         return JoinRSWorkGroup(tid);
96     }
97 #endif
98     (void)tid;
99     return true;
100 }
101 
LeaveWG(int tid)102 inline bool LeaveWG(int tid)
103 {
104 #ifdef QOS_WORKER_FRAME_RTG
105     int uid = getuid();
106     if (uid == RS_UID) {
107         return LeaveRSWorkGroup(tid);
108     }
109 #endif
110     (void)tid;
111     return true;
112 }
113 
114 #endif
115 
116 #if defined(QOS_FRAME_RTG)
117 
118 void WorkgroupStartInterval(struct WorkGroup* wg);
119 void WorkgroupStopInterval(struct WorkGroup* wg);
120 void WorkgroupJoin(struct WorkGroup* wg, int tid);
121 
122 #else /* !QOS_FRAME_RTG */
123 
WorkgroupStartInterval(struct WorkGroup * wg)124 inline void WorkgroupStartInterval(struct WorkGroup* wg)
125 {
126     if (wg->started) {
127         return;
128     }
129     wg->started = true;
130 }
131 
WorkgroupStopInterval(struct WorkGroup * wg)132 inline void WorkgroupStopInterval(struct WorkGroup* wg)
133 {
134     if (!wg->started) {
135         return;
136     }
137     wg->started = false;
138 }
139 
WorkgroupJoin(struct WorkGroup * wg,int tid)140 inline void WorkgroupJoin(struct WorkGroup* wg, int tid)
141 {
142     (void)wg;
143     (void)tid;
144 }
145 
146 #endif /* QOS_FRAME_RTG */
147 }
148 #endif
149