| [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 | // | 
|---|
| [75a17f1] | 7 | // kernel -- | 
|---|
| [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" { | 
|---|
| [3e2b9c9] | 25 | #include <bits/pthreadtypes.h> | 
|---|
| [8def349] | 26 | } | 
|---|
|  | 27 |  | 
|---|
| [db6f06a] | 28 | //----------------------------------------------------------------------------- | 
|---|
|  | 29 | // Locks | 
|---|
| [bdeba0b] | 30 | struct semaphore { | 
|---|
| [ea7d2b0] | 31 | __spinlock_t lock; | 
|---|
| [bdeba0b] | 32 | int count; | 
|---|
| [ac2b598] | 33 | __queue_t($thread) waiting; | 
|---|
| [9c31349] | 34 | }; | 
|---|
|  | 35 |  | 
|---|
| [242a902] | 36 | void  ?{}(semaphore & this, int count = 1); | 
|---|
|  | 37 | void ^?{}(semaphore & this); | 
|---|
| [71c8b7e] | 38 | bool   P (semaphore & this); | 
|---|
| [f0ce5f4] | 39 | bool   V (semaphore & this); | 
|---|
| [d384787] | 40 | bool   V (semaphore & this, unsigned count); | 
|---|
| [9c31349] | 41 |  | 
|---|
| [db6f06a] | 42 |  | 
|---|
| [bd98b58] | 43 | //----------------------------------------------------------------------------- | 
|---|
| [de94a60] | 44 | // Processor | 
|---|
| [de6319f] | 45 | extern struct cluster * mainCluster; | 
|---|
| [bd98b58] | 46 |  | 
|---|
| [9b1dcc2] | 47 | // Processor id, required for scheduling threads | 
|---|
|  | 48 | struct __processor_id_t { | 
|---|
|  | 49 | unsigned id; | 
|---|
| [8834751] | 50 |  | 
|---|
|  | 51 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
|  | 52 | struct __stats_t * stats; | 
|---|
|  | 53 | #endif | 
|---|
| [9b1dcc2] | 54 | }; | 
|---|
|  | 55 |  | 
|---|
| [094476d] | 56 | coroutine processorCtx_t { | 
|---|
|  | 57 | struct processor * proc; | 
|---|
|  | 58 | }; | 
|---|
|  | 59 |  | 
|---|
| [e60e0dc] | 60 | // Wrapper around kernel threads | 
|---|
| [37ba662] | 61 | struct __attribute__((aligned(128))) processor { | 
|---|
| [e60e0dc] | 62 | // Main state | 
|---|
| [37ba662] | 63 | inline __processor_id_t; | 
|---|
| [025278e] | 64 |  | 
|---|
|  | 65 | // Cluster from which to get threads | 
|---|
| [de94a60] | 66 | struct cluster * cltr; | 
|---|
| [025278e] | 67 |  | 
|---|
| [37ba662] | 68 | // Set to true to notify the processor should terminate | 
|---|
|  | 69 | volatile bool do_terminate; | 
|---|
|  | 70 |  | 
|---|
|  | 71 | // Coroutine ctx who does keeps the state of the processor | 
|---|
|  | 72 | struct processorCtx_t runner; | 
|---|
|  | 73 |  | 
|---|
| [de6319f] | 74 | // Name of the processor | 
|---|
|  | 75 | const char * name; | 
|---|
|  | 76 |  | 
|---|
| [025278e] | 77 | // Handle to pthreads | 
|---|
|  | 78 | pthread_t kernel_thread; | 
|---|
| [2ac095d] | 79 |  | 
|---|
| [e60e0dc] | 80 | // RunThread data | 
|---|
| [025278e] | 81 | // Action to do after a thread is ran | 
|---|
| [ac2b598] | 82 | $thread * destroyer; | 
|---|
| [c81ebf9] | 83 |  | 
|---|
| [e60e0dc] | 84 | // Preemption data | 
|---|
| [025278e] | 85 | // Node which is added in the discrete event simulaiton | 
|---|
|  | 86 | struct alarm_node_t * preemption_alarm; | 
|---|
|  | 87 |  | 
|---|
|  | 88 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible | 
|---|
|  | 89 | bool pending_preemption; | 
|---|
| [c81ebf9] | 90 |  | 
|---|
| [92e7631] | 91 | // Idle lock (kernel semaphore) | 
|---|
|  | 92 | __bin_sem_t idle; | 
|---|
| [85b1deb] | 93 |  | 
|---|
| [92e7631] | 94 | // Termination synchronisation (user semaphore) | 
|---|
| [85b1deb] | 95 | semaphore terminated; | 
|---|
| [de94a60] | 96 |  | 
|---|
| [27f5f71] | 97 | // pthread Stack | 
|---|
|  | 98 | void * stack; | 
|---|
|  | 99 |  | 
|---|
| [de94a60] | 100 | // Link lists fields | 
|---|
| [1eb239e4] | 101 | DLISTED_MGD_IMPL_IN(processor) | 
|---|
| [14a61b5] | 102 |  | 
|---|
| [c34ebf2] | 103 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [69fbc61] | 104 | int print_stats; | 
|---|
| [c34ebf2] | 105 | bool print_halts; | 
|---|
|  | 106 | #endif | 
|---|
|  | 107 |  | 
|---|
| [e60e0dc] | 108 | #ifdef __CFA_DEBUG__ | 
|---|
| [025278e] | 109 | // Last function to enable preemption on this processor | 
|---|
| [cdbfab0] | 110 | const char * last_enable; | 
|---|
| [e60e0dc] | 111 | #endif | 
|---|
| [c84e80a] | 112 | }; | 
|---|
|  | 113 |  | 
|---|
| [e3fea42] | 114 | void  ?{}(processor & this, const char name[], struct cluster & cltr); | 
|---|
| [242a902] | 115 | void ^?{}(processor & this); | 
|---|
| [c84e80a] | 116 |  | 
|---|
| [de6319f] | 117 | static inline void  ?{}(processor & this)                    { this{ "Anonymous Processor", *mainCluster}; } | 
|---|
| [de94a60] | 118 | static inline void  ?{}(processor & this, struct cluster & cltr)    { this{ "Anonymous Processor", cltr}; } | 
|---|
| [e3fea42] | 119 | static inline void  ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } | 
|---|
| [de6319f] | 120 |  | 
|---|
| [1eb239e4] | 121 | DLISTED_MGD_IMPL_OUT(processor) | 
|---|
| [de94a60] | 122 |  | 
|---|
| [92976d9] | 123 | //----------------------------------------------------------------------------- | 
|---|
|  | 124 | // I/O | 
|---|
| [61dd73d] | 125 | struct __io_data; | 
|---|
| [92976d9] | 126 |  | 
|---|
| [f00b26d4] | 127 | // IO poller user-thread | 
|---|
|  | 128 | // Not using the "thread" keyword because we want to control | 
|---|
|  | 129 | // more carefully when to start/stop it | 
|---|
|  | 130 | struct $io_ctx_thread { | 
|---|
|  | 131 | struct __io_data * ring; | 
|---|
|  | 132 | single_sem sem; | 
|---|
|  | 133 | volatile bool done; | 
|---|
|  | 134 | $thread self; | 
|---|
|  | 135 | }; | 
|---|
|  | 136 |  | 
|---|
|  | 137 |  | 
|---|
|  | 138 | struct io_context { | 
|---|
|  | 139 | $io_ctx_thread thrd; | 
|---|
|  | 140 | }; | 
|---|
|  | 141 |  | 
|---|
|  | 142 | struct io_context_params { | 
|---|
|  | 143 | int num_entries; | 
|---|
|  | 144 | int num_ready; | 
|---|
|  | 145 | int submit_aff; | 
|---|
|  | 146 | bool eager_submits:1; | 
|---|
|  | 147 | bool poller_submits:1; | 
|---|
|  | 148 | bool poll_submit:1; | 
|---|
|  | 149 | bool poll_complete:1; | 
|---|
|  | 150 | }; | 
|---|
| [de94a60] | 151 |  | 
|---|
| [f00b26d4] | 152 | void  ?{}(io_context_params & this); | 
|---|
|  | 153 |  | 
|---|
|  | 154 | void  ?{}(io_context & this, struct cluster & cl); | 
|---|
|  | 155 | void  ?{}(io_context & this, struct cluster & cl, const io_context_params & params); | 
|---|
|  | 156 | void ^?{}(io_context & this); | 
|---|
|  | 157 |  | 
|---|
|  | 158 | struct io_cancellation { | 
|---|
|  | 159 | uint32_t target; | 
|---|
|  | 160 | }; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | static inline void  ?{}(io_cancellation & this) { this.target = -1u; } | 
|---|
|  | 163 | static inline void ^?{}(io_cancellation & this) {} | 
|---|
|  | 164 | bool cancel(io_cancellation & this); | 
|---|
| [7768b8d] | 165 |  | 
|---|
|  | 166 | //----------------------------------------------------------------------------- | 
|---|
|  | 167 | // Cluster Tools | 
|---|
| [dca5802] | 168 |  | 
|---|
|  | 169 | // Intrusives lanes which are used by the relaxed ready queue | 
|---|
| [61d7bec] | 170 | struct __attribute__((aligned(128))) __intrusive_lane_t; | 
|---|
| [dca5802] | 171 | void  ?{}(__intrusive_lane_t & this); | 
|---|
|  | 172 | void ^?{}(__intrusive_lane_t & this); | 
|---|
| [7768b8d] | 173 |  | 
|---|
| [61d7bec] | 174 | // Counter used for wether or not the lanes are all empty | 
|---|
|  | 175 | struct __attribute__((aligned(128))) __snzi_node_t; | 
|---|
|  | 176 | struct __snzi_t { | 
|---|
|  | 177 | unsigned mask; | 
|---|
|  | 178 | int root; | 
|---|
|  | 179 | __snzi_node_t * nodes; | 
|---|
|  | 180 | }; | 
|---|
| [b798713] | 181 |  | 
|---|
| [61d7bec] | 182 | void  ?{}( __snzi_t & this, unsigned depth ); | 
|---|
|  | 183 | void ^?{}( __snzi_t & this ); | 
|---|
| [b798713] | 184 |  | 
|---|
|  | 185 | //TODO adjust cache size to ARCHITECTURE | 
|---|
| [dca5802] | 186 | // Structure holding the relaxed ready queue | 
|---|
| [37ba662] | 187 | struct __ready_queue_t { | 
|---|
| [dca5802] | 188 | // Data tracking how many/which lanes are used | 
|---|
|  | 189 | // Aligned to 128 for cache locality | 
|---|
| [61d7bec] | 190 | __snzi_t snzi; | 
|---|
| [dca5802] | 191 |  | 
|---|
|  | 192 | // Data tracking the actual lanes | 
|---|
|  | 193 | // On a seperate cacheline from the used struct since | 
|---|
|  | 194 | // used can change on each push/pop but this data | 
|---|
|  | 195 | // only changes on shrink/grow | 
|---|
| [37ba662] | 196 | struct { | 
|---|
| [dca5802] | 197 | // Arary of lanes | 
|---|
|  | 198 | __intrusive_lane_t * volatile data; | 
|---|
|  | 199 |  | 
|---|
|  | 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); | 
|---|
|  | 207 |  | 
|---|
| [1eb239e4] | 208 | // Idle Sleep | 
|---|
|  | 209 | struct __cluster_idles { | 
|---|
|  | 210 | // Spin lock protecting the queue | 
|---|
|  | 211 | volatile uint64_t lock; | 
|---|
|  | 212 |  | 
|---|
|  | 213 | // Total number of processors | 
|---|
|  | 214 | unsigned total; | 
|---|
|  | 215 |  | 
|---|
|  | 216 | // Total number of idle processors | 
|---|
|  | 217 | unsigned idle; | 
|---|
|  | 218 |  | 
|---|
|  | 219 | // List of idle processors | 
|---|
|  | 220 | dlist(processor, processor) list; | 
|---|
|  | 221 | }; | 
|---|
|  | 222 |  | 
|---|
| [de94a60] | 223 | //----------------------------------------------------------------------------- | 
|---|
|  | 224 | // Cluster | 
|---|
| [37ba662] | 225 | struct __attribute__((aligned(128))) cluster { | 
|---|
| [de94a60] | 226 | // Ready queue for threads | 
|---|
| [b798713] | 227 | __ready_queue_t ready_queue; | 
|---|
| [de94a60] | 228 |  | 
|---|
|  | 229 | // Name of the cluster | 
|---|
|  | 230 | const char * name; | 
|---|
|  | 231 |  | 
|---|
|  | 232 | // Preemption rate on this cluster | 
|---|
|  | 233 | Duration preemption_rate; | 
|---|
|  | 234 |  | 
|---|
| [64a7146] | 235 | // List of idle processors | 
|---|
| [1eb239e4] | 236 | __cluster_idles idles; | 
|---|
| [de94a60] | 237 |  | 
|---|
| [d4e68a6] | 238 | // List of threads | 
|---|
| [a1a17a74] | 239 | __spinlock_t thread_list_lock; | 
|---|
| [ac2b598] | 240 | __dllist_t(struct $thread) threads; | 
|---|
| [d4e68a6] | 241 | unsigned int nthreads; | 
|---|
| [a1a17a74] | 242 |  | 
|---|
| [de94a60] | 243 | // Link lists fields | 
|---|
| [ea8b2f7] | 244 | struct __dbg_node_cltr { | 
|---|
| [de94a60] | 245 | cluster * next; | 
|---|
|  | 246 | cluster * prev; | 
|---|
|  | 247 | } node; | 
|---|
| [92976d9] | 248 |  | 
|---|
| [f00b26d4] | 249 | struct { | 
|---|
|  | 250 | io_context * ctxs; | 
|---|
|  | 251 | unsigned cnt; | 
|---|
|  | 252 | } io; | 
|---|
| [038be32] | 253 |  | 
|---|
|  | 254 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [8834751] | 255 | struct __stats_t * stats; | 
|---|
| [69fbc61] | 256 | int print_stats; | 
|---|
| [038be32] | 257 | #endif | 
|---|
| [de94a60] | 258 | }; | 
|---|
|  | 259 | extern Duration default_preemption(); | 
|---|
|  | 260 |  | 
|---|
| [f00b26d4] | 261 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); | 
|---|
| [de94a60] | 262 | void ^?{}(cluster & this); | 
|---|
|  | 263 |  | 
|---|
| [f00b26d4] | 264 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; } | 
|---|
|  | 265 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; } | 
|---|
|  | 266 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; } | 
|---|
|  | 267 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } | 
|---|
|  | 268 | 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}; } | 
|---|
|  | 269 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; } | 
|---|
|  | 270 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } | 
|---|
|  | 271 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } | 
|---|
|  | 272 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; } | 
|---|
|  | 273 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } | 
|---|
|  | 274 | 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}; } | 
|---|
|  | 275 | 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] | 276 |  | 
|---|
| [c7a900a] | 277 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| [de94a60] | 278 |  | 
|---|
| [d4e68a6] | 279 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE | 
|---|
|  | 280 | static inline struct cluster   * active_cluster  () { return TL_GET( this_processor )->cltr; } | 
|---|
|  | 281 |  | 
|---|
| [038be32] | 282 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [69fbc61] | 283 | static inline void print_stats_at_exit( cluster & this, int flags ) { | 
|---|
|  | 284 | this.print_stats |= flags; | 
|---|
| [038be32] | 285 | } | 
|---|
| [c34ebf2] | 286 |  | 
|---|
| [69fbc61] | 287 | static inline void print_stats_at_exit( processor & this, int flags ) { | 
|---|
|  | 288 | this.print_stats |= flags; | 
|---|
| [c34ebf2] | 289 | } | 
|---|
|  | 290 |  | 
|---|
|  | 291 | void print_halts( processor & this ); | 
|---|
| [038be32] | 292 | #endif | 
|---|
|  | 293 |  | 
|---|
| [8118303] | 294 | // Local Variables: // | 
|---|
| [6b0b624] | 295 | // mode: c // | 
|---|
|  | 296 | // tab-width: 4 // | 
|---|
| [8118303] | 297 | // End: // | 
|---|