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