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 int qos;
44 WgType type;
45 };
46
47 #if (defined(QOS_FRAME_RTG))
48
49 struct WorkGroup* WorkgroupCreate(uint64_t interval, int qos);
50 int WorkgroupClear(struct WorkGroup* wg);
51 bool JoinWG(int tid, int qos);
52 bool LeaveWG(int tid, int qos);
53
54 #else
55
56 #ifdef QOS_WORKER_FRAME_RTG
57 WorkGroup* CreateRSWorkGroup(uint64_t interval, int qos);
58 bool JoinRSWorkGroup(int tid, int qos);
59 bool LeaveRSWorkGroup(int tid, int qos);
60 bool DestoryRSWorkGroup(int qos);
61 #endif
62
WorkgroupCreate(uint64_t interval,int qos)63 inline struct WorkGroup* WorkgroupCreate(uint64_t interval __attribute__((unused)), int qos)
64 {
65 #ifdef QOS_WORKER_FRAME_RTG
66 int uid = getuid();
67 if (uid == RS_UID) {
68 return CreateRSWorkGroup(interval, qos);
69 }
70 #endif
71 struct WorkGroup* wg = new (std::nothrow) struct WorkGroup();
72 if (wg == nullptr) {
73 return nullptr;
74 }
75 return wg;
76 }
77
WorkgroupClear(struct WorkGroup * wg)78 inline int WorkgroupClear(struct WorkGroup* wg)
79 {
80 #ifdef QOS_WORKER_FRAME_RTG
81 int uid = getuid();
82 if (uid == RS_UID) {
83 return DestoryRSWorkGroup(wg->qos);
84 }
85 #endif
86 delete wg;
87 wg = nullptr;
88 return 0;
89 }
90
JoinWG(int tid,int qos)91 inline bool JoinWG(int tid, int qos)
92 {
93 #ifdef QOS_WORKER_FRAME_RTG
94 int uid = getuid();
95 if (uid == RS_UID) {
96 return JoinRSWorkGroup(tid, qos);
97 }
98 #endif
99 (void)tid;
100 return true;
101 }
102
LeaveWG(int tid,int qos)103 inline bool LeaveWG(int tid, int qos)
104 {
105 #ifdef QOS_WORKER_FRAME_RTG
106 int uid = getuid();
107 if (uid == RS_UID) {
108 return LeaveRSWorkGroup(tid, qos);
109 }
110 #endif
111 (void)tid;
112 return true;
113 }
114
115 #endif
116
117 #if defined(QOS_FRAME_RTG)
118
119 void WorkgroupStartInterval(struct WorkGroup* wg);
120 void WorkgroupStopInterval(struct WorkGroup* wg);
121 void WorkgroupJoin(struct WorkGroup* wg, int tid);
122
123 #else /* !QOS_FRAME_RTG */
124
WorkgroupStartInterval(struct WorkGroup * wg)125 inline void WorkgroupStartInterval(struct WorkGroup* wg)
126 {
127 if (wg->started) {
128 return;
129 }
130 wg->started = true;
131 }
132
WorkgroupStopInterval(struct WorkGroup * wg)133 inline void WorkgroupStopInterval(struct WorkGroup* wg)
134 {
135 if (!wg->started) {
136 return;
137 }
138 wg->started = false;
139 }
140
WorkgroupJoin(struct WorkGroup * wg,int tid)141 inline void WorkgroupJoin(struct WorkGroup* wg, int tid)
142 {
143 (void)wg;
144 (void)tid;
145 }
146
147 #endif /* QOS_FRAME_RTG */
148 }
149 #endif
150