Debugging Silverlight in Firefox 3.6.4 (and newer)

June 29th, 2010 No comments

While they chug along to Firefox 4, Mozilla just released Firefox 3.6.4 (and quickly followed up with 3.6.6) on the world, sporting a new feature – plugin isolation. By isolating plugins to a different process, Firefox catches up to Chrome in stability, by ensuring that a rogue plugin that crashes does not bring down the whole browser, instead you get the sad face informing you that something has happened. This is a great end-user feature which will increase the general reliability of a user’s browsing experience – but we aren’t just users, are we?

Firefox will shut down a plugin when it is deemed to be either dead, or frozen. Unfortunately frozen is the state that the plugin will enter when you try to debug it (say, through Visual Studio), causing Firefox to kill the plugin and continue on its merry way. There have a been a number of proposed solutions, mainly around disabling the plugin container for Silverlight (see this forum post). Unfortunately, while this works for debugging, this is not a real user situation that you are debugging (since you’re no longer locked in the container, which is the expected state for any user running your app). In order to get Firefox to still run Silverlight in the container, but not kill it when it detects a Freeze, do the following:

  1. type about:config
  2. find dom.ipc.plugins.timeoutSecs (you can start typing it in the filter)
  3. change the value to -1

This effectively cancels the timeout so that even though the plugin is no longer responding to Firefox’s NSAPI messages, it will not be killed.

Happy bug hunting!

Oren Categories: Uncategorized Tags:

System Tray obscures FrameRate counters

June 28th, 2010 No comments

Here’s a small tip for those of you who want to debug performance in a Windows Phone Silverlight app with the frame rate counters, but have the System Tray visible – hide it.

The counters currently show up behind the system tray (since technically the tray is a system overlay which is drawing over the surface available to your Silverlight app), so hiding the tray will show the counters.

Don’t forget: to re-enable the system tray when you’re done!

Visual Studio .AddIn File Encoding Error

May 14th, 2010 No comments

If you’re testing the installer for your Visual Studio Add-In and you get the following error when launching Visual Studio:


"Switch from current encoding to specified encoding not supported"

and your .AddIn file looks completely normal when you open it in a text editor, then you're most likely not writing your .AddIn file in Unicode. Since the .AddIn usually specifies:


<?xml version="1.0" encoding="UTF-16" standalone="no"?>

Visual Studio expects the file type to be Unicode, so if you wrote your .AddIn file to disk using something like:


File.WriteAllText(addinFile, addInConfig);

You'll need to adapt your code to something like:


File.WriteAllText(addinFile, addInConfig, System.Text.Encoding.Unicode);

To make sure that the file is Unicode.

Hope that helps!

Oren Categories: Extensibility, Visual Studio Tags:

Create a WPF based ToolWindow in your Add-In

May 11th, 2010 No comments

One of the big changes in Visual Studio 2010 is the introduction of the WPF based UI which means that (amongst other things) you can now host WPF windows directly within the IDE without having to wrap them inside WinForm controls.

Unfortunately you may notice that if you create a simple WPF UserControl and then try to create a ToolWindow from it using CreateToolWindow2, you’ll run into the following Managed Debugging Assistant:


Managed Debugging Assistant 'NonComVisibleBaseClass' has detected a problem in 'c:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe'.

Additional Information: A QueryInterface call was made requesting the default IDispatch interface of COM visible managed class 'WPFUserControlAddIn.WPFToolWindow'. 

However since this class does not have an explicit default interface and derives from non COM visible class 'System.Windows.Controls.UserControl', the QueryInterface call will fail. 

This is done to prevent the non COM visible base class from being constrained by the COM versioning rules.

If you follow through this error message it will lead you to the MSDN page for this error (here) and from there to the page that holds the solution.

The Solution

Add the following to your [yourfile].xaml.cs around your class declaration:


    using System.Runtime.InteropServices;
    using stdole;

    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.None)]
    class [yourclass] : UserControl, IDispatch

