Mpgo.exe (Managed 特性指引最佳化工具)Mpgo.exe (Managed Profile Guided Optimization Tool)

Managed 特性指引最佳化工具 (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.

當 Intermediate Language (IL) 組件的啟動時間和工作組大小發生效能問題時,建議您先使用 Ngen.exe 消除 Just-in-Time (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:

語法Syntax

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

參數Parameters

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>

-或--or-

-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. 如果 「命令」 指定的路徑包含空格,請在前後加上三組雙引號。例如: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". 不使用雙引號。如果 「命令」 包含空格就無法正確運作。Do not use double quotation marks; they will not work correctly if command includes spaces.

-或--or-

若為 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 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目錄 所指定的目錄中。The assembly names specified in –AssemblyList or –AssemblyListFile must be present in the directory specified by –Importdirectory. 如果 「目錄」 指定的路徑包含空格,請在前後加上三組雙引號。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. 如果輸出目錄資料夾中已有組件,則會建立新的複本,並將索引編號附加至其名稱,例如: 組件名稱-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. 如果 「目錄」 指定的路徑包含空格,請在前後加上雙引號。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> !應用程式失敗時。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.

備註Remarks

您可以在命令列上多次使用 –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.

建議您只對大型 Managed 應用程式使用 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.

注意

不建議您將 Ngen.exe 和 Mpgo.exe 用於 ASP.NET 應用程式和 Windows Communication Foundation (WCF) 服務。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 組件取代為新的 IL 組件,該組件包含 –OutDir 所指定之目錄中的設定檔資訊。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. 在建置流程中,呼叫 Mpgo.exe 並使用 –Import 參數做為建置後步驟,以產生要傳遞至 Ngen.exe 的最佳化 IL 映像。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\" 無效。 ) 若要解決這項限制,您可以將尾端斜線 escape。(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)\".

範例Examples

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