Home > Performance, Silverlight > Profiling Silverlight 4 with Visual Studio 2010

Profiling Silverlight 4 with Visual Studio 2010

Silverlight is now into its fourth version (woo!) and with each iteration the platform becomes more solid, more mature and easier to develop for. One of the newer features that is now supported “out of the box” with Silverlight 4 is the ability to profile your Silverlight application, but you wouldn’t know it from just digging around the menus of Visual Studio. In this post we’ll explore the process of collecting a profile from your XAP (both in and out of browser) and we’ll touch on common troubleshooting techniques for failed profiles.

What You’ll Need

  1. Visual Studio 2010 Ultimate or Premium (check out the product comparison, under “debugging”, to see if your version has profiling support)
  2. Silverlight 4 for Developers (includes the tools for Visual Studio)
    Note: you can’t profile the normal version of the Silverlight plugin (there’s a bit of registry and file magic that will be missing)

Ready, Set, Profile!

Here’s the quick and easy way to profile – open an elevated Visual Studio 2010 Command Prompt (pre configured with useful Visual Studio commands) and run the following:

VSPerfClrEnv /sampleon
VSPerfCmd -start:sample -output:somefile.vsp
VSPerfCmd -globalon
VSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""
VSPerfCmd -shutdown
VSPerfClrEnv /off

This will create a VSP (Visual Studio Profile) file in the current directory, which you can then open in Visual Studio.

Note: If you plan on profiling Internet Explorer 8 (and up) you must disable its per-tab process feature (otherwise you’ll end up profiling the iexplore container process instead of the process that is hosting Silverlight). You can find more information here.

Tips & Tricks / FAQ (Recommended Reading)
Take a couple of minutes and run through the following, since you are most likely going to run into one of these issues on your first go…

  • Can I Profile Silverlight 3 Projects?
    You can only profile a Silverlight 3 project if it is running under the Silverlight 4 plugin. There are moves to release the supporting files to allow profiling of Silverlight 3, but now that Silverlight 4 is out the point is perhaps moot.
  • Associate VSP Files with Visual Studio
    This one makes it easy to open VSP files directly from the command prompt (and aides in symbol resolution) – try to open a VSP from Explorer and when it asks you which program to use, select devenv.exe
  • Symbols Fail to Resolve when opening VSP
    If the missing symbols are in Microsoft DLLs, make sure you have selected the public symbol server in your debugging options in Visual Studio under Tools->Options->Debugging->Symbols.

    If the missing symbols are in your own DLLs (like from your own XAP) then make sure to either launch the VSP from the directory that has your PDB files (see the above suggestion regarding associating VSP files with Visual Studio), or add the directories with your PDB files to your symbol path by adding them under the same tool window as above.

  • Can I Start Profiling After Launching my Target?
    Sure – you just need to change the order a bit:

    VSPerfClrEnv /sampleon
    VSPerfCmd -start:sample -output:somefile.vsp
    VSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""
    VSPerfCmd -globalon
    VSPerfCmd -shutdown
    VSPerfClrEnv /off
  • Can I Stop Profiling Without Closing the Target?
    You bet!

    VSPerfClrEnv /sampleon
    VSPerfCmd -start:sample -output:somefile.vsp
    VSPerfCmd -globalon
    VSPerfCmd -launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" -args:""
    VSPferfCmd -detach
    VSPerfClrEnv /off
  • My profile doesn’t have any of my functions!
    This is probably the most common problem and there are a couple of things to check:

    1. With the VSP open pull up the “Modules” view – if there are some GUIDs in the list of modules, then your code was picked up, but Visual Studio couldn’t find your symbols – continue to the next step
    2. Pull up the Output window (usually Alt+2, View->Output) and you’ll see a list of symbols that were loaded:

      Loaded symbols for C:\Windows\SYSTEM32\ntdll.dll.
      Loaded symbols for c:\Program Files\Microsoft Silverlight\4.0.50401.0\npctrl.dll.
      Loaded symbols for c:\Program Files\Microsoft Silverlight\4.0.50401.0\agcore.dll.
      Loaded symbols for C:\Windows\system32\WINMM.dll.
      ...

      You may also see one of the two following possible warnings:

      Failed to load symbols for C:\Windows\System32\nlaapi.dll.

      The most common problem – Visual Studio has found the DLL that it profiled, but it can’t find the symbols for it. You’ll need to adjust your symbol path to find the PDB files for your project and then reload the VSP.

      Warning VSP2701: Kit3D.dll could not be found when looking for symbol information.

      This is what you’ll see if you’re profiling a XAP from the internet – Visual Studio can’t find the DLL that it profiled (because it was in the XAP that was downloaded) and thus can’t load it to find corresponding PDBs. Load the VSP from the directory that has your DLLs in them (or add that to your global PATH), or download the XAP and extract the DLLs and this warning will go away. Note that if you don’t have the PDBs then the warning will just morph into the above “Failed to load symbols”.

    3. Your DLLs are not in the list: Make sure that you launch the command to profile from the Visual Studio Command Prompt. Failure to do so will result in a bunch of native DLLs being profiled, but no managed code.
    4. Your code isn’t doing enough work: Although a little less likely for heavy apps, some simple apps simply don’t do enough work. The profiler is sample based, meaning that it will take a snapshot (sample) of the call stack every so often. If the time spent in your code is extremely little, due to your code being so fast, it is likely that the profiler will simply miss your code. You can increase the amount of collected samples, to increase the likelihood of hitting your code by running something like:

      VSPerfCmd -timer:5000000

      This controls how often we sample by specifying the number of cycles before sampling (default is 10,000,000). You can also play around with -pf (sample of every n page faults) and -sys (sample on every n system calls). See -? for more info…

  • Can I attach to a Running Process?
    Yes & No. You can attach to any process that was started within an environment that had the profiling variables set (so for example, any program you launch from a command prompt after running “VSPerfClrEnv /sampleon”). If you attach to a process that wasn’t started within the correct environment you will only get native call stacks and no managed code. To attach to a PID use:

    VSPerfCmd -attach:PID
  • Which Modules are Silverlight Itself?
    Silverlight is made up mainly of native code which can be found in two DLLs, agcore.dll and npctrl.dll. Can you guess why the ag?
  • Why isn’t Silverlight support baked into the IDE?
    Stay tuned (both for a stopgap and a final solution).
  • When will we see debug symbols MS?
    This was a question on Maxim’s blog – I’m not sure what the poster was actually getting at though. Microsoft publishes symbols for all released versions of Silverlight (and some select pre-release versions). These symbols are always the retail symbols – there are no CHK builds (or similar) for Silverlight with extended symbols. All of the information that you would want should be in the public symbols – I would love to hear about something that is missing…
  • I Want More Options!
    This is an important one – check out:

    VSPerfCmd -?

    For a wide range of options, a lot of which I haven’t touched on here.

