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

Last change on this file since 527 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
RevLine 
[1]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
[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>
31#include <core.h>
32#include <thread.h>
[296]33#include <chdev.h>
[1]34#include <scheduler.h>
35
[443]36
[296]37///////////////////////////////////////////////////////////////////////////////////////////
38// Extern global variables
39///////////////////////////////////////////////////////////////////////////////////////////
[1]40
[443]41uint32_t   idle_thread_count;
42uint32_t   idle_thread_count_active;
[296]43
[443]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
[1]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
[279]55    sched->current        = CURRENT_THREAD;
[443]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()
[1]59
60    // initialise threads lists
61    list_root_init( &sched->u_root );
62    list_root_init( &sched->k_root );
63
[469]64    // init spinlock
65    spinlock_init( &sched->lock );
66
[443]67    sched->req_ack_pending = false;             // no pending request
68    sched->trace           = false;             // context switches trace desactivated
[409]69
[1]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++;
[279]86        if( sched->u_last == NULL ) sched->u_last = &thread->sched_list;
[1]87    }
88    else // kernel thread
89    {
90        list_add_last( &sched->k_root , &thread->sched_list );
91        sched->k_threads_nr++;
[279]92        if( sched->k_last == NULL ) sched->k_last = &thread->sched_list; 
[1]93    }
94
95    // release lock
[428]96    hal_fence();
[1]97    spinlock_unlock( &sched->lock );
98
[409]99}  // end sched_register_thread()
[1]100
[408]101//////////////////////////////////////////////
102thread_t * sched_select( scheduler_t * sched )
[1]103{
[408]104    thread_t     * thread;
105    list_entry_t * current;
106    list_entry_t * last;
[437]107    list_entry_t * root;
108    bool_t         done;
[450]109    uint32_t       count;
[1]110
111    // take lock protecting sheduler lists
112    spinlock_lock( &sched->lock );
113
[437]114    // first : scan the kernel threads list if not empty
[279]115    if( list_is_empty( &sched->k_root ) == false )
[1]116    {
[437]117        root    = &sched->k_root;
[279]118        last    = sched->k_last;
[450]119        done    = false;
120        count   = 0;
[437]121        current = last;
122
123        while( done == false )
[279]124        {
[492]125            assert( (count < sched->k_threads_nr), "bad kernel threads list" );
[450]126
[279]127            // get next entry in kernel list
[437]128            current = current->next;
[1]129
[437]130            // check exit condition
131            if( current == last ) done = true;
132
[279]133            // skip the root that does not contain a thread
[437]134            if( current == root ) continue;
[450]135            else                  count++;
[1]136
[279]137            // get thread pointer for this entry
138            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]139
[450]140            // select kernel thread if non blocked and non THREAD_IDLE
[440]141            if( (thread->blocked == 0)  && (thread->type != THREAD_IDLE) )
[279]142            {
[438]143                spinlock_unlock( &sched->lock );
144                return thread;
145            }
[437]146        } // end loop on kernel threads
[450]147    } // end kernel threads
[437]148
149    // second : scan the user threads list if not empty
[279]150    if( list_is_empty( &sched->u_root ) == false )
[1]151    {
[437]152        root    = &sched->u_root;
[279]153        last    = sched->u_last;
[450]154        done    = false;
155        count   = 0;
[437]156        current = last;
157
158        while( done == false )
[279]159        {
[492]160            assert( (count < sched->u_threads_nr), "bad user threads list" );
[450]161
[279]162            // get next entry in user list
[437]163            current = current->next;
[1]164
[437]165            // check exit condition
166            if( current == last ) done = true;
167
[279]168            // skip the root that does not contain a thread
[437]169            if( current == root ) continue;
[450]170            else                  count++;
[1]171
[279]172            // get thread pointer for this entry
173            thread = LIST_ELEMENT( current , thread_t , sched_list );
[1]174
[450]175            // select thread if non blocked
[279]176            if( thread->blocked == 0 )
177            {
178                spinlock_unlock( &sched->lock );
179                return thread;
180            }
[437]181        } // end loop on user threads
[450]182    } // end user threads
[1]183
[437]184    // third : return idle thread if no other runnable thread
[1]185    spinlock_unlock( &sched->lock );
186    return sched->idle;
187
[296]188}  // end sched_select()
[1]189
[416]190///////////////////////////////////////////
[433]191void sched_handle_signals( core_t * core )
[1]192{
[437]193
[1]194    list_entry_t * iter;
[440]195    list_entry_t * root;
[1]196    thread_t     * thread;
[428]197    process_t    * process;
[443]198    bool_t         last_thread;
[409]199
[440]200    // get pointer on scheduler
[1]201    scheduler_t  * sched = &core->scheduler;
202
[440]203    // get pointer on user threads root
204    root = &sched->u_root;
205
[1]206    // take lock protecting threads lists
207    spinlock_lock( &sched->lock );
208
[440]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
[416]215    // scan all user threads
[440]216    while( iter != root )
[1]217    {
[440]218        // get pointer on thread
[1]219        thread = LIST_ELEMENT( iter , thread_t , sched_list );
220
[440]221        // increment iterator
222        iter = iter->next;
223
[416]224        // handle REQ_ACK
225        if( thread->flags & THREAD_FLAG_REQ_ACK )
[408]226        {
[416]227            // check thread blocked
228            assert( (thread->blocked & THREAD_BLOCKED_GLOBAL) , 
[492]229            "thread not blocked" );
[416]230 
231            // decrement response counter
232            hal_atomic_add( thread->ack_rsp_count , -1 );
[408]233
[416]234            // reset REQ_ACK in thread descriptor
235            thread_reset_req_ack( thread );
[408]236        }
[416]237
238        // handle REQ_DELETE
239        if( thread->flags & THREAD_FLAG_REQ_DELETE )
240        {
[428]241            // get thread process descriptor
242            process = thread->process;
[416]243
244                // release FPU if required
245                if( thread->core->fpu_owner == thread )  thread->core->fpu_owner = NULL;
246
[428]247            // remove thread from scheduler (scheduler lock already taken)
248            uint32_t threads_nr = sched->u_threads_nr;
[440]249
[492]250            assert( (threads_nr != 0) , "u_threads_nr cannot be 0\n" );
[440]251
[428]252            sched->u_threads_nr = threads_nr - 1;
[416]253            list_unlink( &thread->sched_list );
[450]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            }
[416]269
[450]270            // delete thread descriptor
[443]271            last_thread = thread_destroy( thread );
[416]272
[438]273#if DEBUG_SCHED_HANDLE_SIGNALS
[440]274uint32_t cycle = (uint32_t)hal_get_cycles();
[438]275if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
[445]276printk("\n[DBG] %s : thread %x in process %x on core[%x,%d] deleted / cycle %d\n",
[443]277__FUNCTION__ , thread->trdid , process->pid , local_cxy , thread->core->lid , cycle );
[433]278#endif
[416]279            // destroy process descriptor if no more threads
[443]280            if( last_thread ) 
[428]281            {
282                // delete process   
283                process_destroy( process );
284
[438]285#if DEBUG_SCHED_HANDLE_SIGNALS
[433]286cycle = (uint32_t)hal_get_cycles();
[438]287if( DEBUG_SCHED_HANDLE_SIGNALS < cycle )
[443]288printk("\n[DBG] %s : process %x in cluster %x deleted / cycle %d\n",
289__FUNCTION__ , process->pid , local_cxy , cycle );
[433]290#endif
[428]291            }
[416]292        }
[1]293    }
294
295    // release lock
[428]296    hal_fence();
[1]297    spinlock_unlock( &sched->lock );
298
[433]299} // end sched_handle_signals()
[416]300
[408]301////////////////////////////////
[470]302void sched_yield( const char * cause )
[1]303{
[407]304    thread_t    * next;
[1]305    thread_t    * current = CURRENT_THREAD;
[409]306    core_t      * core    = current->core;
307    scheduler_t * sched   = &core->scheduler;
[407]308 
[438]309#if (DEBUG_SCHED_YIELD & 0x1)
[443]310if( sched->trace )
[433]311sched_display( core->lid );
[407]312#endif
[1]313
[337]314    // delay the yield if current thread has locks
[407]315    if( (current->local_locks != 0) || (current->remote_locks != 0) )
[337]316    {
317        current->flags |= THREAD_FLAG_SCHED;
318        return;
319    }
[1]320
[435]321    // enter critical section / save SR in current thread descriptor
322    hal_disable_irq( &CURRENT_THREAD->save_sr );
[408]323
[407]324    // loop on threads to select next thread
[408]325    next = sched_select( sched );
[1]326
[436]327    // check next thread kernel_stack overflow
[492]328    assert( (next->signature == THREAD_SIGNATURE),
[443]329    "kernel stack overflow for thread %x on core[%x,%d] \n", next, local_cxy, core->lid );
[436]330
[296]331    // check next thread attached to same core as the calling thread
[492]332    assert( (next->core == current->core),
[443]333    "next core %x != current core %x\n", next->core, current->core );
[296]334
[407]335    // check next thread not blocked when type != IDLE
[492]336    assert( ((next->blocked == 0) || (next->type == THREAD_IDLE)) ,
[407]337    "next thread %x (%s) is blocked on core[%x,%d]\n", 
[409]338    next->trdid , thread_type_str(next->type) , local_cxy , core->lid );
[296]339
340    // switch contexts and update scheduler state if next != current
341        if( next != current )
[1]342    {
343
[438]344#if DEBUG_SCHED_YIELD
[443]345if( sched->trace )
[433]346printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
[408]347"      thread %x (%s) (%x,%x) => thread %x (%s) (%x,%x) / cycle %d\n",
[409]348__FUNCTION__, local_cxy, core->lid, cause, 
[443]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() );
[433]351#endif
[279]352
[296]353        // update scheduler
[408]354        sched->current = next;
355        if( next->type == THREAD_USER ) sched->u_last = &next->sched_list;
356        else                            sched->k_last = &next->sched_list;
[1]357
[407]358        // handle FPU ownership
[306]359            if( next->type == THREAD_USER )
[296]360        {
[407]361                if( next == current->core->fpu_owner )  hal_fpu_enable();
362                else                                    hal_fpu_disable();
[296]363        }
[1]364
[435]365        // switch CPU from current thread context to new thread context
[407]366        hal_do_cpu_switch( current->cpu_context, next->cpu_context );
[296]367    }
368    else
369    {
[407]370
[443]371#if DEBUG_SCHED_YIELD
372if( sched->trace )
[435]373printk("\n[DBG] %s : core[%x,%d] / cause = %s\n"
374"      thread %x (%s) (%x,%x) continue / cycle %d\n",
[443]375__FUNCTION__, local_cxy, core->lid, cause, current, thread_type_str(current->type),
376current->process->pid, current->trdid, (uint32_t)hal_get_cycles() );
[428]377#endif
[407]378
[296]379    }
[408]380
[416]381    // handle pending requests for all threads executing on this core.
[433]382    sched_handle_signals( core );
[409]383
[435]384    // exit critical section / restore SR from current thread descriptor
385    hal_restore_irq( CURRENT_THREAD->save_sr );
[408]386
[1]387}  // end sched_yield()
388
[407]389
390///////////////////////////////
391void sched_display( lid_t lid )
[1]392{
[296]393    list_entry_t * iter;
394    thread_t     * thread;
395    uint32_t       save_sr;
[1]396
[492]397    assert( (lid < LOCAL_CLUSTER->cores_nr), "illegal core index %d\n", lid);
[407]398
399    core_t       * core    = &LOCAL_CLUSTER->core_tbl[lid];
[296]400    scheduler_t  * sched   = &core->scheduler;
401   
402    // get pointers on TXT0 chdev
[407]403    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
[296]404    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
405    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
[1]406
[296]407    // get extended pointer on remote TXT0 chdev lock
408    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
[1]409
[296]410    // get TXT0 lock in busy waiting mode
411    remote_spinlock_lock_busy( lock_xp , &save_sr );
412
[437]413    nolock_printk("\n***** threads on core[%x,%d] / current %x / cycle %d\n",
[443]414    local_cxy , core->lid, sched->current, (uint32_t)hal_get_cycles() );
[296]415
416    // display kernel threads
417    LIST_FOREACH( &sched->k_root , iter )
[1]418    {
[296]419        thread = LIST_ELEMENT( iter , thread_t , sched_list );
[408]420        if (thread->type == THREAD_DEV) 
421        {
[416]422            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X / %s\n",
[408]423            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[416]424            thread, thread->blocked, thread->flags, thread->chdev->name );
[408]425        }
426        else
427        {
[437]428            nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
[408]429            thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[437]430            thread, thread->blocked, thread->flags );
[408]431        }
[1]432    }
433
[296]434    // display user threads
435    LIST_FOREACH( &sched->u_root , iter )
[1]436    {
[296]437        thread = LIST_ELEMENT( iter , thread_t , sched_list );
[416]438        nolock_printk(" - %s / pid %X / trdid %X / desc %X / block %X / flags %X\n",
[408]439        thread_type_str( thread->type ), thread->process->pid, thread->trdid,
[416]440        thread, thread->blocked, thread->flags );
[1]441    }
442
[296]443    // release TXT0 lock
444    remote_spinlock_unlock_busy( lock_xp , save_sr );
[1]445
[296]446}  // end sched_display()
[1]447
[450]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 );
[492]457    assert( (undefined == false), "illegal cluster %x\n", cxy );
[450]458
459    // check lid
460    uint32_t cores = hal_remote_lw( XPTR( cxy , &LOCAL_CLUSTER->cores_nr ) );
[492]461    assert( (lid < cores), "illegal core index %d\n", lid);
[450]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.