source: libcfa/src/concurrency/ready_queue.cfa@ 76c94bf

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 76c94bf was a017ee7, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Ready-queue grow/shrink now reassigns the id of all processors.

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