source: libcfa/src/concurrency/ready_queue.cfa@ 33b7d49

ADT ast-experimental enum pthread-emulation qualifiedEnum
Last change on this file since 33b7d49 was bfb9bf5, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fixed some warnings

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