source: libcfa/src/concurrency/ready_queue.cfa@ f6fdfb14

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f6fdfb14 was f6fdfb14, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Removed old sub-queue

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