26/4/2010 Edit: Maxim has a great post which walks you through profiling an actual app using similar steps to those described here – worth checking out!
28/4/2010 Edit: Updated FAQ with some questions from Maxim’s blog
29/4/2010 Edit: Added note about IE8

Categories: Performance, Silverlight Tags:
  1. miteshyh
    April 30th, 2010 at 07:54 | #1

    Great help Oren, I am able to profile now.

    Is there any way to profile code paths against disk usage? I feel my application is busy with disk operations most of the time as I use Isolated storage. I want to findout actual cost of these operations.

    Also is there any way to profile with respect to timeline? I want to find out How much time particuler function call is taking to finish execution.

    All these options were there in Turbo C and Boroland enviorn ment for Win32 programming which are missing here and actually useful.

  2. April 30th, 2010 at 08:49 | #2

    @miteshyh
    Unfortunately the standard tools in Visual Studio do not come with these functions. You can try XPerf for general Process I/O tracking (and other interesting bits of information) across a timeline, but it may not help as much with managed call stacks (though it’s worth a shot).

    I have some code which I can share out which could allow you to set ETW markers from your code, though you would have to create appropriate manifests which may be more effort than it’s worth (depending on what you want to profile).

  3. miteshyh
    April 30th, 2010 at 09:34 | #3

    @Oren
    Hi Oren,

    I didnt get what do you mean by ETW markers, I would like to use it regardless of efforts required. As we have very large product running on Silverlight.

  4. April 30th, 2010 at 15:34 | #4

    @miteshyh
    No worries – I’ll see about getting the code with some examples up on Codeplex sometime next week and post a blog about it. ETW (“Event Tracing for Windows”) provides high resolution (usually microsecond) event markers which you can use to follow the flow of an application and measure time spent in different sections, combined with other sources such as the Kernel. Note that it is only supported in Vista and above.

    There’s a bit more information here: http://msdn.microsoft.com/en-us/magazine/cc163437.aspx

  5. May 10th, 2010 at 05:15 | #5

    Hi! I’m the Italian guy you have answered in stackoverflow!
    Your blog post is super!!! So glad you did it!

    I will link your blog in mine (just opened) if you want.
    As soon as possible I will test it.

    Many many thanks! :)

  6. May 10th, 2010 at 05:37 | #6

    just linked in my blog :)

  7. May 10th, 2010 at 12:13 | #7

    @electricspring Twetter @parseval
    Awesome! Let me know if your run into any “gotchas” and I’ll update the post to include workarounds or fixes :)

  8. May 13th, 2010 at 17:30 | #8

    I am trying to use this method to profile my Silverlight application, but all I ever get is a bunch of calls to agcore.dll. I also get an error saying “It appears that the file was collected without properly setting the environment variables with VSPerfCLREnv.cmd. Symbols for managed binaries may not resolve.” But I have followed the steps exactly, several times. I tried to follow your troubleshooting steps, but in the very first step, I don’t see a “Modules” view. Can you perhaps shed some light on this?

  9. May 13th, 2010 at 22:18 | #9

    @David Nelson
    Hi David -

    The “modules” view is part of the VSP interface, but to bring it up you may need to first click “Show all code” (Visual Studio will hide code it thinks is irrelevant to what you want to see, in order to simplify analysis).

    I’ve never actually encountered your error message though. “agcore” is Silverlight, but it’s a native DLL so it makes sense that that would show up. I’m worried about the claim that you haven’t set your environment variables via VSPerfClrEnv, especially if you have! Were you running this all from an elevated Visual Studio 2010 command prompt? I just noticed that I didn’t mention that it is better to run this elevated (I’ll update the post now).

    Feel free to send me a transcript of the Command Prompt session (oren@) and of the output from the VSP itself – would be great to find out what went wrong here.

  10. June 1st, 2010 at 11:06 | #10

    Oren

    This is the best resource for profiling silverlight 4 apps that I can find. Thanks so much!

    However, I have serious problems with IE8 crashing constantly when trying to profile my application. I have successfully managed to capture around 900 samples of useful profiling data before the crash, including references to all my code etc. However sometimes it crashes after only a few (<20) samples. The crash happens generally as soon as I interact with the application.

    I've tried with and without the reg hack to enable/disable multiple processes in IE. I've even tried it in Google Chrome and I have the same issue.

    The test game that you suggest is no longer available at the link.

    Not sure what I can try next? Please help

    Thanks
    Gareth

  11. June 2nd, 2010 at 03:45 | #11

    @Gareth

    Further to this I have tested on a different, smaller SL application and I can click around it no problem, and the browser does not crash. I;m then able to examine the .vsp in vs 2010 as expected.

    The larger application I have has many projects with dependencies, so I am unsure how to collect the assemblies required. I have tried unzipping all those contained in the xap file, and also merging everything from all the projects Bin directories into a single location. I have then run the described process from that location, but I still get the same crashing browser!

    I’ve run out of ideas at the moment. Any suggestions much appreciated.

    G

  12. Eugen
    July 7th, 2010 at 00:30 | #12

    @Gareth
    Same Problem here, IE 8 crashes after the application is launched when I click on a control. The application is rather small, just for testing. It has one dependency on another class library (small too). Could this dependency force the error?

  13. July 8th, 2010 at 07:06 | #13

    Oren, you have a mistype
    VsPerfClrEnv doesn’t have switch -globalon.
    It’s in VSPerfCmd

  14. August 5th, 2010 at 19:16 | #14

    @Eugen
    @Gareth
    Are you guys running into this?

  15. August 5th, 2010 at 19:16 | #15

    @Alex
    Thanks – well spotted! Fixed now…

  16. Michael Walters
    October 6th, 2010 at 21:10 | #16

    Thanks a lot for a very informative article. Solved most of my problems. A few that weren’t mentioned though:

    If you are attaching to a process and not getting any managed DLL’s profiled, you can get around this by using the /targetclr:v2.0.50727 (Or whatever version you are running in) switch on the attach command. Useful when running on a machine without visual studio installed.

    Using /crosssession and /user:SYSTEM is necessary for the start command when attaching to windows services running as local system. (Not for silverlight I know, but this is what I was trying to achieve).

  17. November 14th, 2010 at 03:11 | #17

    Have you tested this with larger Silverlight RIA Applications >50MB in XAP file size? IE does not get to show the first screen of my app before it crashes even with /TImer:50000 set.

    I tried your -globalon after IE is launched but it crashes even during load of my XAP. Is there a way to tell the VSPerf NOT to profile IE…JUST the SL app in IE? Maybe than the profiler will wait until IE has loaded the XAP…attach to it via /globalon and give me some profiling info.

    Otherwise I guess I have to use the EQATEC profiler

  18. November 17th, 2010 at 14:20 | #18

    @chris Muench
    Hey Chris,

    Any chance you are using Shaders somewhere in your application? If so, there is a bug in Silverlight which causes the profiler to kill Silverlight (seen as a general crash) when profiling. A fix for this is planned sometime soon…

  19. Bernhard
    November 30th, 2010 at 07:17 | #19

    Another Hint: Initially, I didn’t manage to get my dlls loaded into the profiler. It turned out that by disabling all Internet Explorer add-ons except the Silverlight plugin the dlls were found correctly.

  20. November 30th, 2010 at 23:52 | #20

    @Bernhard
    Thanks for the tip – I’ve never seen this one before, but I’ll keep it in mind the next time I run into a dll loading problem. Thanks!

  21. Bernhard
    December 2nd, 2010 at 00:52 | #21

    @Oren
    Maybe the dlls loaded by add-ons somehow interfere with Silverlight. There are e.g. parts of the regular .NET framework (e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll and C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoreei.dll) loaded when add-ons are not disabled. May this be a reason?

  22. June 9th, 2011 at 17:03 | #22

    Good post – For the single process IE issue, I used Firefox rather than change the registry to hack IE. Finally got meaningful info in my vsp that way.

  23. Seema
    September 21st, 2011 at 21:26 | #23

    Hey Oren,
    It looks like I am able to resolve the symbols for all MS symbols except the critical Silverlight ones — is this due to the 64-bit path, or perhaps this version’s symbols haven’t been published yet?

    Loaded symbols for C:\Windows\SysWOW64\ntdll.dll.
    Loaded symbols for C:\Windows\system32\WINMM.dll.
    Failed to load symbols for c:\Program Files (x86)\Microsoft Silverlight\4.0.60531.0\agcore.dll.
    Failed to load symbols for c:\Program Files (x86)\Microsoft Silverlight\4.0.60531.0\npctrl.dll.
    Failed to load symbols for c:\Program Files (x86)\Microsoft Silverlight\4.0.60531.0\coreclr.dll.

  24. JM
    September 23rd, 2011 at 08:56 | #24

    @Oren:
    I am also having trouble getting the agcore.dll symbols to load (my functions are showing up, and pdb’s for my assemblies are being loaded — the vsip file is in the output directory of my project, and I am using the MS public symbol server — all the managed symbols are loading fine.

    I found a post here: http://social.msdn.microsoft.com/Forums/en-AU/vstsprofiler/thread/d1eab82e-0c28-454e-9d46-92818c606c04 where a Microsoft support person claims that symbols for agcore are not publicly available. Is that correct? I’m afraid I’m not getting much use out of the profiler, because it just says that all of my time is spent in agcore.dll.

  25. Jai
    October 12th, 2011 at 03:20 | #25

    I tried all the steps and resolved symbol not loading problem also.

    It did not work. After profiling, I see some user23 and iexplore methods which do not have userfriendly names.
    Nothing related to silverlight was profiled.

  26. Casper
    May 7th, 2012 at 23:43 | #26

    When executing this line: VSPerfCmd -launch:”c:\Program Files (x86)\Internet Explorer\iexplore.exe” -args:”"

    I get error: Could not launch c:\Program Files (x86)\Internet Explorer\iexplore.exe. Error VSP1335: Injection of runtime library failed.

    I’m using Windows XP SP 3, IE 8, VS2010 Premium

  27. Angel
    July 21st, 2013 at 00:26 | #27

    hi
    how can running the Silverlight app out of visual?

  1. May 12th, 2010 at 10:39 | #1
  2. March 9th, 2011 at 23:27 | #2
  3. April 26th, 2011 at 02:17 | #3
  4. August 29th, 2011 at 02:42 | #4