Windows Embedded CE 6.0 Debugging/Profiling

Objectives

After completing this lab, you will be better able to:

  • Create a Platform Image
  • Customize and build the OS Design
  • Download the OS Design
  • Use Remote Tools & Memory Leaks
  • Use Windows CE Remote Tools
Estimated time to complete: 60 min

Computers used in this Lab:
WindowsCE6





Exercise 1 - Creating a Platform Image

Scenario
You will be using two tools in this hands on lab, Visual Studio 2005 (including Windows Embedded CE 6.0 Platform Builder), and a Code Snippet tool called "Code Clip" which will have been automatically started for you.
You can see the Code Clip toolbar at the top of your desktop display.



Figure 1.
Single clicking on the Code Clip toolbar will display a list of available labs and code snippets, double clicking any of the code snippets (for example PostLink) will copy the contents of the code snippet to the clipboard. Once the code snippet has been copied to the clipboard you can single click the Code Clip toolbar to hide the list of labs and code snippets. The code that's been placed on the clipboard can then be pasted into Visual Studio 2005.



Figure 2.
During the first part of this lab you will use the New OS Design Wizard within Platform Builder to create an initial OS Design which you will then modify by adding applications, and modifying the registry.



Task 1: Create a new OS Design

Complete this Task on:
 WindowsCE6


a. Start Visual Studio 2005 with Platform Builder either using the desktop shortcut, or…

b. Start | All Programs | Microsoft Visual Studio 2005 | Microsoft Visual Studio 2005 (there may also be a shortcut on the desktop)

c. Select File | New… | Project…


Figure 3.


d. Select the Platform Builder for CE 6.0 project type.

e. Choose the OS Design template

f. Enter a name for your project, or go with the default name

g. Click OK

Note: The initial Dialog that is displayed below outlines the process of creating an OS Design. There are a number of steps involved and you will step through the wizard and make selections as appropriate.


Figure 4.


Note: We will build an operating system for the Microsoft Device Emulator. The Microsoft Device Emulator version 2.0 is a desktop application that emulates the behavior of a CE based or Windows Mobile-based hardware platform. Using the Device Emulator, you can run, test, and debug a run-time image without the need for a physical device.

Note: You could select more than one BSP (Board Support Package). For example, it may be useful to select the Device Emulator and a hardware reference platform; the emulation environment can be used by your internal/external application developers to build applications for your device while hardware is still being developed.


Figure 5.


h. Select Device Emulator:ARMV4I

i. Click Next

Note: You are now given a number of Design Templates to choose from as the starting point for this project. If none of the options match your needs you can simply select 'Custom Device' and build the image based on the components you select from the catalog.

Note: For the purposes of this tutorial you will be using the PDA Device template.


Figure 6.


j. Select PDA Device from the list of Design Templates

k. Click Next

Note: The Wizard now gives you the option of choosing a variant of the Design Template. For this tutorial you will be using the Mobile Handheld variant.


Figure 7.


l. Select Mobile Handheld from the list of Design Template Variants

m. Click Next

Note: There are a number of options which can be selected for each of the sample platforms, the OS Design Wizard will only show options that are relevant to the platform you are building. For example, it would not make sense to include Internet Explorer or WordPad in a headless device such as a Gateway. The Mobile Handheld device can include applications such as Internet Explorer, WordPad, and Windows Messenger.

Note: In this case we don't need the .NET Compact Framework. De-select this item.


Figure 8.


n. De-select the .NET Compact Framework

o. Click Next
Note: Next comes networking and communications, you can see that Windows CE provides support for Personal, local, and wide area networking through technologies such as Bluetooth, IrDA, Wired and Wireless networking, and VPN.




Figure 9.


p. Click Next
Note: We're done! – The wizard is complete. You've configured the OS Design and can now further customize it by adding and/or removing components from the Catalog.




Figure 10.


q. Click Finish
Note: The Wizard will now prompt you with any security warnings or other notifications related to the components you selected. Acknowledge these notifications.




Figure 11.


r. Click Acknowledge

Note: At this point you have an OS Design containing all of the OS components which have been selected from the Wizard. In the next section we will further customize the OS Design using the Catalog.





Exercise 2 - Customize and build the OS Design

Task 1: Using the Catalog

Complete this Task on:
 WindowsCE6


Note: You will now make changes to the items included in your OS Design using the Catalog View.

