Visual studio 2010 基于MSBuild的轮廓引导优化

Visual studio 2010 基于MSBuild的轮廓引导优化,visual-studio-2010,msbuild-4.0,Visual Studio 2010,Msbuild 4.0,如何使用PGI/PGO在MSBuild(仅限命令行)中构建解决方案,而不向项目添加新的构建配置? 我尝试添加命令行参数/属性:WholeProgramOptimization=PGInstrument,它看起来正常,它创建PGD文件,但在我运行应用程序后不创建PGC。很明显,我在MSBuild命令行中遗漏了一些内容。我花了两天时间对这个问题绞尽脑汁,终于找到了解决方法: 首先,您需要构建程序的插入指令的版本: msbuild.exe /t:Rebuild "Letter:\path\Yo

如何使用PGI/PGO在MSBuild(仅限命令行)中构建解决方案,而不向项目添加新的构建配置?
我尝试添加命令行参数/属性:WholeProgramOptimization=PGInstrument,它看起来正常,它创建PGD文件,但在我运行应用程序后不创建PGC。很明显,我在MSBuild命令行中遗漏了一些内容。

我花了两天时间对这个问题绞尽脑汁,终于找到了解决方法:

首先,您需要构建程序的插入指令的版本:

msbuild.exe
    /t:Rebuild "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGInstrument
Letter:\path\OutPutDir\YourProject.exe
msbuild.exe
    /t:LibLinkOnly "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGOptimize
    /p:LinkTimeCodeGeneration=PGOptimization
    /p:ProfileGuidedDatabase="Letter:\path\OutPutDir\YourProject.pgd"
请注意,在我的计算机上,可以在以下位置找到MSBuild:
C:\ProgramFiles(x86)\MSBuild\12.0\Bin\MSBuild.exe
。参数的顺序非常重要。如果项目放置在/p选项之后,则可能会覆盖这些选项

应在输出目录中创建pgd文件。稍后将需要它的路径

然后您需要插入程序:

msbuild.exe
    /t:Rebuild "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGInstrument
Letter:\path\OutPutDir\YourProject.exe
msbuild.exe
    /t:LibLinkOnly "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGOptimize
    /p:LinkTimeCodeGeneration=PGOptimization
    /p:ProfileGuidedDatabase="Letter:\path\OutPutDir\YourProject.pgd"
显然,在这一步中,您需要添加参数以向程序提供所需的数据。如果您的程序抱怨无法访问pgort120.dll,您可以在脚本中添加这样一行:
set PATH=%PATH%;C:\ProgramFiles(x86)\Microsoft Visual Studio 12.0\VC\bin\amd64

最后,您可以构建程序的优化版本:

msbuild.exe
    /t:Rebuild "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGInstrument
Letter:\path\OutPutDir\YourProject.exe
msbuild.exe
    /t:LibLinkOnly "Letter:\path\YourProject.vcxproj"
    /p:Configuration=Release
    /p:Platform=x64
    /p:WholeProgramOptimization=PGOptimize
    /p:LinkTimeCodeGeneration=PGOptimization
    /p:ProfileGuidedDatabase="Letter:\path\OutPutDir\YourProject.pgd"
这里您需要使用第一步的pgd文件的地址。请注意,目标是LibLinkOnly,因为不需要重新编译所有内容


希望这对其他人有所帮助。

基于Arnaud的答案,如果没有Arnaud,我永远也不会明白这一点,还有一些其他设置可能会很有用。在我的解决方案中,我只想使用PGO在20+个项目中构建一个项目,这需要额外的标志来阻止构建所有引用的项目:

/p:BuildProjectReferences=false
使用此选项可停止重建目标正在生成的目标项目引用的项目。您需要将其添加到/t:Rebuild命令中,并从VS16.8.0开始添加到/t:LibLinkOnly命令中

在VS2019中,您会发现pgort140.dll隐藏在以下文件中: Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.21.27702\bin\HostxNN\xNN 文件夹,其中xNN是x86或x64(视情况而定)

