Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company 8-309.

23
Optimizing Windows Optimizing Windows ® ® CE CE For Real-Time For Real-Time Systems Systems Paul Yao Paul Yao President President The Paul Yao Company The Paul Yao Company http://www.paulyao.com

Transcript of Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company 8-309.

Page 1: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Optimizing WindowsOptimizing Windows®® CE CEFor Real-Time SystemsFor Real-Time Systems

Paul YaoPaul YaoPresidentPresidentThe Paul Yao CompanyThe Paul Yao Companyhttp://www.paulyao.com

8-3098-309

Page 2: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.
Page 3: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

AgendaAgenda

Introductions (5 minutes)Introductions (5 minutes) Terms (5 minutes)Terms (5 minutes) RT-features of Windows CE RT-features of Windows CE

(15 minutes)(15 minutes) RT enhancements for Windows CE RT enhancements for Windows CE

3.0 3.0 (15 minutes)(15 minutes)

Optimization tips (20 minutes)Optimization tips (20 minutes) Discussion (15 minutes)Discussion (15 minutes)

Page 4: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

TermsTerms

Real time – A system in which specific data Real time – A system in which specific data collection or device control must be handled collection or device control must be handled within specified time parameterswithin specified time parameters 1 Millisecond (1 ms) = .001 second1 Millisecond (1 ms) = .001 second 1 Microsecond (1 ms) = .000001 second1 Microsecond (1 ms) = .000001 second

Hard real time – Catastrophic results for Hard real time – Catastrophic results for failure to meet time-critical needsfailure to meet time-critical needs

Soft real time – Non-catastrophic resultsSoft real time – Non-catastrophic results

Page 5: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

CE Kernel FeaturesCE Kernel Features

Multi-process operating system (32 max)Multi-process operating system (32 max) Multi-threadedMulti-threaded

256 thread priorities (new with Windows CE 3.0)256 thread priorities (new with Windows CE 3.0) SynchronizationSynchronization

Critical sectionsCritical sections MutexesMutexes Semaphores (new with Windows CE 3.0)Semaphores (new with Windows CE 3.0) EventsEvents

MemoryMemory PagedPaged No backing storeNo backing store

Page 6: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Interrupt HandlingInterrupt Handling

EventEvent NK.EXENK.EXEIRQIRQ

User ModeUser ModeKernel ModeKernel Mode

ISR = “InterruptISR = “InterruptService Routine”Service Routine”

IST = “InterruptIST = “InterruptService Thread”Service Thread”

AA

BB

CC

EventEvent

Page 7: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Windows CE 3.0 Windows CE 3.0 Real-Time FeaturesReal-Time Features Better interrupt handlingBetter interrupt handling

Support for nested interruptsSupport for nested interrupts Improved interrupt latenciesImproved interrupt latencies

Better control of SchedulerBetter control of Scheduler More thread prioritiesMore thread priorities Control of time-slice quantumControl of time-slice quantum Restrictions based on TrustRestrictions based on Trust Better synchronization supportBetter synchronization support Higher timer resolutionHigher timer resolution Better priority inversion handlingBetter priority inversion handling

Page 8: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Nested InterruptsNested Interrupts

Windows CE 2.xWindows CE 2.x No nestingNo nesting IRQs always run IRQs always run

to completionto completion ISTs run to ISTs run to

completion when completion when created with created with highest priorityhighest priority

Windows CE 3.0Windows CE 3.0 Nesting supportedNesting supported IRQs can be IRQs can be

interruptedinterrupted Registers saved Registers saved

and restoredand restored Interrupts only Interrupts only

occur for higher occur for higher priority IRQspriority IRQs

Page 9: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Interrupt LatencyInterrupt Latency

EventEvent NK.EXENK.EXEIRQIRQ

User ModeUser ModeKernel ModeKernel Mode

ISR = “InterruptISR = “InterruptService Routine”Service Routine”

IST = “InterruptIST = “InterruptService Thread”Service Thread”

AA

BB

EventEvent

Page 10: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Thread PrioritiesThread Priorities

Windows CE 2.xWindows CE 2.x Eight priorities – 0 to 8Eight priorities – 0 to 8 SetThreadPriority(hThread, nPriority)SetThreadPriority(hThread, nPriority)

Windows CE 3.0Windows CE 3.0 256 priorities – 0 (real-time) to 255256 priorities – 0 (real-time) to 255 CeSetThreadPriority(hThread, nPriority)CeSetThreadPriority(hThread, nPriority)

Page 11: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

New Thread PrioritiesNew Thread Priorities

Windows CE 2.x Windows CE 2.x SetThreadPriority()SetThreadPriority()

0 = real-time0 = real-time 11 22 3 = normal3 = normal 44 55 66 7 = idle-time7 = idle-time

