[7768b8d] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2019 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 | // ready_queue.cfa --
|
---|
| 8 | //
|
---|
| 9 | // Author : Thierry Delisle
|
---|
| 10 | // Created On : Mon Nov dd 16:29:18 2019
|
---|
| 11 | // Last Modified By :
|
---|
| 12 | // Last Modified On :
|
---|
| 13 | // Update Count :
|
---|
| 14 | //
|
---|
| 15 |
|
---|
| 16 | #define __cforall_thread__
|
---|
[43784ac] | 17 | #define _GNU_SOURCE
|
---|
| 18 |
|
---|
[1b143de] | 19 | // #define __CFA_DEBUG_PRINT_READY_QUEUE__
|
---|
[7768b8d] | 20 |
|
---|
[1eb239e4] | 21 |
|
---|
[9cc3a18] | 22 | #define USE_RELAXED_FIFO
|
---|
| 23 | // #define USE_WORK_STEALING
|
---|
| 24 |
|
---|
[7768b8d] | 25 | #include "bits/defs.hfa"
|
---|
| 26 | #include "kernel_private.hfa"
|
---|
| 27 |
|
---|
| 28 | #include "stdlib.hfa"
|
---|
[61d7bec] | 29 | #include "math.hfa"
|
---|
[7768b8d] | 30 |
|
---|
[04b5cef] | 31 | #include <unistd.h>
|
---|
| 32 |
|
---|
[13c5e19] | 33 | #include "ready_subqueue.hfa"
|
---|
| 34 |
|
---|
[7768b8d] | 35 | static const size_t cache_line_size = 64;
|
---|
| 36 |
|
---|
[d2fadeb] | 37 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 38 | #define __STATS(...) __VA_ARGS__
|
---|
| 39 | #else
|
---|
| 40 | #define __STATS(...)
|
---|
| 41 | #endif
|
---|
| 42 |
|
---|
[dca5802] | 43 | // No overriden function, no environment variable, no define
|
---|
| 44 | // fall back to a magic number
|
---|
| 45 | #ifndef __CFA_MAX_PROCESSORS__
|
---|
[b388ee81] | 46 | #define __CFA_MAX_PROCESSORS__ 1024
|
---|
[dca5802] | 47 | #endif
|
---|
[7768b8d] | 48 |
|
---|
[9cc3a18] | 49 | #if defined(USE_RELAXED_FIFO)
|
---|
| 50 | #define BIAS 4
|
---|
| 51 | #define READYQ_SHARD_FACTOR 4
|
---|
[5f6a172] | 52 | #define SEQUENTIAL_SHARD 1
|
---|
[9cc3a18] | 53 | #elif defined(USE_WORK_STEALING)
|
---|
| 54 | #define READYQ_SHARD_FACTOR 2
|
---|
[5f6a172] | 55 | #define SEQUENTIAL_SHARD 2
|
---|
[9cc3a18] | 56 | #else
|
---|
| 57 | #error no scheduling strategy selected
|
---|
| 58 | #endif
|
---|
| 59 |
|
---|
[d2fadeb] | 60 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats));
|
---|
| 61 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats));
|
---|
[431cd4f] | 62 | static inline struct $thread * search(struct cluster * cltr);
|
---|
[d2fadeb] | 63 | static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred);
|
---|
[9cc3a18] | 64 |
|
---|
[04b5cef] | 65 |
|
---|
[dca5802] | 66 | // returns the maximum number of processors the RWLock support
|
---|
[7768b8d] | 67 | __attribute__((weak)) unsigned __max_processors() {
|
---|
| 68 | const char * max_cores_s = getenv("CFA_MAX_PROCESSORS");
|
---|
| 69 | if(!max_cores_s) {
|
---|
[504a7dc] | 70 | __cfadbg_print_nolock(ready_queue, "No CFA_MAX_PROCESSORS in ENV\n");
|
---|
[dca5802] | 71 | return __CFA_MAX_PROCESSORS__;
|
---|
[7768b8d] | 72 | }
|
---|
| 73 |
|
---|
| 74 | char * endptr = 0p;
|
---|
| 75 | long int max_cores_l = strtol(max_cores_s, &endptr, 10);
|
---|
| 76 | if(max_cores_l < 1 || max_cores_l > 65535) {
|
---|
[504a7dc] | 77 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS out of range : %ld\n", max_cores_l);
|
---|
[dca5802] | 78 | return __CFA_MAX_PROCESSORS__;
|
---|
[7768b8d] | 79 | }
|
---|
| 80 | if('\0' != *endptr) {
|
---|
[504a7dc] | 81 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS not a decimal number : %s\n", max_cores_s);
|
---|
[dca5802] | 82 | return __CFA_MAX_PROCESSORS__;
|
---|
[7768b8d] | 83 | }
|
---|
| 84 |
|
---|
| 85 | return max_cores_l;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | //=======================================================================
|
---|
| 89 | // Cluster wide reader-writer lock
|
---|
| 90 | //=======================================================================
|
---|
[b388ee81] | 91 | void ?{}(__scheduler_RWLock_t & this) {
|
---|
[7768b8d] | 92 | this.max = __max_processors();
|
---|
| 93 | this.alloc = 0;
|
---|
| 94 | this.ready = 0;
|
---|
| 95 | this.data = alloc(this.max);
|
---|
[c993b15] | 96 | this.write_lock = false;
|
---|
[7768b8d] | 97 |
|
---|
| 98 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.alloc), &this.alloc));
|
---|
| 99 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.ready), &this.ready));
|
---|
| 100 |
|
---|
| 101 | }
|
---|
[b388ee81] | 102 | void ^?{}(__scheduler_RWLock_t & this) {
|
---|
[7768b8d] | 103 | free(this.data);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | //=======================================================================
|
---|
| 108 | // Lock-Free registering/unregistering of threads
|
---|
[c993b15] | 109 | unsigned register_proc_id( void ) with(*__scheduler_lock) {
|
---|
[b388ee81] | 110 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p for RW-Lock\n", proc);
|
---|
[c993b15] | 111 | bool * handle = (bool *)&kernelTLS().sched_lock;
|
---|
[504a7dc] | 112 |
|
---|
[7768b8d] | 113 | // Step - 1 : check if there is already space in the data
|
---|
| 114 | uint_fast32_t s = ready;
|
---|
| 115 |
|
---|
| 116 | // Check among all the ready
|
---|
| 117 | for(uint_fast32_t i = 0; i < s; i++) {
|
---|
[c993b15] | 118 | bool * volatile * cell = (bool * volatile *)&data[i]; // Cforall is bugged and the double volatiles causes problems
|
---|
| 119 | /* paranoid */ verify( handle != *cell );
|
---|
| 120 |
|
---|
| 121 | bool * null = 0p; // Re-write every loop since compare thrashes it
|
---|
| 122 | if( __atomic_load_n(cell, (int)__ATOMIC_RELAXED) == null
|
---|
| 123 | && __atomic_compare_exchange_n( cell, &null, handle, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
|
---|
| 124 | /* paranoid */ verify(i < ready);
|
---|
| 125 | /* paranoid */ verify( (kernelTLS().sched_id = i, true) );
|
---|
| 126 | return i;
|
---|
[7768b8d] | 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
[b388ee81] | 130 | if(max <= alloc) abort("Trying to create more than %ud processors", __scheduler_lock->max);
|
---|
[7768b8d] | 131 |
|
---|
| 132 | // Step - 2 : F&A to get a new spot in the array.
|
---|
| 133 | uint_fast32_t n = __atomic_fetch_add(&alloc, 1, __ATOMIC_SEQ_CST);
|
---|
[b388ee81] | 134 | if(max <= n) abort("Trying to create more than %ud processors", __scheduler_lock->max);
|
---|
[7768b8d] | 135 |
|
---|
| 136 | // Step - 3 : Mark space as used and then publish it.
|
---|
[c993b15] | 137 | data[n] = handle;
|
---|
[fd9b524] | 138 | while() {
|
---|
[7768b8d] | 139 | unsigned copy = n;
|
---|
| 140 | if( __atomic_load_n(&ready, __ATOMIC_RELAXED) == n
|
---|
| 141 | && __atomic_compare_exchange_n(&ready, ©, n + 1, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
|
---|
| 142 | break;
|
---|
[fd9b524] | 143 | Pause();
|
---|
[7768b8d] | 144 | }
|
---|
| 145 |
|
---|
[1b143de] | 146 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p done, id %lu\n", proc, n);
|
---|
[504a7dc] | 147 |
|
---|
[7768b8d] | 148 | // Return new spot.
|
---|
[c993b15] | 149 | /* paranoid */ verify(n < ready);
|
---|
| 150 | /* paranoid */ verify( (kernelTLS().sched_id = n, true) );
|
---|
| 151 | return n;
|
---|
[7768b8d] | 152 | }
|
---|
| 153 |
|
---|
[c993b15] | 154 | void unregister_proc_id( unsigned id ) with(*__scheduler_lock) {
|
---|
| 155 | /* paranoid */ verify(id < ready);
|
---|
| 156 | /* paranoid */ verify(id == kernelTLS().sched_id);
|
---|
| 157 | /* paranoid */ verify(data[id] == &kernelTLS().sched_lock);
|
---|
| 158 |
|
---|
| 159 | bool * volatile * cell = (bool * volatile *)&data[id]; // Cforall is bugged and the double volatiles causes problems
|
---|
| 160 |
|
---|
| 161 | __atomic_store_n(cell, 0p, __ATOMIC_RELEASE);
|
---|
[504a7dc] | 162 |
|
---|
| 163 | __cfadbg_print_safe(ready_queue, "Kernel : Unregister proc %p\n", proc);
|
---|
[7768b8d] | 164 | }
|
---|
| 165 |
|
---|
| 166 | //-----------------------------------------------------------------------
|
---|
| 167 | // Writer side : acquire when changing the ready queue, e.g. adding more
|
---|
| 168 | // queues or removing them.
|
---|
[b388ee81] | 169 | uint_fast32_t ready_mutate_lock( void ) with(*__scheduler_lock) {
|
---|
[8fc652e0] | 170 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 171 | /* paranoid */ verify( ! kernelTLS().sched_lock );
|
---|
[62502cc4] | 172 |
|
---|
[7768b8d] | 173 | // Step 1 : lock global lock
|
---|
| 174 | // It is needed to avoid processors that register mid Critical-Section
|
---|
| 175 | // to simply lock their own lock and enter.
|
---|
[c993b15] | 176 | __atomic_acquire( &write_lock );
|
---|
[7768b8d] | 177 |
|
---|
| 178 | // Step 2 : lock per-proc lock
|
---|
| 179 | // Processors that are currently being registered aren't counted
|
---|
| 180 | // but can't be in read_lock or in the critical section.
|
---|
| 181 | // All other processors are counted
|
---|
| 182 | uint_fast32_t s = ready;
|
---|
| 183 | for(uint_fast32_t i = 0; i < s; i++) {
|
---|
[c993b15] | 184 | volatile bool * llock = data[i];
|
---|
| 185 | if(llock) __atomic_acquire( llock );
|
---|
[7768b8d] | 186 | }
|
---|
| 187 |
|
---|
[8fc652e0] | 188 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[7768b8d] | 189 | return s;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[b388ee81] | 192 | void ready_mutate_unlock( uint_fast32_t last_s ) with(*__scheduler_lock) {
|
---|
[8fc652e0] | 193 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[62502cc4] | 194 |
|
---|
[7768b8d] | 195 | // Step 1 : release local locks
|
---|
| 196 | // This must be done while the global lock is held to avoid
|
---|
| 197 | // threads that where created mid critical section
|
---|
| 198 | // to race to lock their local locks and have the writer
|
---|
| 199 | // immidiately unlock them
|
---|
| 200 | // Alternative solution : return s in write_lock and pass it to write_unlock
|
---|
| 201 | for(uint_fast32_t i = 0; i < last_s; i++) {
|
---|
[c993b15] | 202 | volatile bool * llock = data[i];
|
---|
| 203 | if(llock) __atomic_store_n(llock, (bool)false, __ATOMIC_RELEASE);
|
---|
[7768b8d] | 204 | }
|
---|
| 205 |
|
---|
| 206 | // Step 2 : release global lock
|
---|
[c993b15] | 207 | /*paranoid*/ assert(true == write_lock);
|
---|
| 208 | __atomic_store_n(&write_lock, (bool)false, __ATOMIC_RELEASE);
|
---|
[62502cc4] | 209 |
|
---|
[8fc652e0] | 210 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[7768b8d] | 211 | }
|
---|
| 212 |
|
---|
| 213 | //=======================================================================
|
---|
[9cc3a18] | 214 | // Cforall Ready Queue used for scheduling
|
---|
[b798713] | 215 | //=======================================================================
|
---|
| 216 | void ?{}(__ready_queue_t & this) with (this) {
|
---|
[28d73c1] | 217 | lanes.data = 0p;
|
---|
[9cc3a18] | 218 | lanes.tscs = 0p;
|
---|
[28d73c1] | 219 | lanes.count = 0;
|
---|
[b798713] | 220 | }
|
---|
| 221 |
|
---|
| 222 | void ^?{}(__ready_queue_t & this) with (this) {
|
---|
[5f6a172] | 223 | verify( SEQUENTIAL_SHARD == lanes.count );
|
---|
[dca5802] | 224 | free(lanes.data);
|
---|
[9cc3a18] | 225 | free(lanes.tscs);
|
---|
[dca5802] | 226 | }
|
---|
| 227 |
|
---|
[64a7146] | 228 | //-----------------------------------------------------------------------
|
---|
[431cd4f] | 229 | #if defined(USE_RELAXED_FIFO)
|
---|
| 230 | //-----------------------------------------------------------------------
|
---|
| 231 | // get index from random number with or without bias towards queues
|
---|
| 232 | static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred) {
|
---|
| 233 | unsigned i;
|
---|
| 234 | bool local;
|
---|
| 235 | unsigned rlow = r % BIAS;
|
---|
| 236 | unsigned rhigh = r / BIAS;
|
---|
| 237 | if((0 != rlow) && preferred >= 0) {
|
---|
| 238 | // (BIAS - 1) out of BIAS chances
|
---|
| 239 | // Use perferred queues
|
---|
| 240 | i = preferred + (rhigh % READYQ_SHARD_FACTOR);
|
---|
| 241 | local = true;
|
---|
| 242 | }
|
---|
| 243 | else {
|
---|
| 244 | // 1 out of BIAS chances
|
---|
| 245 | // Use all queues
|
---|
| 246 | i = rhigh;
|
---|
| 247 | local = false;
|
---|
| 248 | }
|
---|
| 249 | return [i, local];
|
---|
| 250 | }
|
---|
| 251 |
|
---|
[b808625] | 252 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool push_local) with (cltr->ready_queue) {
|
---|
[431cd4f] | 253 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
|
---|
[1b143de] | 254 |
|
---|
[b808625] | 255 | const bool external = !push_local || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
|
---|
[431cd4f] | 256 | /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
|
---|
[fd1f65e] | 257 |
|
---|
[431cd4f] | 258 | bool local;
|
---|
| 259 | int preferred = external ? -1 : kernelTLS().this_processor->rdq.id;
|
---|
[52769ba] | 260 |
|
---|
[431cd4f] | 261 | // Try to pick a lane and lock it
|
---|
| 262 | unsigned i;
|
---|
| 263 | do {
|
---|
| 264 | // Pick the index of a lane
|
---|
| 265 | unsigned r = __tls_rand_fwd();
|
---|
| 266 | [i, local] = idx_from_r(r, preferred);
|
---|
[772411a] | 267 |
|
---|
[431cd4f] | 268 | i %= __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
|
---|
| 269 |
|
---|
| 270 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[d2fadeb] | 271 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
|
---|
| 272 | else if(local) __tls_stats()->ready.push.local.attempt++;
|
---|
| 273 | else __tls_stats()->ready.push.share.attempt++;
|
---|
[431cd4f] | 274 | #endif
|
---|
[b798713] | 275 |
|
---|
[431cd4f] | 276 | // If we can't lock it retry
|
---|
| 277 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
|
---|
| 278 |
|
---|
| 279 | // Actually push it
|
---|
| 280 | push(lanes.data[i], thrd);
|
---|
| 281 |
|
---|
[b808625] | 282 | // Unlock and return
|
---|
| 283 | __atomic_unlock( &lanes.data[i].lock );
|
---|
[431cd4f] | 284 |
|
---|
| 285 | // Mark the current index in the tls rng instance as having an item
|
---|
| 286 | __tls_rand_advance_bck();
|
---|
| 287 |
|
---|
| 288 | __cfadbg_print_safe(ready_queue, "Kernel : Pushed %p on cluster %p (idx: %u, mask %llu, first %d)\n", thrd, cltr, i, used.mask[0], lane_first);
|
---|
| 289 |
|
---|
| 290 | // Update statistics
|
---|
[b798713] | 291 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
[d2fadeb] | 292 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
|
---|
| 293 | else if(local) __tls_stats()->ready.push.local.success++;
|
---|
| 294 | else __tls_stats()->ready.push.share.success++;
|
---|
[b798713] | 295 | #endif
|
---|
[431cd4f] | 296 | }
|
---|
[b798713] | 297 |
|
---|
[431cd4f] | 298 | // Pop from the ready queue from a given cluster
|
---|
| 299 | __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
|
---|
| 300 | /* paranoid */ verify( lanes.count > 0 );
|
---|
| 301 | /* paranoid */ verify( kernelTLS().this_processor );
|
---|
| 302 | /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
|
---|
[b798713] | 303 |
|
---|
[431cd4f] | 304 | unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
|
---|
| 305 | int preferred = kernelTLS().this_processor->rdq.id;
|
---|
[dca5802] | 306 |
|
---|
| 307 |
|
---|
[431cd4f] | 308 | // As long as the list is not empty, try finding a lane that isn't empty and pop from it
|
---|
| 309 | for(25) {
|
---|
| 310 | // Pick two lists at random
|
---|
| 311 | unsigned ri = __tls_rand_bck();
|
---|
| 312 | unsigned rj = __tls_rand_bck();
|
---|
[c426b03] | 313 |
|
---|
[431cd4f] | 314 | unsigned i, j;
|
---|
| 315 | __attribute__((unused)) bool locali, localj;
|
---|
| 316 | [i, locali] = idx_from_r(ri, preferred);
|
---|
| 317 | [j, localj] = idx_from_r(rj, preferred);
|
---|
[1b143de] | 318 |
|
---|
[431cd4f] | 319 | i %= count;
|
---|
| 320 | j %= count;
|
---|
[9cc3a18] | 321 |
|
---|
[431cd4f] | 322 | // try popping from the 2 picked lists
|
---|
[d2fadeb] | 323 | struct $thread * thrd = try_pop(cltr, i, j __STATS(, *(locali || localj ? &__tls_stats()->ready.pop.local : &__tls_stats()->ready.pop.help)));
|
---|
[431cd4f] | 324 | if(thrd) {
|
---|
| 325 | return thrd;
|
---|
| 326 | }
|
---|
| 327 | }
|
---|
[13c5e19] | 328 |
|
---|
[431cd4f] | 329 | // All lanes where empty return 0p
|
---|
| 330 | return 0p;
|
---|
| 331 | }
|
---|
[772411a] | 332 |
|
---|
[fc59df78] | 333 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) { return pop_fast(cltr); }
|
---|
| 334 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) {
|
---|
[431cd4f] | 335 | return search(cltr);
|
---|
| 336 | }
|
---|
| 337 | #endif
|
---|
| 338 | #if defined(USE_WORK_STEALING)
|
---|
[b808625] | 339 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool push_local) with (cltr->ready_queue) {
|
---|
[431cd4f] | 340 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
|
---|
[772411a] | 341 |
|
---|
[d3ba775] | 342 | // #define USE_PREFERRED
|
---|
| 343 | #if !defined(USE_PREFERRED)
|
---|
[b808625] | 344 | const bool external = !push_local || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
|
---|
[431cd4f] | 345 | /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
|
---|
[d3ba775] | 346 | #else
|
---|
| 347 | unsigned preferred = thrd->preferred;
|
---|
[b808625] | 348 | const bool external = push_local || (!kernelTLS().this_processor) || preferred == -1u || thrd->curr_cluster != cltr;
|
---|
[d3ba775] | 349 | /* paranoid */ verifyf(external || preferred < lanes.count, "Invalid preferred queue %u for %u lanes", preferred, lanes.count );
|
---|
[772411a] | 350 |
|
---|
[d3ba775] | 351 | unsigned r = preferred % READYQ_SHARD_FACTOR;
|
---|
| 352 | const unsigned start = preferred - r;
|
---|
[2b96031] | 353 | #endif
|
---|
[431cd4f] | 354 |
|
---|
| 355 | // Try to pick a lane and lock it
|
---|
| 356 | unsigned i;
|
---|
| 357 | do {
|
---|
[d2fadeb] | 358 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 359 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
|
---|
| 360 | else __tls_stats()->ready.push.local.attempt++;
|
---|
| 361 | #endif
|
---|
| 362 |
|
---|
[431cd4f] | 363 | if(unlikely(external)) {
|
---|
| 364 | i = __tls_rand() % lanes.count;
|
---|
| 365 | }
|
---|
| 366 | else {
|
---|
[d3ba775] | 367 | #if !defined(USE_PREFERRED)
|
---|
[b808625] | 368 | processor * proc = kernelTLS().this_processor;
|
---|
| 369 | unsigned r = proc->rdq.its++;
|
---|
| 370 | i = proc->rdq.id + (r % READYQ_SHARD_FACTOR);
|
---|
| 371 | #else
|
---|
[d3ba775] | 372 | i = start + (r++ % READYQ_SHARD_FACTOR);
|
---|
| 373 | #endif
|
---|
| 374 | }
|
---|
[431cd4f] | 375 | // If we can't lock it retry
|
---|
| 376 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
|
---|
[13c5e19] | 377 |
|
---|
[431cd4f] | 378 | // Actually push it
|
---|
| 379 | push(lanes.data[i], thrd);
|
---|
[13c5e19] | 380 |
|
---|
[b808625] | 381 | // Unlock and return
|
---|
| 382 | __atomic_unlock( &lanes.data[i].lock );
|
---|
[431cd4f] | 383 |
|
---|
[d2fadeb] | 384 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 385 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
|
---|
| 386 | else __tls_stats()->ready.push.local.success++;
|
---|
| 387 | #endif
|
---|
| 388 |
|
---|
[431cd4f] | 389 | __cfadbg_print_safe(ready_queue, "Kernel : Pushed %p on cluster %p (idx: %u, mask %llu, first %d)\n", thrd, cltr, i, used.mask[0], lane_first);
|
---|
[13c5e19] | 390 | }
|
---|
| 391 |
|
---|
[431cd4f] | 392 | // Pop from the ready queue from a given cluster
|
---|
| 393 | __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
|
---|
| 394 | /* paranoid */ verify( lanes.count > 0 );
|
---|
| 395 | /* paranoid */ verify( kernelTLS().this_processor );
|
---|
| 396 | /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
|
---|
| 397 |
|
---|
| 398 | processor * proc = kernelTLS().this_processor;
|
---|
| 399 |
|
---|
| 400 | if(proc->rdq.target == -1u) {
|
---|
[1680072] | 401 | unsigned long long min = ts(lanes.data[proc->rdq.id]);
|
---|
| 402 | for(int i = 0; i < READYQ_SHARD_FACTOR; i++) {
|
---|
| 403 | unsigned long long tsc = ts(lanes.data[proc->rdq.id + i]);
|
---|
| 404 | if(tsc < min) min = tsc;
|
---|
| 405 | }
|
---|
| 406 | proc->rdq.cutoff = min;
|
---|
[f55d54d] | 407 | proc->rdq.target = __tls_rand() % lanes.count;
|
---|
[431cd4f] | 408 | }
|
---|
[341aa39] | 409 | else {
|
---|
| 410 | unsigned target = proc->rdq.target;
|
---|
[431cd4f] | 411 | proc->rdq.target = -1u;
|
---|
[9cac0da] | 412 | const unsigned long long bias = 0; //2_500_000_000;
|
---|
| 413 | const unsigned long long cutoff = proc->rdq.cutoff > bias ? proc->rdq.cutoff - bias : proc->rdq.cutoff;
|
---|
| 414 | if(lanes.tscs[target].tv < cutoff && ts(lanes.data[target]) < cutoff) {
|
---|
[341aa39] | 415 | $thread * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
|
---|
| 416 | if(t) return t;
|
---|
| 417 | }
|
---|
[431cd4f] | 418 | }
|
---|
[13c5e19] | 419 |
|
---|
[431cd4f] | 420 | for(READYQ_SHARD_FACTOR) {
|
---|
[f55d54d] | 421 | unsigned i = proc->rdq.id + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
|
---|
[d2fadeb] | 422 | if($thread * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
|
---|
[431cd4f] | 423 | }
|
---|
| 424 | return 0p;
|
---|
[1eb239e4] | 425 | }
|
---|
| 426 |
|
---|
[431cd4f] | 427 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
|
---|
[fc59df78] | 428 | unsigned i = __tls_rand() % lanes.count;
|
---|
| 429 | return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
|
---|
| 430 | }
|
---|
[431cd4f] | 431 |
|
---|
[fc59df78] | 432 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) with (cltr->ready_queue) {
|
---|
[431cd4f] | 433 | return search(cltr);
|
---|
| 434 | }
|
---|
| 435 | #endif
|
---|
[1eb239e4] | 436 |
|
---|
[9cc3a18] | 437 | //=======================================================================
|
---|
| 438 | // Various Ready Queue utilities
|
---|
| 439 | //=======================================================================
|
---|
| 440 | // these function work the same or almost the same
|
---|
| 441 | // whether they are using work-stealing or relaxed fifo scheduling
|
---|
[1eb239e4] | 442 |
|
---|
[9cc3a18] | 443 | //-----------------------------------------------------------------------
|
---|
| 444 | // try to pop from a lane given by index w
|
---|
[d2fadeb] | 445 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
|
---|
| 446 | __STATS( stats.attempt++; )
|
---|
| 447 |
|
---|
[dca5802] | 448 | // Get relevant elements locally
|
---|
| 449 | __intrusive_lane_t & lane = lanes.data[w];
|
---|
| 450 |
|
---|
[b798713] | 451 | // If list looks empty retry
|
---|
[d2fadeb] | 452 | if( is_empty(lane) ) {
|
---|
| 453 | return 0p;
|
---|
| 454 | }
|
---|
[b798713] | 455 |
|
---|
| 456 | // If we can't get the lock retry
|
---|
[d2fadeb] | 457 | if( !__atomic_try_acquire(&lane.lock) ) {
|
---|
| 458 | return 0p;
|
---|
| 459 | }
|
---|
[b798713] | 460 |
|
---|
| 461 | // If list is empty, unlock and retry
|
---|
[dca5802] | 462 | if( is_empty(lane) ) {
|
---|
| 463 | __atomic_unlock(&lane.lock);
|
---|
[b798713] | 464 | return 0p;
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | // Actually pop the list
|
---|
[504a7dc] | 468 | struct $thread * thrd;
|
---|
[f302d80] | 469 | unsigned long long tsv;
|
---|
| 470 | [thrd, tsv] = pop(lane);
|
---|
[b798713] | 471 |
|
---|
[dca5802] | 472 | /* paranoid */ verify(thrd);
|
---|
[78ea291] | 473 | /* paranoid */ verify(tsv);
|
---|
[dca5802] | 474 | /* paranoid */ verify(lane.lock);
|
---|
[b798713] | 475 |
|
---|
| 476 | // Unlock and return
|
---|
[dca5802] | 477 | __atomic_unlock(&lane.lock);
|
---|
[b798713] | 478 |
|
---|
[dca5802] | 479 | // Update statistics
|
---|
[d2fadeb] | 480 | __STATS( stats.success++; )
|
---|
[b798713] | 481 |
|
---|
[431cd4f] | 482 | #if defined(USE_WORK_STEALING)
|
---|
[f302d80] | 483 | lanes.tscs[w].tv = tsv;
|
---|
[9cc3a18] | 484 | #endif
|
---|
[d72c074] | 485 |
|
---|
[d3ba775] | 486 | thrd->preferred = w;
|
---|
| 487 |
|
---|
[dca5802] | 488 | // return the popped thread
|
---|
[b798713] | 489 | return thrd;
|
---|
| 490 | }
|
---|
[04b5cef] | 491 |
|
---|
[9cc3a18] | 492 | //-----------------------------------------------------------------------
|
---|
| 493 | // try to pop from any lanes making sure you don't miss any threads push
|
---|
| 494 | // before the start of the function
|
---|
[431cd4f] | 495 | static inline struct $thread * search(struct cluster * cltr) with (cltr->ready_queue) {
|
---|
[9cc3a18] | 496 | /* paranoid */ verify( lanes.count > 0 );
|
---|
| 497 | unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
|
---|
| 498 | unsigned offset = __tls_rand();
|
---|
| 499 | for(i; count) {
|
---|
| 500 | unsigned idx = (offset + i) % count;
|
---|
[d2fadeb] | 501 | struct $thread * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search));
|
---|
[9cc3a18] | 502 | if(thrd) {
|
---|
| 503 | return thrd;
|
---|
| 504 | }
|
---|
[13c5e19] | 505 | }
|
---|
[9cc3a18] | 506 |
|
---|
| 507 | // All lanes where empty return 0p
|
---|
| 508 | return 0p;
|
---|
[b798713] | 509 | }
|
---|
| 510 |
|
---|
| 511 | //-----------------------------------------------------------------------
|
---|
[9cc3a18] | 512 | // Check that all the intrusive queues in the data structure are still consistent
|
---|
[b798713] | 513 | static void check( __ready_queue_t & q ) with (q) {
|
---|
[d3ba775] | 514 | #if defined(__CFA_WITH_VERIFY__)
|
---|
[b798713] | 515 | {
|
---|
[dca5802] | 516 | for( idx ; lanes.count ) {
|
---|
| 517 | __intrusive_lane_t & sl = lanes.data[idx];
|
---|
| 518 | assert(!lanes.data[idx].lock);
|
---|
[b798713] | 519 |
|
---|
[2b96031] | 520 | if(is_empty(sl)) {
|
---|
| 521 | assert( sl.anchor.next == 0p );
|
---|
| 522 | assert( sl.anchor.ts == 0 );
|
---|
| 523 | assert( mock_head(sl) == sl.prev );
|
---|
| 524 | } else {
|
---|
| 525 | assert( sl.anchor.next != 0p );
|
---|
| 526 | assert( sl.anchor.ts != 0 );
|
---|
| 527 | assert( mock_head(sl) != sl.prev );
|
---|
| 528 | }
|
---|
[b798713] | 529 | }
|
---|
| 530 | }
|
---|
| 531 | #endif
|
---|
| 532 | }
|
---|
| 533 |
|
---|
[9cc3a18] | 534 | //-----------------------------------------------------------------------
|
---|
| 535 | // Given 2 indexes, pick the list with the oldest push an try to pop from it
|
---|
[d2fadeb] | 536 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
|
---|
[9cc3a18] | 537 | // Pick the bet list
|
---|
| 538 | int w = i;
|
---|
| 539 | if( __builtin_expect(!is_empty(lanes.data[j]), true) ) {
|
---|
| 540 | w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j;
|
---|
| 541 | }
|
---|
| 542 |
|
---|
[d2fadeb] | 543 | return try_pop(cltr, w __STATS(, stats));
|
---|
[9cc3a18] | 544 | }
|
---|
| 545 |
|
---|
[b798713] | 546 | // Call this function of the intrusive list was moved using memcpy
|
---|
[dca5802] | 547 | // fixes the list so that the pointers back to anchors aren't left dangling
|
---|
| 548 | static inline void fix(__intrusive_lane_t & ll) {
|
---|
[2b96031] | 549 | if(is_empty(ll)) {
|
---|
| 550 | verify(ll.anchor.next == 0p);
|
---|
| 551 | ll.prev = mock_head(ll);
|
---|
| 552 | }
|
---|
[b798713] | 553 | }
|
---|
| 554 |
|
---|
[69914cbc] | 555 | static void assign_list(unsigned & value, dlist(processor) & list, unsigned count) {
|
---|
[a017ee7] | 556 | processor * it = &list`first;
|
---|
| 557 | for(unsigned i = 0; i < count; i++) {
|
---|
| 558 | /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
|
---|
[431cd4f] | 559 | it->rdq.id = value;
|
---|
| 560 | it->rdq.target = -1u;
|
---|
[9cc3a18] | 561 | value += READYQ_SHARD_FACTOR;
|
---|
[a017ee7] | 562 | it = &(*it)`next;
|
---|
| 563 | }
|
---|
| 564 | }
|
---|
| 565 |
|
---|
[9cc3a18] | 566 | static void reassign_cltr_id(struct cluster * cltr) {
|
---|
[a017ee7] | 567 | unsigned preferred = 0;
|
---|
[9cc3a18] | 568 | assign_list(preferred, cltr->procs.actives, cltr->procs.total - cltr->procs.idle);
|
---|
| 569 | assign_list(preferred, cltr->procs.idles , cltr->procs.idle );
|
---|
[a017ee7] | 570 | }
|
---|
| 571 |
|
---|
[431cd4f] | 572 | static void fix_times( struct cluster * cltr ) with( cltr->ready_queue ) {
|
---|
| 573 | #if defined(USE_WORK_STEALING)
|
---|
| 574 | lanes.tscs = alloc(lanes.count, lanes.tscs`realloc);
|
---|
| 575 | for(i; lanes.count) {
|
---|
[9cac0da] | 576 | unsigned long long tsc = ts(lanes.data[i]);
|
---|
| 577 | lanes.tscs[i].tv = tsc != 0 ? tsc : rdtscl();
|
---|
[431cd4f] | 578 | }
|
---|
| 579 | #endif
|
---|
| 580 | }
|
---|
| 581 |
|
---|
[dca5802] | 582 | // Grow the ready queue
|
---|
[a017ee7] | 583 | void ready_queue_grow(struct cluster * cltr) {
|
---|
[bd0bdd37] | 584 | size_t ncount;
|
---|
[a017ee7] | 585 | int target = cltr->procs.total;
|
---|
[bd0bdd37] | 586 |
|
---|
[64a7146] | 587 | /* paranoid */ verify( ready_mutate_islocked() );
|
---|
[504a7dc] | 588 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n");
|
---|
[b798713] | 589 |
|
---|
[dca5802] | 590 | // Make sure that everything is consistent
|
---|
| 591 | /* paranoid */ check( cltr->ready_queue );
|
---|
| 592 |
|
---|
| 593 | // grow the ready queue
|
---|
[b798713] | 594 | with( cltr->ready_queue ) {
|
---|
[39fc03e] | 595 | // Find new count
|
---|
| 596 | // Make sure we always have atleast 1 list
|
---|
[bd0bdd37] | 597 | if(target >= 2) {
|
---|
[9cc3a18] | 598 | ncount = target * READYQ_SHARD_FACTOR;
|
---|
[bd0bdd37] | 599 | } else {
|
---|
[5f6a172] | 600 | ncount = SEQUENTIAL_SHARD;
|
---|
[bd0bdd37] | 601 | }
|
---|
[b798713] | 602 |
|
---|
[dca5802] | 603 | // Allocate new array (uses realloc and memcpies the data)
|
---|
[ceb7db8] | 604 | lanes.data = alloc( ncount, lanes.data`realloc );
|
---|
[b798713] | 605 |
|
---|
| 606 | // Fix the moved data
|
---|
[dca5802] | 607 | for( idx; (size_t)lanes.count ) {
|
---|
| 608 | fix(lanes.data[idx]);
|
---|
[b798713] | 609 | }
|
---|
| 610 |
|
---|
| 611 | // Construct new data
|
---|
[dca5802] | 612 | for( idx; (size_t)lanes.count ~ ncount) {
|
---|
| 613 | (lanes.data[idx]){};
|
---|
[b798713] | 614 | }
|
---|
| 615 |
|
---|
| 616 | // Update original
|
---|
[dca5802] | 617 | lanes.count = ncount;
|
---|
[b798713] | 618 | }
|
---|
| 619 |
|
---|
[9cc3a18] | 620 | fix_times(cltr);
|
---|
| 621 |
|
---|
| 622 | reassign_cltr_id(cltr);
|
---|
[a017ee7] | 623 |
|
---|
[b798713] | 624 | // Make sure that everything is consistent
|
---|
[dca5802] | 625 | /* paranoid */ check( cltr->ready_queue );
|
---|
| 626 |
|
---|
[504a7dc] | 627 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n");
|
---|
[dca5802] | 628 |
|
---|
[64a7146] | 629 | /* paranoid */ verify( ready_mutate_islocked() );
|
---|
[b798713] | 630 | }
|
---|
| 631 |
|
---|
[dca5802] | 632 | // Shrink the ready queue
|
---|
[a017ee7] | 633 | void ready_queue_shrink(struct cluster * cltr) {
|
---|
[64a7146] | 634 | /* paranoid */ verify( ready_mutate_islocked() );
|
---|
[504a7dc] | 635 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n");
|
---|
[dca5802] | 636 |
|
---|
| 637 | // Make sure that everything is consistent
|
---|
| 638 | /* paranoid */ check( cltr->ready_queue );
|
---|
| 639 |
|
---|
[a017ee7] | 640 | int target = cltr->procs.total;
|
---|
| 641 |
|
---|
[b798713] | 642 | with( cltr->ready_queue ) {
|
---|
[39fc03e] | 643 | // Remember old count
|
---|
[dca5802] | 644 | size_t ocount = lanes.count;
|
---|
[b798713] | 645 |
|
---|
[39fc03e] | 646 | // Find new count
|
---|
| 647 | // Make sure we always have atleast 1 list
|
---|
[5f6a172] | 648 | lanes.count = target >= 2 ? target * READYQ_SHARD_FACTOR: SEQUENTIAL_SHARD;
|
---|
[39fc03e] | 649 | /* paranoid */ verify( ocount >= lanes.count );
|
---|
[9cc3a18] | 650 | /* paranoid */ verify( lanes.count == target * READYQ_SHARD_FACTOR || target < 2 );
|
---|
[dca5802] | 651 |
|
---|
| 652 | // for printing count the number of displaced threads
|
---|
[504a7dc] | 653 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
|
---|
[dca5802] | 654 | __attribute__((unused)) size_t displaced = 0;
|
---|
| 655 | #endif
|
---|
[b798713] | 656 |
|
---|
| 657 | // redistribute old data
|
---|
[dca5802] | 658 | for( idx; (size_t)lanes.count ~ ocount) {
|
---|
| 659 | // Lock is not strictly needed but makes checking invariants much easier
|
---|
[1b143de] | 660 | __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock);
|
---|
[b798713] | 661 | verify(locked);
|
---|
[dca5802] | 662 |
|
---|
| 663 | // As long as we can pop from this lane to push the threads somewhere else in the queue
|
---|
| 664 | while(!is_empty(lanes.data[idx])) {
|
---|
[504a7dc] | 665 | struct $thread * thrd;
|
---|
[f302d80] | 666 | unsigned long long _;
|
---|
| 667 | [thrd, _] = pop(lanes.data[idx]);
|
---|
[dca5802] | 668 |
|
---|
[b808625] | 669 | push(cltr, thrd, true);
|
---|
[dca5802] | 670 |
|
---|
| 671 | // for printing count the number of displaced threads
|
---|
[504a7dc] | 672 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
|
---|
[dca5802] | 673 | displaced++;
|
---|
| 674 | #endif
|
---|
[b798713] | 675 | }
|
---|
| 676 |
|
---|
[dca5802] | 677 | // Unlock the lane
|
---|
| 678 | __atomic_unlock(&lanes.data[idx].lock);
|
---|
[b798713] | 679 |
|
---|
| 680 | // TODO print the queue statistics here
|
---|
| 681 |
|
---|
[dca5802] | 682 | ^(lanes.data[idx]){};
|
---|
[b798713] | 683 | }
|
---|
| 684 |
|
---|
[504a7dc] | 685 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced);
|
---|
[c84b4be] | 686 |
|
---|
[dca5802] | 687 | // Allocate new array (uses realloc and memcpies the data)
|
---|
[ceb7db8] | 688 | lanes.data = alloc( lanes.count, lanes.data`realloc );
|
---|
[b798713] | 689 |
|
---|
| 690 | // Fix the moved data
|
---|
[dca5802] | 691 | for( idx; (size_t)lanes.count ) {
|
---|
| 692 | fix(lanes.data[idx]);
|
---|
[b798713] | 693 | }
|
---|
| 694 | }
|
---|
| 695 |
|
---|
[9cc3a18] | 696 | fix_times(cltr);
|
---|
| 697 |
|
---|
| 698 | reassign_cltr_id(cltr);
|
---|
[a017ee7] | 699 |
|
---|
[b798713] | 700 | // Make sure that everything is consistent
|
---|
[dca5802] | 701 | /* paranoid */ check( cltr->ready_queue );
|
---|
| 702 |
|
---|
[504a7dc] | 703 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n");
|
---|
[64a7146] | 704 | /* paranoid */ verify( ready_mutate_islocked() );
|
---|
[fd9b524] | 705 | }
|
---|
[8cd5434] | 706 |
|
---|
| 707 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 708 | unsigned cnt(const __ready_queue_t & this, unsigned idx) {
|
---|
| 709 | /* paranoid */ verify(this.lanes.count > idx);
|
---|
| 710 | return this.lanes.data[idx].cnt;
|
---|
| 711 | }
|
---|
| 712 | #endif
|
---|