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
  • PingBack from http://www.simplynetdev.com/xperf-a-cpu-sampler-for-silverlight/

  • Performance Profiling in Silverlight

  • Post: Approved at: Oct-11-2008 Silverlight 2.0 to be Released to the Wild Monday? It appears that Silverlight

  • Thanks for this, Seema! I was able to get an initial report from my app, but when I try to run it a second time, I get this error in the console:

    c:\Program Files\Microsoft Windows Performance Toolkit>xperf -on base

    xperf: error: NT Kernel Logger: Cannot create a file when that file already exists. (0xb7).

    I deleted the myprofile.etl file and got the same result.

  • It's not the friendliest UI, but it works. I think that you might already have xperf running, so run xperf -stop and then try again. Any interesting results?

  • I'm not sure what was up before, but I tried again and am not getting that error and am able to run multiple reports. However there's nothing in c:\symbols, so when I expand agcore.dll, there are no method names. I also end up with a c:\kernel.etl file after starting xperf.

  • to make sure that the symbols work, make sure you have done the following:

    1) are running on our public RTW bits

    2) have set the symbol path in your cmd shell (before launching xperfview)

    3) in xperfview selected "Trace --> Load Symbols"

    4) and then accepted the EULA, at least once.

    does that help?

  • Yes, thanks, I guess I completely missed the "Load Symbols" step. This is very cool and I'll definitely use it. Though it would be awesome if this and the WPF Perf Tool were just integrated into VS somehow.

  • There’s so much content on http://www.microsoftpdc.com and I’m trying to watch at least once a day. So

  • Thank you everyone for attending. For those who could not make it, you can check out my talk here on

  • 1. Debugging The following items will help you debug performance in your Silverlight application: EnableRedrawRegions

  • Майк работает в группе Web Tools компании Microsoft и ведет блог, посвященный программированию на Silverlight.

  • There’s so much content on http://www.microsoftpdc.com and I’m trying to watch at least once

  • Seema, thanks for the information. I also watched your PDC session, which also gave lot of great information.

    On the data shown by XPerf can you drill down deeper and help bring out what specific APIs to look and what it means. The challenge is that it is difficult to figure out a way to map that code that we have written in C# to the one that is shown in XPerf which is the agcore.dll's C++ code.

  • This one is a quickie for an easy reference to most commonly used debugging tools and links. I hope you

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