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