XPerf: A CPU Sampler for Silverlight

XPerf: A CPU Sampler for Silverlight

  • Comments 21

For those of you who are a) building graphics-intensive applications or b) trying to debug your performance, I would like to introduce xperf and xperfview. These are two profiling tools which can be used to analyze the performance of any Microsoft technology, including Silverlight.

 

These tools have existed internally for quite some time, and I'm excited to see that they have been released. I use xperf as my first step for profiling any app -- it's a simple CPU sampler and gives you a peek to why on earth something is taking up so many CPU cycles. XPerf plugs into the Event Tracing for Windows library, and listens to all the internally embedded events that all Windows products trigger. With the publicly available Silverlight symbols, you can see how these events line back to what Silverlight is doing under the hood, while your app is running.

 

Note: with xperf, managed code (your app code) will show up as samples on the methods that your app code calls. XPerf does not have the capabilities to show CPU cycles in managed code.

 

With XPerf, one can find answers to questions like:

  • Is my app asking Silverlight to constantly spin on CPU cycles.
  • Whether one UI layout or design is more expensive than the other.
  • Whether the time is spent in drawing (agcore.dll), the plug-in’s interactions with (npctrl.dll), or in compilation/JIT (coreclr.dll)
  • How much is stretching/blending/rotating that video going to cost in terms of CPU cycles? How about if you encode it differently?

 Please note that the tools only work on Vista and Windows Server 2k3. For profiling on a mac, see my previous post on how to use Shark from the Apple CHUD tools.

 

The first-time instructions are as follows:

1.       Install XPerf (formerly xperfinfo) and xperfview (formerly xperf) as available here: http://msdn.microsoft.com/en-us/library/cc305187.aspx

[10/9-9am I updated the above link: download link http://www.microsoft.com/whdc/system/sysperf/perftools.mspx]

 

Close everything that is not user-specific (close VS, but keep your virus checker, etc).

2.       Startup your sample

I personally start up my sample without profiling because the startup sampling is very different from that of the runtime.

3.       Open an Administrator-level command prompt. Set your symbol path in the cmd shell:
set _NT_SYMBOL_PATH= srv*C:\symbols*http://msdl.microsoft.com/downloads/symbols 

This will set your symbol path to point to the Microsoft symbol server. The symbol server hosts debugging symbols for most all Microsoft products.

 

[update: cd into C:\Program Files\Microsoft Windows Performance Toolkit]

 

4.       xperf -on base

Run the app for maybe 10 seconds for this initial run. We want to understand what method is taking up the most cycles. At the end of this, you will see a graph of CPU usage over time – so you can use various methods to see where time is going: repeatedly execute a user action (expand/collapse, scroll, etc), let the media play for a while, etc. If you are trying to compare several different options, you can execute different actions at widely-spaced intervals, and compare the graphs for each.

5.       xperf –d myprofile.etl

Step #4 will both stop the profile and start the writing of the event trace log (etl) file. This can take a while, depending on the complexity of your application, how many other apps were running, and how long the profile extended.

 

6.       xperfview myprofile.etl

We see now see multiple graphs. We are most interested in that of the CPU cycles, which will look something like this:

xperfview full graph 

In my sample, I have a dual-core machine so you see two lines, one for each CPU. This graph is of a media file playing, which should have an almost steady hit for the CPU. I can drag and select any section of the graph and take a closer look:

 

7.       TraceàLoad Symbols

8.       Select the area of the CPU graph that you want to see

        • Right-click and select Summary Table

  How to open summary table

 

9.       Accept the EULA for using symbols, expand IExplore.exe (or firefox.exe)

At this point, you’ll see how the time breaks down between the different processes and modules running within windows. Agcore.dll, npctrl.dll, and coreclr.dll are all Silverlight.

10.   If the symbols server has correctly been set, you’ll see that you can expand and see the breakdown of time in each method call within agcore.dll:

Summary Table of Olympics on Beta2 Bits 

The way to read this graph is that IE is taking up 40% of the CPU on average, during the selected sample. Within that, 32% of the time is spent in Silverlight core (graphics/decoding/property engine, etc are all represented in agcore.dll), and then of that time, 29% (9.24/32.33) of the drawing time is spent in one method call.

 

Note: with xperf, managed code (your app code) will show up as samples on the methods that your app code calls. XPerf does not have the capabilities to look at app code.

 

Now what? Well, with just the above think about how you can answer the questions at the top:

  • Is my app asking Silverlight to constantly spin on CPU cycles.
    Do I see activity when the app does not seem to be doing anything? Where does the summary table say that time is going? Perhaps turn on EnableRedrawRegions and see if there is a draw being forced.
  • Whether one UI layout or design is more expensive than the other.
    When I scroll now, is my average CPU higher or lower than before?
  • Whether the time is spent in drawing (agcore.dll), the plug-in’s interactions with (npctrl.dll), or in compilation/JIT (coreclr.dll)
  • How much is stretching/blending/rotating that video going to cost in terms of CPU cycles? How about if you encode it differently?
    Are there new methods that you see in the stack, now that you have stretched the media? Blended it? What is the average time for CPU cycles compared to your baseline? I know that it is cheaper for me to embed an icon overlay directly into the video, but what does it really cost in terms of CPU time?

 Note: The tools only work on Vista and Windows Server 2k3

 

For later usage, your usage pattern is quicker:

  1. Set your symbol path in an Admin-level cmd shell:
    set _NT_SYMBOL_PATH= srv*C:\symbols*http://msdl.microsoft.com/downloads/symbols 
  2. xperf -on base
  3. xperf –d myprofile.etl
  4. xperfview myprofile.etl

 The platform code and the CPU samples will change version to version, so please keep that in mind when profiling between upgrades.

 

See anything interesting? Drop me a note!

 

Have fun!

.seema.

Comments
  • Seema Ramchandani works on performance as a Program Manager on the Silverlight team. She gave a fast-paced

  • Seema Ramchandani Overview < 1 year from Silverlight 2 release Scenarios: Customer adoption blockers

  • I run xperf but it is not enough for stress testing. Is there any tool (free if it is possible) for silverlight testing which allow record scripts and set thinking time, number of user and other parameters for stress testing?

    Thanks.

  • FYI using Perfmonitor should be able to provide call-stacks.

    ETW is available only in Windows

    <a href="www.eastcomfort.net/index.ro.html">Cazare Ieftina Bucuresti</a>

  • FYI using Perfmonitor should be able to provide call-stacks.

    ETW is available only in Windows

    www.eastcomfort.net/index.ro.html

  • @endquote I had the same issue of "xperf: error: NT Kernel Logger: Cannot create a file when that file already exists. (0xb7)." but had a different resolution notied in my blog post: www.blogmynog.com/.../xperf-error-nt-kernel-logger-cannot-create-a-file-when-that-file-already-exists

Page 2 of 2 (21 items) 12
Leave a Comment
  • Please add 1 and 1 and type the answer here:
  • Post