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