1 / 32

Module 5: Implementing Real-time Systems

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.

prisca
Download Presentation

Module 5: Implementing Real-time Systems

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Module 5: Implementing Real-time Systems

  2. Overview • Defining Terms • Windows CE Kernel Features • Primer on Threads and Synchronization • Interrupt Handling • Scheduler Control • Optimizing a Windows CE-device

  3. 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.

  4. 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

  5. Primer on Threads and Synchronization • Why Synchronize? • Synchronize with Critical Sections • Synchronize with Mutexes

  6. 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...*/ } }

  7. 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); }

  8. 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); }

  9. 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

  10. 8 9 7 2 6 3 5 1 4 The Windows CE Interrupt Model Interrupt Service Thread Kernel Interrupt Service Routine Hardware !!

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. Mapping Physical IRQ to ISR Interrupt Service Routine Interrupt Number • 0 • 1 • 2 • 3 • 4 • … • HandleTimer • HandleMyDevice • HandleMouse • HandleKeyboard • HandleOtherDevice • …

  17. Mapping Logical Interrupts to Event Handle Event Handle Logical Interrupt • SYSINTR_RESCHED • SYSINTR_FIRMWARE • SYSINTR_MOUSE • SYINTR_KEYBOARD • heventTimer • hEventMyDevice • hEventMouse • hEventKeyboard • hEventOtherDevice • …

  18. Scheduler Interrupt Raise Interrupt Exception? No OEMDisableInterrupt OEMInterruptHandler return SYSINTR_RESCHED OEMInterruptDone Kernel Hardware OEM Adaptation Layer

  19. 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

  20. Scheduler Control • Thread Priorities • Thread Quantum Control • Certifying Trust • Synchronization Enhancements • Higher Timer Resolution • Better Priority Inversion Handling

  21. 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)

  22. 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)

  23. 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

  24. Synchronization Enhancements • TryEnterCriticalSection() – • Non-blocking call. • Semaphores • A "mutex with a count"

  25. 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

  26. Better Priority Inversion Handling Mutex Priority Blocks on Mutex Acquires Mutex A B C Acquires Mutex Frees Mutex Time

  27. Optimizing a Windows CE-device • Optimizing Tips • Measurement Tools • Measurement APIs

  28. 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()

  29. Measurement Tools • ILTIMING – Check Interrupt Latency • \wince300\public\common\oak\utils\iltiming • OSBENCH – Thread scheduling • \wince300\public\common\oak\utils\osbench

  30. Measurement APIs • QueryPerformanceFrequency() • Returns Ticks per Second • QueryPerformanceCounter() • Returns Current tick count • GetTickCount()

  31. Lab 5: Real-Time Programming

  32. Review • Defining Terms • Windows CE Kernel Features • Primer on Threads and Synchronization • Interrupt Handling • Scheduler Control • Optimizing a Windows CE-device

More Related