[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_AWARE_STEALING
|
---|
[9cc3a18] | 23 |
|
---|
[7768b8d] | 24 | #include "bits/defs.hfa"
|
---|
[12daa43] | 25 | #include "device/cpu.hfa"
|
---|
[708ae38] | 26 | #include "kernel/cluster.hfa"
|
---|
| 27 | #include "kernel/private.hfa"
|
---|
[7768b8d] | 28 |
|
---|
[a2a4566] | 29 | #include "limits.hfa"
|
---|
[7768b8d] | 30 |
|
---|
[c42b8a1] | 31 | // #include <errno.h>
|
---|
| 32 | // #include <unistd.h>
|
---|
[0ee224b] | 33 |
|
---|
[13c5e19] | 34 | #include "ready_subqueue.hfa"
|
---|
| 35 |
|
---|
[7768b8d] | 36 | static const size_t cache_line_size = 64;
|
---|
| 37 |
|
---|
[d2fadeb] | 38 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 39 | #define __STATS(...) __VA_ARGS__
|
---|
| 40 | #else
|
---|
| 41 | #define __STATS(...)
|
---|
| 42 | #endif
|
---|
| 43 |
|
---|
[e84ab3d] | 44 | static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats));
|
---|
| 45 | static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats));
|
---|
| 46 | static inline struct thread$ * search(struct cluster * cltr);
|
---|
[9cc3a18] | 47 |
|
---|
[7768b8d] | 48 | //=======================================================================
|
---|
[9cc3a18] | 49 | // Cforall Ready Queue used for scheduling
|
---|
[b798713] | 50 | //=======================================================================
|
---|
[884f3f67] | 51 | // void ?{}(__ready_queue_t & this) with (this) {
|
---|
| 52 | // lanes.data = 0p;
|
---|
| 53 | // lanes.tscs = 0p;
|
---|
| 54 | // lanes.caches = 0p;
|
---|
| 55 | // lanes.count = 0;
|
---|
| 56 | // }
|
---|
| 57 |
|
---|
| 58 | // void ^?{}(__ready_queue_t & this) with (this) {
|
---|
| 59 | // free(lanes.data);
|
---|
| 60 | // free(lanes.tscs);
|
---|
| 61 | // free(lanes.caches);
|
---|
| 62 | // }
|
---|
[dca5802] | 63 |
|
---|
[64a7146] | 64 | //-----------------------------------------------------------------------
|
---|
[884f3f67] | 65 | __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->sched) {
|
---|
[3c4bf05] | 66 | processor * const proc = kernelTLS().this_processor;
|
---|
| 67 | const bool external = (!proc) || (cltr != proc->cltr);
|
---|
| 68 | const bool remote = hint == UNPARK_REMOTE;
|
---|
[884f3f67] | 69 | const size_t lanes_count = readyQ.count;
|
---|
| 70 |
|
---|
| 71 | /* paranoid */ verify( __shard_factor.readyq > 0 );
|
---|
| 72 | /* paranoid */ verify( lanes_count > 0 );
|
---|
[3c4bf05] | 73 |
|
---|
| 74 | unsigned i;
|
---|
| 75 | if( external || remote ) {
|
---|
| 76 | // Figure out where thread was last time and make sure it's valid
|
---|
| 77 | /* paranoid */ verify(thrd->preferred >= 0);
|
---|
[884f3f67] | 78 | unsigned start = thrd->preferred * __shard_factor.readyq;
|
---|
| 79 | if(start < lanes_count) {
|
---|
[a2a4566] | 80 | do {
|
---|
[3c4bf05] | 81 | unsigned r = __tls_rand();
|
---|
[884f3f67] | 82 | i = start + (r % __shard_factor.readyq);
|
---|
| 83 | /* paranoid */ verify( i < lanes_count );
|
---|
[a2a4566] | 84 | // If we can't lock it retry
|
---|
[884f3f67] | 85 | } while( !__atomic_try_acquire( &readyQ.data[i].lock ) );
|
---|
[3c4bf05] | 86 | } else {
|
---|
| 87 | do {
|
---|
[884f3f67] | 88 | i = __tls_rand() % lanes_count;
|
---|
| 89 | } while( !__atomic_try_acquire( &readyQ.data[i].lock ) );
|
---|
[a2a4566] | 90 | }
|
---|
[3c4bf05] | 91 | } else {
|
---|
[12daa43] | 92 | do {
|
---|
[3c4bf05] | 93 | unsigned r = proc->rdq.its++;
|
---|
[884f3f67] | 94 | i = proc->rdq.id + (r % __shard_factor.readyq);
|
---|
| 95 | /* paranoid */ verify( i < lanes_count );
|
---|
[12daa43] | 96 | // If we can't lock it retry
|
---|
[884f3f67] | 97 | } while( !__atomic_try_acquire( &readyQ.data[i].lock ) );
|
---|
[12daa43] | 98 | }
|
---|
| 99 |
|
---|
[3c4bf05] | 100 | // Actually push it
|
---|
[884f3f67] | 101 | push(readyQ.data[i], thrd);
|
---|
[12daa43] | 102 |
|
---|
[3c4bf05] | 103 | // Unlock and return
|
---|
[884f3f67] | 104 | __atomic_unlock( &readyQ.data[i].lock );
|
---|
[12daa43] | 105 |
|
---|
[3c4bf05] | 106 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 107 | if(unlikely(external || remote)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
|
---|
| 108 | else __tls_stats()->ready.push.local.success++;
|
---|
| 109 | #endif
|
---|
| 110 | }
|
---|
[12daa43] | 111 |
|
---|
[884f3f67] | 112 | __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr) with (cltr->sched) {
|
---|
| 113 | const size_t lanes_count = readyQ.count;
|
---|
[b798713] | 114 |
|
---|
[884f3f67] | 115 | /* paranoid */ verify( __shard_factor.readyq > 0 );
|
---|
| 116 | /* paranoid */ verify( lanes_count > 0 );
|
---|
[3c4bf05] | 117 | /* paranoid */ verify( kernelTLS().this_processor );
|
---|
[884f3f67] | 118 | /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes_count );
|
---|
[3c4bf05] | 119 |
|
---|
| 120 | processor * const proc = kernelTLS().this_processor;
|
---|
| 121 | unsigned this = proc->rdq.id;
|
---|
[884f3f67] | 122 | /* paranoid */ verify( this < lanes_count );
|
---|
[3c4bf05] | 123 | __cfadbg_print_safe(ready_queue, "Kernel : pop from %u\n", this);
|
---|
| 124 |
|
---|
[708ae38] | 125 | // Figure out the current cache is
|
---|
| 126 | const unsigned this_cache = cache_id(cltr, this / __shard_factor.readyq);
|
---|
[3c4bf05] | 127 | const unsigned long long ctsc = rdtscl();
|
---|
| 128 |
|
---|
| 129 | if(proc->rdq.target == MAX) {
|
---|
| 130 | uint64_t chaos = __tls_rand();
|
---|
| 131 | unsigned ext = chaos & 0xff;
|
---|
[884f3f67] | 132 | unsigned other = (chaos >> 8) % (lanes_count);
|
---|
[3c4bf05] | 133 |
|
---|
[884f3f67] | 134 | if(ext < 3 || __atomic_load_n(&caches[other / __shard_factor.readyq].id, __ATOMIC_RELAXED) == this_cache) {
|
---|
[3c4bf05] | 135 | proc->rdq.target = other;
|
---|
[431cd4f] | 136 | }
|
---|
| 137 | }
|
---|
[3c4bf05] | 138 | else {
|
---|
| 139 | const unsigned target = proc->rdq.target;
|
---|
[884f3f67] | 140 | __cfadbg_print_safe(ready_queue, "Kernel : %u considering helping %u, tcsc %llu\n", this, target, readyQ.tscs[target].tv);
|
---|
| 141 | /* paranoid */ verify( readyQ.tscs[target].tv != MAX );
|
---|
| 142 | if(target < lanes_count) {
|
---|
| 143 | const unsigned long long cutoff = calc_cutoff(ctsc, proc, lanes_count, cltr->sched.readyQ.data, cltr->sched.readyQ.tscs, __shard_factor.readyq);
|
---|
| 144 | const unsigned long long age = moving_average(ctsc, readyQ.tscs[target].tv, readyQ.tscs[target].ma);
|
---|
[3c4bf05] | 145 | __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");
|
---|
| 146 | if(age > cutoff) {
|
---|
[e84ab3d] | 147 | thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
|
---|
[341aa39] | 148 | if(t) return t;
|
---|
| 149 | }
|
---|
[431cd4f] | 150 | }
|
---|
[3c4bf05] | 151 | proc->rdq.target = MAX;
|
---|
[1eb239e4] | 152 | }
|
---|
| 153 |
|
---|
[884f3f67] | 154 | for(__shard_factor.readyq) {
|
---|
| 155 | unsigned i = this + (proc->rdq.itr++ % __shard_factor.readyq);
|
---|
[3c4bf05] | 156 | if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
|
---|
[fc59df78] | 157 | }
|
---|
[431cd4f] | 158 |
|
---|
[3c4bf05] | 159 | // All lanes where empty return 0p
|
---|
| 160 | return 0p;
|
---|
| 161 |
|
---|
| 162 | }
|
---|
[884f3f67] | 163 | __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) {
|
---|
| 164 | unsigned i = __tls_rand() % (cltr->sched.readyQ.count);
|
---|
[3c4bf05] | 165 | return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
|
---|
| 166 | }
|
---|
| 167 | __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
|
---|
| 168 | return search(cltr);
|
---|
| 169 | }
|
---|
[1eb239e4] | 170 |
|
---|
[9cc3a18] | 171 | //=======================================================================
|
---|
| 172 | // Various Ready Queue utilities
|
---|
| 173 | //=======================================================================
|
---|
| 174 | // these function work the same or almost the same
|
---|
| 175 | // whether they are using work-stealing or relaxed fifo scheduling
|
---|
[1eb239e4] | 176 |
|
---|
[9cc3a18] | 177 | //-----------------------------------------------------------------------
|
---|
| 178 | // try to pop from a lane given by index w
|
---|
[884f3f67] | 179 | static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->sched) {
|
---|
[bfb9bf5] | 180 | /* paranoid */ verify( w < readyQ.count );
|
---|
[d2fadeb] | 181 | __STATS( stats.attempt++; )
|
---|
| 182 |
|
---|
[dca5802] | 183 | // Get relevant elements locally
|
---|
[884f3f67] | 184 | __intrusive_lane_t & lane = readyQ.data[w];
|
---|
[dca5802] | 185 |
|
---|
[b798713] | 186 | // If list looks empty retry
|
---|
[d2fadeb] | 187 | if( is_empty(lane) ) {
|
---|
| 188 | return 0p;
|
---|
| 189 | }
|
---|
[b798713] | 190 |
|
---|
| 191 | // If we can't get the lock retry
|
---|
[d2fadeb] | 192 | if( !__atomic_try_acquire(&lane.lock) ) {
|
---|
| 193 | return 0p;
|
---|
| 194 | }
|
---|
[b798713] | 195 |
|
---|
| 196 | // If list is empty, unlock and retry
|
---|
[dca5802] | 197 | if( is_empty(lane) ) {
|
---|
| 198 | __atomic_unlock(&lane.lock);
|
---|
[b798713] | 199 | return 0p;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | // Actually pop the list
|
---|
[e84ab3d] | 203 | struct thread$ * thrd;
|
---|
[3c4bf05] | 204 | unsigned long long tsc_before = ts(lane);
|
---|
[f302d80] | 205 | unsigned long long tsv;
|
---|
| 206 | [thrd, tsv] = pop(lane);
|
---|
[b798713] | 207 |
|
---|
[dca5802] | 208 | /* paranoid */ verify(thrd);
|
---|
[78ea291] | 209 | /* paranoid */ verify(tsv);
|
---|
[dca5802] | 210 | /* paranoid */ verify(lane.lock);
|
---|
[b798713] | 211 |
|
---|
| 212 | // Unlock and return
|
---|
[dca5802] | 213 | __atomic_unlock(&lane.lock);
|
---|
[b798713] | 214 |
|
---|
[dca5802] | 215 | // Update statistics
|
---|
[d2fadeb] | 216 | __STATS( stats.success++; )
|
---|
[b798713] | 217 |
|
---|
[3c4bf05] | 218 | if (tsv != MAX) {
|
---|
| 219 | unsigned long long now = rdtscl();
|
---|
[884f3f67] | 220 | unsigned long long pma = __atomic_load_n(&readyQ.tscs[w].ma, __ATOMIC_RELAXED);
|
---|
| 221 | __atomic_store_n(&readyQ.tscs[w].tv, tsv, __ATOMIC_RELAXED);
|
---|
| 222 | __atomic_store_n(&readyQ.tscs[w].ma, moving_average(now, tsc_before, pma), __ATOMIC_RELAXED);
|
---|
[3c4bf05] | 223 | }
|
---|
[d72c074] | 224 |
|
---|
[884f3f67] | 225 | thrd->preferred = w / __shard_factor.readyq;
|
---|
[d3ba775] | 226 |
|
---|
[dca5802] | 227 | // return the popped thread
|
---|
[b798713] | 228 | return thrd;
|
---|
| 229 | }
|
---|
[04b5cef] | 230 |
|
---|
[9cc3a18] | 231 | //-----------------------------------------------------------------------
|
---|
| 232 | // try to pop from any lanes making sure you don't miss any threads push
|
---|
| 233 | // before the start of the function
|
---|
[884f3f67] | 234 | static inline struct thread$ * search(struct cluster * cltr) {
|
---|
| 235 | const size_t lanes_count = cltr->sched.readyQ.count;
|
---|
| 236 | /* paranoid */ verify( lanes_count > 0 );
|
---|
| 237 | unsigned count = __atomic_load_n( &lanes_count, __ATOMIC_RELAXED );
|
---|
[9cc3a18] | 238 | unsigned offset = __tls_rand();
|
---|
| 239 | for(i; count) {
|
---|
| 240 | unsigned idx = (offset + i) % count;
|
---|
[e84ab3d] | 241 | struct thread$ * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search));
|
---|
[9cc3a18] | 242 | if(thrd) {
|
---|
| 243 | return thrd;
|
---|
| 244 | }
|
---|
[13c5e19] | 245 | }
|
---|
[9cc3a18] | 246 |
|
---|
| 247 | // All lanes where empty return 0p
|
---|
| 248 | return 0p;
|
---|
[b798713] | 249 | }
|
---|
| 250 |
|
---|
[24e321c] | 251 | //-----------------------------------------------------------------------
|
---|
| 252 | // get preferred ready for new thread
|
---|
| 253 | unsigned ready_queue_new_preferred() {
|
---|
[3c4bf05] | 254 | unsigned pref = MAX;
|
---|
[24e321c] | 255 | if(struct thread$ * thrd = publicTLS_get( this_thread )) {
|
---|
| 256 | pref = thrd->preferred;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return pref;
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[9cc3a18] | 262 | //-----------------------------------------------------------------------
|
---|
| 263 | // Given 2 indexes, pick the list with the oldest push an try to pop from it
|
---|
[884f3f67] | 264 | static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->sched) {
|
---|
[9cc3a18] | 265 | // Pick the bet list
|
---|
| 266 | int w = i;
|
---|
[884f3f67] | 267 | if( __builtin_expect(!is_empty(readyQ.data[j]), true) ) {
|
---|
| 268 | w = (ts(readyQ.data[i]) < ts(readyQ.data[j])) ? i : j;
|
---|
[9cc3a18] | 269 | }
|
---|
| 270 |
|
---|
[d2fadeb] | 271 | return try_pop(cltr, w __STATS(, stats));
|
---|
[9cc3a18] | 272 | }
|
---|