Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Similar documents
Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Concurrent Programming. Implementation Alternatives. Content. Real-Time Systems, Lecture 2. Historical Implementation Alternatives.

Concurrent Programming

Implementation Alternatives. Lecture 2: Implementation Alternatives & Concurrent Programming. Current Implementation Alternatives

Programming Embedded Systems

Resource management. Real-Time Systems. Resource management. Resource management

Precept 3: Preemptive Scheduler. COS 318: Fall 2018

Timers 1 / 46. Jiffies. Potent and Evil Magic

Real-Time Systems. Lecture #4. Professor Jan Jonsson. Department of Computer Science and Engineering Chalmers University of Technology

INTERRUPTS in microprocessor systems

Xinu on the Transputer

Tasks. Task Implementation and management

Interrupts. How can we synchronize with a peripheral? Polling

THE CPU SPENDS ALMOST ALL of its time fetching instructions from memory

Lecture 3: Concurrency & Tasking

Real-Time Systems Hermann Härtig Real-Time Operating Systems Brief Overview

EECS 482 Introduction to Operating Systems

Process Scheduling Queues

A New Real-time Kernel development on an embedded platform

Roadmap for This Lecture

Implementing Scheduling Algorithms. Real-Time and Embedded Systems (M) Lecture 9

Real-Time Systems. Real-Time Operating Systems

SMD149 - Operating Systems

Notes based on prof. Morris's lecture on scheduling (6.824, fall'02).

Lecture 2 Process Management

Virtual Machine Design

REAL-TIME OPERATING SYSTEMS SHORT OVERVIEW

Signals, Synchronization. CSCI 3753 Operating Systems Spring 2005 Prof. Rick Han

In Class Assignment 2

Micrium µc/os II RTOS Introduction EE J. E. Lumpp

CPS221 Lecture: Threads

Time Handling in Programming Language

Case Study. Windows XP. Windows Operating System Internals - by David A. Solomon and Mark E. Russinovich with Andreas Polze

Processes. Overview. Processes. Process Creation. Process Creation fork() Processes. CPU scheduling. Pål Halvorsen 21/9-2005

Process management. Scheduling

Course Syllabus. Operating Systems

Operating Systems (2INC0) 2018/19. Introduction (01) Dr. Tanir Ozcelebi. Courtesy of Prof. Dr. Johan Lukkien. System Architecture and Networking Group

Computer Systems Assignment 4: Scheduling and I/O

Operating Systems Design Fall 2010 Exam 1 Review. Paul Krzyzanowski

THREADS ADMINISTRIVIA RECAP ALTERNATIVE 2 EXERCISES PAPER READING MICHAEL ROITZSCH 2

Deadlock. Lecture 4: Synchronization & Communication - Part 2. Necessary conditions. Deadlock handling. Hierarchical resource allocation

Grundlagen Microcontroller Interrupts. Günther Gridling Bettina Weiss

Object Oriented Programming. Week 10 Part 1 Threads

Design and Implementation Interrupt Mechanism

EECS 482 Introduction to Operating Systems

Real-Time Performance of Linux. OS Latency

Microprocessors & Interfacing

Zilog Real-Time Kernel

Processes and Non-Preemptive Scheduling. Otto J. Anshus

INTERRUPT MANAGEMENT An interrupt is a hardware mechanism used to service an event that can be external or internal to

e-ale-rt-apps Building Real-Time Applications for Linux Version c CC-BY SA4

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

Real-Time and Concurrent Programming Lecture 6 (F6): Scheduling and bounded response times

