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