Visual Studio 2010 SP1 Beta 1 Enables Concurrency Profiling Features for Silverlight Applications
A month ago, Microsoft released Visual Studio 2010 Service Pack 1 Beta 1, also known as SP1 Beta 1. This Service Pack beta release allows Silverlight application developers to use the concurrency profiling features. This new feature is very useful when you want to optimize Silverlight applications for multicore hardware.If you work with Visual Studio 2010 Premium or Ultimate versions, you can use the concurrency profiling features for your Windows applications. However, when you create a Silverlight application and you try to launch the Performance Wizard you get the following message: "No launchable projects available to profile in the current solution." If you select the Concurrency option and activate the Visualize the behavior of a multithreaded application checkbox, you will just have the option to specify an executable (.EXE file) to profile. With Visual Studio 2010 SP1 Beta 1, this restriction disappears because you can select a Silverlight project as the target for profiling.
In "Silverlight 4 Makes it Easy to Count Logical Cores," I explained how to launch as many threads as the number of available logical cores in a Silverlight 4 application. The following snapshot shows the Performance Wizard in Visual Studio 2010 Ultimate with SP1 Beta 1 installed. SilverlightThreads is the Silverlight 4 project explained in the previously mentioned post.
By default, a Silverlight application uses multithreaded code under the hoods. This means that the application uses multiple threads and you must take this into account while profiling concurrency. You will see all these threads in the results of the concurrency analysis. In addition, you will see the additional threads that you launch in your application by creating manual threads or injecting them through the thread pool services. The first time that you run a concurrency profiling session for a Silverlight project, you might get a bit confused because you will see too many worker threads. However, all the detailed information provided by the profiler makes it easy to focus on your threads.
It is very important to finish your application as soon as possible to avoid collecting unnecessary information. Once the code you wanted to analyze has finished its execution, close the Web browser with the Silverlight application. This way, your profiling session will provide valuable information.
The next screenshot shows an average CPU utilization of 52% for the Silverlight project. However, it is very important to notice that there are many seconds in the timeline where the average CPU utilization was 0% because the Silverlight application was idle. This situation usually happens when you need some time to close the Web browser. The profiler goes on collecting data during this time and reduces the average CPU utilization value. In "Dive Deeper than the CPU Utilization Graph to Check Efficiency," I've provided detailed information about the meaning of the information provided by the CPU utilization graph.
The following snapshot shows the Threads view. There are too many worker threads. However, just four worker threads are running the multithreaded code launched by the SilverlightThreads application:
* Worker Thread(5544) * Worker Thread(5784) * Worker Thread(5352) * Worker Thread(336)
The following snapshot shows the Cores view with the results of profiling the SilverlightThreads project in a computer with a quad-core CPU without Hyper-Threading. The CPU has four logical cores, also known as hardware threads.
If you combine the concurrency profiling features with the information provided by the new Parallel Stacks window, you will understand the code that runs in threads in a Silverlight project and you will be able to profile it. The following snapshot shows the Parallel Stacks window with the code that each thread is running:
However, you have to be careful because Visual Studio 2010 SP1 Beta 1 is a Beta version and it isn't compatible with Visual Studio Async CTP.