330 likes | 369 Vues
Advanced Embedded Systems. Lecture 9 Embedded operating systems (1). Advanced Embedded Systems. An EOS manages the limited resources of an ES; only complex and/or distributed ESs need an EOS;
E N D
Advanced Embedded Systems Lecture 9 Embedded operating systems (1)
Advanced Embedded Systems • An EOS manages the limited resources of an ES; only complex and/or distributed ESs need an EOS; • Its main feature: it is deterministic, meaning one knows in advance how long it takes to perform specific operations, such as activating a task; knowing this, it is possible to predict the impact the EOS will have on system performance; • A classical SO implements fairness and searches to avoid starving processes from lack of CPU access; unlike a classical SO, an EOS must allow the access of the user to the interrupts and to the resources; • 2 types of real time systems (and consequently 2 types of EOSs): • Hard: in a hard real time system an activity must be always completed by a specified deadline; if it is missed, the system fails; the deadline may be a particular time, a time interval or the arrival of some event; • Soft: in a soft real time system there is no a deadline but a larger time specification; an activity must be completed at a timely moment;
Advanced Embedded Systems • Essential features of EOSs: • Configurability: the EOSs must be flexibly tailored on the application; due to the large variety of ESs, there is also a large variety of functional requirements for the EOSs; due to efficiency requirements, it is not possible to include all the functionalities in an EOS; • Configurability in its simplest form might just remove unused functions; • In a more complex form, conditional compilation can be employed; dynamic data might be replaced by static data; • Protection mechanisms are not always necessary, since ESs are typically designed for a single purpose and untested programs are hardly ever loaded; • After the software was tested, it can be assumed to be reliable; • Protection mechanisms may nevertheless still be needed for safety and security reasons; • In most cases, ESs do not have protection mechanisms for input/ output; unlike the desktop applications, I/ O instructions are not privileged and tasks are allowed to do their own I/ O; this drastically reduces the overhead of I/ O operations;
Advanced Embedded Systems • Interrupts can be employed by any process: • In desktop applications, interrupts are not allowed to control the processes directly; • In embedded applications, there is not need for protection but efficient control is desired; interrupts are allowed to control directly the processes; this is much more efficient than going through OS services; • Most of the ESs are real time systems, hence, the EOSs running on these must be RTOSs; • Main requirements for an OS to be a RTOS: • The timing behavior of the OS must be predictable: • For each service of the OS, an upper bound on the execution time must be guaranteed; there may be sets of OS service calls for which an upper bound is known and for which there is not an important variation of the execution time; other service calls have high variations; • There may be times during which interrupts have to be disabled to avoid interferences between tasks; these periods must be short in order to avoid unpredictable delays in processing critical events; • For RTOSs implementing file systems, contiguous files must be implemented;
Advanced Embedded Systems • The OS must manage the timing and scheduling of tasks: the OS must start the tasks and must to be aware of task deadlines for applying appropriate scheduling techniques; the OS must provide time services; • The OS must be fast: the OS must be capable of supporting applications with deadlines that are fractions of a second; • The smallest portion of an OS which provides for task scheduling, task dispatching, intertask communication is called kernel, executive or nucleus; • In ESs, the kernel may be the entire OS, whereas in classical OSs it is only a part of it; • Fig. presents the position of a kernel in a classical OS:
Advanced Embedded Systems • However there are differences between terms as kernel, executive; • Next fig. shows a hierarchy of complexity of different terms; as the complexity decreases, so do the code size and response times; • Nano-kernel: simple thread-of-execution management; it provides only one of the services ensured by a kernel, the task dispatching; • Micro-kernel: a nano-kernel that provides for task scheduling; • Kernel: a micro-kernel that provides for intertask synchronization and communication; • Executive: a kernel that ensures also memory management; • Operating system: an executive that provides for a generalized user interface or command processor, security and file management system;
Advanced Embedded Systems • There are 2 types of RTOSs: • General purpose OS type RTOSs: for these operating systems, some drivers, such as disk, network drivers or audio drivers are implicitly assumed to be present and are embedded into the kernel; the application software and middleware are implemented on top of the application programming interface; • Real time kernel type of RTOSs: since there is hardly any standard device in ESs, device drivers are not embedded into the kernel but are implemented on the top of the kernel; only the necessary drivers are included; applications and middleware may be implemented on top of appropriate drivers, not on top of a standardized API of the OS; • The fig. shows the main difference between a real time kernel and a general purpose OS:
Advanced Embedded Systems • RTOSs can be achieved in several ways: • RTOSs with polled loop, • RTOSs with phase/state driven code, • RTOSs with coroutines, • Interrupt driven RTOSs and • Foreground/background systems; • RTOSs with polled loop: • It is the simplest solution for unitask systems; • The processor runs repetitively a sequence in which it reads a flag for sensing if an event occurred or not; if the event has not occurred, the polling continues; if the event occurred, it is treated; more than one events may be polled, each one needing a separate flag; • The method is indicated when a single processor is dedicated to handle some I/O fast device and when overlapping of events is not allowed; • Its main disadvantage: the time wasted by the processor in the loop;
Advanced Embedded Systems • RTOSs with phase/state driven code: • Phase or state driven code uses nested if-then statements, case statements or a finite automation to break up the processing of a function into code segments; the separation of processes allows each one to be temporarily suspended before completion without loss of critical data; • Certain processes lend well to this type of implementation; for example the compilation process can be regarded as being made of lexical analysis, parsing, code generation and optimization; a process implementing compilation could be interrupted after each of the phases; • State driven systems can be coded in many languages using nested if-then or case statements; they can be used with polled loops where the first state tests some flag and the second state processes the associated event if the flag is set; • RTOSs with coroutines: • Coroutines or cooperative multitasking systems require disciplined programming and an appropriate application;
Advanced Embedded Systems • Two or more processes are coded in the state driven fashion and, after each phase is complete, a call is made to a central dispatcher; the dispatcher holds the program counter for a list of processes that are executed in round-robin fashion, that is it selects the next process to execute; this process then executes until the next phase is complete and the central dispatcher is called again; • Communication between processes is achieved via global variables; any data that need to be preserved between dispatches must be memorized in global variables; • If each programmer provides calls to the dispatcher at known intervals, then the response time is easy to be determined; • Advantages: • The tasks can be written by independent parties, the number of tasks must not be known in advance; • It ensures a fair scheduling; • Certain languages, such as Ada and Modula 2, have built in constructs for implementing coroutines;
Advanced Embedded Systems • Disadvantages: • The strategy is error prone requesting strict discipline on the part of programmers because they must relinquish the CPU at regular intervals; • It requires a communication scheme based on global variables which is undesirable; • Not all tasks can be broken; • Tasks cannot always be broken into uniform size phases, and this will affect the response time since the minimum size depends on the longest phase; • Interrupt driven RTOSs: • Such a RTOS is multitasking, the task scheduling is done through external or internal interrupts and their execution is done by the ISRs; the main program is a single “jump to self” instruction; • Systems are divided in: • Fixed rate systems: the tasks are driven by periodical interrupts; • Sporadic systems: the interrupts are not periodically and • Hybrid systems: with both types of interrupts; • If the interrupts are external and multiple, they must be prioritized and assigned an ID vector; there are 2 solutions:
Advanced Embedded Systems • Hardware: uses an Interrupt Controller (IC) and • Combined: the prioritization is done by software (in the ISR) and the ID assignation is done by hardware • The first solution is fast but requests a specialized circuit, the second solution does not need supplementary circuits but lengthens the interrupt request response time of the processor; it is assumed that each interrupt request source has a flag which is set when the interrupt is generated; the flag will be read by the code from the ISR; • At each task switch the state of the machine, called the context, must be preserved so that a new launch may continue from the state it left; • The process of saving and restoring sufficient information for a task so that it can be resumed after being interrupted is called context switching; the context is ordinarily saved to a stack data structure; • Context switching has a major contribution to the response times so it must be minimized; • The rule for context switching is: save the minimum amount of information necessary to safely restore any process the task after it has been interrupted; • Usually the interrupts are disabled during context switching for protecting it; sometimes, however, interrupts may be enabled after a partial context switch in order to detect spurious interrupts, to handle a burst of interrupts or to handle time-overloaded condition;
Advanced Embedded Systems • There are several methods for dispatching the tasks in interrupt driven RTOSs: • Round-robin, • Preemtive priority, • Major and minor cycles and • Hybrid; • Round-robin: several processes are executed sequentially; • In round-robin with time-slicing systems each task receives a fixed-time quantum, called a time slice, to execute; a fixed rate interrupt, with the rate corresponding to the time slice, switches the tasks; a task executes until it completes or its execution time expires; if the task does not execute to completion, its context must be saved; the task is then placed at the end of the executable list; the context of the next task in the list is restored and it resumes execution; • Next figure illustrates the process:
Advanced Embedded Systems • Preemptive priority: the figure below presents the execution of 3 tasks: • In preemptive priority systems (PPS) a higher priority task can interrupt a running lower priority task; the task is launched by an interrupt and the urgency of each task gives its priority; • The interrupt priorities can be either static or dynamic; static or fixed priority systems are less flexible, the static priority cannot being changed; it can be changed in dynamic priority systems; • In PPS it is possible that higher priority tasks monopolize the resources; the lower priority tasks may have the so called starvation problem;
Advanced Embedded Systems • Preemptive priority can be implemented also in systems with time slicing if the rate of interrupts ≤ the time slicing rate; if not, it can be still implemented if buffers exist for memorizing the data until the tasks change; if this condition is not met a higher priority task may be forced to wait the completion of a lower priority task; • Priority assignation to tasks may be done in several ways: deadline monotonic scheduling and rate monotonic assignation: • Deadline monotonic scheduling: the higher priority is assigned to the task that will end the sooner; it is difficult to know all the time the finishing moments of the tasks; • Rate monotonic assignation (RMA): achieves a static assignation and assumes that there are sufficient priority levels for the tasks; • The assignation is made so that the higher the execution frequency, the higher the priority; it is demonstrated that the RMA is the optimal fixed priority scheduling method; it means that if a successful schedule cannot be found with the RMA, then no other fixed priority schedule way will be found; • In RMA, no deadline will be missed if the CPU utilization is < 70%;
Advanced Embedded Systems • Next figure shows the effect of RMA:
Advanced Embedded Systems a. the execution of a single periodical task with t duration; b. the execution of 2 tasks with S and t durations, S>t and higher priority for S; the system will still work correctly because of the moments when the tasks are activated; c. the execution of the same 2 tasks but the activation moments are not more favorable; for example the task t starts at t3 and is interrupted by the task S; it will continue at the end of S but its execution will not be completed because it is launched again at t4; it may lead to incorrect data offered by the execution started at t3; in b and c the RMA was not respected; d. the execution of both tasks is completed correctly because the RMA was used; • The priority inversion may cause problems in PPS; examples: • A low priority task with a high frequency of execution receives a higher priority than a task with higher priority but lower execution rate; • Disable interrupts: if while a task is running the interrupt system is disabled, the interrupt for changing the tasks will not be treated; the inversion ends to the reactivation of the interrupt system; • One suppose 2 blocked tasks, A and B, with a higher priority for B and an ISR that sends to the 2 kernels messages for deblocking them; if the kernel will process first the message for deblocking the A task and after that the message for the task B an inversion will exist; • A lower priority task holds a resource that an higher priority task needs;
Advanced Embedded Systems • Hybrid systems: interrupts at both fixed rates and sporadically are used; the sporadic interrupts may be used to handle a critical error that must be immediately treated, thus having the highest priority; • Another type: a combination of round robin and preemptive systems; tasks of higher priority can always preempt those of lower priority but if 2 or more tasks of the same priority are ready to run simultaneously, then they will run in round robin fashion; • Advantages for interrupt only systems: easy to write, fast response time because the scheduling can be done by hardware and they are a special case of foreground/background systems which are widely used in ESs; • Disadvantages: the time wasted in the jump-to-self loop, the difficulty in providing advanced services such as device drivers, interfaces to multiple layered networks etc. and vulnerability to malfunction owned to timing variations, race conditions etc.
Advanced Embedded Systems • Foreground/ background systems: • They are an improvement of the interrupt only systems in that the polled loop (which was a dummy background process) is replaced by useful code; it is the most common solution for the ES; • 2 types of tasks run in FBS: • A set of interrupt driven or real time processes called the foreground and • A non-interrupt driven task called the background; • The foreground tasks run in round robin, preemptive priority or combination and the background task is preemptable by any foreground task; • All real time systems are special cases of FBS: • The polled loop is a FBS without foreground and a polled loop as a background; • Phase driven systems is a FBS without foreground and phase driven code for a background; • Coroutine systems are background processes and interrupt only systems are FBS without a background; • The difficulty is to choose the processes that run in background and those for foreground;
Advanced Embedded Systems • The tasks from background must not have critical time requirements; • For example one can implement a software watchdog in background and a counter for each process from foreground that will be reset when the process is launched; if in background it is sensed that a counter is not reset after a certain amount of time a possible error will be indicated; • Another example: a low-priority self-testing such as a complete test of the CPU instruction set; • The background process is the process with the lowest priority; it will always execute to completion, unless the system is time-overloaded; but the rate at which the background will be executed can be very low; • It is necessary that a FBS is initialized at starting; it consists of: • Interrupts are disabled (they are enabled at the starting time for some processors); • The interrupt vector table is built; • The stack (or stacks) are initialized; • Certain hardware tests are executed and • Interrupts are enabled;
Advanced Embedded Systems Scheduling in real time systems • Scheduling of tasks requires some knowledge about the duration of task executions, especially if meeting of time constraints has to be guaranteed, as in embedded and real time systems; • The worst-case execution time, WCET, is the basis for most scheduling algorithms; WCET is an upper bound on the execution times of tasks; • Computing WCET is undecidable in the general case; it results from the fact that it is undecidable whether or not a program terminates; • WCET can only be computed for certain programs/ tasks, such as programs without recursion and while loops and with constant iteration counts; • Computing tight upper bounds on the execution time may still be difficult; estimations of WCET in modern processors are affected by: • Pipelines with their different kinds of hazards and • Memory hierarchies with limited predictability of hit rates; • Computing the WCET for systems with caches and pipelines is a research topic;
Advanced Embedded Systems • Scheduling is closely linked to RTOS but some scheduling techniques are independent of the RTOS; • Scheduling algorithms may be required a number of times during the design of ESs and RTSs: • While fixing the specifications; • During hardware/ software partitioning; • After compilation; • At run-time (for ex. to decide which task is to be executed next); • Classification of scheduling algorithms
Advanced Embedded Systems • Soft and hard deadlines: • Scheduling for soft deadlines is based on extensions to standard operating systems; for ex. providing task and operating system call priorities may be sufficient for systems with soft deadlines; • Dynamic and static schedulers can be used for hard deadline systems; • Scheduling for periodic and aperiodic tasks: • Tasks which must be executed once every p units of time are called periodic tasks and p is called their period; each execution of a periodic task is called a job; • Tasks which are not periodic are called aperiodic; • Aperiodic tasks requesting the processor at unpredictable times are called sporadic, if there is a minimum separation between the times at which they request the processor; • Preemtive and non-preemptive scheduling: • Non-preemptive schedulers are based on the assumption that tasks are executed until they are done; as a result the response time for external events may be quite long if some tasks have a large execution time; • Preemptive schedulers have to be used if some tasks have long execution times or if the response time for external events must be short;
Advanced Embedded Systems • Static and dynamic scheduling: • Static schedulers take their decisions at design time; they are based on planning the start times of tasks and generate tables of start times needed by a dispatcher; the dispatcher can be controlled by a timer, causing it to analyze the table; systems which are totally controlled by a timer are said to be entirely time triggered, TT systems; • Dynamic schedulers take decisions at run-time; they are flexible but generate overhead at run-time; also, they are usually not aware of global contexts such as resource requirements or dependences between tasks; in case of ESs, such global contexts are typically available at design time and should be exploited; • Centralized and distributed scheduling: multiprocessor scheduling algorithms can either be executed locally on one processor or can be distributed among a set of processors;
Advanced Embedded Systems • Mono and multiprocessor scheduling: • Simple scheduling algorithms handle the case of single processors; • Complex algorithms are appropriate for systems with multiple processors; there are algorithms for homogeneous and heterogeneous multiple processors; • Online and offline scheduling: • On line scheduling algorithms schedule tasks at run-time, based on the information about the tasks arrived so far; • Offline algorithms schedule tasks, taking a priori knowledge about arrival time, execution times and deadlines into account; • Cost function: • Different algorithms aim at minimizing different functions; • Maximum lateness is defined as the difference between the completion time and the deadline, maximized over all tasks; it is negative if all tasks complete before their deadline; • Independent and dependent tasks: it is possible to distinguish between simple tasks, without any intertask communication and other complex tasks;
Advanced Embedded Systems • Scheduling with data dependencies or with resource dependencies: • Data dependencies can be represented by directed edges in a graph whose nodes are the processes; unweighted directed graphs specify order; weighted directed edges specify the minimum time required between the completion of the source node process and the initiation of the sink node process; • Some simple bounds on sets of processes with data dependencies can be computed in order to guide scheduling algorithms; the as-soon-as-possible, ASAP, schedule for a set of processes is constructed by putting each process at its earliest possible time as limited by data dependencies; in the as-late-as-possible, ALAP, schedule each process takes place at its latest possible time; • Resource dependencies come from implementation; for ex. two processes requiring the same memory location need the same resource; resource dependencies can be represented by undirected edges in the process graph; two processes that need the same resource do not have to execute in any particular order but they cannot execute at the same time; if, however, they must access the resource in a particular order, then a data dependency can be used to express this fact;
Advanced Embedded Systems • Static schedulers can be implemented in several ways: • Code from several programs can be interleaved to create a unified program; • A state machine that uses subroutines to call the processes can be built; • Neither of these programs use timers, which limits the accuracy of program timing but reduces overhead; • A common form of constructive scheduler is the list scheduler; • This algorithm forms a list of processes to be scheduled, then takes processes at the head of the list to form the schedule; the scheduling heuristic is embodied in the algorithm that determines where a process is placed in the list; • Next fig. presents an example problem for list scheduling; • Given two different heuristics for building the list, two different schedules are obtained; • The first heuristic adds processes to the list in order of CPU time; the process with the smallest CPU time goes first;
Advanced Embedded Systems • In this case two schedules are possible since 2 and 4 take the same amount of CPU time; • The second heuristic adds processes in order of their periods with the shortest period going first; • The process orders in the two cases are different; which heuristic is best depends on the circumstances and goals of the system;
Advanced Embedded Systems • Aperiodic scheduling • Scheduling with no precedence constraints • Let {Ti} be a set of tasks and let: • ci be the execution time of Ti; • di be the deadline interval, that is the time between Ti becoming available and the time until which Ti has to finish execution; • li be the laxity or slack defined as li = di - ci • Case of monoprocessor systems for which all tasks arrive at the same time: a simple scheduling algorithm is Earliest Due Date, EDD, and is based on the following rule: • Given a set of n independent tasks, any algorithm that executes the tasks in order of nondecreasing deadlines is optimal with respect to minimizing the maximum lateness; • EDD requires all tasks to be sorted by their deadlines; • Case of monoprocessor systems with different arrival times: preemption can potentially reduce maximum lateness; an algorithm is Earliest Deadline First, EDF and is based on the following rule:
Advanced Embedded Systems • Given a set of independent tasks with arbitrary arrival times, any algorithm that at any instant executes the task with the earliest absolute deadline among all the ready tasks is optimal with respect to minimizing the maximum lateness; • EDF requires that, each time a new ready task arrives, it is inserted into a queue of ready tasks, sorted by their deadlines; • If a newly arrived task is inserted at the head of the queue, the currently executing task is preempted; • Fig. shows a schedule obtained with the EDF algorithm: • At time4, task T2 has an earlier deadline; therefore it preempts T1; at time 5, task T3 arrives; due to its later deadline it does not preempt T2;
Advanced Embedded Systems • Least Laxity, LL, is another scheduling strategy: • Task priorities are a monotonically decreasing function of the laxity (the less laxity, the higher the priority); LL scheduling is also preemptive; • Next fig. shows an example of LL schedule, together with the computations of the laxity: • At time 4, task T1 is preempted, as before; at time 5, T2 is now also preempted due to the lower laxity of task T3; • LL scheduling requires periodic checks of the laxity and, in contrast to EDF scheduling, the knowledge of the execution time;
Advanced Embedded Systems • If preemption is not allowed, optimal schedulers may have to leave the processor idle a certain time in order to finish tasks with early deadlines arriving late; • Let T1 be a periodic process with an execution time of 2, a period of 4 and a deadline interval of 4; let T2 be a task occasionally becoming available at times 4n + 1 and having an execution time and a deadline interval of 1; it is assumed that the concurrent execution of T1 and T2 is not possible due to some resource conflict; • The scheduler starts the execution of task T1 at time 0; since it is not preemptive, it cannot start T2 when this becomes available, at time 1; hence, T2 misses its deadline; • It is necessary that the scheduler leave the process T1 idle (time 4 in fig.) but this means that the scheduler is not optimal;
Advanced Embedded Systems • Scheduling with precedence constraints • The fig. shows a task graph reflecting tasks dependences: task T3 can be executed only after tasks T1 and T2 have completed and sent messages to T3: • The fig. also shows a schedule; for static scheduling, this schedule can be stored in a table, indicating to the dispatcher the times at which tasks must be started and at which messages must be exchanged; • There are algorithms for minimizing the maximum lateness for the case of simultaneous arrival times (Latest Deadline First, LDF) and for the case of asynchronous arrival times (a modified EDF algorithm);