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