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