Note: First, ensure that the Catalog View is visible.

a. Select the View menu

b. Choose Other Windows | Catalog Items View

Note: To use the Kernel Tracker (remote tool) you will need to make sure that you have included Structured Exception Handling (SHE) and RTTI in your run-time image that you build. Search for "RTTI" to locate the appropriate catalog item.

c. Type RTTI in the search box (top of the catalog window)

d. Hit Enter to search

e. Check the box next to the C++ Runtime Support for exception handling and Runtime Type Information item to include it in your OS Design


Figure 12.


Note: For this lab you don't need networking included in your operating system design, you can easily remove networking components from your operating system design.

f. Type SYSGEN_ETHERNET in the search box

g. Hit Enter to search

h. Remove the Check next to Wired Local Area Networking and Wireless LAN to remove these features from your operating system design.


Figure 13.




Task 2: Enabling Profiling Kernel and Event Tracking.

Complete this Task on:
 WindowsCE6


Note: You're now about ready to build the platform. In the next section we will be using the Kernel Tracker. The Kernel Tracker tool requires that we have enabled the Profiling Kernel.

Note: You enable the Profiling Kernel by selecting the following options:

a. Select the Solutions view

b. Right Click on OSDesign1 (or the name of your platform)

c. Select Project | Properties

d. Expand Configuration Properties

e. Select the Build Options item


Figure 14.


Note: You will notice that the tools are currently set to build a Debug image of the platform. You have "Kernel Debugging" enabled, but in order to use the Kernel Tracker tool you also need to enable the Profiling Kernel and Event Tracking.

f. Select Enable Profiling

g. Select Enable Event Tracking During Boot

h. Click OK



Task 3: Adding Applications to the Platform.

Complete this Task on:
 WindowsCE6


Note: We have two applications to add to our platform, these are memLeak and Note: Philosophers – The source for these applications has already been written and is stored in the Code Clip application – Let's now create these applications and add them to the platform workspace.

Note: MemLeak
We're now ready to add our first application to the platform; this application will be a Windows application with UI. The memLeak will be used to show Debug Zones to display focused debug information from an application or driver, Remote Performance Monitor to monitor memory load within the operating system, and to also show how CELOGDATA can be used to output custom data items into the Kernel Tracker data stream. Memleak is a multi-threaded application which was created using the Platform Builder ‘Application Wizard' – we will examine the code used in this application later in the lab.

a. In the Solution Explorer, right click on the Subprojects folder.

b. Select Add New Subproject…
Note: The Subproject Wizard is displayed and shows a number of different subproject types. Each of the available types is described in the Platform Builder online help.




Figure 15.


c. Choose WCE Application

d. Type the name of your application – MemLeak

e. Click Next
Note: You will be prompted for the type of application you want to add. There are three types of application. An empty project assumes that you will add all the source files needed for the project. A "Simple" Windows CE application will create the core source files needed to build the project and will have an entry point of WinMain. Finally, a typical "Hello World" application will output the string "Hello World" to the screen when executed.




Figure 16.


f. Select A Simple Windows Embedded CE application

g. Click Finish

Note: At this point you have application source written for you that simply exposes the entry point for the application, you are now responsible for writing the rest of the application code.

h. Under Subprojects Expand MemLeak

i. Expand Source Files

j. Double Click on MemLeak.cpp

Note: You will now replace the existing 'skeleton' code created by the application wizard.

k. Select all of the code in the MemLeak application (use CTRL+A or the mouse)

l. Delete all of the MemLeak source code.

m. Go to Code Clip at the top right of your monitor

n. Click on Code Clip – this will display the Code Clip interface

o. Expand the Platform Builder Advanced Lab

p. Select MemLeak

q. Either Click Copy or Double Click MemLeak – this will copy the MemLeak source to the clipboard

r. Click Code Clip to hide the code clip interface

s. In Visual Studio select Edit | Paste, or Right Click | Paste

Note: Philosophers
We're now ready to add our second application to the platform; In this example we will be using the Dining Philosophers problem which is a classic multi-process synchronization problem. The problem consists of five philosophers sitting at a table who do nothing but think and eat. Between each philosopher, there is a single stick. In order to eat, a philosopher must have both sticks. A problem can arise if each philosopher grabs the stick on the right, then waits for the stick on the left. In this case a deadlock has occurred, and all philosophers will starve. Also, the philosophers should be fair and not hold onto the chopsticks for too long, therefore each philosopher should be able to eat as much as the rest.