Note! Don’t forget the addition of the IDispatch implementation (there’s nothing to implement, don’t worry). Without it you’ll get "InvalidCastException" from mscordb.lib and a null in your ref object (the last parameter to CreateToolWindow2.

Got a Sample For Us?

You bet – grab the sample solution. All you need to do is extract, add the .addin file to you AddIns directory (under Documents\Visual Studio 2010\Addins) and away you go.

Oren Categories: Extensibility, Visual Studio Tags:

Media on Windows Phone 7: “Content” Ye Shall Be

May 10th, 2010 No comments

Here’s an awesome gotcha when moving from a desktop Silverlight application to a Windows Phone 7 application – make sure that your media (wmv) files are set to “Build Action” = “Content” and not “Resource”.

You’ll notice that if you do something like:

<MediaElement Source="somevideo.wmv"/>

Where “somevideo.wmv” is set to “Content”, then the Windows Phone Developer Tools (ie. Visual Studio) will underline the "Source" attribute and recommend that you set it to “Resource”. This is a hangover from the desktop and is something that I hope will go away – you can safely ignore this warning (it won’t appear in your build windows).

What’s Wrong With “Resource”?

For those that want more, here are the potential problems you can run into when setting your media to “Resource”:

  1. When a video file is compiled as a Resource it incurs an extra space and performance hit every time you play it, since Silverlight does extra processing to extract the video from your assembly (DLL). In the case of “Content” the file can be read directly from disk (or memory) and you’ll get instant start playback.
  2. Anything that makes your DLL larger is evil (from my point of view) – you want your assemblies to be small (think “quick and nimble”). Although the size doesn’t always directly affect load and memory time (there are a couple of other factors at play here) this helps eliminate one more possible bottleneck.

Silverlight Performance Tuning without Profiling

May 3rd, 2010 No comments

An often overlooked performance tuning feature of Silverlight are the “Frame Rate Counters” and “Redraw Regions” options – Profiling, step aside! In this post I’ll explore the general options and how to enable them, I’ll follow up with a further post on how to use them to help identify performance bottlenecks.

This post is based off the awesome talk that Seema Ramchandani gave at Mix ‘10.

Note: To test these out either open an existing Silverlight application, or start a new one. The screenshots in this post are mostly from the Windows Phone 7 Emulator (XDE) so will vary slightly to the desktop version.

Frame Rate Counters

  • Enabling
    Open up App.xaml.cs and add the following bolded line to your App() constructor:

    public App()
    {
        UnhandledException += new EventHandler(Application_UnhandledException);
        InitializeComponent();
    
        App.Current.Host.Settings.EnableFrameRateCounter = true;
    }
    
  • What you’ll see
    In the top left corner of the screen you should see the following counters:
    Frame Rate Counters

    Silverlight Framerate Counters


    Note:
    On the desktop counter A & B are merged into one counter.

  • I don’t see the counters! (mobile)
    This means that you are running without hardware acceleration. Either the machine running the emulator does not support hardware acceleration (DirectX 10 for the early refreshes) or you’ve run into a bug (there are a number of them which are currently being zapped) which has caused your device / emulator to run out of video memory. Restart the device / emulator and you should be right.

    Note: To tell if your emulator supports hardware acceleration (or if your device has run out of video memory) look at your screen as you launch a XAP – if it does the page flip animation then you’re ok, otherwise if the app just appears then you are in software.

  • What do they mean?
    I’ve added letters to indicate the different fields as so:

    1. Render Thread Framerate (fps) (mobile only)
    2. UI Thread Rate Framerate (fps)
    3. Amount of VRAM used by App (kb)
    4. Total number of textures on GPU
    5. Number of intermediate textures

Redraw Regions

  • Enabling
    Open up App.xaml.cs and add the following bolded line to your App() constructor:

    public App()
    {
        UnhandledException += new EventHandler(Application_UnhandledException);
        InitializeComponent();
    
        App.Current.Host.Settings.EnableRedrawRegions = true;
    }
    
  • What you’ll see
    In this example I’m using the default Windows Phone list application and clicking through the list options and then back with the hardware back button.

    Redraw Regions highlighting redrawn areas as we transition between pages

  • What am I looking at?
    Redraw regions highlights (tints) every part of the screen as it is redrawn, either due to a user initiated action (say, scrolling a non-cached list box) or some code (animation). Cached Objects that are being animated / moved / scrolled etc (BitmapCache) will not be redrawn, but as soon as the object is modified (say, you change its colour as it moves), it will be redrawn – and you will see this highlighted by EnabledRedrawRegions.

    As regions are redrawn the colours will cycle through Blue, Yellow, Pink (Purple) so that you can see the transition – there is no significance to the specific colours.

Oren Categories: Performance, Silverlight Tags:

Disable IE8 Process Per Tab (or: make profiling in IE8 easy)

April 29th, 2010 2 comments

Internet Explorer 8 ships with a feature to increase reliability by spreading tabs across multiple processes, unfortunately if you want to profile (or debug) a plugin (Silverlight, Flash etc) then you need to know the correct process ID (PID) to profile, as the normal PID will usually be of the iexplore.exe container, as opposed to the tab that you want to target.

To make things easy, you can disable per-tab processes by modifying the following key\value:

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\TabProcGrowth

This vale may not exist, so go ahead and create it as a DWORD with a value of 0. To restore normal IE8 operations (I personally run with it constantly disabled and haven’t seen much of an adverse affect, but on lower powered machines or if you’re not constantly profiling) just delete TabProcGrowth.

You can also automate this by adding it to a .reg file or by using the following command line commands (in a batch file etc):

Disable TabProcGrowth

reg add "HKCU\SOFTWARE\Microsoft\Internet Explorer\Main" /v TabProcGrowth /t REG_DWORD /d 0 /f

Back to default

reg delete "HKCU\Software\Microsoft\Internet Explorer\Main" /v TabProcGrowth /f
29/4/2010 Edit: Fixed registry key as per comment from Rick (Thanks!)
Oren Categories: Uncategorized Tags:

Profiling Silverlight 4 with Visual Studio 2010

April 20th, 2010 13 comments

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:""
    VSPerfClrEnv -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

Oren Categories: Performance, Silverlight Tags:

Restart / Shutdown Windows 7 from a Remote Desktop Session

April 13th, 2010 2 comments

I’m constantly machine hopping at work, using remote desktop to administer or actually work on a remote machine. This usually means that every now and then I need to restart the machine, an option which is conspicuously missing from the Start menu – so here are a couple of options I usually use:

1. Run “shutdown -r -t 0″ (this basically requests a restart within time “0″, ie immediately). Luckily this doesn’t require elevation so runs smoothly. See “shutdown /?” for more options (including shutdown).

2. Click on the taskbar (away from a running app) and hit “Alt + F4″ and you’ll get a dialogue asking you what you want the machine to do – one of the options is restart!

Hit Alt+F4 on the taskbar of a remote session, and this is what you'll see

Rodney points out in the comments below that:

Alternatively, you could try “shutdown -i” for an easy to use GUI to shutdown, restart, etc a bunch of machines, remotely.

Thanks for the tip!

Oren Categories: Uncategorized Tags:

IE9: 2D Graphics Acceleration King?

April 8th, 2010 No comments

The IE9 preview has been out for a while (if you haven’t checked it out, then do so here – it’s a quick install that will sit side by side with your existing browsers, including IE8) and as the “wild story” dust settles serious posts are starting to emerge about the future of IE and browsers in general.

Yesterday a post on the IE blog compared IE9’s 2D rendering example (“Flying Images“) with other major (released) browsers including Chrome 4.1, Safari 4.0.5 and Firefox 3.6. Unfortunately (as pointed out by this slashdot story) the IE blog have chosen to compare a pre release of IE9 with a release of everything else, skipping (for example) Firefox’s nightly versions which already include 2D acceleration.

Sidetrack – Pet Peeve: Along with recent trends to create infographics for everything, more and more sites display pretty graphs showing different measurements and statistics, without showing – in a reproducible way – how they got to them. Kudos to the IE blog for stating that they used xperf, now if only they’d show which perf counters and settings they were hitting…

With a distinct lack of time ahead and no time to properly reproduce the results in xperf, I’ve decided to quickly reproduce the test on my hardware, relying on the software FPS counter (which in the end is king, since that’s what the user is going to see on their screen) and the trusty old “CPU Usage History” counters in Task Manager. I did start using xperf and Windows’ “Performance Monitor”, but the numbers are essentially the same and less confusing.

System Specs: My dev machine (at work) is a relatively souped up machine – Intel Core i7 @ 2.67GHz, 6GB of memory running Windows 7 Enterprise 64-bit.

CPU Usage at Test Start

IE9 Preview

IE9 clocks in with 63 FPS and an extremely low CPU usage. During the test I can see activities on 3 of the 8 virtual cores peaking at around the 20% of each. Average CPU during the test is a flat 3%.

IE9 Doing the Rounds

Low CPU Usage Across all cores

Firefox 20100408 3.7a5pre

Firefox nightly, with 2D acceleration enabled, clocks in at 48 FPS with a little more activity on the CPU (a fluctuating 9-10% with activity on 4 cores peaking at around 30%).

Firefox Nightly Gets Into the Fray

Respectably low CPU usage for Firefox

Chrome 4.1.249.1045 (42898)

Chrome clocks in at 2 FPS with a flat 12% CPU usage caused by heavy usage of one core peaking at just over 80%.

Chrome valiantly tries to keep up

One Core To Rule Them All...

Summary

All in all the behaviour of these browsers on my machine indicates that the IE blog was spot on with their general analysis that hardware acceleration is the way of the future. Chrome’s reliance on one CPU core to achieve rendering in this scenario doesn’t get it very far whereas Firefox and IE9, when taking full advantage of the hardware capabilities of the host cruise through this example with flying colours.

Time will tell how many applications will actually come to rely on this type of hardware acceleration for good performance, but with the rise of Canvas based applications who knows what we may be playing in-browser next?

Oren Categories: Uncategorized Tags: