libMesh
Public Types | Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
libMesh::PerfLog Class Reference

The PerfLog class allows monitoring of specific events. More...

#include <perf_log.h>

Public Types

typedef std::map< std::pair< const char *, const char * >, PerfDatalog_type
 Typdef for the underlying logging data structure. More...
 

Public Member Functions

 PerfLog (const std::string &label_name="", const bool log_events=true)
 Constructor. More...
 
 ~PerfLog ()
 Destructor. More...
 
void clear ()
 Clears all the internal data and restores the data structures to a pristine state. More...
 
void disable_logging ()
 Disables performance logging for an active object. More...
 
void enable_logging ()
 Enables performance logging for an active object. More...
 
bool logging_enabled () const
 
void fast_push (const char *label, const char *header="")
 Push the event label onto the stack, pausing any active event. More...
 
void push (const char *label, const char *header="")
 Push the event label onto the stack, pausing any active event. More...
 
void push (const std::string &label, const std::string &header="")
 Push the event label onto the stack, pausing any active event. More...
 
void fast_pop (const char *label, const char *header="")
 Pop the event label off the stack, resuming any lower event. More...
 
void pop (const char *label, const char *header="")
 Pop the event label off the stack, resuming any lower event. More...
 
void pop (const std::string &label, const std::string &header="")
 Pop the event label off the stack, resuming any lower event. More...
 
void start_event (const std::string &label, const std::string &header="")
 Start monitoring the event named label. More...
 
void stop_event (const std::string &label, const std::string &header="")
 Stop monitoring the event named label. More...
 
void pause_event (const std::string &label, const std::string &header="")
 Suspend monitoring of the event. More...
 
void restart_event (const std::string &label, const std::string &header="")
 Restart monitoring the event. More...
 
std::string get_log () const
 
std::string get_info_header () const
 
std::string get_perf_info () const
 
void print_log () const
 Print the log. More...
 
double get_elapsed_time () const
 
double get_active_time () const
 
PerfData get_perf_data (const std::string &label, const std::string &header="")
 Return the PerfData object associated with a label and header. More...
 
const log_typeget_log_raw () const
 

Private Member Functions

void split_on_whitespace (const std::string &input, std::vector< std::string > &output) const
 Splits a string on whitespace into a vector of separate strings. More...
 

Private Attributes

const std::string label_name
 The label for this object. More...
 
bool log_events
 Flag to optionally disable all logging. More...
 
double total_time
 The total running time for recorded events. More...
 
struct timeval tstart
 The time we were constructed or last cleared. More...
 
log_type log
 The actual log. More...
 
std::stack< PerfData * > log_stack
 A stack to hold the current performance log trace. More...
 
std::map< std::string, const char * > non_temporary_strings
 Workaround to give us fixed pointers to character arrays for every string. More...
 

Static Private Attributes

static bool called
 Flag indicating if print_log() has been called. More...
 

Detailed Description

The PerfLog class allows monitoring of specific events.

An event is defined by a unique string that functions as a label. Each time the event is executed data are recorded. This class is particularly useful for finding performance bottlenecks.

Author
Benjamin Kirk
Date
2003 Responsible for timing and summarizing events.

Definition at line 125 of file perf_log.h.

Member Typedef Documentation

◆ log_type

typedef std::map<std::pair<const char *, const char *>, PerfData> libMesh::PerfLog::log_type

Typdef for the underlying logging data structure.

Definition at line 295 of file perf_log.h.

Constructor & Destructor Documentation

◆ PerfLog()

libMesh::PerfLog::PerfLog ( const std::string &  label_name = "",
const bool  log_events = true 
)

Constructor.

label_name is the name of the object, which will bw print in the log to distinguish it from other objects. log_events is a flag to optionally disable logging. You can use this flag to turn off logging without touching any other code.

◆ ~PerfLog()

libMesh::PerfLog::~PerfLog ( )

Destructor.

Calls clear() and print_log().

Member Function Documentation

◆ clear()

void libMesh::PerfLog::clear ( )

Clears all the internal data and restores the data structures to a pristine state.

This function checks to see if it is currently monitoring any events, and if so errors. Be sure you are not logging any events when you call this function.

◆ disable_logging()

void libMesh::PerfLog::disable_logging ( )

Disables performance logging for an active object.

Definition at line 156 of file perf_log.h.

References log_events.

156 { log_events = false; }
bool log_events
Flag to optionally disable all logging.
Definition: perf_log.h:323

◆ enable_logging()

void libMesh::PerfLog::enable_logging ( )

Enables performance logging for an active object.

Definition at line 161 of file perf_log.h.

References log_events.

161 { log_events = true; }
bool log_events
Flag to optionally disable all logging.
Definition: perf_log.h:323

