| [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 | 
|---|
| [8834751] | 51 |  | 
|---|
| [9b1dcc2] | 52 |  | 
|---|
| [094476d] | 53 | coroutine processorCtx_t { | 
|---|
|  | 54 | struct processor * proc; | 
|---|
|  | 55 | }; | 
|---|
|  | 56 |  | 
|---|
| [e60e0dc] | 57 | // Wrapper around kernel threads | 
|---|
| [37ba662] | 58 | struct __attribute__((aligned(128))) processor { | 
|---|
| [025278e] | 59 | // Cluster from which to get threads | 
|---|
| [de94a60] | 60 | struct cluster * cltr; | 
|---|
| [025278e] | 61 |  | 
|---|
| [431cd4f] | 62 | // Ready Queue state per processor | 
|---|
|  | 63 | struct { | 
|---|
|  | 64 | unsigned short its; | 
|---|
|  | 65 | unsigned short itr; | 
|---|
|  | 66 | unsigned id; | 
|---|
|  | 67 | unsigned target; | 
|---|
| [12daa43] | 68 | unsigned last; | 
|---|
| [a2a4566] | 69 | signed   cpu; | 
|---|
| [431cd4f] | 70 | } rdq; | 
|---|
| [bd0bdd37] | 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 |  | 
|---|
| [c993b15] | 84 | // Unique id for the processor (not per cluster) | 
|---|
|  | 85 | unsigned unique_id; | 
|---|
|  | 86 |  | 
|---|
| [78da4ab] | 87 | struct { | 
|---|
| [dddb3dd0] | 88 | $io_context * ctx; | 
|---|
|  | 89 | bool pending; | 
|---|
|  | 90 | bool dirty; | 
|---|
| [78da4ab] | 91 | } io; | 
|---|
|  | 92 |  | 
|---|
| [e60e0dc] | 93 | // Preemption data | 
|---|
| [025278e] | 94 | // Node which is added in the discrete event simulaiton | 
|---|
|  | 95 | struct alarm_node_t * preemption_alarm; | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible | 
|---|
|  | 98 | bool pending_preemption; | 
|---|
| [c81ebf9] | 99 |  | 
|---|
| [92e7631] | 100 | // Idle lock (kernel semaphore) | 
|---|
| [1757f98] | 101 | int idle_fd; | 
|---|
| [85b1deb] | 102 |  | 
|---|
| [92e7631] | 103 | // Termination synchronisation (user semaphore) | 
|---|
| [454f478] | 104 | oneshot terminated; | 
|---|
| [de94a60] | 105 |  | 
|---|
| [27f5f71] | 106 | // pthread Stack | 
|---|
|  | 107 | void * stack; | 
|---|
|  | 108 |  | 
|---|
| [de94a60] | 109 | // Link lists fields | 
|---|
| [69914cbc] | 110 | inline dlink(processor); | 
|---|
| [14a61b5] | 111 |  | 
|---|
| [a1538cd] | 112 | // special init fields | 
|---|
|  | 113 | // This is needed for memcached integration | 
|---|
|  | 114 | // once memcached experiments are done this should probably be removed | 
|---|
|  | 115 | // it is not a particularly safe scheme as it can make processors less homogeneous | 
|---|
|  | 116 | struct { | 
|---|
| [e84ab3d] | 117 | thread$ * thrd; | 
|---|
| [a1538cd] | 118 | } init; | 
|---|
|  | 119 |  | 
|---|
| [a67c5b6] | 120 | struct KernelThreadData * local_data; | 
|---|
|  | 121 |  | 
|---|
| [c34ebf2] | 122 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [69fbc61] | 123 | int print_stats; | 
|---|
| [c34ebf2] | 124 | bool print_halts; | 
|---|
|  | 125 | #endif | 
|---|
|  | 126 |  | 
|---|
| [e60e0dc] | 127 | #ifdef __CFA_DEBUG__ | 
|---|
| [025278e] | 128 | // Last function to enable preemption on this processor | 
|---|
| [cdbfab0] | 129 | const char * last_enable; | 
|---|
| [e60e0dc] | 130 | #endif | 
|---|
| [c84e80a] | 131 | }; | 
|---|
| [69914cbc] | 132 | P9_EMBEDDED( processor, dlink(processor) ) | 
|---|
| [c84e80a] | 133 |  | 
|---|
| [a5e7233] | 134 | void  ?{}(processor & this, const char name[], struct cluster & cltr); | 
|---|
| [242a902] | 135 | void ^?{}(processor & this); | 
|---|
| [c84e80a] | 136 |  | 
|---|
| [a5e7233] | 137 | static inline void  ?{}(processor & this)                        { this{ "Anonymous Processor", *mainCluster}; } | 
|---|
|  | 138 | static inline void  ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } | 
|---|
|  | 139 | static inline void  ?{}(processor & this, const char name[])     { this{name, *mainCluster}; } | 
|---|
| [de6319f] | 140 |  | 
|---|
| [7768b8d] | 141 | //----------------------------------------------------------------------------- | 
|---|
|  | 142 | // Cluster Tools | 
|---|
| [dca5802] | 143 |  | 
|---|
| [9cc3a18] | 144 | // Intrusives lanes which are used by the ready queue | 
|---|
| [61d7bec] | 145 | struct __attribute__((aligned(128))) __intrusive_lane_t; | 
|---|
| [dca5802] | 146 | void  ?{}(__intrusive_lane_t & this); | 
|---|
|  | 147 | void ^?{}(__intrusive_lane_t & this); | 
|---|
| [7768b8d] | 148 |  | 
|---|
| [9cc3a18] | 149 | // Aligned timestamps which are used by the relaxed ready queue | 
|---|
| [8cd40bf] | 150 | struct __attribute__((aligned(128))) __timestamp_t { | 
|---|
|  | 151 | volatile unsigned long long tv; | 
|---|
| [089d30c] | 152 | volatile unsigned long long ma; | 
|---|
| [8cd40bf] | 153 | }; | 
|---|
|  | 154 |  | 
|---|
| [0fb3ee5] | 155 | struct __attribute__((aligned(16))) __cache_id_t { | 
|---|
| [a2a4566] | 156 | volatile unsigned id; | 
|---|
|  | 157 | }; | 
|---|
|  | 158 |  | 
|---|
| [089d30c] | 159 | // Aligned timestamps which are used by the relaxed ready queue | 
|---|
|  | 160 | struct __attribute__((aligned(128))) __help_cnts_t { | 
|---|
|  | 161 | volatile unsigned long long src; | 
|---|
|  | 162 | volatile unsigned long long dst; | 
|---|
|  | 163 | volatile unsigned long long tri; | 
|---|
|  | 164 | }; | 
|---|
|  | 165 |  | 
|---|
|  | 166 | static inline void  ?{}(__timestamp_t & this) { this.tv = 0; this.ma = 0; } | 
|---|
| [8cd40bf] | 167 | static inline void ^?{}(__timestamp_t & this) {} | 
|---|
| [9cc3a18] | 168 |  | 
|---|
| [a2a4566] | 169 | struct __attribute__((aligned(128))) __ready_queue_caches_t; | 
|---|
|  | 170 | void  ?{}(__ready_queue_caches_t & this); | 
|---|
|  | 171 | void ^?{}(__ready_queue_caches_t & this); | 
|---|
|  | 172 |  | 
|---|
| [b798713] | 173 | //TODO adjust cache size to ARCHITECTURE | 
|---|
| [a2a4566] | 174 | // Structure holding the ready queue | 
|---|
| [37ba662] | 175 | struct __ready_queue_t { | 
|---|
| [dca5802] | 176 | // Data tracking the actual lanes | 
|---|
|  | 177 | // On a seperate cacheline from the used struct since | 
|---|
|  | 178 | // used can change on each push/pop but this data | 
|---|
|  | 179 | // only changes on shrink/grow | 
|---|
| [37ba662] | 180 | struct { | 
|---|
| [dca5802] | 181 | // Arary of lanes | 
|---|
|  | 182 | __intrusive_lane_t * volatile data; | 
|---|
|  | 183 |  | 
|---|
| [9cc3a18] | 184 | // Array of times | 
|---|
|  | 185 | __timestamp_t * volatile tscs; | 
|---|
|  | 186 |  | 
|---|
| [a2a4566] | 187 | __cache_id_t * volatile caches; | 
|---|
|  | 188 |  | 
|---|
| [089d30c] | 189 | // Array of stats | 
|---|
|  | 190 | __help_cnts_t * volatile help; | 
|---|
|  | 191 |  | 
|---|
| [dca5802] | 192 | // Number of lanes (empty or not) | 
|---|
| [b798713] | 193 | volatile size_t count; | 
|---|
| [dca5802] | 194 | } lanes; | 
|---|
| [b798713] | 195 | }; | 
|---|
|  | 196 |  | 
|---|
|  | 197 | void  ?{}(__ready_queue_t & this); | 
|---|
|  | 198 | void ^?{}(__ready_queue_t & this); | 
|---|
| [8cd5434] | 199 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
|  | 200 | unsigned cnt(const __ready_queue_t & this, unsigned idx); | 
|---|
|  | 201 | #endif | 
|---|
| [b798713] | 202 |  | 
|---|
| [1eb239e4] | 203 | // Idle Sleep | 
|---|
| [6a9b12b] | 204 | struct __cluster_proc_list { | 
|---|
| [1eb239e4] | 205 | // Spin lock protecting the queue | 
|---|
| [34b8cb7] | 206 | __spinlock_t lock; | 
|---|
|  | 207 |  | 
|---|
|  | 208 | // FD to use to wake a processor | 
|---|
|  | 209 | volatile int fd; | 
|---|
| [1eb239e4] | 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 | 
|---|
| [69914cbc] | 218 | dlist(processor) idles; | 
|---|
| [fc59b580] | 219 |  | 
|---|
|  | 220 | // List of active processors | 
|---|
| [69914cbc] | 221 | dlist(processor) actives; | 
|---|
| [1eb239e4] | 222 | }; | 
|---|
|  | 223 |  | 
|---|
| [de94a60] | 224 | //----------------------------------------------------------------------------- | 
|---|
|  | 225 | // Cluster | 
|---|
| [37ba662] | 226 | struct __attribute__((aligned(128))) cluster { | 
|---|
| [de94a60] | 227 | // Ready queue for threads | 
|---|
| [b798713] | 228 | __ready_queue_t ready_queue; | 
|---|
| [de94a60] | 229 |  | 
|---|
|  | 230 | // Name of the cluster | 
|---|
|  | 231 | const char * name; | 
|---|
|  | 232 |  | 
|---|
|  | 233 | // Preemption rate on this cluster | 
|---|
|  | 234 | Duration preemption_rate; | 
|---|
|  | 235 |  | 
|---|
| [64a7146] | 236 | // List of idle processors | 
|---|
| [6a9b12b] | 237 | __cluster_proc_list procs; | 
|---|
| [de94a60] | 238 |  | 
|---|
| [d4e68a6] | 239 | // List of threads | 
|---|
| [a1a17a74] | 240 | __spinlock_t thread_list_lock; | 
|---|
| [e84ab3d] | 241 | __dllist_t(struct thread$) threads; | 
|---|
| [d4e68a6] | 242 | unsigned int nthreads; | 
|---|
| [a1a17a74] | 243 |  | 
|---|
| [de94a60] | 244 | // Link lists fields | 
|---|
| [ea8b2f7] | 245 | struct __dbg_node_cltr { | 
|---|
| [de94a60] | 246 | cluster * next; | 
|---|
|  | 247 | cluster * prev; | 
|---|
|  | 248 | } node; | 
|---|
| [92976d9] | 249 |  | 
|---|
| [f00b26d4] | 250 | struct { | 
|---|
| [78da4ab] | 251 | $io_arbiter * arbiter; | 
|---|
|  | 252 | io_context_params params; | 
|---|
| [f00b26d4] | 253 | } io; | 
|---|
| [038be32] | 254 |  | 
|---|
|  | 255 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [8834751] | 256 | struct __stats_t * stats; | 
|---|
| [69fbc61] | 257 | int print_stats; | 
|---|
| [038be32] | 258 | #endif | 
|---|
| [de94a60] | 259 | }; | 
|---|
|  | 260 | extern Duration default_preemption(); | 
|---|
|  | 261 |  | 
|---|
| [f00b26d4] | 262 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); | 
|---|
| [de94a60] | 263 | void ^?{}(cluster & this); | 
|---|
|  | 264 |  | 
|---|
| [f00b26d4] | 265 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; } | 
|---|
|  | 266 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; } | 
|---|
|  | 267 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; } | 
|---|
|  | 268 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } | 
|---|
|  | 269 | 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}; } | 
|---|
|  | 270 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; } | 
|---|
|  | 271 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } | 
|---|
|  | 272 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } | 
|---|
|  | 273 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; } | 
|---|
|  | 274 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } | 
|---|
|  | 275 | 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}; } | 
|---|
|  | 276 | 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] | 277 |  | 
|---|
| [c7a900a] | 278 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| [de94a60] | 279 |  | 
|---|
| [8fc652e0] | 280 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE | 
|---|
|  | 281 | static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; } | 
|---|
| [d4e68a6] | 282 |  | 
|---|
| [038be32] | 283 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| [8fc652e0] | 284 | void print_stats_now( cluster & this, int flags ); | 
|---|
|  | 285 |  | 
|---|
| [69fbc61] | 286 | static inline void print_stats_at_exit( cluster & this, int flags ) { | 
|---|
|  | 287 | this.print_stats |= flags; | 
|---|
| [038be32] | 288 | } | 
|---|
| [c34ebf2] | 289 |  | 
|---|
| [69fbc61] | 290 | static inline void print_stats_at_exit( processor & this, int flags ) { | 
|---|
|  | 291 | this.print_stats |= flags; | 
|---|
| [c34ebf2] | 292 | } | 
|---|
|  | 293 |  | 
|---|
|  | 294 | void print_halts( processor & this ); | 
|---|
| [038be32] | 295 | #endif | 
|---|
|  | 296 |  | 
|---|
| [8118303] | 297 | // Local Variables: // | 
|---|
| [6b0b624] | 298 | // mode: c // | 
|---|
|  | 299 | // tab-width: 4 // | 
|---|
| [8118303] | 300 | // End: // | 
|---|