Mpgo.exe(按托管配置文件优化工具)Mpgo.exe (Managed Profile Guided Optimization Tool)

托管配置文件引导式优化工具 (Mpgo.exe) 是一种命令行工具,使用常见的最终用户方案优化由本机映像生成器 (Ngen.exe) 创建的本机映像程序集。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. 本机映像生成器 (Ngen.exe) 使用此数据优化其生成的本机映像应用程序程序集。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 适用于 Visual Studio Ultimate 2012 及更高版本。Mpgo.exe is available in Visual Studio Ultimate 2012 and later versions. 从 Visual Studio 2013 开始,还可以使用 Mpgo.exe 优化 Windows 8.x 应用商店应用。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.

当你遇到与中间语言 (IL) 程序集的启动时间和工作集大小相关的性能问题时,建议你先使用 Ngen.exe 消除实时 (JIT) 编译成本和推动代码共享。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. 如果你需要更多改进,则可使用 Mpgo.exe 进一步优化应用程序。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. 使用 Mpgo.exe 可以获得更短的冷启动时间和更小的工作集大小。Using Mpgo.exe may result in faster cold startup times and a smaller working set size. Mpgo.exe 将信息到添加 Ngen.exe 用于创建优化的本机映像程序集的 IL 程序集中。Mpgo.exe adds information to IL assemblies that Ngen.exe uses to create optimized native image assemblies. 有关详细信息,请参阅 .NET 博客中的 Improving Launch Performance for your Desktop Applications(提高桌面应用程序的启动性能)。For more information, see the entry Improving Launch Performance for your Desktop Applications in the .NET blog.

此工具会自动随 Visual Studio 一起安装。This tool is automatically installed with Visual Studio. 若要运行此工具,请通过管理员凭据使用 Visual Studio 开发人员命令提示(或 Windows 7 中的 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]  

对于 Windows 8.x 应用商店应用:For Windows 8.x Store apps:


mpgo –Scenario <packageName> -AppID <appId> -Timeout <seconds>  


Mpgo.exe 的所有自变量都区分大小写。All arguments to Mpgo.exe are case-insensitive. 命令前加上短划线。Commands are prefixed with a dash.


你可以使用 –Scenario–Import 作为必需命令,但不能两个都用。You can use either –Scenario or –Import as a required command, but not both. 如果指定 –Reset 选项,则不使用任何所需参数。None of the required parameters are used if you specify the –Reset option.

必选参数Required parameter 说明Description
-Scenario <command> -Scenario <command>

- 或 -—or—

-Scenario <packageName> -Scenario <packageName>


-Import <directory> -Import <directory>
对于桌面应用,使用 –Scenario 指定命令来运行你要优化的应用程序,包括任何命令行参数。For desktop apps, use –Scenario to specify the command to run the application you want to optimize, including any command-line arguments. 如果 command 指定的路径包含空格,则应使用三组双引号将其引起来。例如:mpgo.exe -scenario """C:\My App\myapp.exe""" -assemblylist """C:\My App\myapp.exe""" -outdir "C:\optimized files"Use three sets of double quotation marks around command if it specifies a path that includes spaces; for example: mpgo.exe -scenario """C:\My App\myapp.exe""" -assemblylist """C:\My App\myapp.exe""" -outdir "C:\optimized files". 请勿使用双引号;如果 command 包含空格,使用双引号将不能正确发挥作用 。Do not use double quotation marks; they will not work correctly if command includes spaces.


对于 Windows 8.x 应用商店 应用,使用 –Scenario 指定你要为其生成配置文件信息的包。For Windows 8.x Store apps, use –Scenario to specify the package that you want to generate profile information for. 如果指定包显示名称或包系列名称而不是完整的包名称,则 Mpgo.exe 将选择与你提供的名称匹配的包(如果只有一个匹配项)。If you specify the package display name or the package family name instead of the full package name, Mpgo.exe will select the package that matches the name you provided if there is only one match. 如果多个包与指定的名称匹配,则 Mpgo.exe 将提示你选择一个包。If multiple packages match the specified name, Mpgo.exe will prompt you to choose a package.

- 或 -—or—

使用 -Import 指定应使用之前优化的程序集中的优化数据来优化 -AssemblyList 中的程序集。Use -Import to specify that optimization data from previously optimized assemblies should be used to optimize the assemblies in -AssemblyList. directory 指定包含之前优化的文件的目录。 directory specifies the directory that contains the previously optimized files. –AssemblyList–AssemblyListFile 中指定的程序集是要使用导入文件中的数据进行优化的程序集的新版本。The assemblies specified in –AssemblyList or –AssemblyListFile are the new versions of the assemblies to be optimized using the data from the imported files. 通过使用早期版本的程序集中的优化数据,你可以优化较新版本的程序集,而无需重新运行该方案。Using optimization data from older version of assemblies enables you to optimize newer versions of assemblies without re-running the scenario. 但是,如果导入的程序集和目标程序集包含明显不同的代码,则优化数据将无效。However, if the imported and target assemblies include significantly different code, the optimization data will be ineffective. –AssemblyList–AssemblyListFile 中指定的程序集名称必须存在于 –Import directory 指定的目录中。 The assembly names specified in –AssemblyList or –AssemblyListFile must be present in the directory specified by –Importdirectory. 如果 directory 指定的路径包含空格,则应使用三组双引号将其引起来。 Use three sets of double quotation marks around directory if it specifies a path that includes spaces.

