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

Perl 5.12.3

Original Perl source code from CPAN.

Modified Perl source code (just the changes, not all files).

Modified files:

  • run.c
  • dump.c
  • Makefile – makefile for use with Visual Studio nmake found in the win32 directory.

Additional files:

  • profilingapi.h
  • profilingapi.c
  • perlProfilingModifications.txt – a description of the changes made and how to build.

Binaries

Replacement perl512.dll for Perl 5.12.3.

This is functionally the same as the original Perl512.dll with the addition of the Perl Profiling API. No other modifications to the DLL have been made.

To install, simply copy this DLL over the top of your existing Perl512.dll in your Perl 5.12.3 directory.

Warning
If you are using a different version of Perl, copying the DLL over may not work as the Perl internals may have changed. If this is the case you have two options:

  • Modify the Perl source using our changes (easy) and build it yourself.
  • Contact us with full details of which Perl distribution you are trying to work with.

Fully functional, free for 30 days