Lecture Topics. Announcements. Today: Uniprocessor Scheduling (Stallings, chapter ) Next: Advanced Scheduling (Stallings, chapter

CSL373: Lecture 5 Deadlocks (no process runnable) + Scheduling (> 1 process runnable)

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

Reference Model and Scheduling Policies for Real-Time Systems

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Processes. CS 475, Spring 2018 Concurrent & Distributed Systems

GLOSSARY. VisualDSP++ Kernel (VDK) User s Guide B-1

Scheduling. CS 161: Lecture 4 2/9/17

AC OB S. Multi-threaded FW framework (OS) for embedded ARM systems Torsten Jaekel, June 2014

Newbie s Guide to AVR Interrupts

HY345 - Operating Systems

Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology

Embedded Systems. 5. Operating Systems. Lothar Thiele. Computer Engineering and Networks Laboratory

THREADS & CONCURRENCY

Real Time Linux patches: history and usage

Last Class: CPU Scheduling! Adjusting Priorities in MLFQ!

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Concurrent Computing CSCI 201 Principles of Software Development

THREADS AND CONCURRENCY

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 09, SPRING 2013

Embedded Systems Programming

Animation Part 2: MoveableShape interface & Multithreading

Locks. Dongkun Shin, SKKU

A Predictable RTOS. Mantis Cheng Department of Computer Science University of Victoria

Multitasking and scheduling

Interrupts, Etc. Operating Systems In Depth V 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Process Description and Control

Exam Solutions EDA040 Concurrent and Real-Time Programming

OVERVIEW. Last Week: But if frequency of high priority task increases temporarily, system may encounter overload: Today: Slide 1. Slide 3.

By Arjan Van De Ven, Senior Staff Software Engineer at Intel.

Latency on preemptible Real-Time Linux

AN HONORS UNIVERSITY IN MARYLAND UMBC. AvrX. Yousef Ebrahimi Professor Ryan Robucci

2. Introduction to Software for Embedded Systems

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Efficiency and memory footprint of Xilkernel for the Microblaze soft processor

EE458 - Embedded Systems Lecture 8 Semaphores

CSC Operating Systems Spring Lecture - XII Midterm Review. Tevfik Ko!ar. Louisiana State University. March 4 th, 2008.

CPU Scheduling. Operating Systems (Fall/Winter 2018) Yajin Zhou ( Zhejiang University

CS 326: Operating Systems. CPU Scheduling. Lecture 6


Distributed Real- Time Control Systems. Lecture 7 Real- Time Control

KERNEL THREAD IMPLEMENTATION DETAILS. CS124 Operating Systems Winter , Lecture 9

Chapter 5 - Input / Output

3.1 Introduction. Computers perform operations concurrently

I/O Management Software. Chapter 5

Informatica 3. Marcello Restelli. Laurea in Ingegneria Informatica Politecnico di Milano 9/15/07 10/29/07

AUTOBEST: A United AUTOSAR-OS And ARINC 653 Kernel. Alexander Züpke, Marc Bommert, Daniel Lohmann

Transcription:

Interrupts and Time Real-Time Systems, Lecture 5 Martina Maggio 28 January 2016 Lund University, Department of Automatic Control

Content [Real-Time Control System: Chapter 5] 1. Interrupts 2. Clock Interrupts 3. Time Primitives 4. Periodic Tasks 2

External Communication A real-time system must communicate with the environment: A/D and D/A converters; serial and parallel ports; keyboard and mouse; bus interfaces; timers. The communication can be based on (1) polling, (2) interrupts. 3

Interrupts

Interrupts Interrupts are generated at the CPU hardware level, asynchronously. When an interrupt is generated the execution is transfered to an interrupt handler method. Interrupt Table Interrupt #2 Save and restore Program Counter Interrupt Handler Save Status Restore Status The Interrupt Table contains a mapping between the Interrupt Number and the Interrupt Handler that should be called. The interrupt number is known as the Interrupt Request (IRQ). 5

Interrupts When an interrupt is received, the program counter is saved (and later restored). The interrupt handler saves all the status of the registers it uses and restore the status when it terminates. The context can be saved: in the stack of the interrupted process; in a special stack common to all interrupts; in a specialized set of registers (DSPs, Power PC). A context switch may be initiated from the interrupt handler. In this case, the program counter will be restored to a different value when the interrupt handler terminates. 6

Interrupt Priorities Process Handler Level 3 Handler Level 2 Handler Level 1 Interrupt #2 Interrupt #1 Interrupt #3 7

Interrupt Priorities Disabling the interrupts in the kernel causes all interrupt levels to be disabled (hardware priorities). It is only possible to store a limited number of pending interrupts. Interrupts handlers need to be short and efficient. Time consuming processing in device processes. 8

Time-based vs Event-based Kernels Most real-time kernels are tick-based: A system clock gives interrupts at regular intervals; Typical tick intervals are 1 ms, 10 ms; Defines the time resolution of the kernel. An event-based kernel relies on a high-precision timer to keep track of time: No regular clock interrupts. 9

Clock Interrupts

Clock Interrupts Process Clock Interrupt Handler Clock interrupt Clock Procedure Tick 11

[STORK] Clock Procedure 1 PROCEDURE Clock; 2 VAR P: ProcessRef; 3 BEGIN 4 IncTime(Now,Tick); (* Now := Now + Tick *) 5 LOOP 6 P := TimeQueue^.succ; 7 IF CompareTime(P^.head.nextTime,Now) <= 0 THEN 8 MovePriority(P,ReadyQueue); 9 ELSE EXIT; 10 END; 11 END; 12 DEC(Running^.timer); (* Round-robin time slicing *) 13 IF Running^.timer <= 0 THEN 14 MovePriority(Running,ReadyQueue); 15 END; 16 Schedule; 17 END Clock; 12

[STORK] Clock Procedure Now is a global variable that keeps track of the current time. TimeQueue is a time-sorted list containing processes waiting on time. Round-robin time-slicing within the same priority levels: if a process has executed longer than its time slice and other processes with the same priority are ready then a context switch takes place; used by the Linux real-time scheduling class SCHED RR. The Linux real-time scheduling class SCHED FIFO does not use round-robin within the same priority levels. 13

Event-based Clock Interrupts Clock interrupts from a variable time source (e.g. high-resolution timer) instead of a fixed clock. When a process is inserted in TimeQueue the kernel sets up the timer to give an interrupt at the wake-up time of the first process in TimeQueue. When the clock interrupt occurs, a context switch to the first process is performed and the timing chip is set up to give an interrupt at the wake-up time of the new first process in TimeQueue. 14

[JAVA] Interrupts In the native thread model each Java thread is mapped onto a separate thread. Essentially as in STORK. In the green thread model: The system level interrupt handling facility has no notion of Java threads. When a Java thread performs a blocking operation the JVM indicates that it wants to be informed by the operating system when the associated interrupt occurs. The JVM Linux thread does not block until it has serviced all Java threads that are Ready. When no Java threads are Ready, the JVM thread does a selective wait on all the IO interrupts that it needs to be informed about. A timeout is set to the time when the next sleeping Java thread should execute. 15

[LINUX] Interrupts The interrupt handler is known as the Interrupt Service Routine (ISR). The conflicting goal of having ISRs that both execute fast and perform a lot of work is solved by splitting them in two halves: the top half (the actual interrupt handler); the bottom half: executes at later stage (deferred until later); executes in a similar way as an ordinary task, but is more efficient, e.g., has a smaller context; compare with device processes; supported in multiple ways, like * softirq, * tasklet, * work queue. 16

Exceptions Many modern programming languages support software fault handling using exceptions. When a fault occurs in a piece of code, an exception is raised (or thrown). The run-time system locates the closest handler for the exception and transfers the execution to it. Many similarities with interrupts: exceptions occur synchronously w.r.t. the processor clock, i.e. they can be seen as synchronous interrupts generated by the processor; interrupts = asynchronous interrupts generated by the hardware. 17

Exceptions Procedure A A calls B Procedure B B calls C Procedure C B terminates Exception X raised Handler for X 18

[JAVA] Exception Handling 1 try { 2 // Perform some method calls that 3 // might throw exceptions. 4 } catch (Exception e) { 5 // Control transfered here if there 6 // is an exception. Handle the fault. 7 } finally { 8 // These lines are always executed. Clean-up. 9 } 19

Time Primitives

Wait Time Primitives Two main types: Wait for a specified time interval: relative to current time; sleep (Java), delay (Ada), WaitTime (STORK). Wait until a specified time (more powerful): absolute time; delayuntil (Ada), WaitUntil (STORK); not available in Java. 21

Wait Time and Process States Running Ready waiting event Blocked When WaitTime/WaitUntil is called: process moved from Running to Blocked (moved from ReadyQueue to TimeQueue). When time has passed: process moved from Blocked to Ready (done in the Clock procedure). 22

[STORK] Time Primitives PROCEDURE Tick(): CARDINAL; Returns the tick interval of the current machine in milliseconds. This makes it possible to write real-time code that is portable between platforms with different time resolution. PROCEDURE CurrentTime(VAR t: Time); Returns the current time (Now). PROCEDURE IncTime(VAR t: Time, c: CARDINAL); Increments the value of t with c milliseconds. 23

[STORK] Time Primitives PROCEDURE CompareTime(VAR t1,t2: TIME): INTEGER; Compares two time variables. Returns 1 if t1 < t2. Returns 0 if t1 = t2. Returns 1 if t1 > t2. PROCEDURE WaitUntil(t: Time); Delays the calling process until Now t. If Now is already larger than t when WaitUntil is called it is a null operation. PROCEDURE WaitTime(t: CARDINAL); Delays the calling process for t milliseconds. 24

[STORK] Time Primitives: Implementation 1 PROCEDURE WaitUntil(t: Time); 2 BEGIN 3 Running^.head.nextTime := t; 4 MoveTime(Running,TimeQueue); 5 Schedule; 6 END WaitUntil; 7 8 9 PROCEDURE WaitTime(t: CARDINAL); 10 VAR next: Time; 11 BEGIN 12 CurrentTime(next); 13 IncTime(next,t); 14 WaitUntil(next); 15 END WaitTime; 25

[JAVA] Time Primitives There is no WaitUntil, only WaitTime (sleep). Methods: sleep(long milliseconds); puts the currently executing thread to sleep for (at least) the specified amount of time. Static method of the Thread class; currenttimemillis(); returns the current time in milliseconds. Static method of the System class. 26

The Idle process What to do when all the processes are blocked? The CPU contains no other processes. The Idle process (lowest priority) is executed. 1 (* Process *) PROCEDURE Idle; 2 BEGIN 3 SetPriority(MaxPriority - 1); 4 LOOP END; 5 END Idle; The CPU contains other non-real-time processes. Wait until the wakeup time of the first process in the TimeQueue. 27

A complete real-time kernel Real-Time kernel parts: how a process/thread/task is represented; what happens during a context switch; communication and synchronization mechanisms; interrupt handling; sleep; the idle process. 28

A complete real-time kernel: Task Queues ReadyQueue: one in the single-processor case or when using global scheduling for multicores; multiple in case of partitioned scheduling for multicores; sorted in priority order. TimeQueue: sorted in earliest wakup time order. WaitQueues for semaphores, monitors, locks: sorted in priority order. WaitQueues for threads waiting for event/condition variable: normally sorted in priority order. 29

Reasons for a context switch 1 The running thread executes an operation that leads to a context switch. Voluntarily releases the CPU: sleeps, the thread terminates, yields. Performs an operation that may cause it to block: waits on semaphore, tries to take/lock a monitor. Performs an operation that unblocks another higher priority thread: signals a semaphore, returns a lock. 30

Reasons for a context switch 2 Due to an interrupt. Clock interrupt: a sleeping thread of higher priority than the executing one is woken up; the running thread has executed longer than its time slice and there is another thread with the same priority that is ready to execute. Other types of interrupts, like bus, keyboard, mouse: context switch to a device thread that handles the interrupt, which eventually may cause a context switch to a thread waiting for events like input/output ones. 31

Periodic Tasks

Implementing Periodic Tasks Periodic tasks are very common in real-time systems. Implementation options without a real-time kernel: Implement each periodic activity in an interrupt handler associated with a period timer: only limited number of timers; difficult and error-prone. Use a static cyclic executive: scheduler driven by period timer; inflexible. 33

Implementing Periodic Tasks Implementation options with a real-time kernel: Real-Time kernel with wait time primitives: self-scheduling tasks (infinite loop with wait statements). Real-Time kernel with explicit support for periodic tasks: allows the programmer to register a function in the kernel to be executed every T seconds; not common. 34

Periodic Tasks Actual start time Actual start time Ideal start time Latency Period Service time Ideal start time Ideal start time Latency: Release jitter due to limited time precision (for example tick scheduling) and preemption from higher-priority tasks. Service time: Actual execution time and preemption from higher-priority tasks. 35

Implementing Self-Scheduling Periodic Tasks Attempt 1 1 LOOP 2 PeriodicActivity; 3 WaitTime(h); 4 END; Does not work. The period is greater than h, and time-varying. The execution time of PeriodicActivity is not accounted for. 36

Implementing Self-Scheduling Periodic Tasks Attempt 2 1 LOOP 2 CurrentTime(Start); 3 PeriodicActivity; 4 CurrentTime(Finish); 5 Duration := Finish - Start; 6 WaitTime(h - Duration); 7 END; Does not work. An interrupt causing suspension may occur between the assignment and WaitTime. Need a WaitUntil primitive. 37

Implementing Self-Scheduling Periodic Tasks Attempt 3 1 LOOP 2 CurrentTime(t); 3 PeriodicActivity; 4 IncTime(t, h); 5 WaitUntil(t); 6 END; Does not work. Preemption by a higher-priority task may delay CurrentTime from being executed. 38

Implementing Self-Scheduling Periodic Tasks Attempt 4 1 CurrentTime(t); 2 LOOP 3 PeriodicActivity; 4 IncTime(t, h); 5 WaitUntil(t); 6 END; Correct. Will however try to catch up if the actual execution time of PeriodicActivity occasionally becomes larger than the period. 39

Implementing Self-Scheduling Periodic Tasks Attempt 5: reset the base time in case of overruns. Accept a too long sample and try to be on time from that sample on. 1 PROCEDURE NewWaitUntil(VAR t : TIME) // VAR = call-by-reference 2 VAR diff : INTEGER; 3 4 BEGIN 5 disableinterrupts; 6 diff := CompareTime(t, Now); 7 IF diff > 0 THEN // not overrun 8 Running^.head.nextTime := t; 9 MoveTime(Running, TimeQueue); 10 Schedule; 11 ELSE 12 CurrentTime(t); 13 END; 14 enableinterrupts; 15 END NewWaitUntil; 16 END; 40

Implementing Self-Scheduling Periodic Tasks Attempt 5: the code becomes: 1 CurrentTime(t); 2 LOOP 3 PeriodicActivity; 4 IncTime(t, h); 5 NewWaitUntil(t); 6 END; 41

[JAVA] Implementing Self-Scheduling Periodic Tasks 1 public void run() { 2 long h = 10; // period (ms) 3 long duration; 4 long t = System.currentTimeMillis(); 5 6 while (true) { 7 periodicactivity(); 8 t = t + h; // when it should be repeated 9 duration = t - System.currentTimeMillis(); 10 if (duration > 0) { 11 try { 12 sleep(duration); 13 } catch (InterruptedException e) { e.printstacktrace(); } 14 } 15 } 16 } 42

Foreground-Background Scheduler Foreground tasks (like controllers) execute in interrupt handlers. The background task runs as the main program loop. A common way to achieve simple concurrency on low-end implementation platforms that do not support any real-time kernels. Will be used in the ATMEL AVR projects in the course as well as in Lab3. 43

Periodic Execution in The ATMEL AVR mega16 Main Program: 1 #include <avr/io.h> 2 #include <avr/signal.h> 3 #include <avr/interrupt.h> 4 5 int main() { 6 TCNT2 = 0x00; /* Timer 2: Reset counter (periodic timer) */ 7 TCCR2 = 0x0f; /* Set clock prescaler to 1024 */ 8 OCR2 = 144; /* Set the compare value, corr. to ~100 Hz 9 when clock runs @14.7 MHz */ 10 /* 14.7 MHz/1024/144 is approx 100 Hz */ 11 12 outp(bv(ocie2),timsk); /* Start periodic timer */ 13 sei(); /* Enable interrupts */ 14 while (1) { 15 /* Do some background work */ 16 } 17 } 44

Periodic Execution in The ATMEL AVR mega16 1 /** Timer Interrupt Handler: 2 * Interrupt handler for the periodic timer. 3 * Interrupts are generated every 10 ms. The 4 * control algorithm is executed every 50 ms. 5 */ 6 SIGNAL(SIG_OUTPUT_COMPARE2) { 7 static int8_t ctr = 0; /* static to retain value 8 between invocations! */ 9 if (++ctr == 5) { 10 ctr = 0; 11 /* Run the controller */ 12 } 13 } 45