Mpgo.exe (Managed Profile Guided Optimization Tool)
The Managed Profile Guided Optimization Tool (Mpgo.exe) is a command-line tool that uses common end-user scenarios to optimize the native image assemblies that are created by the Native Image Generator (Ngen.exe). This tool enables you to run training scenarios that generate profile data. The Native Image Generator (Ngen.exe) uses this data to optimize its generated native image application assemblies. A training scenario is a trial run of an expected use of your application. Mpgo.exe is available in Visual Studio Ultimate 2012 and later versions. Starting with Visual Studio 2013, you can also use Mpgo.exe to optimize Windows 8.x Store apps.
Profile-guided optimization improves application startup time, memory utilization (working set size), and throughput by gathering data from training scenarios and using it to optimize the layout of native images.
When you encounter performance issues with startup time and working set size for Intermediate Language (IL) assemblies, we recommend that you first use Ngen.exe to eliminate just-in-time (JIT) compilation costs and to facilitate code sharing. If you need additional improvements, you can then use Mpgo.exe to further optimize your application. You can use the performance data from the un-optimized native image assemblies as a baseline to evaluate the performance gains. Using Mpgo.exe may result in faster cold startup times and a smaller working set size. Mpgo.exe adds information to IL assemblies that Ngen.exe uses to create optimized native image assemblies. For more information, see the entry Improving Launch Performance for your Desktop Applications in the .NET blog.
This tool is automatically installed with Visual Studio. To run the tool, use the Developer Command Prompt for Visual Studio (or the Visual Studio Command Prompt in Windows 7) with administrator credentials, and type the following at the command prompt. For more information, see Command Prompts.
For desktop apps:
mpgo –Scenario <command> [-Import <directory>] –AssemblyList <assembly1> <assembly2> ... -OutDir <directory> [options]
For Windows 8.x Store apps:
mpgo –Scenario <packageName> -AppID <appId> -Timeout <seconds>
All arguments to Mpgo.exe are case-insensitive. Commands are prefixed with a dash.
You can use either
–Import as a required command, but not both. None of the required parameters are used if you specify the
|For desktop apps, use
For Windows 8.x Store apps, use
You must specify either
||The directory in which to place the optimized assemblies. If an assembly already exists in the output directory folder, a new copy is created and an index number is appended to its name; for example: assemblyname-1.exe. Use double quotation marks around directory if it specifies a path that contains spaces.|
|A list of assemblies (including .exe and .dll files), separated by spaces, that you want collect profile information about. You can specify
A text file that contains the list of assemblies you want to collect profile information about, listed one assembly per line. If an assembly name begins with a hyphen (-), use an assembly file list or rename the assembly.
||The ID of the application in the specified package. If you use the wildcard (*), Mpgo.exe will try to enumerate the AppIDs in the package and will fall back to <package_family_name>!App if it fails. If you specify a string that is prefixed by an exclamation point (!), Mpgo.exe will concatenate the package family name with the argument provided.|
||The amount of time to allow the Windows 8.x Store app to run before the app exits.|
||Instruments the assemblies for 64-bit systems. You must specify this parameter for 64-bit assemblies, even if your assembly declares itself as 64 bit.|
||Specifies the configuration file that your scenario uses to provide version and loader information.|
||Forces the inclusion of the profile data in a binary assembly, even if it's signed. If the assembly is signed, it must be re-signed; otherwise, the assembly will fail to load and run.|
||Resets the environment to make certain that an aborted profiling session doesn't affect your assemblies, and then quits. The environment is reset by default before and after a profiling session.|
||Specifies the profiling duration in seconds. Use a value that is slightly more than your observed startup times for GUI applications. At the end of the time-out period, the profile data is recorded although the application continues to run. If you don't set this option, profiling will continue until application shutdown, at which time the data will be recorded.|
||Specifies that the instrumented native images shouldn't be removed after running the scenario. This option is primarily used when you're getting the application that you specified for the scenario running. It will prevent the recreation of native images for subsequent runs of Mpgo.exe. When you have finished running your application, there may be orphaned native images in the cache if you specify this option. In this case, run Mpgo.exe with the same scenario and assembly list and use the
||Cleans up from a run where
You can use both
- AssemblyListFile multiple times on the command line.
If you do not specify full path names when specifying assemblies, Mpgo.exe looks in the current directory. If you specify an incorrect path, Mpgo.exe displays an error message but continues to generate data for other assemblies. If you specify an assembly that is not loaded during the training scenario, no training data is generated for that assembly.
If an assembly in the list is in the global assembly cache, it will not be updated to contain the profile information. Remove it from the global assembly cache to collect profile information.
The use of Ngen.exe and Mpgo.exe is recommended only for large managed applications, because the benefit of precompiled native images is typically seen only when it eliminates significant JIT compilation at run time. Running Mpgo.exe on "Hello World" style applications that aren’t working-set intensive will not provide any benefits, and Mpgo.exe may even fail to gather profile data.
Ngen.exe and Mpgo.exe are not recommended for ASP.NET applications and Windows Communication Foundation (WCF) services.
To Use Mpgo.exe
Use a computer that has the Visual Studio Ultimate 2012 and your application installed.
Run Mpgo.exe as an administrator with the necessary parameters. See the next section for sample commands.
The optimized intermediate language (IL) assemblies are created in the folder specified by the
–OutDirparameter (in the examples, this is the
Replace the IL assemblies you used for Ngen.exe with the new IL assemblies that contain the profile information from the directory specified by
The application setup (using the images provided by Mpgo.exe) will install optimized native images.
Create a set of optimized IL assemblies by using Mpgo.exe with the
Check the optimized IL assemblies into source control.
In the build process, call Mpgo.exe with the
–Importparameter as a post-build step to generate optimized IL images to pass to Ngen.exe.
This process ensures that all assemblies have optimization data. If you check in updated optimized assemblies (steps 1 and 2) more frequently, the performance numbers will be more consistent throughout product development.
Using Mpgo.exe from Visual Studio
You can run Mpgo.exe from Visual Studio (see the article How to: Specify Build Events (C#)) with the following restrictions:
You cannot use quoted paths with trailing slash marks, because Visual Studio macros also use trailing slash marks by default. (For example,
–OutDir "C:\Output Folder\"is invalid.) To work around this restriction, you can escape the trailing slash. (For example, use
By default, Mpgo.exe is not on the Visual Studio build path. You must either add the path to Visual Studio or specify the full path on the Mpgo command line. You can use either the
–Importparameter in the post-build event in Visual Studio. However, the typical process is to use
–Scenarioone time from a Developer Command Prompt for Visual Studio, and then use
–Importto update the optimized assemblies after each build; for example:
"C:\Program Files\Microsoft Visual Studio 11.0\Team Tools\Performance Tools\mpgo.exe" -import "$(OutDir)tmp" -assemblylist "$(TargetPath)" -outdir "$(OutDir)\".
The following Mpgo.exe command from a Developer Command Prompt for Visual Studio optimizes a tax application:
mpgo –scenario "C:\MyApp\MyTax.exe /params par" –AssemblyList Mytax.dll MyTaxUtil2011.dll –OutDir C:\Optimized –TimeOut 15
The following Mpgo.exe command optimizes a sound application:
mpgo –scenario "C:\MyApp\wav2wma.exe –input song1.wav –output song1.wma" –AssemblyList transcode.dll –OutDir C:\Optimized –TimeOut 15
The following Mpgo.exe command uses data from previously optimized assemblies to optimize newer versions of the assemblies:
mpgo.exe -import "C:\Optimized" -assemblylist "C:\MyApp\MyTax.dll" "C:\MyApp\MyTaxUtil2011.dll" -outdir C:\ReOptimized