[75f3522] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // |
---|
[708ae38] | 7 | // kernel/private.hfa -- |
---|
[75f3522] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Mon Feb 13 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[fd9b524] | 12 | // Last Modified On : Wed Aug 12 08:21:33 2020 |
---|
| 13 | // Update Count : 9 |
---|
[75f3522] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[75f3522] | 17 | |
---|
[3489ea6] | 18 | #if !defined(__cforall_thread__) |
---|
[708ae38] | 19 | #error kernel/private.hfa should only be included in libcfathread source |
---|
[3489ea6] | 20 | #endif |
---|
| 21 | |
---|
[58b6d1b] | 22 | #include "kernel.hfa" |
---|
| 23 | #include "thread.hfa" |
---|
[75f3522] | 24 | |
---|
[73abe95] | 25 | #include "alarm.hfa" |
---|
[8834751] | 26 | #include "stats.hfa" |
---|
[fa21ac9] | 27 | |
---|
[3489ea6] | 28 | extern "C" { |
---|
| 29 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
| 30 | #include <rseq/rseq.h> |
---|
| 31 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
[f558b5f] | 32 | #include <linux/rseq.h> |
---|
[3489ea6] | 33 | #else |
---|
| 34 | #ifndef _GNU_SOURCE |
---|
[708ae38] | 35 | #error kernel/private requires gnu_source |
---|
[3489ea6] | 36 | #endif |
---|
| 37 | #include <sched.h> |
---|
| 38 | #endif |
---|
| 39 | } |
---|
| 40 | |
---|
[d3605f8] | 41 | // Defines whether or not we *want* to use io_uring_enter as the idle_sleep blocking call |
---|
[f870e257] | 42 | // #define CFA_WANT_IO_URING_IDLE |
---|
[d3605f8] | 43 | |
---|
| 44 | // Defines whether or not we *can* use io_uring_enter as the idle_sleep blocking call |
---|
| 45 | #if defined(CFA_WANT_IO_URING_IDLE) && defined(CFA_HAVE_LINUX_IO_URING_H) |
---|
| 46 | #if defined(CFA_HAVE_IORING_OP_READ) || (defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV)) |
---|
| 47 | #define CFA_WITH_IO_URING_IDLE |
---|
| 48 | #endif |
---|
| 49 | #endif |
---|
[059ad16] | 50 | |
---|
[75f3522] | 51 | //----------------------------------------------------------------------------- |
---|
| 52 | // Scheduler |
---|
[1c273d0] | 53 | extern "C" { |
---|
[2026bb6] | 54 | void disable_interrupts() OPTIONAL_THREAD; |
---|
[a3821fa] | 55 | void enable_interrupts( bool poll = true ); |
---|
[1c273d0] | 56 | } |
---|
| 57 | |
---|
[24e321c] | 58 | void schedule_thread$( thread$ *, unpark_hint hint ) __attribute__((nonnull (1))); |
---|
[75f3522] | 59 | |
---|
[8fc652e0] | 60 | extern bool __preemption_enabled(); |
---|
| 61 | |
---|
[13fdf86] | 62 | enum { |
---|
| 63 | PREEMPT_NORMAL = 0, |
---|
| 64 | PREEMPT_TERMINATE = 1, |
---|
| 65 | PREEMPT_IO = 2, |
---|
| 66 | }; |
---|
| 67 | |
---|
[7d0ebd0] | 68 | static inline void __disable_interrupts_checked() { |
---|
| 69 | /* paranoid */ verify( __preemption_enabled() ); |
---|
| 70 | disable_interrupts(); |
---|
| 71 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | static inline void __enable_interrupts_checked( bool poll = true ) { |
---|
| 75 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 76 | enable_interrupts( poll ); |
---|
| 77 | /* paranoid */ verify( __preemption_enabled() ); |
---|
| 78 | } |
---|
| 79 | |
---|
[5afb49a] | 80 | //release/wake-up the following resources |
---|
[e84ab3d] | 81 | void __thread_finish( thread$ * thrd ); |
---|
[db6f06a] | 82 | |
---|
[3489ea6] | 83 | //----------------------------------------------------------------------------- |
---|
| 84 | // Hardware |
---|
| 85 | |
---|
| 86 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
| 87 | // No data needed |
---|
| 88 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
| 89 | extern "Cforall" { |
---|
[2af1943] | 90 | extern __attribute__((aligned(64))) thread_local volatile struct rseq __cfaabi_rseq; |
---|
[3489ea6] | 91 | } |
---|
| 92 | #else |
---|
| 93 | // No data needed |
---|
| 94 | #endif |
---|
| 95 | |
---|
| 96 | static inline int __kernel_getcpu() { |
---|
| 97 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 98 | #if defined(CFA_HAVE_LINUX_LIBRSEQ) |
---|
[f558b5f] | 99 | return rseq_current_cpu(); |
---|
[3489ea6] | 100 | #elif defined(CFA_HAVE_LINUX_RSEQ_H) |
---|
[f558b5f] | 101 | int r = __cfaabi_rseq.cpu_id; |
---|
| 102 | /* paranoid */ verify( r >= 0 ); |
---|
| 103 | return r; |
---|
[3489ea6] | 104 | #else |
---|
| 105 | return sched_getcpu(); |
---|
| 106 | #endif |
---|
| 107 | } |
---|
| 108 | |
---|
[75f3522] | 109 | //----------------------------------------------------------------------------- |
---|
| 110 | // Processor |
---|
[c18bf9e] | 111 | void main(processorCtx_t &); |
---|
| 112 | static inline coroutine$* get_coroutine(processorCtx_t & this) { return &this.self; } |
---|
[85b1deb] | 113 | |
---|
[8c50aed] | 114 | void * __create_pthread( pthread_t *, void * (*)(void *), void * ); |
---|
[bfcf6b9] | 115 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval ); |
---|
[1805b1b] | 116 | |
---|
[6502a2b] | 117 | extern cluster * mainCluster; |
---|
| 118 | |
---|
[75f3522] | 119 | //----------------------------------------------------------------------------- |
---|
| 120 | // Threads |
---|
| 121 | extern "C" { |
---|
[c7a900a] | 122 | void __cfactx_invoke_thread(void (*main)(void *), void * this); |
---|
[75f3522] | 123 | } |
---|
| 124 | |
---|
[f7d6bb0] | 125 | __cfaabi_dbg_debug_do( |
---|
[e84ab3d] | 126 | extern void __cfaabi_dbg_thread_register ( thread$ * thrd ); |
---|
| 127 | extern void __cfaabi_dbg_thread_unregister( thread$ * thrd ); |
---|
[f7d6bb0] | 128 | ) |
---|
| 129 | |
---|
[6a77224] | 130 | #define TICKET_BLOCKED (-1) // thread is blocked |
---|
| 131 | #define TICKET_RUNNING ( 0) // thread is running |
---|
| 132 | #define TICKET_UNBLOCK ( 1) // thread should ignore next block |
---|
| 133 | |
---|
[969b3fe] | 134 | //----------------------------------------------------------------------------- |
---|
| 135 | // Utils |
---|
[e84ab3d] | 136 | void doregister( struct cluster * cltr, struct thread$ & thrd ); |
---|
| 137 | void unregister( struct cluster * cltr, struct thread$ & thrd ); |
---|
[de94a60] | 138 | |
---|
[f00b26d4] | 139 | //----------------------------------------------------------------------------- |
---|
| 140 | // I/O |
---|
[8bee858] | 141 | io_arbiter$ * create(void); |
---|
| 142 | void destroy(io_arbiter$ *); |
---|
[f00b26d4] | 143 | |
---|
[7768b8d] | 144 | //======================================================================= |
---|
| 145 | // Cluster lock API |
---|
| 146 | //======================================================================= |
---|
| 147 | // Lock-Free registering/unregistering of threads |
---|
| 148 | // Register a processor to a given cluster and get its unique id in return |
---|
[c993b15] | 149 | unsigned register_proc_id( void ); |
---|
[7768b8d] | 150 | |
---|
| 151 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
[c993b15] | 152 | void unregister_proc_id( unsigned ); |
---|
[7768b8d] | 153 | |
---|
| 154 | //======================================================================= |
---|
| 155 | // Reader-writer lock implementation |
---|
| 156 | // Concurrent with doregister/unregister, |
---|
| 157 | // i.e., threads can be added at any point during or between the entry/exit |
---|
[dca5802] | 158 | |
---|
| 159 | //----------------------------------------------------------------------- |
---|
| 160 | // simple spinlock underlying the RWLock |
---|
| 161 | // Blocking acquire |
---|
[7768b8d] | 162 | static inline void __atomic_acquire(volatile bool * ll) { |
---|
| 163 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) { |
---|
| 164 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED)) |
---|
[fd9b524] | 165 | Pause(); |
---|
[7768b8d] | 166 | } |
---|
| 167 | /* paranoid */ verify(*ll); |
---|
| 168 | } |
---|
| 169 | |
---|
[dca5802] | 170 | // Non-Blocking acquire |
---|
[7768b8d] | 171 | static inline bool __atomic_try_acquire(volatile bool * ll) { |
---|
[b798713] | 172 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST); |
---|
[7768b8d] | 173 | } |
---|
| 174 | |
---|
[dca5802] | 175 | // Release |
---|
[7768b8d] | 176 | static inline void __atomic_unlock(volatile bool * ll) { |
---|
| 177 | /* paranoid */ verify(*ll); |
---|
| 178 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE); |
---|
| 179 | } |
---|
| 180 | |
---|
[b388ee81] | 181 | //----------------------------------------------------------------------- |
---|
| 182 | // Reader-Writer lock protecting the ready-queues |
---|
| 183 | // while this lock is mostly generic some aspects |
---|
| 184 | // have been hard-coded to for the ready-queue for |
---|
| 185 | // simplicity and performance |
---|
[741e22c] | 186 | union __attribute__((aligned(64))) __scheduler_RWLock_t { |
---|
| 187 | struct { |
---|
| 188 | // total cachelines allocated |
---|
| 189 | unsigned int max; |
---|
[b388ee81] | 190 | |
---|
[741e22c] | 191 | // cachelines currently in use |
---|
| 192 | volatile unsigned int alloc; |
---|
[b388ee81] | 193 | |
---|
[741e22c] | 194 | // cachelines ready to itereate over |
---|
| 195 | // (!= to alloc when thread is in second half of doregister) |
---|
| 196 | volatile unsigned int ready; |
---|
[b388ee81] | 197 | |
---|
[741e22c] | 198 | // writer lock |
---|
| 199 | volatile bool write_lock; |
---|
[b388ee81] | 200 | |
---|
[741e22c] | 201 | // data pointer |
---|
| 202 | volatile bool * volatile * data; |
---|
| 203 | } lock; |
---|
| 204 | char pad[192]; |
---|
[b388ee81] | 205 | }; |
---|
| 206 | |
---|
| 207 | void ?{}(__scheduler_RWLock_t & this); |
---|
| 208 | void ^?{}(__scheduler_RWLock_t & this); |
---|
| 209 | |
---|
| 210 | extern __scheduler_RWLock_t * __scheduler_lock; |
---|
| 211 | |
---|
[7768b8d] | 212 | //----------------------------------------------------------------------- |
---|
| 213 | // Reader side : acquire when using the ready queue to schedule but not |
---|
| 214 | // creating/destroying queues |
---|
[741e22c] | 215 | static inline void ready_schedule_lock(void) with(__scheduler_lock->lock) { |
---|
[8fc652e0] | 216 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[c993b15] | 217 | /* paranoid */ verify( ! kernelTLS().in_sched_lock ); |
---|
| 218 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock ); |
---|
| 219 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id ); |
---|
[7768b8d] | 220 | |
---|
| 221 | // Step 1 : make sure no writer are in the middle of the critical section |
---|
[c993b15] | 222 | while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED)) |
---|
[fd9b524] | 223 | Pause(); |
---|
[7768b8d] | 224 | |
---|
| 225 | // Fence needed because we don't want to start trying to acquire the lock |
---|
| 226 | // before we read a false. |
---|
| 227 | // Not needed on x86 |
---|
| 228 | // std::atomic_thread_fence(std::memory_order_seq_cst); |
---|
| 229 | |
---|
| 230 | // Step 2 : acquire our local lock |
---|
[c993b15] | 231 | __atomic_acquire( &kernelTLS().sched_lock ); |
---|
| 232 | /*paranoid*/ verify(kernelTLS().sched_lock); |
---|
[64a7146] | 233 | |
---|
| 234 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 235 | // Debug, check if this is owned for reading |
---|
[c993b15] | 236 | kernelTLS().in_sched_lock = true; |
---|
[64a7146] | 237 | #endif |
---|
[7768b8d] | 238 | } |
---|
| 239 | |
---|
[741e22c] | 240 | static inline void ready_schedule_unlock(void) with(__scheduler_lock->lock) { |
---|
[8fc652e0] | 241 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[c993b15] | 242 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock ); |
---|
| 243 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id ); |
---|
| 244 | /* paranoid */ verify( kernelTLS().sched_lock ); |
---|
| 245 | /* paranoid */ verify( kernelTLS().in_sched_lock ); |
---|
[64a7146] | 246 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 247 | // Debug, check if this is owned for reading |
---|
[c993b15] | 248 | kernelTLS().in_sched_lock = false; |
---|
[64a7146] | 249 | #endif |
---|
[c993b15] | 250 | __atomic_unlock(&kernelTLS().sched_lock); |
---|
[7768b8d] | 251 | } |
---|
| 252 | |
---|
[64a7146] | 253 | #ifdef __CFA_WITH_VERIFY__ |
---|
[e873838] | 254 | static inline bool ready_schedule_islocked(void) { |
---|
[8fc652e0] | 255 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
[c993b15] | 256 | /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock ); |
---|
| 257 | return kernelTLS().sched_lock; |
---|
[64a7146] | 258 | } |
---|
| 259 | |
---|
| 260 | static inline bool ready_mutate_islocked() { |
---|
[741e22c] | 261 | return __scheduler_lock->lock.write_lock; |
---|
[64a7146] | 262 | } |
---|
| 263 | #endif |
---|
| 264 | |
---|
[7768b8d] | 265 | //----------------------------------------------------------------------- |
---|
| 266 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
| 267 | // queues or removing them. |
---|
[b388ee81] | 268 | uint_fast32_t ready_mutate_lock( void ); |
---|
[7768b8d] | 269 | |
---|
[b388ee81] | 270 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ ); |
---|
[7768b8d] | 271 | |
---|
[a33c113] | 272 | //----------------------------------------------------------------------- |
---|
| 273 | // Lock-Free registering/unregistering of threads |
---|
| 274 | // Register a processor to a given cluster and get its unique id in return |
---|
| 275 | // For convenience, also acquires the lock |
---|
[c993b15] | 276 | static inline [unsigned, uint_fast32_t] ready_mutate_register() { |
---|
| 277 | unsigned id = register_proc_id(); |
---|
| 278 | uint_fast32_t last = ready_mutate_lock(); |
---|
| 279 | return [id, last]; |
---|
[a33c113] | 280 | } |
---|
| 281 | |
---|
| 282 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
| 283 | // assumes the lock is acquired |
---|
[c993b15] | 284 | static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) { |
---|
[a33c113] | 285 | ready_mutate_unlock( last_s ); |
---|
[c993b15] | 286 | unregister_proc_id( id ); |
---|
[a33c113] | 287 | } |
---|
| 288 | |
---|
[a7504db] | 289 | //----------------------------------------------------------------------- |
---|
| 290 | // Cluster idle lock/unlock |
---|
[6a9b12b] | 291 | static inline void lock(__cluster_proc_list & this) { |
---|
[a7504db] | 292 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 293 | |
---|
| 294 | // Start by locking the global RWlock so that we know no-one is |
---|
| 295 | // adding/removing processors while we mess with the idle lock |
---|
| 296 | ready_schedule_lock(); |
---|
| 297 | |
---|
[a633f6f] | 298 | lock( this.lock __cfaabi_dbg_ctx2 ); |
---|
[a7504db] | 299 | |
---|
| 300 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 301 | } |
---|
| 302 | |
---|
[5f5a729] | 303 | static inline bool try_lock(__cluster_proc_list & this) { |
---|
| 304 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 305 | |
---|
| 306 | // Start by locking the global RWlock so that we know no-one is |
---|
| 307 | // adding/removing processors while we mess with the idle lock |
---|
| 308 | ready_schedule_lock(); |
---|
| 309 | |
---|
[a633f6f] | 310 | if(try_lock( this.lock __cfaabi_dbg_ctx2 )) { |
---|
[5f5a729] | 311 | // success |
---|
| 312 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 313 | return true; |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | // failed to lock |
---|
| 317 | ready_schedule_unlock(); |
---|
| 318 | |
---|
| 319 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 320 | return false; |
---|
| 321 | } |
---|
| 322 | |
---|
[6a9b12b] | 323 | static inline void unlock(__cluster_proc_list & this) { |
---|
[a7504db] | 324 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 325 | |
---|
[a633f6f] | 326 | unlock(this.lock); |
---|
[a7504db] | 327 | |
---|
| 328 | // Release the global lock, which we acquired when locking |
---|
| 329 | ready_schedule_unlock(); |
---|
| 330 | |
---|
| 331 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 332 | } |
---|
| 333 | |
---|
[b798713] | 334 | //======================================================================= |
---|
| 335 | // Ready-Queue API |
---|
[dca5802] | 336 | //----------------------------------------------------------------------- |
---|
| 337 | // push thread onto a ready queue for a cluster |
---|
| 338 | // returns true if the list was previously empty, false otherwise |
---|
[24e321c] | 339 | __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint); |
---|
[dca5802] | 340 | |
---|
| 341 | //----------------------------------------------------------------------- |
---|
[fc59df78] | 342 | // pop thread from the local queues of a cluster |
---|
[dca5802] | 343 | // returns 0p if empty |
---|
[1eb239e4] | 344 | // May return 0p spuriously |
---|
[e84ab3d] | 345 | __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr); |
---|
[dca5802] | 346 | |
---|
[1eb239e4] | 347 | //----------------------------------------------------------------------- |
---|
[fc59df78] | 348 | // pop thread from any ready queue of a cluster |
---|
[1eb239e4] | 349 | // returns 0p if empty |
---|
[fc59df78] | 350 | // May return 0p spuriously |
---|
[e84ab3d] | 351 | __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr); |
---|
[1eb239e4] | 352 | |
---|
[fc59df78] | 353 | //----------------------------------------------------------------------- |
---|
| 354 | // search all ready queues of a cluster for any thread |
---|
| 355 | // returns 0p if empty |
---|
| 356 | // guaranteed to find any threads added before this call |
---|
[e84ab3d] | 357 | __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr); |
---|
[fc59df78] | 358 | |
---|
[24e321c] | 359 | //----------------------------------------------------------------------- |
---|
| 360 | // get preferred ready for new thread |
---|
| 361 | unsigned ready_queue_new_preferred(); |
---|
| 362 | |
---|
[dca5802] | 363 | //----------------------------------------------------------------------- |
---|
| 364 | // Increase the width of the ready queue (number of lanes) by 4 |
---|
[a017ee7] | 365 | void ready_queue_grow (struct cluster * cltr); |
---|
[dca5802] | 366 | |
---|
| 367 | //----------------------------------------------------------------------- |
---|
| 368 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
[a017ee7] | 369 | void ready_queue_shrink(struct cluster * cltr); |
---|
[b798713] | 370 | |
---|
[884f3f67] | 371 | //----------------------------------------------------------------------- |
---|
| 372 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
| 373 | void ready_queue_close(struct cluster * cltr); |
---|
| 374 | |
---|
[75f3522] | 375 | // Local Variables: // |
---|
| 376 | // mode: c // |
---|
| 377 | // tab-width: 4 // |
---|
[4aa2fb2] | 378 | // End: // |
---|