[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 | |
---|
[9b1dcc2] | 50 | // Processor id, required for scheduling threads |
---|
| 51 | struct __processor_id_t { |
---|
[58d64a4] | 52 | unsigned id:24; |
---|
[8834751] | 53 | |
---|
| 54 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 55 | struct __stats_t * stats; |
---|
| 56 | #endif |
---|
[9b1dcc2] | 57 | }; |
---|
| 58 | |
---|
[094476d] | 59 | coroutine processorCtx_t { |
---|
| 60 | struct processor * proc; |
---|
| 61 | }; |
---|
| 62 | |
---|
[e60e0dc] | 63 | // Wrapper around kernel threads |
---|
[37ba662] | 64 | struct __attribute__((aligned(128))) processor { |
---|
[e60e0dc] | 65 | // Main state |
---|
[37ba662] | 66 | inline __processor_id_t; |
---|
[025278e] | 67 | |
---|
| 68 | // Cluster from which to get threads |
---|
[de94a60] | 69 | struct cluster * cltr; |
---|
[025278e] | 70 | |
---|
[bd0bdd37] | 71 | // Id within the cluster |
---|
| 72 | unsigned cltr_id; |
---|
| 73 | |
---|
[37ba662] | 74 | // Set to true to notify the processor should terminate |
---|
| 75 | volatile bool do_terminate; |
---|
| 76 | |
---|
| 77 | // Coroutine ctx who does keeps the state of the processor |
---|
| 78 | struct processorCtx_t runner; |
---|
| 79 | |
---|
[de6319f] | 80 | // Name of the processor |
---|
| 81 | const char * name; |
---|
| 82 | |
---|
[025278e] | 83 | // Handle to pthreads |
---|
| 84 | pthread_t kernel_thread; |
---|
[2ac095d] | 85 | |
---|
[78da4ab] | 86 | struct { |
---|
[dddb3dd0] | 87 | $io_context * ctx; |
---|
| 88 | bool pending; |
---|
| 89 | bool dirty; |
---|
[78da4ab] | 90 | } io; |
---|
| 91 | |
---|
[e60e0dc] | 92 | // Preemption data |
---|
[025278e] | 93 | // Node which is added in the discrete event simulaiton |
---|
| 94 | struct alarm_node_t * preemption_alarm; |
---|
| 95 | |
---|
| 96 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 97 | bool pending_preemption; |
---|
[c81ebf9] | 98 | |
---|
[92e7631] | 99 | // Idle lock (kernel semaphore) |
---|
[dddb3dd0] | 100 | int idle; |
---|
[85b1deb] | 101 | |
---|
[92e7631] | 102 | // Termination synchronisation (user semaphore) |
---|
[454f478] | 103 | oneshot terminated; |
---|
[de94a60] | 104 | |
---|
[27f5f71] | 105 | // pthread Stack |
---|
| 106 | void * stack; |
---|
| 107 | |
---|
[de94a60] | 108 | // Link lists fields |
---|
[1eb239e4] | 109 | DLISTED_MGD_IMPL_IN(processor) |
---|
[14a61b5] | 110 | |
---|
[a1538cd] | 111 | // special init fields |
---|
| 112 | // This is needed for memcached integration |
---|
| 113 | // once memcached experiments are done this should probably be removed |
---|
| 114 | // it is not a particularly safe scheme as it can make processors less homogeneous |
---|
| 115 | struct { |
---|
[a5e7233] | 116 | $thread * thrd; |
---|
[a1538cd] | 117 | } init; |
---|
| 118 | |
---|
[c34ebf2] | 119 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[69fbc61] | 120 | int print_stats; |
---|
[c34ebf2] | 121 | bool print_halts; |
---|
| 122 | #endif |
---|
| 123 | |
---|
[e60e0dc] | 124 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 125 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 126 | const char * last_enable; |
---|
[e60e0dc] | 127 | #endif |
---|
[c84e80a] | 128 | }; |
---|
| 129 | |
---|
[a5e7233] | 130 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
[242a902] | 131 | void ^?{}(processor & this); |
---|
[c84e80a] | 132 | |
---|
[a5e7233] | 133 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
| 134 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
| 135 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster}; } |
---|
[de6319f] | 136 | |
---|
[1eb239e4] | 137 | DLISTED_MGD_IMPL_OUT(processor) |
---|
[de94a60] | 138 | |
---|
[7768b8d] | 139 | //----------------------------------------------------------------------------- |
---|
| 140 | // Cluster Tools |
---|
[dca5802] | 141 | |
---|
| 142 | // Intrusives lanes which are used by the relaxed ready queue |
---|
[61d7bec] | 143 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
[dca5802] | 144 | void ?{}(__intrusive_lane_t & this); |
---|
| 145 | void ^?{}(__intrusive_lane_t & this); |
---|
[7768b8d] | 146 | |
---|
[61d7bec] | 147 | // Counter used for wether or not the lanes are all empty |
---|
| 148 | struct __attribute__((aligned(128))) __snzi_node_t; |
---|
| 149 | struct __snzi_t { |
---|
| 150 | unsigned mask; |
---|
| 151 | int root; |
---|
| 152 | __snzi_node_t * nodes; |
---|
| 153 | }; |
---|
[b798713] | 154 | |
---|
[61d7bec] | 155 | void ?{}( __snzi_t & this, unsigned depth ); |
---|
| 156 | void ^?{}( __snzi_t & this ); |
---|
[b798713] | 157 | |
---|
| 158 | //TODO adjust cache size to ARCHITECTURE |
---|
[dca5802] | 159 | // Structure holding the relaxed ready queue |
---|
[37ba662] | 160 | struct __ready_queue_t { |
---|
[dca5802] | 161 | // Data tracking how many/which lanes are used |
---|
| 162 | // Aligned to 128 for cache locality |
---|
[61d7bec] | 163 | __snzi_t snzi; |
---|
[dca5802] | 164 | |
---|
| 165 | // Data tracking the actual lanes |
---|
| 166 | // On a seperate cacheline from the used struct since |
---|
| 167 | // used can change on each push/pop but this data |
---|
| 168 | // only changes on shrink/grow |
---|
[37ba662] | 169 | struct { |
---|
[dca5802] | 170 | // Arary of lanes |
---|
| 171 | __intrusive_lane_t * volatile data; |
---|
| 172 | |
---|
| 173 | // Number of lanes (empty or not) |
---|
[b798713] | 174 | volatile size_t count; |
---|
[dca5802] | 175 | } lanes; |
---|
[b798713] | 176 | }; |
---|
| 177 | |
---|
| 178 | void ?{}(__ready_queue_t & this); |
---|
| 179 | void ^?{}(__ready_queue_t & this); |
---|
| 180 | |
---|
[1eb239e4] | 181 | // Idle Sleep |
---|
| 182 | struct __cluster_idles { |
---|
| 183 | // Spin lock protecting the queue |
---|
| 184 | volatile uint64_t lock; |
---|
| 185 | |
---|
| 186 | // Total number of processors |
---|
| 187 | unsigned total; |
---|
| 188 | |
---|
| 189 | // Total number of idle processors |
---|
| 190 | unsigned idle; |
---|
| 191 | |
---|
| 192 | // List of idle processors |
---|
| 193 | dlist(processor, processor) list; |
---|
| 194 | }; |
---|
| 195 | |
---|
[de94a60] | 196 | //----------------------------------------------------------------------------- |
---|
| 197 | // Cluster |
---|
[37ba662] | 198 | struct __attribute__((aligned(128))) cluster { |
---|
[de94a60] | 199 | // Ready queue for threads |
---|
[b798713] | 200 | __ready_queue_t ready_queue; |
---|
[de94a60] | 201 | |
---|
| 202 | // Name of the cluster |
---|
| 203 | const char * name; |
---|
| 204 | |
---|
| 205 | // Preemption rate on this cluster |
---|
| 206 | Duration preemption_rate; |
---|
| 207 | |
---|
[64a7146] | 208 | // List of idle processors |
---|
[1eb239e4] | 209 | __cluster_idles idles; |
---|
[de94a60] | 210 | |
---|
[d4e68a6] | 211 | // List of threads |
---|
[a1a17a74] | 212 | __spinlock_t thread_list_lock; |
---|
[ac2b598] | 213 | __dllist_t(struct $thread) threads; |
---|
[d4e68a6] | 214 | unsigned int nthreads; |
---|
[a1a17a74] | 215 | |
---|
[de94a60] | 216 | // Link lists fields |
---|
[ea8b2f7] | 217 | struct __dbg_node_cltr { |
---|
[de94a60] | 218 | cluster * next; |
---|
| 219 | cluster * prev; |
---|
| 220 | } node; |
---|
[92976d9] | 221 | |
---|
[f00b26d4] | 222 | struct { |
---|
[78da4ab] | 223 | $io_arbiter * arbiter; |
---|
| 224 | io_context_params params; |
---|
[f00b26d4] | 225 | } io; |
---|
[038be32] | 226 | |
---|
| 227 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[8834751] | 228 | struct __stats_t * stats; |
---|
[69fbc61] | 229 | int print_stats; |
---|
[038be32] | 230 | #endif |
---|
[de94a60] | 231 | }; |
---|
| 232 | extern Duration default_preemption(); |
---|
| 233 | |
---|
[f00b26d4] | 234 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); |
---|
[de94a60] | 235 | void ^?{}(cluster & this); |
---|
| 236 | |
---|
[f00b26d4] | 237 | static inline void ?{} (cluster & this) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), 1, default_params}; } |
---|
| 238 | static inline void ?{} (cluster & this, Duration preemption_rate) { io_context_params default_params; this{"Anonymous Cluster", preemption_rate, 1, default_params}; } |
---|
| 239 | static inline void ?{} (cluster & this, const char name[]) { io_context_params default_params; this{name, default_preemption(), 1, default_params}; } |
---|
| 240 | static inline void ?{} (cluster & this, unsigned num_io) { io_context_params default_params; this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } |
---|
| 241 | 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}; } |
---|
| 242 | static inline void ?{} (cluster & this, const char name[], unsigned num_io) { io_context_params default_params; this{name, default_preemption(), num_io, default_params}; } |
---|
| 243 | static inline void ?{} (cluster & this, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } |
---|
| 244 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params) { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } |
---|
| 245 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params) { this{name, default_preemption(), 1, io_params}; } |
---|
| 246 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params) { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } |
---|
| 247 | 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}; } |
---|
| 248 | 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] | 249 | |
---|
[c7a900a] | 250 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 251 | |
---|
[8fc652e0] | 252 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE |
---|
| 253 | static inline struct cluster * active_cluster () { return publicTLS_get( this_processor )->cltr; } |
---|
[d4e68a6] | 254 | |
---|
[038be32] | 255 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[8fc652e0] | 256 | void print_stats_now( cluster & this, int flags ); |
---|
| 257 | |
---|
[69fbc61] | 258 | static inline void print_stats_at_exit( cluster & this, int flags ) { |
---|
| 259 | this.print_stats |= flags; |
---|
[038be32] | 260 | } |
---|
[c34ebf2] | 261 | |
---|
[69fbc61] | 262 | static inline void print_stats_at_exit( processor & this, int flags ) { |
---|
| 263 | this.print_stats |= flags; |
---|
[c34ebf2] | 264 | } |
---|
| 265 | |
---|
| 266 | void print_halts( processor & this ); |
---|
[038be32] | 267 | #endif |
---|
| 268 | |
---|
[8118303] | 269 | // Local Variables: // |
---|
[6b0b624] | 270 | // mode: c // |
---|
| 271 | // tab-width: 4 // |
---|
[8118303] | 272 | // End: // |
---|