source: libcfa/src/concurrency/ready_queue.cfa @ 75d874a

ADTast-experimental
Last change on this file since 75d874a was f5f2768, checked in by Peter A. Buhr <pabuhr@…>, 17 months ago

make _GNU_SOURCE default, change IO to use SOCKADDR_ARG and CONST_SOCKADDR_ARG, move sys/socket.h to first include because of anonymous naming problem

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