[8118303] | 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 | // |
---|
[454f478] | 7 | // kernel -- Header containing the core of the kernel API |
---|
[8118303] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
[75f3522] | 10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[e3fea42] | 12 | // Last Modified On : Tue Feb 4 12:29:26 2020 |
---|
| 13 | // Update Count : 22 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[8118303] | 17 | |
---|
[bd98b58] | 18 | #include "invoke.h" |
---|
[73abe95] | 19 | #include "time_t.hfa" |
---|
[d76bd79] | 20 | #include "coroutine.hfa" |
---|
[bd98b58] | 21 | |
---|
[1eb239e4] | 22 | #include "containers/list.hfa" |
---|
[64a7146] | 23 | |
---|
[8def349] | 24 | extern "C" { |
---|
[c402739f] | 25 | #include <bits/pthreadtypes.h> |
---|
[454f478] | 26 | #include <pthread.h> |
---|
[c402739f] | 27 | #include <linux/types.h> |
---|
[8def349] | 28 | } |
---|
| 29 | |
---|
[454f478] | 30 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 31 | extern bool __cfaabi_dbg_in_kernel(); |
---|
| 32 | #endif |
---|
| 33 | |
---|
[78da4ab] | 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 | |
---|
[bd98b58] | 46 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 47 | // Processor |
---|
[de6319f] | 48 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 49 | |
---|
[9b1dcc2] | 50 | // Processor id, required for scheduling threads |
---|
[8834751] | 51 | |
---|
[9b1dcc2] | 52 | |
---|
[094476d] | 53 | coroutine processorCtx_t { |
---|
| 54 | struct processor * proc; |
---|
| 55 | }; |
---|
| 56 | |
---|
[7cf3b1d] | 57 | |
---|
| 58 | struct __fd_waitctx { |
---|
| 59 | volatile int fd; |
---|
| 60 | }; |
---|
| 61 | |
---|
[e60e0dc] | 62 | // Wrapper around kernel threads |
---|
[37ba662] | 63 | struct __attribute__((aligned(128))) processor { |
---|
[025278e] | 64 | // Cluster from which to get threads |
---|
[de94a60] | 65 | struct cluster * cltr; |
---|
[025278e] | 66 | |
---|
[431cd4f] | 67 | // Ready Queue state per processor |
---|
| 68 | struct { |
---|
| 69 | unsigned short its; |
---|
| 70 | unsigned short itr; |
---|
| 71 | unsigned id; |
---|
| 72 | unsigned target; |
---|
[12daa43] | 73 | unsigned last; |
---|
[a2a4566] | 74 | signed cpu; |
---|
[431cd4f] | 75 | } rdq; |
---|
[bd0bdd37] | 76 | |
---|
[37ba662] | 77 | // Set to true to notify the processor should terminate |
---|
| 78 | volatile bool do_terminate; |
---|
| 79 | |
---|
| 80 | // Coroutine ctx who does keeps the state of the processor |
---|
| 81 | struct processorCtx_t runner; |
---|
| 82 | |
---|
[de6319f] | 83 | // Name of the processor |
---|
| 84 | const char * name; |
---|
| 85 | |
---|
[025278e] | 86 | // Handle to pthreads |
---|
| 87 | pthread_t kernel_thread; |
---|
[2ac095d] | 88 | |
---|
[c993b15] | 89 | // Unique id for the processor (not per cluster) |
---|
| 90 | unsigned unique_id; |
---|
| 91 | |
---|
[78da4ab] | 92 | struct { |
---|
[dddb3dd0] | 93 | $io_context * ctx; |
---|
| 94 | bool pending; |
---|
| 95 | bool dirty; |
---|
[78da4ab] | 96 | } io; |
---|
| 97 | |
---|
[e60e0dc] | 98 | // Preemption data |
---|
[025278e] | 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; |
---|
[c81ebf9] | 104 | |
---|
[92e7631] | 105 | // Idle lock (kernel semaphore) |
---|
[1757f98] | 106 | int idle_fd; |
---|
[85b1deb] | 107 | |
---|
[7cf3b1d] | 108 | // Idle waitctx |
---|
| 109 | struct __fd_waitctx idle_wctx; |
---|
| 110 | |
---|
[92e7631] | 111 | // Termination synchronisation (user semaphore) |
---|
[454f478] | 112 | oneshot terminated; |
---|
[de94a60] | 113 | |
---|
[27f5f71] | 114 | // pthread Stack |
---|
| 115 | void * stack; |
---|
| 116 | |
---|
[de94a60] | 117 | // Link lists fields |
---|
[69914cbc] | 118 | inline dlink(processor); |
---|
[14a61b5] | 119 | |
---|
[a1538cd] | 120 | // special init fields |
---|
| 121 | // This is needed for memcached integration |
---|
| 122 | // once memcached experiments are done this should probably be removed |
---|
| 123 | // it is not a particularly safe scheme as it can make processors less homogeneous |
---|
| 124 | struct { |
---|
[e84ab3d] | 125 | thread$ * thrd; |
---|
[a1538cd] | 126 | } init; |
---|
| 127 | |
---|
[a67c5b6] | 128 | struct KernelThreadData * local_data; |
---|
| 129 | |
---|
[c34ebf2] | 130 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[69fbc61] | 131 | int print_stats; |
---|
[c34ebf2] | 132 | bool print_halts; |
---|
| 133 | #endif |
---|
| 134 | |
---|
[e60e0dc] | 135 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 136 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 137 | const char * last_enable; |
---|
[e60e0dc] | 138 | #endif |
---|
[c84e80a] | 139 | }; |
---|
[69914cbc] | 140 | P9_EMBEDDED( processor, dlink(processor) ) |
---|
[c84e80a] | 141 | |
---|
[a5e7233] | 142 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
[242a902] | 143 | void ^?{}(processor & this); |
---|
[c84e80a] | 144 | |
---|
[a5e7233] | 145 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
| 146 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
| 147 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster}; } |
---|
[de6319f] | 148 | |
---|
[7768b8d] | 149 | //----------------------------------------------------------------------------- |
---|
| 150 | // Cluster Tools |
---|
[dca5802] | 151 | |
---|
[9cc3a18] | 152 | // Intrusives lanes which are used by the ready queue |
---|
[61d7bec] | 153 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
[dca5802] | 154 | void ?{}(__intrusive_lane_t & this); |
---|
| 155 | void ^?{}(__intrusive_lane_t & this); |
---|
[7768b8d] | 156 | |
---|
[9cc3a18] | 157 | // Aligned timestamps which are used by the relaxed ready queue |
---|
[8cd40bf] | 158 | struct __attribute__((aligned(128))) __timestamp_t { |
---|
| 159 | volatile unsigned long long tv; |
---|
[089d30c] | 160 | volatile unsigned long long ma; |
---|
[8cd40bf] | 161 | }; |
---|
| 162 | |
---|
[0fb3ee5] | 163 | struct __attribute__((aligned(16))) __cache_id_t { |
---|
[a2a4566] | 164 | volatile unsigned id; |
---|
| 165 | }; |
---|
| 166 | |
---|
[089d30c] | 167 | // Aligned timestamps which are used by the relaxed ready queue |
---|
| 168 | struct __attribute__((aligned(128))) __help_cnts_t { |
---|
| 169 | volatile unsigned long long src; |
---|
| 170 | volatile unsigned long long dst; |
---|
| 171 | volatile unsigned long long tri; |
---|
| 172 | }; |
---|
| 173 | |
---|
| 174 | static inline void ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; } |
---|
[a892e61] | 175 | static inline void ^?{}(__timestamp_t &) {} |
---|
[9cc3a18] | 176 | |
---|
[a2a4566] | 177 | struct __attribute__((aligned(128))) __ready_queue_caches_t; |
---|
| 178 | void ?{}(__ready_queue_caches_t & this); |
---|
| 179 | void ^?{}(__ready_queue_caches_t & this); |
---|
| 180 | |
---|
[b798713] | 181 | //TODO adjust cache size to ARCHITECTURE |
---|
[a2a4566] | 182 | // Structure holding the ready queue |
---|
[37ba662] | 183 | struct __ready_queue_t { |
---|
[dca5802] | 184 | // Data tracking the actual lanes |
---|
| 185 | // On a seperate cacheline from the used struct since |
---|
| 186 | // used can change on each push/pop but this data |
---|
| 187 | // only changes on shrink/grow |
---|
[37ba662] | 188 | struct { |
---|
[dca5802] | 189 | // Arary of lanes |
---|
| 190 | __intrusive_lane_t * volatile data; |
---|
| 191 | |
---|
[9cc3a18] | 192 | // Array of times |
---|
| 193 | __timestamp_t * volatile tscs; |
---|
| 194 | |
---|
[a2a4566] | 195 | __cache_id_t * volatile caches; |
---|
| 196 | |
---|
[089d30c] | 197 | // Array of stats |
---|
| 198 | __help_cnts_t * volatile help; |
---|
| 199 | |
---|
[dca5802] | 200 | // Number of lanes (empty or not) |
---|
[b798713] | 201 | volatile size_t count; |
---|
[dca5802] | 202 | } lanes; |
---|
[b798713] | 203 | }; |
---|
| 204 | |
---|
| 205 | void ?{}(__ready_queue_t & this); |
---|
| 206 | void ^?{}(__ready_queue_t & this); |
---|
[8cd5434] | 207 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 208 | unsigned cnt(const __ready_queue_t & this, unsigned idx); |
---|
| 209 | #endif |
---|
[b798713] | 210 | |
---|
[1eb239e4] | 211 | // Idle Sleep |
---|
[6a9b12b] | 212 | struct __cluster_proc_list { |
---|
[1eb239e4] | 213 | // Spin lock protecting the queue |
---|
[34b8cb7] | 214 | __spinlock_t lock; |
---|
| 215 | |
---|
| 216 | // FD to use to wake a processor |
---|
[7cf3b1d] | 217 | struct __fd_waitctx * volatile fdw; |
---|
[1eb239e4] | 218 | |
---|
| 219 | // Total number of processors |
---|
| 220 | unsigned total; |
---|
| 221 | |
---|
| 222 | // Total number of idle processors |
---|
| 223 | unsigned idle; |
---|
| 224 | |
---|
| 225 | // List of idle processors |
---|
[69914cbc] | 226 | dlist(processor) idles; |
---|
[fc59b580] | 227 | |
---|
| 228 | // List of active processors |
---|
[69914cbc] | 229 | dlist(processor) actives; |
---|
[1eb239e4] | 230 | }; |
---|
| 231 | |
---|
[de94a60] | 232 | //----------------------------------------------------------------------------- |
---|
| 233 | // Cluster |
---|
[37ba662] | 234 | struct __attribute__((aligned(128))) cluster { |
---|
[de94a60] | 235 | // Ready queue for threads |
---|
[b798713] | 236 | __ready_queue_t ready_queue; |
---|
[de94a60] | 237 | |
---|
| 238 | // Name of the cluster |
---|
| 239 | const char * name; |
---|
| 240 | |
---|
| 241 | // Preemption rate on this cluster |
---|
| 242 | Duration preemption_rate; |
---|
| 243 | |
---|
[64a7146] | 244 | // List of idle processors |
---|
[6a9b12b] | 245 | __cluster_proc_list procs; |
---|
[de94a60] | 246 | |
---|
[d4e68a6] | 247 | // List of threads |
---|
[a1a17a74] | 248 | __spinlock_t thread_list_lock; |
---|
[e84ab3d] | 249 | __dllist_t(struct thread$) threads; |
---|
[d4e68a6] | 250 | unsigned int nthreads; |
---|
[a1a17a74] | 251 | |
---|
[de94a60] | 252 | // Link lists fields |
---|
[ea8b2f7] | 253 | struct __dbg_node_cltr { |
---|
[de94a60] | 254 | cluster * next; |
---|
| 255 | cluster * prev; |
---|
| 256 | } node; |
---|
[92976d9] | 257 | |
---|
[f00b26d4] | 258 | struct { |
---|
[78da4ab] | 259 | $io_arbiter * arbiter; |
---|
| 260 | io_context_params params; |
---|
[f00b26d4] | 261 | } io; |
---|
[038be32] | 262 | |
---|
| 263 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[8834751] | 264 | struct __stats_t * stats; |
---|
[69fbc61] | 265 | int print_stats; |
---|
[038be32] | 266 | #endif |
---|
[de94a60] | 267 | }; |
---|
| 268 | extern Duration default_preemption(); |
---|
| 269 | |
---|
[f00b26d4] | 270 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); |
---|
[de94a60] | 271 | void ^?{}(cluster & this); |
---|
| 272 | |
---|
[f00b26d4] | 273 | static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; } |
---|
| 274 | static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; } |
---|
| 275 | static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; } |
---|
| 276 | static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } |
---|
| 277 | 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}; } |
---|
| 278 | static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; } |
---|
| 279 | static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } |
---|
| 280 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } |
---|
| 281 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; } |
---|
| 282 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } |
---|
| 283 | 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}; } |
---|
| 284 | 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}; } |
---|
[de94a60] | 285 | |
---|
[c7a900a] | 286 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 287 | |
---|
[8fc652e0] | 288 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE |
---|
| 289 | static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; } |
---|
[d4e68a6] | 290 | |
---|
[038be32] | 291 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[8fc652e0] | 292 | void print_stats_now( cluster & this, int flags ); |
---|
| 293 | |
---|
[69fbc61] | 294 | static inline void print_stats_at_exit( cluster & this, int flags ) { |
---|
| 295 | this.print_stats |= flags; |
---|
[038be32] | 296 | } |
---|
[c34ebf2] | 297 | |
---|
[69fbc61] | 298 | static inline void print_stats_at_exit( processor & this, int flags ) { |
---|
| 299 | this.print_stats |= flags; |
---|
[c34ebf2] | 300 | } |
---|
| 301 | |
---|
| 302 | void print_halts( processor & this ); |
---|
[038be32] | 303 | #endif |
---|
| 304 | |
---|
[8118303] | 305 | // Local Variables: // |
---|
[6b0b624] | 306 | // mode: c // |
---|
| 307 | // tab-width: 4 // |
---|
[8118303] | 308 | // End: // |
---|