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

ADT ast-experimental enum forall-pointer-decay pthread-emulation qualifiedEnum
Last change on this file since b5f17e14 was a2a4566, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added new ready-queue that uses per-thread queues but with some cpu awarness

  • Property mode set to 100644
File size: 40.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2019 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// ready_queue.cfa --
8//
9// Author : Thierry Delisle
10// Created On : Mon Nov dd 16:29:18 2019
11// Last Modified By :
12// Last Modified On :
13// Update Count :
14//
15
16#define __cforall_thread__
17#define _GNU_SOURCE
18
19// #define __CFA_DEBUG_PRINT_READY_QUEUE__
20
21
22// #define USE_RELAXED_FIFO
23// #define USE_WORK_STEALING
24// #define USE_CPU_WORK_STEALING
25#define USE_AWARE_STEALING
26
27#include "bits/defs.hfa"
28#include "device/cpu.hfa"
29#include "kernel_private.hfa"
30
31#include "stdlib.hfa"
32#include "limits.hfa"
33#include "math.hfa"
34
35#include <errno.h>
36#include <unistd.h>
37
38extern "C" {
39 #include <sys/syscall.h> // __NR_xxx
40}
41
42#include "ready_subqueue.hfa"
43
44static const size_t cache_line_size = 64;
45
46#if !defined(__CFA_NO_STATISTICS__)
47 #define __STATS(...) __VA_ARGS__
48#else
49 #define __STATS(...)
50#endif
51
52// No overriden function, no environment variable, no define
53// fall back to a magic number
54#ifndef __CFA_MAX_PROCESSORS__
55 #define __CFA_MAX_PROCESSORS__ 1024
56#endif
57
58#if defined(USE_AWARE_STEALING)
59 #define READYQ_SHARD_FACTOR 2
60 #define SEQUENTIAL_SHARD 2
61#elif defined(USE_CPU_WORK_STEALING)
62 #define READYQ_SHARD_FACTOR 2
63#elif defined(USE_RELAXED_FIFO)
64 #define BIAS 4
65 #define READYQ_SHARD_FACTOR 4
66 #define SEQUENTIAL_SHARD 1
67#elif defined(USE_WORK_STEALING)
68 #define READYQ_SHARD_FACTOR 2
69 #define SEQUENTIAL_SHARD 2
70#else
71 #error no scheduling strategy selected
72#endif
73
74static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats));
75static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats));
76static inline struct thread$ * search(struct cluster * cltr);
77static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred);
78
79
80// returns the maximum number of processors the RWLock support
81__attribute__((weak)) unsigned __max_processors() {
82 const char * max_cores_s = getenv("CFA_MAX_PROCESSORS");
83 if(!max_cores_s) {
84 __cfadbg_print_nolock(ready_queue, "No CFA_MAX_PROCESSORS in ENV\n");
85 return __CFA_MAX_PROCESSORS__;
86 }
87
88 char * endptr = 0p;
89 long int max_cores_l = strtol(max_cores_s, &endptr, 10);
90 if(max_cores_l < 1 || max_cores_l > 65535) {
91 __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS out of range : %ld\n", max_cores_l);
92 return __CFA_MAX_PROCESSORS__;
93 }
94 if('\0' != *endptr) {
95 __cfadbg_print_nolock(ready_queue, "CFA_MAX_PROCESSORS not a decimal number : %s\n", max_cores_s);
96 return __CFA_MAX_PROCESSORS__;
97 }
98
99 return max_cores_l;
100}
101
102#if defined(CFA_HAVE_LINUX_LIBRSEQ)
103 // No forward declaration needed
104 #define __kernel_rseq_register rseq_register_current_thread
105 #define __kernel_rseq_unregister rseq_unregister_current_thread
106#elif defined(CFA_HAVE_LINUX_RSEQ_H)
107 static void __kernel_raw_rseq_register (void);
108 static void __kernel_raw_rseq_unregister(void);
109
110 #define __kernel_rseq_register __kernel_raw_rseq_register
111 #define __kernel_rseq_unregister __kernel_raw_rseq_unregister
112#else
113 // No forward declaration needed
114 // No initialization needed
115 static inline void noop(void) {}
116
117 #define __kernel_rseq_register noop
118 #define __kernel_rseq_unregister noop
119#endif
120
121//=======================================================================
122// Cluster wide reader-writer lock
123//=======================================================================
124void ?{}(__scheduler_RWLock_t & this) {
125 this.max = __max_processors();
126 this.alloc = 0;
127 this.ready = 0;
128 this.data = alloc(this.max);
129 this.write_lock = false;
130
131 /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.alloc), &this.alloc));
132 /*paranoid*/ verify(__atomic_is_lock_free(sizeof(this.ready), &this.ready));
133
134}
135void ^?{}(__scheduler_RWLock_t & this) {
136 free(this.data);
137}
138
139
140//=======================================================================
141// Lock-Free registering/unregistering of threads
142unsigned register_proc_id( void ) with(*__scheduler_lock) {
143 __kernel_rseq_register();
144
145 bool * handle = (bool *)&kernelTLS().sched_lock;
146
147 // Step - 1 : check if there is already space in the data
148 uint_fast32_t s = ready;
149
150 // Check among all the ready
151 for(uint_fast32_t i = 0; i < s; i++) {
152 bool * volatile * cell = (bool * volatile *)&data[i]; // Cforall is bugged and the double volatiles causes problems
153 /* paranoid */ verify( handle != *cell );
154
155 bool * null = 0p; // Re-write every loop since compare thrashes it
156 if( __atomic_load_n(cell, (int)__ATOMIC_RELAXED) == null
157 && __atomic_compare_exchange_n( cell, &null, handle, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
158 /* paranoid */ verify(i < ready);
159 /* paranoid */ verify( (kernelTLS().sched_id = i, true) );
160 return i;
161 }
162 }
163
164 if(max <= alloc) abort("Trying to create more than %ud processors", __scheduler_lock->max);
165
166 // Step - 2 : F&A to get a new spot in the array.
167 uint_fast32_t n = __atomic_fetch_add(&alloc, 1, __ATOMIC_SEQ_CST);
168 if(max <= n) abort("Trying to create more than %ud processors", __scheduler_lock->max);
169
170 // Step - 3 : Mark space as used and then publish it.
171 data[n] = handle;
172 while() {
173 unsigned copy = n;
174 if( __atomic_load_n(&ready, __ATOMIC_RELAXED) == n
175 && __atomic_compare_exchange_n(&ready, &copy, n + 1, true, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST))
176 break;
177 Pause();
178 }
179
180 // Return new spot.
181 /* paranoid */ verify(n < ready);
182 /* paranoid */ verify( (kernelTLS().sched_id = n, true) );
183 return n;
184}
185
186void unregister_proc_id( unsigned id ) with(*__scheduler_lock) {
187 /* paranoid */ verify(id < ready);
188 /* paranoid */ verify(id == kernelTLS().sched_id);
189 /* paranoid */ verify(data[id] == &kernelTLS().sched_lock);
190
191 bool * volatile * cell = (bool * volatile *)&data[id]; // Cforall is bugged and the double volatiles causes problems
192
193 __atomic_store_n(cell, 0p, __ATOMIC_RELEASE);
194
195 __kernel_rseq_unregister();
196}
197
198//-----------------------------------------------------------------------
199// Writer side : acquire when changing the ready queue, e.g. adding more
200// queues or removing them.
201uint_fast32_t ready_mutate_lock( void ) with(*__scheduler_lock) {
202 /* paranoid */ verify( ! __preemption_enabled() );
203 /* paranoid */ verify( ! kernelTLS().sched_lock );
204
205 // Step 1 : lock global lock
206 // It is needed to avoid processors that register mid Critical-Section
207 // to simply lock their own lock and enter.
208 __atomic_acquire( &write_lock );
209
210 // Step 2 : lock per-proc lock
211 // Processors that are currently being registered aren't counted
212 // but can't be in read_lock or in the critical section.
213 // All other processors are counted
214 uint_fast32_t s = ready;
215 for(uint_fast32_t i = 0; i < s; i++) {
216 volatile bool * llock = data[i];
217 if(llock) __atomic_acquire( llock );
218 }
219
220 /* paranoid */ verify( ! __preemption_enabled() );
221 return s;
222}
223
224void ready_mutate_unlock( uint_fast32_t last_s ) with(*__scheduler_lock) {
225 /* paranoid */ verify( ! __preemption_enabled() );
226
227 // Step 1 : release local locks
228 // This must be done while the global lock is held to avoid
229 // threads that where created mid critical section
230 // to race to lock their local locks and have the writer
231 // immidiately unlock them
232 // Alternative solution : return s in write_lock and pass it to write_unlock
233 for(uint_fast32_t i = 0; i < last_s; i++) {
234 volatile bool * llock = data[i];
235 if(llock) __atomic_store_n(llock, (bool)false, __ATOMIC_RELEASE);
236 }
237
238 // Step 2 : release global lock
239 /*paranoid*/ assert(true == write_lock);
240 __atomic_store_n(&write_lock, (bool)false, __ATOMIC_RELEASE);
241
242 /* paranoid */ verify( ! __preemption_enabled() );
243}
244
245//=======================================================================
246// caches handling
247
248struct __attribute__((aligned(128))) __ready_queue_caches_t {
249 // Count States:
250 // - 0 : No one is looking after this cache
251 // - 1 : No one is looking after this cache, BUT it's not empty
252 // - 2+ : At least one processor is looking after this cache
253 volatile unsigned count;
254};
255
256void ?{}(__ready_queue_caches_t & this) { this.count = 0; }
257void ^?{}(__ready_queue_caches_t & this) {}
258
259static inline void depart(__ready_queue_caches_t & cache) {
260 /* paranoid */ verify( cache.count > 1);
261 __atomic_fetch_add(&cache.count, -1, __ATOMIC_SEQ_CST);
262 /* paranoid */ verify( cache.count != 0);
263 /* paranoid */ verify( cache.count < 65536 ); // This verify assumes no cluster will have more than 65000 kernel threads mapped to a single cache, which could be correct but is super weird.
264}
265
266static inline void arrive(__ready_queue_caches_t & cache) {
267 // for() {
268 // unsigned expected = cache.count;
269 // unsigned desired = 0 == expected ? 2 : expected + 1;
270 // }
271}
272
273//=======================================================================
274// Cforall Ready Queue used for scheduling
275//=======================================================================
276unsigned long long moving_average(unsigned long long currtsc, unsigned long long instsc, unsigned long long old_avg) {
277 /* paranoid */ verifyf( currtsc < 45000000000000000, "Suspiciously large current time: %'llu (%llx)\n", currtsc, currtsc );
278 /* paranoid */ verifyf( instsc < 45000000000000000, "Suspiciously large insert time: %'llu (%llx)\n", instsc, instsc );
279 /* paranoid */ verifyf( old_avg < 15000000000000, "Suspiciously large previous average: %'llu (%llx)\n", old_avg, old_avg );
280
281 const unsigned long long new_val = currtsc > instsc ? currtsc - instsc : 0;
282 const unsigned long long total_weight = 16;
283 const unsigned long long new_weight = 4;
284 const unsigned long long old_weight = total_weight - new_weight;
285 const unsigned long long ret = ((new_weight * new_val) + (old_weight * old_avg)) / total_weight;
286 return ret;
287}
288
289void ?{}(__ready_queue_t & this) with (this) {
290 #if defined(USE_CPU_WORK_STEALING)
291 lanes.count = cpu_info.hthrd_count * READYQ_SHARD_FACTOR;
292 lanes.data = alloc( lanes.count );
293 lanes.tscs = alloc( lanes.count );
294 lanes.help = alloc( cpu_info.hthrd_count );
295
296 for( idx; (size_t)lanes.count ) {
297 (lanes.data[idx]){};
298 lanes.tscs[idx].tv = rdtscl();
299 lanes.tscs[idx].ma = rdtscl();
300 }
301 for( idx; (size_t)cpu_info.hthrd_count ) {
302 lanes.help[idx].src = 0;
303 lanes.help[idx].dst = 0;
304 lanes.help[idx].tri = 0;
305 }
306
307 caches = alloc( cpu_info.llc_count );
308 for( idx; (size_t)cpu_info.llc_count ) {
309 (caches[idx]){};
310 }
311 #else
312 lanes.data = 0p;
313 lanes.tscs = 0p;
314 lanes.caches = 0p;
315 lanes.help = 0p;
316 lanes.count = 0;
317 #endif
318}
319
320void ^?{}(__ready_queue_t & this) with (this) {
321 #if !defined(USE_CPU_WORK_STEALING)
322 verify( SEQUENTIAL_SHARD == lanes.count );
323 #endif
324
325 free(lanes.data);
326 free(lanes.tscs);
327 free(lanes.caches);
328 free(lanes.help);
329}
330
331//-----------------------------------------------------------------------
332#if defined(USE_AWARE_STEALING)
333 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
334 processor * const proc = kernelTLS().this_processor;
335 const bool external = (!proc) || (cltr != proc->cltr);
336 const bool remote = hint == UNPARK_REMOTE;
337
338 unsigned i;
339 if( external || remote ) {
340 // Figure out where thread was last time and make sure it's valid
341 /* paranoid */ verify(thrd->preferred >= 0);
342 if(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count) {
343 /* paranoid */ verify(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count);
344 unsigned start = thrd->preferred * READYQ_SHARD_FACTOR;
345 do {
346 unsigned r = __tls_rand();
347 i = start + (r % READYQ_SHARD_FACTOR);
348 /* paranoid */ verify( i < lanes.count );
349 // If we can't lock it retry
350 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
351 } else {
352 do {
353 i = __tls_rand() % lanes.count;
354 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
355 }
356 } else {
357 do {
358 unsigned r = proc->rdq.its++;
359 i = proc->rdq.id + (r % READYQ_SHARD_FACTOR);
360 /* paranoid */ verify( i < lanes.count );
361 // If we can't lock it retry
362 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
363 }
364
365 // Actually push it
366 push(lanes.data[i], thrd);
367
368 // Unlock and return
369 __atomic_unlock( &lanes.data[i].lock );
370
371 #if !defined(__CFA_NO_STATISTICS__)
372 if(unlikely(external || remote)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
373 else __tls_stats()->ready.push.local.success++;
374 #endif
375 }
376
377 static inline unsigned long long calc_cutoff(const unsigned long long ctsc, const processor * proc, __ready_queue_t & rdq) {
378 unsigned start = proc->rdq.id;
379 unsigned long long max = 0;
380 for(i; READYQ_SHARD_FACTOR) {
381 unsigned long long ptsc = ts(rdq.lanes.data[start + i]);
382 if(ptsc != -1ull) {
383 /* paranoid */ verify( start + i < rdq.lanes.count );
384 unsigned long long tsc = moving_average(ctsc, ptsc, rdq.lanes.tscs[start + i].ma);
385 if(tsc > max) max = tsc;
386 }
387 }
388 return (max + 2 * max) / 2;
389 }
390
391 __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
392 /* paranoid */ verify( lanes.count > 0 );
393 /* paranoid */ verify( kernelTLS().this_processor );
394 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
395
396 processor * const proc = kernelTLS().this_processor;
397 unsigned this = proc->rdq.id;
398 /* paranoid */ verify( this < lanes.count );
399 __cfadbg_print_safe(ready_queue, "Kernel : pop from %u\n", this);
400
401 // Figure out the current cpu and make sure it is valid
402 const int cpu = __kernel_getcpu();
403 /* paranoid */ verify(cpu >= 0);
404 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
405 unsigned this_cache = cpu_info.llc_map[cpu].cache;
406 __atomic_store_n(&lanes.caches[this / READYQ_SHARD_FACTOR].id, this_cache, __ATOMIC_RELAXED);
407
408 const unsigned long long ctsc = rdtscl();
409
410 if(proc->rdq.target == MAX) {
411 uint64_t chaos = __tls_rand();
412 unsigned ext = chaos & 0xff;
413 unsigned other = (chaos >> 8) % (lanes.count);
414
415 if(ext < 3 || __atomic_load_n(&lanes.caches[other / READYQ_SHARD_FACTOR].id, __ATOMIC_RELAXED) == this_cache) {
416 proc->rdq.target = other;
417 }
418 }
419 else {
420 const unsigned target = proc->rdq.target;
421 __cfadbg_print_safe(ready_queue, "Kernel : %u considering helping %u, tcsc %llu\n", this, target, lanes.tscs[target].tv);
422 /* paranoid */ verify( lanes.tscs[target].tv != MAX );
423 if(target < lanes.count) {
424 const unsigned long long cutoff = calc_cutoff(ctsc, proc, cltr->ready_queue);
425 const unsigned long long age = moving_average(ctsc, lanes.tscs[target].tv, lanes.tscs[target].ma);
426 __cfadbg_print_safe(ready_queue, "Kernel : Help attempt on %u from %u, age %'llu vs cutoff %'llu, %s\n", target, this, age, cutoff, age > cutoff ? "yes" : "no");
427 if(age > cutoff) {
428 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
429 if(t) return t;
430 }
431 }
432 proc->rdq.target = MAX;
433 }
434
435 for(READYQ_SHARD_FACTOR) {
436 unsigned i = this + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
437 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
438 }
439
440 // All lanes where empty return 0p
441 return 0p;
442
443 }
444 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
445 unsigned i = __tls_rand() % lanes.count;
446 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
447 }
448 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
449 return search(cltr);
450 }
451#endif
452#if defined(USE_CPU_WORK_STEALING)
453 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
454 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
455
456 processor * const proc = kernelTLS().this_processor;
457 const bool external = (!proc) || (cltr != proc->cltr);
458
459 // Figure out the current cpu and make sure it is valid
460 const int cpu = __kernel_getcpu();
461 /* paranoid */ verify(cpu >= 0);
462 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
463 /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count);
464
465 // Figure out where thread was last time and make sure it's
466 /* paranoid */ verify(thrd->preferred >= 0);
467 /* paranoid */ verify(thrd->preferred < cpu_info.hthrd_count);
468 /* paranoid */ verify(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count);
469 const int prf = thrd->preferred * READYQ_SHARD_FACTOR;
470
471 const cpu_map_entry_t & map;
472 choose(hint) {
473 case UNPARK_LOCAL : &map = &cpu_info.llc_map[cpu];
474 case UNPARK_REMOTE: &map = &cpu_info.llc_map[prf];
475 }
476 /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count);
477 /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count);
478 /* paranoid */ verifyf((map.start + map.count) * READYQ_SHARD_FACTOR <= lanes.count, "have %zu lanes but map can go up to %u", lanes.count, (map.start + map.count) * READYQ_SHARD_FACTOR);
479
480 const int start = map.self * READYQ_SHARD_FACTOR;
481 unsigned i;
482 do {
483 unsigned r;
484 if(unlikely(external)) { r = __tls_rand(); }
485 else { r = proc->rdq.its++; }
486 choose(hint) {
487 case UNPARK_LOCAL : i = start + (r % READYQ_SHARD_FACTOR);
488 case UNPARK_REMOTE: i = prf + (r % READYQ_SHARD_FACTOR);
489 }
490 // If we can't lock it retry
491 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
492
493 // Actually push it
494 push(lanes.data[i], thrd);
495
496 // Unlock and return
497 __atomic_unlock( &lanes.data[i].lock );
498
499 #if !defined(__CFA_NO_STATISTICS__)
500 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
501 else __tls_stats()->ready.push.local.success++;
502 #endif
503
504 __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);
505
506 }
507
508 static inline int pop_getcpu(processor * proc, __ready_queue_caches_t * caches) {
509 const int prv = proc->rdq.cpu;
510 const int cpu = __kernel_getcpu();
511 if( prv != proc->rdq.cpu ) {
512 unsigned pidx = cpu_info.llc_map[prv].cache;
513 /* paranoid */ verify(pidx < cpu_info.llc_count);
514
515 unsigned nidx = cpu_info.llc_map[cpu].cache;
516 /* paranoid */ verify(pidx < cpu_info.llc_count);
517
518 depart(caches[pidx]);
519 arrive(caches[nidx]);
520
521 __STATS( /* cpu migs++ */ )
522 }
523 return proc->rdq.cpu = cpu;
524 }
525
526 // Pop from the ready queue from a given cluster
527 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
528 /* paranoid */ verify( lanes.count > 0 );
529 /* paranoid */ verify( kernelTLS().this_processor );
530
531 processor * const proc = kernelTLS().this_processor;
532 const int cpu = pop_getcpu( proc, caches );
533 // const int cpu = __kernel_getcpu();
534 /* paranoid */ verify(cpu >= 0);
535 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
536 /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count);
537
538 const cpu_map_entry_t & map = cpu_info.llc_map[cpu];
539 /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count);
540 /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count);
541 /* paranoid */ verifyf((map.start + map.count) * READYQ_SHARD_FACTOR <= lanes.count, "have %zu lanes but map can go up to %u", lanes.count, (map.start + map.count) * READYQ_SHARD_FACTOR);
542
543 const int start = map.self * READYQ_SHARD_FACTOR;
544 const unsigned long long ctsc = rdtscl();
545
546 // Did we already have a help target
547 if(proc->rdq.target == MAX) {
548 unsigned long long max = 0;
549 for(i; READYQ_SHARD_FACTOR) {
550 unsigned long long tsc = moving_average(ctsc - ts(lanes.data[start + i]), lanes.tscs[start + i].ma);
551 if(tsc > max) max = tsc;
552 }
553 // proc->rdq.cutoff = (max + 2 * max) / 2;
554 /* paranoid */ verify(lanes.count < 65536); // The following code assumes max 65536 cores.
555 /* paranoid */ verify(map.count < 65536); // The following code assumes max 65536 cores.
556
557 if(0 == (__tls_rand() % 100)) {
558 proc->rdq.target = __tls_rand() % lanes.count;
559 } else {
560 unsigned cpu_chaos = map.start + (__tls_rand() % map.count);
561 proc->rdq.target = (cpu_chaos * READYQ_SHARD_FACTOR) + (__tls_rand() % READYQ_SHARD_FACTOR);
562 /* paranoid */ verify(proc->rdq.target >= (map.start * READYQ_SHARD_FACTOR));
563 /* paranoid */ verify(proc->rdq.target < ((map.start + map.count) * READYQ_SHARD_FACTOR));
564 }
565
566 /* paranoid */ verify(proc->rdq.target != MAX);
567 }
568 else {
569 unsigned long long max = 0;
570 for(i; READYQ_SHARD_FACTOR) {
571 unsigned long long tsc = moving_average(ctsc - ts(lanes.data[start + i]), lanes.tscs[start + i].ma);
572 if(tsc > max) max = tsc;
573 }
574 const unsigned long long cutoff = (max + 2 * max) / 2;
575 {
576 unsigned target = proc->rdq.target;
577 proc->rdq.target = MAX;
578 lanes.help[target / READYQ_SHARD_FACTOR].tri++;
579 if(moving_average(ctsc - lanes.tscs[target].tv, lanes.tscs[target].ma) > cutoff) {
580 __STATS( __tls_stats()->ready.pop.helped[target]++; )
581 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
582 proc->rdq.last = target;
583 if(t) return t;
584 }
585 proc->rdq.target = MAX;
586 }
587
588 unsigned last = proc->rdq.last;
589 if(last != MAX && moving_average(ctsc - lanes.tscs[last].tv, lanes.tscs[last].ma) > cutoff) {
590 __STATS( __tls_stats()->ready.pop.helped[last]++; )
591 thread$ * t = try_pop(cltr, last __STATS(, __tls_stats()->ready.pop.help));
592 if(t) return t;
593 }
594 else {
595 proc->rdq.last = MAX;
596 }
597 }
598
599 for(READYQ_SHARD_FACTOR) {
600 unsigned i = start + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
601 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
602 }
603
604 // All lanes where empty return 0p
605 return 0p;
606 }
607
608 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
609 processor * const proc = kernelTLS().this_processor;
610 unsigned last = proc->rdq.last;
611 if(last != MAX) {
612 struct thread$ * t = try_pop(cltr, last __STATS(, __tls_stats()->ready.pop.steal));
613 if(t) return t;
614 proc->rdq.last = MAX;
615 }
616
617 unsigned i = __tls_rand() % lanes.count;
618 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
619 }
620 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
621 return search(cltr);
622 }
623#endif
624#if defined(USE_RELAXED_FIFO)
625 //-----------------------------------------------------------------------
626 // get index from random number with or without bias towards queues
627 static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred) {
628 unsigned i;
629 bool local;
630 unsigned rlow = r % BIAS;
631 unsigned rhigh = r / BIAS;
632 if((0 != rlow) && preferred >= 0) {
633 // (BIAS - 1) out of BIAS chances
634 // Use perferred queues
635 i = preferred + (rhigh % READYQ_SHARD_FACTOR);
636 local = true;
637 }
638 else {
639 // 1 out of BIAS chances
640 // Use all queues
641 i = rhigh;
642 local = false;
643 }
644 return [i, local];
645 }
646
647 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
648 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
649
650 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
651 /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
652
653 bool local;
654 int preferred = external ? -1 : kernelTLS().this_processor->rdq.id;
655
656 // Try to pick a lane and lock it
657 unsigned i;
658 do {
659 // Pick the index of a lane
660 unsigned r = __tls_rand_fwd();
661 [i, local] = idx_from_r(r, preferred);
662
663 i %= __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
664
665 #if !defined(__CFA_NO_STATISTICS__)
666 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
667 else if(local) __tls_stats()->ready.push.local.attempt++;
668 else __tls_stats()->ready.push.share.attempt++;
669 #endif
670
671 // If we can't lock it retry
672 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
673
674 // Actually push it
675 push(lanes.data[i], thrd);
676
677 // Unlock and return
678 __atomic_unlock( &lanes.data[i].lock );
679
680 // Mark the current index in the tls rng instance as having an item
681 __tls_rand_advance_bck();
682
683 __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);
684
685 // Update statistics
686 #if !defined(__CFA_NO_STATISTICS__)
687 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
688 else if(local) __tls_stats()->ready.push.local.success++;
689 else __tls_stats()->ready.push.share.success++;
690 #endif
691 }
692
693 // Pop from the ready queue from a given cluster
694 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
695 /* paranoid */ verify( lanes.count > 0 );
696 /* paranoid */ verify( kernelTLS().this_processor );
697 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
698
699 unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
700 int preferred = kernelTLS().this_processor->rdq.id;
701
702
703 // As long as the list is not empty, try finding a lane that isn't empty and pop from it
704 for(25) {
705 // Pick two lists at random
706 unsigned ri = __tls_rand_bck();
707 unsigned rj = __tls_rand_bck();
708
709 unsigned i, j;
710 __attribute__((unused)) bool locali, localj;
711 [i, locali] = idx_from_r(ri, preferred);
712 [j, localj] = idx_from_r(rj, preferred);
713
714 i %= count;
715 j %= count;
716
717 // try popping from the 2 picked lists
718 struct thread$ * thrd = try_pop(cltr, i, j __STATS(, *(locali || localj ? &__tls_stats()->ready.pop.local : &__tls_stats()->ready.pop.help)));
719 if(thrd) {
720 return thrd;
721 }
722 }
723
724 // All lanes where empty return 0p
725 return 0p;
726 }
727
728 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) { return pop_fast(cltr); }
729 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
730 return search(cltr);
731 }
732#endif
733#if defined(USE_WORK_STEALING)
734 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
735 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
736
737 // #define USE_PREFERRED
738 #if !defined(USE_PREFERRED)
739 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
740 /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
741 #else
742 unsigned preferred = thrd->preferred;
743 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || preferred == MAX || thrd->curr_cluster != cltr;
744 /* paranoid */ verifyf(external || preferred < lanes.count, "Invalid preferred queue %u for %u lanes", preferred, lanes.count );
745
746 unsigned r = preferred % READYQ_SHARD_FACTOR;
747 const unsigned start = preferred - r;
748 #endif
749
750 // Try to pick a lane and lock it
751 unsigned i;
752 do {
753 #if !defined(__CFA_NO_STATISTICS__)
754 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
755 else __tls_stats()->ready.push.local.attempt++;
756 #endif
757
758 if(unlikely(external)) {
759 i = __tls_rand() % lanes.count;
760 }
761 else {
762 #if !defined(USE_PREFERRED)
763 processor * proc = kernelTLS().this_processor;
764 unsigned r = proc->rdq.its++;
765 i = proc->rdq.id + (r % READYQ_SHARD_FACTOR);
766 #else
767 i = start + (r++ % READYQ_SHARD_FACTOR);
768 #endif
769 }
770 // If we can't lock it retry
771 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
772
773 // Actually push it
774 push(lanes.data[i], thrd);
775
776 // Unlock and return
777 __atomic_unlock( &lanes.data[i].lock );
778
779 #if !defined(__CFA_NO_STATISTICS__)
780 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
781 else __tls_stats()->ready.push.local.success++;
782 #endif
783
784 __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);
785 }
786
787 // Pop from the ready queue from a given cluster
788 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
789 /* paranoid */ verify( lanes.count > 0 );
790 /* paranoid */ verify( kernelTLS().this_processor );
791 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
792
793 processor * proc = kernelTLS().this_processor;
794
795 if(proc->rdq.target == MAX) {
796 unsigned long long min = ts(lanes.data[proc->rdq.id]);
797 for(int i = 0; i < READYQ_SHARD_FACTOR; i++) {
798 unsigned long long tsc = ts(lanes.data[proc->rdq.id + i]);
799 if(tsc < min) min = tsc;
800 }
801 proc->rdq.cutoff = min;
802 proc->rdq.target = __tls_rand() % lanes.count;
803 }
804 else {
805 unsigned target = proc->rdq.target;
806 proc->rdq.target = MAX;
807 const unsigned long long bias = 0; //2_500_000_000;
808 const unsigned long long cutoff = proc->rdq.cutoff > bias ? proc->rdq.cutoff - bias : proc->rdq.cutoff;
809 if(lanes.tscs[target].tv < cutoff && ts(lanes.data[target]) < cutoff) {
810 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
811 if(t) return t;
812 }
813 }
814
815 for(READYQ_SHARD_FACTOR) {
816 unsigned i = proc->rdq.id + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
817 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
818 }
819 return 0p;
820 }
821
822 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
823 unsigned i = __tls_rand() % lanes.count;
824 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
825 }
826
827 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) with (cltr->ready_queue) {
828 return search(cltr);
829 }
830#endif
831
832//=======================================================================
833// Various Ready Queue utilities
834//=======================================================================
835// these function work the same or almost the same
836// whether they are using work-stealing or relaxed fifo scheduling
837
838//-----------------------------------------------------------------------
839// try to pop from a lane given by index w
840static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
841 /* paranoid */ verify( w < lanes.count );
842 __STATS( stats.attempt++; )
843
844 // Get relevant elements locally
845 __intrusive_lane_t & lane = lanes.data[w];
846
847 // If list looks empty retry
848 if( is_empty(lane) ) {
849 return 0p;
850 }
851
852 // If we can't get the lock retry
853 if( !__atomic_try_acquire(&lane.lock) ) {
854 return 0p;
855 }
856
857 // If list is empty, unlock and retry
858 if( is_empty(lane) ) {
859 __atomic_unlock(&lane.lock);
860 return 0p;
861 }
862
863 // Actually pop the list
864 struct thread$ * thrd;
865 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING) || defined(USE_CPU_WORK_STEALING)
866 unsigned long long tsc_before = ts(lane);
867 #endif
868 unsigned long long tsv;
869 [thrd, tsv] = pop(lane);
870
871 /* paranoid */ verify(thrd);
872 /* paranoid */ verify(tsv);
873 /* paranoid */ verify(lane.lock);
874
875 // Unlock and return
876 __atomic_unlock(&lane.lock);
877
878 // Update statistics
879 __STATS( stats.success++; )
880
881 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING) || defined(USE_CPU_WORK_STEALING)
882 if (tsv != MAX) {
883 unsigned long long now = rdtscl();
884 unsigned long long pma = __atomic_load_n(&lanes.tscs[w].ma, __ATOMIC_RELAXED);
885 __atomic_store_n(&lanes.tscs[w].tv, tsv, __ATOMIC_RELAXED);
886 __atomic_store_n(&lanes.tscs[w].ma, moving_average(now, tsc_before, pma), __ATOMIC_RELAXED);
887 }
888 #endif
889
890 #if defined(USE_AWARE_STEALING) || defined(USE_CPU_WORK_STEALING)
891 thrd->preferred = w / READYQ_SHARD_FACTOR;
892 #else
893 thrd->preferred = w;
894 #endif
895
896 // return the popped thread
897 return thrd;
898}
899
900//-----------------------------------------------------------------------
901// try to pop from any lanes making sure you don't miss any threads push
902// before the start of the function
903static inline struct thread$ * search(struct cluster * cltr) with (cltr->ready_queue) {
904 /* paranoid */ verify( lanes.count > 0 );
905 unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
906 unsigned offset = __tls_rand();
907 for(i; count) {
908 unsigned idx = (offset + i) % count;
909 struct thread$ * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search));
910 if(thrd) {
911 return thrd;
912 }
913 }
914
915 // All lanes where empty return 0p
916 return 0p;
917}
918
919//-----------------------------------------------------------------------
920// get preferred ready for new thread
921unsigned ready_queue_new_preferred() {
922 unsigned pref = 0;
923 if(struct thread$ * thrd = publicTLS_get( this_thread )) {
924 pref = thrd->preferred;
925 }
926 else {
927 #if defined(USE_CPU_WORK_STEALING)
928 pref = __kernel_getcpu();
929 #endif
930 }
931
932 #if defined(USE_CPU_WORK_STEALING)
933 /* paranoid */ verify(pref >= 0);
934 /* paranoid */ verify(pref < cpu_info.hthrd_count);
935 #endif
936
937 return pref;
938}
939
940//-----------------------------------------------------------------------
941// Check that all the intrusive queues in the data structure are still consistent
942static void check( __ready_queue_t & q ) with (q) {
943 #if defined(__CFA_WITH_VERIFY__)
944 {
945 for( idx ; lanes.count ) {
946 __intrusive_lane_t & sl = lanes.data[idx];
947 assert(!lanes.data[idx].lock);
948
949 if(is_empty(sl)) {
950 assert( sl.anchor.next == 0p );
951 assert( sl.anchor.ts == -1llu );
952 assert( mock_head(sl) == sl.prev );
953 } else {
954 assert( sl.anchor.next != 0p );
955 assert( sl.anchor.ts != -1llu );
956 assert( mock_head(sl) != sl.prev );
957 }
958 }
959 }
960 #endif
961}
962
963//-----------------------------------------------------------------------
964// Given 2 indexes, pick the list with the oldest push an try to pop from it
965static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
966 // Pick the bet list
967 int w = i;
968 if( __builtin_expect(!is_empty(lanes.data[j]), true) ) {
969 w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j;
970 }
971
972 return try_pop(cltr, w __STATS(, stats));
973}
974
975// Call this function of the intrusive list was moved using memcpy
976// fixes the list so that the pointers back to anchors aren't left dangling
977static inline void fix(__intrusive_lane_t & ll) {
978 if(is_empty(ll)) {
979 verify(ll.anchor.next == 0p);
980 ll.prev = mock_head(ll);
981 }
982}
983
984static void assign_list(unsigned & value, dlist(processor) & list, unsigned count) {
985 processor * it = &list`first;
986 for(unsigned i = 0; i < count; i++) {
987 /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
988 it->rdq.id = value;
989 it->rdq.target = MAX;
990 value += READYQ_SHARD_FACTOR;
991 it = &(*it)`next;
992 }
993}
994
995static void reassign_cltr_id(struct cluster * cltr) {
996 unsigned preferred = 0;
997 assign_list(preferred, cltr->procs.actives, cltr->procs.total - cltr->procs.idle);
998 assign_list(preferred, cltr->procs.idles , cltr->procs.idle );
999}
1000
1001static void fix_times( struct cluster * cltr ) with( cltr->ready_queue ) {
1002 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING)
1003 lanes.tscs = alloc(lanes.count, lanes.tscs`realloc);
1004 for(i; lanes.count) {
1005 lanes.tscs[i].tv = rdtscl();
1006 lanes.tscs[i].ma = 0;
1007 }
1008 #endif
1009}
1010
1011#if defined(USE_CPU_WORK_STEALING)
1012 // ready_queue size is fixed in this case
1013 void ready_queue_grow(struct cluster * cltr) {}
1014 void ready_queue_shrink(struct cluster * cltr) {}
1015#else
1016 // Grow the ready queue
1017 void ready_queue_grow(struct cluster * cltr) {
1018 size_t ncount;
1019 int target = cltr->procs.total;
1020
1021 /* paranoid */ verify( ready_mutate_islocked() );
1022 __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n");
1023
1024 // Make sure that everything is consistent
1025 /* paranoid */ check( cltr->ready_queue );
1026
1027 // grow the ready queue
1028 with( cltr->ready_queue ) {
1029 // Find new count
1030 // Make sure we always have atleast 1 list
1031 if(target >= 2) {
1032 ncount = target * READYQ_SHARD_FACTOR;
1033 } else {
1034 ncount = SEQUENTIAL_SHARD;
1035 }
1036
1037 // Allocate new array (uses realloc and memcpies the data)
1038 lanes.data = alloc( ncount, lanes.data`realloc );
1039
1040 // Fix the moved data
1041 for( idx; (size_t)lanes.count ) {
1042 fix(lanes.data[idx]);
1043 }
1044
1045 // Construct new data
1046 for( idx; (size_t)lanes.count ~ ncount) {
1047 (lanes.data[idx]){};
1048 }
1049
1050 // Update original
1051 lanes.count = ncount;
1052
1053 lanes.caches = alloc( target, lanes.caches`realloc );
1054 }
1055
1056 fix_times(cltr);
1057
1058 reassign_cltr_id(cltr);
1059
1060 // Make sure that everything is consistent
1061 /* paranoid */ check( cltr->ready_queue );
1062
1063 __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n");
1064
1065 /* paranoid */ verify( ready_mutate_islocked() );
1066 }
1067
1068 // Shrink the ready queue
1069 void ready_queue_shrink(struct cluster * cltr) {
1070 /* paranoid */ verify( ready_mutate_islocked() );
1071 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n");
1072
1073 // Make sure that everything is consistent
1074 /* paranoid */ check( cltr->ready_queue );
1075
1076 int target = cltr->procs.total;
1077
1078 with( cltr->ready_queue ) {
1079 // Remember old count
1080 size_t ocount = lanes.count;
1081
1082 // Find new count
1083 // Make sure we always have atleast 1 list
1084 lanes.count = target >= 2 ? target * READYQ_SHARD_FACTOR: SEQUENTIAL_SHARD;
1085 /* paranoid */ verify( ocount >= lanes.count );
1086 /* paranoid */ verify( lanes.count == target * READYQ_SHARD_FACTOR || target < 2 );
1087
1088 // for printing count the number of displaced threads
1089 #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
1090 __attribute__((unused)) size_t displaced = 0;
1091 #endif
1092
1093 // redistribute old data
1094 for( idx; (size_t)lanes.count ~ ocount) {
1095 // Lock is not strictly needed but makes checking invariants much easier
1096 __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock);
1097 verify(locked);
1098
1099 // As long as we can pop from this lane to push the threads somewhere else in the queue
1100 while(!is_empty(lanes.data[idx])) {
1101 struct thread$ * thrd;
1102 unsigned long long _;
1103 [thrd, _] = pop(lanes.data[idx]);
1104
1105 push(cltr, thrd, true);
1106
1107 // for printing count the number of displaced threads
1108 #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
1109 displaced++;
1110 #endif
1111 }
1112
1113 // Unlock the lane
1114 __atomic_unlock(&lanes.data[idx].lock);
1115
1116 // TODO print the queue statistics here
1117
1118 ^(lanes.data[idx]){};
1119 }
1120
1121 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced);
1122
1123 // Allocate new array (uses realloc and memcpies the data)
1124 lanes.data = alloc( lanes.count, lanes.data`realloc );
1125
1126 // Fix the moved data
1127 for( idx; (size_t)lanes.count ) {
1128 fix(lanes.data[idx]);
1129 }
1130
1131 lanes.caches = alloc( target, lanes.caches`realloc );
1132 }
1133
1134 fix_times(cltr);
1135
1136
1137 reassign_cltr_id(cltr);
1138
1139 // Make sure that everything is consistent
1140 /* paranoid */ check( cltr->ready_queue );
1141
1142 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n");
1143 /* paranoid */ verify( ready_mutate_islocked() );
1144 }
1145#endif
1146
1147#if !defined(__CFA_NO_STATISTICS__)
1148 unsigned cnt(const __ready_queue_t & this, unsigned idx) {
1149 /* paranoid */ verify(this.lanes.count > idx);
1150 return this.lanes.data[idx].cnt;
1151 }
1152#endif
1153
1154
1155#if defined(CFA_HAVE_LINUX_LIBRSEQ)
1156 // No definition needed
1157#elif defined(CFA_HAVE_LINUX_RSEQ_H)
1158
1159 #if defined( __x86_64 ) || defined( __i386 )
1160 #define RSEQ_SIG 0x53053053
1161 #elif defined( __ARM_ARCH )
1162 #ifdef __ARMEB__
1163 #define RSEQ_SIG 0xf3def5e7 /* udf #24035 ; 0x5de3 (ARMv6+) */
1164 #else
1165 #define RSEQ_SIG 0xe7f5def3 /* udf #24035 ; 0x5de3 */
1166 #endif
1167 #endif
1168
1169 extern void __disable_interrupts_hard();
1170 extern void __enable_interrupts_hard();
1171
1172 static void __kernel_raw_rseq_register (void) {
1173 /* paranoid */ verify( __cfaabi_rseq.cpu_id == RSEQ_CPU_ID_UNINITIALIZED );
1174
1175 // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, (sigset_t *)0p, _NSIG / 8);
1176 int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, RSEQ_SIG);
1177 if(ret != 0) {
1178 int e = errno;
1179 switch(e) {
1180 case EINVAL: abort("KERNEL ERROR: rseq register invalid argument");
1181 case ENOSYS: abort("KERNEL ERROR: rseq register no supported");
1182 case EFAULT: abort("KERNEL ERROR: rseq register with invalid argument");
1183 case EBUSY : abort("KERNEL ERROR: rseq register already registered");
1184 case EPERM : abort("KERNEL ERROR: rseq register sig argument on unregistration does not match the signature received on registration");
1185 default: abort("KERNEL ERROR: rseq register unexpected return %d", e);
1186 }
1187 }
1188 }
1189
1190 static void __kernel_raw_rseq_unregister(void) {
1191 /* paranoid */ verify( __cfaabi_rseq.cpu_id >= 0 );
1192
1193 // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, (sigset_t *)0p, _NSIG / 8);
1194 int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
1195 if(ret != 0) {
1196 int e = errno;
1197 switch(e) {
1198 case EINVAL: abort("KERNEL ERROR: rseq unregister invalid argument");
1199 case ENOSYS: abort("KERNEL ERROR: rseq unregister no supported");
1200 case EFAULT: abort("KERNEL ERROR: rseq unregister with invalid argument");
1201 case EBUSY : abort("KERNEL ERROR: rseq unregister already registered");
1202 case EPERM : abort("KERNEL ERROR: rseq unregister sig argument on unregistration does not match the signature received on registration");
1203 default: abort("KERNEL ERROR: rseq unregisteunexpected return %d", e);
1204 }
1205 }
1206 }
1207#else
1208 // No definition needed
1209#endif
Note: See TracBrowser for help on using the repository browser.