你必须指定 –Scenario–Import,但是不能同时指定这两个参数。You must specify either –Scenario or –Import, but not both parameters.
-OutDir <directory> -OutDir <directory> 用于放置优化过的程序集的目录。The directory in which to place the optimized assemblies. 如果某个程序集已经位于输出目录文件夹中,则将创建新副本并且向其名称追加索引号;例如:assemblyname-1.exe。 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. 如果 directory 指定的路径包含空格,则使用双引号将其引起来。 Use double quotation marks around directory if it specifies a path that contains spaces.
-AssemblyList <assembly1 assembly2 ...> -AssemblyList <assembly1 assembly2 ...>

- 或 -—or—

-AssemblyListFile <file> -AssemblyListFile <file>
你希望收集与其有关的配置文件信息的程序集(包括 .exe 和 .dll 文件)的列表,各程序集之用空格隔开。A list of assemblies (including .exe and .dll files), separated by spaces, that you want collect profile information about. 你可以指定 C:\Dir\*.dll*.dll 来选择指定工作目录或当前工作目录中的所有程序集。You can specify C:\Dir\*.dll or *.dll to select all the assemblies in the designated or current working directory. 有关详细信息,请参阅备注部分。See the Remarks section for more information.

- 或 -—or—

一个文本文件,其中包含你希望收集与其有关的配置文件信息的程序集的列表,每个程序集在单独的行上列出。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.
-AppID<appId> -AppID <appId> 指定包中的应用程序的 ID。The ID of the application in the specified package. 如果使用通配符 (*),则 Mpgo.exe 将尝试枚举包中的 AppID,如果失败,则将回退到 <package_family_name>!App。 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. 如果指定前缀为感叹号 (!) 的字符串,则 Mpgo.exe 会将包系列名称与提供的自变量连接。If you specify a string that is prefixed by an exclamation point (!), Mpgo.exe will concatenate the package family name with the argument provided.
-Timeout <seconds> -Timeout <seconds> 允许 Windows 8.x 应用商店应用在此应用退出前运行的时间。The amount of time to allow the Windows 8.x Store app to run before the app exits.
可选参数Optional parameter 说明Description
-64bit 为 64 位系统检测程序集。Instruments the assemblies for 64-bit systems. 你必须为 64 位程序集指定此参数,即使你的程序集声明为 64 位也是如此。You must specify this parameter for 64-bit assemblies, even if your assembly declares itself as 64 bit.
-ExeConfig <filename> -ExeConfig <filename> 指定你的方案用于提供版本和加载程序信息的配置文件。Specifies the configuration file that your scenario uses to provide version and loader information.
-f 强制在二进制程序集中包含配置文件数据,即使它已签名也是如此。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.
-Reset 重置环境以确保中止的分析会话不会影响你的程序集,然后退出。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.
-Timeout <time in seconds> -Timeout <time in seconds> 指定分析持续时间(以秒为单位)。Specifies the profiling duration in seconds. 使用稍高于你观察到的 GUI 应用程序启动时间的值。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.
-LeaveNativeImages 指定在运行方案后不应删除检测到的本机映像。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. 它将防止在后续运行 Mpgo.exe 时重新创建本机映像。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. 在这种情况下,请用同样的方案和程序集列表运行 Mpgo.exe,并使用 –RemoveNativeImages 参数来删除这些本机映像。In this case, run Mpgo.exe with the same scenario and assembly list and use the –RemoveNativeImages parameter to remove these native images.
-RemoveNativeImages 通过指定了 –LeaveNativeImages 的运行进行清理。Cleans up from a run where –LeaveNativeImages was specified. 如果指定 -RemoveNativeImages,则 Mpgo.exe 将忽略除 -64bit–AssemblyList 之外的所有参数,然后在删除检测到的所有本机映像后退出。If you specify -RemoveNativeImages, Mpgo.exe ignores any arguments except -64bit and –AssemblyList, and exits after removing all instrumented native images.


你可以在同一命令行上多次使用 –AssemblyList- AssemblyListFileYou can use both –AssemblyList and - AssemblyListFile multiple times on the command line.

如果在指定程序集时未指定完整路径名称,则 Mpgo.exe 将在当前目录中查找。If you do not specify full path names when specifying assemblies, Mpgo.exe looks in the current directory. 如果指定了不正确的路径,则 Mpgo.exe 将显示错误消息,但将继续为其他程序集生成数据。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.

