[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 | |
---|
[07b4970] | 22 | #define USE_RELAXED_FIFO |
---|
[9cc3a18] | 23 | // #define USE_WORK_STEALING |
---|
| 24 | |
---|
[7768b8d] | 25 | #include "bits/defs.hfa" |
---|
[12daa43] | 26 | #include "device/cpu.hfa" |
---|
[7768b8d] | 27 | #include "kernel_private.hfa" |
---|
| 28 | |
---|
| 29 | #include "stdlib.hfa" |
---|
[61d7bec] | 30 | #include "math.hfa" |
---|
[7768b8d] | 31 | |
---|
[0ee224b] | 32 | #include <errno.h> |
---|
[04b5cef] | 33 | #include <unistd.h> |
---|
| 34 | |
---|
[0ee224b] | 35 | extern "C" { |
---|
| 36 | #include <sys/syscall.h> // __NR_xxx |
---|
| 37 | } |
---|
| 38 | |
---|
[13c5e19] | 39 | #include "ready_subqueue.hfa" |
---|
| 40 | |
---|
[7768b8d] | 41 | static const size_t cache_line_size = 64; |
---|
| 42 | |
---|
[d2fadeb] | 43 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 44 | #define __STATS(...) __VA_ARGS__ |
---|
| 45 | #else |
---|
| 46 | #define __STATS(...) |
---|
| 47 | #endif |
---|
| 48 | |
---|
[dca5802] | 49 | // No overriden function, no environment variable, no define |
---|
| 50 | // fall back to a magic number |
---|
| 51 | #ifndef __CFA_MAX_PROCESSORS__ |
---|
[b388ee81] | 52 | #define __CFA_MAX_PROCESSORS__ 1024 |
---|
[dca5802] | 53 | #endif |
---|
[7768b8d] | 54 | |
---|
[12daa43] | 55 | #if defined(USE_CPU_WORK_STEALING) |
---|
| 56 | #define READYQ_SHARD_FACTOR 2 |
---|
| 57 | #elif defined(USE_RELAXED_FIFO) |
---|
[9cc3a18] | 58 | #define BIAS 4 |
---|
| 59 | #define READYQ_SHARD_FACTOR 4 |
---|
[5f6a172] | 60 | #define SEQUENTIAL_SHARD 1 |
---|
[9cc3a18] | 61 | #elif defined(USE_WORK_STEALING) |
---|
| 62 | #define READYQ_SHARD_FACTOR 2 |
---|
[5f6a172] | 63 | #define SEQUENTIAL_SHARD 2 |
---|
[9cc3a18] | 64 | #else |
---|
| 65 | #error no scheduling strategy selected |
---|
| 66 | #endif |
---|
| 67 | |
---|
[d2fadeb] | 68 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)); |
---|
| 69 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)); |
---|
[431cd4f] | 70 | static inline struct $thread * search(struct cluster * cltr); |
---|
[d2fadeb] | 71 | static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred); |
---|
[9cc3a18] | 72 | |
---|
[04b5cef] | 73 | |
---|
[dca5802] | 74 | // returns the maximum number of processors the RWLock support |
---|
[7768b8d] | 75 | __attribute__((weak)) unsigned __max_processors() { |
---|
| 76 | const char * max_cores_s = getenv("CFA_MAX_PROCESSORS"); |
---|
| 77 | if(!max_cores_s) { |
---|
[504a7dc] | 78 | __cfadbg_print_nolock(ready_queue, "No CFA_MAX_PROCESSORS in ENV\n"); |
---|
[dca5802] | 79 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 80 | } |
---|
| 81 | |
---|
| 82 | char * endptr = 0p; |
---|
| 83 | long int max_cores_l = strtol(max_cores_s, &endptr, 10); |
---|
| 84 | if(max_cores_l < 1 || max_cores_l > 65535) { |
---|
[504a7dc] | 85 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS out of range : %ld\n", max_cores_l); |
---|
[dca5802] | 86 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 87 | } |
---|
| 88 | if('\0' != *endptr) { |
---|
[504a7dc] | 89 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS not a decimal number : %s\n", max_cores_s); |
---|
[dca5802] | 90 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 91 | } |
---|
| 92 | |
---|
| 93 | return max_cores_l; |
---|
| 94 | } |
---|
| 95 | |
---|
[0ee224b] | 96 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
| 97 | // No forward declaration needed |
---|
| 98 | #define __kernel_rseq_register rseq_register_current_thread |
---|
| 99 | #define __kernel_rseq_unregister rseq_unregister_current_thread |
---|
| 100 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
| 101 | void __kernel_raw_rseq_register (void); |
---|
| 102 | void __kernel_raw_rseq_unregister(void); |
---|
| 103 | |
---|
| 104 | #define __kernel_rseq_register __kernel_raw_rseq_register |
---|
| 105 | #define __kernel_rseq_unregister __kernel_raw_rseq_unregister |
---|
| 106 | #else |
---|
| 107 | // No forward declaration needed |
---|
| 108 | // No initialization needed |
---|
| 109 | static inline void noop(void) {} |
---|
| 110 | |
---|
| 111 | #define __kernel_rseq_register noop |
---|
| 112 | #define __kernel_rseq_unregister noop |
---|
| 113 | #endif |
---|
| 114 | |
---|
[7768b8d] | 115 | //======================================================================= |
---|
| 116 | // Cluster wide reader-writer lock |
---|
| 117 | //======================================================================= |
---|
[b388ee81] | 118 | void ?{}(__scheduler_RWLock_t & this) { |
---|
[7768b8d] | 119 | this.max = __max_processors(); |
---|
| 120 | this.alloc = 0; |
---|
| 121 | this.ready = 0; |
---|
| 122 | this.data = alloc(this.max); |
---|
[c993b15] | 123 | this.write_lock = false; |
---|
[7768b8d] | 124 | |
---|
| 125 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.alloc), &this.alloc)); |
---|
| 126 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.ready), &this.ready)); |
---|
| 127 | |
---|
| 128 | } |
---|
[b388ee81] | 129 | void ^?{}(__scheduler_RWLock_t & this) { |
---|
[7768b8d] | 130 | free(this.data); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | //======================================================================= |
---|
| 135 | // Lock-Free registering/unregistering of threads |
---|
[c993b15] | 136 | unsigned register_proc_id( void ) with(*__scheduler_lock) { |
---|
[0ee224b] | 137 | __kernel_rseq_register(); |
---|
| 138 | |
---|
[b388ee81] | 139 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p for RW-Lock\n", proc); |
---|
[c993b15] | 140 | bool * handle = (bool *)&kernelTLS().sched_lock; |
---|
[504a7dc] | 141 | |
---|
[7768b8d] | 142 | // Step - 1 : check if there is already space in the data |
---|
| 143 | uint_fast32_t s = ready; |
---|
| 144 | |
---|
| 145 | // Check among all the ready |
---|
| 146 | for(uint_fast32_t i = 0; i < s; i++) { |
---|
[c993b15] | 147 | bool * volatile * cell = (bool * volatile *)&data[i]; // Cforall is bugged and the double volatiles causes problems |
---|
| 148 | /* paranoid */ verify( handle != *cell ); |
---|
| 149 | |
---|
| 150 | bool * null = 0p; // Re-write every loop since compare thrashes it |
---|
| 151 | if( __atomic_load_n(cell, (int)__ATOMIC_RELAXED) == null |
---|
| 152 | && __atomic_compare_exchange_n( cell, &null, handle, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 153 | /* paranoid */ verify(i < ready); |
---|
| 154 | /* paranoid */ verify( (kernelTLS().sched_id = i, true) ); |
---|
| 155 | return i; |
---|
[7768b8d] | 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
[b388ee81] | 159 | if(max <= alloc) abort("Trying to create more than %ud processors", __scheduler_lock->max); |
---|
[7768b8d] | 160 | |
---|
| 161 | // Step - 2 : F&A to get a new spot in the array. |
---|
| 162 | uint_fast32_t n = __atomic_fetch_add(&alloc, 1, __ATOMIC_SEQ_CST); |
---|
[b388ee81] | 163 | if(max <= n) abort("Trying to create more than %ud processors", __scheduler_lock->max); |
---|
[7768b8d] | 164 | |
---|
| 165 | // Step - 3 : Mark space as used and then publish it. |
---|
[c993b15] | 166 | data[n] = handle; |
---|
[fd9b524] | 167 | while() { |
---|
[7768b8d] | 168 | unsigned copy = n; |
---|
| 169 | if( __atomic_load_n(&ready, __ATOMIC_RELAXED) == n |
---|
| 170 | && __atomic_compare_exchange_n(&ready, ©, n + 1, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
---|
| 171 | break; |
---|
[fd9b524] | 172 | Pause(); |
---|
[7768b8d] | 173 | } |
---|
| 174 | |
---|
[1b143de] | 175 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p done, id %lu\n", proc, n); |
---|
[504a7dc] | 176 | |
---|
[7768b8d] | 177 | // Return new spot. |
---|
[c993b15] | 178 | /* paranoid */ verify(n < ready); |
---|
| 179 | /* paranoid */ verify( (kernelTLS().sched_id = n, true) ); |
---|
| 180 | return n; |
---|
[7768b8d] | 181 | } |
---|
| 182 | |
---|
[c993b15] | 183 | void unregister_proc_id( unsigned id ) with(*__scheduler_lock) { |
---|
| 184 | /* paranoid */ verify(id < ready); |
---|
| 185 | /* paranoid */ verify(id == kernelTLS().sched_id); |
---|
| 186 | /* paranoid */ verify(data[id] == &kernelTLS().sched_lock); |
---|
| 187 | |
---|
| 188 | bool * volatile * cell = (bool * volatile *)&data[id]; // Cforall is bugged and the double volatiles causes problems |
---|
| 189 | |
---|
| 190 | __atomic_store_n(cell, 0p, __ATOMIC_RELEASE); |
---|
[504a7dc] | 191 | |
---|
| 192 | __cfadbg_print_safe(ready_queue, "Kernel : Unregister proc %p\n", proc); |
---|
[0ee224b] | 193 | |
---|
| 194 | __kernel_rseq_unregister(); |
---|
[7768b8d] | 195 | } |
---|
| 196 | |
---|
| 197 | //----------------------------------------------------------------------- |
---|
| 198 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
| 199 | // queues or removing them. |
---|
[b388ee81] | 200 | uint_fast32_t ready_mutate_lock( void ) with(*__scheduler_lock) { |
---|
[8fc652e0] | 201 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[c993b15] | 202 | /* paranoid */ verify( ! kernelTLS().sched_lock ); |
---|
[62502cc4] | 203 | |
---|
[7768b8d] | 204 | // Step 1 : lock global lock |
---|
| 205 | // It is needed to avoid processors that register mid Critical-Section |
---|
| 206 | // to simply lock their own lock and enter. |
---|
[c993b15] | 207 | __atomic_acquire( &write_lock ); |
---|
[7768b8d] | 208 | |
---|
| 209 | // Step 2 : lock per-proc lock |
---|
| 210 | // Processors that are currently being registered aren't counted |
---|
| 211 | // but can't be in read_lock or in the critical section. |
---|
| 212 | // All other processors are counted |
---|
| 213 | uint_fast32_t s = ready; |
---|
| 214 | for(uint_fast32_t i = 0; i < s; i++) { |
---|
[c993b15] | 215 | volatile bool * llock = data[i]; |
---|
| 216 | if(llock) __atomic_acquire( llock ); |
---|
[7768b8d] | 217 | } |
---|
| 218 | |
---|
[8fc652e0] | 219 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[7768b8d] | 220 | return s; |
---|
| 221 | } |
---|
| 222 | |
---|
[b388ee81] | 223 | void ready_mutate_unlock( uint_fast32_t last_s ) with(*__scheduler_lock) { |
---|
[8fc652e0] | 224 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[62502cc4] | 225 | |
---|
[7768b8d] | 226 | // Step 1 : release local locks |
---|
| 227 | // This must be done while the global lock is held to avoid |
---|
| 228 | // threads that where created mid critical section |
---|
| 229 | // to race to lock their local locks and have the writer |
---|
| 230 | // immidiately unlock them |
---|
| 231 | // Alternative solution : return s in write_lock and pass it to write_unlock |
---|
| 232 | for(uint_fast32_t i = 0; i < last_s; i++) { |
---|
[c993b15] | 233 | volatile bool * llock = data[i]; |
---|
| 234 | if(llock) __atomic_store_n(llock, (bool)false, __ATOMIC_RELEASE); |
---|
[7768b8d] | 235 | } |
---|
| 236 | |
---|
| 237 | // Step 2 : release global lock |
---|
[c993b15] | 238 | /*paranoid*/ assert(true == write_lock); |
---|
| 239 | __atomic_store_n(&write_lock, (bool)false, __ATOMIC_RELEASE); |
---|
[62502cc4] | 240 | |
---|
[8fc652e0] | 241 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[7768b8d] | 242 | } |
---|
| 243 | |
---|
| 244 | //======================================================================= |
---|
[9cc3a18] | 245 | // Cforall Ready Queue used for scheduling |
---|
[b798713] | 246 | //======================================================================= |
---|
| 247 | void ?{}(__ready_queue_t & this) with (this) { |
---|
[12daa43] | 248 | #if defined(USE_CPU_WORK_STEALING) |
---|
| 249 | lanes.count = cpu_info.hthrd_count * READYQ_SHARD_FACTOR; |
---|
| 250 | lanes.data = alloc( lanes.count ); |
---|
| 251 | lanes.tscs = alloc( lanes.count ); |
---|
| 252 | |
---|
| 253 | for( idx; (size_t)lanes.count ) { |
---|
| 254 | (lanes.data[idx]){}; |
---|
| 255 | lanes.tscs[idx].tv = rdtscl(); |
---|
| 256 | } |
---|
| 257 | #else |
---|
| 258 | lanes.data = 0p; |
---|
| 259 | lanes.tscs = 0p; |
---|
| 260 | lanes.count = 0; |
---|
| 261 | #endif |
---|
[b798713] | 262 | } |
---|
| 263 | |
---|
| 264 | void ^?{}(__ready_queue_t & this) with (this) { |
---|
[12daa43] | 265 | #if !defined(USE_CPU_WORK_STEALING) |
---|
| 266 | verify( SEQUENTIAL_SHARD == lanes.count ); |
---|
| 267 | #endif |
---|
| 268 | |
---|
[dca5802] | 269 | free(lanes.data); |
---|
[9cc3a18] | 270 | free(lanes.tscs); |
---|
[dca5802] | 271 | } |
---|
| 272 | |
---|
[64a7146] | 273 | //----------------------------------------------------------------------- |
---|
[12daa43] | 274 | #if defined(USE_CPU_WORK_STEALING) |
---|
| 275 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool push_local) with (cltr->ready_queue) { |
---|
| 276 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr); |
---|
| 277 | |
---|
| 278 | processor * const proc = kernelTLS().this_processor; |
---|
| 279 | const bool external = !push_local || (!proc) || (cltr != proc->cltr); |
---|
| 280 | |
---|
| 281 | const int cpu = __kernel_getcpu(); |
---|
| 282 | /* paranoid */ verify(cpu >= 0); |
---|
| 283 | /* paranoid */ verify(cpu < cpu_info.hthrd_count); |
---|
| 284 | /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 285 | |
---|
[df7597e0] | 286 | const cpu_map_entry_t & map = cpu_info.llc_map[cpu]; |
---|
| 287 | /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 288 | /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 289 | /* paranoid */ verifyf((map.start + map.count) * READYQ_SHARD_FACTOR <= lanes.count, "have %u lanes but map can go up to %u", lanes.count, (map.start + map.count) * READYQ_SHARD_FACTOR); |
---|
| 290 | |
---|
| 291 | const int start = map.self * READYQ_SHARD_FACTOR; |
---|
[12daa43] | 292 | unsigned i; |
---|
| 293 | do { |
---|
| 294 | unsigned r; |
---|
| 295 | if(unlikely(external)) { r = __tls_rand(); } |
---|
| 296 | else { r = proc->rdq.its++; } |
---|
| 297 | i = start + (r % READYQ_SHARD_FACTOR); |
---|
| 298 | // If we can't lock it retry |
---|
| 299 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) ); |
---|
| 300 | |
---|
| 301 | // Actually push it |
---|
| 302 | push(lanes.data[i], thrd); |
---|
| 303 | |
---|
| 304 | // Unlock and return |
---|
| 305 | __atomic_unlock( &lanes.data[i].lock ); |
---|
| 306 | |
---|
| 307 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 308 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED); |
---|
| 309 | else __tls_stats()->ready.push.local.success++; |
---|
| 310 | #endif |
---|
| 311 | |
---|
| 312 | __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); |
---|
| 313 | |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | // Pop from the ready queue from a given cluster |
---|
| 317 | __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) { |
---|
| 318 | /* paranoid */ verify( lanes.count > 0 ); |
---|
| 319 | /* paranoid */ verify( kernelTLS().this_processor ); |
---|
| 320 | |
---|
| 321 | const int cpu = __kernel_getcpu(); |
---|
| 322 | /* paranoid */ verify(cpu >= 0); |
---|
| 323 | /* paranoid */ verify(cpu < cpu_info.hthrd_count); |
---|
[df7597e0] | 324 | /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 325 | |
---|
| 326 | const cpu_map_entry_t & map = cpu_info.llc_map[cpu]; |
---|
| 327 | /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 328 | /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count); |
---|
| 329 | /* paranoid */ verifyf((map.start + map.count) * READYQ_SHARD_FACTOR <= lanes.count, "have %u lanes but map can go up to %u", lanes.count, (map.start + map.count) * READYQ_SHARD_FACTOR); |
---|
[12daa43] | 330 | |
---|
| 331 | processor * const proc = kernelTLS().this_processor; |
---|
[df7597e0] | 332 | const int start = map.self * READYQ_SHARD_FACTOR; |
---|
[12daa43] | 333 | |
---|
| 334 | // Did we already have a help target |
---|
| 335 | if(proc->rdq.target == -1u) { |
---|
| 336 | // if We don't have a |
---|
| 337 | unsigned long long min = ts(lanes.data[start]); |
---|
| 338 | for(i; READYQ_SHARD_FACTOR) { |
---|
| 339 | unsigned long long tsc = ts(lanes.data[start + i]); |
---|
| 340 | if(tsc < min) min = tsc; |
---|
| 341 | } |
---|
| 342 | proc->rdq.cutoff = min; |
---|
[df7597e0] | 343 | proc->rdq.target = (map.start * READYQ_SHARD_FACTOR) + (__tls_rand() % (map.count* READYQ_SHARD_FACTOR)); |
---|
[12daa43] | 344 | } |
---|
| 345 | else { |
---|
| 346 | const unsigned long long bias = 0; //2_500_000_000; |
---|
| 347 | const unsigned long long cutoff = proc->rdq.cutoff > bias ? proc->rdq.cutoff - bias : proc->rdq.cutoff; |
---|
| 348 | { |
---|
| 349 | unsigned target = proc->rdq.target; |
---|
| 350 | proc->rdq.target = -1u; |
---|
| 351 | if(lanes.tscs[target].tv < cutoff && ts(lanes.data[target]) < cutoff) { |
---|
| 352 | $thread * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help)); |
---|
| 353 | proc->rdq.last = target; |
---|
| 354 | if(t) return t; |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | |
---|
| 358 | unsigned last = proc->rdq.last; |
---|
| 359 | if(last != -1u && lanes.tscs[last].tv < cutoff && ts(lanes.data[last]) < cutoff) { |
---|
| 360 | $thread * t = try_pop(cltr, last __STATS(, __tls_stats()->ready.pop.help)); |
---|
| 361 | if(t) return t; |
---|
| 362 | } |
---|
| 363 | else { |
---|
| 364 | proc->rdq.last = -1u; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | for(READYQ_SHARD_FACTOR) { |
---|
| 369 | unsigned i = start + (proc->rdq.itr++ % READYQ_SHARD_FACTOR); |
---|
| 370 | if($thread * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | // All lanes where empty return 0p |
---|
| 374 | return 0p; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) with (cltr->ready_queue) { |
---|
| 378 | processor * const proc = kernelTLS().this_processor; |
---|
| 379 | unsigned last = proc->rdq.last; |
---|
| 380 | |
---|
| 381 | unsigned i = __tls_rand() % lanes.count; |
---|
| 382 | return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal)); |
---|
| 383 | } |
---|
| 384 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) { |
---|
| 385 | return search(cltr); |
---|
| 386 | } |
---|
| 387 | #endif |
---|
[431cd4f] | 388 | #if defined(USE_RELAXED_FIFO) |
---|
| 389 | //----------------------------------------------------------------------- |
---|
| 390 | // get index from random number with or without bias towards queues |
---|
| 391 | static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred) { |
---|
| 392 | unsigned i; |
---|
| 393 | bool local; |
---|
| 394 | unsigned rlow = r % BIAS; |
---|
| 395 | unsigned rhigh = r / BIAS; |
---|
| 396 | if((0 != rlow) && preferred >= 0) { |
---|
| 397 | // (BIAS - 1) out of BIAS chances |
---|
| 398 | // Use perferred queues |
---|
| 399 | i = preferred + (rhigh % READYQ_SHARD_FACTOR); |
---|
| 400 | local = true; |
---|
| 401 | } |
---|
| 402 | else { |
---|
| 403 | // 1 out of BIAS chances |
---|
| 404 | // Use all queues |
---|
| 405 | i = rhigh; |
---|
| 406 | local = false; |
---|
| 407 | } |
---|
| 408 | return [i, local]; |
---|
| 409 | } |
---|
| 410 | |
---|
[b808625] | 411 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool push_local) with (cltr->ready_queue) { |
---|
[431cd4f] | 412 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr); |
---|
[1b143de] | 413 | |
---|
[b808625] | 414 | const bool external = !push_local || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr); |
---|
[431cd4f] | 415 | /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count ); |
---|
[fd1f65e] | 416 | |
---|
[431cd4f] | 417 | bool local; |
---|
| 418 | int preferred = external ? -1 : kernelTLS().this_processor->rdq.id; |
---|
[52769ba] | 419 | |
---|
[431cd4f] | 420 | // Try to pick a lane and lock it |
---|
| 421 | unsigned i; |
---|
| 422 | do { |
---|
| 423 | // Pick the index of a lane |
---|
| 424 | unsigned r = __tls_rand_fwd(); |
---|
| 425 | [i, local] = idx_from_r(r, preferred); |
---|
[772411a] | 426 | |
---|
[431cd4f] | 427 | i %= __atomic_load_n( &lanes.count, __ATOMIC_RELAXED ); |
---|
| 428 | |
---|
| 429 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[d2fadeb] | 430 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED); |
---|
| 431 | else if(local) __tls_stats()->ready.push.local.attempt++; |
---|
| 432 | else __tls_stats()->ready.push.share.attempt++; |
---|
[431cd4f] | 433 | #endif |
---|
[b798713] | 434 | |
---|
[431cd4f] | 435 | // If we can't lock it retry |
---|
| 436 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) ); |
---|
| 437 | |
---|
| 438 | // Actually push it |
---|
| 439 | push(lanes.data[i], thrd); |
---|
| 440 | |
---|
[b808625] | 441 | // Unlock and return |
---|
| 442 | __atomic_unlock( &lanes.data[i].lock ); |
---|
[431cd4f] | 443 | |
---|
| 444 | // Mark the current index in the tls rng instance as having an item |
---|
| 445 | __tls_rand_advance_bck(); |
---|
| 446 | |
---|
| 447 | __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); |
---|
| 448 | |
---|
| 449 | // Update statistics |
---|
[b798713] | 450 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[d2fadeb] | 451 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED); |
---|
| 452 | else if(local) __tls_stats()->ready.push.local.success++; |
---|
| 453 | else __tls_stats()->ready.push.share.success++; |
---|
[b798713] | 454 | #endif |
---|
[431cd4f] | 455 | } |
---|
[b798713] | 456 | |
---|
[431cd4f] | 457 | // Pop from the ready queue from a given cluster |
---|
| 458 | __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) { |
---|
| 459 | /* paranoid */ verify( lanes.count > 0 ); |
---|
| 460 | /* paranoid */ verify( kernelTLS().this_processor ); |
---|
| 461 | /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count ); |
---|
[b798713] | 462 | |
---|
[431cd4f] | 463 | unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED ); |
---|
| 464 | int preferred = kernelTLS().this_processor->rdq.id; |
---|
[dca5802] | 465 | |
---|
| 466 | |
---|
[431cd4f] | 467 | // As long as the list is not empty, try finding a lane that isn't empty and pop from it |
---|
| 468 | for(25) { |
---|
| 469 | // Pick two lists at random |
---|
| 470 | unsigned ri = __tls_rand_bck(); |
---|
| 471 | unsigned rj = __tls_rand_bck(); |
---|
[c426b03] | 472 | |
---|
[431cd4f] | 473 | unsigned i, j; |
---|
| 474 | __attribute__((unused)) bool locali, localj; |
---|
| 475 | [i, locali] = idx_from_r(ri, preferred); |
---|
| 476 | [j, localj] = idx_from_r(rj, preferred); |
---|
[1b143de] | 477 | |
---|
[431cd4f] | 478 | i %= count; |
---|
| 479 | j %= count; |
---|
[9cc3a18] | 480 | |
---|
[431cd4f] | 481 | // try popping from the 2 picked lists |
---|
[d2fadeb] | 482 | struct $thread * thrd = try_pop(cltr, i, j __STATS(, *(locali || localj ? &__tls_stats()->ready.pop.local : &__tls_stats()->ready.pop.help))); |
---|
[431cd4f] | 483 | if(thrd) { |
---|
| 484 | return thrd; |
---|
| 485 | } |
---|
| 486 | } |
---|
[13c5e19] | 487 | |
---|
[431cd4f] | 488 | // All lanes where empty return 0p |
---|
| 489 | return 0p; |
---|
| 490 | } |
---|
[772411a] | 491 | |
---|
[fc59df78] | 492 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) { return pop_fast(cltr); } |
---|
| 493 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) { |
---|
[431cd4f] | 494 | return search(cltr); |
---|
| 495 | } |
---|
| 496 | #endif |
---|
| 497 | #if defined(USE_WORK_STEALING) |
---|
[b808625] | 498 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool push_local) with (cltr->ready_queue) { |
---|
[431cd4f] | 499 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr); |
---|
[772411a] | 500 | |
---|
[d3ba775] | 501 | // #define USE_PREFERRED |
---|
| 502 | #if !defined(USE_PREFERRED) |
---|
[b808625] | 503 | const bool external = !push_local || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr); |
---|
[431cd4f] | 504 | /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count ); |
---|
[d3ba775] | 505 | #else |
---|
| 506 | unsigned preferred = thrd->preferred; |
---|
[b808625] | 507 | const bool external = push_local || (!kernelTLS().this_processor) || preferred == -1u || thrd->curr_cluster != cltr; |
---|
[d3ba775] | 508 | /* paranoid */ verifyf(external || preferred < lanes.count, "Invalid preferred queue %u for %u lanes", preferred, lanes.count ); |
---|
[772411a] | 509 | |
---|
[d3ba775] | 510 | unsigned r = preferred % READYQ_SHARD_FACTOR; |
---|
| 511 | const unsigned start = preferred - r; |
---|
[2b96031] | 512 | #endif |
---|
[431cd4f] | 513 | |
---|
| 514 | // Try to pick a lane and lock it |
---|
| 515 | unsigned i; |
---|
| 516 | do { |
---|
[d2fadeb] | 517 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 518 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED); |
---|
| 519 | else __tls_stats()->ready.push.local.attempt++; |
---|
| 520 | #endif |
---|
| 521 | |
---|
[431cd4f] | 522 | if(unlikely(external)) { |
---|
| 523 | i = __tls_rand() % lanes.count; |
---|
| 524 | } |
---|
| 525 | else { |
---|
[d3ba775] | 526 | #if !defined(USE_PREFERRED) |
---|
[b808625] | 527 | processor * proc = kernelTLS().this_processor; |
---|
| 528 | unsigned r = proc->rdq.its++; |
---|
| 529 | i = proc->rdq.id + (r % READYQ_SHARD_FACTOR); |
---|
| 530 | #else |
---|
[d3ba775] | 531 | i = start + (r++ % READYQ_SHARD_FACTOR); |
---|
| 532 | #endif |
---|
| 533 | } |
---|
[431cd4f] | 534 | // If we can't lock it retry |
---|
| 535 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) ); |
---|
[13c5e19] | 536 | |
---|
[431cd4f] | 537 | // Actually push it |
---|
| 538 | push(lanes.data[i], thrd); |
---|
[13c5e19] | 539 | |
---|
[b808625] | 540 | // Unlock and return |
---|
| 541 | __atomic_unlock( &lanes.data[i].lock ); |
---|
[431cd4f] | 542 | |
---|
[d2fadeb] | 543 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 544 | if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED); |
---|
| 545 | else __tls_stats()->ready.push.local.success++; |
---|
| 546 | #endif |
---|
| 547 | |
---|
[431cd4f] | 548 | __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] | 549 | } |
---|
| 550 | |
---|
[431cd4f] | 551 | // Pop from the ready queue from a given cluster |
---|
| 552 | __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) { |
---|
| 553 | /* paranoid */ verify( lanes.count > 0 ); |
---|
| 554 | /* paranoid */ verify( kernelTLS().this_processor ); |
---|
| 555 | /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count ); |
---|
| 556 | |
---|
| 557 | processor * proc = kernelTLS().this_processor; |
---|
| 558 | |
---|
| 559 | if(proc->rdq.target == -1u) { |
---|
[1680072] | 560 | unsigned long long min = ts(lanes.data[proc->rdq.id]); |
---|
| 561 | for(int i = 0; i < READYQ_SHARD_FACTOR; i++) { |
---|
| 562 | unsigned long long tsc = ts(lanes.data[proc->rdq.id + i]); |
---|
| 563 | if(tsc < min) min = tsc; |
---|
| 564 | } |
---|
| 565 | proc->rdq.cutoff = min; |
---|
[f55d54d] | 566 | proc->rdq.target = __tls_rand() % lanes.count; |
---|
[431cd4f] | 567 | } |
---|
[341aa39] | 568 | else { |
---|
| 569 | unsigned target = proc->rdq.target; |
---|
[431cd4f] | 570 | proc->rdq.target = -1u; |
---|
[9cac0da] | 571 | const unsigned long long bias = 0; //2_500_000_000; |
---|
| 572 | const unsigned long long cutoff = proc->rdq.cutoff > bias ? proc->rdq.cutoff - bias : proc->rdq.cutoff; |
---|
| 573 | if(lanes.tscs[target].tv < cutoff && ts(lanes.data[target]) < cutoff) { |
---|
[341aa39] | 574 | $thread * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help)); |
---|
| 575 | if(t) return t; |
---|
| 576 | } |
---|
[431cd4f] | 577 | } |
---|
[13c5e19] | 578 | |
---|
[431cd4f] | 579 | for(READYQ_SHARD_FACTOR) { |
---|
[f55d54d] | 580 | unsigned i = proc->rdq.id + (proc->rdq.itr++ % READYQ_SHARD_FACTOR); |
---|
[d2fadeb] | 581 | if($thread * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t; |
---|
[431cd4f] | 582 | } |
---|
| 583 | return 0p; |
---|
[1eb239e4] | 584 | } |
---|
| 585 | |
---|
[431cd4f] | 586 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) with (cltr->ready_queue) { |
---|
[fc59df78] | 587 | unsigned i = __tls_rand() % lanes.count; |
---|
| 588 | return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal)); |
---|
| 589 | } |
---|
[431cd4f] | 590 | |
---|
[fc59df78] | 591 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) with (cltr->ready_queue) { |
---|
[431cd4f] | 592 | return search(cltr); |
---|
| 593 | } |
---|
| 594 | #endif |
---|
[1eb239e4] | 595 | |
---|
[9cc3a18] | 596 | //======================================================================= |
---|
| 597 | // Various Ready Queue utilities |
---|
| 598 | //======================================================================= |
---|
| 599 | // these function work the same or almost the same |
---|
| 600 | // whether they are using work-stealing or relaxed fifo scheduling |
---|
[1eb239e4] | 601 | |
---|
[9cc3a18] | 602 | //----------------------------------------------------------------------- |
---|
| 603 | // try to pop from a lane given by index w |
---|
[d2fadeb] | 604 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) { |
---|
| 605 | __STATS( stats.attempt++; ) |
---|
| 606 | |
---|
[dca5802] | 607 | // Get relevant elements locally |
---|
| 608 | __intrusive_lane_t & lane = lanes.data[w]; |
---|
| 609 | |
---|
[b798713] | 610 | // If list looks empty retry |
---|
[d2fadeb] | 611 | if( is_empty(lane) ) { |
---|
| 612 | return 0p; |
---|
| 613 | } |
---|
[b798713] | 614 | |
---|
| 615 | // If we can't get the lock retry |
---|
[d2fadeb] | 616 | if( !__atomic_try_acquire(&lane.lock) ) { |
---|
| 617 | return 0p; |
---|
| 618 | } |
---|
[b798713] | 619 | |
---|
| 620 | // If list is empty, unlock and retry |
---|
[dca5802] | 621 | if( is_empty(lane) ) { |
---|
| 622 | __atomic_unlock(&lane.lock); |
---|
[b798713] | 623 | return 0p; |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | // Actually pop the list |
---|
[504a7dc] | 627 | struct $thread * thrd; |
---|
[f302d80] | 628 | unsigned long long tsv; |
---|
| 629 | [thrd, tsv] = pop(lane); |
---|
[b798713] | 630 | |
---|
[dca5802] | 631 | /* paranoid */ verify(thrd); |
---|
[78ea291] | 632 | /* paranoid */ verify(tsv); |
---|
[dca5802] | 633 | /* paranoid */ verify(lane.lock); |
---|
[b798713] | 634 | |
---|
| 635 | // Unlock and return |
---|
[dca5802] | 636 | __atomic_unlock(&lane.lock); |
---|
[b798713] | 637 | |
---|
[dca5802] | 638 | // Update statistics |
---|
[d2fadeb] | 639 | __STATS( stats.success++; ) |
---|
[b798713] | 640 | |
---|
[431cd4f] | 641 | #if defined(USE_WORK_STEALING) |
---|
[f302d80] | 642 | lanes.tscs[w].tv = tsv; |
---|
[9cc3a18] | 643 | #endif |
---|
[d72c074] | 644 | |
---|
[d3ba775] | 645 | thrd->preferred = w; |
---|
| 646 | |
---|
[dca5802] | 647 | // return the popped thread |
---|
[b798713] | 648 | return thrd; |
---|
| 649 | } |
---|
[04b5cef] | 650 | |
---|
[9cc3a18] | 651 | //----------------------------------------------------------------------- |
---|
| 652 | // try to pop from any lanes making sure you don't miss any threads push |
---|
| 653 | // before the start of the function |
---|
[431cd4f] | 654 | static inline struct $thread * search(struct cluster * cltr) with (cltr->ready_queue) { |
---|
[9cc3a18] | 655 | /* paranoid */ verify( lanes.count > 0 ); |
---|
| 656 | unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED ); |
---|
| 657 | unsigned offset = __tls_rand(); |
---|
| 658 | for(i; count) { |
---|
| 659 | unsigned idx = (offset + i) % count; |
---|
[d2fadeb] | 660 | struct $thread * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search)); |
---|
[9cc3a18] | 661 | if(thrd) { |
---|
| 662 | return thrd; |
---|
| 663 | } |
---|
[13c5e19] | 664 | } |
---|
[9cc3a18] | 665 | |
---|
| 666 | // All lanes where empty return 0p |
---|
| 667 | return 0p; |
---|
[b798713] | 668 | } |
---|
| 669 | |
---|
| 670 | //----------------------------------------------------------------------- |
---|
[9cc3a18] | 671 | // Check that all the intrusive queues in the data structure are still consistent |
---|
[b798713] | 672 | static void check( __ready_queue_t & q ) with (q) { |
---|
[d3ba775] | 673 | #if defined(__CFA_WITH_VERIFY__) |
---|
[b798713] | 674 | { |
---|
[dca5802] | 675 | for( idx ; lanes.count ) { |
---|
| 676 | __intrusive_lane_t & sl = lanes.data[idx]; |
---|
| 677 | assert(!lanes.data[idx].lock); |
---|
[b798713] | 678 | |
---|
[2b96031] | 679 | if(is_empty(sl)) { |
---|
| 680 | assert( sl.anchor.next == 0p ); |
---|
| 681 | assert( sl.anchor.ts == 0 ); |
---|
| 682 | assert( mock_head(sl) == sl.prev ); |
---|
| 683 | } else { |
---|
| 684 | assert( sl.anchor.next != 0p ); |
---|
| 685 | assert( sl.anchor.ts != 0 ); |
---|
| 686 | assert( mock_head(sl) != sl.prev ); |
---|
| 687 | } |
---|
[b798713] | 688 | } |
---|
| 689 | } |
---|
| 690 | #endif |
---|
| 691 | } |
---|
| 692 | |
---|
[9cc3a18] | 693 | //----------------------------------------------------------------------- |
---|
| 694 | // Given 2 indexes, pick the list with the oldest push an try to pop from it |
---|
[d2fadeb] | 695 | static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) { |
---|
[9cc3a18] | 696 | // Pick the bet list |
---|
| 697 | int w = i; |
---|
| 698 | if( __builtin_expect(!is_empty(lanes.data[j]), true) ) { |
---|
| 699 | w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j; |
---|
| 700 | } |
---|
| 701 | |
---|
[d2fadeb] | 702 | return try_pop(cltr, w __STATS(, stats)); |
---|
[9cc3a18] | 703 | } |
---|
| 704 | |
---|
[b798713] | 705 | // Call this function of the intrusive list was moved using memcpy |
---|
[dca5802] | 706 | // fixes the list so that the pointers back to anchors aren't left dangling |
---|
| 707 | static inline void fix(__intrusive_lane_t & ll) { |
---|
[2b96031] | 708 | if(is_empty(ll)) { |
---|
| 709 | verify(ll.anchor.next == 0p); |
---|
| 710 | ll.prev = mock_head(ll); |
---|
| 711 | } |
---|
[b798713] | 712 | } |
---|
| 713 | |
---|
[69914cbc] | 714 | static void assign_list(unsigned & value, dlist(processor) & list, unsigned count) { |
---|
[a017ee7] | 715 | processor * it = &list`first; |
---|
| 716 | for(unsigned i = 0; i < count; i++) { |
---|
| 717 | /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count); |
---|
[431cd4f] | 718 | it->rdq.id = value; |
---|
| 719 | it->rdq.target = -1u; |
---|
[9cc3a18] | 720 | value += READYQ_SHARD_FACTOR; |
---|
[a017ee7] | 721 | it = &(*it)`next; |
---|
| 722 | } |
---|
| 723 | } |
---|
| 724 | |
---|
[9cc3a18] | 725 | static void reassign_cltr_id(struct cluster * cltr) { |
---|
[a017ee7] | 726 | unsigned preferred = 0; |
---|
[9cc3a18] | 727 | assign_list(preferred, cltr->procs.actives, cltr->procs.total - cltr->procs.idle); |
---|
| 728 | assign_list(preferred, cltr->procs.idles , cltr->procs.idle ); |
---|
[a017ee7] | 729 | } |
---|
| 730 | |
---|
[431cd4f] | 731 | static void fix_times( struct cluster * cltr ) with( cltr->ready_queue ) { |
---|
| 732 | #if defined(USE_WORK_STEALING) |
---|
| 733 | lanes.tscs = alloc(lanes.count, lanes.tscs`realloc); |
---|
| 734 | for(i; lanes.count) { |
---|
[9cac0da] | 735 | unsigned long long tsc = ts(lanes.data[i]); |
---|
| 736 | lanes.tscs[i].tv = tsc != 0 ? tsc : rdtscl(); |
---|
[431cd4f] | 737 | } |
---|
| 738 | #endif |
---|
| 739 | } |
---|
| 740 | |
---|
[12daa43] | 741 | #if defined(USE_CPU_WORK_STEALING) |
---|
| 742 | // ready_queue size is fixed in this case |
---|
| 743 | void ready_queue_grow(struct cluster * cltr) {} |
---|
| 744 | void ready_queue_shrink(struct cluster * cltr) {} |
---|
| 745 | #else |
---|
| 746 | // Grow the ready queue |
---|
| 747 | void ready_queue_grow(struct cluster * cltr) { |
---|
| 748 | size_t ncount; |
---|
| 749 | int target = cltr->procs.total; |
---|
| 750 | |
---|
| 751 | /* paranoid */ verify( ready_mutate_islocked() ); |
---|
| 752 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n"); |
---|
| 753 | |
---|
| 754 | // Make sure that everything is consistent |
---|
| 755 | /* paranoid */ check( cltr->ready_queue ); |
---|
| 756 | |
---|
| 757 | // grow the ready queue |
---|
| 758 | with( cltr->ready_queue ) { |
---|
| 759 | // Find new count |
---|
| 760 | // Make sure we always have atleast 1 list |
---|
| 761 | if(target >= 2) { |
---|
| 762 | ncount = target * READYQ_SHARD_FACTOR; |
---|
| 763 | } else { |
---|
| 764 | ncount = SEQUENTIAL_SHARD; |
---|
| 765 | } |
---|
[b798713] | 766 | |
---|
[12daa43] | 767 | // Allocate new array (uses realloc and memcpies the data) |
---|
| 768 | lanes.data = alloc( ncount, lanes.data`realloc ); |
---|
[b798713] | 769 | |
---|
[12daa43] | 770 | // Fix the moved data |
---|
| 771 | for( idx; (size_t)lanes.count ) { |
---|
| 772 | fix(lanes.data[idx]); |
---|
| 773 | } |
---|
[b798713] | 774 | |
---|
[12daa43] | 775 | // Construct new data |
---|
| 776 | for( idx; (size_t)lanes.count ~ ncount) { |
---|
| 777 | (lanes.data[idx]){}; |
---|
| 778 | } |
---|
[b798713] | 779 | |
---|
[12daa43] | 780 | // Update original |
---|
| 781 | lanes.count = ncount; |
---|
| 782 | } |
---|
[b798713] | 783 | |
---|
[12daa43] | 784 | fix_times(cltr); |
---|
[9cc3a18] | 785 | |
---|
[12daa43] | 786 | reassign_cltr_id(cltr); |
---|
[a017ee7] | 787 | |
---|
[12daa43] | 788 | // Make sure that everything is consistent |
---|
| 789 | /* paranoid */ check( cltr->ready_queue ); |
---|
[dca5802] | 790 | |
---|
[12daa43] | 791 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n"); |
---|
[dca5802] | 792 | |
---|
[12daa43] | 793 | /* paranoid */ verify( ready_mutate_islocked() ); |
---|
| 794 | } |
---|
[b798713] | 795 | |
---|
[12daa43] | 796 | // Shrink the ready queue |
---|
| 797 | void ready_queue_shrink(struct cluster * cltr) { |
---|
| 798 | /* paranoid */ verify( ready_mutate_islocked() ); |
---|
| 799 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n"); |
---|
[dca5802] | 800 | |
---|
[12daa43] | 801 | // Make sure that everything is consistent |
---|
| 802 | /* paranoid */ check( cltr->ready_queue ); |
---|
[dca5802] | 803 | |
---|
[12daa43] | 804 | int target = cltr->procs.total; |
---|
[a017ee7] | 805 | |
---|
[12daa43] | 806 | with( cltr->ready_queue ) { |
---|
| 807 | // Remember old count |
---|
| 808 | size_t ocount = lanes.count; |
---|
[b798713] | 809 | |
---|
[12daa43] | 810 | // Find new count |
---|
| 811 | // Make sure we always have atleast 1 list |
---|
| 812 | lanes.count = target >= 2 ? target * READYQ_SHARD_FACTOR: SEQUENTIAL_SHARD; |
---|
| 813 | /* paranoid */ verify( ocount >= lanes.count ); |
---|
| 814 | /* paranoid */ verify( lanes.count == target * READYQ_SHARD_FACTOR || target < 2 ); |
---|
[dca5802] | 815 | |
---|
[12daa43] | 816 | // for printing count the number of displaced threads |
---|
| 817 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__) |
---|
| 818 | __attribute__((unused)) size_t displaced = 0; |
---|
| 819 | #endif |
---|
[b798713] | 820 | |
---|
[12daa43] | 821 | // redistribute old data |
---|
| 822 | for( idx; (size_t)lanes.count ~ ocount) { |
---|
| 823 | // Lock is not strictly needed but makes checking invariants much easier |
---|
| 824 | __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock); |
---|
| 825 | verify(locked); |
---|
[dca5802] | 826 | |
---|
[12daa43] | 827 | // As long as we can pop from this lane to push the threads somewhere else in the queue |
---|
| 828 | while(!is_empty(lanes.data[idx])) { |
---|
| 829 | struct $thread * thrd; |
---|
| 830 | unsigned long long _; |
---|
| 831 | [thrd, _] = pop(lanes.data[idx]); |
---|
[dca5802] | 832 | |
---|
[12daa43] | 833 | push(cltr, thrd, true); |
---|
[dca5802] | 834 | |
---|
[12daa43] | 835 | // for printing count the number of displaced threads |
---|
| 836 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__) |
---|
| 837 | displaced++; |
---|
| 838 | #endif |
---|
| 839 | } |
---|
[b798713] | 840 | |
---|
[12daa43] | 841 | // Unlock the lane |
---|
| 842 | __atomic_unlock(&lanes.data[idx].lock); |
---|
[b798713] | 843 | |
---|
[12daa43] | 844 | // TODO print the queue statistics here |
---|
[b798713] | 845 | |
---|
[12daa43] | 846 | ^(lanes.data[idx]){}; |
---|
| 847 | } |
---|
[b798713] | 848 | |
---|
[12daa43] | 849 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced); |
---|
[c84b4be] | 850 | |
---|
[12daa43] | 851 | // Allocate new array (uses realloc and memcpies the data) |
---|
| 852 | lanes.data = alloc( lanes.count, lanes.data`realloc ); |
---|
[b798713] | 853 | |
---|
[12daa43] | 854 | // Fix the moved data |
---|
| 855 | for( idx; (size_t)lanes.count ) { |
---|
| 856 | fix(lanes.data[idx]); |
---|
| 857 | } |
---|
[b798713] | 858 | } |
---|
| 859 | |
---|
[12daa43] | 860 | fix_times(cltr); |
---|
[9cc3a18] | 861 | |
---|
[12daa43] | 862 | reassign_cltr_id(cltr); |
---|
[a017ee7] | 863 | |
---|
[12daa43] | 864 | // Make sure that everything is consistent |
---|
| 865 | /* paranoid */ check( cltr->ready_queue ); |
---|
[dca5802] | 866 | |
---|
[12daa43] | 867 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n"); |
---|
| 868 | /* paranoid */ verify( ready_mutate_islocked() ); |
---|
| 869 | } |
---|
| 870 | #endif |
---|
[8cd5434] | 871 | |
---|
| 872 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 873 | unsigned cnt(const __ready_queue_t & this, unsigned idx) { |
---|
| 874 | /* paranoid */ verify(this.lanes.count > idx); |
---|
| 875 | return this.lanes.data[idx].cnt; |
---|
| 876 | } |
---|
| 877 | #endif |
---|
[0ee224b] | 878 | |
---|
| 879 | |
---|
| 880 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
| 881 | // No definition needed |
---|
| 882 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
| 883 | |
---|
| 884 | #if defined( __x86_64 ) || defined( __i386 ) |
---|
| 885 | #define RSEQ_SIG 0x53053053 |
---|
| 886 | #elif defined( __ARM_ARCH ) |
---|
| 887 | #ifdef __ARMEB__ |
---|
| 888 | #define RSEQ_SIG 0xf3def5e7 /* udf #24035 ; 0x5de3 (ARMv6+) */ |
---|
| 889 | #else |
---|
| 890 | #define RSEQ_SIG 0xe7f5def3 /* udf #24035 ; 0x5de3 */ |
---|
| 891 | #endif |
---|
| 892 | #endif |
---|
| 893 | |
---|
| 894 | extern void __disable_interrupts_hard(); |
---|
| 895 | extern void __enable_interrupts_hard(); |
---|
| 896 | |
---|
| 897 | void __kernel_raw_rseq_register (void) { |
---|
| 898 | /* paranoid */ verify( __cfaabi_rseq.cpu_id == RSEQ_CPU_ID_UNINITIALIZED ); |
---|
| 899 | |
---|
| 900 | // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, (sigset_t *)0p, _NSIG / 8); |
---|
| 901 | int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, RSEQ_SIG); |
---|
| 902 | if(ret != 0) { |
---|
| 903 | int e = errno; |
---|
| 904 | switch(e) { |
---|
| 905 | case EINVAL: abort("KERNEL ERROR: rseq register invalid argument"); |
---|
| 906 | case ENOSYS: abort("KERNEL ERROR: rseq register no supported"); |
---|
| 907 | case EFAULT: abort("KERNEL ERROR: rseq register with invalid argument"); |
---|
| 908 | case EBUSY : abort("KERNEL ERROR: rseq register already registered"); |
---|
| 909 | case EPERM : abort("KERNEL ERROR: rseq register sig argument on unregistration does not match the signature received on registration"); |
---|
| 910 | default: abort("KERNEL ERROR: rseq register unexpected return %d", e); |
---|
| 911 | } |
---|
| 912 | } |
---|
| 913 | } |
---|
| 914 | |
---|
| 915 | void __kernel_raw_rseq_unregister(void) { |
---|
| 916 | /* paranoid */ verify( __cfaabi_rseq.cpu_id >= 0 ); |
---|
| 917 | |
---|
| 918 | // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, (sigset_t *)0p, _NSIG / 8); |
---|
| 919 | int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, RSEQ_SIG); |
---|
| 920 | if(ret != 0) { |
---|
| 921 | int e = errno; |
---|
| 922 | switch(e) { |
---|
| 923 | case EINVAL: abort("KERNEL ERROR: rseq unregister invalid argument"); |
---|
| 924 | case ENOSYS: abort("KERNEL ERROR: rseq unregister no supported"); |
---|
| 925 | case EFAULT: abort("KERNEL ERROR: rseq unregister with invalid argument"); |
---|
| 926 | case EBUSY : abort("KERNEL ERROR: rseq unregister already registered"); |
---|
| 927 | case EPERM : abort("KERNEL ERROR: rseq unregister sig argument on unregistration does not match the signature received on registration"); |
---|
| 928 | default: abort("KERNEL ERROR: rseq unregisteunexpected return %d", e); |
---|
| 929 | } |
---|
| 930 | } |
---|
| 931 | } |
---|
| 932 | #else |
---|
| 933 | // No definition needed |
---|
| 934 | #endif |
---|