profiler  0.0
molpro::profiler::Profiler Class Reference

Instrumental profiler for timing sections of code. More...

#include <Profiler.h>

Classes

struct  Proxy
 Proxy object that calls start() on creation and stop() on destruction. More...
 

Public Member Functions

 Profiler (std::string description_, bool with_wall=true, bool with_cpu=false)
 Construct profiler and start timing. More...
 
 Profiler (Profiler &&)=default
 
Profileroperator= (Profiler &&)=default
 
 ~Profiler ()
 
 Profiler ()=delete
 
 Profiler (const Profiler &)=delete
 
Profileroperator= (const Profiler &)=delete
 
const std::string & description () const
 
int get_max_depth () const
 Get the maximum depth the profiler tree is allowed to reach. More...
 
void set_max_depth (int new_max_depth)
 Set the maximum depth the profiler tree is allowed to reach. More...
 
int get_current_depth () const
 
Profilerstart (const std::string &name)
 Traverse down to a child node and start timing. More...
 
Profilerstop (const std::string &name="")
 Stop the active node and traverse up to its parent. More...
 
Profilerstop_all ()
 Stop all nodes and traverse up to the root. More...
 
profiler::Countercounter ()
 Access counter at the top of the call stack. More...
 
Profilerreset (const std::string &name)
 Erases all data and starts from root again. More...
 
void operator+= (size_t operations)
 Advance the counter holding the notional number of operations executed in the code segment. More...
 
void operator++ ()
 Advance the counter holding the notional number of operations executed in the code segment. More...
 
size_t operator++ (int)
 
Proxy push (const std::string &name)
 Returns a proxy object which will start() on construction and stop on destruction. More...
 
std::string str (bool cumulative=true, profiler::SortBy sort_by=profiler::SortBy::wall) const
 
std::string dotgraph (std::string path, double threshold=0.01, bool cumulative=true, int hot[3]=hot_default, int cool[3]=cool_default, SortBy sort_by=profiler::SortBy::none, std::vector< std::pair< double, double >> heat_adjust=default_heat_adjust, bool get_percentage_time=false)
 Get a graphviz .dot markup file for a profile. More...
 

Static Public Member Functions

static std::shared_ptr< Profilersingle (const std::string &description_, bool with_wall=true, bool with_cpu=false)
 
static std::shared_ptr< Profilersingle ()
 Access the last registered Profiler. More...
 
static void erase (const std::string &description)
 Remove Profiler with specified description from the singleton register. More...
 

Public Attributes

std::shared_ptr< profiler::Node< profiler::Counter > > root
 root node of the profiler call tree More...
 
std::shared_ptr< profiler::Node< profiler::Counter > > active_node
 the most recent active node. More...
 

Protected Attributes

std::string m_description
 name of the root node More...
 
std::string m_root_name = "All"
 name of the root node More...
 
int m_max_depth
 max depth level of profiler tree counting root as 0. Defaults to largest possible value. More...
 
int m_current_depth = 0
 current depth of the active node More...
 

Friends

std::ostream & operator<< (std::ostream &os, const Profiler &obj)
 

Detailed Description

Instrumental profiler for timing sections of code.

Profiler constructs and navigates the call tree using start() and stop() to move between nodes or create them on first run. The profiler uses wall clock by default, but could also use cpu time, or do no timing at all and simply accumulate number of calls to each node. The maximum depth of profiler tree can be set using set_max_depth(), any nodes below max_depth are not created. This allow for profiler calls to be used in Production code without degrading the performance

Note
See README.md and examples for different ways to use the Profiler, and see report() on how the results can we reported.

Constructor & Destructor Documentation

◆ Profiler() [1/4]

molpro::profiler::Profiler::Profiler ( std::string  description_,
bool  with_wall = true,
bool  with_cpu = false 
)
explicit

Construct profiler and start timing.

Parameters
description_description of profiler
with_wallwhether to include wall time
with_cpuwhether to include cpu time

◆ Profiler() [2/4]

molpro::profiler::Profiler::Profiler ( Profiler &&  )
default

◆ ~Profiler()

molpro::profiler::Profiler::~Profiler ( )

◆ Profiler() [3/4]

molpro::profiler::Profiler::Profiler ( )
delete

◆ Profiler() [4/4]

molpro::profiler::Profiler::Profiler ( const Profiler )
delete

Member Function Documentation

◆ counter()

Counter & molpro::profiler::Profiler::counter ( )

Access counter at the top of the call stack.

◆ description()

const std::string& molpro::profiler::Profiler::description ( ) const
inline

◆ dotgraph()

std::string molpro::profiler::Profiler::dotgraph ( std::string  path,
double  threshold = 0.01,
bool  cumulative = true,
int  hot[3] = hot_default,
int  cool[3] = cool_default,
SortBy  sort_by = profiler::SortBy::none,
std::vector< std::pair< double, double >>  heat_adjust = default_heat_adjust,
bool  get_percentage_time = false 
)

