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