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