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