Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company
Agenda Introductions (5 minutes) Introductions (5 minutes) Terms (5 minutes) Terms (5 minutes) RT-features of Windows CE (15 minutes) RT-features of Windows CE (15 minutes) RT enhancements for Windows CE 3.0 (15 minutes) RT enhancements for Windows CE 3.0 (15 minutes) Optimization tips (20 minutes) Optimization tips (20 minutes) Discussion (15 minutes) Discussion (15 minutes)
Terms Real time – A system in which specific data collection or device control must be handled within specified time parameters Real time – A system in which specific data collection or device control must be handled within specified time parameters 1 Millisecond (1 ms) =.001 second 1 Microsecond (1 ms) = second Hard real time – Catastrophic results for failure to meet time-critical needs Hard real time – Catastrophic results for failure to meet time-critical needs Soft real time – Non-catastrophic results Soft real time – Non-catastrophic results
CE Kernel Features Multi-process operating system (32 max) Multi-process operating system (32 max) Multi-threaded Multi-threaded 256 thread priorities (new with Windows CE 3.0) Synchronization Synchronization Critical sections Mutexes Semaphores (new with Windows CE 3.0) Events Memory Memory Paged No backing store
Interrupt Handling Event NK.EXE IRQ User Mode Kernel Mode ISR = “Interrupt Service Routine” IST = “Interrupt Service Thread” A B C Event
Windows CE 3.0 Real-Time Features Better interrupt handling Better interrupt handling Support for nested interrupts Improved interrupt latencies Better control of Scheduler Better control of Scheduler More thread priorities Control of time-slice quantum Restrictions based on Trust Better synchronization support Higher timer resolution Better priority inversion handling
Nested Interrupts Windows CE 2.x Windows CE 2.x No nesting IRQs always run to completion ISTs run to completion when created with highest priority Windows CE 3.0 Windows CE 3.0 Nesting supported IRQs can be interrupted Registers saved and restored Interrupts only occur for higher priority IRQs
Interrupt Latency Event NK.EXE IRQ User Mode Kernel Mode ISR = “Interrupt Service Routine” IST = “Interrupt Service Thread” A B Event
Thread Priorities Windows CE 2.x Windows CE 2.x Eight priorities – 0 to 8 SetThreadPriority(hThread, nPriority) Windows CE 3.0 Windows CE 3.0 256 priorities – 0 (real-time) to 255 CeSetThreadPriority(hThread, nPriority)
New Thread Priorities Windows CE 2.x SetThreadPriority() Windows CE 2.x SetThreadPriority() 0 = real-time 1 2 3 = normal 4 5 6 7 = idle-time Windows CE 3.0 CeSetThreadPriority() Windows CE 3.0 CeSetThreadPriority() 0 (RT) to 247 248 249 250 251 252 253 255
Thread Quantum Control Quantum = Duration of time slice Quantum = Duration of time slice Platform builder – Set default Platform builder – Set default During call to OEMInit() Set dwDefaultThreadQuantum Individual threads: Individual threads: DWORD CeGetThreadQuantum(); BOOL CeSetThreadQuantum (DWORD dwTime)
Thread Quantum Examples // Set quantum to 100 ms. CeSetThreadQuantum(100); // Set quantum to 500 ms. CeSetThreadQuantum(500); // Set run to completion. CeSetThreadQuantum(0);
Certifying Trust Untrusted applications: Untrusted applications: Cannot call CeSetThreadPriority Cannot call CeSetThreadQuantum Platform Builder – OEMCertifyModule Platform Builder – OEMCertifyModule Checking Trust: Checking Trust: DWORD CeGetCurrentTrust(void); DWORD CeGetCallerTrust(void); 0 = None, 1 = Some, 2 = All
Synchronization Enhancements TryEnterCriticalSection() – TryEnterCriticalSection() – Non-blocking call Semaphores Semaphores A “mutex with a count”
Timers Sleep(int nMilliseconds) Sleep(int nMilliseconds) 1 ms granularity on CE 3.0 Sleep(100); // block for 100 ms. Sleep(5); // block for 5 ms. Sleep(0); // yield quantum. Avoid SetTimer() Avoid SetTimer() Not RT Message-based, UI-oriented timer
Priority Inversion Mutex ABC Priority Time Blocks on Mutex Acquires Mutex Frees Mutex
Optimizing Tips Avoid priority inversion Avoid priority inversion Avoid file I/O from RT threads Avoid graphic calls from RT threads Avoid UI calls from RT threads Memory Memory Pre-allocate heap memory Pre-commit stack memory To Control Paging To Control Paging Use LoadDriver() - not LoadLibrary() For individual pages - LockPages()
Measurement Tools ILTIMING – Check Interrupt Latency ILTIMING – Check Interrupt Latency \wince300\public\common\oak\ utils\iltiming OSBENCH – Thread scheduling OSBENCH – Thread scheduling \wince300\public\common\oak\ utils\osbench White paper: White paper: embedded/ce/resources/developing
Measurement APIs QueryPerformanceFrequency() QueryPerformanceFrequency() QueryPerformanceFrequency() Returns Ticks per Second QueryPerformanceCounter() QueryPerformanceCounter() Returns Current tick count GetTickCount() GetTickCount()
Discussion
Thank You!