Saturday, August 23, 2008

Visual Studio Profiler Tips

Here are some random tips for the Visual Studio 2008 Profiler that I have found useful recently:

Adding Marks Programatically

You can add marks to a profile log through the IDE's Data Collection Control pane, but this is a bit inaccurate and tedious. To add them programmatically you can use the Microsoft.VisualStudio.Profiler.DataCollection class in the Microsoft.VisualStudio.Profiler library.

One great advantage of the profile marks is that you can filter the analysis results to focus on a particular phase.

System Library Calls

By default there is no symbol information for interop libraries, so all calls to a particular DLL are treated as a single function call. For instance, profiler could report that 50% of the time was spent in [mscorwks.dll] and the functions that called into this DLL would appear in a single list. This is not very useful for working out how to reduce the number of calls.

To fix this, add the symbol table as follows:

1. Go to Tools > Options > Debugging > Symbols
2. Add to the search path

Now the calls to mscorwks.dll will resolve to things like _JIT_DblRem@16 (fmod in the C math library). Most importantly, the callers of that function are resolved properly rather than being lumped together with every other interop call, so there is some hope of optimization.

UPDATE: Visual Studio 2008 Service Pack 1

Upgrading to SP1 caused me some stress. It appears that all of the symbols are now available from the Microsoft Servers through a single URL and via the press of a magic button. The Reference Source Server Discussion is the place to go for more information.

I had a couple of problems getting [mscorwks.dll] to work in the profiler and the debugger. The solution involved the following:

1. Launch the process unattached and then attach with Managed and Native debugging active. This forces it to do this in future for all processes and makes [mscorwks.dll] visible to the IDE (important for the next step).
2. When the process is attached and running, go to Tools > Options > Debugging > Symbols and press the "Load symbols from Microsoft symbol servers" magic button.

When the magic button is pressed, symbols are downloaded for any libraries you have loaded AT THAT TIME - this is important to note. This can take a while, but is a reliable way of getting the right symbols for the library versions you are actually using. Alternatively you can download the source code and point at it, but I found this unreliable.

If you follow this method you should not even need to set the search path manually, although I wouldn't swear to it and I can't be bothered to use a virgin machine to find out - comments welcome.

No comments: