| 1 | /* |
|---|
| 2 | * scheduler.c - Core scheduler implementation. |
|---|
| 3 | * |
|---|
| 4 | * Author Alain Greiner (2016,2017,2018) |
|---|
| 5 | * |
|---|
| 6 | * Copyright (c) UPMC Sorbonne Universites |
|---|
| 7 | * |
|---|
| 8 | * This file is part of ALMOS-MKH. |
|---|
| 9 | * |
|---|
| 10 | * ALMOS-MKH. is free software; you can redistribute it and/or modify it |
|---|
| 11 | * under the terms of the GNU General Public License as published by |
|---|
| 12 | * the Free Software Foundation; version 2.0 of the License. |
|---|
| 13 | * |
|---|
| 14 | * ALMOS-MKH. is distributed in the hope that it will be useful, but |
|---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 17 | * General Public License for more details. |
|---|
| 18 | * |
|---|
| 19 | * You should have received a copy of the GNU General Public License |
|---|
| 20 | * along with ALMOS-MKH.; if not, write to the Free Software Foundation, |
|---|
| 21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #include <kernel_config.h> |
|---|
| 25 | #include <hal_kernel_types.h> |
|---|
| 26 | #include <hal_switch.h> |
|---|
| 27 | #include <hal_irqmask.h> |
|---|
| 28 | #include <hal_context.h> |
|---|
| 29 | #include <printk.h> |
|---|
| 30 | #include <list.h> |
|---|
| 31 | #include <core.h> |
|---|
| 32 | #include <thread.h> |
|---|
| 33 | #include <chdev.h> |
|---|
| 34 | #include <scheduler.h> |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 38 | // global variables |
|---|
| 39 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 40 | |
|---|
| 41 | extern chdev_directory_t chdev_dir; // allocated in kernel_init.c |
|---|
| 42 | extern process_t process_zero; // allocated in kernel_init.c |
|---|
| 43 | |
|---|
| 44 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 45 | // private functions |
|---|
| 46 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 50 | // This static function does NOT modify the scheduler state. |
|---|
| 51 | // It just select a thread in the list of attached threads, implementing the following |
|---|
| 52 | // three steps policy: |
|---|
| 53 | // 1) It scan the list of kernel threads, from the next thread after the last executed one, |
|---|
| 54 | // and returns the first runnable found : not IDLE, not blocked, client queue not empty. |
|---|
| 55 | // It can be the current thread. |
|---|
| 56 | // 2) If no kernel thread found, it scan the list of user thread, from the next thread after |
|---|
| 57 | // the last executed one, and returns the first runable found : not blocked. |
|---|
| 58 | // It can be the current thread. |
|---|
| 59 | // 3) If no runable thread found, it returns the idle thread. |
|---|
| 60 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 61 | // @ sched : local pointer on scheduler. |
|---|
| 62 | // @ returns pointer on selected thread descriptor |
|---|
| 63 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 64 | thread_t * sched_select( scheduler_t * sched ) |
|---|
| 65 | { |
|---|
| 66 | thread_t * thread; |
|---|
| 67 | list_entry_t * current; |
|---|
| 68 | list_entry_t * last; |
|---|
| 69 | list_entry_t * root; |
|---|
| 70 | bool_t done; |
|---|
| 71 | uint32_t count; |
|---|
| 72 | |
|---|
| 73 | // first : scan the kernel threads list if not empty |
|---|
| 74 | if( list_is_empty( &sched->k_root ) == false ) |
|---|
| 75 | { |
|---|
| 76 | root = &sched->k_root; |
|---|
| 77 | last = sched->k_last; |
|---|
| 78 | done = false; |
|---|
| 79 | count = 0; |
|---|
| 80 | current = last; |
|---|
| 81 | |
|---|
| 82 | while( done == false ) |
|---|
| 83 | { |
|---|
| 84 | |
|---|
| 85 | // check kernel threads list |
|---|
| 86 | assert( (count < sched->k_threads_nr), "bad kernel threads list" ); |
|---|
| 87 | |
|---|
| 88 | // get next entry in kernel list |
|---|
| 89 | current = current->next; |
|---|
| 90 | |
|---|
| 91 | // check exit condition |
|---|
| 92 | if( current == last ) done = true; |
|---|
| 93 | |
|---|
| 94 | // skip the root that does not contain a thread |
|---|
| 95 | if( current == root ) continue; |
|---|
| 96 | else count++; |
|---|
| 97 | |
|---|
| 98 | // get thread pointer for this entry |
|---|
| 99 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
|---|
| 100 | |
|---|
| 101 | // select kernel thread if non blocked and non THREAD_IDLE |
|---|
| 102 | if( (thread->blocked == 0) && (thread->type != THREAD_IDLE) ) return thread; |
|---|
| 103 | |
|---|
| 104 | } // end loop on kernel threads |
|---|
| 105 | } // end kernel threads |
|---|
| 106 | |
|---|
| 107 | // second : scan the user threads list if not empty |
|---|
| 108 | if( list_is_empty( &sched->u_root ) == false ) |
|---|
| 109 | { |
|---|
| 110 | root = &sched->u_root; |
|---|
| 111 | last = sched->u_last; |
|---|
| 112 | done = false; |
|---|
| 113 | count = 0; |
|---|
| 114 | current = last; |
|---|
| 115 | |
|---|
| 116 | while( done == false ) |
|---|
| 117 | { |
|---|
| 118 | |
|---|
| 119 | // check user threads list |
|---|
| 120 | assert( (count < sched->u_threads_nr), "bad user threads list" ); |
|---|
| 121 | |
|---|
| 122 | // get next entry in user list |
|---|
| 123 | current = current->next; |
|---|
| 124 | |
|---|
| 125 | // check exit condition |
|---|
| 126 | if( current == last ) done = true; |
|---|
| 127 | |
|---|
| 128 | // skip the root that does not contain a thread |
|---|
| 129 | if( current == root ) continue; |
|---|
| 130 | else count++; |
|---|
| 131 | |
|---|
| 132 | // get thread pointer for this entry |
|---|
| 133 | thread = LIST_ELEMENT( current , thread_t , sched_list ); |
|---|
| 134 | |
|---|
| 135 | // select thread if non blocked |
|---|
| 136 | if( thread->blocked == 0 ) return thread; |
|---|
| 137 | |
|---|
| 138 | } // end loop on user threads |
|---|
| 139 | } // end user threads |
|---|
| 140 | |
|---|
| 141 | // third : return idle thread if no other runnable thread |
|---|
| 142 | return sched->idle; |
|---|
| 143 | |
|---|
| 144 | } // end sched_select() |
|---|
| 145 | |
|---|
| 146 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 147 | // This static function is the only function that can actually delete a thread, |
|---|
| 148 | // and the associated process descriptor, if required. |
|---|
| 149 | // It is private, because it is called by the sched_yield() public function. |
|---|
| 150 | // It scan all threads attached to a given scheduler, and executes the relevant |
|---|
| 151 | // actions for two types of pending requests: |
|---|
| 152 | // |
|---|
| 153 | // - REQ_ACK : it checks that target thread is blocked, decrements the response counter |
|---|
| 154 | // to acknowledge the client thread, and reset the pending request. |
|---|
| 155 | // - REQ_DELETE : it removes the target thread from the process th_tbl[], remove it |
|---|
| 156 | // from the scheduler list, and release the memory allocated to thread descriptor. |
|---|
| 157 | // For an user thread, it destroys the process descriptor it the target thread is |
|---|
| 158 | // the last thread in the local process descriptor. |
|---|
| 159 | // |
|---|
| 160 | // Implementation note: |
|---|
| 161 | // We use a while to scan the threads in scheduler lists, because some threads can |
|---|
| 162 | // be destroyed, and we want not use a LIST_FOREACH() |
|---|
| 163 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 164 | // @ core : local pointer on the core descriptor. |
|---|
| 165 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 166 | static void sched_handle_signals( core_t * core ) |
|---|
| 167 | { |
|---|
| 168 | |
|---|
| 169 | list_entry_t * iter; |
|---|
| 170 | list_entry_t * root; |
|---|
| 171 | thread_t * thread; |
|---|
| 172 | process_t * process; |
|---|
| 173 | scheduler_t * sched; |
|---|
| 174 | uint32_t threads_nr; // number of threads in scheduler list |
|---|
| 175 | ltid_t ltid; // thread local index |
|---|
| 176 | uint32_t count; // number of threads in local process |
|---|
| 177 | |
|---|
| 178 | // get pointer on scheduler |
|---|
| 179 | sched = &core->scheduler; |
|---|
| 180 | |
|---|
| 181 | /////////////// scan user threads to handle both ACK and DELETE requests |
|---|
| 182 | root = &sched->u_root; |
|---|
| 183 | iter = root->next; |
|---|
| 184 | while( iter != root ) |
|---|
| 185 | { |
|---|
| 186 | // get pointer on thread |
|---|
| 187 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
|---|
| 188 | |
|---|
| 189 | // increment iterator |
|---|
| 190 | iter = iter->next; |
|---|
| 191 | |
|---|
| 192 | // handle REQ_ACK |
|---|
| 193 | if( thread->flags & THREAD_FLAG_REQ_ACK ) |
|---|
| 194 | { |
|---|
| 195 | |
|---|
| 196 | // check thread blocked |
|---|
| 197 | assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , "thread not blocked" ); |
|---|
| 198 | |
|---|
| 199 | // decrement response counter |
|---|
| 200 | hal_atomic_add( thread->ack_rsp_count , -1 ); |
|---|
| 201 | |
|---|
| 202 | // reset REQ_ACK in thread descriptor |
|---|
| 203 | thread_reset_req_ack( thread ); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | // handle REQ_DELETE only if target thread != calling thread |
|---|
| 207 | if( (thread->flags & THREAD_FLAG_REQ_DELETE) && (thread != CURRENT_THREAD) ) |
|---|
| 208 | { |
|---|
| 209 | // get thread process descriptor |
|---|
| 210 | process = thread->process; |
|---|
| 211 | |
|---|
| 212 | // get thread ltid |
|---|
| 213 | ltid = LTID_FROM_TRDID( thread->trdid); |
|---|
| 214 | |
|---|
| 215 | // take the lock protecting sheduler state |
|---|
| 216 | busylock_acquire( &sched->lock ); |
|---|
| 217 | |
|---|
| 218 | // update scheduler state |
|---|
| 219 | threads_nr = sched->u_threads_nr; |
|---|
| 220 | sched->u_threads_nr = threads_nr - 1; |
|---|
| 221 | list_unlink( &thread->sched_list ); |
|---|
| 222 | if( sched->u_last == &thread->sched_list ) |
|---|
| 223 | { |
|---|
| 224 | if( threads_nr == 1 ) |
|---|
| 225 | { |
|---|
| 226 | sched->u_last = NULL; |
|---|
| 227 | } |
|---|
| 228 | else if( sched->u_root.next == &thread->sched_list ) |
|---|
| 229 | { |
|---|
| 230 | sched->u_last = sched->u_root.pred; |
|---|
| 231 | } |
|---|
| 232 | else |
|---|
| 233 | { |
|---|
| 234 | sched->u_last = sched->u_root.next; |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | // release the lock protecting sheduler state |
|---|
| 239 | busylock_release( &sched->lock ); |
|---|
| 240 | |
|---|
| 241 | // check th_nr value |
|---|
| 242 | assert( (process->th_nr > 0) , "process th_nr cannot be 0\n" ); |
|---|
| 243 | |
|---|
| 244 | // remove thread from process th_tbl[] |
|---|
| 245 | process->th_tbl[ltid] = NULL; |
|---|
| 246 | count = hal_atomic_add( &process->th_nr , - 1 ); |
|---|
| 247 | |
|---|
| 248 | // release memory allocated for thread descriptor |
|---|
| 249 | thread_destroy( thread ); |
|---|
| 250 | |
|---|
| 251 | hal_fence(); |
|---|
| 252 | |
|---|
| 253 | #if DEBUG_SCHED_HANDLE_SIGNALS |
|---|
| 254 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 255 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
|---|
| 256 | printk("\n[DBG] %s : thread[%x,%x] on core[%x,%d] deleted / cycle %d\n", |
|---|
| 257 | __FUNCTION__ , process->pid , thread->trdid , local_cxy , thread->core->lid , cycle ); |
|---|
| 258 | #endif |
|---|
| 259 | // destroy process descriptor if last thread |
|---|
| 260 | if( count == 1 ) |
|---|
| 261 | { |
|---|
| 262 | // delete process |
|---|
| 263 | process_destroy( process ); |
|---|
| 264 | |
|---|
| 265 | #if DEBUG_SCHED_HANDLE_SIGNALS |
|---|
| 266 | cycle = (uint32_t)hal_get_cycles(); |
|---|
| 267 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
|---|
| 268 | printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n", |
|---|
| 269 | __FUNCTION__ , process->pid , local_cxy , cycle ); |
|---|
| 270 | #endif |
|---|
| 271 | } |
|---|
| 272 | } |
|---|
| 273 | } // end user threads |
|---|
| 274 | |
|---|
| 275 | ////// scan kernel threads for DELETE only |
|---|
| 276 | root = &sched->k_root; |
|---|
| 277 | iter = root->next; |
|---|
| 278 | while( iter != root ) |
|---|
| 279 | { |
|---|
| 280 | // get pointer on thread |
|---|
| 281 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
|---|
| 282 | |
|---|
| 283 | // increment iterator |
|---|
| 284 | iter = iter->next; |
|---|
| 285 | |
|---|
| 286 | // handle REQ_DELETE only if target thread != calling thread |
|---|
| 287 | if( (thread->flags & THREAD_FLAG_REQ_DELETE) && (thread != CURRENT_THREAD) ) |
|---|
| 288 | { |
|---|
| 289 | |
|---|
| 290 | // check process descriptor is local kernel process |
|---|
| 291 | assert( ( thread->process == &process_zero ) , "illegal process descriptor\n"); |
|---|
| 292 | |
|---|
| 293 | // get thread ltid |
|---|
| 294 | ltid = LTID_FROM_TRDID( thread->trdid); |
|---|
| 295 | |
|---|
| 296 | // take the lock protecting sheduler state |
|---|
| 297 | busylock_acquire( &sched->lock ); |
|---|
| 298 | |
|---|
| 299 | // update scheduler state |
|---|
| 300 | threads_nr = sched->k_threads_nr; |
|---|
| 301 | sched->k_threads_nr = threads_nr - 1; |
|---|
| 302 | list_unlink( &thread->sched_list ); |
|---|
| 303 | if( sched->k_last == &thread->sched_list ) |
|---|
| 304 | { |
|---|
| 305 | if( threads_nr == 1 ) |
|---|
| 306 | { |
|---|
| 307 | sched->k_last = NULL; |
|---|
| 308 | } |
|---|
| 309 | else if( sched->k_root.next == &thread->sched_list ) |
|---|
| 310 | { |
|---|
| 311 | sched->k_last = sched->k_root.pred; |
|---|
| 312 | } |
|---|
| 313 | else |
|---|
| 314 | { |
|---|
| 315 | sched->k_last = sched->k_root.next; |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | // release the lock protecting sheduler state |
|---|
| 320 | busylock_release( &sched->lock ); |
|---|
| 321 | |
|---|
| 322 | // get number of threads in local kernel process |
|---|
| 323 | count = process_zero.th_nr; |
|---|
| 324 | |
|---|
| 325 | // check th_nr value |
|---|
| 326 | assert( (process_zero.th_nr > 0) , "kernel process th_nr cannot be 0\n" ); |
|---|
| 327 | |
|---|
| 328 | // remove thread from process th_tbl[] |
|---|
| 329 | process_zero.th_tbl[ltid] = NULL; |
|---|
| 330 | hal_atomic_add( &process_zero.th_nr , - 1 ); |
|---|
| 331 | |
|---|
| 332 | // delete thread descriptor |
|---|
| 333 | thread_destroy( thread ); |
|---|
| 334 | |
|---|
| 335 | #if DEBUG_SCHED_HANDLE_SIGNALS |
|---|
| 336 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 337 | if( DEBUG_SCHED_HANDLE_SIGNALS < cycle ) |
|---|
| 338 | printk("\n[DBG] %s : thread[%x,%x] on core[%x,%d] deleted / cycle %d\n", |
|---|
| 339 | __FUNCTION__ , process_zero.pid , thread->trdid , local_cxy , thread->core->lid , cycle ); |
|---|
| 340 | #endif |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | } // end sched_handle_signals() |
|---|
| 344 | |
|---|
| 345 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 346 | // This static function is called by the sched_yield function when the RFC_FIFO |
|---|
| 347 | // associated to the core is not empty. |
|---|
| 348 | // It search an idle RPC thread for this core, and unblock it if found. |
|---|
| 349 | // It creates a new RPC thread if no idle RPC thread is found. |
|---|
| 350 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 351 | // @ sched : local pointer on scheduler. |
|---|
| 352 | //////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 353 | static void sched_rpc_activate( scheduler_t * sched ) |
|---|
| 354 | { |
|---|
| 355 | error_t error; |
|---|
| 356 | thread_t * thread; |
|---|
| 357 | list_entry_t * iter; |
|---|
| 358 | lid_t lid = CURRENT_THREAD->core->lid; |
|---|
| 359 | bool_t found = false; |
|---|
| 360 | |
|---|
| 361 | // search one IDLE RPC thread associated to the selected core |
|---|
| 362 | LIST_FOREACH( &sched->k_root , iter ) |
|---|
| 363 | { |
|---|
| 364 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
|---|
| 365 | |
|---|
| 366 | if( (thread->type == THREAD_RPC) && |
|---|
| 367 | (thread->blocked == THREAD_BLOCKED_IDLE ) ) |
|---|
| 368 | { |
|---|
| 369 | found = true; |
|---|
| 370 | break; |
|---|
| 371 | } |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | if( found == false ) // create new RPC thread |
|---|
| 375 | { |
|---|
| 376 | error = thread_kernel_create( &thread, |
|---|
| 377 | THREAD_RPC, |
|---|
| 378 | &rpc_thread_func, |
|---|
| 379 | NULL, |
|---|
| 380 | lid ); |
|---|
| 381 | // check memory |
|---|
| 382 | if ( error ) |
|---|
| 383 | { |
|---|
| 384 | printk("\n[ERROR] in %s : no memory to create a RPC thread in cluster %x\n", |
|---|
| 385 | __FUNCTION__, local_cxy ); |
|---|
| 386 | } |
|---|
| 387 | else |
|---|
| 388 | { |
|---|
| 389 | // unblock created RPC thread |
|---|
| 390 | thread->blocked = 0; |
|---|
| 391 | |
|---|
| 392 | // update RPC threads counter |
|---|
| 393 | hal_atomic_add( &LOCAL_CLUSTER->rpc_threads[lid] , 1 ); |
|---|
| 394 | |
|---|
| 395 | #if DEBUG_SCHED_RPC_ACTIVATE |
|---|
| 396 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 397 | if( DEBUG_SCHED_RPC_ACTIVATE < cycle ) |
|---|
| 398 | printk("\n[DBG] %s : new RPC thread %x created for core[%x,%d] / total %d / cycle %d\n", |
|---|
| 399 | __FUNCTION__, thread->trdid, local_cxy, lid, LOCAL_CLUSTER->rpc_threads[lid], cycle ); |
|---|
| 400 | #endif |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | else // RPC thread found => unblock it |
|---|
| 404 | { |
|---|
| 405 | // unblock found RPC thread |
|---|
| 406 | thread_unblock( XPTR( local_cxy , thread ) , THREAD_BLOCKED_IDLE ); |
|---|
| 407 | |
|---|
| 408 | #if DEBUG_SCHED_RPC_ACTIVATE |
|---|
| 409 | uint32_t cycle = (uint32_t)hal_get_cycles(); |
|---|
| 410 | if( DEBUG_SCHED_RPC_ACTIVATE < cycle ) |
|---|
| 411 | printk("\n[DBG] %s : idle RPC thread %x unblocked for core[%x,%d] / cycle %d\n", |
|---|
| 412 | __FUNCTION__, thread->trdid, local_cxy, lid, cycle ); |
|---|
| 413 | #endif |
|---|
| 414 | |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | } // end sched_rpc_activate() |
|---|
| 418 | |
|---|
| 419 | |
|---|
| 420 | |
|---|
| 421 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 422 | // public functions |
|---|
| 423 | /////////////////////////////////////////////////////////////////////////////////////////// |
|---|
| 424 | |
|---|
| 425 | //////////////////////////////// |
|---|
| 426 | void sched_init( core_t * core ) |
|---|
| 427 | { |
|---|
| 428 | scheduler_t * sched = &core->scheduler; |
|---|
| 429 | |
|---|
| 430 | sched->u_threads_nr = 0; |
|---|
| 431 | sched->k_threads_nr = 0; |
|---|
| 432 | |
|---|
| 433 | sched->current = CURRENT_THREAD; |
|---|
| 434 | sched->idle = NULL; // initialized in kernel_init() |
|---|
| 435 | sched->u_last = NULL; // initialized in sched_register_thread() |
|---|
| 436 | sched->k_last = NULL; // initialized in sched_register_thread() |
|---|
| 437 | |
|---|
| 438 | // initialise threads lists |
|---|
| 439 | list_root_init( &sched->u_root ); |
|---|
| 440 | list_root_init( &sched->k_root ); |
|---|
| 441 | |
|---|
| 442 | // init lock |
|---|
| 443 | busylock_init( &sched->lock , LOCK_SCHED_STATE ); |
|---|
| 444 | |
|---|
| 445 | sched->req_ack_pending = false; // no pending request |
|---|
| 446 | sched->trace = false; // context switches trace desactivated |
|---|
| 447 | |
|---|
| 448 | } // end sched_init() |
|---|
| 449 | |
|---|
| 450 | //////////////////////////////////////////// |
|---|
| 451 | void sched_register_thread( core_t * core, |
|---|
| 452 | thread_t * thread ) |
|---|
| 453 | { |
|---|
| 454 | scheduler_t * sched = &core->scheduler; |
|---|
| 455 | thread_type_t type = thread->type; |
|---|
| 456 | |
|---|
| 457 | // take lock protecting sheduler state |
|---|
| 458 | busylock_acquire( &sched->lock ); |
|---|
| 459 | |
|---|
| 460 | if( type == THREAD_USER ) |
|---|
| 461 | { |
|---|
| 462 | list_add_last( &sched->u_root , &thread->sched_list ); |
|---|
| 463 | sched->u_threads_nr++; |
|---|
| 464 | if( sched->u_last == NULL ) sched->u_last = &thread->sched_list; |
|---|
| 465 | } |
|---|
| 466 | else // kernel thread |
|---|
| 467 | { |
|---|
| 468 | list_add_last( &sched->k_root , &thread->sched_list ); |
|---|
| 469 | sched->k_threads_nr++; |
|---|
| 470 | if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | // release lock |
|---|
| 474 | busylock_release( &sched->lock ); |
|---|
| 475 | |
|---|
| 476 | } // end sched_register_thread() |
|---|
| 477 | |
|---|
| 478 | ////////////////////////////////////// |
|---|
| 479 | void sched_yield( const char * cause ) |
|---|
| 480 | { |
|---|
| 481 | thread_t * next; |
|---|
| 482 | thread_t * current = CURRENT_THREAD; |
|---|
| 483 | core_t * core = current->core; |
|---|
| 484 | lid_t lid = core->lid; |
|---|
| 485 | scheduler_t * sched = &core->scheduler; |
|---|
| 486 | remote_fifo_t * fifo = &LOCAL_CLUSTER->rpc_fifo[lid]; |
|---|
| 487 | |
|---|
| 488 | #if (DEBUG_SCHED_YIELD & 0x1) |
|---|
| 489 | if( sched->trace ) sched_display( lid ); |
|---|
| 490 | #endif |
|---|
| 491 | |
|---|
| 492 | // This assert should never be false, as this check must be |
|---|
| 493 | // done before by any function that can possibly deschedule... |
|---|
| 494 | assert( (current->busylocks == 0), |
|---|
| 495 | "unexpected descheduling of thread holding %d busylocks = %d\n", current->busylocks ); |
|---|
| 496 | |
|---|
| 497 | // activate or create an RPC thread if RPC_FIFO non empty |
|---|
| 498 | if( remote_fifo_is_empty( fifo ) == false ) sched_rpc_activate( sched ); |
|---|
| 499 | |
|---|
| 500 | // disable IRQs / save SR in current thread descriptor |
|---|
| 501 | hal_disable_irq( ¤t->save_sr ); |
|---|
| 502 | |
|---|
| 503 | // take lock protecting sheduler state |
|---|
| 504 | busylock_acquire( &sched->lock ); |
|---|
| 505 | |
|---|
| 506 | // select next thread |
|---|
| 507 | next = sched_select( sched ); |
|---|
| 508 | |
|---|
| 509 | // check next thread kernel_stack overflow |
|---|
| 510 | assert( (next->signature == THREAD_SIGNATURE), |
|---|
| 511 | "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, lid ); |
|---|
| 512 | |
|---|
| 513 | // check next thread attached to same core as the calling thread |
|---|
| 514 | assert( (next->core == current->core), |
|---|
| 515 | "next core %x != current core %x\n", next->core, current->core ); |
|---|
| 516 | |
|---|
| 517 | // check next thread not blocked when type != IDLE |
|---|
| 518 | assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) , |
|---|
| 519 | "next thread %x (%s) is blocked on core[%x,%d]\n", |
|---|
| 520 | next->trdid , thread_type_str(next->type) , local_cxy , lid ); |
|---|
| 521 | |
|---|
| 522 | // switch contexts and update scheduler state if next != current |
|---|
| 523 | if( next != current ) |
|---|
| 524 | { |
|---|
| 525 | // update scheduler |
|---|
| 526 | sched->current = next; |
|---|
| 527 | if( next->type == THREAD_USER ) sched->u_last = &next->sched_list; |
|---|
| 528 | else sched->k_last = &next->sched_list; |
|---|
| 529 | |
|---|
| 530 | // handle FPU ownership |
|---|
| 531 | if( next->type == THREAD_USER ) |
|---|
| 532 | { |
|---|
| 533 | if( next == current->core->fpu_owner ) hal_fpu_enable(); |
|---|
| 534 | else hal_fpu_disable(); |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | // release lock protecting scheduler state |
|---|
| 538 | busylock_release( &sched->lock ); |
|---|
| 539 | |
|---|
| 540 | #if DEBUG_SCHED_YIELD |
|---|
| 541 | if( sched->trace ) |
|---|
| 542 | printk("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
|---|
| 543 | " thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n", |
|---|
| 544 | __FUNCTION__, local_cxy, lid, cause, |
|---|
| 545 | current, thread_type_str(current->type), current->process->pid, current->trdid,next , |
|---|
| 546 | thread_type_str(next->type) , next->process->pid , next->trdid , (uint32_t)hal_get_cycles() ); |
|---|
| 547 | #endif |
|---|
| 548 | |
|---|
| 549 | // switch CPU from current thread context to new thread context |
|---|
| 550 | hal_do_cpu_switch( current->cpu_context, next->cpu_context ); |
|---|
| 551 | } |
|---|
| 552 | else |
|---|
| 553 | { |
|---|
| 554 | // release lock protecting scheduler state |
|---|
| 555 | busylock_release( &sched->lock ); |
|---|
| 556 | |
|---|
| 557 | #if (DEBUG_SCHED_YIELD & 1) |
|---|
| 558 | if( sched->trace ) |
|---|
| 559 | printk("\n[DBG] %s : core[%x,%d] / cause = %s\n" |
|---|
| 560 | " thread %x (%s) (%x,%x) continue / cycle %d\n", |
|---|
| 561 | __FUNCTION__, local_cxy, lid, cause, current, thread_type_str(current->type), |
|---|
| 562 | current->process->pid, current->trdid, (uint32_t)hal_get_cycles() ); |
|---|
| 563 | #endif |
|---|
| 564 | |
|---|
| 565 | } |
|---|
| 566 | |
|---|
| 567 | // handle pending requests for all threads executing on this core. |
|---|
| 568 | sched_handle_signals( core ); |
|---|
| 569 | |
|---|
| 570 | // exit critical section / restore SR from current thread descriptor |
|---|
| 571 | hal_restore_irq( CURRENT_THREAD->save_sr ); |
|---|
| 572 | |
|---|
| 573 | } // end sched_yield() |
|---|
| 574 | |
|---|
| 575 | |
|---|
| 576 | /////////////////////////////// |
|---|
| 577 | void sched_display( lid_t lid ) |
|---|
| 578 | { |
|---|
| 579 | list_entry_t * iter; |
|---|
| 580 | thread_t * thread; |
|---|
| 581 | |
|---|
| 582 | // check lid |
|---|
| 583 | assert( (lid < LOCAL_CLUSTER->cores_nr), |
|---|
| 584 | "illegal core index %d\n", lid); |
|---|
| 585 | |
|---|
| 586 | core_t * core = &LOCAL_CLUSTER->core_tbl[lid]; |
|---|
| 587 | scheduler_t * sched = &core->scheduler; |
|---|
| 588 | |
|---|
| 589 | // get pointers on TXT0 chdev |
|---|
| 590 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
|---|
| 591 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
|---|
| 592 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
|---|
| 593 | |
|---|
| 594 | // get extended pointer on remote TXT0 lock |
|---|
| 595 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
|---|
| 596 | |
|---|
| 597 | // get TXT0 lock |
|---|
| 598 | remote_busylock_acquire( lock_xp ); |
|---|
| 599 | |
|---|
| 600 | nolock_printk("\n***** threads on core[%x,%d] / current %x / rpc_threads %d / cycle %d\n", |
|---|
| 601 | local_cxy , core->lid, sched->current, LOCAL_CLUSTER->rpc_threads[lid], |
|---|
| 602 | (uint32_t)hal_get_cycles() ); |
|---|
| 603 | |
|---|
| 604 | // display kernel threads |
|---|
| 605 | LIST_FOREACH( &sched->k_root , iter ) |
|---|
| 606 | { |
|---|
| 607 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
|---|
| 608 | if (thread->type == THREAD_DEV) |
|---|
| 609 | { |
|---|
| 610 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n", |
|---|
| 611 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
|---|
| 612 | thread, thread->blocked, thread->flags, thread->chdev->name ); |
|---|
| 613 | } |
|---|
| 614 | else |
|---|
| 615 | { |
|---|
| 616 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n", |
|---|
| 617 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
|---|
| 618 | thread, thread->blocked, thread->flags ); |
|---|
| 619 | } |
|---|
| 620 | } |
|---|
| 621 | |
|---|
| 622 | // display user threads |
|---|
| 623 | LIST_FOREACH( &sched->u_root , iter ) |
|---|
| 624 | { |
|---|
| 625 | thread = LIST_ELEMENT( iter , thread_t , sched_list ); |
|---|
| 626 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n", |
|---|
| 627 | thread_type_str( thread->type ), thread->process->pid, thread->trdid, |
|---|
| 628 | thread, thread->blocked, thread->flags ); |
|---|
| 629 | } |
|---|
| 630 | |
|---|
| 631 | // release TXT0 lock |
|---|
| 632 | remote_busylock_release( lock_xp ); |
|---|
| 633 | |
|---|
| 634 | } // end sched_display() |
|---|
| 635 | |
|---|
| 636 | ///////////////////////////////////// |
|---|
| 637 | void sched_remote_display( cxy_t cxy, |
|---|
| 638 | lid_t lid ) |
|---|
| 639 | { |
|---|
| 640 | thread_t * thread; |
|---|
| 641 | |
|---|
| 642 | // check cxy |
|---|
| 643 | assert( (cluster_is_undefined( cxy ) == false), |
|---|
| 644 | "illegal cluster %x\n", cxy ); |
|---|
| 645 | |
|---|
| 646 | assert( (lid < hal_remote_l32( XPTR( cxy , &LOCAL_CLUSTER->cores_nr ) ) ), |
|---|
| 647 | "illegal core index %d\n", lid ); |
|---|
| 648 | |
|---|
| 649 | // get local pointer on target scheduler |
|---|
| 650 | core_t * core = &LOCAL_CLUSTER->core_tbl[lid]; |
|---|
| 651 | scheduler_t * sched = &core->scheduler; |
|---|
| 652 | |
|---|
| 653 | // get local pointer on current thread in target scheduler |
|---|
| 654 | thread_t * current = hal_remote_lpt( XPTR( cxy, &sched->current ) ); |
|---|
| 655 | |
|---|
| 656 | // get local pointer on the first kernel and user threads list_entry |
|---|
| 657 | list_entry_t * k_entry = hal_remote_lpt( XPTR( cxy , &sched->k_root.next ) ); |
|---|
| 658 | list_entry_t * u_entry = hal_remote_lpt( XPTR( cxy , &sched->u_root.next ) ); |
|---|
| 659 | |
|---|
| 660 | // get pointers on TXT0 chdev |
|---|
| 661 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
|---|
| 662 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
|---|
| 663 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
|---|
| 664 | |
|---|
| 665 | // get extended pointer on remote TXT0 chdev lock |
|---|
| 666 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
|---|
| 667 | |
|---|
| 668 | // get TXT0 lock |
|---|
| 669 | remote_busylock_acquire( lock_xp ); |
|---|
| 670 | |
|---|
| 671 | // get rpc_threads |
|---|
| 672 | uint32_t rpcs = hal_remote_l32( XPTR( cxy , &LOCAL_CLUSTER->rpc_threads[lid] ) ); |
|---|
| 673 | |
|---|
| 674 | // display header |
|---|
| 675 | nolock_printk("\n***** threads on core[%x,%d] / current %x / rpc_threads %d / cycle %d\n", |
|---|
| 676 | cxy , lid, current, rpcs, (uint32_t)hal_get_cycles() ); |
|---|
| 677 | |
|---|
| 678 | // display kernel threads |
|---|
| 679 | while( k_entry != &sched->k_root ) |
|---|
| 680 | { |
|---|
| 681 | // get local pointer on kernel_thread |
|---|
| 682 | thread = LIST_ELEMENT( k_entry , thread_t , sched_list ); |
|---|
| 683 | |
|---|
| 684 | // get relevant thead info |
|---|
| 685 | thread_type_t type = hal_remote_l32 ( XPTR( cxy , &thread->type ) ); |
|---|
| 686 | trdid_t trdid = hal_remote_l32 ( XPTR( cxy , &thread->trdid ) ); |
|---|
| 687 | uint32_t blocked = hal_remote_l32 ( XPTR( cxy , &thread->blocked ) ); |
|---|
| 688 | uint32_t flags = hal_remote_l32 ( XPTR( cxy , &thread->flags ) ); |
|---|
| 689 | process_t * process = hal_remote_lpt( XPTR( cxy , &thread->process ) ); |
|---|
| 690 | pid_t pid = hal_remote_l32 ( XPTR( cxy , &process->pid ) ); |
|---|
| 691 | |
|---|
| 692 | // display thread info |
|---|
| 693 | if (type == THREAD_DEV) |
|---|
| 694 | { |
|---|
| 695 | char name[16]; |
|---|
| 696 | chdev_t * chdev = hal_remote_lpt( XPTR( cxy , &thread->chdev ) ); |
|---|
| 697 | hal_remote_strcpy( XPTR( local_cxy , name ), XPTR( cxy , &chdev->name ) ); |
|---|
| 698 | |
|---|
| 699 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n", |
|---|
| 700 | thread_type_str( type ), pid, trdid, thread, blocked, flags, name ); |
|---|
| 701 | } |
|---|
| 702 | else |
|---|
| 703 | { |
|---|
| 704 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n", |
|---|
| 705 | thread_type_str( type ), pid, trdid, thread, blocked, flags ); |
|---|
| 706 | } |
|---|
| 707 | |
|---|
| 708 | // get next remote kernel thread list_entry |
|---|
| 709 | k_entry = hal_remote_lpt( XPTR( cxy , &k_entry->next ) ); |
|---|
| 710 | } |
|---|
| 711 | |
|---|
| 712 | // display user threads |
|---|
| 713 | while( u_entry != &sched->u_root ) |
|---|
| 714 | { |
|---|
| 715 | // get local pointer on user_thread |
|---|
| 716 | thread = LIST_ELEMENT( u_entry , thread_t , sched_list ); |
|---|
| 717 | |
|---|
| 718 | // get relevant thead info |
|---|
| 719 | thread_type_t type = hal_remote_l32 ( XPTR( cxy , &thread->type ) ); |
|---|
| 720 | trdid_t trdid = hal_remote_l32 ( XPTR( cxy , &thread->trdid ) ); |
|---|
| 721 | uint32_t blocked = hal_remote_l32 ( XPTR( cxy , &thread->blocked ) ); |
|---|
| 722 | uint32_t flags = hal_remote_l32 ( XPTR( cxy , &thread->flags ) ); |
|---|
| 723 | process_t * process = hal_remote_lpt( XPTR( cxy , &thread->process ) ); |
|---|
| 724 | pid_t pid = hal_remote_l32 ( XPTR( cxy , &process->pid ) ); |
|---|
| 725 | |
|---|
| 726 | nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n", |
|---|
| 727 | thread_type_str( type ), pid, trdid, thread, blocked, flags ); |
|---|
| 728 | |
|---|
| 729 | // get next user thread list_entry |
|---|
| 730 | u_entry = hal_remote_lpt( XPTR( cxy , &u_entry->next ) ); |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | // release TXT0 lock |
|---|
| 734 | remote_busylock_release( lock_xp ); |
|---|
| 735 | |
|---|
| 736 | } // end sched_remote_display() |
|---|
| 737 | |
|---|
| 738 | |
|---|