[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 | // |
---|
[75a17f1] | 7 | // kernel -- |
---|
[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 | |
---|
[c84e80a] | 18 | #include <stdbool.h> |
---|
[92976d9] | 19 | #include <stdint.h> |
---|
[8118303] | 20 | |
---|
[bd98b58] | 21 | #include "invoke.h" |
---|
[73abe95] | 22 | #include "time_t.hfa" |
---|
[d76bd79] | 23 | #include "coroutine.hfa" |
---|
[bd98b58] | 24 | |
---|
[8def349] | 25 | extern "C" { |
---|
| 26 | #include <pthread.h> |
---|
[6b4cdd3] | 27 | #include <semaphore.h> |
---|
[8def349] | 28 | } |
---|
| 29 | |
---|
[db6f06a] | 30 | //----------------------------------------------------------------------------- |
---|
| 31 | // Locks |
---|
[bdeba0b] | 32 | struct semaphore { |
---|
[ea7d2b0] | 33 | __spinlock_t lock; |
---|
[bdeba0b] | 34 | int count; |
---|
[ac2b598] | 35 | __queue_t($thread) waiting; |
---|
[9c31349] | 36 | }; |
---|
| 37 | |
---|
[242a902] | 38 | void ?{}(semaphore & this, int count = 1); |
---|
| 39 | void ^?{}(semaphore & this); |
---|
[71c8b7e] | 40 | bool P (semaphore & this); |
---|
[f0ce5f4] | 41 | bool V (semaphore & this); |
---|
[d384787] | 42 | bool V (semaphore & this, unsigned count); |
---|
[9c31349] | 43 | |
---|
[db6f06a] | 44 | |
---|
[bd98b58] | 45 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 46 | // Processor |
---|
[de6319f] | 47 | extern struct cluster * mainCluster; |
---|
[bd98b58] | 48 | |
---|
[9b1dcc2] | 49 | // Processor id, required for scheduling threads |
---|
| 50 | struct __processor_id_t { |
---|
| 51 | unsigned id; |
---|
| 52 | }; |
---|
| 53 | |
---|
[094476d] | 54 | coroutine processorCtx_t { |
---|
| 55 | struct processor * proc; |
---|
| 56 | }; |
---|
| 57 | |
---|
[e60e0dc] | 58 | // Wrapper around kernel threads |
---|
[c84e80a] | 59 | struct processor { |
---|
[9b1dcc2] | 60 | inline __processor_id_t; |
---|
| 61 | |
---|
[e60e0dc] | 62 | // Main state |
---|
[025278e] | 63 | // Coroutine ctx who does keeps the state of the processor |
---|
[094476d] | 64 | struct processorCtx_t runner; |
---|
[025278e] | 65 | |
---|
| 66 | // Cluster from which to get threads |
---|
[de94a60] | 67 | struct cluster * cltr; |
---|
[025278e] | 68 | |
---|
[de6319f] | 69 | // Name of the processor |
---|
| 70 | const char * name; |
---|
| 71 | |
---|
[025278e] | 72 | // Handle to pthreads |
---|
| 73 | pthread_t kernel_thread; |
---|
[2ac095d] | 74 | |
---|
[e60e0dc] | 75 | // RunThread data |
---|
[025278e] | 76 | // Action to do after a thread is ran |
---|
[ac2b598] | 77 | $thread * destroyer; |
---|
[c81ebf9] | 78 | |
---|
[e60e0dc] | 79 | // Preemption data |
---|
[025278e] | 80 | // Node which is added in the discrete event simulaiton |
---|
| 81 | struct alarm_node_t * preemption_alarm; |
---|
| 82 | |
---|
| 83 | // If true, a preemption was triggered in an unsafe region, the processor must preempt as soon as possible |
---|
| 84 | bool pending_preemption; |
---|
[c81ebf9] | 85 | |
---|
[92e7631] | 86 | // Idle lock (kernel semaphore) |
---|
| 87 | __bin_sem_t idle; |
---|
[85b1deb] | 88 | |
---|
| 89 | // Termination |
---|
| 90 | // Set to true to notify the processor should terminate |
---|
| 91 | volatile bool do_terminate; |
---|
| 92 | |
---|
[92e7631] | 93 | // Termination synchronisation (user semaphore) |
---|
[85b1deb] | 94 | semaphore terminated; |
---|
[de94a60] | 95 | |
---|
[27f5f71] | 96 | // pthread Stack |
---|
| 97 | void * stack; |
---|
| 98 | |
---|
[de94a60] | 99 | // Link lists fields |
---|
[504a7dc] | 100 | struct __dbg_node_cltr { |
---|
| 101 | processor * next; |
---|
| 102 | processor * prev; |
---|
[de94a60] | 103 | } node; |
---|
[14a61b5] | 104 | |
---|
[e60e0dc] | 105 | #ifdef __CFA_DEBUG__ |
---|
[025278e] | 106 | // Last function to enable preemption on this processor |
---|
[cdbfab0] | 107 | const char * last_enable; |
---|
[e60e0dc] | 108 | #endif |
---|
[c84e80a] | 109 | }; |
---|
| 110 | |
---|
[e3fea42] | 111 | void ?{}(processor & this, const char name[], struct cluster & cltr); |
---|
[242a902] | 112 | void ^?{}(processor & this); |
---|
[c84e80a] | 113 | |
---|
[de6319f] | 114 | static inline void ?{}(processor & this) { this{ "Anonymous Processor", *mainCluster}; } |
---|
[de94a60] | 115 | static inline void ?{}(processor & this, struct cluster & cltr) { this{ "Anonymous Processor", cltr}; } |
---|
[e3fea42] | 116 | static inline void ?{}(processor & this, const char name[]) { this{name, *mainCluster }; } |
---|
[de6319f] | 117 | |
---|
[c7a900a] | 118 | static inline [processor *&, processor *& ] __get( processor & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 119 | |
---|
[92976d9] | 120 | //----------------------------------------------------------------------------- |
---|
| 121 | // I/O |
---|
[61dd73d] | 122 | struct __io_data; |
---|
[92976d9] | 123 | |
---|
[5dadc9b] | 124 | #define CFA_CLUSTER_IO_POLLER_USER_THREAD 1 << 0 // 0x1 |
---|
| 125 | #define CFA_CLUSTER_IO_POLLER_THREAD_SUBMITS 1 << 1 // 0x2 |
---|
| 126 | // #define CFA_CLUSTER_IO_POLLER_KERNEL_SIDE 1 << 2 // 0x4 |
---|
[dd4e2d7] | 127 | #define CFA_CLUSTER_IO_BUFFLEN_OFFSET 16 |
---|
[de94a60] | 128 | |
---|
[7768b8d] | 129 | |
---|
| 130 | //----------------------------------------------------------------------------- |
---|
| 131 | // Cluster Tools |
---|
[dca5802] | 132 | |
---|
| 133 | // Intrusives lanes which are used by the relaxed ready queue |
---|
[61d7bec] | 134 | struct __attribute__((aligned(128))) __intrusive_lane_t; |
---|
[dca5802] | 135 | void ?{}(__intrusive_lane_t & this); |
---|
| 136 | void ^?{}(__intrusive_lane_t & this); |
---|
[7768b8d] | 137 | |
---|
[61d7bec] | 138 | // Counter used for wether or not the lanes are all empty |
---|
| 139 | struct __attribute__((aligned(128))) __snzi_node_t; |
---|
| 140 | struct __snzi_t { |
---|
| 141 | unsigned mask; |
---|
| 142 | int root; |
---|
| 143 | __snzi_node_t * nodes; |
---|
| 144 | }; |
---|
[b798713] | 145 | |
---|
[61d7bec] | 146 | void ?{}( __snzi_t & this, unsigned depth ); |
---|
| 147 | void ^?{}( __snzi_t & this ); |
---|
[b798713] | 148 | |
---|
| 149 | //TODO adjust cache size to ARCHITECTURE |
---|
[dca5802] | 150 | // Structure holding the relaxed ready queue |
---|
[b798713] | 151 | struct __attribute__((aligned(128))) __ready_queue_t { |
---|
[dca5802] | 152 | // Data tracking how many/which lanes are used |
---|
| 153 | // Aligned to 128 for cache locality |
---|
[61d7bec] | 154 | __snzi_t snzi; |
---|
[dca5802] | 155 | |
---|
| 156 | // Data tracking the actual lanes |
---|
| 157 | // On a seperate cacheline from the used struct since |
---|
| 158 | // used can change on each push/pop but this data |
---|
| 159 | // only changes on shrink/grow |
---|
[b798713] | 160 | struct __attribute__((aligned(64))) { |
---|
[dca5802] | 161 | // Arary of lanes |
---|
| 162 | __intrusive_lane_t * volatile data; |
---|
| 163 | |
---|
| 164 | // Number of lanes (empty or not) |
---|
[b798713] | 165 | volatile size_t count; |
---|
[dca5802] | 166 | } lanes; |
---|
[b798713] | 167 | |
---|
[dca5802] | 168 | // Statistics |
---|
[b798713] | 169 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[61d7bec] | 170 | struct __attribute__((aligned(64))) { |
---|
[b798713] | 171 | struct { |
---|
[dca5802] | 172 | // Push statistic |
---|
[b798713] | 173 | struct { |
---|
[dca5802] | 174 | // number of attemps at pushing something |
---|
[b798713] | 175 | volatile size_t attempt; |
---|
[dca5802] | 176 | |
---|
| 177 | // number of successes at pushing |
---|
[b798713] | 178 | volatile size_t success; |
---|
| 179 | } push; |
---|
[dca5802] | 180 | |
---|
| 181 | // Pop statistic |
---|
[b798713] | 182 | struct { |
---|
[dca5802] | 183 | // number of reads of the mask |
---|
| 184 | // picking an empty __cfa_readyQ_mask_t counts here |
---|
| 185 | // but not as an attempt |
---|
[b798713] | 186 | volatile size_t maskrds; |
---|
[dca5802] | 187 | |
---|
| 188 | // number of attemps at poping something |
---|
[b798713] | 189 | volatile size_t attempt; |
---|
[dca5802] | 190 | |
---|
| 191 | // number of successes at poping |
---|
[b798713] | 192 | volatile size_t success; |
---|
| 193 | } pop; |
---|
| 194 | } pick; |
---|
[dca5802] | 195 | |
---|
| 196 | // stats on the "used" struct of the queue |
---|
| 197 | // tracks average number of queues that are not empty |
---|
| 198 | // when pushing / poping |
---|
[b798713] | 199 | struct { |
---|
| 200 | volatile size_t value; |
---|
| 201 | volatile size_t count; |
---|
[dca5802] | 202 | } used; |
---|
[b798713] | 203 | } global_stats; |
---|
| 204 | |
---|
| 205 | #endif |
---|
| 206 | }; |
---|
| 207 | |
---|
| 208 | void ?{}(__ready_queue_t & this); |
---|
| 209 | void ^?{}(__ready_queue_t & this); |
---|
| 210 | |
---|
[de94a60] | 211 | //----------------------------------------------------------------------------- |
---|
| 212 | // Cluster |
---|
| 213 | struct cluster { |
---|
| 214 | // Ready queue for threads |
---|
[b798713] | 215 | __ready_queue_t ready_queue; |
---|
[de94a60] | 216 | |
---|
| 217 | // Name of the cluster |
---|
| 218 | const char * name; |
---|
| 219 | |
---|
| 220 | // Preemption rate on this cluster |
---|
| 221 | Duration preemption_rate; |
---|
| 222 | |
---|
| 223 | // List of processors |
---|
[504a7dc] | 224 | __spinlock_t idle_lock; |
---|
| 225 | __dllist_t(struct processor) procs; |
---|
[de94a60] | 226 | __dllist_t(struct processor) idles; |
---|
[504a7dc] | 227 | unsigned int nprocessors; |
---|
[de94a60] | 228 | |
---|
[d4e68a6] | 229 | // List of threads |
---|
[a1a17a74] | 230 | __spinlock_t thread_list_lock; |
---|
[ac2b598] | 231 | __dllist_t(struct $thread) threads; |
---|
[d4e68a6] | 232 | unsigned int nthreads; |
---|
[a1a17a74] | 233 | |
---|
[de94a60] | 234 | // Link lists fields |
---|
[ea8b2f7] | 235 | struct __dbg_node_cltr { |
---|
[de94a60] | 236 | cluster * next; |
---|
| 237 | cluster * prev; |
---|
| 238 | } node; |
---|
[92976d9] | 239 | |
---|
[61dd73d] | 240 | struct __io_data * io; |
---|
[038be32] | 241 | |
---|
| 242 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 243 | bool print_stats; |
---|
| 244 | #endif |
---|
[de94a60] | 245 | }; |
---|
| 246 | extern Duration default_preemption(); |
---|
| 247 | |
---|
[dd4e2d7] | 248 | void ?{} (cluster & this, const char name[], Duration preemption_rate, unsigned flags); |
---|
[de94a60] | 249 | void ^?{}(cluster & this); |
---|
| 250 | |
---|
[dd4e2d7] | 251 | static inline void ?{} (cluster & this) { this{"Anonymous Cluster", default_preemption(), 0}; } |
---|
| 252 | static inline void ?{} (cluster & this, Duration preemption_rate) { this{"Anonymous Cluster", preemption_rate, 0}; } |
---|
| 253 | static inline void ?{} (cluster & this, const char name[]) { this{name, default_preemption(), 0}; } |
---|
| 254 | static inline void ?{} (cluster & this, unsigned flags) { this{"Anonymous Cluster", default_preemption(), flags}; } |
---|
| 255 | static inline void ?{} (cluster & this, Duration preemption_rate, unsigned flags) { this{"Anonymous Cluster", preemption_rate, flags}; } |
---|
| 256 | static inline void ?{} (cluster & this, const char name[], unsigned flags) { this{name, default_preemption(), flags}; } |
---|
[de94a60] | 257 | |
---|
[c7a900a] | 258 | static inline [cluster *&, cluster *& ] __get( cluster & this ) __attribute__((const)) { return this.node.[next, prev]; } |
---|
[de94a60] | 259 | |
---|
[d4e68a6] | 260 | static inline struct processor * active_processor() { return TL_GET( this_processor ); } // UNSAFE |
---|
| 261 | static inline struct cluster * active_cluster () { return TL_GET( this_processor )->cltr; } |
---|
| 262 | |
---|
[038be32] | 263 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 264 | static inline void print_stats_at_exit( cluster & this ) { |
---|
| 265 | this.print_stats = true; |
---|
| 266 | } |
---|
| 267 | #endif |
---|
| 268 | |
---|
[8118303] | 269 | // Local Variables: // |
---|
[6b0b624] | 270 | // mode: c // |
---|
| 271 | // tab-width: 4 // |
---|
[8118303] | 272 | // End: // |
---|