t. In the Solution Explorer, right click on the Subprojects folder.

u. Select Add New Subproject…
Note: The Subproject Wizard is displayed and shows a number of different subproject types. Each of the available types is described in the Platform Builder online help




Figure 17.


v. Choose WCE Application

w. Type the name of your application – Philo

x. Click Next
Note: You will be prompted for the type of application you want to add. There are three types of application. An empty project assumes that you will add all the source files needed for the project. A "Simple" Windows CE application will create the core source files needed to build the project and will have an entry point of WinMain. Finally, a typical "Hello World" application will output the string "Hello World" to the screen when executed.




Figure 18.


y. Select A Simple Windows Embedded CE application

z. Click Finish

Note: At this point you have application source written for you that simply exposes the entry point for the application, you are now responsible for writing the rest of the application code.

aa. Under Subprojects Expand Philo

bb. Expand Source Files

cc. Double Click on Philo.cpp

Note: You will now replace the existing 'skeleton' code created by the application wizard.

dd. Select all of the code in the Philo application (use CTRL+A or the mouse)

ee. Delete all of the Philo source code.

ff. Go to Code Clip at the top right of your monitor

gg. Click on Code Clip – this will display the Code Clip interface

hh. Expand the Platform Builder Advanced Lab

ii. Select Philosophers

jj. Either Click Copy or Double Click Philophers – this will copy the Philo source to the clipboard

kk. Click Code Clip to hide the code clip interface

ll. In Visual Studio select Edit | Paste, or Right Click | Paste

Note: We're going to run the Remote Call Profiler against the Philosophers application, in order to do this we need to instrument the application.

mm. In Solution View, right click on the Philo Project(the top most node of the Philo application)

nn. Select Properties
Note: This will display the Project Settings dialog for the Philosophers application.


Figure 19.


Note: We now have all of the components we need to build and deploy our operating system.



oo. Select and open Custom Variables


Figure 20.


pp. Select New

qq. Enter WINCECALLCAP for the Variable Name

rr. Enter 1 for the Variable value


Figure 21.


ss. Click OK

Note: We also need to enable the generation of a .MAP file for this project.

tt. On the Custom Variables Dialog, click New

uu. Enter WINCEMAP for the variable name

vv. Enter 1 for the Variable value


Figure 22.


ww. Click OK

xx. Click OK



Task 4: Build the Platform

Complete this Task on:
 WindowsCE6


Note: You're now ready to build the platform.

a. Select Build | Build Solution

Note: The build process will take approximately 10 minutes to complete.





Exercise 3 - Download ths OS Design

Task 1: Configure download/debug transport

Complete this Task on:
 WindowsCE6


Note: You are now ready to download and debug the Windows CE operating system image – before you download let's check to make sure the debug and kernel transports are configured.

a. Select Target | Connectivity Options
Note: The following dialog will be displayed




Figure 23.


b. Confirm the following settings:
  • Target Device: CE Device
  • Download: Device Emulator (DMA)
  • Transport: Device Emulator (DMA)
  • Debugger: KdStub


c. Click Apply

d. Click Close to close the Connectivity Settings dialog

Note: DMA Transport allows an OS Image to be downloaded to the Device Emulator without involving the network. Transport speed is improved by simply referencing the OS image in memory.



Task 2: Download the operating system

Complete this Task on:
 WindowsCE6


Note: You can now download the operating system

a. Select Target | Attach Device
Note: The operating system image will be downloaded, and the Device Emulator will be displayed. The OS will download to the device immediately via DMA transport. You will then see a number of debug messages being displayed in the Platform Builder debug output window.

Note: When you boot the operating system image you will be prompted with the following dialog, this shows that the networking hardware has been found but a suitable driver cannot be loaded.




Figure 24.


b. Click OK to dismiss this dialog.





Exercise 4 - Remote Tools & Memory Leaks

Scenario
The Remote Performance Monitor is a graphical tool for measuring the performance of a remote Windows CE system. You can view the behavior of performance objects, such as processors, memory, threads, and processes. We will be using Remote Performance Monitor to monitor memory load on the reference board, this is useful to track memory load over a period of time, if memory load increases over time this could be normal behavior of the currently running processes and drivers, or this could indicate a memory leak. We will use the memLeak application to show how Remote Performance Monitor can be used.


Task 1:

Complete this Task on:
 WindowsCE6


Note: The Remote Performance Monitor is a graphical tool for measuring the performance of a remote Windows CE system. You can view the behavior of performance objects, such as processors, memory, threads, and processes.
We will be using Remote Performance Monitor to monitor memory load on the reference board, this is useful to track memory load over a period of time, if memory load increases over time this could be normal behavior of the currently running processes and drivers, or this could indicate a memory leak. We will use the memLeak application to show how Remote Performance Monitor can be used.
To start PerfMon –

a. Within Platform Builder Select Target | Remote Tools | Performance Monitor

Note: You will be prompted for the device to connect to.

b. Select Windows CE Default Device

c. Click OK


Figure 25.


Note: Once connected we will need to select which performance items we wish to monitor, in this case we will be monitoring memory load -

d. Select Edit | Add to Chart (or CTRL+I)
Note: The Default objects displayed are power and battery status -




Figure 26.


e. Scroll the Object ComboBox to CE Memory Statistics

f. Scroll the Counter ComboBox to Memory Load

g. Click Add

h. Click Done



Figure 27.
Note: Performance Monitor will now display the current memory load, which should be flat. We will now run the MemLeak program which leaks memory and will show the leak on the Performance Monitor chart.

Note: You can see from the Remote Display Monitor image below that the o/s is running at about 17% memory load.


Figure 28.




i. Switch back to Platform Builder

j. Select Target | Run Programs…
Note: You will be prompted with a dialog showing all of the executable programs which are in the build directory for the workspace.




Figure 29.


k. Locate and select MemLeak.exe from the list of Available programs

l. Click Run

m. Switch back to Performance Monitor
Note: You will notice that the graph clearly shows a change in the memory load in the system – we can also obtain memory load information directly from our application using the GlobalMemoryStatus API.




Figure 30.
Note: We're done with Performance Monitor so this can now be closed.



n. Select File | Exit

Note: With the Remote Performance Monitor tool, you can observe the behavior of performance objects such as CPUs, threads, processes, and system memory. Each performance object has an associated set of performance counters that provide information about device usage, queue lengths, and delays, and information used to measure throughput and internal congestion. Remote Performance Monitor provides the ability to track current activity on a target device and the ability to view data from a log file.



Task 2: Enabling Debug Zones in the MemLeak Application…

Complete this Task on:
 WindowsCE6


Note: Now that our MemLeak application is running, we can explore debug zones. Zones are useful in allowing a developer to dynamically alter the verbosity of debug message output to the kernel debugger. This is helpful when trying to narrow in on the source of a problem.

Note: Debug zones and the Windows CE Console Debug Shell tool (Cesh.exe) provide the ability to selectively turn the debugging message output from your code on and off by using macros. This allows you to trace execution of your code without halting the OS. Tracing is a simple and non-intrusive way of catching problems in your code without causing the OS to stop responding. Debug Zones can be enabled either through Target Control, or through the Platform Builder IDE.

Note: Debug zones are implemented by declaring a DBGPARAM structure in your source code. DBRPARAM is defined in dbgapi.h. DBGPARAM contains three elements – we can see this through the structure definition from dbgapi.h
  typedef struct _DBGPARAM {
  WCHAR	lpszName[32];     // @field Name of module
  WCHAR   rglpszZones[16][32]; // @field names of zones for first 16 bits
  ULONG   ulZoneMask;          // @field Current zone Mask
  } DBGPARAM, *LPDBGPARAM;


Note: The following code example shows how the DBGPARAM structure is defined in memleak – in this case we're only defining three zones, Initialization, Function Tracing, and memory allocation/free – you can also see that we're defining that initialization is enabled as the default zone, we could of course add tracing for any/all of the other zones.
DBGPARAM dpCurSettings = {
TEXT("MemLeak"), {
TEXT("Init"),TEXT("Trace Fn( );"),TEXT("Memory"),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT(""),
TEXT(""),TEXT(""),TEXT(""),TEXT("")},
// By default, turn on the zones for init and errors.
ZONEMASK_INIT
};


Note: The MemLeak sample, like most of the OS, is built to include debug zone information. We can see this by opening the debug zone dialog in Platform Builder.

a. Select Target and choose CE Debug Zones – the zone dialog will appear

