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