Skip to content

kode9/function_profiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

function_profiler: Per-thread c++11 function profiler

Introduction

function_profiler is a simple c++ header only library to profile function calls.

It is an implementation of an idea of Jeff Preshing in his blog.

It allows you to measure the number of calls and the time spent in chosen functions, on a per-thread basis.

It is not a replacement for full-fledged profiling frameworks such as GNU gprof or Linux perf, but much easier to use.

It does not collect statistical data on all functions, but rather time specific functions. One downside is that it is intrusive. This means you must alterate your source code to use it. Fortunately, all you need is to include a header and add a single line in the functions you want to profile.

Usage

Basic usage

To start profiling functions, drop the single header where your compiler can find it, include it in your source and add call the FUNCTION_PROFILE macro in any function:

#include "function_profiler.hpp"
// ...

void function()
{
  PROFILE_FUNCTION()
  // ...
}

After a profiled function call it will print a report if enough time has elapsed since the last report:

[FP] 7fb2a5dfd700 function #40960    1.06712  0.01157  43709.3171    473.9214

The report has the following structure:

[FP] <thread> <function_name> <number_of_calls> <steady_average> <cpu_average> <steady_total> <cpu_total>

Controlling behavior

To enable profiling, add the preprocessor definition FP_ENABLE. If it is not defined, the FUNCTION_PROFILE will not do anything.

Implementation

The idea relies on two simple things:

The PROFILE_FUNCTION macro only instantiate two objects.

First a data structure with the thread_local storage specifier, accountable for collecting statistics.

The second one is a local structure whose sole purpose is to update the statistics upon destruction.

The timing are done using boost::chrono library. Although the standard now has a std::chrono module, boost provides an additional thread_clock function that gives CPU time for the calling thread.

Dependencies

  • A C++11 compiler
  • boost::chrono
  • CMake >= 3.1 (optional)

TODO

  • Add option to disable thread_lock and use std::steady_clock to remove boost dependency.
  • Implement thread_clock to allow thread_clock without boost
  • Support different logging output (stderr, stream, file, ...)
  • Allow paremetrisation of logging rate (every N calls, every N seconds, manually, ...)
  • Change name generation to something more specific (like adding line number) too differentiate same function / different scope calls
  • README: scoped profiling

Licence

This software is licenced under the Apache License, Version 2.0