source: trunk/kernel/kern/scheduler.c @ 519

Last change on this file since 519 was 500, checked in by viala@…, 6 years ago

Minor change: Remove an extra call to spinlock_init in scheduler.c

Alain commited it after me.

File size: 18.0 KB
Line 
1/*
2 * scheduler.c - Core scheduler implementation.
3 *
4 * Author    Alain Greiner (2016)
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// Extern global variables
39///////////////////////////////////////////////////////////////////////////////////////////
40
41uint32_t   idle_thread_count;
42uint32_t   idle_thread_count_active;
43
44extern chdev_directory_t    chdev_dir;          // allocated in kernel_init.c file
45extern uint32_t             switch_save_sr[];   // allocated in kernel_init.c file
46
47////////////////////////////////
48void sched_init( core_t * core )
49{
50    scheduler_t * sched = &core->scheduler;
51
52    sched->u_threads_nr   = 0;
53    sched->k_threads_nr   = 0;
54
55    sched->current        = CURRENT_THREAD;
56    sched->idle           = NULL;               // initialized in kernel_init()
57    sched->u_last         = NULL;               // initialized in sched_register_thread()
58    sched->k_last         = NULL;               // initialized in sched_register_thread()
59
60    // initialise threads lists
61    list_root_init( &sched->u_root );
62    list_root_init( &sched->k_root );
63
64    // init spinlock
65    spinlock_init( &sched->lock );
66
67    sched->req_ack_pending = false;             // no pending request
68    sched->trace           = false;             // context switches trace desactivated
69
70}  // end sched_init()
71
72////////////////////////////////////////////
73void sched_register_thread( core_t   * core,
74                            thread_t * thread )
75{
76    scheduler_t * sched = &core->scheduler;
77    thread_type_t type  = thread->type;
78
79    // take lock protecting sheduler lists
80    spinlock_lock( &sched->lock );
81
82    if( type == THREAD_USER )
83    {
84        list_add_last( &sched->u_root , &thread->sched_list );
85        sched->u_threads_nr++;
86        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
87    }
88    else // kernel thread
89    {
90        list_add_last( &sched->k_root , &thread->sched_list );
91        sched->k_threads_nr++;
92        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
93    }
94
95    // release lock
96    hal_fence();
97    spinlock_unlock( &sched->lock );
98
99}  // end sched_register_thread()
100
101//////////////////////////////////////////////
102thread_t * sched_select( scheduler_t * sched )
103{
104    thread_t     * thread;
105    list_entry_t * current;
106    list_entry_t * last;
107    list_entry_t * root;
108    bool_t         done;
109    uint32_t       count;
110
111    // take lock protecting sheduler lists
112    spinlock_lock( &sched->lock );
113
114    // first : scan the kernel threads list if not empty
115    if( list_is_empty( &sched->k_root ) == false )
116    {
117        root    = &sched->k_root;
118        last    = sched->k_last;
119        done    = false;
120        count   = 0;
121        current = last;
122
123        while( done == false )
124        {
125            assert( (count < sched->k_threads_nr), "bad kernel threads list" );
126
127            // get next entry in kernel list
128            current = current->next;
129
130            // check exit condition
131            if( current == last ) done = true;
132
133            // skip the root that does not contain a thread
134            if( current == root ) continue;
135            else                  count++;
136
137            // get thread pointer for this entry
138            thread = LIST_ELEMENT( current , thread_t , sched_list );
139
140            // select kernel thread if non blocked and non THREAD_IDLE
141            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
142            {
143                spinlock_unlock( &sched->lock );
144                return thread;
145            }
146        } // end loop on kernel threads
147    } // end kernel threads
148
149    // second : scan the user threads list if not empty
150    if( list_is_empty( &sched->u_root ) == false )
151    {
152        root    = &sched->u_root;
153        last    = sched->u_last;
154        done    = false;
155        count   = 0;
156        current = last;
157
158        while( done == false )
159        {
160            assert( (count < sched->u_threads_nr), "bad user threads list" );
161
162            // get next entry in user list
163            current = current->next;
164
165            // check exit condition
166            if( current == last ) done = true;
167
168            // skip the root that does not contain a thread
169            if( current == root ) continue;
170            else                  count++;
171
172            // get thread pointer for this entry
173            thread = LIST_ELEMENT( current , thread_t , sched_list );
174
175            // select thread if non blocked
176            if( thread->blocked == 0 )
177            {
178                spinlock_unlock( &sched->lock );
179                return thread;
180            }
181        } // end loop on user threads
182    } // end user threads
183
184    // third : return idle thread if no other runnable thread
185    spinlock_unlock( &sched->lock );
186    return sched->idle;
187
188}  // end sched_select()
189
190///////////////////////////////////////////
191void sched_handle_signals( core_t * core )
192{
193
194    list_entry_t * iter;
195    list_entry_t * root;
196    thread_t     * thread;
197    process_t    * process;
198    bool_t         last_thread;
199
200    // get pointer on scheduler
201    scheduler_t  * sched = &core->scheduler;
202
203    // get pointer on user threads root
204    root = &sched->u_root;
205
206    // take lock protecting threads lists
207    spinlock_lock( &sched->lock );
208
209    // We use a while to scan the user threads, to control the iterator increment,
210    // because some threads will be destroyed, and we cannot use a LIST_FOREACH()
211
212    // initialise list iterator
213    iter = root->next;
214
215    // scan all user threads
216    while( iter != root )
217    {
218        // get pointer on thread
219        thread = LIST_ELEMENT( iter , thread_t , sched_list );
220
221        // increment iterator
222        iter = iter->next;
223
224        // handle REQ_ACK
225        if( thread->flags & THREAD_FLAG_REQ_ACK )
226        {
227            // check thread blocked
228            assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , 
229            "thread not blocked" );
230 
231            // decrement response counter
232            hal_atomic_add( thread->ack_rsp_count , -1 );
233
234            // reset REQ_ACK in thread descriptor
235            thread_reset_req_ack( thread );
236        }
237
238        // handle REQ_DELETE
239        if( thread->flags & THREAD_FLAG_REQ_DELETE )
240        {
241            // get thread process descriptor
242            process = thread->process;
243
244                // release FPU if required
245                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
246
247            // remove thread from scheduler (scheduler lock already taken)
248            uint32_t threads_nr = sched->u_threads_nr;
249
250            assert( (threads_nr != 0) , "u_threads_nr cannot be 0\n" );
251
252            sched->u_threads_nr = threads_nr - 1;
253            list_unlink( &thread->sched_list );
254            if( sched->u_last == &thread->sched_list )
255            {
256                if( threads_nr == 1 ) 
257                {
258                    sched->u_last = NULL;
259                }
260                else if( sched->u_root.next == &thread->sched_list )
261                {
262                    sched->u_last = sched->u_root.pred;
263                }
264                else
265                {
266                    sched->u_last = sched->u_root.next;
267                }
268            }
269
270            // delete thread descriptor
271            last_thread = thread_destroy( thread );
272
273#if DEBUG_SCHED_HANDLE_SIGNALS
274uint32_t cycle = (uint32_t)hal_get_cycles();
275if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
276printk("\n[DBG] %s : thread %x in process %x on core[%x,%d] deleted / cycle %d\n",
277__FUNCTION__ , thread->trdid , process->pid , local_cxy , thread->core->lid , cycle );
278#endif
279            // destroy process descriptor if no more threads
280            if( last_thread ) 
281            {
282                // delete process   
283                process_destroy( process );
284
285#if DEBUG_SCHED_HANDLE_SIGNALS
286cycle = (uint32_t)hal_get_cycles();
287if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
288printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n",
289__FUNCTION__ , process->pid , local_cxy , cycle );
290#endif
291            }
292        }
293    }
294
295    // release lock
296    hal_fence();
297    spinlock_unlock( &sched->lock );
298
299} // end sched_handle_signals()
300
301////////////////////////////////
302void sched_yield( const char * cause )
303{
304    thread_t    * next;
305    thread_t    * current = CURRENT_THREAD;
306    core_t      * core    = current->core;
307    scheduler_t * sched   = &core->scheduler;
308 
309#if (DEBUG_SCHED_YIELD & 0x1)
310if( sched->trace )
311sched_display( core->lid );
312#endif
313
314    // delay the yield if current thread has locks
315    if( (current->local_locks != 0) || (current->remote_locks != 0) )
316    {
317        current->flags |= THREAD_FLAG_SCHED;
318        return;
319    }
320
321    // enter critical section / save SR in current thread descriptor
322    hal_disable_irq( &CURRENT_THREAD->save_sr );
323
324    // loop on threads to select next thread
325    next = sched_select( sched );
326
327    // check next thread kernel_stack overflow
328    assert( (next->signature == THREAD_SIGNATURE),
329    "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, core->lid );
330
331    // check next thread attached to same core as the calling thread
332    assert( (next->core == current->core),
333    "next core %x != current core %x\n", next->core, current->core );
334
335    // check next thread not blocked when type != IDLE
336    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) ,
337    "next thread %x (%s) is blocked on core[%x,%d]\n", 
338    next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
339
340    // switch contexts and update scheduler state if next != current
341        if( next != current )
342    {
343
344#if DEBUG_SCHED_YIELD
345if( sched->trace )
346printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
347"      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
348__FUNCTION__, local_cxy, core->lid, cause, 
349current, thread_type_str(current->type), current->process->pid, current->trdid,next ,
350thread_type_str(next->type) , next->process->pid , next->trdid , (uint32_t)hal_get_cycles() );
351#endif
352
353        // update scheduler
354        sched->current = next;
355        if( next->type == THREAD_USER ) sched->u_last = &next->sched_list;
356        else                            sched->k_last = &next->sched_list;
357
358        // handle FPU ownership
359            if( next->type == THREAD_USER )
360        {
361                if( next == current->core->fpu_owner )  hal_fpu_enable();
362                else                                    hal_fpu_disable();
363        }
364
365        // switch CPU from current thread context to new thread context
366        hal_do_cpu_switch( current->cpu_context, next->cpu_context );
367    }
368    else
369    {
370
371#if DEBUG_SCHED_YIELD
372if( sched->trace )
373printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
374"      thread %x (%s) (%x,%x) continue / cycle %d\n",
375__FUNCTION__, local_cxy, core->lid, cause, current, thread_type_str(current->type),
376current->process->pid, current->trdid, (uint32_t)hal_get_cycles() );
377#endif
378
379    }
380
381    // handle pending requests for all threads executing on this core.
382    sched_handle_signals( core );
383
384    // exit critical section / restore SR from current thread descriptor
385    hal_restore_irq( CURRENT_THREAD->save_sr );
386
387}  // end sched_yield()
388
389
390///////////////////////////////
391void sched_display( lid_t lid )
392{
393    list_entry_t * iter;
394    thread_t     * thread;
395    uint32_t       save_sr;
396
397    assert( (lid < LOCAL_CLUSTER->cores_nr), "illegal core index %d\n", lid);
398
399    core_t       * core    = &LOCAL_CLUSTER->core_tbl[lid];
400    scheduler_t  * sched   = &core->scheduler;
401   
402    // get pointers on TXT0 chdev
403    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
404    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
405    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
406
407    // get extended pointer on remote TXT0 chdev lock
408    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
409
410    // get TXT0 lock in busy waiting mode
411    remote_spinlock_lock_busy( lock_xp , &save_sr );
412
413    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
414    local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
415
416    // display kernel threads
417    LIST_FOREACH( &sched->k_root , iter )
418    {
419        thread = LIST_ELEMENT( iter , thread_t , sched_list );
420        if (thread->type == THREAD_DEV) 
421        {
422            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
423            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
424            thread, thread->blocked, thread->flags, thread->chdev->name );
425        }
426        else
427        {
428            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
429            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
430            thread, thread->blocked, thread->flags );
431        }
432    }
433
434    // display user threads
435    LIST_FOREACH( &sched->u_root , iter )
436    {
437        thread = LIST_ELEMENT( iter , thread_t , sched_list );
438        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
439        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
440        thread, thread->blocked, thread->flags );
441    }
442
443    // release TXT0 lock
444    remote_spinlock_unlock_busy( lock_xp , save_sr );
445
446}  // end sched_display()
447
448/////////////////////////////////////
449void sched_remote_display( cxy_t cxy,
450                           lid_t lid )
451{
452    thread_t     * thread;
453    uint32_t       save_sr;
454
455    // check cxy
456    bool_t undefined = cluster_is_undefined( cxy );
457    assert( (undefined == false), "illegal cluster %x\n", cxy );
458
459    // check lid
460    uint32_t cores = hal_remote_lw( XPTR( cxy , &LOCAL_CLUSTER->cores_nr ) );
461    assert( (lid < cores), "illegal core index %d\n", lid);
462
463    // get local pointer on target scheduler
464    core_t      * core  = &LOCAL_CLUSTER->core_tbl[lid];
465    scheduler_t * sched = &core->scheduler;
466
467    // get local pointer on current thread in target scheduler
468    thread_t * current = hal_remote_lpt( XPTR( cxy, &sched->current ) );
469
470    // get local pointer on the first kernel and user threads list_entry
471    list_entry_t * k_entry = hal_remote_lpt( XPTR( cxy , &sched->k_root.next ) );
472    list_entry_t * u_entry = hal_remote_lpt( XPTR( cxy , &sched->u_root.next ) );
473   
474    // get pointers on TXT0 chdev
475    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
476    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
477    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
478
479    // get extended pointer on remote TXT0 chdev lock
480    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
481
482    // get TXT0 lock in busy waiting mode
483    remote_spinlock_lock_busy( lock_xp , &save_sr );
484
485    // display header
486    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
487    cxy , lid, current, (uint32_t)hal_get_cycles() );
488
489    // display kernel threads
490    while( k_entry != &sched->k_root )
491    {
492        // get local pointer on kernel_thread
493        thread = LIST_ELEMENT( k_entry , thread_t , sched_list );
494
495        // get relevant thead info
496        thread_type_t type    = hal_remote_lw ( XPTR( cxy , &thread->type ) );
497        trdid_t       trdid   = hal_remote_lw ( XPTR( cxy , &thread->trdid ) );
498        uint32_t      blocked = hal_remote_lw ( XPTR( cxy , &thread->blocked ) );
499        uint32_t      flags   = hal_remote_lw ( XPTR( cxy , &thread->flags ) );
500        process_t *   process = hal_remote_lpt( XPTR( cxy , &thread->process ) );
501        pid_t         pid     = hal_remote_lw ( XPTR( cxy , &process->pid ) );
502
503        // display thread info
504        if (type == THREAD_DEV) 
505        {
506            char      name[16];
507            chdev_t * chdev = hal_remote_lpt( XPTR( cxy , &thread->chdev ) );
508            hal_remote_strcpy( XPTR( local_cxy , name ), XPTR( cxy , &chdev->name ) );
509
510            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
511            thread_type_str( type ), pid, trdid, thread, blocked, flags, name );
512        }
513        else
514        {
515            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
516            thread_type_str( type ), pid, trdid, thread, blocked, flags );
517        }
518
519        // get next remote kernel thread list_entry
520        k_entry = hal_remote_lpt( XPTR( cxy , &k_entry->next ) );
521    }
522
523    // display user threads
524    while( u_entry != &sched->u_root )
525    {
526        // get local pointer on user_thread
527        thread = LIST_ELEMENT( u_entry , thread_t , sched_list );
528
529        // get relevant thead info
530        thread_type_t type    = hal_remote_lw ( XPTR( cxy , &thread->type ) );
531        trdid_t       trdid   = hal_remote_lw ( XPTR( cxy , &thread->trdid ) );
532        uint32_t      blocked = hal_remote_lw ( XPTR( cxy , &thread->blocked ) );
533        uint32_t      flags   = hal_remote_lw ( XPTR( cxy , &thread->flags ) );
534        process_t *   process = hal_remote_lpt( XPTR( cxy , &thread->process ) );
535        pid_t         pid     = hal_remote_lw ( XPTR( cxy , &process->pid ) );
536
537        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
538        thread_type_str( type ), pid, trdid, thread, blocked, flags );
539
540        // get next user thread list_entry
541        u_entry = hal_remote_lpt( XPTR( cxy , &u_entry->next ) );
542    }
543
544    // release TXT0 lock
545    remote_spinlock_unlock_busy( lock_xp , save_sr );
546
547}  // end sched_remote_display()
548
Note: See TracBrowser for help on using the repository browser.