Get a graphviz .dot markup file for a profile.

Parameters
pathpath to the file to be written to disk. This should be an absolute path, as the working directory may be a temporary folder.
threshold- ratio of the time spent in one node to the whole runtime. If the time spent in that node is less that the threshold, the node is hidden.
hotthe RGB values (0-255) of the 'hot' colour (a ratio of 1 will produce the hot colour, 0 the cool)
coolthe RGB values (0-255) of the 'cool' colour.
cumulativewhether to print the cumulative time, or to subtract the time spent in the children from a node's timings.
sort_bywhether to sort by wall clock time, calls, frequency, etc. Currently unused!
heat_adjustcurrently unused, will eventually be used to establish non-linear mappings between ratio and colour.
get_percentage_timewhether to show the time as a percentage of the runtime (true) or in seconds (false).
Returns
a string containing the dotgraph. Using this string is optional as the dotgraph is already written to path.

◆ erase()

void molpro::profiler::Profiler::erase ( const std::string &  description)
static

Remove Profiler with specified description from the singleton register.

◆ get_current_depth()

int molpro::profiler::Profiler::get_current_depth ( ) const

Get the current depth of the call stack from root to the active node. This tracks virtual nodes beyond max_depth, even though they are not constructed.

◆ get_max_depth()

int molpro::profiler::Profiler::get_max_depth ( ) const

Get the maximum depth the profiler tree is allowed to reach.

Any calls to start() that would lead to profiler tree growing above max depth do nothing. The corresponding stop() still needs to be posted, since the virtual depth of the tree is still being tracked.

◆ operator++() [1/2]

void molpro::profiler::Profiler::operator++ ( )

Advance the counter holding the notional number of operations executed in the code segment.

◆ operator++() [2/2]

size_t molpro::profiler::Profiler::operator++ ( int  )

◆ operator+=()

void molpro::profiler::Profiler::operator+= ( size_t  operations)

Advance the counter holding the notional number of operations executed in the code segment.

Parameters
operationsThe number of additional operations.

◆ operator=() [1/2]

Profiler& molpro::profiler::Profiler::operator= ( const Profiler )
delete

◆ operator=() [2/2]

Profiler& molpro::profiler::Profiler::operator= ( Profiler &&  )
default

◆ push()

Proxy molpro::profiler::Profiler::push ( const std::string &  name)
inline

Returns a proxy object which will start() on construction and stop on destruction.

◆ reset()

Profiler & molpro::profiler::Profiler::reset ( const std::string &  name)

Erases all data and starts from root again.

◆ set_max_depth()

void molpro::profiler::Profiler::set_max_depth ( int  new_max_depth)

Set the maximum depth the profiler tree is allowed to reach.

◆ single() [1/2]

std::shared_ptr< Profiler > molpro::profiler::Profiler::single ( )
static

Access the last registered Profiler.

◆ single() [2/2]

std::shared_ptr< Profiler > molpro::profiler::Profiler::single ( const std::string &  description_,
bool  with_wall = true,
bool  with_cpu = false 
)
static

Access a Profiler registered with the WeakSingleton creating a new one if a Profiler instance with that description does not exist

◆ start()

Profiler & molpro::profiler::Profiler::start ( const std::string &  name)

Traverse down to a child node and start timing.

Parameters
namename of the child node
Returns

◆ stop()

Profiler & molpro::profiler::Profiler::stop ( const std::string &  name = "")

Stop the active node and traverse up to its parent.

◆ stop_all()

Profiler & molpro::profiler::Profiler::stop_all ( )

Stop all nodes and traverse up to the root.

◆ str()

std::string molpro::profiler::Profiler::str ( bool  cumulative = true,
profiler::SortBy  sort_by = profiler::SortBy::wall 
) const

Produce a report on profiler as a string

Parameters
cumulativewhether to use cumulative times or subtract the time spend by children

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Profiler obj 
)
friend

Member Data Documentation

◆ active_node

std::shared_ptr<profiler::Node<profiler::Counter> > molpro::profiler::Profiler::active_node

the most recent active node.

◆ m_current_depth

int molpro::profiler::Profiler::m_current_depth = 0
protected

current depth of the active node

◆ m_description

std::string molpro::profiler::Profiler::m_description
protected

name of the root node

◆ m_max_depth

int molpro::profiler::Profiler::m_max_depth
protected

max depth level of profiler tree counting root as 0. Defaults to largest possible value.

◆ m_root_name

std::string molpro::profiler::Profiler::m_root_name = "All"
protected

name of the root node

◆ root

std::shared_ptr<profiler::Node<profiler::Counter> > molpro::profiler::Profiler::root

root node of the profiler call tree