source: libcfa/src/concurrency/ready_queue.cfa@ 6a33e40

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

@optim: maximize chances "cache" array stays in cache.

  • Property mode set to 100644
File size: 40.0 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 #else
307 lanes.data = 0p;
308 lanes.tscs = 0p;
309 lanes.caches = 0p;
310 lanes.help = 0p;
311 lanes.count = 0;
312 #endif
313}
314
315void ^?{}(__ready_queue_t & this) with (this) {
316 #if !defined(USE_CPU_WORK_STEALING)
317 verify( SEQUENTIAL_SHARD == lanes.count );
318 #endif
319
320 free(lanes.data);
321 free(lanes.tscs);
322 free(lanes.caches);
323 free(lanes.help);
324}
325
326//-----------------------------------------------------------------------
327#if defined(USE_AWARE_STEALING)
328 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
329 processor * const proc = kernelTLS().this_processor;
330 const bool external = (!proc) || (cltr != proc->cltr);
331 const bool remote = hint == UNPARK_REMOTE;
332
333 unsigned i;
334 if( external || remote ) {
335 // Figure out where thread was last time and make sure it's valid
336 /* paranoid */ verify(thrd->preferred >= 0);
337 if(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count) {
338 /* paranoid */ verify(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count);
339 unsigned start = thrd->preferred * READYQ_SHARD_FACTOR;
340 do {
341 unsigned r = __tls_rand();
342 i = start + (r % READYQ_SHARD_FACTOR);
343 /* paranoid */ verify( i < lanes.count );
344 // If we can't lock it retry
345 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
346 } else {
347 do {
348 i = __tls_rand() % lanes.count;
349 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
350 }
351 } else {
352 do {
353 unsigned r = proc->rdq.its++;
354 i = proc->rdq.id + (r % READYQ_SHARD_FACTOR);
355 /* paranoid */ verify( i < lanes.count );
356 // If we can't lock it retry
357 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
358 }
359
360 // Actually push it
361 push(lanes.data[i], thrd);
362
363 // Unlock and return
364 __atomic_unlock( &lanes.data[i].lock );
365
366 #if !defined(__CFA_NO_STATISTICS__)
367 if(unlikely(external || remote)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
368 else __tls_stats()->ready.push.local.success++;
369 #endif
370 }
371
372 static inline unsigned long long calc_cutoff(const unsigned long long ctsc, const processor * proc, __ready_queue_t & rdq) {
373 unsigned start = proc->rdq.id;
374 unsigned long long max = 0;
375 for(i; READYQ_SHARD_FACTOR) {
376 unsigned long long ptsc = ts(rdq.lanes.data[start + i]);
377 if(ptsc != -1ull) {
378 /* paranoid */ verify( start + i < rdq.lanes.count );
379 unsigned long long tsc = moving_average(ctsc, ptsc, rdq.lanes.tscs[start + i].ma);
380 if(tsc > max) max = tsc;
381 }
382 }
383 return (max + 2 * max) / 2;
384 }
385
386 __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
387 /* paranoid */ verify( lanes.count > 0 );
388 /* paranoid */ verify( kernelTLS().this_processor );
389 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
390
391 processor * const proc = kernelTLS().this_processor;
392 unsigned this = proc->rdq.id;
393 /* paranoid */ verify( this < lanes.count );
394 __cfadbg_print_safe(ready_queue, "Kernel : pop from %u\n", this);
395
396 // Figure out the current cpu and make sure it is valid
397 const int cpu = __kernel_getcpu();
398 /* paranoid */ verify(cpu >= 0);
399 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
400 unsigned this_cache = cpu_info.llc_map[cpu].cache;
401
402 // Super important: don't write the same value over and over again
403 // We want to maximise our chances that his particular values stays in cache
404 if(lanes.caches[this / READYQ_SHARD_FACTOR].id != this_cache)
405 __atomic_store_n(&lanes.caches[this / READYQ_SHARD_FACTOR].id, this_cache, __ATOMIC_RELAXED);
406
407 const unsigned long long ctsc = rdtscl();
408
409 if(proc->rdq.target == MAX) {
410 uint64_t chaos = __tls_rand();
411 unsigned ext = chaos & 0xff;
412 unsigned other = (chaos >> 8) % (lanes.count);
413
414 if(ext < 3 || __atomic_load_n(&lanes.caches[other / READYQ_SHARD_FACTOR].id, __ATOMIC_RELAXED) == this_cache) {
415 proc->rdq.target = other;
416 }
417 }
418 else {
419 const unsigned target = proc->rdq.target;
420 __cfadbg_print_safe(ready_queue, "Kernel : %u considering helping %u, tcsc %llu\n", this, target, lanes.tscs[target].tv);
421 /* paranoid */ verify( lanes.tscs[target].tv != MAX );
422 if(target < lanes.count) {
423 const unsigned long long cutoff = calc_cutoff(ctsc, proc, cltr->ready_queue);
424 const unsigned long long age = moving_average(ctsc, lanes.tscs[target].tv, lanes.tscs[target].ma);
425 __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");
426 if(age > cutoff) {
427 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
428 if(t) return t;
429 }
430 }
431 proc->rdq.target = MAX;
432 }
433
434 for(READYQ_SHARD_FACTOR) {
435 unsigned i = this + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
436 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
437 }
438
439 // All lanes where empty return 0p
440 return 0p;
441
442 }
443 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
444 unsigned i = __tls_rand() % lanes.count;
445 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
446 }
447 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
448 return search(cltr);
449 }
450#endif
451#if defined(USE_CPU_WORK_STEALING)
452 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
453 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
454
455 processor * const proc = kernelTLS().this_processor;
456 const bool external = (!proc) || (cltr != proc->cltr);
457
458 // Figure out the current cpu and make sure it is valid
459 const int cpu = __kernel_getcpu();
460 /* paranoid */ verify(cpu >= 0);
461 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
462 /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count);
463
464 // Figure out where thread was last time and make sure it's
465 /* paranoid */ verify(thrd->preferred >= 0);
466 /* paranoid */ verify(thrd->preferred < cpu_info.hthrd_count);
467 /* paranoid */ verify(thrd->preferred * READYQ_SHARD_FACTOR < lanes.count);
468 const int prf = thrd->preferred * READYQ_SHARD_FACTOR;
469
470 const cpu_map_entry_t & map;
471 choose(hint) {
472 case UNPARK_LOCAL : &map = &cpu_info.llc_map[cpu];
473 case UNPARK_REMOTE: &map = &cpu_info.llc_map[prf];
474 }
475 /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count);
476 /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count);
477 /* 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);
478
479 const int start = map.self * READYQ_SHARD_FACTOR;
480 unsigned i;
481 do {
482 unsigned r;
483 if(unlikely(external)) { r = __tls_rand(); }
484 else { r = proc->rdq.its++; }
485 choose(hint) {
486 case UNPARK_LOCAL : i = start + (r % READYQ_SHARD_FACTOR);
487 case UNPARK_REMOTE: i = prf + (r % READYQ_SHARD_FACTOR);
488 }
489 // If we can't lock it retry
490 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
491
492 // Actually push it
493 push(lanes.data[i], thrd);
494
495 // Unlock and return
496 __atomic_unlock( &lanes.data[i].lock );
497
498 #if !defined(__CFA_NO_STATISTICS__)
499 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
500 else __tls_stats()->ready.push.local.success++;
501 #endif
502
503 __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);
504
505 }
506
507 // Pop from the ready queue from a given cluster
508 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
509 /* paranoid */ verify( lanes.count > 0 );
510 /* paranoid */ verify( kernelTLS().this_processor );
511
512 processor * const proc = kernelTLS().this_processor;
513 const int cpu = __kernel_getcpu();
514 /* paranoid */ verify(cpu >= 0);
515 /* paranoid */ verify(cpu < cpu_info.hthrd_count);
516 /* paranoid */ verify(cpu * READYQ_SHARD_FACTOR < lanes.count);
517
518 const cpu_map_entry_t & map = cpu_info.llc_map[cpu];
519 /* paranoid */ verify(map.start * READYQ_SHARD_FACTOR < lanes.count);
520 /* paranoid */ verify(map.self * READYQ_SHARD_FACTOR < lanes.count);
521 /* 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);
522
523 const int start = map.self * READYQ_SHARD_FACTOR;
524 const unsigned long long ctsc = rdtscl();
525
526 // Did we already have a help target
527 if(proc->rdq.target == MAX) {
528 unsigned long long max = 0;
529 for(i; READYQ_SHARD_FACTOR) {
530 unsigned long long tsc = moving_average(ctsc, ts(lanes.data[start + i]), lanes.tscs[start + i].ma);
531 if(tsc > max) max = tsc;
532 }
533 // proc->rdq.cutoff = (max + 2 * max) / 2;
534 /* paranoid */ verify(lanes.count < 65536); // The following code assumes max 65536 cores.
535 /* paranoid */ verify(map.count < 65536); // The following code assumes max 65536 cores.
536
537 if(0 == (__tls_rand() % 100)) {
538 proc->rdq.target = __tls_rand() % lanes.count;
539 } else {
540 unsigned cpu_chaos = map.start + (__tls_rand() % map.count);
541 proc->rdq.target = (cpu_chaos * READYQ_SHARD_FACTOR) + (__tls_rand() % READYQ_SHARD_FACTOR);
542 /* paranoid */ verify(proc->rdq.target >= (map.start * READYQ_SHARD_FACTOR));
543 /* paranoid */ verify(proc->rdq.target < ((map.start + map.count) * READYQ_SHARD_FACTOR));
544 }
545
546 /* paranoid */ verify(proc->rdq.target != MAX);
547 }
548 else {
549 unsigned long long max = 0;
550 for(i; READYQ_SHARD_FACTOR) {
551 unsigned long long tsc = moving_average(ctsc, ts(lanes.data[start + i]), lanes.tscs[start + i].ma);
552 if(tsc > max) max = tsc;
553 }
554 const unsigned long long cutoff = (max + 2 * max) / 2;
555 {
556 unsigned target = proc->rdq.target;
557 proc->rdq.target = MAX;
558 lanes.help[target / READYQ_SHARD_FACTOR].tri++;
559 if(moving_average(ctsc, lanes.tscs[target].tv, lanes.tscs[target].ma) > cutoff) {
560 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
561 proc->rdq.last = target;
562 if(t) return t;
563 }
564 proc->rdq.target = MAX;
565 }
566
567 unsigned last = proc->rdq.last;
568 if(last != MAX && moving_average(ctsc, lanes.tscs[last].tv, lanes.tscs[last].ma) > cutoff) {
569 thread$ * t = try_pop(cltr, last __STATS(, __tls_stats()->ready.pop.help));
570 if(t) return t;
571 }
572 else {
573 proc->rdq.last = MAX;
574 }
575 }
576
577 for(READYQ_SHARD_FACTOR) {
578 unsigned i = start + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
579 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
580 }
581
582 // All lanes where empty return 0p
583 return 0p;
584 }
585
586 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
587 processor * const proc = kernelTLS().this_processor;
588 unsigned last = proc->rdq.last;
589 if(last != MAX) {
590 struct thread$ * t = try_pop(cltr, last __STATS(, __tls_stats()->ready.pop.steal));
591 if(t) return t;
592 proc->rdq.last = MAX;
593 }
594
595 unsigned i = __tls_rand() % lanes.count;
596 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
597 }
598 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
599 return search(cltr);
600 }
601#endif
602#if defined(USE_RELAXED_FIFO)
603 //-----------------------------------------------------------------------
604 // get index from random number with or without bias towards queues
605 static inline [unsigned, bool] idx_from_r(unsigned r, unsigned preferred) {
606 unsigned i;
607 bool local;
608 unsigned rlow = r % BIAS;
609 unsigned rhigh = r / BIAS;
610 if((0 != rlow) && preferred >= 0) {
611 // (BIAS - 1) out of BIAS chances
612 // Use perferred queues
613 i = preferred + (rhigh % READYQ_SHARD_FACTOR);
614 local = true;
615 }
616 else {
617 // 1 out of BIAS chances
618 // Use all queues
619 i = rhigh;
620 local = false;
621 }
622 return [i, local];
623 }
624
625 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
626 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
627
628 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
629 /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
630
631 bool local;
632 int preferred = external ? -1 : kernelTLS().this_processor->rdq.id;
633
634 // Try to pick a lane and lock it
635 unsigned i;
636 do {
637 // Pick the index of a lane
638 unsigned r = __tls_rand_fwd();
639 [i, local] = idx_from_r(r, preferred);
640
641 i %= __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
642
643 #if !defined(__CFA_NO_STATISTICS__)
644 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
645 else if(local) __tls_stats()->ready.push.local.attempt++;
646 else __tls_stats()->ready.push.share.attempt++;
647 #endif
648
649 // If we can't lock it retry
650 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
651
652 // Actually push it
653 push(lanes.data[i], thrd);
654
655 // Unlock and return
656 __atomic_unlock( &lanes.data[i].lock );
657
658 // Mark the current index in the tls rng instance as having an item
659 __tls_rand_advance_bck();
660
661 __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);
662
663 // Update statistics
664 #if !defined(__CFA_NO_STATISTICS__)
665 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
666 else if(local) __tls_stats()->ready.push.local.success++;
667 else __tls_stats()->ready.push.share.success++;
668 #endif
669 }
670
671 // Pop from the ready queue from a given cluster
672 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
673 /* paranoid */ verify( lanes.count > 0 );
674 /* paranoid */ verify( kernelTLS().this_processor );
675 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
676
677 unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
678 int preferred = kernelTLS().this_processor->rdq.id;
679
680
681 // As long as the list is not empty, try finding a lane that isn't empty and pop from it
682 for(25) {
683 // Pick two lists at random
684 unsigned ri = __tls_rand_bck();
685 unsigned rj = __tls_rand_bck();
686
687 unsigned i, j;
688 __attribute__((unused)) bool locali, localj;
689 [i, locali] = idx_from_r(ri, preferred);
690 [j, localj] = idx_from_r(rj, preferred);
691
692 i %= count;
693 j %= count;
694
695 // try popping from the 2 picked lists
696 struct thread$ * thrd = try_pop(cltr, i, j __STATS(, *(locali || localj ? &__tls_stats()->ready.pop.local : &__tls_stats()->ready.pop.help)));
697 if(thrd) {
698 return thrd;
699 }
700 }
701
702 // All lanes where empty return 0p
703 return 0p;
704 }
705
706 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) { return pop_fast(cltr); }
707 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) {
708 return search(cltr);
709 }
710#endif
711#if defined(USE_WORK_STEALING)
712 __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint) with (cltr->ready_queue) {
713 __cfadbg_print_safe(ready_queue, "Kernel : Pushing %p on cluster %p\n", thrd, cltr);
714
715 // #define USE_PREFERRED
716 #if !defined(USE_PREFERRED)
717 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || (cltr != kernelTLS().this_processor->cltr);
718 /* paranoid */ verify(external || kernelTLS().this_processor->rdq.id < lanes.count );
719 #else
720 unsigned preferred = thrd->preferred;
721 const bool external = (hint != UNPARK_LOCAL) || (!kernelTLS().this_processor) || preferred == MAX || thrd->curr_cluster != cltr;
722 /* paranoid */ verifyf(external || preferred < lanes.count, "Invalid preferred queue %u for %u lanes", preferred, lanes.count );
723
724 unsigned r = preferred % READYQ_SHARD_FACTOR;
725 const unsigned start = preferred - r;
726 #endif
727
728 // Try to pick a lane and lock it
729 unsigned i;
730 do {
731 #if !defined(__CFA_NO_STATISTICS__)
732 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.attempt, 1, __ATOMIC_RELAXED);
733 else __tls_stats()->ready.push.local.attempt++;
734 #endif
735
736 if(unlikely(external)) {
737 i = __tls_rand() % lanes.count;
738 }
739 else {
740 #if !defined(USE_PREFERRED)
741 processor * proc = kernelTLS().this_processor;
742 unsigned r = proc->rdq.its++;
743 i = proc->rdq.id + (r % READYQ_SHARD_FACTOR);
744 #else
745 i = start + (r++ % READYQ_SHARD_FACTOR);
746 #endif
747 }
748 // If we can't lock it retry
749 } while( !__atomic_try_acquire( &lanes.data[i].lock ) );
750
751 // Actually push it
752 push(lanes.data[i], thrd);
753
754 // Unlock and return
755 __atomic_unlock( &lanes.data[i].lock );
756
757 #if !defined(__CFA_NO_STATISTICS__)
758 if(unlikely(external)) __atomic_fetch_add(&cltr->stats->ready.push.extrn.success, 1, __ATOMIC_RELAXED);
759 else __tls_stats()->ready.push.local.success++;
760 #endif
761
762 __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);
763 }
764
765 // Pop from the ready queue from a given cluster
766 __attribute__((hot)) thread$ * pop_fast(struct cluster * cltr) with (cltr->ready_queue) {
767 /* paranoid */ verify( lanes.count > 0 );
768 /* paranoid */ verify( kernelTLS().this_processor );
769 /* paranoid */ verify( kernelTLS().this_processor->rdq.id < lanes.count );
770
771 processor * proc = kernelTLS().this_processor;
772
773 if(proc->rdq.target == MAX) {
774 unsigned long long min = ts(lanes.data[proc->rdq.id]);
775 for(int i = 0; i < READYQ_SHARD_FACTOR; i++) {
776 unsigned long long tsc = ts(lanes.data[proc->rdq.id + i]);
777 if(tsc < min) min = tsc;
778 }
779 proc->rdq.cutoff = min;
780 proc->rdq.target = __tls_rand() % lanes.count;
781 }
782 else {
783 unsigned target = proc->rdq.target;
784 proc->rdq.target = MAX;
785 const unsigned long long bias = 0; //2_500_000_000;
786 const unsigned long long cutoff = proc->rdq.cutoff > bias ? proc->rdq.cutoff - bias : proc->rdq.cutoff;
787 if(lanes.tscs[target].tv < cutoff && ts(lanes.data[target]) < cutoff) {
788 thread$ * t = try_pop(cltr, target __STATS(, __tls_stats()->ready.pop.help));
789 if(t) return t;
790 }
791 }
792
793 for(READYQ_SHARD_FACTOR) {
794 unsigned i = proc->rdq.id + (proc->rdq.itr++ % READYQ_SHARD_FACTOR);
795 if(thread$ * t = try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.local))) return t;
796 }
797 return 0p;
798 }
799
800 __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr) with (cltr->ready_queue) {
801 unsigned i = __tls_rand() % lanes.count;
802 return try_pop(cltr, i __STATS(, __tls_stats()->ready.pop.steal));
803 }
804
805 __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr) with (cltr->ready_queue) {
806 return search(cltr);
807 }
808#endif
809
810//=======================================================================
811// Various Ready Queue utilities
812//=======================================================================
813// these function work the same or almost the same
814// whether they are using work-stealing or relaxed fifo scheduling
815
816//-----------------------------------------------------------------------
817// try to pop from a lane given by index w
818static inline struct thread$ * try_pop(struct cluster * cltr, unsigned w __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
819 /* paranoid */ verify( w < lanes.count );
820 __STATS( stats.attempt++; )
821
822 // Get relevant elements locally
823 __intrusive_lane_t & lane = lanes.data[w];
824
825 // If list looks empty retry
826 if( is_empty(lane) ) {
827 return 0p;
828 }
829
830 // If we can't get the lock retry
831 if( !__atomic_try_acquire(&lane.lock) ) {
832 return 0p;
833 }
834
835 // If list is empty, unlock and retry
836 if( is_empty(lane) ) {
837 __atomic_unlock(&lane.lock);
838 return 0p;
839 }
840
841 // Actually pop the list
842 struct thread$ * thrd;
843 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING) || defined(USE_CPU_WORK_STEALING)
844 unsigned long long tsc_before = ts(lane);
845 #endif
846 unsigned long long tsv;
847 [thrd, tsv] = pop(lane);
848
849 /* paranoid */ verify(thrd);
850 /* paranoid */ verify(tsv);
851 /* paranoid */ verify(lane.lock);
852
853 // Unlock and return
854 __atomic_unlock(&lane.lock);
855
856 // Update statistics
857 __STATS( stats.success++; )
858
859 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING) || defined(USE_CPU_WORK_STEALING)
860 if (tsv != MAX) {
861 unsigned long long now = rdtscl();
862 unsigned long long pma = __atomic_load_n(&lanes.tscs[w].ma, __ATOMIC_RELAXED);
863 __atomic_store_n(&lanes.tscs[w].tv, tsv, __ATOMIC_RELAXED);
864 __atomic_store_n(&lanes.tscs[w].ma, moving_average(now, tsc_before, pma), __ATOMIC_RELAXED);
865 }
866 #endif
867
868 #if defined(USE_AWARE_STEALING) || defined(USE_CPU_WORK_STEALING)
869 thrd->preferred = w / READYQ_SHARD_FACTOR;
870 #else
871 thrd->preferred = w;
872 #endif
873
874 // return the popped thread
875 return thrd;
876}
877
878//-----------------------------------------------------------------------
879// try to pop from any lanes making sure you don't miss any threads push
880// before the start of the function
881static inline struct thread$ * search(struct cluster * cltr) with (cltr->ready_queue) {
882 /* paranoid */ verify( lanes.count > 0 );
883 unsigned count = __atomic_load_n( &lanes.count, __ATOMIC_RELAXED );
884 unsigned offset = __tls_rand();
885 for(i; count) {
886 unsigned idx = (offset + i) % count;
887 struct thread$ * thrd = try_pop(cltr, idx __STATS(, __tls_stats()->ready.pop.search));
888 if(thrd) {
889 return thrd;
890 }
891 }
892
893 // All lanes where empty return 0p
894 return 0p;
895}
896
897//-----------------------------------------------------------------------
898// get preferred ready for new thread
899unsigned ready_queue_new_preferred() {
900 unsigned pref = 0;
901 if(struct thread$ * thrd = publicTLS_get( this_thread )) {
902 pref = thrd->preferred;
903 }
904 else {
905 #if defined(USE_CPU_WORK_STEALING)
906 pref = __kernel_getcpu();
907 #endif
908 }
909
910 #if defined(USE_CPU_WORK_STEALING)
911 /* paranoid */ verify(pref >= 0);
912 /* paranoid */ verify(pref < cpu_info.hthrd_count);
913 #endif
914
915 return pref;
916}
917
918//-----------------------------------------------------------------------
919// Check that all the intrusive queues in the data structure are still consistent
920static void check( __ready_queue_t & q ) with (q) {
921 #if defined(__CFA_WITH_VERIFY__)
922 {
923 for( idx ; lanes.count ) {
924 __intrusive_lane_t & sl = lanes.data[idx];
925 assert(!lanes.data[idx].lock);
926
927 if(is_empty(sl)) {
928 assert( sl.anchor.next == 0p );
929 assert( sl.anchor.ts == -1llu );
930 assert( mock_head(sl) == sl.prev );
931 } else {
932 assert( sl.anchor.next != 0p );
933 assert( sl.anchor.ts != -1llu );
934 assert( mock_head(sl) != sl.prev );
935 }
936 }
937 }
938 #endif
939}
940
941//-----------------------------------------------------------------------
942// Given 2 indexes, pick the list with the oldest push an try to pop from it
943static inline struct thread$ * try_pop(struct cluster * cltr, unsigned i, unsigned j __STATS(, __stats_readyQ_pop_t & stats)) with (cltr->ready_queue) {
944 // Pick the bet list
945 int w = i;
946 if( __builtin_expect(!is_empty(lanes.data[j]), true) ) {
947 w = (ts(lanes.data[i]) < ts(lanes.data[j])) ? i : j;
948 }
949
950 return try_pop(cltr, w __STATS(, stats));
951}
952
953// Call this function of the intrusive list was moved using memcpy
954// fixes the list so that the pointers back to anchors aren't left dangling
955static inline void fix(__intrusive_lane_t & ll) {
956 if(is_empty(ll)) {
957 verify(ll.anchor.next == 0p);
958 ll.prev = mock_head(ll);
959 }
960}
961
962static void assign_list(unsigned & value, dlist(processor) & list, unsigned count) {
963 processor * it = &list`first;
964 for(unsigned i = 0; i < count; i++) {
965 /* paranoid */ verifyf( it, "Unexpected null iterator, at index %u of %u\n", i, count);
966 it->rdq.id = value;
967 it->rdq.target = MAX;
968 value += READYQ_SHARD_FACTOR;
969 it = &(*it)`next;
970 }
971}
972
973static void reassign_cltr_id(struct cluster * cltr) {
974 unsigned preferred = 0;
975 assign_list(preferred, cltr->procs.actives, cltr->procs.total - cltr->procs.idle);
976 assign_list(preferred, cltr->procs.idles , cltr->procs.idle );
977}
978
979static void fix_times( struct cluster * cltr ) with( cltr->ready_queue ) {
980 #if defined(USE_AWARE_STEALING) || defined(USE_WORK_STEALING)
981 lanes.tscs = alloc(lanes.count, lanes.tscs`realloc);
982 for(i; lanes.count) {
983 lanes.tscs[i].tv = rdtscl();
984 lanes.tscs[i].ma = 0;
985 }
986 #endif
987}
988
989#if defined(USE_CPU_WORK_STEALING)
990 // ready_queue size is fixed in this case
991 void ready_queue_grow(struct cluster * cltr) {}
992 void ready_queue_shrink(struct cluster * cltr) {}
993#else
994 // Grow the ready queue
995 void ready_queue_grow(struct cluster * cltr) {
996 size_t ncount;
997 int target = cltr->procs.total;
998
999 /* paranoid */ verify( ready_mutate_islocked() );
1000 __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue\n");
1001
1002 // Make sure that everything is consistent
1003 /* paranoid */ check( cltr->ready_queue );
1004
1005 // grow the ready queue
1006 with( cltr->ready_queue ) {
1007 // Find new count
1008 // Make sure we always have atleast 1 list
1009 if(target >= 2) {
1010 ncount = target * READYQ_SHARD_FACTOR;
1011 } else {
1012 ncount = SEQUENTIAL_SHARD;
1013 }
1014
1015 // Allocate new array (uses realloc and memcpies the data)
1016 lanes.data = alloc( ncount, lanes.data`realloc );
1017
1018 // Fix the moved data
1019 for( idx; (size_t)lanes.count ) {
1020 fix(lanes.data[idx]);
1021 }
1022
1023 // Construct new data
1024 for( idx; (size_t)lanes.count ~ ncount) {
1025 (lanes.data[idx]){};
1026 }
1027
1028 // Update original
1029 lanes.count = ncount;
1030
1031 lanes.caches = alloc( target, lanes.caches`realloc );
1032 }
1033
1034 fix_times(cltr);
1035
1036 reassign_cltr_id(cltr);
1037
1038 // Make sure that everything is consistent
1039 /* paranoid */ check( cltr->ready_queue );
1040
1041 __cfadbg_print_safe(ready_queue, "Kernel : Growing ready queue done\n");
1042
1043 /* paranoid */ verify( ready_mutate_islocked() );
1044 }
1045
1046 // Shrink the ready queue
1047 void ready_queue_shrink(struct cluster * cltr) {
1048 /* paranoid */ verify( ready_mutate_islocked() );
1049 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue\n");
1050
1051 // Make sure that everything is consistent
1052 /* paranoid */ check( cltr->ready_queue );
1053
1054 int target = cltr->procs.total;
1055
1056 with( cltr->ready_queue ) {
1057 // Remember old count
1058 size_t ocount = lanes.count;
1059
1060 // Find new count
1061 // Make sure we always have atleast 1 list
1062 lanes.count = target >= 2 ? target * READYQ_SHARD_FACTOR: SEQUENTIAL_SHARD;
1063 /* paranoid */ verify( ocount >= lanes.count );
1064 /* paranoid */ verify( lanes.count == target * READYQ_SHARD_FACTOR || target < 2 );
1065
1066 // for printing count the number of displaced threads
1067 #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
1068 __attribute__((unused)) size_t displaced = 0;
1069 #endif
1070
1071 // redistribute old data
1072 for( idx; (size_t)lanes.count ~ ocount) {
1073 // Lock is not strictly needed but makes checking invariants much easier
1074 __attribute__((unused)) bool locked = __atomic_try_acquire(&lanes.data[idx].lock);
1075 verify(locked);
1076
1077 // As long as we can pop from this lane to push the threads somewhere else in the queue
1078 while(!is_empty(lanes.data[idx])) {
1079 struct thread$ * thrd;
1080 unsigned long long _;
1081 [thrd, _] = pop(lanes.data[idx]);
1082
1083 push(cltr, thrd, true);
1084
1085 // for printing count the number of displaced threads
1086 #if defined(__CFA_DEBUG_PRINT__) || defined(__CFA_DEBUG_PRINT_READY_QUEUE__)
1087 displaced++;
1088 #endif
1089 }
1090
1091 // Unlock the lane
1092 __atomic_unlock(&lanes.data[idx].lock);
1093
1094 // TODO print the queue statistics here
1095
1096 ^(lanes.data[idx]){};
1097 }
1098
1099 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue displaced %zu threads\n", displaced);
1100
1101 // Allocate new array (uses realloc and memcpies the data)
1102 lanes.data = alloc( lanes.count, lanes.data`realloc );
1103
1104 // Fix the moved data
1105 for( idx; (size_t)lanes.count ) {
1106 fix(lanes.data[idx]);
1107 }
1108
1109 lanes.caches = alloc( target, lanes.caches`realloc );
1110 }
1111
1112 fix_times(cltr);
1113
1114
1115 reassign_cltr_id(cltr);
1116
1117 // Make sure that everything is consistent
1118 /* paranoid */ check( cltr->ready_queue );
1119
1120 __cfadbg_print_safe(ready_queue, "Kernel : Shrinking ready queue done\n");
1121 /* paranoid */ verify( ready_mutate_islocked() );
1122 }
1123#endif
1124
1125#if !defined(__CFA_NO_STATISTICS__)
1126 unsigned cnt(const __ready_queue_t & this, unsigned idx) {
1127 /* paranoid */ verify(this.lanes.count > idx);
1128 return this.lanes.data[idx].cnt;
1129 }
1130#endif
1131
1132
1133#if defined(CFA_HAVE_LINUX_LIBRSEQ)
1134 // No definition needed
1135#elif defined(CFA_HAVE_LINUX_RSEQ_H)
1136
1137 #if defined( __x86_64 ) || defined( __i386 )
1138 #define RSEQ_SIG 0x53053053
1139 #elif defined( __ARM_ARCH )
1140 #ifdef __ARMEB__
1141 #define RSEQ_SIG 0xf3def5e7 /* udf #24035 ; 0x5de3 (ARMv6+) */
1142 #else
1143 #define RSEQ_SIG 0xe7f5def3 /* udf #24035 ; 0x5de3 */
1144 #endif
1145 #endif
1146
1147 extern void __disable_interrupts_hard();
1148 extern void __enable_interrupts_hard();
1149
1150 static void __kernel_raw_rseq_register (void) {
1151 /* paranoid */ verify( __cfaabi_rseq.cpu_id == RSEQ_CPU_ID_UNINITIALIZED );
1152
1153 // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, (sigset_t *)0p, _NSIG / 8);
1154 int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), 0, RSEQ_SIG);
1155 if(ret != 0) {
1156 int e = errno;
1157 switch(e) {
1158 case EINVAL: abort("KERNEL ERROR: rseq register invalid argument");
1159 case ENOSYS: abort("KERNEL ERROR: rseq register no supported");
1160 case EFAULT: abort("KERNEL ERROR: rseq register with invalid argument");
1161 case EBUSY : abort("KERNEL ERROR: rseq register already registered");
1162 case EPERM : abort("KERNEL ERROR: rseq register sig argument on unregistration does not match the signature received on registration");
1163 default: abort("KERNEL ERROR: rseq register unexpected return %d", e);
1164 }
1165 }
1166 }
1167
1168 static void __kernel_raw_rseq_unregister(void) {
1169 /* paranoid */ verify( __cfaabi_rseq.cpu_id >= 0 );
1170
1171 // int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, (sigset_t *)0p, _NSIG / 8);
1172 int ret = syscall(__NR_rseq, &__cfaabi_rseq, sizeof(struct rseq), RSEQ_FLAG_UNREGISTER, RSEQ_SIG);
1173 if(ret != 0) {
1174 int e = errno;
1175 switch(e) {
1176 case EINVAL: abort("KERNEL ERROR: rseq unregister invalid argument");
1177 case ENOSYS: abort("KERNEL ERROR: rseq unregister no supported");
1178 case EFAULT: abort("KERNEL ERROR: rseq unregister with invalid argument");
1179 case EBUSY : abort("KERNEL ERROR: rseq unregister already registered");
1180 case EPERM : abort("KERNEL ERROR: rseq unregister sig argument on unregistration does not match the signature received on registration");
1181 default: abort("KERNEL ERROR: rseq unregisteunexpected return %d", e);
1182 }
1183 }
1184 }
1185#else
1186 // No definition needed
1187#endif
Note: See TracBrowser for help on using the repository browser.