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