Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions source/MRMesh/MRLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "MRSystem.h"
#include "MRStringConvert.h"
#include "MRPch/MRSpdlog.h"
#include "MRTimer.h"

#ifndef __EMSCRIPTEN__
#include <boost/stacktrace.hpp>
Expand Down Expand Up @@ -35,6 +36,8 @@ void crashSignalHandler( int signal )
auto stacktrace = boost::stacktrace::stacktrace();
for ( const auto& frame : stacktrace )
spdlog::critical( "{} {} {}", frame.name(), frame.source_file(), frame.source_line() );

MR::printCurrentTimerBranch();
std::exit( signal );
}
#endif
Expand Down
46 changes: 39 additions & 7 deletions source/MRMesh/MRTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace MR

struct TimeRecord
{
TimeRecord* parent = nullptr;
int count = 0;
nanoseconds time = {};
std::map<std::string, TimeRecord> children;
Expand Down Expand Up @@ -82,32 +83,63 @@ void printTimingTreeAtEnd( bool on )
rootTimeRecord.printTreeInDtor = on;
}

void printCurrentTimerBranch()
{
Timer t( "Print Timer branch leaf" );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need timer here?

const TimeRecord* active = currentRecord;
auto& logger = rootTimeRecord.loggerHandle;
if ( !logger )
return;
while ( active )
{
if ( !active->parent )
{
logger->info( "Root" );
break;
}
for ( const auto& child : active->parent->children )
{
if ( &child.second != active )
continue;
logger->info( child.first );
break;
}
active = active->parent;
}
}

void printTimingTreeAndStop()
{
rootTimeRecord.printTree();
printTimingTreeAtEnd( false );
}

void Timer::restart( std::string name )
void Timer::restart( const std::string& name )
{
finish();
start( name );
}

void Timer::start( const std::string& name )
{
if ( std::this_thread::get_id() != mainThreadId )
return;
name_ = std::move( name );
start_ = high_resolution_clock::now();
parent_ = currentRecord;
currentRecord = &currentRecord->children[name_];
auto parent = currentRecord;
currentRecord = &parent->children[name];
currentRecord->parent = parent;
}

void Timer::finish()
{
if ( !parent_ )
auto currentParent = currentRecord->parent;
if ( !currentParent )
return;

currentRecord->time += high_resolution_clock::now() - start_;
++currentRecord->count;
currentRecord = parent_;
parent_ = nullptr;

currentRecord = currentParent;
}

} //namespace MR
10 changes: 6 additions & 4 deletions source/MRMesh/MRTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ struct TimeRecord;
class Timer
{
public:
Timer( std::string name ) { restart( std::move( name ) ); }
Timer( const std::string& name ) { start( name ); }
~Timer() { finish(); }

MRMESH_API void restart( std::string name );
MRMESH_API void restart( const std::string& name );
MRMESH_API void start( const std::string& name );
MRMESH_API void finish();

Timer( const Timer & ) = delete;
Expand All @@ -29,14 +30,15 @@ class Timer
std::chrono::duration<double> secondsPassed() const { return std::chrono::high_resolution_clock::now() - start_; }

private:
TimeRecord * parent_ = nullptr;
std::chrono::time_point<std::chrono::high_resolution_clock> start_;
std::string name_;
};

/// enables or disables printing of timing tree when application terminates
MRMESH_API void printTimingTreeAtEnd( bool on );

/// prints current timer branch
MRMESH_API void printCurrentTimerBranch();

/// prints the current timing tree, then calls printTimingTreeAtEnd( false );
MRMESH_API void printTimingTreeAndStop();

Expand Down
1 change: 1 addition & 0 deletions source/MRViewer/MRViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ int launchDefaultViewer( const Viewer::LaunchParams& params, const ViewerSetup&
auto stacktrace = boost::stacktrace::stacktrace();
for ( const auto& frame : stacktrace )
spdlog::critical( "{} {} {}", frame.name(), frame.source_file(), frame.source_line() );
printCurrentTimerBranch();
res = 1;
}

Expand Down