◆ fast_pop()

void libMesh::PerfLog::fast_pop ( const char *  label,
const char *  header = "" 
)

Pop the event label off the stack, resuming any lower event.

This method must be passed the exact same pointers as were passed to fast_push, not merely pointers to identical strings.

Definition at line 486 of file perf_log.h.

References libMesh::err, log, log_events, log_stack, and total_time.

Referenced by libMesh::PerfItem::~PerfItem().

488 {
489  if (this->log_events)
490  {
491  libmesh_assert (!log_stack.empty());
492 
493 #ifndef NDEBUG
494  PerfData * perf_data = &(log[std::make_pair(header,label)]);
495  if (perf_data != log_stack.top())
496  {
497  libMesh::err << "PerfLog can't pop (" << header << ',' << label << ')' << std::endl;
498  libMesh::err << "From top of stack of running logs:" << std::endl;
499  for (auto i : log)
500  if (&(i.second) == log_stack.top())
501  libMesh::err << '(' << i.first.first << ',' << i.first.second << ')' << std::endl;
502 
503  libmesh_assert_equal_to (perf_data, log_stack.top());
504  }
505 #endif
506 
507  total_time += log_stack.top()->stopit();
508 
509  log_stack.pop();
510 
511  if (!log_stack.empty())
512  log_stack.top()->restart();
513  }
514 }
bool log_events
Flag to optionally disable all logging.
Definition: perf_log.h:323
double total_time
The total running time for recorded events.
Definition: perf_log.h:328
log_type log
The actual log.
Definition: perf_log.h:342
std::stack< PerfData * > log_stack
A stack to hold the current performance log trace.
Definition: perf_log.h:347
OStreamProxy err

◆ fast_push()

void libMesh::PerfLog::fast_push ( const char *  label,
const char *  header = "" 
)

Push the event label onto the stack, pausing any active event.

Note - for speed the PerfLog directly considers the pointers here. Supply pointers to string literals or other character arrays whose lifetime will exceed the lifetime of the PerfLog object, not to temporarily allocated arrays.

Definition at line 466 of file perf_log.h.

References log, log_events, log_stack, libMesh::PerfData::start(), and total_time.

Referenced by libMesh::PerfItem::PerfItem().

468 {
469  if (this->log_events)
470  {
471  // Get a reference to the event data to avoid
472  // repeated map lookups
473  PerfData * perf_data = &(log[std::make_pair(header,label)]);
474 
475  if (!log_stack.empty())
476  total_time += log_stack.top()->pause_for(*perf_data);
477  else
478  perf_data->start();
479  log_stack.push(perf_data);
480  }
481 }
bool log_events
Flag to optionally disable all logging.
Definition: perf_log.h:323
double total_time
The total running time for recorded events.
Definition: perf_log.h:328
log_type log
The actual log.
Definition: perf_log.h:342
std::stack< PerfData * > log_stack
A stack to hold the current performance log trace.
Definition: perf_log.h:347

◆ get_active_time()

double libMesh::PerfLog::get_active_time ( ) const
Returns
The active time

Definition at line 531 of file perf_log.h.

References total_time.

532 {
533  return total_time;
534 }
double total_time
The total running time for recorded events.
Definition: perf_log.h:328

◆ get_elapsed_time()

double libMesh::PerfLog::get_elapsed_time ( ) const
Returns
The total time spent on this event.

Definition at line 519 of file perf_log.h.

References tstart.

520 {
521  struct timeval tnow;
522 
523  gettimeofday (&tnow, nullptr);
524 
525  const double elapsed_time = (static_cast<double>(tnow.tv_sec - tstart.tv_sec) +
526  static_cast<double>(tnow.tv_usec - tstart.tv_usec)*1.e-6);
527  return elapsed_time;
528 }
struct timeval tstart
The time we were constructed or last cleared.
Definition: perf_log.h:333

◆ get_info_header()

std::string libMesh::PerfLog::get_info_header ( ) const
Returns
A string containing ONLY the information header.

◆ get_log()

std::string libMesh::PerfLog::get_log ( ) const
Returns
A string containing: (1) Basic machine information (if first call) (2) The performance log

◆ get_log_raw()

const log_type& libMesh::PerfLog::get_log_raw ( ) const
Returns
the raw underlying data structure for the entire performance log.
Deprecated:
because encapsulation is good.

Also probably broken by the switch from string to const char *, though users who are liberal with "auto" might be safe.

Definition at line 305 of file perf_log.h.

References log.

306  {
307  libmesh_deprecated();
308  return log;
309  }
log_type log
The actual log.
Definition: perf_log.h:342

◆ get_perf_data()

PerfData libMesh::PerfLog::get_perf_data ( const std::string &  label,
const std::string &  header = "" 
)