仅建议大型托管应用程序使用 Ngen.exe 和 Mpgo.exe,因为预编译的本机映像的好处一般只能在它消除了大量运行时 JIT 编译工作的情况下才会显现出来。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. 在没有大量工作集的“Hello World”样式应用程序上运行 Mpgo.exe 将不会带来任何好处,并且 Mpgo.exe 甚至可能无法收集配置文件数据。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.


对于 ASP.NET 应用程序和 Windows Communication Foundation (WCF) 服务,不推荐 Ngen.exe 和 Mpgo.exe。Ngen.exe and Mpgo.exe are not recommended for ASP.NET applications and Windows Communication Foundation (WCF) services.

使用 Mpgo.exeTo Use Mpgo.exe

  1. 使用安装了 Visual Studio Ultimate 2012 和你的应用程序的计算机。Use a computer that has the Visual Studio Ultimate 2012 and your application installed.

  2. 以管理员身份用必需的参数运行 Mpgo.exe。Run Mpgo.exe as an administrator with the necessary parameters. 有关示例命令,请参见下一节。See the next section for sample commands.

    优化的中间语言 (IL) 程序集是在由 –OutDir 参数指定的文件夹(在此示例中,这是 C:\Optimized 文件夹)中创建的。The optimized intermediate language (IL) assemblies are created in the folder specified by the –OutDir parameter (in the examples, this is the C:\Optimized folder).

  3. 将用于 Ngen.exe 的 IL 程序集替换为包含 –OutDir 指定的目录中的配置文件信息的新 IL 程序集。Replace the IL assemblies you used for Ngen.exe with the new IL assemblies that contain the profile information from the directory specified by –OutDir.

  4. 应用程序安装程序(使用 Mpgo.exe 提供的映像)将安装优化的本机映像。The application setup (using the images provided by Mpgo.exe) will install optimized native images.

建议的工作流Suggested Workflow

  1. 通过将 Mpgo.exe 与 –Scenario 参数结合使用来创建一组优化的 IL 程序集。Create a set of optimized IL assemblies by using Mpgo.exe with the –Scenario parameter.

  2. 将优化的 IL 程序集签入到源代码管理中。Check the optimized IL assemblies into source control.

  3. 在生成过程中,使用 –Import 参数调用 Mpgo.exe 作为一个后期生成步骤来生成优化的 IL 映像以传递到 Ngen.exe。In the build process, call Mpgo.exe with the –Import parameter as a post-build step to generate optimized IL images to pass to Ngen.exe.

此过程确保所有程序集具有优化数据。This process ensures that all assemblies have optimization data. 如果更频繁地签入更新的优化程序集(步骤 1 和 2),则性能数字在整个产品开发中将更一致。If you check in updated optimized assemblies (steps 1 and 2) more frequently, the performance numbers will be more consistent throughout product development.

从 Visual Studio 使用 Mpgo.exeUsing Mpgo.exe from Visual Studio

可以从 Visual Studio 运行 Mpgo.exe(请参见文章如何:指定生成事件 (C#)),其具有下列限制:You can run Mpgo.exe from Visual Studio (see the article How to: Specify Build Events (C#)) with the following restrictions:

  • 你不能使用具有尾部反斜杠标记的引用路径,因为在默认情况下,Visual Studio 宏也使用尾部反斜杠标记。You cannot use quoted paths with trailing slash marks, because Visual Studio macros also use trailing slash marks by default. (例如,–OutDir "C:\Output Folder\" 无效。)若要解决此限制,你可以转义尾部斜杠。(For example, –OutDir "C:\Output Folder\" is invalid.) To work around this restriction, you can escape the trailing slash. (例如,请改用 -OutDir "$(OutDir)\"。)(For example, use -OutDir "$(OutDir)\" instead.)

  • 默认情况下,Mpgo.exe 不在 Visual Studio 生成路径中。By default, Mpgo.exe is not on the Visual Studio build path. 你必须添加 Visual Studio 的路径,或在 Mpgo 命令行上指定完整路径。You must either add the path to Visual Studio or specify the full path on the Mpgo command line. 你可在 Visual Studio 中的后期生成事件中使用 –Scenario–Import 参数。You can use either the –Scenario or the –Import parameter in the post-build event in Visual Studio. 但是,一般的处理方式是从 Visual Studio 开发人员命令提示符处使用一次 –Scenario,然后在每次生成后使用 –Import 更新优化的程序集,例如:"C:\Program Files\Microsoft Visual Studio 11.0\Team Tools\Performance Tools\mpgo.exe" -import "$(OutDir)tmp" -assemblylist "$(TargetPath)" -outdir "$(OutDir)\"However, the typical process is to use –Scenario one time from a Developer Command Prompt for Visual Studio, and then use –Import to 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)\".


以下来自 Visual Studio 开发人员命令提示处的 Mpgo.exe 命令将优化一个税务应用程序: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  

以下 Mpgo.exe 命令将优化一个声音应用程序: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  

以下 Mpgo.exe 命令将使用以前优化的程序集中的数据优化新版本的程序集: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  

请参阅See also