<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>Computing</title>
        <link>http://www.garfoot.com/blog/category/5.aspx</link>
        <description>Computing</description>
        <language>en-GB</language>
        <copyright>Rob Garfoot</copyright>
        <generator>Subtext Version 2.1.2.2</generator>
        <item>
            <title>Performance Profiling .NET Applications Using the Visual Studio Profiler</title>
            <link>http://garfoot.com/blog/archive/2010/01/27/performance-profiling-.net-applications-using-the-visual-studio-profiler.aspx</link>
            <description>&lt;p&gt;When I consult with development teams, at some point performance always rears its ugly head. When I code review something I often find it overly complex with lots of caching, “clever” code etc. and when I ask why the code is this complex the answer is usually for performance, E.g. “we need to cache the results as that’s an expensive operation”.&lt;/p&gt;
&lt;p&gt;At this point I usually ask if they have any evidence to show how expensive the operation is, all too often the answer is “no”. Once again another developer is the victim of premature optimisation.&lt;/p&gt;
&lt;p&gt;Now don’t get me wrong, optimising code is important, things like caching can indeed help reduce the cost of expensive operations. However, if that operation is only called once is caching really going to help? If the operation is “expensive” but only takes 2ms to execute and is called next to an operation that takes 2s is optimising it really going to help? I hope you all answered no there!&lt;/p&gt;
&lt;p&gt;So how do we find out if we need to optimise, where do we focus our effort with limited time and resources to get the best return? That’s where profilers come in. If you’ve not used one before, they are applications that monitor how an application runs, usually capturing data such as number of calls, time taken, memory allocated etc.&lt;/p&gt;
&lt;p&gt;Visual Studio comes with some pretty good profiling tools in the Team System version that can really help. There is a nice Performance Wizard on the Analyze menu that does much of the work for you. But as nice as the wizard is, it doesn’t fit all situations. If you need to profile services, code running as different users, on servers without VS installed or just need more control then the command line tools is where it’s at. You can download a standalone version from &lt;a title="http://www.microsoft.com/downloads/details.aspx?familyid=fd02c7d6-5306-41f2-a1be-b7dcb74c9c0b&amp;amp;displaylang=en" href="http://www.microsoft.com/downloads/details.aspx?familyid=fd02c7d6-5306-41f2-a1be-b7dcb74c9c0b&amp;amp;displaylang=en"&gt;http://www.microsoft.com/downloads/details.aspx?familyid=fd02c7d6-5306-41f2-a1be-b7dcb74c9c0b&amp;amp;displaylang=en&lt;/a&gt; for installation on servers or machines without VS.&lt;/p&gt;
&lt;p&gt;In this post I’m going to go through the (simple) steps needed to profile an application, I’ll leave interpreting the reports to another post.&lt;/p&gt;
&lt;h2&gt;Types of profiling&lt;/h2&gt;
&lt;p&gt;There are two types of profiling you can do, sampling and instrumentation. I’m going to mainly cover instrumentation as that’s the one I usually use as it’s the most accurate, it is however also the most invasive and requires changing assemblies so isn’t always suitable.&lt;/p&gt;
&lt;h2&gt;Setting up the profiler&lt;/h2&gt;
&lt;p&gt;If you have Visual Studio Team System for Developers then you have the profiler installed. The first thing to do is configure the command prompt to let you use the profiling tools. You’ll need to add them into your path. They are located by default in “C:\Program Files\Microsoft Visual Studio 9.0\Team Tools\Performance Tools” but it may be a different location on 64bit (Program Files (x86)) or if you' installed to a none standard location.&lt;/p&gt;
&lt;p&gt;Once that’s done you need to enable the environment variables the profiler needs. You can do this with the &lt;strong&gt;VSPerfCLREnv&lt;/strong&gt; command. You want to use &lt;strong&gt;/traceon&lt;/strong&gt; if you are able to trace the application from the command console window by launching it but if you are tracing a service then use &lt;strong&gt;/globaltraceon&lt;/strong&gt;. The global one will most likely need a reboot for the service to pick up the new settings so I usually do that afterwards.&lt;/p&gt;
&lt;p&gt;Note if you are using sampling then you use &lt;strong&gt;/sampleon&lt;/strong&gt; and &lt;strong&gt;/globalsampleon&lt;/strong&gt; instead.&lt;/p&gt;
&lt;h2&gt;Preparing the assemblies&lt;/h2&gt;
&lt;p&gt;Now that the profiler is configured you need to prep the assemblies as we are instrumenting and not sampling. If you are sampling you can miss this step out.&lt;/p&gt;
&lt;p&gt;Build your assemblies as normal, ensure you have pdb files generated though as you’ll need those later to view the source code from the profiling reports.&lt;/p&gt;
&lt;p&gt;Now instrument all of the assemblies you want performance data from, you use &lt;strong&gt;VSInstr&lt;/strong&gt; to do this. Simply specify the assembly name and you’ll get a new assembly and pdb file, the original ones get renamed to .orig.&lt;/p&gt;
&lt;p&gt;Note that if you do this to a strong named assembly it’ll break the signing, you’ll need to use &lt;strong&gt;sn&lt;/strong&gt; with the –&lt;strong&gt;Vr&lt;/strong&gt; option to skip strong name verification on the assemblies you have instrumented. Don’t forgot to switch it back once you are done.&lt;/p&gt;
&lt;h2&gt;Profiling&lt;/h2&gt;
&lt;p&gt;Now you’re all ready to profile. To control the profiler you simply start it using the &lt;strong&gt;VSPerfCmd&lt;/strong&gt; command. You specify the &lt;strong&gt;/start:trace&lt;/strong&gt; option to start instrumenting or &lt;strong&gt;/start:sample&lt;/strong&gt; to start sampling. You will also need &lt;strong&gt;/output&lt;/strong&gt; to set the output filename for the trace results.&lt;/p&gt;
&lt;p&gt;E.g. &lt;strong&gt;VSPerfCmd /start:trace /output:traceoutput.vsp&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;If you are tracing a service you’ll also need to add &lt;strong&gt;/user:&lt;/strong&gt; and probably &lt;strong&gt;/crosssession&lt;/strong&gt; as the service will be running as a different user and in a different session from the profiler. You’ll need to have the permissions to do this.&lt;/p&gt;
&lt;p&gt;Once the profiler is started simply now run up your application and use it. It's important to make sure that the application is not running until after the profiler is started, this is often missed when profiling IIS, stop the app pool or do an &lt;strong&gt;IISReset&lt;/strong&gt; before starting the profiler.&lt;/p&gt;
&lt;p&gt;When you’ve done you need to shutdown the profiler, simply issue &lt;strong&gt;VSPerfCmd /shutdown&lt;/strong&gt; to do that.&lt;/p&gt;
&lt;p&gt;You should now have a shiny new .vsp file that contains the trace data. You can open this in Visual Studio, however the chances are you’ll just see stream of hex addresses and no source information.&lt;/p&gt;
&lt;h2&gt;I’m packing symbols&lt;/h2&gt;
&lt;p&gt;To make a .vsp file that can link back to source you need symbols, remember I told you that you’ll need these so if you don’t have them go back and start again and tell yourself that you’re a very naughty boy/girl (delete as appropriate).&lt;/p&gt;
&lt;p&gt;How do we get the symbols in there or what if I don’t have Visual Studio? You can use the &lt;strong&gt;VSPerfReport&lt;/strong&gt; command to pack the symbols into the .vsp file and to generate a text report of the trace.&lt;/p&gt;
&lt;p&gt;You want to ensure you have your symbols set up to pull down from a symbol server to get all of them for the core .NET bits. To do that set the environment variable &lt;strong&gt;_NT_SYMBOL_PATH&lt;/strong&gt; to point to the symbol server and your local cache.&lt;/p&gt;
&lt;p&gt;E.g. &lt;strong&gt;set _NT_SYMBOL_PATH=symsrv*symsrv.dll*c:\mylocalcache*http://msdl.microsoft.com/download/symbols&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;See &lt;a title="http://support.microsoft.com/kb/311503" href="http://support.microsoft.com/kb/311503"&gt;http://support.microsoft.com/kb/311503&lt;/a&gt; for more details on setting the symbol path.&lt;/p&gt;
&lt;p&gt;I usually just do a &lt;strong&gt;VSPerfReport /packsymbols /summary:function /symbolpath=&amp;lt;path to .instr.pdb files&amp;gt; traceoutput.vsp &lt;/strong&gt;to get my symbols packed. This will also generate a summary text file, you need to do that even if you don’t need the summary or the symbols will not get packed. If you want a more detailed report then you can use the &lt;strong&gt;/summary&lt;/strong&gt; option to specify what information you want in CSV format.&lt;/p&gt;
&lt;h2&gt;Comparing runs&lt;/h2&gt;
&lt;p&gt;Another nice feature of &lt;strong&gt;VSPerfReport&lt;/strong&gt; is the &lt;strong&gt;/diff&lt;/strong&gt; option. This lets you compare two .vsp files and shows the differences, this can be useful for checking against a baselined performance session to see if you’ve made things better or worse.&lt;/p&gt;
&lt;h2&gt;And finally…&lt;/h2&gt;
&lt;p&gt;Don’t forget that you want to turn instrumentation off if you are running it on a production server, replace the assemblies with the uninstrumented versions, revert the &lt;strong&gt;sn&lt;/strong&gt; commands if you used them, run &lt;strong&gt;VSPerfCLREnv /off&lt;/strong&gt; to clean up environement variables.&lt;/p&gt;
&lt;p&gt;I’ll try and post something about interpreting the reports that the profiler gives you soon, after all doing all of this doesn’t help if you can’t read the data!&lt;/p&gt;&lt;img src="http://garfoot.com/blog/aggbug/15.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Garfoot</dc:creator>
            <guid>http://garfoot.com/blog/archive/2010/01/27/performance-profiling-.net-applications-using-the-visual-studio-profiler.aspx</guid>
            <pubDate>Wed, 27 Jan 2010 15:08:00 GMT</pubDate>
            <wfw:comment>http://garfoot.com/blog/comments/15.aspx</wfw:comment>
            <comments>http://garfoot.com/blog/archive/2010/01/27/performance-profiling-.net-applications-using-the-visual-studio-profiler.aspx#feedback</comments>
            <wfw:commentRss>http://garfoot.com/blog/comments/commentRss/15.aspx</wfw:commentRss>
            <trackback:ping>http://garfoot.com/blog/services/trackbacks/15.aspx</trackback:ping>
        </item>
        <item>
            <title>Activating AHCI mode after installing Windows on IDE mode</title>
            <link>http://garfoot.com/blog/archive/2009/09/16/ahci-ide-migration.aspx</link>
            <description>&lt;p&gt;I recently just rebuilt my home PC and installed Winodws 7 on it. It was a nice fast, smooth install and generally went without a hitch.&lt;/p&gt;
