Click here to learn
about this Sponsor:
Home  |  News  |  Articles  |  Polls  |  Forum  |  Directory

Keywords: Match:
Understanding the Windows CE variable tick timer
by Sha Viswanathan (Oct. 26, 2006)

Foreword: In this technical whitepaper, Windows Mobile BSP (board support package) developer Sha Viswanathan explains the differences between Windows CE's two modes of timer-tick interrupt management, and the implications of those differences.



Understanding the Windows CE variable tick timer

by Sha Viswanathan


The timer provides the "heartbeat" for every Windows CE system. On each timer interrupt, the kernel analyzes threads in the sleep- and run-queues to decide which thread will run next. Although beyond the scope of this article, learning the system timer is a good step to understanding common CE themes such as real-time operation, the behavior of sleeping threads and the effect of priority, and the power saving potential of OEMIdle().

Overview

Execution in a Windows CE system is interrupt-driven; a key press, call notification from the radio, or insertion of an SD card all cause interrupts that trigger specific code to execute. The kernel is no different; it maintains control of the system by tying itself to a hardware interrupt. For a particular chipset to support Windows CE, it must have a dedicated timer interrupt as part of the OEM Adaptation Layer (OAL).

Typically, interrupts in a system trigger a notification, or "SYSINTR" event to the kernel. If a driver has requested a SYSINTR mapping for a physical interrupt line such as a keypad, its associated thread will be woken up and scheduled every time the SYSINTR event occurs. Timer interrupts are unique because they always return the dedicated SYSINTR_RESCHED. Instead of a waking up a thread, SYSINTR_RESCHED is a "heads up" telling the system that the scheduler needs to be run. I won't go into detail here, but a lot can occur after a reschedule: if no threads exist, or all are sleeping, we will enter idle. The scheduler can also choose to change the thread context if a higher priority thread has entered the run-queue, etc.

Generally, timer hardware consists of at least two registers. One is a free-running counter, which increments or decrements by one at a pre-determined frequency. The second is a match register, which is programmable. When the free-running counter equals the match register, an interrupt is generated. This paradigm is known as "count-compare." Another paradigm consists of just one register, which can be programmed at any time, and will decrement until it hits 0, at which point an interrupt is generated ("count-down").

Variable vs. fixed tick

Our goal then, when implementing a timer, is to manage the flow of interrupts to the system. Two common timer implementations exist in Microsoft-produced OALs. The first is called the "fixed tick" timer. As the name implies, we always set the next timer interrupt to occur one millisecond into the future. When the interrupt occurs, we check to see if the current time has passed dwReschedTime (a kernel global variable), and if so we return SYSINTR_RESCHED, otherwise, SYSINTR_NOP (false interrupt). So, any running thread will be quickly interrupted each millisecond so the kernel can check to see if the scheduler needs to run.

Variable tick works differently. Instead of setting the next timer interrupt to occur in one millisecond, we expose a function to the kernel to set the next interrupt. This ensures that no unnecessary timer interrupts are taken. This is the major advantage of using a variable tick timer -- the kernel calculates and sets the "earliest required wakeup time" instead of "polling" every millisecond. Here are just a few scenarios that describe when the next timer interrupt will occur:
  • One quantum (100 ms) when there is only one active thread in the system.
  • If a low priority thread is about to run because a higher-priority thread is calling Sleep(50), the next interrupt will occur in 50 ms.
  • If no threads are running, the next interrupt will be set for the maximum time allowable by hardware (The system has nothing to do but wait for external interrupts, like a keypress).
Timers in idle

When all threads are sleeping or inactive, OEMIdle is called to sleep the core. In a fixed tick system, OEMIdle should reset the next timer interrupt to around dwReschedTime (a global timer variable) to save power; otherwise, we will wake up every millisecond unnecessarily (we’ll see this happen later on CEPC, which is wall-powered). Not being able to save power with fixed tick is a common misconception. Again, a platform that implements the fixed tick timer and wants idle power savings should simply reprogram the timer interrupt. Another way to think about it is that, regardless of timer, idle is always "variable tick."

With a variable tick timer, the timer interrupt will already be set to dwReschedTime by the kernel, before OEMIdle is called. Aside from this difference, behavior in OEMIdle is very much platform specific, and can be completely independent of the timer. It turns out that even variable-tick platforms may still have to reprogram the timer interrupt. This is because some low power modes require many milliseconds of recovery time. In that case we end up having to "roll back" the next timer interrupt by roughly [dwReschedTime – wakeupTime].

Timers in action

Let's compare the basic operation of a fixed tick timer with a variable tick timer. If we build a TinyKernel image with IMGPROFILER=1, and run CELog/KernelTracker, we can look at all physical interrupts that fired in the system. The simplest case: what happens when only one thread is running in the system? I ran this simple test program with both types of timers to point out the differences (spin for 200ms, then sleep):




(Click image for larger view)

In the Kernel Tracker output above, the "i" symbols represent interrupts. Interrupt 1 is the timer interrupt, and Interrupt 16 is KITL (dumping the CELog data to my desktop), which we will ignore.

In the first Kernel Tracker picture, I started vartest.exe at about the "250" mark. The kernel was idling previously, so vartest.exe was the only thread in the system, aside from the occasional KITL interrupt. After I launch vartest.exe, we see a timer interrupt come in, and afterwards, a process switch from nk.exe (OEMIdle) to vartest.exe. Because no other threads were found in the run/sleep queues, the kernel set the next reschedule time to 100ms in the future. This is (you guessed it!) the variable tick timer.

The second Kernel Tracker picture is the fixed tick timer. This looks almost exactly the same as the variable tick timer, but with one key difference: Interrupt 0 is very active. Interrupt 0 corresponds to SYSINTR_NOP, which is returned when the timer interrupt fired, but the current time didn’t actually exceed dwReschedTime. While our thread was running its 100ms quantum, we effectively ignored 99 timer interrupts. This is CEPC; notice that its implementation of OEMIdle does not bother to reprogram or stifle timer interrupts, because it is wall powered.

In summary, the major difference between these two timers is that fixed tick timers "poll", while variable tick timers are truly interrupt-driven. The major advantage to using a variable tick timer is that you can save cycles. To find out if this could result in savings on your platform, measure the time spent handling one "fake" interrupt: the time it takes to execute your ISR and return SYSINTR_NOP, plus some kernel overhead, and potentially hardware-related overhead as it vectors into the ISR. This is the amount of execution time you can save per millisecond.

Which timer should you choose? The trade-off is difficulty of implementation vs. avoiding unnecessary interrupts. On the Mainstone platform, I measured the amount of time spent executing the ISR, as a conservative estimate of what the variable tick timer can save. In the "one spinning thread" example, we took 99 unnecessary interrupts in one quantum. For each, the Mainstone ISR took 25 microseconds to return SYSINTR_NOP. This corresponds to about a 2.5 percent loss in performance. Hold on though! The "one spinning thread" example is actually an extreme case. When the scheduler is interacting with hundreds of threads, it is not always the case that 99 out of 100 timer interrupts are "wasted." Real scheduler behavior is so complex that your real savings with variable tick is, well....variable. Think of 2.5 percent as "the ballpark," if not less. To accurately measure the potential gains of implementing variable tick on your system, I recommend timing one SYSINTR_NOP as I have done, and count them while using your device.

Currently, most platforms still use a fixed-tick timer. CEPC was used to gather data for the fixed-tick timer, and the Mainstone platform was used for variable-tick. FSample is a good fixed-tick implementation that still does variable idling. To learn more about the variable tick timer, or if you write one yourself, I would suggest referencing the PXA27X (Bulverde) vartick timer code. Core timer handling is done in OALTimerInit, OALTimerIntrHandler, and OALTimerUpdateRescheduleTime.


Copyright (c) 2006 Microsoft Corp. All rights reserved. Reproduced by WindowsForDevices.com with permission. This article was originally published on the Windows CE Base Team Blog, here.



About the author: Sha Viswanathan is a developer on the Windows Mobile BSP team who came to Microsoft after graduating from the University of Texas in 2005. He's worked mostly on Intel-based BSPs and is now working on future BSPs. When not writing BSPs, Viswanathan says he likes getting out in the beautiful Pacific Northwest, enjoying friends, the many microbrews, mountains, parks, and islands... "except in the winter, where I spend my time wondering where the sun went."



Related stories:

(Click here for further information)


Windows XP for Embedded Applications
This white paper describes the benefits of using Windows XP when developing embedded applications.

A Manager's Guide to Selecting a Mobile Device Operating System
This white paper offers a comparative review of Microsoft Windows CE and Windows Mobile.

Visual Basic 6.0 to .NET Migration
This paper focuses on the methodology and techniques which Infosys (Microsoft Technology Center) has developed for migrating VB 6.0 Applications to .NET. Our approach ensures a smooth, cost effective, and efficient migration.

Mobile Device Security: Securing the Handheld, Securing the Enterprise
This whitepaper identifies security threats to corporate data on mobile devices and details how mobile devices can become a "backdoor" to the enterprise.

Mobile Device Security: The Eight Areas of Risk
It's common knowledge that adding mobile devices to your network increases security risks. There are multiple facets to mobile security, all of which should be paid close attention to. This E-Guide presents a more in depth look into the eight key areas of securing wireless devices.

Quality Assurance and .NET
This paper discusses best practices for functional, regression and load testing of .NET applications.

SCADA Security in Integrated Networks
As businesses leverage their SCADA systems by integrating them into the business networks, they must also assure the security of the SCADA system.

The Advantages of Small Form Factor HMI
HMIs have mutated and changed with new requirements, and they have become more flexible and capable. And while they've been doing that, they've become smaller and more useful.

9 Critical Requirements for Web Application Security
Learn why your Web applications expose dangerous security breaches and what’s required to effectively protect your Web applications and the sensitive information behind them.

 


Got a HOT tip?   please tell us!
Free weekly newsletter
Enter your email...
Click here for a profile of each sponsor:
PLATINUM SPONSORS
(Become a sponsor)

ADVERTISEMENT
(Advertise here)

Updated! The latest Windows-powered...

mobile phones!

other cool
gadgets

HOT TOPICS
Microsoft targets PNDs with new embedded OS
Microsoft tips .NET MF 3.0 highlights
Microsoft previews Windows Embedded Standard
Microsoft offers free Windows CE 6.0 textbook
Microsoft renames embedded operating systems
Microsoft unveils Windows Mobile 6.1
New Atom models target low-cost PCs
REFERENCE GUIDES
Windows Device Showcase
Intro to Windows Embedded
Intro to Shared Source
Real-time Windows Embedded
Windows Embedded books
Join our Windows Embedded discussion forums:
Windows XP Embedded
Windows CE
Windows Mobile


Windows Embedded developer newsgroups
Windows CE
XP Embedded
PocketPC
Smartphone

Microsoft's Windows Embedded resources
Embedded dev center
Mobile dev center
Windows CE tutorials
XP Embedded tutorials
Windows Embedded seminars
Windows Embedded application categories
3rd-party partners


BREAKING NEWS

• Cortex-A8 SBCs target signage and kiosks
• Student competition offers a different kind of fireworks
• Windows CE SBC targets HMI development
• All-you-can-eat carrier launches -- sort of
• Adeneo and IntervalZero promote "soft-control" architecture
• App creates virtual serial ports in Windows CE
• Windows satellite phone service takes off (literally)
• Bsquare and Microsoft renew key distribution agreement
• Software syncs HTC phones with Macs
• Electricity meter runs Windows, talks to Google
• Windows device is two phones in one
• MIcrosoft's online store to debut with 600 Windows Mobile apps
• Windows Mobile gets upgraded Skype VoIP client
• Panel PC targets outdoor use
• Samsung shrinks ARM11 application processor


MOST POPULAR (last 90 days)
• "Netbook" uses Intel's Atom N270
• Windows CE takes on Linux in low-end netbooks
• HTC ups Touch resolution
• Microsoft unleashes new embedded OS
• Windows Mobile phone gets 800 x 480 display
• HTC spins WiMAX phone?
• Smart camera sports Atom
• Dual-core AMD netbook gets rave review
• Windows Mobile 7 "delayed"
• GPS phone uses new Marvell "Tavor" chip
MOST POPULAR (Classics from the vault)
Windows XP Embedded USB boot
Troubleshooting Windows XPe's blue screen "Stop 0x0000007B" error
Asus reveals $190 mini notebook
Windows Mobile 6 SDKs available for download
Windows Mobile VPN client plays with Cisco
HTC adds GPS to Windows Mobile Touch line
Microsoft unveils Windows Mobile 6.1
Guide to HTC's Windows Mobile smartphone platforms
• HTC releases Touch Diamond ROM upgrade
Customizing Windows XP Embedded thin clients

Also visit our sister sites:


Sign up for WindowsForDevices.com's...

news feed

Or, follow us on Twitter...



Home  |  News  |  Articles  |  Polls  |  Forum  |  Directory  |  About  |  Contact
 

Ziff Davis Enterprise Home | Contact Us | Advertise | Link to Us | Reprints | Magazine Subscriptions | Newsletters
Tech RSS Feeds | White Papers | ROI Calculators | Tech Podcasts | Tech Video | VARs | Channel News

Baseline | Careers | Channel Insider | CIO Insight | DesktopLinux | DeviceForge | DevSource | eSeminars |
eWEEK | Enterprise Network Security | LinuxDevices | Linux Watch | Microsoft Watch | Mid-market | Networking | PDF Zone |
Publish | Security IT Hub | Strategic Partner | Web Buyer's Guide | Windows for Devices

Developer Shed | Dev Shed | ASP Free | Dev Articles | Dev Hardware | SEO Chat | Tutorialized | Scripts |
Code Walkers | Web Hosters | Dev Mechanic | Dev Archives | igrep

Use of this site is governed by our Terms of Service and Privacy Policy. Except where otherwise specified, the contents of this site are copyright © 1999-2008 Ziff Davis Enterprise Holdings Inc. All Rights Reserved. Reproduction in whole or in part in any form or medium without express written permission of Ziff Davis Enterprise is prohibited. Windows is a trademark or registered trademark of Microsoft Corporation in the United States and/or other countries and is used by WindowsForDevices under license from owner. All other marks are the property of their respective owners. WindowsForDevices is an independent publication not affiliated with Microsoft Corporation.