source: libcfa/src/concurrency/ready_queue.cfa @ 5f9c42b

ADTast-experimental
Last change on this file since 5f9c42b was 5f9c42b, checked in by Thierry Delisle <tdelisle@…>, 18 months ago

Added strict flag to moving average to keep track of which timestamps need to be correct

  • Property mode set to 100644
File size: 8.5 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"
[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]34static 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]42static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats));
43static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats));
44static 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) {
[1756e08]64        struct processor * const proc = kernelTLS().this_processor;
[3c4bf05]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
[2af1943]83                        } while( !__atomic_try_acquire( &readyQ.data[i].l.lock ) );
[3c4bf05]84                } else {
85                        do {
[884f3f67]86                                i = __tls_rand() % lanes_count;
[2af1943]87                        } while( !__atomic_try_acquire( &readyQ.data[i].l.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
[2af1943]95                } while( !__atomic_try_acquire( &readyQ.data[i].l.lock ) );
[12daa43]96        }
97
[3c4bf05]98        // Actually push it
[884f3f67]99        push(readyQ.data[i], thrd);
[12daa43]100
[3c4bf05]101        // Unlock and return
[2af1943]102        __atomic_unlock( &readyQ.data[i].l.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
[1756e08]118        struct processor * const proc = kernelTLS().this_processor;
[3c4bf05]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
[b035046]127        if(proc->rdq.target == UINT_MAX) {
[3c4bf05]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;
[2af1943]138                __cfadbg_print_safe(ready_queue, "Kernel : %u considering helping %u, tcsc %llu\n", this, target, readyQ.tscs[target].t.tv);
139                /* paranoid */ verify( readyQ.tscs[target].t.tv != ULLONG_MAX );
[884f3f67]140                if(target < lanes_count) {
[5f9c42b]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, true);
142                        const unsigned long long age = moving_average(ctsc, readyQ.tscs[target].t.tv, readyQ.tscs[target].t.ma, false);
[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                }
[b035046]149                proc->rdq.target = UINT_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]177static 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
[2af1943]190        if( !__atomic_try_acquire(&lane.l.lock) ) {
[d2fadeb]191                return 0p;
192        }
[b798713]193
194        // If list is empty, unlock and retry
[dca5802]195        if( is_empty(lane) ) {
[2af1943]196                __atomic_unlock(&lane.l.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);
[2af1943]208        /* paranoid */ verify(lane.l.lock);
[b798713]209
210        // Unlock and return
[2af1943]211        __atomic_unlock(&lane.l.lock);
[b798713]212
[dca5802]213        // Update statistics
[d2fadeb]214        __STATS( stats.success++; )
[b798713]215
[5f9c42b]216        touch_tsc(readyQ.tscs, w, ts_prev, ts_next, true);
[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]227static 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
246unsigned ready_queue_new_preferred() {
[b035046]247        unsigned pref = UINT_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]257static 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}
Note: See TracBrowser for help on using the repository browser.