b. Click on MemLeak.exe in the Name list and note the zone information dynamically updates in the Debug Zones list.


Figure 31.


Note: By way of demonstrating a zone, we'll enable Trace Fn( ) messages – this will enable debug output messages when the application allocates and releases memory.

c. Click the box next to Trace Fn( ) in the Debug Zones list.

d. Click OK


Figure 32.


Note: Here's the output from the MemLeak application, we can see the entry to AllocateMemory( ) and the allocation of 2048 * TCHAR, we can also see the exit from Allocate memory – also, we can see the entry point to FreeMemory( ) and the exit from FreeMemory( ) function.
--------------------------------------------
4294949561 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - AllocateMemory( ) Function
4294949563 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - AllocateMemory( ) Function
4294949563 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - UseMemory( ) Function
4294949564 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - UseMemory( ) Function
4294949564 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - FreeMemory( ) Function
4294949564 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - FreeMemory( ) Function
4294950065 PID:2dda5912 TID:4dda5d36 0x8dd806dc:
------------------------------ --------------

Note: This simply shows the flow of the application but doesn't provide much additional debug information about memory allocation. We can enable the second debug zone to obtain more information from the application.

Note: Next, we will enable Memory Tracing.

e. Select Target and choose CE Debug Zones – the zone dialog will appear

f. Click on MemLeak.exe in the Name list and note the zone information dynamically updates in the Debug Zones list.

g. Click the box next to Memory in the Debug Zones list.

h. Click OK

Note: Here's a sample of the updated debug information from the memleak application – the highlighted areas below show the additional debug information that has been added by enabling memory tracking.
--------------------------------------------
187306 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - AllocateMemory( ) Function
187306 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Check GlobalMemoryStatus( )
187306 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Memory Load 24%
187307 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Allocate TCHAR *2048 (4096 UNICODE Characters)
187310 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Pointer 0x310030
187310 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - AllocateMemory( ) Function
187310 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - UseMemory( ) Function
187310 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Do Something Interesting here.
187311 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - UseMemory( ) Function
187312 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Enter - FreeMemory( ) Function
187312 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Free Pointer 0x0
187312 PID:2dda5912 TID:4dda5d36 0x8dd806dc: Leave - FreeMemory( ) Function
187814 PID:2dda5912 TID:4dda5d36 0x8dd806dc:
-------------------------------

Note: We can see that in the above sample a pointer has been returned from the LocalAlloc function of 0x310030 – we can also see that the Free memory function is calling LocalFree and passing a pointer value of zero – we can assume from this that we're not using the same variable to allocate and free memory.

i. Select ClassView for your platform workspace

j. Expand the memleak application
Note: The expanded memleak application will display the various functions and variables contained within the memleak program. The AllocateMemory and FreeMemory functions are of particular interest to us.




Figure 33.