Return the PerfData object associated with a label and header.

◆ get_perf_info()

std::string libMesh::PerfLog::get_perf_info ( ) const
Returns
A string containing ONLY the log information

◆ logging_enabled()

bool libMesh::PerfLog::logging_enabled ( ) const
Returns
true iff performance logging is enabled

Definition at line 166 of file perf_log.h.

References log_events.

166 { return log_events; }
bool log_events
Flag to optionally disable all logging.
Definition: perf_log.h:323

◆ pause_event()

void libMesh::PerfLog::pause_event ( const std::string &  label,
const std::string &  header = "" 
)

Suspend monitoring of the event.

◆ pop() [1/2]

void libMesh::PerfLog::pop ( const char *  label,
const char *  header = "" 
)

Pop the event label off the stack, resuming any lower event.

This method will eventually be deprecated. Use fast_pop() (with the exact same pointers supplied to fast_push()) instead.

◆ pop() [2/2]

void libMesh::PerfLog::pop ( const std::string &  label,
const std::string &  header = "" 
)

Pop the event label off the stack, resuming any lower event.

This method will eventually be deprecated. String manipulation is too low performance to use when performance monitoring hot spots. Use fast_pop() instead.

◆ print_log()

void libMesh::PerfLog::print_log ( ) const

Print the log.

◆ push() [1/2]

void libMesh::PerfLog::push ( const char *  label,
const char *  header = "" 
)

Push the event label onto the stack, pausing any active event.

This method will eventually be deprecated. For backwards compatibility, the PerfLog must copy the contents of these character arrays into strings, which ironically damages the performance we are trying to profile. Use fast_push() (with compatible long-lived character array data) instead.

◆ push() [2/2]

void libMesh::PerfLog::push ( const std::string &  label,
const std::string &  header = "" 
)

Push the event label onto the stack, pausing any active event.

This method will eventually be deprecated. String manipulation is too low performance to use when performance monitoring hot spots. Use fast_push() instead.

◆ restart_event()

void libMesh::PerfLog::restart_event ( const std::string &  label,
const std::string &  header = "" 
)

Restart monitoring the event.

◆ split_on_whitespace()

void libMesh::PerfLog::split_on_whitespace ( const std::string &  input,
std::vector< std::string > &  output 
) const
private

Splits a string on whitespace into a vector of separate strings.

This is used to make the LIBMESH_CONFIGURE_INFO a little more manageable.

◆ start_event()

void libMesh::PerfLog::start_event ( const std::string &  label,
const std::string &  header = "" 
)

Start monitoring the event named label.

◆ stop_event()

void libMesh::PerfLog::stop_event ( const std::string &  label,
const std::string &  header = "" 
)

Stop monitoring the event named label.

Member Data Documentation

◆ called

bool libMesh::PerfLog::called
staticprivate

Flag indicating if print_log() has been called.

This is used to print a header with machine-specific data the first time that print_log() is called.

Definition at line 354 of file perf_log.h.

◆ label_name

const std::string libMesh::PerfLog::label_name
private

The label for this object.

Definition at line 318 of file perf_log.h.

◆ log

log_type libMesh::PerfLog::log
private

The actual log.

An unsorted_map would work fine here and would be asymptotically faster, but in my tests for our log sizes there was no improvement.

Definition at line 342 of file perf_log.h.

Referenced by fast_pop(), fast_push(), and get_log_raw().

◆ log_events

bool libMesh::PerfLog::log_events
private

Flag to optionally disable all logging.

Definition at line 323 of file perf_log.h.

Referenced by disable_logging(), enable_logging(), fast_pop(), fast_push(), and logging_enabled().

◆ log_stack

std::stack<PerfData*> libMesh::PerfLog::log_stack
private

A stack to hold the current performance log trace.

Definition at line 347 of file perf_log.h.

Referenced by fast_pop(), and fast_push().

◆ non_temporary_strings

std::map<std::string, const char *> libMesh::PerfLog::non_temporary_strings
private

Workaround to give us fixed pointers to character arrays for every string.

Using std::set instead might work: it won't invalidate iterators, which I think means it doesn't have any reason to modify or copy their contents or otherwise invalidate their c_str() pointers... but I can't prove it from the standards doc, so let's be safe.

Definition at line 371 of file perf_log.h.

◆ total_time

double libMesh::PerfLog::total_time
private

The total running time for recorded events.

Definition at line 328 of file perf_log.h.

Referenced by fast_pop(), fast_push(), and get_active_time().

◆ tstart

struct timeval libMesh::PerfLog::tstart
private

The time we were constructed or last cleared.

Definition at line 333 of file perf_log.h.

Referenced by get_elapsed_time().


The documentation for this class was generated from the following file: