Download presentation
Presentation is loading. Please wait.
Published byNoah Dawson Modified over 8 years ago
1
Lecture 9 Scheduling
2
Scheduling Policies
3
Preemptive Priority Scheduling
5
Round Robin Scheduling
16
than fixed-priority case.
17
Because LL algorithm (and ED )assume that all the deadlines are hard.
23
Minimizing Maximum Lateness Scheduling Sometimes one task cannot start until another completes. In addition, the completion of each action j is associated with a cost function h j. An optimal scheduling minimizing cost can be constructed using the following off-line static algorithm:
24
Minimizing Maximum Lateness Scheduling J –ordered set of tasks already scheduled J c –set of task still to be scheduled(complement of J) J’ –set of task that can be scheduled immediately before J(J can’t start before J’ completes) 1. J empty, J c =all the tasks, J’=all tasks with no successors 2. Select j* from J c with minimum h j* that has no predecessor in J c (so j* must be in J’) 3. Add j* to J and remove from J c 4. Adjust J’ 5. If J c is empty stop, otherwise go to step 2
25
Our project There is no OS Every group should implement a scheduler in the project and can use any of the above scheduling algorithms The main loop is the scheduling loop You should state in the code and the design document which scheduling algorithm you are using. Alternatively, you can upload any operating system from the Web to the microcontrollers.
26
Example Task1 should be performed every 100 ms (high priority) Task2 should be performed every 150 ms (low priority) There are simple Interrupt 1 and Interrupt 2 (short exe time) There is Interrupt3 that creates a new med priority task in the system.
27
Example Tasks = {Task1,Task2}; Last_call_task1 = 0; Last_call_task2 = 0; Simple Interrupt 1 handler; Simple Interrupt 2 handler; Interrupt 3 handler {Add Task3 to the Tasks} main() { while(1){} } Timer interrupt every 10 ms {ReadyTasks=Tasks; IF (current_time - Last_call_task1 ) < 100 ms THEN remove Task1 from ReadyTasks; IF (current_time - Last_call_task2 ) < 150 ms THEN remove Task2 from ReadyTasks; // apply here the scheduling algorithm. For example: Choose task from ReadyTasks with maximal priority; //Emulate context change: move PC register to run the chosen task } Task 1(or 2 or 3) function {interrupt disable; … interrupt enable; set PC to while(1) }
28
More comments on MPC430 working environment and general concepts of embedded programming
29
Using efficient data types The data types you use should be considered carefully, because this can have a large impact on code size and code speed: ● Use small data types. ● Try to avoid 64-bit data types, such as double and long long. ● Bitfields with sizes other than 1 bit should be avoided because they will result in inefficient code compared to bit operations. ● Using floating-point types is very inefficient, both in terms of code size and execution speed. If possible, consider using integer operations instead. ● Declaring a pointer to const data tells the calling function that the data pointed to will not change, which opens for better optimizations
30
Alignment on the MSP430 Can access memory using 8- or 16-bit operations. However, when a 16-bit access is performed, the data must be located at an even address. The MSP430 IAR C/C++ Compiler ensures this by assigning an alignment to every data type, ensuring that the MSP430 microcontroller will be able to read the data.
31
Rearranging elements in a structure The MSP430 microcontroller requires that data in memory must be aligned. Each element in a structure needs to be aligned according to its specified type requirements. This means that the compiler must insert pad bytes if the alignment is not correct.
32
Alignment – cont. Be careful about padding: ● Network communication protocols are usually specified in terms of data types with no padding in between ● It “wastes” data memory. There are two ways to solve the problem: 1. Use the #pragma pack directive. +Easy and fast solution -Each access to an unaligned element in the structure will use more code. 2. Write your own customized functions for packing and unpacking structures. +Will not produce any more code apart from your functions - You will need two views on the structure data—packed and unpacked.
33
#pragma pack Example: struct First { char alpha; short beta; }; #pragma pack(1) struct FirstPacked { char alpha; short beta; }; #pragma pack() struct Second { struct FirstPacked first; Short Empty; short gamma; };
34
Compiler optimization: common subexpression elimination Redundant re-evaluation of common subexpressions is by default eliminated at optimization levels Medium and High. This optimization normally reduces both code size and execution time. However, the resulting code might be difficult to debug.
35
Loop unrolling It is possible to duplicate the loop body of a small loop, whose number of iterations can be determined at compile time, to reduce the loop overhead. This optimization, which can be performed at optimization level High, normally reduces execution time, but increases code size. The resulting code might be difficult to debug. The compiler heuristically decides which loops to unroll. Different heuristics are used when optimizing for speed, size, or when balancing between size and speed.
36
Mixing C and Assembler There are several ways to mix C or C++ and assembler: ● Modules written entirely in assembler ● Intrinsic functions (the C alternative) ● Inline assembler
37
Intrinsic functions A small number of predefined functions that allow direct access to low-level processor operations without having to use the assembler language. They can be very useful in, for example, time- critical routines.
38
Intrinsic function – cont. An intrinsic function looks like a normal function call, but it is really a built-in function that the compiler recognizes. The intrinsic functions compile into inline code, either as a single instruction, or as a short sequence of instructions. The advantage of an intrinsic function compared to using inline assembler is that the compiler has all necessary information to interface the sequence properly with register allocation and variables. The compiler also knows how to optimize functions with such sequences; something the compiler is unable to do with inline assembler sequences. The result is that you get the desired sequence properly integrated in your code, and that the compiler can optimize the result. To use intrinsic functions in an application, include the header file intrinsics.h. Note that the intrinsic function names start with double underscores
39
Intrinsic functions – cont.
41
Writing efficient code Local variables—auto variables and parameters—are preferred over static or global variables. The reason is that the optimizer must assume, for example, that called functions may modify non- local variables. When the life spans for local variables end, the previously occupied memory can then be reused. Globally declared variables will occupy data memory during the whole program execution. Avoid taking the address of local variables using the & operator, because: 1. The variable must be placed in memory, and thus cannot be placed in a processor register. This results in larger and slower code. 2. The optimizer can no longer assume that the local variable is unaffected over function calls. Module-local variables—variables that are declared static—are preferred over global variables. Also avoid taking the address of frequently accessed static variables. The compiler is capable of inlining functions. instead of calling a function, the compiler inserts the content of the function at the location where the function was called. The result is a faster, but often larger, application. Inlining may enable further optimizations. The compiler often inlines small functions declared static. The use of the #pragma inline directive and the C++ keyword inline gives you fine-grained control, and it is the preferred method compared to the traditional way of using preprocessor macros. Avoid using inline assembler. Instead, try writing the code in C or C++, use intrinsic functions, or write a separate module in assembler language.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.