&lt;p&gt;That is, without a hitch until I discovered I'd installed Windows whilst the motherboard's SATA controller was in IDE mode and not AHCI mode. Why is this important? Well without AHCI you don't get nice things like power saving, native command queuing etc and that impacts the power consumption, speed and noise of your drives.&lt;/p&gt;
&lt;p&gt;Of course that does rely on having a drive that supports those features, since like most newish drives mine do, I wanted to benefit.&lt;/p&gt;
&lt;p&gt;Unfortunately just changing the setting in the BIOS causes Windows 7 and Windows Vista to both blue screen (BSOD) at boot up with the error &lt;strong&gt;STOP 0x0000007B INACCESSABLE_BOOT_DEVICE&lt;/strong&gt;. That's because Windows doesn't have the drivers for AHCI since I installed it with IDE drivers.&lt;/p&gt;
&lt;p&gt;How to get around this? Well it's surprisingly simple. After reading lots of hairy articles about hacking in drivers etc I found this knowledge base article &lt;font face=""&gt;&lt;a href="http://support.microsoft.com/kb/922976"&gt;http://support.microsoft.com/kb/922976&lt;/a&gt;. One simple registry setting and Windows enables it's default AHCI driver. You can then reboot, change your BIOS settings from IDE to AHCI and Windows will boot and redetect your controller and drives.&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;Once that's done, I rebooted to finish the install, then I installed the Intel Matrix Raid drivers so I had the actual manufacturers drivers on rather than the generic Windows ones.&lt;/p&gt;
&lt;p&gt;All sorted now and it saved me a reinstall. I've just done a copy of 20GB of data from partition to partition on the same drive, much faster and quieter with AHCI than without. I guess that's down to NCQ being able to reorder the reads and writes into something a bit more effiicient.&lt;/p&gt;&lt;img src="http://garfoot.com/blog/aggbug/14.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Rob Garfoot</dc:creator>
            <guid>http://garfoot.com/blog/archive/2009/09/16/ahci-ide-migration.aspx</guid>
            <pubDate>Wed, 16 Sep 2009 00:41:45 GMT</pubDate>
            <wfw:comment>http://garfoot.com/blog/comments/14.aspx</wfw:comment>
            <comments>http://garfoot.com/blog/archive/2009/09/16/ahci-ide-migration.aspx#feedback</comments>
            <wfw:commentRss>http://garfoot.com/blog/comments/commentRss/14.aspx</wfw:commentRss>
            <trackback:ping>http://garfoot.com/blog/services/trackbacks/14.aspx</trackback:ping>
        </item>
    </channel>
</rss>