Real-Time Behavior of The
Real-Time Behavior of The
Contents
Introduction............................................................................................................. ..3
A Managed and an Unmanaged World.............................................. ......................3
Platform Invoke at Work................................................................... ....................3
A Real-time Scenario...................................................................... .........................6
The Actual Test................................................................................................ .........7
The Results..................................................................................... .......................12
Pitfalls.......................................................................................... .......................17
Proof of Results................................................................................................... 18
Conclusion......................................................................................................... .....20
Acknowledgments............................................................................ ...................20
About the Authors........................................................................................... .....21
Call to Action and Resources.......................................................... .......................21
Acronyms and Terms....................................................................................... .......21
Real-time Behavior of the Microsoft .NET Compact Framework - 2
WinHEC Sponsors’ Disclaimer: The contents of this document have not been authored or confirmed by
Microsoft or the WinHEC conference co-sponsors (hereinafter “WinHEC Sponsors”). Accordingly, the
information contained in this document does not necessarily represent the views of the WinHEC Sponsors
and the WinHEC Sponsors cannot make any representation concerning its accuracy. THE WinHEC
SPONSORS MAKE NO WARRANTIES, EXPRESS OR IMPLIED, WITH RESPECT TO THIS
INFORMATION.
Microsoft, Windows, Windows NT, Visual Basic, Visual C#, Visual C++, Visual Studio, and Win32 are
trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective owners.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 3
Introduction
With the arrival of Microsoft Visual Studio® .NET 2003 with integrated support for
Smart Device Applications it is possible to develop applications for a broad range
of devices using managed code. Software developers can now use new exciting
languages like Microsoft Visual Basic® .NET and Microsoft Visual C#® for device
development. Although this sounds promising one question is still to be answered.
Is it possible to make use of the real time capabilities of Windows CE .NET while
using managed code to write applications for an embedded device? In this paper
we will answer that question and we suggest a possible scenario in which real-time
behavior can be combined with .NET functionality.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 4
namespace CFinRT
{
public class WCEThreadIntf
{
[DllImport("RTCF.dll")]
public static extern bool Init();
[DllImport("RTCF.dll")]
public static extern bool DeInit();
[DllImport("RTCF.Dll")]
public static extern uint GetTimingInfo(
ref uint perfAvg,
ref uint perfMax,
ref uint perfMin,
ref uint perfTickMax,
ref uint perfTickMin);
}
}
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 5
maxSleepTime = (uint)(float)((maxSleepTime *
scaleValue) / 1.19318);
minSleepTime = (uint)(float)((minSleepTime *
scaleValue) / 1.19318);
}
StoreValue();
counter = (counter + 1) % samplesInMinute;
}
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 6
return 0;
}
// GetTimingInfo prototype
#ifdef RTCF_EXPORTS
#define RTCF_API __declspec(dllexport)
#else
#define RTCF_API __declspec(dllimport)
#endif
extern "C"
{
RTCF_API BOOL Init();
RTCF_API BOOL DeInit();
RTCF_API DWORD GetTimingInfo(LPDWORD lpdwAvgPerfTicks,
LPDWORD lpdwMax,
LPDWORD lpdwMin,
LPDWORD lpdwDeltaMax,
LPDWORD lpdwDeltaMin);
}
A Real-time Scenario
Imagine the following scenario: A system needs hard real-time functionality to
retrieve information from an external source. The information is stored in the
system and will be presented to the user in some graphical way. Figure 1 shows a
possible scenario for this problem.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 7
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 8
&dwIRQ,
sizeof(DWORD),
&g_dwSysIntr,
sizeof(DWORD),
NULL))
{
// create an event that will activate our IST
g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (g_hEvent)
{
// Connect the interrupt to our event and
// create our Interrupt Service Thread.
// The actual IST is shown in listing 4
InterruptDisable(g_dwSysIntr);
if (InterruptInitialize(g_dwSysIntr,
g_hEvent, NULL, 0))
{
g_bFinish = FALSE;
g_hThread = CreateThread(NULL,
0,
IST,
NULL,
0,
NULL);
if (g_hThread)
{
bRet = TRUE;
}
else
{
InterruptDisable(g_dwSysIntr);
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 9
CloseHandle(g_hEvent);
g_hEvent = NULL;
}
}
}
}
return bRet;
}
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 10
DLL and the .NET CF GUI a double buffering mechanism is used in combination
with P/Invoke. The GUI requests new timing information on regular intervals,
making use of a System.Threading.Timer object. The DLL decides when it has
time available to pass information to the GUI. Until data is ready, the GUI is
blocked. The refresh rate of the information presented in the GUI is user selectable.
For our test we used a refresh rate of 50 msec.
The following pseudocode explains the operation of the IST and the mechanism by
which the GUI retrieves information, stored in the native Win32 DLL.
During the test we hooked up an oscilloscope and made printouts of both the scope
and the Windows CE graphical display 10 minutes into the experiment. In figure 3
the interrupt latency, measured with an oscilloscope is displayed. Best case, the
latency is 14.0 µ sec., worst case the latency is 54.4 µ sec, meaning a jitter of 40.4
µ sec. In figure 4 the periodic time is displayed when the IST is activated. This
figure is a screen shot of the actual user interface. Ideally the IST should run every
100 µ sec, which is also the average time during our measurement (the blue line in
the middle). We also measured overall minimum (green) and maximum (red)
times, as well as minimum and maximum times over the sample period of 50 msec
(the white block). The deviation we found during the test period is limited to ± 40 µ
sec.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 11
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 12
The Results
We measured over a longer period of time to make sure that both the Garbage
Collector and the JIT compiler were frequently active. Thanks to the folks at
Microsoft, we were able to monitor the behavior of the .NET CF because they
provided us with a performance counters registry key. Using this key, a number of
performance counters within the .NET CF are activated. We mainly used this
performance information to verify that JITter and Garbage Collector actually ran. It
also gave a nice indication about the number of objects used during the cause of
the test.
// Our periodic timer method in which we want to collect new
// data and refresh the screen
private void OnTimer(object source)
{
// Temporarily stop the timer, to prevent against
// a whole bunch of OnTimer calls to be invoked
if (theTimer != null)
{
theTimer.Change(Timeout.Infinite, dp.Interval);
}
Pen blackPen = new Pen(Color.Black);
Pen yellowPen = new Pen(Color.Yellow);
Graphics gfx = CreateGraphics();
dp.CollectValue();
td.SetTimePointer(dp.CurrentSample, gfx, yellowPen);
gfx.Dispose();
yellowPen.Dispose();
blackPen.Dispose();
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 13
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 14
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 15
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 16
Table 1: .NET CF performance results after running the test for ten minutes
Counter Value n Mean min max
Execution Engine Startup Time 492 0 0 0 0
Bytes Pitched 0 0 0 0 0
Number of Exceptions 0 0 0 0 0
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 17
Table 2: .NET CF performance results after running the test for hundred minutes
Counter Value n mean min max
Execution Engine Startup Time 478 0 0 0 0
Number of Exceptions 0 0 0 0 0
Pitfalls
During the test, increasing the frequency of the block wave led to unexpected
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 18
results in the managed application. Especially in the situation where the screen
needed frequent repaints (because areas of the screen were invalid), the
application randomly hung up the system. Investigation of this problem showed
unexpected behavior for experienced Win32 programmers. In a Win32 application,
using a timer results in a WM_TIMER message each time a timer expires. However,
in the message queue WM_TIMER messages are low priority messages, only posted
when there are no other higher priority messages to be processed. This behavior
can possibly lead to missing timer ticks, but since CreateTimer does not give us
an accurate timer to begin with. This is no problem, especially if the timer is used to
update a graphical user interface. However, in the managed application, we use a
System.Threading.Timer object to create a timer. This timer calls a delegate
every time the timer expires. The delegate is called from within a separate thread
that exists in a thread pool. If the system is too busy with other activities, like
repainting an entire screen, more timer delegates, each in separate threads, are
activated before previously activated delegates are finished. This might lead to
consuming all available threads from the thread pool, causing the system to hang.
The solution to prevent this behavior is found in listing 3. Each time a timer
delegate is activated, we stop the timer object by invoking the Change method of
the Timer object, to indicate that we do not want the next timer message until we
have processed the current one.
Proof of Results
To be able to compare the results of our experiment with typical results in the same
setting, we also wrote a Win32 application that invoked the same DLL with real-
time functionality. The Win32 application is functionally identical to the managed
application. It provides the system with a graphical user interface in which timing
information is displayed in a window. This application paints timing results upon
reception of WM_TIMER messages, solely making use of Win32 APIs. The update
rate of the screen for both applications is user selectable, but for both applications
we chose an update rate of 50 milliseconds. Basically we did not find any
difference in performance, as figures 6 and 7 show. In figure 6 the interrupt latency
is again measured with an oscilloscope. For the Win32 application, the latency is
14.4 µ sec. Worst case the latency is 55.2 µ sec, meaning a jitter of 40.8 µ sec.
These results are identical to the test run with a .NET CF managed application. In
figure 7 the periodic time is displayed when the IST is activated, again for the
Win32 application. Again, the results are identical to the results of a .NET CF
managed application. The source for the Win32 application is also downloadable so
you can compare the behavior of the two different applications yourself.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 19
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 20
Conclusion
First we need to make absolutely sure that you understand that we are not
suggesting the .NET CF for any real-time work by itself. We suggest that it can be
used advantage as a presentation layer. In such a system, the .NET CF can
"peacefully co-exist" with real-time functionality, not affecting the real time behavior
of Windows CE .NET. In this article we have not benchmarked the graphics
capabilities of the .NET CF. In our situation we did not find any significant
difference in an application, written entirely in Win32 or an application, partly
written in a managed environment with Visual C#. Given the higher programmer
productivity and the richness of the .NET Compact Framework, there are many
advantages in writing presentation layers in managed code and writing hard real-
time functionality in unmanaged code. The clear distinction between these different
types of functionality is something you will get for free, using this approach.
Acknowledgments
We have been thinking quite a while about testing the usability of the .NET
Compact Framework in real-time scenarios. This test was only possible by
cooperating with people and companies that could provide us with the proper
hardware and measuring equipment. Therefore we like to thank Willem Haring of
Getronics for his support, ideas and hospitality during this project. We also like to
thank the folks at Delem for their hospitality and for providing us with the necessary
equipment to execute our tests.
WinHEC 2003
Microsoft Windows Hardware Engineering Conference
Real-time Behavior of the Microsoft .NET Compact Framework - 21
WinHEC 2003
Microsoft Windows Hardware Engineering Conference