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