source: libcfa/src/concurrency/ready_queue.cfa @ 78ea291

arm-ehenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 78ea291 was 78ea291, checked in by Thierry Delisle <tdelisle@…>, 19 months ago

Cleanup stats for concision.

  • Property mode set to 100644
File size: 22.0 KB
Line 
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__
17// #define __CFA_DEBUG_PRINT_READY_QUEUE__
18
19
20#define USE_RELAXED_FIFO
21// #define USE_WORK_STEALING
22
23#include "bits/defs.hfa"
24#include "kernel_private.hfa"
25
26#define _GNU_SOURCE
27#include "stdlib.hfa"
28#include "math.hfa"
29
30#include <unistd.h>
31
32#include "ready_subqueue.hfa"
33
34static const size_t cache_line_size = 64;
35
36#if !defined(__CFA_NO_STATISTICS__)
37        #define __STATS(...) __VA_ARGS__
38#else
39        #define __STATS(...)
40#endif
41
42// No overriden function, no environment variable, no define
43// fall back to a magic number
44#ifndef __CFA_MAX_PROCESSORS__
45        #define __CFA_MAX_PROCESSORS__ 1024
46#endif
47
48#if   defined(USE_RELAXED_FIFO)
49        #define BIAS 4
50        #define READYQ_SHARD_FACTOR 4
51        #define SEQUENTIAL_SHARD 1
52#elif defined(USE_WORK_STEALING)
53        #define READYQ_SHARD_FACTOR 2
54        #define SEQUENTIAL_SHARD 2
55#else
56        #error no scheduling strategy selected
57#endif
58
59static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats));
60static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats));
61static inline struct $thread * search(struct cluster * cltr);
62static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred);
63
64
65// returns the maximum number of processors the RWLock support
66__attribute__((weak)) unsigned __max_processors() {
67        const char * max_cores_s = getenv("CFA_MAX_PROCESSORS");
68        if(!max_cores_s) {
69                __cfadbg_print_nolock(ready_queue, "No CFA_MAX_PROCESSORS in ENV\n");
70                return __CFA_MAX_PROCESSORS__;
71        }
72
73        char * endptr = 0p;
74        long int max_cores_l = strtol(max_cores_s, &endptr, 10);
75        if(max_cores_l < 1 || max_cores_l > 65535) {
76                __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS out of range : %ld\n", max_cores_l);
77                return __CFA_MAX_PROCESSORS__;
78        }
79        if('\0' != *endptr) {
80                __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS not a decimal number : %s\n", max_cores_s);
81                return __CFA_MAX_PROCESSORS__;
82        }
83
84        return max_cores_l;
85}
86
87//=======================================================================
88// Cluster wide reader-writer lock
89//=======================================================================
90void  ?{}(__scheduler_RWLock_t & this) {
91        this.max   = __max_processors();
92        this.alloc = 0;
93        this.ready = 0;
94        this.data  = alloc(this.max);
95        this.write_lock  = false;
96
97        /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.alloc), &this.alloc));
98        /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.ready), &this.ready));
99
100}
101void ^?{}(__scheduler_RWLock_t & this) {
102        free(this.data);
103}
104
105
106//=======================================================================
107// Lock-Free registering/unregistering of threads
108unsigned register_proc_id( void ) with(*__scheduler_lock) {
109        __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p for RW-Lock\n", proc);
110        bool * handle = (bool *)&kernelTLS().sched_lock;
111
112        // Step - 1 : check if there is already space in the data
113        uint_fast32_t s = ready;
114
115        // Check among all the ready
116        for(uint_fast32_t i = 0; i < s; i++) {
117                bool * volatile * cell = (bool * volatile *)&data[i]; // Cforall is bugged and the double volatiles causes problems
118                /* paranoid */ verify( handle != *cell );
119
120                bool * null = 0p; // Re-write every loop since compare thrashes it
121                if( __atomic_load_n(cell, (int)__ATOMIC_RELAXED) == null
122                        && __atomic_compare_exchange_n( cell, &null, handle, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
123                        /* paranoid */ verify(i < ready);
124                        /* paranoid */ verify( (kernelTLS().sched_id = i, true) );
125                        return i;
126                }
127        }
128
129        if(max <= alloc) abort("Trying to create more than %ud processors", __scheduler_lock->max);
130
131        // Step - 2 : F&A to get a new spot in the array.
132        uint_fast32_t n = __atomic_fetch_add(&alloc, 1, __ATOMIC_SEQ_CST);
133        if(max <= n) abort("Trying to create more than %ud processors", __scheduler_lock->max);
134
135        // Step - 3 : Mark space as used and then publish it.
136        data[n] = handle;
137        while() {
138                unsigned copy = n;
139                if( __atomic_load_n(&ready, __ATOMIC_RELAXED) == n
140                        && __atomic_compare_exchange_n(&ready, &copy, n + 1, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
141                        break;
142                Pause();
143        }
144
145        __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p done, id %lu\n", proc, n);
146
147        // Return new spot.
148        /* paranoid */ verify(n < ready);
149        /* paranoid */ verify( (kernelTLS().sched_id = n, true) );
150        return n;
151}
152
153void unregister_proc_id( unsigned id ) with(*__scheduler_lock) {
154        /* paranoid */ verify(id < ready);
155        /* paranoid */ verify(id == kernelTLS().sched_id);
156        /* paranoid */ verify(data[id] == &kernelTLS().sched_lock);
157
158        bool * volatile * cell = (bool * volatile *)&data[id]; // Cforall is bugged and the double volatiles causes problems
159
160        __atomic_store_n(cell, 0p, __ATOMIC_RELEASE);
161
162        __cfadbg_print_safe(ready_queue, "Kernel : Unregister proc %p\n", proc);
163}
164
165//-----------------------------------------------------------------------
166// Writer side : acquire when changing the ready queue, e.g. adding more
167//  queues or removing them.
168uint_fast32_t ready_mutate_lock( void ) with(*__scheduler_lock) {
169        /* paranoid */ verify( ! __preemption_enabled() );
170        /* paranoid */ verify( ! kernelTLS().sched_lock );
171
172        // Step 1 : lock global lock
173        // It is needed to avoid processors that register mid Critical-Section
174        //   to simply lock their own lock and enter.
175        __atomic_acquire( &write_lock );
176
177        // Step 2 : lock per-proc lock
178        // Processors that are currently being registered aren't counted
179        //   but can't be in read_lock or in the critical section.
180        // All other processors are counted
181        uint_fast32_t s = ready;
182        for(uint_fast32_t i = 0; i < s; i++) {
183                volatile bool * llock = data[i];
184                if(llock) __atomic_acquire( llock );
185        }
186
187        /* paranoid */ verify( ! __preemption_enabled() );
188        return s;
189}
190
191void ready_mutate_unlock( uint_fast32_t last_s ) with(*__scheduler_lock) {
192        /* paranoid */ verify( ! __preemption_enabled() );
193
194        // Step 1 : release local locks
195        // This must be done while the global lock is held to avoid
196        //   threads that where created mid critical section
197        //   to race to lock their local locks and have the writer
198        //   immidiately unlock them
199        // Alternative solution : return s in write_lock and pass it to write_unlock
200        for(uint_fast32_t i = 0; i < last_s; i++) {
201                volatile bool * llock = data[i];
202                if(llock) __atomic_store_n(llock, (bool)false, __ATOMIC_RELEASE);
203        }
204
205        // Step 2 : release global lock
206        /*paranoid*/ assert(true == write_lock);
207        __atomic_store_n(&write_lock, (bool)false, __ATOMIC_RELEASE);
208
209        /* paranoid */ verify( ! __preemption_enabled() );
210}
211
212//=======================================================================
213// Cforall Ready Queue used for scheduling
214//=======================================================================
215void ?{}(__ready_queue_t & this) with (this) {
216        lanes.data  = 0p;
217        lanes.tscs  = 0p;
218        lanes.count = 0;
219}
220
221void ^?{}(__ready_queue_t & this) with (this) {
222        verify( SEQUENTIAL_SHARD == lanes.count );
223        free(lanes.data);
224        free(lanes.tscs);
225}
226
227//-----------------------------------------------------------------------
228#if defined(USE_RELAXED_FIFO)
229        //-----------------------------------------------------------------------
230        // get index from random number with or without bias towards queues
231        static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred) {
232                unsigned i;
233                bool local;
234                unsigned rlow  = r % BIAS;
235                unsigned rhigh = r / BIAS;
236                if((0 != rlow) && preferred >= 0) {
237                        // (BIAS - 1) out of BIAS chances
238                        // Use perferred queues
239                        i = preferred + (rhigh % READYQ_SHARD_FACTOR);
240                        local = true;
241                }
242                else {
243                        // 1 out of BIAS chances
244                        // Use all queues
245                        i = rhigh;
246                        local = false;
247                }
248                return [i, local];
249        }
250
251        __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd) with (cltr->ready_queue) {
252                __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
253
254                const bool external = (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
255                /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
256
257                bool local;
258                int preferred = external ? -1 : kernelTLS().this_processor->rdq.id;
259
260                // Try to pick a lane and lock it
261                unsigned i;
262                do {
263                        // Pick the index of a lane
264                        unsigned r = __tls_rand_fwd();
265                        [i, local] = idx_from_r(r, preferred);
266
267                        i %= __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
268
269                        #if !defined(__CFA_NO_STATISTICS__)
270                                if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
271                                else if(local) __tls_stats()->ready.push.local.attempt++;
272                                else __tls_stats()->ready.push.share.attempt++;
273                        #endif
274
275                        // If we can't lock it retry
276                } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
277
278                // Actually push it
279                push(lanes.data[i], thrd);
280
281                        // Unlock and return
282                        __atomic_unlock( &lanes.data[i].lock );
283
284                // Mark the current index in the tls rng instance as having an item
285                __tls_rand_advance_bck();
286
287                __cfadbg_print_safe(ready_queue, "Kernel : Pushed %p on cluster %p (idx: %u, mask %llu, first %d)\n", thrd, cltr, i, used.mask[0], lane_first);
288
289                // Update statistics
290                #if !defined(__CFA_NO_STATISTICS__)
291                        if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
292                        else if(local) __tls_stats()->ready.push.local.success++;
293                        else __tls_stats()->ready.push.share.success++;
294                #endif
295        }
296
297        // Pop from the ready queue from a given cluster
298        __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
299                /* paranoid */ verify( lanes.count > 0 );
300                /* paranoid */ verify( kernelTLS().this_processor );
301                /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
302
303                unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
304                int preferred = kernelTLS().this_processor->rdq.id;
305
306
307                // As long as the list is not empty, try finding a lane that isn't empty and pop from it
308                for(25) {
309                        // Pick two lists at random
310                        unsigned ri = __tls_rand_bck();
311                        unsigned rj = __tls_rand_bck();
312
313                        unsigned i, j;
314                        __attribute__((unused)) bool locali, localj;
315                        [i, locali] = idx_from_r(ri, preferred);
316                        [j, localj] = idx_from_r(rj, preferred);
317
318                        i %= count;
319                        j %= count;
320
321                        // try popping from the 2 picked lists
322                        struct $thread * thrd = try_pop(cltr, i, j __STATS(, *(locali || localj ? &__tls_stats()->ready.pop.local : &__tls_stats()->ready.pop.help)));
323                        if(thrd) {
324                                return thrd;
325                        }
326                }
327
328                // All lanes where empty return 0p
329                return 0p;
330        }
331
332        __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) { return pop_fast(cltr); }
333        __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) {
334                return search(cltr);
335        }
336#endif
337#if defined(USE_WORK_STEALING)
338        __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd) with (cltr->ready_queue) {
339                __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
340
341                // #define USE_PREFERRED
342                #if !defined(USE_PREFERRED)
343                const bool external = (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
344                /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
345                #else
346                        unsigned preferred = thrd->preferred;
347                        const bool external = (!kernelTLS().this_processor) || preferred == -1u || thrd->curr_cluster != cltr;
348                        /* paranoid */ verifyf(external || preferred < lanes.count, "Invalid preferred queue %u for %u lanes", preferred, lanes.count );
349
350                        unsigned r = preferred % READYQ_SHARD_FACTOR;
351                        const unsigned start = preferred - r;
352                #endif
353
354                // Try to pick a lane and lock it
355                unsigned i;
356                do {
357                        #if !defined(__CFA_NO_STATISTICS__)
358                                if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
359                                else __tls_stats()->ready.push.local.attempt++;
360                        #endif
361
362                        if(unlikely(external)) {
363                                i = __tls_rand() % lanes.count;
364                        }
365                        else {
366                                #if !defined(USE_PREFERRED)
367                                processor * proc = kernelTLS().this_processor;
368                                unsigned r = proc->rdq.its++;
369                                i =  proc->rdq.id + (r % READYQ_SHARD_FACTOR);
370                #else
371                                        i = start + (r++ % READYQ_SHARD_FACTOR);
372                                #endif
373                        }
374                        // If we can't lock it retry
375                } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
376
377                // Actually push it
378                push(lanes.data[i], thrd);
379
380                        // Unlock and return
381                        __atomic_unlock( &lanes.data[i].lock );
382
383                #if !defined(__CFA_NO_STATISTICS__)
384                        if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
385                        else __tls_stats()->ready.push.local.success++;
386                #endif
387
388                __cfadbg_print_safe(ready_queue, "Kernel : Pushed %p on cluster %p (idx: %u, mask %llu, first %d)\n", thrd, cltr, i, used.mask[0], lane_first);
389        }
390
391        // Pop from the ready queue from a given cluster
392        __attribute__((hot)) $thread * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
393                /* paranoid */ verify( lanes.count > 0 );
394                /* paranoid */ verify( kernelTLS().this_processor );
395                /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
396
397                processor * proc = kernelTLS().this_processor;
398
399                if(proc->rdq.target == -1u) {
400                        unsigned long long min = ts(lanes.data[proc->rdq.id]);
401                        for(int i = 0; i < READYQ_SHARD_FACTOR; i++) {
402                                unsigned long long tsc = ts(lanes.data[proc->rdq.id + i]);
403                                if(tsc < min) min = tsc;
404                        }
405                        proc->rdq.cutoff = min;
406                        proc->rdq.target = __tls_rand() % lanes.count;
407                }
408                else {
409                        unsigned target = proc->rdq.target;
410                        proc->rdq.target = -1u;
411                        if(lanes.tscs[target].tv < proc->rdq.cutoff) {
412                                $thread * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
413                                if(t) return t;
414                        }
415                }
416
417                for(READYQ_SHARD_FACTOR) {
418                        unsigned i = proc->rdq.id + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
419                        if($thread * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
420                }
421                return 0p;
422        }
423
424        __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
425                unsigned i = __tls_rand() % lanes.count;
426                return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
427        }
428
429        __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr) with (cltr->ready_queue) {
430                return search(cltr);
431        }
432#endif
433
434//=======================================================================
435// Various Ready Queue utilities
436//=======================================================================
437// these function work the same or almost the same
438// whether they are using work-stealing or relaxed fifo scheduling
439
440//-----------------------------------------------------------------------
441// try to pop from a lane given by index w
442static inline struct $thread * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
443        __STATS( stats.attempt++; )
444
445        // Get relevant elements locally
446        __intrusive_lane_t & lane = lanes.data[w];
447
448        // If list looks empty retry
449        if( is_empty(lane) ) {
450                return 0p;
451        }
452
453        // If we can't get the lock retry
454        if( !__atomic_try_acquire(&lane.lock) ) {
455                return 0p;
456        }
457
458        // If list is empty, unlock and retry
459        if( is_empty(lane) ) {
460                __atomic_unlock(&lane.lock);
461                return 0p;
462        }
463
464        // Actually pop the list
465        struct $thread * thrd;
466        unsigned long long tsv;
467        [thrd, tsv] = pop(lane);
468
469        /* paranoid */ verify(thrd);
470        /* paranoid */ verify(tsv);
471        /* paranoid */ verify(lane.lock);
472
473        // Unlock and return
474        __atomic_unlock(&lane.lock);
475
476        // Update statistics
477        __STATS( stats.success++; )
478
479        #if defined(USE_WORK_STEALING)
480                lanes.tscs[w].tv = tsv;
481        #endif
482
483        thrd->preferred = w;
484
485        // return the popped thread
486        return thrd;
487}
488
489//-----------------------------------------------------------------------
490// try to pop from any lanes making sure you don't miss any threads push
491// before the start of the function
492static inline struct $thread * search(struct cluster * cltr) with (cltr->ready_queue) {
493        /* paranoid */ verify( lanes.count > 0 );
494        unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
495        unsigned offset = __tls_rand();
496        for(i; count) {
497                unsigned idx = (offset + i) % count;
498                struct $thread * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search));
499                if(thrd) {
500                        return thrd;
501                }
502        }
503
504        // All lanes where empty return 0p
505        return 0p;
506}
507
508//-----------------------------------------------------------------------
509// Check that all the intrusive queues in the data structure are still consistent
510static void check( __ready_queue_t & q ) with (q) {
511        #if defined(__CFA_WITH_VERIFY__)
512                {
513                        for( idx ; lanes.count ) {
514                                __intrusive_lane_t & sl = lanes.data[idx];
515                                assert(!lanes.data[idx].lock);
516
517                                        if(is_empty(sl)) {
518                                                assert( sl.anchor.next == 0p );
519                                                assert( sl.anchor.ts   == 0  );
520                                                assert( mock_head(sl)  == sl.prev );
521                                        } else {
522                                                assert( sl.anchor.next != 0p );
523                                                assert( sl.anchor.ts   != 0  );
524                                                assert( mock_head(sl)  != sl.prev );
525                                        }
526                        }
527                }
528        #endif
529}
530
531//-----------------------------------------------------------------------
532// Given 2 indexes, pick the list with the oldest push an try to pop from it
533static inline struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
534        // Pick the bet list
535        int w = i;
536        if( __builtin_expect(!is_empty(lanes.data[j]), true) ) {
537                w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j;
538        }
539
540        return try_pop(cltr, w __STATS(, stats));
541}
542
543// Call this function of the intrusive list was moved using memcpy
544// fixes the list so that the pointers back to anchors aren't left dangling
545static inline void fix(__intrusive_lane_t & ll) {
546                        if(is_empty(ll)) {
547                                verify(ll.anchor.next == 0p);
548                                ll.prev = mock_head(ll);
549                        }
550}
551
552static void assign_list(unsigned & value, dlist(processor) & list, unsigned count) {
553        processor * it = &list`first;
554        for(unsigned i = 0; i < count; i++) {
555                /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
556                it->rdq.id = value;
557                it->rdq.target = -1u;
558                value += READYQ_SHARD_FACTOR;
559                it = &(*it)`next;
560        }
561}
562
563static void reassign_cltr_id(struct cluster * cltr) {
564        unsigned preferred = 0;
565        assign_list(preferred, cltr->procs.actives, cltr->procs.total - cltr->procs.idle);
566        assign_list(preferred, cltr->procs.idles  , cltr->procs.idle );
567}
568
569static void fix_times( struct cluster * cltr ) with( cltr->ready_queue ) {
570        #if defined(USE_WORK_STEALING)
571                lanes.tscs = alloc(lanes.count, lanes.tscs`realloc);
572                for(i; lanes.count) {
573                        lanes.tscs[i].tv = ts(lanes.data[i]);
574                }
575        #endif
576}
577
578// Grow the ready queue
579void ready_queue_grow(struct cluster * cltr) {
580        size_t ncount;
581        int target = cltr->procs.total;
582
583        /* paranoid */ verify( ready_mutate_islocked() );
584        __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n");
585
586        // Make sure that everything is consistent
587        /* paranoid */ check( cltr->ready_queue );
588
589        // grow the ready queue
590        with( cltr->ready_queue ) {
591                // Find new count
592                // Make sure we always have atleast 1 list
593                if(target >= 2) {
594                        ncount = target * READYQ_SHARD_FACTOR;
595                } else {
596                        ncount = SEQUENTIAL_SHARD;
597                }
598
599                // Allocate new array (uses realloc and memcpies the data)
600                lanes.data = alloc( ncount, lanes.data`realloc );
601
602                // Fix the moved data
603                for( idx; (size_t)lanes.count ) {
604                        fix(lanes.data[idx]);
605                }
606
607                // Construct new data
608                for( idx; (size_t)lanes.count ~ ncount) {
609                        (lanes.data[idx]){};
610                }
611
612                // Update original
613                lanes.count = ncount;
614        }
615
616        fix_times(cltr);
617
618        reassign_cltr_id(cltr);
619
620        // Make sure that everything is consistent
621        /* paranoid */ check( cltr->ready_queue );
622
623        __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n");
624
625        /* paranoid */ verify( ready_mutate_islocked() );
626}
627
628// Shrink the ready queue
629void ready_queue_shrink(struct cluster * cltr) {
630        /* paranoid */ verify( ready_mutate_islocked() );
631        __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n");
632
633        // Make sure that everything is consistent
634        /* paranoid */ check( cltr->ready_queue );
635
636        int target = cltr->procs.total;
637
638        with( cltr->ready_queue ) {
639                // Remember old count
640                size_t ocount = lanes.count;
641
642                // Find new count
643                // Make sure we always have atleast 1 list
644                lanes.count = target >= 2 ? target * READYQ_SHARD_FACTOR: SEQUENTIAL_SHARD;
645                /* paranoid */ verify( ocount >= lanes.count );
646                /* paranoid */ verify( lanes.count == target * READYQ_SHARD_FACTOR || target < 2 );
647
648                // for printing count the number of displaced threads
649                #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
650                        __attribute__((unused)) size_t displaced = 0;
651                #endif
652
653                // redistribute old data
654                for( idx; (size_t)lanes.count ~ ocount) {
655                        // Lock is not strictly needed but makes checking invariants much easier
656                        __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock);
657                        verify(locked);
658
659                        // As long as we can pop from this lane to push the threads somewhere else in the queue
660                        while(!is_empty(lanes.data[idx])) {
661                                struct $thread * thrd;
662                                unsigned long long _;
663                                [thrd, _] = pop(lanes.data[idx]);
664
665                                push(cltr, thrd);
666
667                                // for printing count the number of displaced threads
668                                #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
669                                        displaced++;
670                                #endif
671                        }
672
673                        // Unlock the lane
674                        __atomic_unlock(&lanes.data[idx].lock);
675
676                        // TODO print the queue statistics here
677
678                        ^(lanes.data[idx]){};
679                }
680
681                __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced);
682
683                // Allocate new array (uses realloc and memcpies the data)
684                lanes.data = alloc( lanes.count, lanes.data`realloc );
685
686                // Fix the moved data
687                for( idx; (size_t)lanes.count ) {
688                        fix(lanes.data[idx]);
689                }
690        }
691
692        fix_times(cltr);
693
694        reassign_cltr_id(cltr);
695
696        // Make sure that everything is consistent
697        /* paranoid */ check( cltr->ready_queue );
698
699        __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n");
700        /* paranoid */ verify( ready_mutate_islocked() );
701}
702
703#if !defined(__CFA_NO_STATISTICS__)
704        unsigned cnt(const __ready_queue_t & this, unsigned idx) {
705                /* paranoid */ verify(this.lanes.count > idx);
706                return this.lanes.data[idx].cnt;
707        }
708#endif
Note: See TracBrowser for help on using the repository browser.