[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 | |
---|
| 19 | #include "bits/defs.hfa" |
---|
| 20 | #include "kernel_private.hfa" |
---|
| 21 | |
---|
| 22 | #define _GNU_SOURCE |
---|
| 23 | #include "stdlib.hfa" |
---|
| 24 | |
---|
| 25 | static const size_t cache_line_size = 64; |
---|
| 26 | |
---|
[dca5802] | 27 | // No overriden function, no environment variable, no define |
---|
| 28 | // fall back to a magic number |
---|
| 29 | #ifndef __CFA_MAX_PROCESSORS__ |
---|
| 30 | #define __CFA_MAX_PROCESSORS__ 128 |
---|
| 31 | #endif |
---|
[7768b8d] | 32 | |
---|
[dca5802] | 33 | // returns the maximum number of processors the RWLock support |
---|
[7768b8d] | 34 | __attribute__((weak)) unsigned __max_processors() { |
---|
| 35 | const char * max_cores_s = getenv("CFA_MAX_PROCESSORS"); |
---|
| 36 | if(!max_cores_s) { |
---|
[504a7dc] | 37 | __cfadbg_print_nolock(ready_queue, "No CFA_MAX_PROCESSORS in ENV\n"); |
---|
[dca5802] | 38 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 39 | } |
---|
| 40 | |
---|
| 41 | char * endptr = 0p; |
---|
| 42 | long int max_cores_l = strtol(max_cores_s, &endptr, 10); |
---|
| 43 | if(max_cores_l < 1 || max_cores_l > 65535) { |
---|
[504a7dc] | 44 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS out of range : %ld\n", max_cores_l); |
---|
[dca5802] | 45 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 46 | } |
---|
| 47 | if('\0' != *endptr) { |
---|
[504a7dc] | 48 | __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS not a decimal number : %s\n", max_cores_s); |
---|
[dca5802] | 49 | return __CFA_MAX_PROCESSORS__; |
---|
[7768b8d] | 50 | } |
---|
| 51 | |
---|
| 52 | return max_cores_l; |
---|
| 53 | } |
---|
| 54 | |
---|
[dca5802] | 55 | // Picks a random 1 bit in 'mask' according to random number 'rnum'. |
---|
| 56 | static inline unsigned rand_bit(unsigned rnum, __cfa_readyQ_mask_t mask) { |
---|
| 57 | #if defined( __i386 ) |
---|
| 58 | static_assert(sizeof(mask) == 4); |
---|
| 59 | unsigned bit = mask ? rnum % __builtin_popcount(mask) : 0; |
---|
| 60 | #if !defined(__BMI2__) |
---|
| 61 | #error rand_bit not implemented for non __BMI2__ i386 |
---|
| 62 | #else |
---|
| 63 | uint32_t picked = _pdep_u32(1ul << bit, mask); |
---|
| 64 | return picked ? __builtin_ctz(picked) : 0; |
---|
| 65 | #endif |
---|
| 66 | #elif defined( __x86_64 ) |
---|
| 67 | static_assert(sizeof(mask) == 8); |
---|
[b798713] | 68 | unsigned bit = mask ? rnum % __builtin_popcountl(mask) : 0; |
---|
[dca5802] | 69 | #if !defined(__BMI2__) |
---|
| 70 | uint64_t v = mask; // Input value to find position with rank r. |
---|
| 71 | unsigned int r = bit + 1;// Input: bit's desired rank [1-64]. |
---|
| 72 | unsigned int s; // Output: Resulting position of bit with rank r [1-64] |
---|
| 73 | uint64_t a, b, c, d; // Intermediate temporaries for bit count. |
---|
| 74 | unsigned int t; // Bit count temporary. |
---|
| 75 | |
---|
| 76 | // Do a normal parallel bit count for a 64-bit integer, |
---|
| 77 | // but store all intermediate steps. |
---|
| 78 | a = v - ((v >> 1) & ~0UL/3); |
---|
| 79 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
---|
| 80 | c = (b + (b >> 4)) & ~0UL/0x11; |
---|
| 81 | d = (c + (c >> 8)) & ~0UL/0x101; |
---|
| 82 | |
---|
| 83 | |
---|
| 84 | t = (d >> 32) + (d >> 48); |
---|
| 85 | // Now do branchless select! |
---|
| 86 | s = 64; |
---|
| 87 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
---|
| 88 | t = (d >> (s - 16)) & 0xff; |
---|
| 89 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
---|
| 90 | t = (c >> (s - 8)) & 0xf; |
---|
| 91 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
---|
| 92 | t = (b >> (s - 4)) & 0x7; |
---|
| 93 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
---|
| 94 | t = (a >> (s - 2)) & 0x3; |
---|
| 95 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
---|
| 96 | t = (v >> (s - 1)) & 0x1; |
---|
| 97 | s -= ((t - r) & 256) >> 8; |
---|
| 98 | return s - 1; |
---|
| 99 | #else |
---|
| 100 | uint64_t picked = _pdep_u64(1ul << bit, mask); |
---|
| 101 | return picked ? __builtin_ctzl(picked) : 0; |
---|
| 102 | #endif |
---|
| 103 | #elif defined( __ARM_ARCH ) |
---|
| 104 | #error rand_bit not implemented for arm |
---|
[b798713] | 105 | #else |
---|
[dca5802] | 106 | #error uknown hardware architecture |
---|
[b798713] | 107 | #endif |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | |
---|
[dca5802] | 111 | //----------------------------------------------------------------------------- |
---|
| 112 | // Helpers used by extract |
---|
| 113 | // (_mask_bitsidx() & X) returns a bit index valid for a __cfa_readyQ_mask_t, where X is any integer |
---|
[1b143de] | 114 | static inline __cfa_readyQ_mask_t _mask_bitsidx () __attribute__ ((const)) { return (8 * sizeof(__cfa_readyQ_mask_t)) - 1; } |
---|
[dca5802] | 115 | |
---|
| 116 | // (X >> _mask_shiftidx()) retuns an index into an array of __cfa_readyQ_mask_t |
---|
[1b143de] | 117 | static inline __cfa_readyQ_mask_t _mask_shiftidx() __attribute__ ((const)) { return (8 * sizeof(__cfa_readyQ_mask_t)) - __builtin_clzl(_mask_bitsidx()); } |
---|
[dca5802] | 118 | |
---|
| 119 | |
---|
| 120 | // Assuming a large bit mask represented as an array of __cfa_readyQ_mask_t |
---|
| 121 | // Given an index into the large mask, returns the bit index and which __cfa_readyQ_mask_t index in the array |
---|
[b798713] | 122 | static inline [__cfa_readyQ_mask_t, __cfa_readyQ_mask_t] extract(__cfa_readyQ_mask_t idx) { |
---|
[1b143de] | 123 | __cfa_readyQ_mask_t word = idx >> _mask_shiftidx(); |
---|
| 124 | __cfa_readyQ_mask_t bit = idx & _mask_bitsidx(); |
---|
[b798713] | 125 | return [bit, word]; |
---|
| 126 | } |
---|
| 127 | |
---|
[7768b8d] | 128 | //======================================================================= |
---|
| 129 | // Cluster wide reader-writer lock |
---|
| 130 | //======================================================================= |
---|
| 131 | void ?{}(__clusterRWLock_t & this) { |
---|
| 132 | this.max = __max_processors(); |
---|
| 133 | this.alloc = 0; |
---|
| 134 | this.ready = 0; |
---|
| 135 | this.lock = false; |
---|
| 136 | this.data = alloc(this.max); |
---|
| 137 | |
---|
| 138 | /*paranoid*/ verify( 0 == (((uintptr_t)(this.data )) % 64) ); |
---|
| 139 | /*paranoid*/ verify( 0 == (((uintptr_t)(this.data + 1)) % 64) ); |
---|
| 140 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.alloc), &this.alloc)); |
---|
| 141 | /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.ready), &this.ready)); |
---|
| 142 | |
---|
| 143 | } |
---|
| 144 | void ^?{}(__clusterRWLock_t & this) { |
---|
| 145 | free(this.data); |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | void ?{}( __processor_id & this, struct processor * proc ) { |
---|
| 149 | this.handle = proc; |
---|
| 150 | this.lock = false; |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | //======================================================================= |
---|
| 154 | // Lock-Free registering/unregistering of threads |
---|
[504a7dc] | 155 | unsigned doregister2( struct cluster * cltr, struct processor * proc ) with(cltr->ready_lock) { |
---|
| 156 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p with cluster %p\n", proc, cltr); |
---|
| 157 | |
---|
[7768b8d] | 158 | // Step - 1 : check if there is already space in the data |
---|
| 159 | uint_fast32_t s = ready; |
---|
| 160 | |
---|
| 161 | // Check among all the ready |
---|
| 162 | for(uint_fast32_t i = 0; i < s; i++) { |
---|
| 163 | processor * null = 0p; // Re-write every loop since compare thrashes it |
---|
| 164 | if( __atomic_load_n(&data[i].handle, (int)__ATOMIC_RELAXED) == null |
---|
| 165 | && __atomic_compare_exchange_n( &data[i].handle, &null, proc, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 166 | /*paranoid*/ verify(i < ready); |
---|
| 167 | /*paranoid*/ verify(__alignof__(data[i]) == cache_line_size); |
---|
| 168 | /*paranoid*/ verify((((uintptr_t)&data[i]) % cache_line_size) == 0); |
---|
| 169 | return i; |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | if(max <= alloc) abort("Trying to create more than %ud processors", cltr->ready_lock.max); |
---|
| 174 | |
---|
| 175 | // Step - 2 : F&A to get a new spot in the array. |
---|
| 176 | uint_fast32_t n = __atomic_fetch_add(&alloc, 1, __ATOMIC_SEQ_CST); |
---|
| 177 | if(max <= n) abort("Trying to create more than %ud processors", cltr->ready_lock.max); |
---|
| 178 | |
---|
| 179 | // Step - 3 : Mark space as used and then publish it. |
---|
| 180 | __processor_id * storage = (__processor_id *)&data[n]; |
---|
| 181 | (*storage){ proc }; |
---|
| 182 | while(true) { |
---|
| 183 | unsigned copy = n; |
---|
| 184 | if( __atomic_load_n(&ready, __ATOMIC_RELAXED) == n |
---|
| 185 | && __atomic_compare_exchange_n(&ready, ©, n + 1, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) |
---|
| 186 | break; |
---|
| 187 | asm volatile("pause"); |
---|
| 188 | } |
---|
| 189 | |
---|
[1b143de] | 190 | __cfadbg_print_safe(ready_queue, "Kernel : Registering proc %p done, id %lu\n", proc, n); |
---|
[504a7dc] | 191 | |
---|
[7768b8d] | 192 | // Return new spot. |
---|
| 193 | /*paranoid*/ verify(n < ready); |
---|
| 194 | /*paranoid*/ verify(__alignof__(data[n]) == cache_line_size); |
---|
| 195 | /*paranoid*/ verify((((uintptr_t)&data[n]) % cache_line_size) == 0); |
---|
| 196 | return n; |
---|
| 197 | } |
---|
| 198 | |
---|
[504a7dc] | 199 | void unregister2( struct cluster * cltr, struct processor * proc ) with(cltr->ready_lock) { |
---|
[7768b8d] | 200 | unsigned id = proc->id; |
---|
| 201 | /*paranoid*/ verify(id < ready); |
---|
| 202 | /*paranoid*/ verify(proc == __atomic_load_n(&data[id].handle, __ATOMIC_RELAXED)); |
---|
| 203 | __atomic_store_n(&data[id].handle, 0p, __ATOMIC_RELEASE); |
---|
[504a7dc] | 204 | |
---|
| 205 | __cfadbg_print_safe(ready_queue, "Kernel : Unregister proc %p\n", proc); |
---|
[7768b8d] | 206 | } |
---|
| 207 | |
---|
| 208 | //----------------------------------------------------------------------- |
---|
| 209 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
| 210 | // queues or removing them. |
---|
| 211 | uint_fast32_t ready_mutate_lock( struct cluster & cltr ) with(cltr.ready_lock) { |
---|
| 212 | // Step 1 : lock global lock |
---|
| 213 | // It is needed to avoid processors that register mid Critical-Section |
---|
| 214 | // to simply lock their own lock and enter. |
---|
| 215 | __atomic_acquire( &lock ); |
---|
| 216 | |
---|
| 217 | // Step 2 : lock per-proc lock |
---|
| 218 | // Processors that are currently being registered aren't counted |
---|
| 219 | // but can't be in read_lock or in the critical section. |
---|
| 220 | // All other processors are counted |
---|
| 221 | uint_fast32_t s = ready; |
---|
| 222 | for(uint_fast32_t i = 0; i < s; i++) { |
---|
| 223 | __atomic_acquire( &data[i].lock ); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | return s; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | void ready_mutate_unlock( struct cluster & cltr, uint_fast32_t last_s ) with(cltr.ready_lock) { |
---|
| 230 | // Step 1 : release local locks |
---|
| 231 | // This must be done while the global lock is held to avoid |
---|
| 232 | // threads that where created mid critical section |
---|
| 233 | // to race to lock their local locks and have the writer |
---|
| 234 | // immidiately unlock them |
---|
| 235 | // Alternative solution : return s in write_lock and pass it to write_unlock |
---|
| 236 | for(uint_fast32_t i = 0; i < last_s; i++) { |
---|
| 237 | verify(data[i].lock); |
---|
| 238 | __atomic_store_n(&data[i].lock, (bool)false, __ATOMIC_RELEASE); |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | // Step 2 : release global lock |
---|
| 242 | /*paranoid*/ assert(true == lock); |
---|
| 243 | __atomic_store_n(&lock, (bool)false, __ATOMIC_RELEASE); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | //======================================================================= |
---|
| 247 | // Intrusive Queue used by ready queue |
---|
| 248 | //======================================================================= |
---|
| 249 | // Get the head pointer (one before the first element) from the anchor |
---|
[504a7dc] | 250 | static inline $thread * head(const __intrusive_lane_t & this) { |
---|
| 251 | $thread * rhead = ($thread *)( |
---|
| 252 | (uintptr_t)( &this.before ) - offsetof( $thread, link ) |
---|
[7768b8d] | 253 | ); |
---|
| 254 | /* paranoid */ verify(rhead); |
---|
| 255 | return rhead; |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // Get the tail pointer (one after the last element) from the anchor |
---|
[504a7dc] | 259 | static inline $thread * tail(const __intrusive_lane_t & this) { |
---|
| 260 | $thread * rtail = ($thread *)( |
---|
| 261 | (uintptr_t)( &this.after ) - offsetof( $thread, link ) |
---|
[7768b8d] | 262 | ); |
---|
| 263 | /* paranoid */ verify(rtail); |
---|
| 264 | return rtail; |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | // Ctor |
---|
[dca5802] | 268 | void ?{}( __intrusive_lane_t & this ) { |
---|
[b798713] | 269 | this.lock = false; |
---|
[504a7dc] | 270 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 271 | this.last_id = -1u; |
---|
| 272 | this.count = 0u; |
---|
| 273 | #endif |
---|
[b798713] | 274 | |
---|
| 275 | this.before.link.prev = 0p; |
---|
| 276 | this.before.link.next = tail(this); |
---|
| 277 | this.before.link.ts = 0; |
---|
[7768b8d] | 278 | |
---|
[b798713] | 279 | this.after .link.prev = head(this); |
---|
| 280 | this.after .link.next = 0p; |
---|
| 281 | this.after .link.ts = 0; |
---|
| 282 | |
---|
| 283 | #if !defined(__CFA_NO_SCHED_STATS__) |
---|
| 284 | this.stat.diff = 0; |
---|
| 285 | this.stat.push = 0; |
---|
| 286 | this.stat.pop = 0; |
---|
| 287 | #endif |
---|
[7768b8d] | 288 | |
---|
| 289 | // We add a boat-load of assertions here because the anchor code is very fragile |
---|
[504a7dc] | 290 | /* paranoid */ verify(((uintptr_t)( head(this) ) + offsetof( $thread, link )) == (uintptr_t)(&this.before)); |
---|
| 291 | /* paranoid */ verify(((uintptr_t)( tail(this) ) + offsetof( $thread, link )) == (uintptr_t)(&this.after )); |
---|
[b798713] | 292 | /* paranoid */ verify(head(this)->link.prev == 0p ); |
---|
| 293 | /* paranoid */ verify(head(this)->link.next == tail(this) ); |
---|
| 294 | /* paranoid */ verify(tail(this)->link.next == 0p ); |
---|
| 295 | /* paranoid */ verify(tail(this)->link.prev == head(this) ); |
---|
| 296 | /* paranoid */ verify(&head(this)->link.prev == &this.before.link.prev ); |
---|
| 297 | /* paranoid */ verify(&head(this)->link.next == &this.before.link.next ); |
---|
| 298 | /* paranoid */ verify(&tail(this)->link.prev == &this.after .link.prev ); |
---|
| 299 | /* paranoid */ verify(&tail(this)->link.next == &this.after .link.next ); |
---|
[dca5802] | 300 | /* paranoid */ verify(sizeof(__intrusive_lane_t) == 128); |
---|
[7768b8d] | 301 | /* paranoid */ verify(sizeof(this) == 128); |
---|
[dca5802] | 302 | /* paranoid */ verify(__alignof__(__intrusive_lane_t) == 128); |
---|
[7768b8d] | 303 | /* paranoid */ verify(__alignof__(this) == 128); |
---|
| 304 | /* paranoid */ verifyf(((intptr_t)(&this) % 128) == 0, "Expected address to be aligned %p %% 128 == %zd", &this, ((intptr_t)(&this) % 128)); |
---|
[b798713] | 305 | |
---|
[1b143de] | 306 | /* paranoid */ verifyf(_mask_shiftidx() == 6 , "%llu", _mask_shiftidx()); |
---|
| 307 | /* paranoid */ verifyf(_mask_bitsidx () == 63, "%llu", _mask_bitsidx()); |
---|
[7768b8d] | 308 | } |
---|
| 309 | |
---|
| 310 | // Dtor is trivial |
---|
[dca5802] | 311 | void ^?{}( __intrusive_lane_t & this ) { |
---|
[7768b8d] | 312 | // Make sure the list is empty |
---|
[b798713] | 313 | /* paranoid */ verify(head(this)->link.prev == 0p ); |
---|
| 314 | /* paranoid */ verify(head(this)->link.next == tail(this) ); |
---|
| 315 | /* paranoid */ verify(tail(this)->link.next == 0p ); |
---|
| 316 | /* paranoid */ verify(tail(this)->link.prev == head(this) ); |
---|
[c84b4be] | 317 | /* paranoid */ verify(this.count == 0u ); |
---|
[7768b8d] | 318 | } |
---|
| 319 | |
---|
[dca5802] | 320 | // Push a thread onto this lane |
---|
| 321 | // returns true of lane was empty before push, false otherwise |
---|
[504a7dc] | 322 | bool push(__intrusive_lane_t & this, $thread * node) { |
---|
[dca5802] | 323 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 324 | /* paranoid */ verify(this.lock); |
---|
| 325 | /* paranoid */ verify(node->link.ts != 0); |
---|
| 326 | /* paranoid */ verify(node->link.next == 0p); |
---|
| 327 | /* paranoid */ verify(node->link.prev == 0p); |
---|
[504a7dc] | 328 | /* paranoid */ verify(tail(this)->link.next == 0p); |
---|
| 329 | /* paranoid */ verify(head(this)->link.prev == 0p); |
---|
[dca5802] | 330 | |
---|
| 331 | this.count++; |
---|
| 332 | |
---|
| 333 | if(this.before.link.ts == 0l) { |
---|
| 334 | /* paranoid */ verify(tail(this)->link.prev == head(this)); |
---|
| 335 | /* paranoid */ verify(head(this)->link.next == tail(this)); |
---|
[504a7dc] | 336 | } else { |
---|
| 337 | /* paranoid */ verify(tail(this)->link.prev != head(this)); |
---|
| 338 | /* paranoid */ verify(head(this)->link.next != tail(this)); |
---|
[dca5802] | 339 | } |
---|
| 340 | #endif |
---|
[7768b8d] | 341 | |
---|
| 342 | // Get the relevant nodes locally |
---|
[504a7dc] | 343 | $thread * tail = tail(this); |
---|
| 344 | $thread * prev = tail->link.prev; |
---|
[7768b8d] | 345 | |
---|
| 346 | // Do the push |
---|
[b798713] | 347 | node->link.next = tail; |
---|
| 348 | node->link.prev = prev; |
---|
| 349 | prev->link.next = node; |
---|
| 350 | tail->link.prev = node; |
---|
[7768b8d] | 351 | |
---|
| 352 | // Update stats |
---|
[dca5802] | 353 | #if !defined(__CFA_NO_SCHED_STATS__) |
---|
[7768b8d] | 354 | this.stat.diff++; |
---|
| 355 | this.stat.push++; |
---|
| 356 | #endif |
---|
| 357 | |
---|
[b798713] | 358 | verify(node->link.next == tail(this)); |
---|
| 359 | |
---|
[7768b8d] | 360 | // Check if the queue used to be empty |
---|
[b798713] | 361 | if(this.before.link.ts == 0l) { |
---|
| 362 | this.before.link.ts = node->link.ts; |
---|
[dca5802] | 363 | /* paranoid */ verify(node->link.prev == head(this)); |
---|
[7768b8d] | 364 | return true; |
---|
| 365 | } |
---|
| 366 | return false; |
---|
| 367 | } |
---|
| 368 | |
---|
[dca5802] | 369 | // Pop a thread from this lane (must be non-empty) |
---|
| 370 | // returns popped |
---|
| 371 | // returns true of lane was empty before push, false otherwise |
---|
[504a7dc] | 372 | [$thread *, bool] pop(__intrusive_lane_t & this) { |
---|
[dca5802] | 373 | /* paranoid */ verify(this.lock); |
---|
| 374 | /* paranoid */ verify(this.before.link.ts != 0ul); |
---|
| 375 | |
---|
| 376 | // Get anchors locally |
---|
[504a7dc] | 377 | $thread * head = head(this); |
---|
| 378 | $thread * tail = tail(this); |
---|
[7768b8d] | 379 | |
---|
[dca5802] | 380 | // Get the relevant nodes locally |
---|
[504a7dc] | 381 | $thread * node = head->link.next; |
---|
| 382 | $thread * next = node->link.next; |
---|
[7768b8d] | 383 | |
---|
[dca5802] | 384 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 385 | this.count--; |
---|
| 386 | /* paranoid */ verify(node != tail); |
---|
| 387 | /* paranoid */ verify(node); |
---|
| 388 | #endif |
---|
[7768b8d] | 389 | |
---|
[dca5802] | 390 | // Do the pop |
---|
[b798713] | 391 | head->link.next = next; |
---|
| 392 | next->link.prev = head; |
---|
[dca5802] | 393 | node->link.[next, prev] = 0p; |
---|
| 394 | |
---|
| 395 | // Update head time stamp |
---|
| 396 | this.before.link.ts = next->link.ts; |
---|
[7768b8d] | 397 | |
---|
[dca5802] | 398 | // Update stats |
---|
[7768b8d] | 399 | #ifndef __CFA_NO_SCHED_STATS__ |
---|
| 400 | this.stat.diff--; |
---|
| 401 | this.stat.pop ++; |
---|
| 402 | #endif |
---|
| 403 | |
---|
[dca5802] | 404 | // Check if we emptied list and return accordingly |
---|
[504a7dc] | 405 | /* paranoid */ verify(tail(this)->link.next == 0p); |
---|
| 406 | /* paranoid */ verify(head(this)->link.prev == 0p); |
---|
[7768b8d] | 407 | if(next == tail) { |
---|
[dca5802] | 408 | /* paranoid */ verify(this.before.link.ts == 0); |
---|
| 409 | /* paranoid */ verify(tail(this)->link.prev == head(this)); |
---|
| 410 | /* paranoid */ verify(head(this)->link.next == tail(this)); |
---|
[7768b8d] | 411 | return [node, true]; |
---|
| 412 | } |
---|
| 413 | else { |
---|
[dca5802] | 414 | /* paranoid */ verify(next->link.ts != 0); |
---|
[504a7dc] | 415 | /* paranoid */ verify(tail(this)->link.prev != head(this)); |
---|
| 416 | /* paranoid */ verify(head(this)->link.next != tail(this)); |
---|
[dca5802] | 417 | /* paranoid */ verify(this.before.link.ts != 0); |
---|
[7768b8d] | 418 | return [node, false]; |
---|
| 419 | } |
---|
| 420 | } |
---|
| 421 | |
---|
[dca5802] | 422 | // Check whether or not list is empty |
---|
| 423 | static inline bool is_empty(__intrusive_lane_t & this) { |
---|
[1b143de] | 424 | // Cannot verify here since it may not be locked |
---|
[dca5802] | 425 | return this.before.link.ts == 0; |
---|
| 426 | } |
---|
| 427 | |
---|
| 428 | // Return the timestamp |
---|
| 429 | static inline unsigned long long ts(__intrusive_lane_t & this) { |
---|
[1b143de] | 430 | // Cannot verify here since it may not be locked |
---|
[b798713] | 431 | return this.before.link.ts; |
---|
| 432 | } |
---|
| 433 | |
---|
| 434 | //======================================================================= |
---|
| 435 | // Cforall Reqdy Queue used by ready queue |
---|
| 436 | //======================================================================= |
---|
| 437 | |
---|
[dca5802] | 438 | // Thread local mirror of ready queue statistics |
---|
| 439 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[b798713] | 440 | static __attribute__((aligned(128))) thread_local struct { |
---|
| 441 | struct { |
---|
| 442 | struct { |
---|
| 443 | size_t attempt; |
---|
| 444 | size_t success; |
---|
| 445 | } push; |
---|
| 446 | struct { |
---|
| 447 | size_t maskrds; |
---|
| 448 | size_t attempt; |
---|
| 449 | size_t success; |
---|
| 450 | } pop; |
---|
| 451 | } pick; |
---|
| 452 | struct { |
---|
| 453 | size_t value; |
---|
| 454 | size_t count; |
---|
[dca5802] | 455 | } used; |
---|
[b798713] | 456 | } tls = { |
---|
| 457 | /* pick */{ |
---|
| 458 | /* push */{ 0, 0 }, |
---|
| 459 | /* pop */{ 0, 0, 0 }, |
---|
| 460 | }, |
---|
[dca5802] | 461 | /* used */{ 0, 0 } |
---|
[b798713] | 462 | }; |
---|
[dca5802] | 463 | #endif |
---|
[b798713] | 464 | |
---|
| 465 | //----------------------------------------------------------------------- |
---|
| 466 | |
---|
| 467 | void ?{}(__ready_queue_t & this) with (this) { |
---|
[dca5802] | 468 | used.count = 0; |
---|
| 469 | for( i ; __cfa_lane_mask_size ) { |
---|
| 470 | used.mask[i] = 0; |
---|
[b798713] | 471 | } |
---|
| 472 | |
---|
[dca5802] | 473 | lanes.data = alloc(4); |
---|
[b798713] | 474 | for( i; 4 ) { |
---|
[dca5802] | 475 | (lanes.data[i]){}; |
---|
[b798713] | 476 | } |
---|
[dca5802] | 477 | lanes.count = 4; |
---|
[b798713] | 478 | |
---|
| 479 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 480 | global_stats.pick.push.attempt = 0; |
---|
| 481 | global_stats.pick.push.success = 0; |
---|
| 482 | global_stats.pick.pop .maskrds = 0; |
---|
| 483 | global_stats.pick.pop .attempt = 0; |
---|
| 484 | global_stats.pick.pop .success = 0; |
---|
| 485 | |
---|
[dca5802] | 486 | global_stats.used.value = 0; |
---|
| 487 | global_stats.used.count = 0; |
---|
[b798713] | 488 | #endif |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | void ^?{}(__ready_queue_t & this) with (this) { |
---|
[dca5802] | 492 | verify( 4 == lanes.count ); |
---|
| 493 | verify( 0 == used .count ); |
---|
[b798713] | 494 | |
---|
| 495 | for( i; 4 ) { |
---|
[dca5802] | 496 | ^(lanes.data[i]){}; |
---|
[b798713] | 497 | } |
---|
[dca5802] | 498 | free(lanes.data); |
---|
[b798713] | 499 | |
---|
| 500 | |
---|
| 501 | #if defined(__CFA_WITH_VERIFY__) |
---|
[dca5802] | 502 | for( i ; __cfa_lane_mask_size ) { |
---|
| 503 | assert( 0 == used.mask[i] ); |
---|
[b798713] | 504 | } |
---|
| 505 | #endif |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | //----------------------------------------------------------------------- |
---|
[dca5802] | 509 | enum mask_strictness { |
---|
| 510 | STRICT, |
---|
| 511 | NOCHECK |
---|
| 512 | }; |
---|
| 513 | |
---|
| 514 | // Set a given bit in the bit mask array |
---|
| 515 | // strictness determines of the bit had to be cleared before |
---|
| 516 | static inline void mask_set(__cfa_readyQ_mask_t * mask, unsigned index, mask_strictness strict) { |
---|
| 517 | // Extract the array and bit indexes |
---|
| 518 | __cfa_readyQ_mask_t word; |
---|
| 519 | __cfa_readyQ_mask_t bit; |
---|
| 520 | [bit, word] = extract(index); |
---|
| 521 | |
---|
[1b143de] | 522 | __cfadbg_print_safe(ready_queue, "Kernel : Ready queue extracted index %u as [bit %llu, word %llu]\n", index, bit, word); |
---|
| 523 | |
---|
[dca5802] | 524 | // Conditional check |
---|
| 525 | verifyf( |
---|
[504a7dc] | 526 | strict != STRICT || // Conditional check if it was expected to be cleared |
---|
[dca5802] | 527 | ((mask[word] & (1ull << bit)) == 0), |
---|
| 528 | "Before set %llu:%llu (%u), %llx & %llx", word, bit, index, mask[word], (1ull << bit) |
---|
| 529 | ); |
---|
| 530 | |
---|
| 531 | // Atomically set the bit |
---|
| 532 | __attribute__((unused)) bool ret = __atomic_bts(&mask[word], bit); |
---|
| 533 | |
---|
| 534 | // Conditional check |
---|
| 535 | verifyf( |
---|
[504a7dc] | 536 | strict != STRICT || // Conditional check if it was expected to be cleared |
---|
[dca5802] | 537 | !ret, |
---|
| 538 | "Bit was not set but bts returned true" |
---|
| 539 | ); |
---|
| 540 | |
---|
| 541 | // Unconditional check |
---|
| 542 | verifyf( |
---|
| 543 | (mask[word] & (1ull << bit)) != 0, |
---|
| 544 | "After set %llu:%llu (%u), %llx & %llx", word, bit, index, mask[word], (1ull << bit) |
---|
| 545 | ); |
---|
| 546 | } |
---|
| 547 | |
---|
| 548 | static inline void mask_clear(__cfa_readyQ_mask_t * mask, unsigned index, mask_strictness strict) { |
---|
| 549 | // Extract the array and bit indexes |
---|
| 550 | __cfa_readyQ_mask_t word; |
---|
| 551 | __cfa_readyQ_mask_t bit; |
---|
| 552 | [bit, word] = extract(index); |
---|
| 553 | |
---|
| 554 | // Conditional check |
---|
| 555 | verifyf( |
---|
[1b143de] | 556 | strict != STRICT || // Conditional check if it was expected to be set |
---|
[dca5802] | 557 | ((mask[word] & (1ull << bit)) != 0), |
---|
| 558 | "Before clear %llu:%llu (%u), %llx & %llx", word, bit, index, mask[word], (1ull << bit) |
---|
| 559 | ); |
---|
| 560 | |
---|
| 561 | // Atomically clear the bit |
---|
| 562 | __attribute__((unused)) bool ret = __atomic_btr(&mask[word], bit); |
---|
[b798713] | 563 | |
---|
[dca5802] | 564 | // Conditional check |
---|
| 565 | verifyf( |
---|
[1b143de] | 566 | strict != STRICT || // Conditional check if it was expected to be cleared |
---|
[dca5802] | 567 | ret, |
---|
| 568 | "Bit was set but btr returned false" |
---|
| 569 | ); |
---|
| 570 | |
---|
| 571 | // Unconditional check |
---|
| 572 | verifyf( |
---|
| 573 | (mask[word] & (1ull << bit)) == 0, |
---|
| 574 | "After clear %llu:%llu (%u), %llx & %llx", word, bit, index, mask[word], (1ull << bit) |
---|
| 575 | ); |
---|
| 576 | } |
---|
| 577 | |
---|
| 578 | //----------------------------------------------------------------------- |
---|
[504a7dc] | 579 | __attribute__((hot)) bool push(struct cluster * cltr, struct $thread * thrd) with (cltr->ready_queue) { |
---|
[1b143de] | 580 | __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p (mask %llu)\n", thrd, cltr, used.mask[0]); |
---|
| 581 | |
---|
[dca5802] | 582 | // write timestamp |
---|
[b798713] | 583 | thrd->link.ts = rdtscl(); |
---|
| 584 | |
---|
[dca5802] | 585 | // Try to pick a lane and lock it |
---|
| 586 | unsigned i; |
---|
| 587 | do { |
---|
| 588 | // Pick the index of a lane |
---|
[504a7dc] | 589 | i = __tls_rand() % lanes.count; |
---|
[b798713] | 590 | |
---|
| 591 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 592 | tls.pick.push.attempt++; |
---|
| 593 | #endif |
---|
| 594 | |
---|
| 595 | // If we can't lock it retry |
---|
[dca5802] | 596 | } while( !__atomic_try_acquire( &lanes.data[i].lock ) ); |
---|
[b798713] | 597 | |
---|
[dca5802] | 598 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 599 | /* paranoid */ verify(lanes.data[i].last_id == -1u); |
---|
| 600 | /* paranoid */ lanes.data[i].last_id = kernelTLS.this_processor->id; |
---|
| 601 | #endif |
---|
[b798713] | 602 | |
---|
[dca5802] | 603 | __attribute__((unused)) size_t num = __atomic_load_n( &used.count, __ATOMIC_RELAXED ); |
---|
| 604 | bool first = false; |
---|
| 605 | |
---|
| 606 | // Actually push it |
---|
| 607 | bool lane_first = push(lanes.data[i], thrd); |
---|
| 608 | |
---|
| 609 | // If this lane used to be empty we need to do more |
---|
| 610 | if(lane_first) { |
---|
[1b143de] | 611 | // Update the bit mask |
---|
| 612 | mask_set((__cfa_readyQ_mask_t *)used.mask, i, STRICT); |
---|
| 613 | |
---|
[dca5802] | 614 | // Update the global count |
---|
| 615 | size_t ret = __atomic_fetch_add( &used.count, 1z, __ATOMIC_SEQ_CST); |
---|
| 616 | |
---|
[504a7dc] | 617 | // Check if the entire queue used to be empty |
---|
[dca5802] | 618 | first = (ret == 0); |
---|
[b798713] | 619 | } |
---|
[dca5802] | 620 | |
---|
| 621 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 622 | /* paranoid */ verifyf( used.count <= lanes.count, "Non-empty count (%zu) exceeds actual count (%zu)\n", used.count, lanes.count ); |
---|
| 623 | /* paranoid */ verifyf( lanes.data[i].last_id == kernelTLS.this_processor->id, "Expected last processor to lock queue %u to be %u, was %u\n", i, lanes.data[i].last_id, kernelTLS.this_processor->id ); |
---|
| 624 | /* paranoid */ verifyf( lanes.data[i].lock, "List %u is not locked\n", i ); |
---|
| 625 | /* paranoid */ lanes.data[i].last_id = -1u; |
---|
| 626 | #endif |
---|
| 627 | |
---|
| 628 | // Unlock and return |
---|
| 629 | __atomic_unlock( &lanes.data[i].lock ); |
---|
| 630 | |
---|
[1b143de] | 631 | __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); |
---|
| 632 | |
---|
[dca5802] | 633 | // Update statistics |
---|
| 634 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 635 | tls.pick.push.success++; |
---|
| 636 | tls.used.value += num; |
---|
| 637 | tls.used.count += 1; |
---|
| 638 | #endif |
---|
| 639 | |
---|
| 640 | // return whether or not the list was empty before this push |
---|
| 641 | return first; |
---|
[b798713] | 642 | } |
---|
| 643 | |
---|
| 644 | //----------------------------------------------------------------------- |
---|
[dca5802] | 645 | // Given 2 indexes, pick the list with the oldest push an try to pop from it |
---|
[504a7dc] | 646 | static struct $thread * try_pop(struct cluster * cltr, unsigned i, unsigned j) with (cltr->ready_queue) { |
---|
[b798713] | 647 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 648 | tls.pick.pop.attempt++; |
---|
| 649 | #endif |
---|
| 650 | |
---|
| 651 | // Pick the bet list |
---|
| 652 | int w = i; |
---|
[dca5802] | 653 | if( __builtin_expect(!is_empty(lanes.data[j]), true) ) { |
---|
| 654 | w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j; |
---|
[b798713] | 655 | } |
---|
| 656 | |
---|
[dca5802] | 657 | // Get relevant elements locally |
---|
| 658 | __intrusive_lane_t & lane = lanes.data[w]; |
---|
| 659 | |
---|
[b798713] | 660 | // If list looks empty retry |
---|
[dca5802] | 661 | if( is_empty(lane) ) return 0p; |
---|
[b798713] | 662 | |
---|
| 663 | // If we can't get the lock retry |
---|
[dca5802] | 664 | if( !__atomic_try_acquire(&lane.lock) ) return 0p; |
---|
[b798713] | 665 | |
---|
[dca5802] | 666 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 667 | /* paranoid */ verify(lane.last_id == -1u); |
---|
| 668 | /* paranoid */ lane.last_id = kernelTLS.this_processor->id; |
---|
| 669 | #endif |
---|
[b798713] | 670 | |
---|
| 671 | |
---|
| 672 | // If list is empty, unlock and retry |
---|
[dca5802] | 673 | if( is_empty(lane) ) { |
---|
| 674 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 675 | /* paranoid */ verify(lane.last_id == kernelTLS.this_processor->id); |
---|
| 676 | /* paranoid */ lane.last_id = -1u; |
---|
| 677 | #endif |
---|
| 678 | |
---|
| 679 | __atomic_unlock(&lane.lock); |
---|
[b798713] | 680 | return 0p; |
---|
| 681 | } |
---|
| 682 | |
---|
| 683 | // Actually pop the list |
---|
[504a7dc] | 684 | struct $thread * thrd; |
---|
[b798713] | 685 | bool emptied; |
---|
[dca5802] | 686 | [thrd, emptied] = pop(lane); |
---|
[b798713] | 687 | |
---|
[dca5802] | 688 | /* paranoid */ verify(thrd); |
---|
| 689 | /* paranoid */ verify(lane.last_id == kernelTLS.this_processor->id); |
---|
| 690 | /* paranoid */ verify(lane.lock); |
---|
[b798713] | 691 | |
---|
[dca5802] | 692 | // If this was the last element in the lane |
---|
[b798713] | 693 | if(emptied) { |
---|
[dca5802] | 694 | // Update the global count |
---|
| 695 | __atomic_fetch_sub( &used.count, 1z, __ATOMIC_SEQ_CST); |
---|
| 696 | |
---|
| 697 | // Update the bit mask |
---|
| 698 | mask_clear((__cfa_readyQ_mask_t *)used.mask, w, STRICT); |
---|
[b798713] | 699 | } |
---|
| 700 | |
---|
[dca5802] | 701 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 702 | /* paranoid */ verify(lane.last_id == kernelTLS.this_processor->id); |
---|
| 703 | /* paranoid */ lane.last_id = -1u; |
---|
| 704 | #endif |
---|
| 705 | |
---|
| 706 | // For statistics, check the count before we release the lock |
---|
| 707 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 708 | int num = __atomic_load_n( &used.count, __ATOMIC_RELAXED ); |
---|
| 709 | #endif |
---|
[b798713] | 710 | |
---|
| 711 | // Unlock and return |
---|
[dca5802] | 712 | __atomic_unlock(&lane.lock); |
---|
[b798713] | 713 | |
---|
[dca5802] | 714 | // Update statistics |
---|
[b798713] | 715 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 716 | tls.pick.pop.success++; |
---|
[dca5802] | 717 | tls.used.value += num; |
---|
| 718 | tls.used.count += 1; |
---|
[b798713] | 719 | #endif |
---|
| 720 | |
---|
[dca5802] | 721 | // return the popped thread |
---|
[b798713] | 722 | return thrd; |
---|
| 723 | } |
---|
| 724 | |
---|
[dca5802] | 725 | // Pop from the ready queue from a given cluster |
---|
[504a7dc] | 726 | __attribute__((hot)) $thread * pop(struct cluster * cltr) with (cltr->ready_queue) { |
---|
[dca5802] | 727 | /* paranoid */ verify( lanes.count > 0 ); |
---|
[b798713] | 728 | |
---|
[dca5802] | 729 | // As long as the list is not empty, try finding a lane that isn't empty and pop from it |
---|
| 730 | while( __atomic_load_n( &used.count, __ATOMIC_RELAXED ) != 0) { |
---|
| 731 | #if !defined(__CFA_READQ_NO_BITMASK__) |
---|
| 732 | // If using bit masks |
---|
| 733 | #if !defined(__CFA_NO_SCHED_STATS__) |
---|
| 734 | tls.pick.pop.maskrds++; |
---|
| 735 | #endif |
---|
[b798713] | 736 | |
---|
[dca5802] | 737 | // Pick two lists at random |
---|
[504a7dc] | 738 | unsigned ri = __tls_rand(); |
---|
| 739 | unsigned rj = __tls_rand(); |
---|
[b798713] | 740 | |
---|
[dca5802] | 741 | // Find which __cfa_readyQ_mask_t the two lists belong |
---|
| 742 | unsigned num = ((__atomic_load_n( &lanes.count, __ATOMIC_RELAXED ) - 1) >> 6) + 1; |
---|
| 743 | unsigned wdxi = (ri >> 6u) % num; |
---|
| 744 | unsigned wdxj = (rj >> 6u) % num; |
---|
[b798713] | 745 | |
---|
[dca5802] | 746 | // Get the actual __cfa_readyQ_mask_t |
---|
| 747 | size_t maski = __atomic_load_n( &used.mask[wdxi], __ATOMIC_RELAXED ); |
---|
| 748 | size_t maskj = __atomic_load_n( &used.mask[wdxj], __ATOMIC_RELAXED ); |
---|
[b798713] | 749 | |
---|
[dca5802] | 750 | // If both of these masks are empty, retry |
---|
| 751 | if(maski == 0 && maskj == 0) continue; |
---|
[b798713] | 752 | |
---|
[dca5802] | 753 | // Pick one of the non-zero bits in the masks and get the bit indexes |
---|
| 754 | unsigned bi = rand_bit(ri, maski); |
---|
| 755 | unsigned bj = rand_bit(rj, maskj); |
---|
[b798713] | 756 | |
---|
[dca5802] | 757 | // some checks |
---|
| 758 | /* paranoid */ verifyf(bi < 64, "%zu %u", maski, bi); |
---|
| 759 | /* paranoid */ verifyf(bj < 64, "%zu %u", maskj, bj); |
---|
[b798713] | 760 | |
---|
[dca5802] | 761 | // get the general list index |
---|
| 762 | unsigned i = bi | (wdxi << 6); |
---|
| 763 | unsigned j = bj | (wdxj << 6); |
---|
[b798713] | 764 | |
---|
[dca5802] | 765 | // some more checks |
---|
| 766 | /* paranoid */ verifyf(i < lanes.count, "%u", wdxi << 6); |
---|
| 767 | /* paranoid */ verifyf(j < lanes.count, "%u", wdxj << 6); |
---|
[b798713] | 768 | |
---|
[dca5802] | 769 | // try popping from the 2 picked lists |
---|
[504a7dc] | 770 | struct $thread * thrd = try_pop(cltr, i, j); |
---|
[b798713] | 771 | if(thrd) return thrd; |
---|
| 772 | #else |
---|
| 773 | // Pick two lists at random |
---|
[504a7dc] | 774 | int i = __tls_rand() % __atomic_load_n( &lanes.count, __ATOMIC_RELAXED ); |
---|
| 775 | int j = __tls_rand() % __atomic_load_n( &lanes.count, __ATOMIC_RELAXED ); |
---|
[b798713] | 776 | |
---|
[dca5802] | 777 | // try popping from the 2 picked lists |
---|
[504a7dc] | 778 | struct $thread * thrd = try_pop(cltr, i, j); |
---|
[b798713] | 779 | if(thrd) return thrd; |
---|
| 780 | #endif |
---|
| 781 | } |
---|
| 782 | |
---|
[dca5802] | 783 | // All lanes where empty return 0p |
---|
[b798713] | 784 | return 0p; |
---|
| 785 | } |
---|
| 786 | |
---|
| 787 | //----------------------------------------------------------------------- |
---|
| 788 | |
---|
| 789 | static void check( __ready_queue_t & q ) with (q) { |
---|
| 790 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 791 | { |
---|
| 792 | int idx = 0; |
---|
[dca5802] | 793 | for( w ; __cfa_lane_mask_size ) { |
---|
[b798713] | 794 | for( b ; 8 * sizeof(__cfa_readyQ_mask_t) ) { |
---|
[dca5802] | 795 | bool is_empty = idx < lanes.count ? (ts(lanes.data[idx]) == 0) : true; |
---|
| 796 | bool should_be_empty = 0 == (used.mask[w] & (1z << b)); |
---|
[b798713] | 797 | assertf(should_be_empty == is_empty, "Inconsistent list %d, mask expect : %d, actual is got %d", idx, should_be_empty, (bool)is_empty); |
---|
[dca5802] | 798 | assert(__cfa_max_lanes > idx); |
---|
[b798713] | 799 | idx++; |
---|
| 800 | } |
---|
| 801 | } |
---|
| 802 | } |
---|
| 803 | |
---|
| 804 | { |
---|
[dca5802] | 805 | for( idx ; lanes.count ) { |
---|
| 806 | __intrusive_lane_t & sl = lanes.data[idx]; |
---|
| 807 | assert(!lanes.data[idx].lock); |
---|
[b798713] | 808 | |
---|
| 809 | assert(head(sl)->link.prev == 0p ); |
---|
| 810 | assert(head(sl)->link.next->link.prev == head(sl) ); |
---|
| 811 | assert(tail(sl)->link.next == 0p ); |
---|
| 812 | assert(tail(sl)->link.prev->link.next == tail(sl) ); |
---|
| 813 | |
---|
| 814 | if(sl.before.link.ts == 0l) { |
---|
| 815 | assert(tail(sl)->link.prev == head(sl)); |
---|
| 816 | assert(head(sl)->link.next == tail(sl)); |
---|
[1b143de] | 817 | } else { |
---|
| 818 | assert(tail(sl)->link.prev != head(sl)); |
---|
| 819 | assert(head(sl)->link.next != tail(sl)); |
---|
[b798713] | 820 | } |
---|
| 821 | } |
---|
| 822 | } |
---|
| 823 | #endif |
---|
| 824 | } |
---|
| 825 | |
---|
| 826 | // Call this function of the intrusive list was moved using memcpy |
---|
[dca5802] | 827 | // fixes the list so that the pointers back to anchors aren't left dangling |
---|
| 828 | static inline void fix(__intrusive_lane_t & ll) { |
---|
| 829 | // if the list is not empty then follow he pointer and fix its reverse |
---|
| 830 | if(!is_empty(ll)) { |
---|
[b798713] | 831 | head(ll)->link.next->link.prev = head(ll); |
---|
| 832 | tail(ll)->link.prev->link.next = tail(ll); |
---|
| 833 | } |
---|
| 834 | // Otherwise just reset the list |
---|
| 835 | else { |
---|
[dca5802] | 836 | verify(tail(ll)->link.next == 0p); |
---|
[b798713] | 837 | tail(ll)->link.prev = head(ll); |
---|
| 838 | head(ll)->link.next = tail(ll); |
---|
[dca5802] | 839 | verify(head(ll)->link.prev == 0p); |
---|
[b798713] | 840 | } |
---|
| 841 | } |
---|
| 842 | |
---|
[dca5802] | 843 | // Grow the ready queue |
---|
[b798713] | 844 | void ready_queue_grow (struct cluster * cltr) { |
---|
[dca5802] | 845 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
[b798713] | 846 | uint_fast32_t last_size = ready_mutate_lock( *cltr ); |
---|
[dca5802] | 847 | |
---|
[504a7dc] | 848 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n"); |
---|
[b798713] | 849 | |
---|
[dca5802] | 850 | // Make sure that everything is consistent |
---|
| 851 | /* paranoid */ check( cltr->ready_queue ); |
---|
| 852 | |
---|
| 853 | // grow the ready queue |
---|
[b798713] | 854 | with( cltr->ready_queue ) { |
---|
[dca5802] | 855 | size_t ncount = lanes.count; |
---|
[b798713] | 856 | |
---|
| 857 | // Check that we have some space left |
---|
[dca5802] | 858 | if(ncount + 4 >= __cfa_max_lanes) abort("Program attempted to create more than maximum number of Ready Queues (%zu)", __cfa_max_lanes); |
---|
[b798713] | 859 | |
---|
[dca5802] | 860 | // increase count |
---|
[b798713] | 861 | ncount += 4; |
---|
| 862 | |
---|
[dca5802] | 863 | // Allocate new array (uses realloc and memcpies the data) |
---|
| 864 | lanes.data = alloc(lanes.data, ncount); |
---|
[b798713] | 865 | |
---|
| 866 | // Fix the moved data |
---|
[dca5802] | 867 | for( idx; (size_t)lanes.count ) { |
---|
| 868 | fix(lanes.data[idx]); |
---|
[b798713] | 869 | } |
---|
| 870 | |
---|
| 871 | // Construct new data |
---|
[dca5802] | 872 | for( idx; (size_t)lanes.count ~ ncount) { |
---|
| 873 | (lanes.data[idx]){}; |
---|
[b798713] | 874 | } |
---|
| 875 | |
---|
| 876 | // Update original |
---|
[dca5802] | 877 | lanes.count = ncount; |
---|
| 878 | |
---|
| 879 | // fields in 'used' don't need to change when growing |
---|
[b798713] | 880 | } |
---|
| 881 | |
---|
| 882 | // Make sure that everything is consistent |
---|
[dca5802] | 883 | /* paranoid */ check( cltr->ready_queue ); |
---|
| 884 | |
---|
[504a7dc] | 885 | __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n"); |
---|
[dca5802] | 886 | |
---|
| 887 | // Unlock the RWlock |
---|
[b798713] | 888 | ready_mutate_unlock( *cltr, last_size ); |
---|
| 889 | } |
---|
| 890 | |
---|
[dca5802] | 891 | // Shrink the ready queue |
---|
[b798713] | 892 | void ready_queue_shrink(struct cluster * cltr) { |
---|
[dca5802] | 893 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
[b798713] | 894 | uint_fast32_t last_size = ready_mutate_lock( *cltr ); |
---|
[dca5802] | 895 | |
---|
[504a7dc] | 896 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n"); |
---|
[dca5802] | 897 | |
---|
| 898 | // Make sure that everything is consistent |
---|
| 899 | /* paranoid */ check( cltr->ready_queue ); |
---|
| 900 | |
---|
[b798713] | 901 | with( cltr->ready_queue ) { |
---|
[dca5802] | 902 | // Make sure that the total thread count stays the same |
---|
[c84b4be] | 903 | #if defined(__CFA_WITH_VERIFY__) |
---|
| 904 | size_t nthreads = 0; |
---|
[dca5802] | 905 | for( idx; (size_t)lanes.count ) { |
---|
| 906 | nthreads += lanes.data[idx].count; |
---|
[c84b4be] | 907 | } |
---|
| 908 | #endif |
---|
| 909 | |
---|
[dca5802] | 910 | size_t ocount = lanes.count; |
---|
[b798713] | 911 | // Check that we have some space left |
---|
| 912 | if(ocount < 8) abort("Program attempted to destroy more Ready Queues than were created"); |
---|
| 913 | |
---|
[dca5802] | 914 | // reduce the actual count so push doesn't use the old queues |
---|
| 915 | lanes.count -= 4; |
---|
| 916 | verify(ocount > lanes.count); |
---|
| 917 | |
---|
| 918 | // for printing count the number of displaced threads |
---|
[504a7dc] | 919 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__) |
---|
[dca5802] | 920 | __attribute__((unused)) size_t displaced = 0; |
---|
| 921 | #endif |
---|
[b798713] | 922 | |
---|
| 923 | // redistribute old data |
---|
[dca5802] | 924 | for( idx; (size_t)lanes.count ~ ocount) { |
---|
| 925 | // Lock is not strictly needed but makes checking invariants much easier |
---|
[1b143de] | 926 | __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock); |
---|
[b798713] | 927 | verify(locked); |
---|
[dca5802] | 928 | |
---|
| 929 | // As long as we can pop from this lane to push the threads somewhere else in the queue |
---|
| 930 | while(!is_empty(lanes.data[idx])) { |
---|
[504a7dc] | 931 | struct $thread * thrd; |
---|
[b798713] | 932 | __attribute__((unused)) bool _; |
---|
[dca5802] | 933 | [thrd, _] = pop(lanes.data[idx]); |
---|
| 934 | |
---|
[b798713] | 935 | push(cltr, thrd); |
---|
[dca5802] | 936 | |
---|
| 937 | // for printing count the number of displaced threads |
---|
[504a7dc] | 938 | #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__) |
---|
[dca5802] | 939 | displaced++; |
---|
| 940 | #endif |
---|
[b798713] | 941 | } |
---|
| 942 | |
---|
[dca5802] | 943 | mask_clear((__cfa_readyQ_mask_t *)used.mask, idx, NOCHECK); |
---|
| 944 | |
---|
| 945 | // Unlock the lane |
---|
| 946 | __atomic_unlock(&lanes.data[idx].lock); |
---|
[b798713] | 947 | |
---|
| 948 | // TODO print the queue statistics here |
---|
| 949 | |
---|
[dca5802] | 950 | ^(lanes.data[idx]){}; |
---|
[b798713] | 951 | } |
---|
| 952 | |
---|
[504a7dc] | 953 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced); |
---|
[c84b4be] | 954 | |
---|
[dca5802] | 955 | // recompute the used.count instead of maintaining it |
---|
| 956 | used.count = 0; |
---|
| 957 | for( i ; __cfa_lane_mask_size ) { |
---|
| 958 | used.count += __builtin_popcountl(used.mask[i]); |
---|
[b798713] | 959 | } |
---|
| 960 | |
---|
[dca5802] | 961 | // Allocate new array (uses realloc and memcpies the data) |
---|
| 962 | lanes.data = alloc(lanes.data, lanes.count); |
---|
[b798713] | 963 | |
---|
| 964 | // Fix the moved data |
---|
[dca5802] | 965 | for( idx; (size_t)lanes.count ) { |
---|
| 966 | fix(lanes.data[idx]); |
---|
[b798713] | 967 | } |
---|
[c84b4be] | 968 | |
---|
[dca5802] | 969 | // Make sure that the total thread count stayed the same |
---|
[c84b4be] | 970 | #if defined(__CFA_WITH_VERIFY__) |
---|
[dca5802] | 971 | for( idx; (size_t)lanes.count ) { |
---|
| 972 | nthreads -= lanes.data[idx].count; |
---|
[c84b4be] | 973 | } |
---|
[dca5802] | 974 | verifyf(nthreads == 0, "Shrinking changed number of threads"); |
---|
[c84b4be] | 975 | #endif |
---|
[b798713] | 976 | } |
---|
| 977 | |
---|
| 978 | // Make sure that everything is consistent |
---|
[dca5802] | 979 | /* paranoid */ check( cltr->ready_queue ); |
---|
| 980 | |
---|
[504a7dc] | 981 | __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n"); |
---|
[dca5802] | 982 | |
---|
| 983 | // Unlock the RWlock |
---|
[b798713] | 984 | ready_mutate_unlock( *cltr, last_size ); |
---|
| 985 | } |
---|
| 986 | |
---|
| 987 | //----------------------------------------------------------------------- |
---|
| 988 | |
---|
| 989 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 990 | void stats_tls_tally(struct cluster * cltr) with (cltr->ready_queue) { |
---|
| 991 | __atomic_fetch_add( &global_stats.pick.push.attempt, tls.pick.push.attempt, __ATOMIC_SEQ_CST ); |
---|
| 992 | __atomic_fetch_add( &global_stats.pick.push.success, tls.pick.push.success, __ATOMIC_SEQ_CST ); |
---|
| 993 | __atomic_fetch_add( &global_stats.pick.pop .maskrds, tls.pick.pop .maskrds, __ATOMIC_SEQ_CST ); |
---|
| 994 | __atomic_fetch_add( &global_stats.pick.pop .attempt, tls.pick.pop .attempt, __ATOMIC_SEQ_CST ); |
---|
| 995 | __atomic_fetch_add( &global_stats.pick.pop .success, tls.pick.pop .success, __ATOMIC_SEQ_CST ); |
---|
| 996 | |
---|
[dca5802] | 997 | __atomic_fetch_add( &global_stats.used.value, tls.used.value, __ATOMIC_SEQ_CST ); |
---|
| 998 | __atomic_fetch_add( &global_stats.used.count, tls.used.count, __ATOMIC_SEQ_CST ); |
---|
[b798713] | 999 | } |
---|
| 1000 | #endif |
---|