| [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 | 
|---|
| [55b060d] | 12 | // Last Modified On : Wed Aug 30 21:28:46 2023 | 
|---|
|  | 13 | // Update Count     : 23 | 
|---|
| [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 |  | 
|---|
| [55b060d] | 22 | #include "collections/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; | 
|---|
| [8bee858] | 37 | struct io_context$; | 
|---|
|  | 38 | struct io_arbiter$; | 
|---|
| [78da4ab] | 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 |  | 
|---|
| [22226e4] | 50 | // Coroutine used py processors for the 2-step context switch | 
|---|
| [c18bf9e] | 51 |  | 
|---|
|  | 52 | struct processorCtx_t { | 
|---|
|  | 53 | struct coroutine$ self; | 
|---|
| [094476d] | 54 | struct processor * proc; | 
|---|
|  | 55 | }; | 
|---|
|  | 56 |  | 
|---|
| [22226e4] | 57 | struct io_future_t; | 
|---|
| [7cf3b1d] | 58 |  | 
|---|
| [22226e4] | 59 | // Information needed for idle sleep | 
|---|
| [7cf3b1d] | 60 | struct __fd_waitctx { | 
|---|
| [22226e4] | 61 | // semaphore/future like object | 
|---|
|  | 62 | // values can be 0, 1 or some file descriptor. | 
|---|
|  | 63 | // 0 - is the default state | 
|---|
|  | 64 | // 1 - means the proc should wake-up immediately | 
|---|
|  | 65 | // FD - means the proc is going asleep and should be woken by writing to the FD. | 
|---|
| [a757ba1] | 66 | //      The FD value should always be the evfd field just below. | 
|---|
| [22226e4] | 67 | volatile int sem; | 
|---|
|  | 68 |  | 
|---|
|  | 69 | // The event FD that corresponds to this processor | 
|---|
|  | 70 | int evfd; | 
|---|
|  | 71 |  | 
|---|
| [a757ba1] | 72 | // Used for debugging, should be removed eventually. | 
|---|
| [efa28d5] | 73 | volatile unsigned long long wake__time; | 
|---|
| [262fafd9] | 74 | volatile unsigned long long sleep_time; | 
|---|
| [d28b70a] | 75 | volatile unsigned long long drain_time; | 
|---|
| [7cf3b1d] | 76 | }; | 
|---|
|  | 77 |  | 
|---|
| [e60e0dc] | 78 | // Wrapper around kernel threads | 
|---|
| [2af1943] | 79 | struct __attribute__((aligned(64))) processor { | 
|---|
| [025278e] | 80 | // Cluster from which to get threads | 
|---|
| [de94a60] | 81 | struct cluster * cltr; | 
|---|
| [025278e] | 82 |  | 
|---|
| [431cd4f] | 83 | // Ready Queue state per processor | 
|---|
|  | 84 | struct { | 
|---|
|  | 85 | unsigned short its; | 
|---|
|  | 86 | unsigned short itr; | 
|---|
|  | 87 | unsigned id; | 
|---|
|  | 88 | unsigned target; | 
|---|
| [12daa43] | 89 | unsigned last; | 
|---|
| [a2a4566] | 90 | signed   cpu; | 
|---|
| [431cd4f] | 91 | } rdq; | 
|---|
| [bd0bdd37] | 92 |  | 
|---|
| [37ba662] | 93 | // Set to true to notify the processor should terminate | 
|---|
|  | 94 | volatile bool do_terminate; | 
|---|
|  | 95 |  | 
|---|
|  | 96 | // Coroutine ctx who does keeps the state of the processor | 
|---|
|  | 97 | struct processorCtx_t runner; | 
|---|
|  | 98 |  | 
|---|
| [de6319f] | 99 | // Name of the processor | 
|---|
|  | 100 | const char * name; | 
|---|
|  | 101 |  | 
|---|
| [025278e] | 102 | // Handle to pthreads | 
|---|
|  | 103 | pthread_t kernel_thread; | 
|---|
| [2ac095d] | 104 |  | 
|---|
| [c993b15] | 105 | // Unique id for the processor (not per cluster) | 
|---|
|  | 106 | unsigned unique_id; | 
|---|
|  | 107 |  | 
|---|
| [78da4ab] | 108 | struct { | 
|---|
| [8bee858] | 109 | io_context$ * ctx; | 
|---|
| [adb3ea1] | 110 | unsigned target; | 
|---|
| [d529ad0] | 111 | volatile bool pending; | 
|---|
|  | 112 | volatile bool dirty; | 
|---|
| [78da4ab] | 113 | } io; | 
|---|
|  | 114 |  | 
|---|
| [e60e0dc] | 115 | // Preemption data | 
|---|
| [025278e] | 116 | // Node which is added in the discrete event simulaiton | 
|---|
|  | 117 | struct alarm_node_t * preemption_alarm; | 
|---|
|  | 118 |  | 
|---|
|  | 119 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible | 
|---|
|  | 120 | bool pending_preemption; | 
|---|
| [c81ebf9] | 121 |  | 
|---|
| [22226e4] | 122 | // context for idle sleep | 
|---|
| [7cf3b1d] | 123 | struct __fd_waitctx idle_wctx; | 
|---|
|  | 124 |  | 
|---|
| [92e7631] | 125 | // Termination synchronisation (user semaphore) | 
|---|
| [454f478] | 126 | oneshot terminated; | 
|---|
| [de94a60] | 127 |  | 
|---|
| [27f5f71] | 128 | // pthread Stack | 
|---|
|  | 129 | void * stack; | 
|---|
|  | 130 |  | 
|---|
| [de94a60] | 131 | // Link lists fields | 
|---|
| [160f1aa] | 132 | dlink(processor) link; | 
|---|
| [14a61b5] | 133 |  | 
|---|
| [a1538cd] | 134 | // special init fields | 
|---|
|  | 135 | // This is needed for memcached integration | 
|---|
|  | 136 | // once memcached experiments are done this should probably be removed | 
|---|
|  | 137 | // it is not a particularly safe scheme as it can make processors less homogeneous | 
|---|
|  | 138 | struct { | 
|---|
| [e84ab3d] | 139 | thread$ * thrd; | 
|---|
| [a1538cd] | 140 | } init; | 
|---|
|  | 141 |  | 
|---|
| [a67c5b6] | 142 | struct KernelThreadData * local_data; | 
|---|
|  | 143 |  | 
|---|
| [c34ebf2] | 144 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [69fbc61] | 145 | int print_stats; | 
|---|
| [c34ebf2] | 146 | bool print_halts; | 
|---|
|  | 147 | #endif | 
|---|
|  | 148 |  | 
|---|
| [e60e0dc] | 149 | #ifdef __CFA_DEBUG__ | 
|---|
| [025278e] | 150 | // Last function to enable preemption on this processor | 
|---|
| [cdbfab0] | 151 | const char * last_enable; | 
|---|
| [e60e0dc] | 152 | #endif | 
|---|
| [c84e80a] | 153 | }; | 
|---|
| [160f1aa] | 154 | // P9_EMBEDDED( processor, dlink(processor) ) | 
|---|
|  | 155 | static inline tytagref( dlink(processor), dlink(processor) ) ?`inner( processor & this ) { | 
|---|
| [8b74fa7] | 156 | dlink(processor) & b = this.link; | 
|---|
|  | 157 | tytagref( dlink(processor), dlink(processor) ) result = { b }; | 
|---|
|  | 158 | return result; | 
|---|
| [160f1aa] | 159 | } | 
|---|
| [c84e80a] | 160 |  | 
|---|
| [a5e7233] | 161 | void  ?{}(processor & this, const char name[], struct cluster & cltr); | 
|---|
| [242a902] | 162 | void ^?{}(processor & this); | 
|---|
| [c84e80a] | 163 |  | 
|---|
| [a5e7233] | 164 | static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; } | 
|---|
|  | 165 | static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } | 
|---|
|  | 166 | static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; } | 
|---|
| [de6319f] | 167 |  | 
|---|
| [7768b8d] | 168 | //----------------------------------------------------------------------------- | 
|---|
|  | 169 | // Cluster Tools | 
|---|
| [dca5802] | 170 |  | 
|---|
| [9cc3a18] | 171 | // Intrusives lanes which are used by the ready queue | 
|---|
| [2af1943] | 172 | union __attribute__((aligned(64))) __intrusive_lane_t; | 
|---|
| [dca5802] | 173 | void  ?{}(__intrusive_lane_t & this); | 
|---|
|  | 174 | void ^?{}(__intrusive_lane_t & this); | 
|---|
| [7768b8d] | 175 |  | 
|---|
| [884f3f67] | 176 | // Aligned timestamps which are used by the ready queue and io subsystem | 
|---|
| [31c967b] | 177 | union __attribute__((aligned(64))) __timestamp_t; | 
|---|
| [8cd40bf] | 178 |  | 
|---|
| [31c967b] | 179 | void  ?{}(__timestamp_t & this); | 
|---|
|  | 180 | void ^?{}(__timestamp_t &); | 
|---|
| [9cc3a18] | 181 |  | 
|---|
| [dca5802] | 182 |  | 
|---|
| [884f3f67] | 183 | struct __attribute__((aligned(16))) __cache_id_t { | 
|---|
|  | 184 | volatile unsigned id; | 
|---|
|  | 185 | }; | 
|---|
| [9cc3a18] | 186 |  | 
|---|
| [1eb239e4] | 187 | // Idle Sleep | 
|---|
| [6a9b12b] | 188 | struct __cluster_proc_list { | 
|---|
| [1eb239e4] | 189 | // Spin lock protecting the queue | 
|---|
| [34b8cb7] | 190 | __spinlock_t lock; | 
|---|
|  | 191 |  | 
|---|
|  | 192 | // FD to use to wake a processor | 
|---|
| [7cf3b1d] | 193 | struct __fd_waitctx * volatile fdw; | 
|---|
| [1eb239e4] | 194 |  | 
|---|
|  | 195 | // Total number of processors | 
|---|
|  | 196 | unsigned total; | 
|---|
|  | 197 |  | 
|---|
| [ca0c311] | 198 | // Number of processors constructed | 
|---|
|  | 199 | //  incremented at construction time, total is incremented once the processor asyncronously registers | 
|---|
|  | 200 | unsigned constructed; | 
|---|
|  | 201 |  | 
|---|
| [1eb239e4] | 202 | // Total number of idle processors | 
|---|
|  | 203 | unsigned idle; | 
|---|
|  | 204 |  | 
|---|
|  | 205 | // List of idle processors | 
|---|
| [69914cbc] | 206 | dlist(processor) idles; | 
|---|
| [fc59b580] | 207 |  | 
|---|
|  | 208 | // List of active processors | 
|---|
| [69914cbc] | 209 | dlist(processor) actives; | 
|---|
| [1eb239e4] | 210 | }; | 
|---|
|  | 211 |  | 
|---|
| [de94a60] | 212 | //----------------------------------------------------------------------------- | 
|---|
|  | 213 | // Cluster | 
|---|
| [2af1943] | 214 | struct __attribute__((aligned(64))) cluster { | 
|---|
| [884f3f67] | 215 | struct { | 
|---|
|  | 216 | struct { | 
|---|
|  | 217 | // Arary of subqueues | 
|---|
| [adb3ea1] | 218 | __intrusive_lane_t * data; | 
|---|
| [884f3f67] | 219 |  | 
|---|
|  | 220 | // Time since subqueues were processed | 
|---|
| [adb3ea1] | 221 | __timestamp_t * tscs; | 
|---|
| [884f3f67] | 222 |  | 
|---|
|  | 223 | // Number of subqueue / timestamps | 
|---|
|  | 224 | size_t count; | 
|---|
|  | 225 | } readyQ; | 
|---|
|  | 226 |  | 
|---|
|  | 227 | struct { | 
|---|
| [adb3ea1] | 228 | // Array of $io_ | 
|---|
| [8bee858] | 229 | io_context$ ** data; | 
|---|
| [adb3ea1] | 230 |  | 
|---|
| [708ae38] | 231 | // Time since subqueues were processed | 
|---|
| [adb3ea1] | 232 | __timestamp_t * tscs; | 
|---|
| [708ae38] | 233 |  | 
|---|
|  | 234 | // Number of I/O subqueues | 
|---|
|  | 235 | size_t count; | 
|---|
| [884f3f67] | 236 | } io; | 
|---|
|  | 237 |  | 
|---|
|  | 238 | // Cache each kernel thread belongs to | 
|---|
| [adb3ea1] | 239 | __cache_id_t * caches; | 
|---|
| [884f3f67] | 240 | } sched; | 
|---|
|  | 241 |  | 
|---|
|  | 242 | // // Ready queue for threads | 
|---|
|  | 243 | // __ready_queue_t ready_queue; | 
|---|
| [de94a60] | 244 |  | 
|---|
|  | 245 | // Name of the cluster | 
|---|
|  | 246 | const char * name; | 
|---|
|  | 247 |  | 
|---|
|  | 248 | // Preemption rate on this cluster | 
|---|
|  | 249 | Duration preemption_rate; | 
|---|
|  | 250 |  | 
|---|
| [64a7146] | 251 | // List of idle processors | 
|---|
| [6a9b12b] | 252 | __cluster_proc_list procs; | 
|---|
| [de94a60] | 253 |  | 
|---|
| [d4e68a6] | 254 | // List of threads | 
|---|
| [a1a17a74] | 255 | __spinlock_t thread_list_lock; | 
|---|
| [639e4fc] | 256 | dlist(struct thread$, struct __thread_user_link) threads; | 
|---|
| [d4e68a6] | 257 | unsigned int nthreads; | 
|---|
| [a1a17a74] | 258 |  | 
|---|
| [de94a60] | 259 | // Link lists fields | 
|---|
| [ea8b2f7] | 260 | struct __dbg_node_cltr { | 
|---|
| [de94a60] | 261 | cluster * next; | 
|---|
|  | 262 | cluster * prev; | 
|---|
|  | 263 | } node; | 
|---|
| [92976d9] | 264 |  | 
|---|
| [f00b26d4] | 265 | struct { | 
|---|
| [8bee858] | 266 | io_arbiter$ * arbiter; | 
|---|
| [78da4ab] | 267 | io_context_params params; | 
|---|
| [f00b26d4] | 268 | } io; | 
|---|
| [038be32] | 269 |  | 
|---|
| [8b74fa7] | 270 | struct { | 
|---|
|  | 271 | struct processor ** procs; | 
|---|
|  | 272 | unsigned cnt; | 
|---|
|  | 273 | } managed; | 
|---|
|  | 274 |  | 
|---|
| [038be32] | 275 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [8834751] | 276 | struct __stats_t * stats; | 
|---|
| [69fbc61] | 277 | int print_stats; | 
|---|
| [038be32] | 278 | #endif | 
|---|
| [de94a60] | 279 | }; | 
|---|
|  | 280 | extern Duration default_preemption(); | 
|---|
|  | 281 |  | 
|---|
| [f00b26d4] | 282 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); | 
|---|
| [de94a60] | 283 | void ^?{}(cluster & this); | 
|---|
|  | 284 |  | 
|---|
| [f00b26d4] | 285 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; } | 
|---|
|  | 286 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; } | 
|---|
|  | 287 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; } | 
|---|
|  | 288 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } | 
|---|
|  | 289 | 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}; } | 
|---|
|  | 290 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; } | 
|---|
|  | 291 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } | 
|---|
|  | 292 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } | 
|---|
|  | 293 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; } | 
|---|
|  | 294 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } | 
|---|
|  | 295 | 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}; } | 
|---|
|  | 296 | 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] | 297 |  | 
|---|
| [c7a900a] | 298 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| [de94a60] | 299 |  | 
|---|
| [8fc652e0] | 300 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE | 
|---|
|  | 301 | static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; } | 
|---|
| [d4e68a6] | 302 |  | 
|---|
| [ca0c311] | 303 | // gets the number of constructed processors on the cluster | 
|---|
|  | 304 | static inline unsigned get_proc_count( cluster & this ) { return this.procs.constructed; } | 
|---|
|  | 305 |  | 
|---|
| [8b74fa7] | 306 | // set the number of internal processors | 
|---|
|  | 307 | // these processors are in addition to any explicitly declared processors | 
|---|
|  | 308 | unsigned set_concurrency( cluster & this, unsigned new_count ); | 
|---|
|  | 309 |  | 
|---|
| [038be32] | 310 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [8fc652e0] | 311 | void print_stats_now( cluster & this, int flags ); | 
|---|
|  | 312 |  | 
|---|
| [69fbc61] | 313 | static inline void print_stats_at_exit( cluster & this, int flags ) { | 
|---|
|  | 314 | this.print_stats |= flags; | 
|---|
| [038be32] | 315 | } | 
|---|
| [c34ebf2] | 316 |  | 
|---|
| [69fbc61] | 317 | static inline void print_stats_at_exit( processor & this, int flags ) { | 
|---|
|  | 318 | this.print_stats |= flags; | 
|---|
| [c34ebf2] | 319 | } | 
|---|
|  | 320 |  | 
|---|
|  | 321 | void print_halts( processor & this ); | 
|---|
| [038be32] | 322 | #endif | 
|---|
|  | 323 |  | 
|---|
| [8118303] | 324 | // Local Variables: // | 
|---|
| [6b0b624] | 325 | // mode: c // | 
|---|
|  | 326 | // tab-width: 4 // | 
|---|
| [8118303] | 327 | // End: // | 
|---|