帮助你分析并发现程序运行的瓶颈,找到耗时所在,同时也能帮助你发现不会被执行的代码。从而最终实现程序的优化。
Profile的组成
Profile包括3个命令行工具:PREP,PROFILE,PLIST。可以以命令行方式运行Profile,其过程是:PREP读取应用程序的可执行文件并生成一个.PBI文件和一个.PBT文件;PROFILE根据.PBI文件,实际运行并分析程序,生成.PBO输出文件;PREP再根据.PBO文件和.PBT文件,生成新的.PBT文件;PLIST根据.PBT文件生成可阅读的输出。
Profile的具体功能
Function timing:对程序花费在执行特定函数上的时间进行评估。可以通过Profile对话框激活该功能。分析结果中,Func Time一栏以秒为单位记录了函数运行所花时间,下一栏显示了该函数时间占总运行时间的百分比;Func+Child Time栏记录了函数及其所调用的子函数运行所花的总时间,下一栏显示了前述时间占总运行时间的百分比;Hit Count栏记录函数被调用的次数;Function栏显示函数的名称。
Function coverage:记录特定函数是否被调用,可以用来确定代码中的未执行部分。可以通过Profile对话框激活该功能。分析结果列出所有被分析的函数,并使用*号标记执行过的函数。
Function counting:记录程序调用特定函数的次数。在Profile对话框中选择Custom,并在Custome Settings中指定fcount.bat(位于VC98/bin目录下)。需要注意的是,在指定fcount.bat所在目录时,最好不要用长文件名的方式,这样有可能出错,比如要将c:/Program Files写成c:/Progra~1。
Line counting:记录程序所执行的代码中特定行的次数。在Profile对话框中选择Custom,并在Custome Settings中指定lcount.bat(位于VC98/bin目录下)。该功能使用.EXE中的调试信息启动Profile,因此不需要.MAP文件。分析结果中,Line栏标示源代码的行号,Hit Count栏记录该行执行次数,下一栏显示了该行执行次数占所有代码行执行次数的百分比,Source Line显示了对应的源代码。
Line coverage:记录代码中的特定行是否被执行,可以用来确定代码中的未执行部分。可以通过Profile对话框激活该功能。分析结果列出所有被分析的代码行,并使用*号标记执行过的行。由于Line coverage只记录代码行是否被执行过,所以其执行开销要比Line counting小。
此外,Profile对话框还提供了Merge功能,用以把多次运行Profile之后的统计结果组合起来。如果你正在使用Function coverage功能,则会看到是否测试了所有函数;如果你正在使用Function timing功能,则会看到以往分析与本次分析所有合并运行的累计时间。
IDE环境下Profile的使用
对于涉及函数分析的功能
- 选择Project->Settings->Link,选择Enable profiling复选框
- 重建项目
- 选择Build->Profile,弹出Profile对话框
- 做必要设置后,选择OK,开始运行程序
对于涉及行分析的功能
- 选择Project->Settings->Link,选择Enable profiling复选框和Generate debug info复选框
- 选择Project->Settings->C/C++,选择Line Numbers Only
- 重建项目
- 选择Build->Profile,弹出Profile对话框
- 做必要设置后,选择OK,开始运行程序
配置Profile的三种方式
修改profiler.ini文件
profiler.ini位于VC98/bin目录下,在其[profiler]段中,你可以指定不参与分析的LIB文件或OBJ文件。比如:
[profiler]
exclude:user32.lib
exclude:gdi32.lib
在Profile对话框中指定选项
若你选择了Funciton timing、Function coverage或Line coverage选项,则你可以在Advanced settings中指定进一步的范围,比如:你希望Profile只分析SampleApp.cpp文件中特定范围内的代码,可以在Advanced settings中填入, /EXCALL /INC SampleApp.cpp(30-67) 。又如:你希望file1.obj和file2.obj不参与分析,则可以在Advanced settings中填入, /EXC file1.obj /EXC file2.obj 。再如:你希望只描述指定函数,则可以在Advanced settings中填入, /SF ?SampleFunc@@YAXPAH@@ ,紧跟SF参数的是特定函数的修饰符名,获取该名称的最简单的方式是在创建项目时生成的MAP文件中查找。
SF,EXCALL,EXC,INC都是PREP的命令行参数,有关其他参数的详细说明可以通过在命令行提示符输入PREP /H得到。
编写批命令文件
可以参考fcount.bat、fcover.bat、ftime.bat、lcount.bat以及lcover.bat
从Profile中输出数据
PLIST /T命令允许PLIST将.PBT文件内容以制表格式输出到文本文件中,该格式适合输入到电子表格或数据库中。比如:PLIST /T MYPROG > MYPROG.TXT,生成的MYPROG.TXT可以利用profiler.xlm(位于VC98/bin目录下)导入到Microsoft Excel电子表格中。
注意
通常,分析整个程序的意义不大,因为大多数Windows应用程序,主要时间花费在消息等待上,因此精确定位要分析的代码,可以加快Profile的执行速度,提高其分析准确度。在Profile执行期间尽量关闭其他不相干的应用程序。
若启用了远程调试,则不能够从Build菜单中调用Profile功能。
对于inline函数,编译器以实际代码替换函数调用,因此inline函数不生成.MAP文件或CALL指令,所以当执行这样的函数时,Profile将无法得知,花费时间、运行次数等数据都归属于调用该函数的函数。Profile可以提供有关inline函数的行一级的运行次数和覆盖信息。
对于多线程应用程序,Profile的行为取决于你所选择的分析方式,对于Line counting和Line coverage,Profile并未区分线程之间有何不同,它将包含当前运行的所有线程。对于Function timing、Function coverage和Function counting,分析结果取决于线程,你可以用以下方式分析一个独立线程:
- 将线程的主函数声明为初始函数(用PREP /SF选项)
- 包含程序中的所有函数(不要使用PREP /EXC选项)
否则,分析结果很难解释。
---------------------------------------------------------------------------------------
Profiler Errors PRF1005 through PRF4642
The following descriptions cover Profiler Errors PRF1005 through PRF4642:
Profiler Error PRF1005
fatal error -- common -- operating system ran out of memory
There is not enough memory to run this profiler module.
Profiler Error PRF1011
fatal error -- common -- cannot open file filename
The profiler module could not find the specified file.
Profiler Error PRF1012
fatal error -- common -- cannot read expected number of bytes from file filename
The profiler module could not read the expected number of bytes. This error is often caused by trying to profile an .EXE or .DLL file that was not linked with profiling enabled (/PROFILE linker command-line option).
Profiler Error PRF1013
fatal error -- common -- cannot write expected number of bytes to file filename
The profiler module could not write the expected number of bytes. Perhaps the disk is full or the file is read-only.
Profiler Error PRF1101
fatal error -- common -- missing option specifier
A / or - character was not followed by a command-line option specifier sequence. Check the command line.
Profiler Error PRF1102
fatal error -- common -- option: invalid option
An invalid option was found on the command line. Check the command line.
Profiler Error PRF1103
fatal error -- common -- option option requires an additional parameter.
This option was not followed by the correct number of parameters. Check the command line.
Profiler Error PRF1104
fatal error -- common -- option option requires a numeric parameter
A numeric parameter was expected for this option but was not found. Check the command line.
Profiler Error PRF1105
fatal error -- common -- option option specified more than number times
An option was specified too many times. Check the command line.
Profiler Error PRF1201
fatal error -- common -- missing filename after @
The @ character is supposed to be followed by a response filename. Check the command line.
Profiler Error PRF1202
fatal error -- common -- response files nested too deeply
Response files can be nested eight deep. Perhaps a response file calls itself recursively.
Profiler Error PRF1301
fatal error -- common -- file filename has an incorrect (old) signature
A PBI, PBO, PBT, _XE, or _LL file does not contain the correct header bytes. Perhaps the file is corrupted or was created by an old version of the profiler.
Profiler Error PRF1302
fatal error -- common -- PBI file filename has an incorrect (old) version number
A PBI file does not contain the correct version number. Perhaps the file is corrupted or was created by an old version of the profiler.
Profiler Error PRF1303
fatal error -- common -- PBT file filename has an incorrect (old) version number
A PBT file does not contain the correct version number. Perhaps the file is corrupted or was created by an old version of the profiler.
Profiler Error PRF1304
fatal error -- common -- PBT or PBO file filename conflicts with current profiling state
There was an attempt to merge a PBT or a PBO file into another PBT file with a different profiling mode selected.
Profiler Error PRF1306
fatal error -- common -- PBT or PBO file filename is not derived from same PBI file
The PBT, PBO, and PBI files are not properly synchronized. Run PREP Phase I again.
Profiler Error PRF1307
fatal error -- common -- CPU type of module module is different from initial one
CPU types for different modules don't match. All modules must be compiled for the same CPU type.
Profiler Error PRF1401
fatal error -- common -- filename: not a portable executable file
Function profiling only: The specified executable file is not a standard executable file for Windows NT 4.0 or Windows 2000. Perhaps it is a 16-bit executable for Windows 3.x.
Profiler Error PRF1421
fatal error -- PREP I -- no input specified
There were no input modules specified for PREP Phase I. Check the command line.
Profiler Error PRF1422
fatal error -- PREP I -- syntax error in /EXC or /INC specification: specification
The /EXC and /INC options require function names or filenames. This specification could not be decoded. Check the command line.
Profiler Error PRF1423
fatal error -- PREP I -- number of modules exceeds number
The maximum number of modules has been exceeded. Restructure the merge procedure to include more stages.
Profiler Error PRF1424
fatal error -- PREP I -- Debugging information in filename is not the right format for profiling, relink with /PROFILE
Could not use debuggine information stored in the Program Database. Debug information, to be used, must be stored in the executable in old-style format. Relink with the /PROFILE option to do this.
Profiler Error PRF1425
fatal error -- PREP I -- filename: invalid runtime relocation, try relink
The profiler found bad relocation information in the program to be profiled. This is usually caused by using an obsolete version of the linker. Relink your code with the new profiler and run PREP again.
Profiler Error PRF1426
A fixed image executable cannot be profiled, relink file without the /fixed switch.
This error can also be caused by omitting the /PROFILE option on the LINK command line.
Profiler Error PRF1427
Executable image too large to profile - try breaking it down into smaller DLLs.
The executable cannot be profile at its present size.
Profiler Error PRF1441
fatal error -- PROFILE -- filename: not a valid Win32s executable file
Win32s only. The Win32s extension could not load this file. Perhaps the file is corrupted, has not been prepared for profiling, or is not a graphical user interface application. If the file were a 16-bit executable, error message 1401 would be issued.
Profiler Error PRF1442
fatal error -- PROFILE -- timestamp of executable has changed since PREP was run
The PBI file was created from an older EXE file. This means that the processed executable file has changed since it was originally created by PREP Phase I. Run PREP Phase I again.
Profiler Error PRF1443
fatal error -- PROFILE -- cannot run program filename
The profiler could not load the EXE file. Check that the original EXE file does run under Windows NT 4.0 or Windows 2000.
Profiler Error PRF1444
this version of Windows does not support this manner of profiling
The function timing (/ft) option is not supported for this version of the Windows system.
Profiler Error PRF1445
Line count and line coverage are not supported on Win32S.
The Win32S version of the profiler does not support line count or line coverage profiling.
Profiler Error PRF1461
fatal error -- PREP II -- no PBT output file specified
Neither the /OT nor /M option was specified. Check the command line.
Profiler Error PRF1462
fatal error -- PREP II -- no PBT input file specified
Neither the /IT nor /M option was specified. Check the command line.
Profiler Error PRF1463
fatal error -- PREP II -- module: different clock/sampling frequency
The sampling frequency for this module is different from that of other modules that were profiled. Rerun PROFILE again for all modules, using the same sampling frequency.
Profiler Error PRF1465
fatal error -- PREP II -- CPU type in PBT or PBO file filename conflicts with current one
Occurs only during PREP merging. The CPU type does not match the CPU type used to generate the incoming PBT or PBO files. All profiler steps must be run on the same type of computer.
Profiler Error PRF1481
fatal error -- PLIST -- option inconsistent with sorting type
The options for PLIST depend on the kind of profiling being done. This error occurs, for example, when the Sort By Name option is specified for a line profiling run.
Profiler Error PRF1482
fatal error -- PREP or PROFILE -- abnormal termination in PREP or PROFILE detected, PLIST cannot continue
The files output by PREP or PROFILE were not valid because one of these utilities did not complete its task. This error is usually caused by an error during the execution of PREP or PROFILE, such as a missing .MAP file.
Profiler Error PRF1504
fatal error -- common -- illegal fixup address in mapfile filename
The mapfile lists all link-time fixups (including all the function call sites). This error indicates that one of the fixups was illegal, perhaps because the mapfile was not generated by the same link as the corresponding EXE (or DLL). This error is often preceded by the warning (PRF4528) about the EXE and mapfile timestamps being out of sync.
Profiler Error PRF1521
fatal error -- PREP I -- no functions were marked for profiling in module module
Function profiling only: at least one function must be included for profiling in each module. Check the /INC, /EXC, and /EXCALL options on the command line.
Profiler Error PRF1522
fatal error -- PREP I -- more than one function matched entry specification: funcname1 and funcname2
Function profiling only: there were two or more functions with matching names. Perhaps these are C++ overloaded functions. Use decorated names to specify the functions.
Profiler Error PRF1523
fatal error -- PREP I -- filename is not profiled, and can not import modified version version
For function-level profiles, PREP uses modified EXEs and DLLs, with modified names with extensions _XE and _LL. If any modules listed in the import table of a profiled module refer to another profiled module, but the imported module is itself not being profiled, then the import table of that imported module cannot be changed, and this error occurs. The solution is to include the offending module in the profile.
Profiler Error PRF1524
fatal error -- PREP I -- Debugging info in filename is not the right format for profiling, relink with /PROFILE
You must link executable files with profiling enabled (the /PROFILE linker option) to profile them. Change your linker options, rebuild, and re-run PREP.
Profiler Error PRF1542
fatal error -- PROFILE -- cannot set or delete environment variable var
Function profiling only: The specified environment variable was unavailable.
Profiler Error PRF1543
fatal error -- PROFILE -- NtQueryIntervalProfile: NT internal error number occurred
A Windows NT 4.0 or Windows 2000 error was detected.
Profiler Error PRF1544
fatal error -- PROFILE -- NtSetIntervalProfile: NT internal error number occurred
A Windows NT 4.0 or Windows 2000 error was detected.
Profiler Error PRF1545
fatal error -- PROFILE -- function sampling is no longer supported
Function sampling is not supported in this version. Use function timing to get more accurate results.
Profiler Error PRF1621
fatal error -- PREP I -- no debugging information in file filename
Line profiling only: debugging information must be included in the executable file. Run the compiler and linker again with the debugging options set.
Profiler Error PRF1622
fatal error -- PREP I -- no source lines marked for profiling in module module
Line profiling only: at least one line must be included for profiling in each module. Check the /INC, /EXC, and /EXCALL options on the command line.
Profiler Error PRF1641
fatal error -- PROFILE -- user-defined breakpoint found at address
Line profiling only: No breakpoints may be set in the executable modules being profiled. These are not breakpoints set by the debugger but rather embedded INT 3 instructions. This error is only fatal if the INT 3 instruction is encountered during the execution of your program.
Profiler Error PRF1642
fatal error -- PROFILE -- unexpected single-step exception occurred at address
Line profiling only: There was a problem with the executable file. Try relinking and rerunning PREP Phase I.
Profiler Error PRF1643
fatal error -- PROFILE -- an access violation occurred at address
Line profiling only: There was a problem with the executable file. Try relinking and rerunning PREP Phase I. Check the EXE file with the debugger.
Profiler Error PRF2541
error -- PROFILE -- cannot start sampler
Perhaps the executable program is too large.
Profiler Error PRF2542
error -- PROFILE -- cannot stop sampler
Perhaps the executable program is too large.
Profiler Error PRF4101
warning -- common -- option option overrides an earlier option
Two or more mutually exclusive switches were specified, or the same switch was specified more than once. The last switch takes precedence.
Profiler Error PRF4106
warning -- common -- function/source/object/lib name was never matched
The program element was not found. Make sure the name was spelled or decorated correctly.
Profiler Error PRF4107
warning -- common -- /SF option not valid for line-level or sample profiles
The start function option should be used only with function timing, function counting, or function coverage profiling.
Profiler Error PRF4111
warning -- common -- no error file (/E) specified for /A option
The /A option cannot be used without the /E option. The /A option is ignored and the error output sent to stdout.
Profiler Error PRF4421
warning -- PREP I -- an offset duplication was detected
There appears to be a problem with the (codeview) line number debug information. Perhaps there is an aliasing problem.
Profiler Error PRF4422
warning -- PREP I -- filename imports unknown module module
PREP searches for imported modules (mostly DLLs) in the following sequence:
- Directory PREP itself lives in
- Current directory
- Windows directories
- Directories on the path
Because the current directory depends on the profiler rather than on the profiled program, all modules might not be found. In most cases, this warning is not serious. However, if profiling leads to a GP fault (or to incorrect behavior), it might be necessary to include the "unknown module" in the profile.
Profiler Error PRF4423
warning -- PREP I -- filename: ignoring unknown runtime relocation type, verify linker version
(MIPS Specific) The profiler found bad relocation information in the program to be profiled. This is usually caused by using an obsolete version of the linker. Relink your code with the new profiler, and run PREP again.
Profiler Error PRF4481
warning -- PLIST -- clock frequency is zero
The clock frequency field in the PBT file has not been filled in yet, perhaps because the user has not done a profile yet, or has not merged his PBO file into his PBT file. Running PREP /M should fix the problem.
Profiler Error PRF4521
warning -- PREP I -- INIT environment variable not defined
The INIT environment variable should point to the directory that contains TOOLS.INI. Set the INIT environment variable to the directory containing your TOOLS.INI and rerun PREP.
Profiler Error PRF4522
warning -- PREP I -- 'publics' section not found in mapfile filename
The mapfile is corrupt.
Profiler Error PRF4523
warning -- PREP I -- 'static symbols' section not found in mapfile filename
The mapfile is corrupt.
Profiler Error PRF4524
warning -- PREP I -- entry function function not found
PREP could not find the specified start function (entry function) in the mapfile (see /SF switch). Perhaps the function name is misspelled.
Profiler Error PRF4525
warning -- PREP I -- cannot profile function function in file filename
A special function such as setjmp was specified.
Profiler Error PRF4526
warning -- PREP I -- module module already includes profiler DLL filename
The specified EXE or DLL already imports from PROFILER.DLL. The EXE (or DLL) may have been modified by PREP using the /OM switch and then renamed (from app._XE to app.EXE) before PREP was run again. Make sure that the module (EXE or DLL) specified is the original module and a module modified by PREP.
Profiler Error PRF4527
warning -- PREP I -- preferred load address not found in mapfile filename; assumed address
Current versions of the linker list the preferred load address (that is, when the EXE gets loaded at this address none of the run-time relocation addresses need to be fixed up). PREP needs this information. Somehow this information was missing from the mapfile. If the latest linker was used, the mapfile was probably edited. Regenerate the mapfile, retaining the line that states the preferred load address.
Profiler Error PRF4528
warning -- PREP I -- timestamps of mapfile filename and executable file do not match
The mapfile is not properly synchronized with the executable file. Relink and run PREP again.
Profiler Error PRF4541
warning -- PROFILE -- module not found in PBI file filename
An attempt was made to profile a program, but the EXE was not modified by PREP. This warning occurs while selectively profiling a DLL that is called by the EXE.
Profiler Error PRF4621
warning -- PREP I -- line number duplicated in file filename
There is a problem with the line number debug information. The same line number was identified with two different addresses. This is generally not serious.
Profiler Error PRF4641
warning -- PROFILE -- ReadProcessMemory: internal NT error number occurred
An internal error occurred. The profiler results may be meaningless.
Profiler Error PRF4642
warning -- PROFILE -- WriteProcessMemory: internal NT error number occurred
An internal error occurred. The profiler results may be meaningless.