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