330 likes | 455 Views
Module 5: Implementing Real-time Systems. Overview. Defining Terms Windows CE Kernel Features Primer on Threads and Synchronization Interrupt Handling Scheduler Control Optimizing a Windows CE-device. Defining Terms.
E N D
Overview • Defining Terms • Windows CE Kernel Features • Primer on Threads and Synchronization • Interrupt Handling • Scheduler Control • Optimizing a Windows CE-device
Defining Terms • Real Time – A system in which specific data collection or device control must be handled within specified time parameters. • 1 Millisecond (1 ms) = .001 sec. • 1 Microsecond (1 ms) = .000001 sec. • Hard Real Time – Catastrophic results for failure to meet time constraints. • Soft Real Time – Non-catastrophic results for failing to meet time constraints.
Windows CE Kernel Features • Multi-Process OS (32 max) • Multi-Threaded • 256 thread priorities (New with Windows CE 3.0) • Synchronization Objects • Critical Sections • Mutexes • Semaphores (New with Windows CE 3.0) • Events • Memory • Paged • No backing store
Primer on Threads and Synchronization • Why Synchronize? • Synchronize with Critical Sections • Synchronize with Mutexes
Why Synchronize? WinMain(HANDLE hInstance, …) { CreateThread(…, A_Main, …); CreateThread(…, B_Main, …); } int x; DWORD WINAPI A_Main(LPVOID p) { for(x=0;x<100000;x++) { /* ...Work...*/ } } DWORD WINAPI B_Main(LPVOID p) { for(x=1000;x>0;x--) { /* ...Work...*/ } }
Synchronizing with Critical Sections CRITICAL_SECTION cs; WinMain(HANDLE hInstance, …) { InitializeCriticalSection(&cs); CreateThread(…, A_Main, …); CreateThread(…, B_Main, …); } int x; DWORD WINAPI A_Main(LPVOID p) { EnterCriticalSection(&cs); for(x=0;x<100000;x++) { /* ...Work...*/ } LeaveCriticalSection(&cs); } DWORD WINAPI B_Main(LPVOID p) { EnterCriticalSection(&cs); for(x=1000;x>0;x--) { /* ...Work...*/ } LeaveCriticalSection(&cs); }
Synchronizing with Mutexes HANDLE hMutex; WinMain(HANDLE hInstance, …) { hMutex = CreateMutex(…); CreateThread(…, A_Main, …); CreateThread(…, B_Main, …); } int x; DWORD WINAPI A_Main(LPVOID p) { WaitForSingleObject(hMutex,…); for(x=0;x<100000;x++) { /* ...Work...*/ } ReleaseMutex(hMutex); } DWORD WINAPI B_Main(LPVOID p) { WaitForSingleObject(hMutex,…); for(x=1000;x>0;x--) { /* ...Work...*/ } ReleaseMutex(hMutex); }
Interrupt Handling • Windows CE Interrupt Model • Interrupt Handling – Single ISR • Interrupt Handling – Multiple ISRs • Interrupt Service Thread Startup • Interrupt Service Thread Processing • Mapping Physical IRQ to ISR • Mapping Logical Interrupts to Event Handle • Scheduler Interrupt • Support for nested interrupts
8 9 7 2 6 3 5 1 4 The Windows CE Interrupt Model Interrupt Service Thread Kernel Interrupt Service Routine Hardware !!
Interrupt Handling – Single ISR Raise Interrupt Exception? Yes – Kernel handles it. Raise Interrupt Exception? No. OEMDisableInterrupt OEMInterruptHandler return SYSINTR_NOP OEMInterruptDone Kernel Hardware OEM Adaptation Layer
Interrupt Handling – Single ISR (continued) Raise Interrupt Exception? No OEMDisableInterrupt OEMInterruptHandler return SYSINTR_KEYBOARD 1. Map SYSINTR to hMutex 2. PulseEvent(hMutex) Interrupt Service thread wakes and handles interrupt Kernel Hardware OEM Adaptation Layer
Interrupt Handling – Multiple ISRs Raise Interrupt Exception? No OEMDisableInterrupt 1. Map IRQ to ISR 2. Call ISR return SYSINTR_KEYBOARD 1. Map SYSINTR to hMutex 2. PulseEvent(hMutex) Interrupt Service thread wakes and handles interrupt Kernel Hardware OEM Adaptation Layer
Interrupt Service Thread Startup OEMInit CreateThread ThreadMain() { hEvent= CreateEvent(…); InitializeInterrupt(SYINTR_KEYBOARD, hEvent, pData, cbData); while (1) { WaitForSingleObject(hEvent); // Do Work InterruptDone(SYSINTR_KEYBOARD); } InitializeInterrupt OEMInterruptEnable WaitForSingleObject Then, we wait. Until… Kernel OEM Adaptation Layer Device Driver
Interrupt Service Thread Processing OEMInterruptHandler return SYSINTR_KEYBOARD ThreadMain() { //Initialization code removed while (1) { WaitForSingleObject(hEvent); // Do Work InterruptDone(SYSINTR_KEYBOARD); } 1. Map SYSINTR To hMutex 2. PulseEvent(hEvent) InterruptDone OEMInterruptDone Kernel OEM Adaptation Layer Device Driver
Mapping Physical IRQ to ISR Interrupt Service Routine Interrupt Number • 0 • 1 • 2 • 3 • 4 • … • HandleTimer • HandleMyDevice • HandleMouse • HandleKeyboard • HandleOtherDevice • …
Mapping Logical Interrupts to Event Handle Event Handle Logical Interrupt • SYSINTR_RESCHED • SYSINTR_FIRMWARE • SYSINTR_MOUSE • SYINTR_KEYBOARD • heventTimer • hEventMyDevice • hEventMouse • hEventKeyboard • hEventOtherDevice • …
Scheduler Interrupt Raise Interrupt Exception? No OEMDisableInterrupt OEMInterruptHandler return SYSINTR_RESCHED OEMInterruptDone Kernel Hardware OEM Adaptation Layer
Support for Nested Interrupts Windows CE 2.x Windows CE3.0 • No nesting • ISRs always run to completion • ISTs run to completion when created with highest priority • Nesting Supported • ISRs can be interrupted • Registers saved and restored • Higher priority ISRs caninterrupt lower priority ISRs
Scheduler Control • Thread Priorities • Thread Quantum Control • Certifying Trust • Synchronization Enhancements • Higher Timer Resolution • Better Priority Inversion Handling
Thread Priorities • Windows CE 2.x • Eight priorities – 0 to 7 • SetThreadPriority(hThread, nPriority) • Windows CE 3.0 • 256 Priorities – 0 (real-time) to 255 • CeSetThreadPriority(hThread, nPriority)
Thread Quantum Control • Quantum = duration of time slice • Platform Builder – Set default • Windows CE uses a default value of 100ms • During call to OEMInit(), OEM can override this value • Override by setting dwDefaultThreadQuantum • Applications Can Override for Individual Threads: • DWORD CeGetThreadQuantum(); • BOOL CeSetThreadQuantum(DWORD dwTime)
Certifying Trust • Untrusted Applications: • Cannot call CeSetThreadPriority • Cannot call CeSetThreadQuantum • Platform Builder – OEMCertifyModule • Checking Trust: • DWORD CeGetCurrentTrust(void); • DWORD CeGetCallerTrust(void); • 0 = None, 1 = Some, 2 = All
Synchronization Enhancements • TryEnterCriticalSection() – • Non-blocking call. • Semaphores • A "mutex with a count"
Higher Timer Resolution • Sleep(int nMilliseconds) • 1 ms granularity on Windows CE 3.0 • Sleep(100); // block for 100 ms. • Sleep(5); // block for 5 ms. • Sleep(0); // yield quantum. • Avoid SetTimer() • Not Real Time • Message-based, UI-oriented timer
Better Priority Inversion Handling Mutex Priority Blocks on Mutex Acquires Mutex A B C Acquires Mutex Frees Mutex Time
Optimizing a Windows CE-device • Optimizing Tips • Measurement Tools • Measurement APIs
Optimizing Tips • Avoid priority inversion • Avoid file I/O from RT threads • Avoid graphic calls from RT threads • Avoid UI calls from RT threads • Pre-allocate Memory • Pre-allocate heap memory • Pre-commit stack memory • Control Paging • Use LoadDriver() - not LoadLibrary() • For Individual Pages - LockPages()
Measurement Tools • ILTIMING – Check Interrupt Latency • \wince300\public\common\oak\utils\iltiming • OSBENCH – Thread scheduling • \wince300\public\common\oak\utils\osbench
Measurement APIs • QueryPerformanceFrequency() • Returns Ticks per Second • QueryPerformanceCounter() • Returns Current tick count • GetTickCount()
Review • Defining Terms • Windows CE Kernel Features • Primer on Threads and Synchronization • Interrupt Handling • Scheduler Control • Optimizing a Windows CE-device