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