| 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 | // | 
|---|
| 7 | // kernel -- Header containing the core of the kernel API | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Thierry Delisle | 
|---|
| 10 | // Created On       : Tue Jan 17 12:27:26 2017 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Tue Feb  4 12:29:26 2020 | 
|---|
| 13 | // Update Count     : 22 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include "invoke.h" | 
|---|
| 19 | #include "time_t.hfa" | 
|---|
| 20 | #include "coroutine.hfa" | 
|---|
| 21 |  | 
|---|
| 22 | #include "containers/list.hfa" | 
|---|
| 23 |  | 
|---|
| 24 | extern "C" { | 
|---|
| 25 | #include <bits/pthreadtypes.h> | 
|---|
| 26 | #include <pthread.h> | 
|---|
| 27 | #include <linux/types.h> | 
|---|
| 28 | } | 
|---|
| 29 |  | 
|---|
| 30 | #ifdef __CFA_WITH_VERIFY__ | 
|---|
| 31 | extern bool __cfaabi_dbg_in_kernel(); | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 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 |  | 
|---|
| 46 | //----------------------------------------------------------------------------- | 
|---|
| 47 | // Processor | 
|---|
| 48 | extern struct cluster * mainCluster; | 
|---|
| 49 |  | 
|---|
| 50 | // Processor id, required for scheduling threads | 
|---|
| 51 | struct __processor_id_t { | 
|---|
| 52 | unsigned id:24; | 
|---|
| 53 |  | 
|---|
| 54 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| 55 | struct __stats_t * stats; | 
|---|
| 56 | #endif | 
|---|
| 57 | }; | 
|---|
| 58 |  | 
|---|
| 59 | coroutine processorCtx_t { | 
|---|
| 60 | struct processor * proc; | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 | // Wrapper around kernel threads | 
|---|
| 64 | struct __attribute__((aligned(128))) processor { | 
|---|
| 65 | // Main state | 
|---|
| 66 | inline __processor_id_t; | 
|---|
| 67 |  | 
|---|
| 68 | // Cluster from which to get threads | 
|---|
| 69 | struct cluster * cltr; | 
|---|
| 70 |  | 
|---|
| 71 | // Id within the cluster | 
|---|
| 72 | unsigned cltr_id; | 
|---|
| 73 |  | 
|---|
| 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 |  | 
|---|
| 80 | // Name of the processor | 
|---|
| 81 | const char * name; | 
|---|
| 82 |  | 
|---|
| 83 | // Handle to pthreads | 
|---|
| 84 | pthread_t kernel_thread; | 
|---|
| 85 |  | 
|---|
| 86 | struct { | 
|---|
| 87 | $io_context * ctx; | 
|---|
| 88 | bool pending; | 
|---|
| 89 | bool dirty; | 
|---|
| 90 | } io; | 
|---|
| 91 |  | 
|---|
| 92 | // Preemption data | 
|---|
| 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; | 
|---|
| 98 |  | 
|---|
| 99 | // Idle lock (kernel semaphore) | 
|---|
| 100 | int idle; | 
|---|
| 101 |  | 
|---|
| 102 | // Termination synchronisation (user semaphore) | 
|---|
| 103 | oneshot terminated; | 
|---|
| 104 |  | 
|---|
| 105 | // pthread Stack | 
|---|
| 106 | void * stack; | 
|---|
| 107 |  | 
|---|
| 108 | // Link lists fields | 
|---|
| 109 | DLISTED_MGD_IMPL_IN(processor) | 
|---|
| 110 |  | 
|---|
| 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 { | 
|---|
| 116 | $thread * thrd; | 
|---|
| 117 | } init; | 
|---|
| 118 |  | 
|---|
| 119 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| 120 | int print_stats; | 
|---|
| 121 | bool print_halts; | 
|---|
| 122 | #endif | 
|---|
| 123 |  | 
|---|
| 124 | #ifdef __CFA_DEBUG__ | 
|---|
| 125 | // Last function to enable preemption on this processor | 
|---|
| 126 | const char * last_enable; | 
|---|
| 127 | #endif | 
|---|
| 128 | }; | 
|---|
| 129 |  | 
|---|
| 130 | void  ?{}(processor & this, const char name[], struct cluster & cltr); | 
|---|
| 131 | void ^?{}(processor & this); | 
|---|
| 132 |  | 
|---|
| 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}; } | 
|---|
| 136 |  | 
|---|
| 137 | DLISTED_MGD_IMPL_OUT(processor) | 
|---|
| 138 |  | 
|---|
| 139 | //----------------------------------------------------------------------------- | 
|---|
| 140 | // Cluster Tools | 
|---|
| 141 |  | 
|---|
| 142 | // Intrusives lanes which are used by the relaxed ready queue | 
|---|
| 143 | struct __attribute__((aligned(128))) __intrusive_lane_t; | 
|---|
| 144 | void  ?{}(__intrusive_lane_t & this); | 
|---|
| 145 | void ^?{}(__intrusive_lane_t & this); | 
|---|
| 146 |  | 
|---|
| 147 | //TODO adjust cache size to ARCHITECTURE | 
|---|
| 148 | // Structure holding the relaxed ready queue | 
|---|
| 149 | struct __ready_queue_t { | 
|---|
| 150 | // Data tracking the actual lanes | 
|---|
| 151 | // On a seperate cacheline from the used struct since | 
|---|
| 152 | // used can change on each push/pop but this data | 
|---|
| 153 | // only changes on shrink/grow | 
|---|
| 154 | struct { | 
|---|
| 155 | // Arary of lanes | 
|---|
| 156 | __intrusive_lane_t * volatile data; | 
|---|
| 157 |  | 
|---|
| 158 | // Number of lanes (empty or not) | 
|---|
| 159 | volatile size_t count; | 
|---|
| 160 | } lanes; | 
|---|
| 161 | }; | 
|---|
| 162 |  | 
|---|
| 163 | void  ?{}(__ready_queue_t & this); | 
|---|
| 164 | void ^?{}(__ready_queue_t & this); | 
|---|
| 165 |  | 
|---|
| 166 | // Idle Sleep | 
|---|
| 167 | struct __cluster_proc_list { | 
|---|
| 168 | // Spin lock protecting the queue | 
|---|
| 169 | volatile uint64_t lock; | 
|---|
| 170 |  | 
|---|
| 171 | // Total number of processors | 
|---|
| 172 | unsigned total; | 
|---|
| 173 |  | 
|---|
| 174 | // Total number of idle processors | 
|---|
| 175 | unsigned idle; | 
|---|
| 176 |  | 
|---|
| 177 | // List of idle processors | 
|---|
| 178 | dlist(processor, processor) idles; | 
|---|
| 179 |  | 
|---|
| 180 | // List of active processors | 
|---|
| 181 | dlist(processor, processor) actives; | 
|---|
| 182 | }; | 
|---|
| 183 |  | 
|---|
| 184 | //----------------------------------------------------------------------------- | 
|---|
| 185 | // Cluster | 
|---|
| 186 | struct __attribute__((aligned(128))) cluster { | 
|---|
| 187 | // Ready queue for threads | 
|---|
| 188 | __ready_queue_t ready_queue; | 
|---|
| 189 |  | 
|---|
| 190 | // Name of the cluster | 
|---|
| 191 | const char * name; | 
|---|
| 192 |  | 
|---|
| 193 | // Preemption rate on this cluster | 
|---|
| 194 | Duration preemption_rate; | 
|---|
| 195 |  | 
|---|
| 196 | // List of idle processors | 
|---|
| 197 | __cluster_proc_list procs; | 
|---|
| 198 |  | 
|---|
| 199 | // List of threads | 
|---|
| 200 | __spinlock_t thread_list_lock; | 
|---|
| 201 | __dllist_t(struct $thread) threads; | 
|---|
| 202 | unsigned int nthreads; | 
|---|
| 203 |  | 
|---|
| 204 | // Link lists fields | 
|---|
| 205 | struct __dbg_node_cltr { | 
|---|
| 206 | cluster * next; | 
|---|
| 207 | cluster * prev; | 
|---|
| 208 | } node; | 
|---|
| 209 |  | 
|---|
| 210 | struct { | 
|---|
| 211 | $io_arbiter * arbiter; | 
|---|
| 212 | io_context_params params; | 
|---|
| 213 | } io; | 
|---|
| 214 |  | 
|---|
| 215 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| 216 | struct __stats_t * stats; | 
|---|
| 217 | int print_stats; | 
|---|
| 218 | #endif | 
|---|
| 219 | }; | 
|---|
| 220 | extern Duration default_preemption(); | 
|---|
| 221 |  | 
|---|
| 222 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned num_io, const io_context_params & io_params); | 
|---|
| 223 | void ^?{}(cluster & this); | 
|---|
| 224 |  | 
|---|
| 225 | static inline void ?{} (cluster & this)                                            { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), 1, default_params}; } | 
|---|
| 226 | static inline void ?{} (cluster & this, Duration preemption_rate)                  { io_context_params default_params;    this{"Anonymous Cluster", preemption_rate, 1, default_params}; } | 
|---|
| 227 | static inline void ?{} (cluster & this, const char name[])                         { io_context_params default_params;    this{name, default_preemption(), 1, default_params}; } | 
|---|
| 228 | static inline void ?{} (cluster & this, unsigned num_io)                           { io_context_params default_params;    this{"Anonymous Cluster", default_preemption(), num_io, default_params}; } | 
|---|
| 229 | 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}; } | 
|---|
| 230 | static inline void ?{} (cluster & this, const char name[], unsigned num_io)        { io_context_params default_params;    this{name, default_preemption(), num_io, default_params}; } | 
|---|
| 231 | static inline void ?{} (cluster & this, const io_context_params & io_params)                                            { this{"Anonymous Cluster", default_preemption(), 1, io_params}; } | 
|---|
| 232 | static inline void ?{} (cluster & this, Duration preemption_rate, const io_context_params & io_params)                  { this{"Anonymous Cluster", preemption_rate, 1, io_params}; } | 
|---|
| 233 | static inline void ?{} (cluster & this, const char name[], const io_context_params & io_params)                         { this{name, default_preemption(), 1, io_params}; } | 
|---|
| 234 | static inline void ?{} (cluster & this, unsigned num_io, const io_context_params & io_params)                           { this{"Anonymous Cluster", default_preemption(), num_io, io_params}; } | 
|---|
| 235 | 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}; } | 
|---|
| 236 | 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}; } | 
|---|
| 237 |  | 
|---|
| 238 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } | 
|---|
| 239 |  | 
|---|
| 240 | static inline struct processor * active_processor() { return publicTLS_get( this_processor ); } // UNSAFE | 
|---|
| 241 | static inline struct cluster   * active_cluster  () { return publicTLS_get( this_processor )->cltr; } | 
|---|
| 242 |  | 
|---|
| 243 | #if !defined(__CFA_NO_STATISTICS__) | 
|---|
| 244 | void print_stats_now( cluster & this, int flags ); | 
|---|
| 245 |  | 
|---|
| 246 | static inline void print_stats_at_exit( cluster & this, int flags ) { | 
|---|
| 247 | this.print_stats |= flags; | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | static inline void print_stats_at_exit( processor & this, int flags ) { | 
|---|
| 251 | this.print_stats |= flags; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | void print_halts( processor & this ); | 
|---|
| 255 | #endif | 
|---|
| 256 |  | 
|---|
| 257 | // Local Variables: // | 
|---|
| 258 | // mode: c // | 
|---|
| 259 | // tab-width: 4 // | 
|---|
| 260 | // End: // | 
|---|