Windows CE 3.0 Windows CE 3.0 CeSetThreadPriority()CeSetThreadPriority() 0 (RT) to 2470 (RT) to 247 248248 249249 250250 251251 252252 253253 253253 255255

Page 12: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Thread Quantum ControlThread Quantum Control

Quantum = Duration of time sliceQuantum = Duration of time slice Platform builder – Set defaultPlatform builder – Set default

During call to OEMInit()During call to OEMInit() Set dwDefaultThreadQuantumSet dwDefaultThreadQuantum

Individual threads:Individual threads: DWORD CeGetThreadQuantum();DWORD CeGetThreadQuantum(); BOOL CeSetThreadQuantumBOOL CeSetThreadQuantum

(DWORD dwTime)(DWORD dwTime)

Page 13: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Thread Quantum ExamplesThread Quantum Examples

// Set quantum to 100 ms.// Set quantum to 100 ms.

CeSetThreadQuantum(100);CeSetThreadQuantum(100);

// Set quantum to 500 ms.// Set quantum to 500 ms.

CeSetThreadQuantum(500);CeSetThreadQuantum(500);

// Set run to completion.// Set run to completion.

CeSetThreadQuantum(0);CeSetThreadQuantum(0);

Page 14: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Certifying TrustCertifying Trust

Untrusted applications:Untrusted applications: Cannot call CeSetThreadPriorityCannot call CeSetThreadPriority Cannot call CeSetThreadQuantumCannot call CeSetThreadQuantum

Platform Builder – OEMCertifyModulePlatform Builder – OEMCertifyModule Checking Trust:Checking Trust:

DWORD CeGetCurrentTrust(void);DWORD CeGetCurrentTrust(void); DWORD CeGetCallerTrust(void);DWORD CeGetCallerTrust(void); 0 = None, 1 = Some, 2 = All0 = None, 1 = Some, 2 = All

Page 15: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Synchronization Synchronization EnhancementsEnhancements TryEnterCriticalSection() – TryEnterCriticalSection() –

Non-blocking callNon-blocking call SemaphoresSemaphores

A “mutex with a count”A “mutex with a count”

Page 16: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

TimersTimers

Sleep(int nMilliseconds) Sleep(int nMilliseconds) 1 ms granularity on CE 3.01 ms granularity on CE 3.0

Sleep(100); // block for 100 ms.Sleep(100); // block for 100 ms.

Sleep(5); // block for 5 ms.Sleep(5); // block for 5 ms.

Sleep(0); // yield quantum.Sleep(0); // yield quantum.

Avoid SetTimer()Avoid SetTimer() Not RTNot RT Message-based, UI-oriented timerMessage-based, UI-oriented timer

Page 17: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Priority InversionPriority InversionMutexMutex

AA

BB

CC

PriorityPriority

TimeTime

Blocks on Blocks on MutexMutex

Acquires Acquires MutexMutex

Acquires Acquires MutexMutex Frees Frees

MutexMutex

Page 18: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Optimizing TipsOptimizing Tips

Avoid priority inversionAvoid priority inversion Avoid file I/O from RT threadsAvoid file I/O from RT threads Avoid graphic calls from RT threadsAvoid graphic calls from RT threads Avoid UI calls from RT threadsAvoid UI calls from RT threads

MemoryMemory Pre-allocate heap memoryPre-allocate heap memory Pre-commit stack memoryPre-commit stack memory

To Control PagingTo Control Paging Use LoadDriver() - not LoadLibrary()Use LoadDriver() - not LoadLibrary() For individual pages - LockPages()For individual pages - LockPages()

Page 19: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Measurement ToolsMeasurement Tools

ILTIMING – Check Interrupt LatencyILTIMING – Check Interrupt Latency \wince300\public\common\oak\\wince300\public\common\oak\

utils\iltimingutils\iltiming OSBENCH – Thread schedulingOSBENCH – Thread scheduling

\wince300\public\common\oak\\wince300\public\common\oak\utils\osbenchutils\osbench

White paper:White paper: http://www.microsoft.com/windows/http://www.microsoft.com/windows/

embedded/ce/resources/developingembedded/ce/resources/developing

Page 20: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Measurement APIsMeasurement APIsQueryPerformanceFrequency()QueryPerformanceFrequency()

QueryPerformanceFrequency()QueryPerformanceFrequency() Returns Ticks per SecondReturns Ticks per Second

QueryPerformanceCounter()QueryPerformanceCounter() Returns Current tick countReturns Current tick count

GetTickCount()GetTickCount()

Page 21: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

DiscussionDiscussion

Page 22: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.

Thank You!Thank You!

Page 23: Optimizing Windows ® CE For Real-Time Systems Paul Yao President The Paul Yao Company   8-309.