在解决方案的情况下,可能会有一个$(OutDir)输出位置,该位置取决于解决方案设置,但当您构建一个单独的项目时,结果出现在错误的位置,因此您可能都需要告诉项目文件输出的位置:

/p:OutDir="path to output"
在我的例子中,由于一个解决方案文件包含许多项目,并且我需要使用PGO运行一个解决方案文件,我最终得到了一个批处理文件(见下文)。在这种情况下,在顶层I 有一个解决方案文件MyApp.sln,其中包含子文件夹中的多个子项目。这个 我想要的一个名为subproj,它与一个项目文件一起位于subprob文件夹中 在该文件夹中调用subproj.vcxproj。每个子项目生成一个DLL,并在其中 是subproj对其他子项目的依赖关系。批处理文件的要点是 详情如下:

REM Get the folder we are running out of, assumed to hold the solution file
SET parent=%~dp0

REM Set PLATFORM=x64, TOOLS=HOSTx64\x64 for 64-bits
SET PLATFORM=Win32
SET TOOLS=Hostx86\x86

ECHO Build %PLATFORM% Release of MyApp with PGO of the subprog project
REM Expanding Program Files (x86) causes problems so use PROGRA~2
SET VSPATH=C:\PROGRA~2\Microsoft Visual Studio\2019\Professional
REM Value for VS 16.?.? = \14.21.27702
REM Value for VS 16.3.2 = \14.22.27905\bin
REM Value for VS 16.3.3 = \14.23.28105\bin
REM Value for VS 16.4.0 = \14.24.28314\bin
REM Value for VS 16.5.0 = \14.25.28610
REM Value for VS 16.6.0 = \14.26.28801
REM Value for VS 16.7.0 = \14.27.29110
REM Value for VS 16.8.0 = \14.28.29333
SET VSTOOLS=%VSPATH%\VC\Tools\MSVC\14.21.278.29333\bin
if not exist "%VSTOOLS%" (
ECHO %VSTOOLS% Was not found. This is probably due to a new release. Please
ECHO find the new location and correct this batch file:
ECHO %parent%%me%.bat
exit /b 1
)

REM Set tools path (needed to locate the pgoNnn.dll used for PGO)
set PATH=%PATH%;%VSTOOLS%\%TOOLS%

REM MSB is the path to the MSBuild.exe command
SET MSB="%VSPATH%\MSBuild\Current\Bin\MSBuild.exe"

REM OPT is the common options shared by everything
REM /v: n=normal, m=minimal, q=quiet
SET OPT=/v:m /p:Configuration=Release /p:Platform=%PLATFORM%

REM Set where our output must go. VS likes it to end with \ for $(OutDir)
SET OUTDIR=%parent%%PLATFORM%\Release\

REM It is easier to build everything and rebuild subproj that build all the
REM sub-projects separately.
echo Build the entire solution in the Release build for the desired platform.
%MSB% MyApp.sln %OPT%

echo Now instrument the subproj
%MSB% /t:Rebuild "subproj\subproj.vcxproj" %OPT% /p:OutDir=%OUTDIR% /p:WholeProgramOptimization=PGInstrument /p:BuildProjectReferences=false

echo Run MyApp to exercise the subproj DLL as needed to generate the PGO database
%parent%%PLATFORM%\Release\MyApp.exe arguments as required...

echo Now build PGO optimized version of subproj
%MSB% /t:LibLinkOnly "subproj\subproj.vcxproj" %OPT% /p:WholeProgramOptimization=PGOptimize /p:LinkTimeCodeGeneration=PGOptimization /p:OutDir=%OUTDIR% /p:ProfileGuidedDatabase="%OUTDIR%compiler.pgd"  /p:BuildProjectReferences=false

可疑的您是否已检查该程序是否具有对该目录的写入权限?好的,因为它是在LocalSystem帐户下运行的IIS池,并且在该特定文件夹中写入可能存在问题,我已添加具有完全控制权限的所有人。还是不行。汉斯,进一步调查表明你是完全正确的。此计算机上的权限有问题。当我将这些二进制文件复制到本地机器时,一切正常。非常感谢。