What is Profiling?
Profiling is a technique of investigating the programs behavior and estimating the amount of time spent in various sections of a program.
Profilers provide vital performance statistics such as time taken in each function, functions trace, statistics on number of times each call executed, usage of memory, potential memory leaks and uninitialized memory access information. These are also referred to as software profilers.
There are various profilers available with different functionalities like – CPU profiling, memory profiling. Profilers can profile code written in different languages like C/C++, Java etc. On database end, we have profilers which provide the path taken by a query on the database and related statistics.
Types of Profiling?
•There are a variety of types of profilers
•Sampling
•Tracing
•Instrumenting
•While choosing the correct profiler there is usually a trade-off in terms of:
•accuracy
•speed
•granularity of information
•intrusiveness
Sampling
In this mode, the profiler stops periodically every thread of a application and inspects method frames on its call stacks. The output snapshot is not an exact representation of the runtime conditions rather a statistical approximation, but the profiling overhead is very low and the application runs almost at full speed with minimum side effects, such as additional memory allocations, cache faults and context switches. The profiling overhead in the sampling mode does not depend on the number of methods called by the application opposed to other profiling modes.
The profiler cannot count the number of method calls. It can only count their occurrence on top of a stack. Method durations are computed purely based on gathered statistics. No source code or a binary alternation is required. The runtime ability to stop execution to do a call stack snapshot or alternatively an interrupt instruction can be used.
Tracing Profilers
This way of profiling relies on a runtime environment or some kind of hardware notification. The notification is performed when a method is entered or left. This method leads to accurate results that provide exact count of method calls and their durations.
However, there is performance overhead. In addition, the overhead increases with the number of method calls and slows down the application.
Similar to the sampling profiling, there are no changes to code or binaries required. The profiler has to only register callback methods for the entry/leave notifications with the runtime or the hardware profiling infrastructure.
Instrumentation Profilers
Code or binaries of the application are modified by injection of arbitrary code or instructions in order to collect profiling data. This approach can have similar effects and results as the tracing profiling, but offers a lot more choice of what and how to profile. There are chances of alternation of the original application behavior or even introducing bugs.
Instrumentation can be performed on every level of the software live-time (source, compilation, binary, runtime) and can be both manual, performed by a developer, as well as automatic, done by a compiler or a runtime environment
Types of profiler (based on output)
•Flat profiler computes the average call times, from the calls and don‘t compute call times w.r.t callee. It only shows the time spent executing individual functions.
•Call-Graph profiler shows functions frequencies, call times and also the call chains based on the callee. JProbe can be an example for call-graph profiler in J2EE technology whereas PurifyPlus can be considered for C/C++ technology.
•Memory profiler shows the usage of memory, object allocation with size and memory leak related statistics during program execution. JProbe is an example for memory profiler in J2EE technology whereas PurifyPlus is an example in C/C++ technology.
Granularity of profiling
•Line-by-line granularity
Line-by-line profiling is the most accurate method and most demanding. Every program statement is measured and analyzed. It brings precise result; however, the additional burden can alter the program's behavior. Such granularity can be achieved by instrumentation.
•Method granularity
Only information regarding an entire method run is collected. The lower overhead can improve performance of the application and still provide very informative results.
This level of granularity is the only option for the sampling profilers and most types of the tracing profilers. The instrumentation profilers can operate in the method granularity mode
•Selective granularity
The profiled process can be filtered with combined levels of granularity, where some parts of code are monitored on the line-by-line level and others only on the method level or not at all in order to capture desired statistics with lowest possible overhead and highest possible accuracy.
Sample Outputs
Cpu Profiling
Memory Profling