Home |
RTKernel-32 Programming Manual Multitasking, Real-Time, and RTKernel-32 Alternate APIs for RTKernel-32 Module RTProf |
Module RTProfThe RTKernel-32 Profiler can be used to gather statistics about how much time a program spends in which parts of the code. The profiler generates hardware interrupts at a high rate. In the interrupt handler, the profiler determines which code sequence was executing when the interrupt occurred. If a source file line is associated with that code, a counter associated with that line is incremented; otherwise, the increment is applied to the line containing the function call which invoked the interrupted code. Code that takes more time to execute or is executed more frequently than other code will receive more increments. With a sufficiently high sample (for example, 100000 and more), the number of counts per line is proportional to the amount of time a program has spent executing that line. When the profiler is unable to determine the source code from which a code sequence was generated (i.e., no debug symbols are available for that code sequence) or RTPStart flag RTP_FUNCTION_CALLS has been specified, it must follow the interrupted task's stack frames to find a function call for which source information is available. However, this is only possible if the code contains standard EBP stack frames. Many compilers like to optimize such stack frames away. Therefore, it is necessary to compile code to be profiled with optimizations disabled, or at least with compiler option "frame pointer omission" disabled. With Microsoft Visual C++, this is achieved with option -Oy- or in the source code with #pragma optimize("y", off). With Borland compilers, the required compiler option is -k+ or #pragma option -k+. When the profiler cannot find any source code information on the complete call stack of a task, the interrupted task's counter is incremented instead. Such a counter is maintained for each task in the system and is returned by RTPResult with parameter *LineNumber set to zero. To be able to associate code sequences with source files and line numbers, the program must load a symbol table using function RTKLoadSymbols (typically a .tds or .map file). The profiler can then be started by calling RTPStart, RTPStartRTC, RTPStartTimer, or RTPStartCOM. Which RTPStart... function to call depends on which hardware should be used to generate interrupts. For best results, some hardware not required by the program should be used. For example RTPStartCOM uses a free COM port in loop-back mode to generate interrupts. Function RTPStop stops the profiler. Function RTPResult can then be used to read the gathered statistics. Example: 51816 total samples 21386 41.2% Profdemo.cpp.173 15108 29.1% Profdemo.cpp.172 7837 15.1% Profdemo.cpp.175 5968 11.5% Main Task 1515 2.9% Profdemo.cpp.176 All API functions of the profiler are declared in header file Rtprof.h and are documented in the RTKernel-32 Reference Manual. Demo program Profdemo shows how the profiler can be used.
|