Perl Profiling API – implementation

By Stephen Kellett
6 August, 2011

To enable us to create our Perl code coverage, Perl flow tracing and Perl profiling tools we have modified the Perl runtime to include a Perl Profiling API.

The Perl Profiling API is simple to use, implemented in one source file and one header file and only requires modifications in two places in the entire Perl source code base.

The Perl Profiling API allows the following activities to be monitored:

  • Function entry
  • Function exit
  • Line visits

Profiling overhead

Due to the simplicity of design the overhead of using the Perl Profiling API is determined by the code you put into your profiling callback.
When not in use the Perl Profiling API does not cause a normal Perl program to run any slower than if the Perl Profiling API were not present.

Why create the Perl Profiling API?

The Perl runtime already has a built in profiling feature that allows you to gather some performance statistics about your Perl program. So why create a dedicated profiling API?

The reason for this is because the built in profiler only gathers and displays the statistics the original authors decided were interesting. Those statistics are not necessarily
what other users of Perl find interesting. Additionally these statistics are aggregated and prevent you from determining call-trees and call-graphs, code coverage and call history
statistics.

By providing a dedicated API for monitoring function entry, function exit and line visits the user of the Perl Profiling API can decide what they wish to do with the collected
profiling information and process it as they see fit. You can use this information to build a simple flat profiler similar to the built in profiler, or to build a call tree profiler,
a code coverage tool, a flow tracer, or some other data monitoring tool. They key thing is this is a flexible solution, allowing a variety of implementations.

The API

The Perl Profiling API is intended to be called using a C calling convention. Thus it can easily be called from C or C++ and with a few changes from Delphi.

The Perl Profiling API implements the following functions

  • void Perl_set_do_profiling(int enable);
  • int Perl_get_do_profiling();
  • void Perl_set_coverage_callback(PERL_PROFILER_CALLBACK callback, void *userData);
  • void Perl_set_profiling_callback(PERL_PROFILER_CALLBACK callback, void *userData);

Source Code and Binaries

You can download the source code and a replacement perl512.dll from Github.

 

Fully functional, free for 30 days