k. Double Click the AllocateMemory function in the ClassView
if (g_MemStatus.dwMemoryLoad < 60) {
DEBUGMSG (ZONE_MEMORY, (TEXT("Allocate TCHAR *2048 (4096 UNICODE Characters)\n")));
g_tcTemp=LocalAlloc(LPTR,(2048*sizeof(TCHAR)));
DEBUGMSG (ZONE_MEMORY, (TEXT("Pointer 0x%lx\n"),g_tcTemp));
} else {


Note: We can see from the above code sample that we're allocating 2048*sizeof(TCHAR), the result of calling LocalAlloc is being stored in a variable g_tcTemp.

Note: Now let's examine the FreeMemory function.

l. Double Click the FreeMemory function in the ClassView
void FreeMemory( )
{
  DEBUGMSG (ZONE_TRACE, (TEXT("Enter - FreeMemory( ) Function\n")));
  DEBUGMSG (ZONE_MEMORY, (TEXT("Free Pointer 0x%lx\n"),g_tc_Temp));
  LocalFree(g_tc_Temp);
  DEBUGMSG (ZONE_TRACE, (TEXT("Leave - FreeMemory( ) Function\n")));
}


Note: We can clearly see that we're using a variable called g_tc_Temp to free memory, this is initialized to NULL during the initialization of the application – we have a mix up of variable names.



Task 3: Kernel Tracker

Complete this Task on:
 WindowsCE6


Note: Using the Remote Kernel Tracker you can view thread interactions, internal dependencies, and system state information, the tool shows all processes and threads in the system and when these processes and threads are created, run, or stopped. It also shows when processes and threads are sleeping. In addition to showing the running states of processes and threads, the Remote Kernel Tracker tool displays system events. These system events are mapped onto the thread that was executing at the time they occurred. The tool also shows system interrupts.

Note: Kernel Tracker is an essential tool for tracking real-time events (you will see how this works when you attend the Real-Time hands on lab) – but how can Kernel Tracker be used to track memory? – The answer is simple, through the use of a call to the CELOG( ) API – let's take a look at some of the source code to the memleak application.
MEMORYSTATUS g_MemStatus;
memset(>_MemStatus,0x00,sizeof(g_MemStatus));
g_MemStatus.dwLength=sizeof(g_MemStatus);
GlobalMemoryStatus(>_MemStatus);
DEBUGMSG (ZONE_MEMORY, (TEXT("Memory Load %d%%\n"),g_MemStatus.dwMemoryLoad));
CELOGDATA(TRUE, CELID_RAW_LONG, >_MemStatus.dwMemoryLoad, (WORD) (sizeof(DWORD)), 1, CELZONE_MISC);


Note: In the above code we obtain the current memory load by calling GlobalMemoryStatus( ) – we then call CELOGDATA using the memory load as one of the parameters. In this case we're outputting memory load whenever we allocate memory, of course we could output any useful information, including handles to events, or threads, memory allocation and free etc…

a. In Platform Builder, select Target | Remote Tools | Kernel Tracker

Note: You will be prompted for the device to connect to.

b. Select Windows CE Default Device

c. Click OK

Note: Kernel Tracker will download a number of device side components to the emulator; once these have been downloaded Kernel Tracker will display all running processes, and interrupts

d. Expand the memleak item within Kernel Tracker
Note: You will notice two threads being displayed, one being the application thread (identified as memleak.exe), and the second thread (identified as Thread) which is checking memory load every 500ms, and allocating 2048*TCHAR if memory load is below 60%.




Figure 34.
Note: So how do we locate our custom CELOGDATA information – there's potentially tons of information being displayed by Kernel Tracker.



e. Right Click on the Thread (part of the memleak application)

f. Select Find Next Event on Thread


Figure 35.


Note: Kernel Tracker will 'Jump' to the next event on the selected thread – you will notice in the image below that there's a white square with four dots, this is a custom (or CELOGDATA) item, we can hover our mouse over this item to get a 'data tip' this will display the information we output from our code).


Figure 36.


g. Hover your mouse over the CELOGDATA item – view the data tip.
Note: You will notice that the data tip shown below has [event info] Raw long, 21 – this shows that memory load was at 21% at the time we called CELOGDATA (of course this value may be different on your system).




Figure 37.


h. We're done with kernel tracker – close this down…

i. If prompted to save data from Kernel Tracker, select No

Note: We also need to stop the MemLeak application, there are a number of ways to achieve this, either through the Windows CE Target Control Window, or through the Platform Builder UI directly.

j. Select Debug | Windows | Processes

k. Locate and select Memleak


Figure 38.


l. Click the RED close process button

m. Confirm "YES" to close the process





Exercise 5 - Windows CE Remote Tools

Scenario
The Remote Call Profiler combines profiling and analysis tools with a graphical interface that enables you to identify algorithmic bottlenecks in your code.
The technology in Remote Call Profiler provides a graphical environment to display the information that you want to analyze. You can compare information from different sections of your data and view your data in a variety of formats with Remote Call Profiler.
In this example we will be using the Dining Philosophers problem which is a classic multi-process synchronization problem. The problem consists of five philosophers sitting at a table who do nothing but think and eat. Between each philosopher, there is a single stick. In order to eat, a philosopher must have both sticks. A problem can arise if each philosopher grabs the stick on the right, then waits for the stick on the left. In this case a deadlock has occurred, and all philosophers will starve. Also, the philosophers should be fair and not hold onto the chopsticks for too long, therefore each philosopher should be able to eat as much as the rest.
In this example we will use the Remote Call Profiler to determine where the time is being spent in the application.



Task 1: Remote Call Profiler

Complete this Task on:
 WindowsCE6


a. Select Target | Remote Tools | Call Profiler

b. Connect to Windows CE Default Device

Note: When the Remote Call Profiler is downloading components you may be prompted to locate debug files for some of the tools components (see image below). If prompted to locate components:

c. Select Don't display this dialog again

d. Click Cancel


Figure 39.


Note: When the Remote Call Profiler is ready you will be prompted to Start, Launch, Finish or Cancel.

e. Select Launch

Note: For this lab we will be using an application called Philo.exe

f. Enter the application name Philo.exe


Figure 40.


Note: The philosopher application will run without displaying any user interface – the application does output data to the debug window within Platform Builder – here's an example of the output.
3722641 PID:e3c55f36 TID:23a08d72 #0 waiting forever on right chopstick 23a0889a
3722643 PID:e3c55f36 TID:23a08d72 #0 waiting forever on left chopstick 23a088fa
3722652 PID:e3c55f36 TID:239e2802 #1 Eating
3722653 PID:e3c55f36 TID:239e2802 #1 waiting forever on right chopstick 23a088fa
3722681 PID:e3c55f36 TID:239e2fa6 #2 Eating
3722682 PID:e3c55f36 TID:239e2fa6 #2 waiting forever on right chopstick 23a0895a
3722683 PID:e3c55f36 TID:239e2fa6 #2 waiting forever on left chopstick 23a08a62
3722691 PID:e3c55f36 TID:239e2f66 #3 Eating
3722692 PID:e3c55f36 TID:239e2f66 #3 waiting forever on right chopstick 23a08a62
3722706 PID:e3c55f36 TID:239de1aa #4 Eating
3722707 PID:e3c55f36 TID:239de1aa #4 waiting forever on right chopstick 23a08af2
3722708 PID:e3c55f36 TID:239de1aa #4 waiting forever on left chopstick 23a0889a
3723145 PID:e3c55f36 TID:23a08d72 #0 PonderNothingness
3723146 PID:e3c55f36 TID:239e2802 #1 waiting forever on left chopstick 23a0895a
3723185 PID:e3c55f36 TID:239e2fa6 #2 PonderNothingness
3723186 PID:e3c55f36 TID:239e2f66 #3 waiting forever on left chopstick 23a08af2
3723649 PID:e3c55f36 TID:239de1aa #4 PonderNothingness
3723688 PID:e3c55f36 TID:239e2802 #1 PonderNothingness
3724147 PID:e3c55f36 TID:23a08d72 #0 Eating
3724148 PID:e3c55f36 TID:23a08d72 #0 waiting forever on right chopstick 23a0889a
3724149 PID:e3c55f36 TID:23a08d72 #0 waiting forever on left chopstick 23a088fa
3724151 PID:e3c55f36 TID:239e2f66 #3 PonderNothingness
3724187 PID:e3c55f36 TID:239e2fa6 #2 Eating


Note: Once philo.exe has completed running collection control will show that the number of processes and threads will be zero and Profile Data and File Size will show that data has been collected.

g. Click Finish to complete capturing data.

Note: The Remote Call Profiler provides a number of views onto the data, in the example below we show the TopX view, this lists all functions called in the application, the number of times each function was called, and the amount of time spent in each function, we can clearly see that the majority of time is spent in the function MySleep( ) – if we were now wanting to fine tune this application we should spend time looking at this function, actually, if we examine the code for MySleep we will see that this function simply calls Sleep(dwMilliseconds)

h. Select TopX view within the Call Profiler application (the snail icon on the toolbar)

i. You will be prompted to locate the binary for philo.exe – browse to your build output folder (_FLATRELEASEDIR) – for OSDesign1 this would be C:\WINCE600\OSDesigns\OSDesign1\OSDesign1\RelDir\DeviceEmulator_ARMV4I_Debug

j. Click OK
Note: You will now see the TopX view showing the number of calls to each function within the Philo application and the %age of calls for each function compared to the application as a whole.




Figure 41.


k. Close the Call Profiler application

l. When prompted to save data click No



Task 2: Monte Carlo Profiling

Complete this Task on:
 WindowsCE6


Note: Monte Carlo profiling is a statistical software diagnostic tool in which the operating system is interrupted at a high rate and the interrupted address is recorded. From this information is derived the number of hits per routine. This type of profiling does not measure or record execution time.
Monte Carlo profiling provides two approaches. The first, called process profiling, periodically tracks the current running routine. This is Monte Carlo profiling in the strict sense. A second profiling technique, called object call profiling, is akin to Monte Carlo profiling, and is discussed here also.
Monte Carlo profiling relies on the Windows CE profile-enabled kernel (we enabled profiled kernel when configuring the operating system image). This version of the kernel contains the process and function identification routines, as well as the result-gathering and display routines. The process profiling approach also relies on a periodic interrupt from the OAL.
The profiler collects data as soon as it is launched. When stopped, it analyzes the data and sends the results over the debug port.
CE 6.0 does not automatically start or stop the profiler. You must start and stop the profiler. You can start and stop the profiler by having an application make calls to the ProfileStart and ProfileStop functions.
You can also start and stop the profiler with the default keyboard driver, which supports keys for controlling the profiler.
Finally, you can use the prof command in the Target Control window to control the profiler – this is the approach we’re going to use for this lab.

Note: To use the Monte Carlo Profiler from the Windows CE Target Control Window, use the following commands.

a. To display the Windows CE Target Control Window type ALT+1

b. prof on – turns on profiling

c. s philo – Starts the Philo.exe application (same application we used with the Remote Call Profiler)

d. prof off – turns off profiling, note, you should wait for the application to exit before turning off the profiler.

Note: The following is the output taken from running the profiler against the Philo.exe application.
Total samples recorded = 120814
Module Hits Percent
------------ ---------- -------
nk.exe 118695 98.2
coredll.dll 905 0.7
tcpstk.dll 226 0.1
afd.dll 52 0.0
ndis.dll 46 0.0
cxport.dll 26 0.0
relfsd.dll 20 0.0
Philo.exe 18 0.0
gwes.exe 15 0.0
VMini.dll 11 0.0
netbios.dll 7 0.0
filesys.exe 6 0.0
fsdmgr.dll 4 0.0
ddi.dll 4 0.0
explorer.exe 3 0.0
shell.exe 2 0.0
pm.dll 1 0.0
commctrl.dll 1 0.0
tapi.dll 1 0.0
redir.dll 1 0.0
ohci2.dll 1 0.0
UNKNOWN 769 0.6
Hits Percent Address Module Routine
---------- ------- -------- ------------:---------------------
113891 94.2 8055ab26 nk.exe :_IDLE_STATE
320 0.2 8054e977 nk.exe :_WaitOneMore
292 0.2 80549ca6 nk.exe :_KCNextThread
244 0.2 8054f292 nk.exe :_SC_WaitForMultiple
153 0.1 805451e6 nk.exe :_RunqDequeue
144 0.1 8054b2d4 nk.exe :_SleepOneMore
134 0.1 80540c88 nk.exe :_memcpy
133 0.1 805430a4 nk.exe :_KCall
120 0.0 03fd10a0 coredll.dll :_memset
103 0.0 8054921b nk.exe :_NextThread
101 0.0 8054191f nk.exe :_INTERRUPTS_ENABLE
101 0.0 805430cd nk.exe :_Int22KCallHandler
98 0.0 80540c10 nk.exe :_memset
3 0.0 03f33e1b relfsd.dll :_write_header
3 0.0 03f343aa relfsd.dll :_rreadnocs
3 0.0 03de1090 fsdmgr.dll :?ExitVolume
3 0.0 03cc3a37 cxport.dll :_CTEpEventThread
3 0.0 03bc26d6 afd.dll :_WsaBufToNdisBuf
3 0.0 03bc5988 afd.dll :_EnterCriticalSection
3 0.0 03a5cf70 tcpstk.dll :_InterlockedIncrement
3 0.0 03a7990b tcpstk.dll :_UDPRcv
3 0.0 03a95e71 tcpstk.dll :_GetAddrType
3 0.0 00011915 Philo.exe :?OutputDebug
3 0.0 00011ee3 Philo.exe :?IsAvailable
3 0.0 0001257f Philo.exe :?IsInhibitProfiling
2 0.0 00011018 Philo.exe :?MyWaitForSingleObject
2 0.0 000120bf Philo.exe :?CeCapInit
1 0.0 0001113a Philo.exe :?MySleep
1 0.0 000114fa Philo.exe :?Eat
1 0.0 000125c1 Philo.exe :__penter
1 0.0 02cb8b2c ohci2.dll :?GetPortStatus
773 0.6

Note: We can see from the profiling information that 94% of the time spent while the philo.exe application was running was spent in _IDLE_STATE.