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