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 | //----------------------------------------------------------------------------- |
---|
31 | // Underlying Locks |
---|
32 | #ifdef __CFA_WITH_VERIFY__ |
---|
33 | extern bool __cfaabi_dbg_in_kernel(); |
---|
34 | #endif |
---|
35 | |
---|
36 | struct __bin_sem_t { |
---|
37 | pthread_mutex_t lock; |
---|
38 | pthread_cond_t cond; |
---|
39 | int val; |
---|
40 | }; |
---|
41 | |
---|
42 | //----------------------------------------------------------------------------- |
---|
43 | // I/O |
---|
44 | struct cluster; |
---|
45 | struct $io_context; |
---|
46 | struct $io_arbiter; |
---|
47 | |
---|
48 | struct io_context_params { |
---|
49 | int num_entries; |
---|
50 | }; |
---|
51 | |
---|
52 | void ?{}(io_context_params & this); |
---|
53 | |
---|
54 | struct io_context { |
---|
55 | $io_context * ctx; |
---|
56 | cluster * cltr; |
---|
57 | }; |
---|
58 | void ?{}(io_context & this, struct cluster & cl); |
---|
59 | void ^?{}(io_context & this); |
---|
60 | |
---|
61 | //----------------------------------------------------------------------------- |
---|
62 | // Processor |
---|
63 | extern struct cluster * mainCluster; |
---|
64 | |
---|
65 | // Processor id, required for scheduling threads |
---|
66 | struct __processor_id_t { |
---|
67 | unsigned id:24; |
---|
68 | bool full_proc:1; |
---|
69 | |
---|
70 | #if !defined(__CFA_NO_STATISTICS__) |
---|
71 | struct __stats_t * stats; |
---|
72 | #endif |
---|
73 | }; |
---|
74 | |
---|
75 | coroutine processorCtx_t { |
---|
76 | struct processor * proc; |
---|
77 | }; |
---|
78 | |
---|
79 | // Wrapper around kernel threads |
---|
80 | struct __attribute__((aligned(128))) processor { |
---|
81 | // Main state |
---|
82 | inline __processor_id_t; |
---|
83 | |
---|
84 | // Cluster from which to get threads |
---|
85 | struct cluster * cltr; |
---|
86 | |
---|
87 | // Set to true to notify the processor should terminate |
---|
88 | volatile bool do_terminate; |
---|
89 | |
---|
90 | // Coroutine ctx who does keeps the state of the processor |
---|
91 | struct processorCtx_t runner; |
---|
92 | |
---|
93 | // Name of the processor |
---|
94 | const char * name; |
---|
95 | |
---|
96 | // Handle to pthreads |
---|
97 | pthread_t kernel_thread; |
---|
98 | |
---|
99 | struct { |
---|
100 | $io_context * volatile ctx; |
---|
101 | volatile bool lock; |
---|
102 | } io; |
---|
103 | |
---|
104 | // Preemption data |
---|
105 | // Node which is added in the discrete event simulaiton |
---|
106 | struct alarm_node_t * preemption_alarm; |
---|
107 | |
---|
108 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
109 | bool pending_preemption; |
---|
110 | |
---|
111 | // Idle lock (kernel semaphore) |
---|
112 | __bin_sem_t idle; |
---|
113 | |
---|
114 | // Termination synchronisation (user semaphore) |
---|
115 | oneshot terminated; |
---|
116 | |
---|
117 | // pthread Stack |
---|
118 | void * stack; |
---|
119 | |
---|
120 | // Link lists fields |
---|
121 | DLISTED_MGD_IMPL_IN(processor) |
---|
122 | |
---|
123 | #if !defined(__CFA_NO_STATISTICS__) |
---|
124 | int print_stats; |
---|
125 | bool print_halts; |
---|
126 | #endif |
---|
127 | |
---|
128 | #ifdef __CFA_DEBUG__ |
---|
129 | // Last function to enable preemption on this processor |
---|
130 | const char * last_enable; |
---|
131 | #endif |
---|
132 | }; |
---|
133 | |
---|
134 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
135 | void ^?{}(processor & this); |
---|
136 | |
---|
137 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
138 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
139 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } |
---|
140 | |
---|
141 | DLISTED_MGD_IMPL_OUT(processor) |
---|
142 | |
---|
143 | //----------------------------------------------------------------------------- |
---|
144 | // Cluster Tools |
---|
145 | |
---|
146 | // Intrusives lanes which are used by the relaxed ready queue |
---|
147 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
148 | void ?{}(__intrusive_lane_t & this); |
---|
149 | void ^?{}(__intrusive_lane_t & this); |
---|
150 | |
---|
151 | // Counter used for wether or not the lanes are all empty |
---|
152 | struct __attribute__((aligned(128))) __snzi_node_t; |
---|
153 | struct __snzi_t { |
---|
154 | unsigned mask; |
---|
155 | int root; |
---|
156 | __snzi_node_t * nodes; |
---|
157 | }; |
---|
158 | |
---|
159 | void ?{}( __snzi_t & this, unsigned depth ); |
---|
160 | void ^?{}( __snzi_t & this ); |
---|
161 | |
---|
162 | //TODO adjust cache size to ARCHITECTURE |
---|
163 | // Structure holding the relaxed ready queue |
---|
164 | struct __ready_queue_t { |
---|
165 | // Data tracking how many/which lanes are used |
---|
166 | // Aligned to 128 for cache locality |
---|
167 | __snzi_t snzi; |
---|
168 | |
---|
169 | // Data tracking the actual lanes |
---|
170 | // On a seperate cacheline from the used struct since |
---|
171 | // used can change on each push/pop but this data |
---|
172 | // only changes on shrink/grow |
---|
173 | struct { |
---|
174 | // Arary of lanes |
---|
175 | __intrusive_lane_t * volatile data; |
---|
176 | |
---|
177 | // Number of lanes (empty or not) |
---|
178 | volatile size_t count; |
---|
179 | } lanes; |
---|
180 | }; |
---|
181 | |
---|
182 | void ?{}(__ready_queue_t & this); |
---|
183 | void ^?{}(__ready_queue_t & this); |
---|
184 | |
---|
185 | // Idle Sleep |
---|
186 | struct __cluster_idles { |
---|
187 | // Spin lock protecting the queue |
---|
188 | volatile uint64_t lock; |
---|
189 | |
---|
190 | // Total number of processors |
---|
191 | unsigned total; |
---|
192 | |
---|
193 | // Total number of idle processors |
---|
194 | unsigned idle; |
---|
195 | |
---|
196 | // List of idle processors |
---|
197 | dlist(processor, processor) list; |
---|
198 | }; |
---|
199 | |
---|
200 | //----------------------------------------------------------------------------- |
---|
201 | // Cluster |
---|
202 | struct __attribute__((aligned(128))) cluster { |
---|
203 | // Ready queue for threads |
---|
204 | __ready_queue_t ready_queue; |
---|
205 | |
---|
206 | // Name of the cluster |
---|
207 | const char * name; |
---|
208 | |
---|
209 | // Preemption rate on this cluster |
---|
210 | Duration preemption_rate; |
---|
211 | |
---|
212 | // List of idle processors |
---|
213 | __cluster_idles idles; |
---|
214 | |
---|
215 | // List of threads |
---|
216 | __spinlock_t thread_list_lock; |
---|
217 | __dllist_t(struct $thread) threads; |
---|
218 | unsigned int nthreads; |
---|
219 | |
---|
220 | // Link lists fields |
---|
221 | struct __dbg_node_cltr { |
---|
222 | cluster * next; |
---|
223 | cluster * prev; |
---|
224 | } node; |
---|
225 | |
---|
226 | struct { |
---|
227 | $io_arbiter * arbiter; |
---|
228 | io_context_params params; |
---|
229 | } io; |
---|
230 | |
---|
231 | #if !defined(__CFA_NO_STATISTICS__) |
---|
232 | struct __stats_t * stats; |
---|
233 | int print_stats; |
---|
234 | #endif |
---|
235 | }; |
---|
236 | extern Duration default_preemption(); |
---|
237 | |
---|
238 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); |
---|
239 | void ^?{}(cluster & this); |
---|
240 | |
---|
241 | static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; } |
---|
242 | static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; } |
---|
243 | static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; } |
---|
244 | static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } |
---|
245 | 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}; } |
---|
246 | static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; } |
---|
247 | static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } |
---|
248 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } |
---|
249 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; } |
---|
250 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } |
---|
251 | 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}; } |
---|
252 | 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}; } |
---|
253 | |
---|
254 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
255 | |
---|
256 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE |
---|
257 | static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; } |
---|
258 | |
---|
259 | #if !defined(__CFA_NO_STATISTICS__) |
---|
260 | void print_stats_now( cluster & this, int flags ); |
---|
261 | |
---|
262 | static inline void print_stats_at_exit( cluster & this, int flags ) { |
---|
263 | this.print_stats |= flags; |
---|
264 | } |
---|
265 | |
---|
266 | static inline void print_stats_at_exit( processor & this, int flags ) { |
---|
267 | this.print_stats |= flags; |
---|
268 | } |
---|
269 | |
---|
270 | void print_halts( processor & this ); |
---|
271 | #endif |
---|
272 | |
---|
273 | // Local Variables: // |
---|
274 | // mode: c // |
---|
275 | // tab-width: 4 // |
---|
276 | // End: // |
---|