VS2010: Silverlight 4 profiling
Did you know that Visual Studio 2010 Profiler allows you to profile your Silverlight 4 applications? You didn’t? It does.
This feature, however, is only partially integrated with Visual Studio UI, and you need to use command line tools to collect the data. After that, you can open the resulting .VSP file in Visual Studio as you normally do after other types of profiling, and enjoy rich visualization experience while investigating your performance problems.
Yes, we know it is not as cool as clicking VS buttons. We are working on a better experience. Yet I thought it is worth to emphasize that there is a way to use command line tools for customers that seek performance improvement in their Silverlight applications.
Let assume you develop an application like Breakout Game.
You compile it inside VS2010, run it, and want to profiler it. Performance Wizard tells you that this project can not be launched by the profiler. Alas. Time to go to the command line tools. Click you Start button, go to All Programs and find there Microsoft Visual Studio 2010. There, go to Visual Studio Tools and launch a Visual Studio Command Prompt (2010).
This command line window has predefined PATH that includes profiler command line tools:
Now go to the directory where your Silverlight binaries are compiled:
>cd c:\Breakout\Breakout\Bin\Release
We are ready to start our profiling session.
- VSPerfClrEnv /sampleon
- "c:\Program Files (x86)\Internet Explorer\iexplore.exe" C:\Breakout\Breakout\Bin\Release\TestPage.html
- VSPerfCmd /start:sample /output:MyFile /attach:<PID of iexplore.exe process>
- Run your scenario
- VSPerfCmd /detach
- VSPerfCmd /shutdown
- VSPerfClrEnv /off
Steps above require some explanation. In 1 – you prepare environment that is needed for Silverlight profiling, and in 2 you start your hosting process (iexplore.exe, in our example) within that environment. This is essential, as if you start IE by clicking an icon on your desktop, you will get no Silverlight profiling data at the end. Step 3 starts profiler in Sampling mode, and attaches it to the process of your choice. Keep in mind that you might have multiple IE processes running, so you need to choose one that runs your code. If you are not completely sure which one is it, or if your code runs in several IE processes – well, attach to all of them! You can do it either running “VSPerfCmd /attach:<PidN>” several times, or enumerating all PIDs in a single command: “VSPerfCmd /attach:PID1,PID2,PID3”. After profiler is attached, you run your scenario in step 4, and detach profiler from all target processes in step 5. Detaching is not enough, as you need to shutdown the profiler in order to finalize your result file (step 6). Step 7 cancels changes of the environment that are made in step 1.
It could be that you need to profile your application from the very beginning. In this case steps 2 and 3 can be combined into one, where you launch IE under profiler:
VSPerfCmd /start:sample /output:MyFile /launch:"c:\Program Files (x86)\Internet Explorer\iexplore.exe" /args:”C:\Breakout\Breakout\Bin\Release\TestPage.html“
The caveat here is that if IE process spawns a child process that will run your application, the profiler will be still attached to the “father” IE process, as it doesn’t re-attach itself automatically to spawned processes. You may, however, eliminate creation of child processes by IE8 through the registry, modifying TabProcGrowth value.
Please make sure that you run all the above steps from the directory where your binaries are located. Until we complete the integration of this feature with VS, your function names will not be properly resolved otherwise.
You can kill you IE process now. Open the MyFile.VSP in VS2010 as you would do normally for profiling results.
Formally, that would conclude an official support of Silverlight profiling in VS2010 – Sampling mode through command line tools.
However, if you want to try .NET memory or concurrency resource contention profiling of your Silverlight application – I won’t stop you. This is not currently supported and tested modes of work, but they might work for you and help you to see your performance problems from a different angle.
For .NET memory profiling, use “VSPerfClrEnv /samplegc” or “VSPerfClrEnv /samplegclife” in your step 1 to get either allocation profiling or allocation and lifetime profiling.
For concurrency resource contention – use “/start":concurrency,ResourceOnly” in the step 3 above.
Oren Nachman from Sliverlight team is another excellent source for Silverlight profiling information. In his great article he provides several additional tricks and options that you can find quite useful.
Enjoy!
Comments
Anonymous
April 26, 2010
I have followed your directions with your Breakout source code and I cannot produce a profile that contains samples from the Breakout code. I see a similar call tree to your final graphic but it stops at Draw with no System.Windows.dll samples. Can you think of any reason why I would not get any samples below agcore.dll?Anonymous
April 26, 2010
Looks symbols for silverlight 4 are not publically available, when will it become available? its much needed and we have waited more than a year now.Anonymous
April 27, 2010
Thanks miteshyh. I retried with a Release build and I now see the symbols. I'll repeat your question, too. When will we see debug symbols MS?Anonymous
April 27, 2010
Hi MarkRi and Miteshyh, The SL4 team member reply to your question is "the debug symbols will only apply to debug builds (chk). Since we only release ret builds, and only publish ret symbols, I’m not sure what the question is about. The ret symbols give you all the information you would want to have…". Saying that, as I am not Silverlight developer myself, may I suggest that you ask this question on forums on silverlight.net? There are many SL developers trolling those forums, so you might get an answer there. Thanks!Anonymous
April 29, 2010
This doesn't work for me, no matter what I've tried. Basically, the vsp file is created, but it doesn't have any of the symbols for the user code. I even grabbed your breakout solution, put it in c:codeBreakout..., changed dir to that Release dir, rebuilt the project, etc. I can successfully attach, run the app, and close, but I don't even see agcore.dll in my call tree. I'm running VS and the cmd window as admin, too. Any ideas on what's missing?Anonymous
April 29, 2010
@Chuck - which install of VS and Silverlight are you running? Is it possible that you are running a version of Silverlight for which MS has not shared symbols for like an RC or Beta, etc?Anonymous
April 29, 2010
Hi Chuck! When you run "VSPerfClrEnv /sampleon", a number of environment variables are set. Two of them are of the most importance for SL4 profiling - CORECLR_ENABLE_PROFILING=1 CORECLR_PROFILER={F1216318-0905-4fe8-B2E8-105CEB7CD689} Can you please double check that all of the below is valid in your case:
- You use latest available version of Silverlight 4 and VS2010 (as MarkRi noticed).
- You run your scenario from the directory where you SL4 binaries are located.
- You use IE8 and you start it from the prepared environment, not by clicking an IE icon.
- You attach to either all IE8 processes, or to the process that runs your SL code.
- You have CORECLR_ENABLE_PROFILING and CORECLR_PROFILER environment variables set in your target process
- You see VSPerfCorProf.dll is loaded into your target process.
Anonymous
April 29, 2010
Hey Chuck, Did you disable IE multi tab TabProcGrowth registry setting? Without this you will be attaching to the IE container as opposed to the tab process and will not see agcore.dll. Do you have the SL4 dev tools installed - or are you using a stock install of VS2010 RTM to build the project and run it under the SL4 plugin? If so, can you install the SL4 dev tools from http://www.silverlight.net/getstarted/ and retry? Just a note that profiling of the standard install of the SL4 plugin is not supported (you need the dev install to wire up the extra registry settings / files). Regardless, you should always see agcore.dll in your profiles though (regardless of version) as it is native, there just won't be any symbols underneath it. Cheers, OrenAnonymous
April 30, 2010
The comment has been removedAnonymous
April 30, 2010
Hi, I can get the SL4 profiler to work only if the IE loads HTML page, but if using aspx pages, I cannot see my own dll in the module list. Any idea?Anonymous
April 30, 2010
Ah, got it. I used ProcExp to help me ensure I had the VSPerfCorProf.dll loaded within the process I was trying to profile, and that was the missing piece. Apparently orena's comment holds true, I was essentially profiling the wrong process. That also means the cmd that combines steps 2 and 3 didn't work for me because I didn't disable TabProcGrowth in the registry. Hope all this helps others.Anonymous
April 30, 2010
Chuck - great to hear that you got it solved! :) Xsun - there is no difference from Silverlight's point of view if it is being loaded from aspx or html (it's all html in the end). I imagine that you are launching IE in slightly different ways each time:
- Are you always launching IE from the Visual Studio command prompt?
- Have you disabled TabProcGrowth in the registry? (set to 0) Oren
- Anonymous
April 30, 2010
The comment has been removed - Anonymous
April 30, 2010
Hi,
- Yes, always launching IE from VS prompt.
- Yes, TabProcGrowth = 0. I can see the VSPerfCorProf.dll loaded. I tried to use VS test Web service, which didn't sample anything. And I installed in IIS, the profiler captured something. But the result is different if I change the directory in the VS prompt. I can see the System.Windows.dll, and unkown under agcore. What else can I try?
Anonymous
April 30, 2010
It sounds like you are actually getting agcore in the modules list - all you are missing is the symbols. You need to enable the Microsoft Symbol Server under Tools -> Options -> Debugging -> Symbols. Also make sure that you have the directory to your pdbs in the same list, so that you get the correct managed symbols. OrenAnonymous
April 30, 2010
Got it. Tried the same steps as before, worked using both VS test web server and IIS. I might have entered the wrong directory. Thanks.Anonymous
May 07, 2010
SL4 sampling works great. How to get instrumentation to work with SL4? Is it supported?Anonymous
May 07, 2010
Hi Xsun! Glad it works for you. No, instrumentation is not supported currently.Anonymous
May 14, 2010
I can succesfully attach to the IE process, but when I start to use my app IE crashes. What are my next steps?Anonymous
May 25, 2010
Hi Eli, sorry it took so long to reply to you (MSDN blog comments were disabled for the site maintenance). Can you please try to use SLLauncher.exe instead of IE for your application. Does it have the same problem? Does your application, btw, works with drop shadows?Anonymous
May 26, 2010
I had a similar experience as Eli, when I try to profile my app, it crashes out. The sample app worked just fine. Honestly I gave up trying to make my app work, but didn't know that running SLLauncher was an option for an In-browser app so I'll try that I guess...Anonymous
June 02, 2010
Hi CUrwiler! Thanks for your comment and please let us know if usage of SLLauncher helps you.Anonymous
June 03, 2010
I have the same problem - for anything but the simplest Silverlight apps, IE crashes within 15 seconds or so. Any advice would be appreciated.Anonymous
June 04, 2010
Ritchie, CUrwiler, Eli - can you please try to use /timer option of VSPerfCmd to modify frequency of sampling? I wonder if change of sampling timing will allow to skip a problematic code area. This issue is also being discussed on profiler forum: social.msdn.microsoft.com/.../14be439a-ebf1-445b-8492-62010b945f33 Please add there any information you have. Thanks a lot for your help!Anonymous
July 17, 2010
I'm experiencing the same problem when the profiler attaches to the IE process. I have updated the frequency of sampling but it seems that increasing the interval is not the solution because the profiler does not capture enough data. Did you solve the problem? Are there any updates from the SL team on this issue?Anonymous
September 07, 2010
Hi, I tried running the above commands and got the error that 'MyFile.vsp' cannot be created since Access is denied. I know this might be a very basic question but can I know where this file is getting created so that I can get permission on that folder or can I change the path where this file gets created. Thanks in advance, HiralAnonymous
October 07, 2010
The only way this worked for me was to set the TabProcGrowth to 1 instead of 0. At 0 or absent, IE8 always crashed within a second or so.Anonymous
October 07, 2010
Scratch that...I had to enable the debug symbols in VS and set the TabprocGrowth to 0. Now it's working...sheesh.Anonymous
March 03, 2011
Hi, I'm trying to use profiling in instrumentation mode. I don't see help anywhere for this. Could you briefly give the commands for this mode as well?Anonymous
May 11, 2011
Hi, I have the same question as Uday: need to know how to profile instrumentation of a Silverlight project. I have search around and tried many ways, but no luck. Is instrumentation supported now? If yes, what are the steps? Thanks a lot!