1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // kernel -- Header containing the core of the kernel API |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Tue Feb 4 12:29:26 2020 |
---|
13 | // Update Count : 22 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include "invoke.h" |
---|
19 | #include "time_t.hfa" |
---|
20 | #include "coroutine.hfa" |
---|
21 | |
---|
22 | #include "containers/list.hfa" |
---|
23 | |
---|
24 | extern "C" { |
---|
25 | #include <bits/pthreadtypes.h> |
---|
26 | #include <pthread.h> |
---|
27 | #include <linux/types.h> |
---|
28 | } |
---|
29 | |
---|
30 | #ifdef __CFA_WITH_VERIFY__ |
---|
31 | extern bool __cfaabi_dbg_in_kernel(); |
---|
32 | #endif |
---|
33 | |
---|
34 | //----------------------------------------------------------------------------- |
---|
35 | // I/O |
---|
36 | struct cluster; |
---|
37 | struct $io_context; |
---|
38 | struct $io_arbiter; |
---|
39 | |
---|
40 | struct io_context_params { |
---|
41 | int num_entries; |
---|
42 | }; |
---|
43 | |
---|
44 | void ?{}(io_context_params & this); |
---|
45 | |
---|
46 | //----------------------------------------------------------------------------- |
---|
47 | // Processor |
---|
48 | extern struct cluster * mainCluster; |
---|
49 | |
---|
50 | // Coroutine used py processors for the 2-step context switch |
---|
51 | coroutine processorCtx_t { |
---|
52 | struct processor * proc; |
---|
53 | }; |
---|
54 | |
---|
55 | struct io_future_t; |
---|
56 | |
---|
57 | // Information needed for idle sleep |
---|
58 | struct __fd_waitctx { |
---|
59 | // semaphore/future like object |
---|
60 | // values can be 0, 1 or some file descriptor. |
---|
61 | // 0 - is the default state |
---|
62 | // 1 - means the proc should wake-up immediately |
---|
63 | // FD - means the proc is going asleep and should be woken by writing to the FD. |
---|
64 | volatile int sem; |
---|
65 | |
---|
66 | // The event FD that corresponds to this processor |
---|
67 | int evfd; |
---|
68 | |
---|
69 | // buffer into which the proc will read from evfd |
---|
70 | // unused if not using io_uring for idle sleep |
---|
71 | void * rdbuf; |
---|
72 | |
---|
73 | // future use to track the read of the eventfd |
---|
74 | // unused if not using io_uring for idle sleep |
---|
75 | io_future_t * ftr; |
---|
76 | }; |
---|
77 | |
---|
78 | // Wrapper around kernel threads |
---|
79 | struct __attribute__((aligned(128))) processor { |
---|
80 | // Cluster from which to get threads |
---|
81 | struct cluster * cltr; |
---|
82 | |
---|
83 | // Ready Queue state per processor |
---|
84 | struct { |
---|
85 | unsigned short its; |
---|
86 | unsigned short itr; |
---|
87 | unsigned id; |
---|
88 | unsigned target; |
---|
89 | unsigned last; |
---|
90 | signed cpu; |
---|
91 | } rdq; |
---|
92 | |
---|
93 | // Set to true to notify the processor should terminate |
---|
94 | volatile bool do_terminate; |
---|
95 | |
---|
96 | // Coroutine ctx who does keeps the state of the processor |
---|
97 | struct processorCtx_t runner; |
---|
98 | |
---|
99 | // Name of the processor |
---|
100 | const char * name; |
---|
101 | |
---|
102 | // Handle to pthreads |
---|
103 | pthread_t kernel_thread; |
---|
104 | |
---|
105 | // Unique id for the processor (not per cluster) |
---|
106 | unsigned unique_id; |
---|
107 | |
---|
108 | struct { |
---|
109 | $io_context * ctx; |
---|
110 | unsigned target; |
---|
111 | volatile bool pending; |
---|
112 | volatile bool dirty; |
---|
113 | } io; |
---|
114 | |
---|
115 | // Preemption data |
---|
116 | // Node which is added in the discrete event simulaiton |
---|
117 | struct alarm_node_t * preemption_alarm; |
---|
118 | |
---|
119 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
120 | bool pending_preemption; |
---|
121 | |
---|
122 | // context for idle sleep |
---|
123 | struct __fd_waitctx idle_wctx; |
---|
124 | |
---|
125 | // Termination synchronisation (user semaphore) |
---|
126 | oneshot terminated; |
---|
127 | |
---|
128 | // pthread Stack |
---|
129 | void * stack; |
---|
130 | |
---|
131 | // Link lists fields |
---|
132 | inline dlink(processor); |
---|
133 | |
---|
134 | // special init fields |
---|
135 | // This is needed for memcached integration |
---|
136 | // once memcached experiments are done this should probably be removed |
---|
137 | // it is not a particularly safe scheme as it can make processors less homogeneous |
---|
138 | struct { |
---|
139 | thread$ * thrd; |
---|
140 | } init; |
---|
141 | |
---|
142 | struct KernelThreadData * local_data; |
---|
143 | |
---|
144 | #if !defined(__CFA_NO_STATISTICS__) |
---|
145 | int print_stats; |
---|
146 | bool print_halts; |
---|
147 | #endif |
---|
148 | |
---|
149 | #ifdef __CFA_DEBUG__ |
---|
150 | // Last function to enable preemption on this processor |
---|
151 | const char * last_enable; |
---|
152 | #endif |
---|
153 | }; |
---|
154 | P9_EMBEDDED( processor, dlink(processor) ) |
---|
155 | |
---|
156 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
157 | void ^?{}(processor & this); |
---|
158 | |
---|
159 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
160 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
161 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster}; } |
---|
162 | |
---|
163 | //----------------------------------------------------------------------------- |
---|
164 | // Cluster Tools |
---|
165 | |
---|
166 | // Intrusives lanes which are used by the ready queue |
---|
167 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
168 | void ?{}(__intrusive_lane_t & this); |
---|
169 | void ^?{}(__intrusive_lane_t & this); |
---|
170 | |
---|
171 | // Aligned timestamps which are used by the ready queue and io subsystem |
---|
172 | struct __attribute__((aligned(128))) __timestamp_t { |
---|
173 | volatile unsigned long long tv; |
---|
174 | volatile unsigned long long ma; |
---|
175 | }; |
---|
176 | |
---|
177 | static inline void ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; } |
---|
178 | static inline void ^?{}(__timestamp_t &) {} |
---|
179 | |
---|
180 | |
---|
181 | struct __attribute__((aligned(16))) __cache_id_t { |
---|
182 | volatile unsigned id; |
---|
183 | }; |
---|
184 | |
---|
185 | // Idle Sleep |
---|
186 | struct __cluster_proc_list { |
---|
187 | // Spin lock protecting the queue |
---|
188 | __spinlock_t lock; |
---|
189 | |
---|
190 | // FD to use to wake a processor |
---|
191 | struct __fd_waitctx * volatile fdw; |
---|
192 | |
---|
193 | // Total number of processors |
---|
194 | unsigned total; |
---|
195 | |
---|
196 | // Total number of idle processors |
---|
197 | unsigned idle; |
---|
198 | |
---|
199 | // List of idle processors |
---|
200 | dlist(processor) idles; |
---|
201 | |
---|
202 | // List of active processors |
---|
203 | dlist(processor) actives; |
---|
204 | }; |
---|
205 | |
---|
206 | //----------------------------------------------------------------------------- |
---|
207 | // Cluster |
---|
208 | struct __attribute__((aligned(128))) cluster { |
---|
209 | struct { |
---|
210 | struct { |
---|
211 | // Arary of subqueues |
---|
212 | __intrusive_lane_t * data; |
---|
213 | |
---|
214 | // Time since subqueues were processed |
---|
215 | __timestamp_t * tscs; |
---|
216 | |
---|
217 | // Number of subqueue / timestamps |
---|
218 | size_t count; |
---|
219 | } readyQ; |
---|
220 | |
---|
221 | struct { |
---|
222 | // Array of $io_ |
---|
223 | $io_context ** data; |
---|
224 | |
---|
225 | // Time since subqueues were processed |
---|
226 | __timestamp_t * tscs; |
---|
227 | |
---|
228 | // Number of I/O subqueues |
---|
229 | size_t count; |
---|
230 | } io; |
---|
231 | |
---|
232 | // Cache each kernel thread belongs to |
---|
233 | __cache_id_t * caches; |
---|
234 | } sched; |
---|
235 | |
---|
236 | // // Ready queue for threads |
---|
237 | // __ready_queue_t ready_queue; |
---|
238 | |
---|
239 | // Name of the cluster |
---|
240 | const char * name; |
---|
241 | |
---|
242 | // Preemption rate on this cluster |
---|
243 | Duration preemption_rate; |
---|
244 | |
---|
245 | // List of idle processors |
---|
246 | __cluster_proc_list procs; |
---|
247 | |
---|
248 | // List of threads |
---|
249 | __spinlock_t thread_list_lock; |
---|
250 | __dllist_t(struct thread$) threads; |
---|
251 | unsigned int nthreads; |
---|
252 | |
---|
253 | // Link lists fields |
---|
254 | struct __dbg_node_cltr { |
---|
255 | cluster * next; |
---|
256 | cluster * prev; |
---|
257 | } node; |
---|
258 | |
---|
259 | struct { |
---|
260 | $io_arbiter * arbiter; |
---|
261 | io_context_params params; |
---|
262 | } io; |
---|
263 | |
---|
264 | #if !defined(__CFA_NO_STATISTICS__) |
---|
265 | struct __stats_t * stats; |
---|
266 | int print_stats; |
---|
267 | #endif |
---|
268 | }; |
---|
269 | extern Duration default_preemption(); |
---|
270 | |
---|
271 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); |
---|
272 | void ^?{}(cluster & this); |
---|
273 | |
---|
274 | static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; } |
---|
275 | static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; } |
---|
276 | static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; } |
---|
277 | static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } |
---|
278 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, num_io, default_params}; } |
---|
279 | static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; } |
---|
280 | static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } |
---|
281 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } |
---|
282 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; } |
---|
283 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } |
---|
284 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, num_io, io_params}; } |
---|
285 | static inline void ?{} (cluster & this, const char name[], unsigned num_io, const io_context_params & io_params) { this{name, default_preemption(), num_io, io_params}; } |
---|
286 | |
---|
287 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
288 | |
---|
289 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE |
---|
290 | static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; } |
---|
291 | |
---|
292 | #if !defined(__CFA_NO_STATISTICS__) |
---|
293 | void print_stats_now( cluster & this, int flags ); |
---|
294 | |
---|
295 | static inline void print_stats_at_exit( cluster & this, int flags ) { |
---|
296 | this.print_stats |= flags; |
---|
297 | } |
---|
298 | |
---|
299 | static inline void print_stats_at_exit( processor & this, int flags ) { |
---|
300 | this.print_stats |= flags; |
---|
301 | } |
---|
302 | |
---|
303 | void print_halts( processor & this ); |
---|
304 | #endif |
---|
305 | |
---|
306 | // Local Variables: // |
---|
307 | // mode: c // |
---|
308 | // tab-width: 4 // |
---|
309 | // End: // |
---|