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