kicad/include/profile.h

270 lines
6.8 KiB
C
Raw Normal View History

2013-09-18 17:56:37 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2013 CERN
2020-12-21 15:17:52 +00:00
* Copyright (C) 2019-2020 KiCad Developers, see AUTHORS.txt for contributors.
2013-09-18 17:56:37 +00:00
* @author Tomasz Wlostowski <tomasz.wlostowski@cern.ch>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/**
* @file profile.h:
* @brief Simple profiling functions for measuring code execution time.
*/
2019-10-14 10:38:52 +00:00
#ifndef TPROFILE_H
#define TPROFILE_H
2013-09-18 17:56:37 +00:00
#include <atomic>
2016-12-31 07:15:20 +00:00
#include <chrono>
2016-11-04 21:29:42 +00:00
#include <string>
2016-12-31 07:15:20 +00:00
#include <iostream>
#include <iomanip>
2013-09-18 17:56:37 +00:00
/**
2020-12-21 15:17:52 +00:00
* A small class to help profiling.
*
* It allows the calculation of the elapsed time (in milliseconds) between
* its creation (or the last call to Start() ) and the last call to Stop()
*/
class PROF_TIMER
2016-11-04 21:29:42 +00:00
{
public:
/**
2020-12-21 15:17:52 +00:00
* Create a PROF_COUNTER for measuring an elapsed time in milliseconds.
*
* @param aName a string that will be printed in message.
* @param aAutostart true (default) to immediately start the timer
*/
PROF_TIMER( const std::string& aName, bool aAutostart = true ) :
m_name( aName ), m_running( false )
{
if( aAutostart )
Start();
}
/**
2020-12-21 15:17:52 +00:00
* Create a PROF_COUNTER for measuring an elapsed time in milliseconds
*
* The counter is started and the string to print in message is left empty.
*/
PROF_TIMER()
2016-12-09 11:04:32 +00:00
{
Start();
2016-12-09 11:04:32 +00:00
}
/**
2020-12-21 15:17:52 +00:00
* Start or restart the counter.
*/
void Start()
2016-12-09 11:04:32 +00:00
{
m_running = true;
m_starttime = CLOCK::now();
2019-03-14 08:11:34 +00:00
m_lasttime = m_starttime;
2016-12-09 11:04:32 +00:00
}
/**
2020-12-21 15:17:52 +00:00
* Save the time when this function was called, and set the counter stane to stop.
*/
void Stop()
2016-12-09 11:04:32 +00:00
{
if( !m_running )
return;
m_stoptime = CLOCK::now();
m_running = false;
2016-12-09 11:04:32 +00:00
}
/**
* Print the elapsed time (in a suitable unit) to a stream.
*
* The unit is automatically chosen from ns, us, ms and s, depending on the
* size of the current count.
*
* @param the stream to print to.
*/
void Show( std::ostream& aStream = std::cerr )
2016-12-09 11:04:32 +00:00
{
using DURATION = std::chrono::duration<double, std::nano>;
const auto duration = SinceStart<DURATION>();
const double cnt = duration.count();
if( m_name.size() )
{
aStream << m_name << " took ";
}
if( cnt < 1e3 )
aStream << cnt << "ns";
else if( cnt < 1e6 )
aStream << cnt / 1e3 << "µs";
else if( cnt < 1e9 )
aStream << cnt / 1e6 << "ms";
else
aStream << cnt / 1e9 << "s";
aStream << std::endl;
2019-03-14 08:11:34 +00:00
}
/**
2020-12-21 15:17:52 +00:00
* @return the time since the timer was started. If the timer is stopped, the duration
* is from the start time to the time it was stopped, else it is to the current
* time.
*/
template <typename DURATION>
DURATION SinceStart( bool aSinceLast = false )
2016-12-09 11:04:32 +00:00
{
const TIME_POINT stoptime = m_running ? CLOCK::now() : m_stoptime;
const TIME_POINT starttime = aSinceLast ? m_lasttime : m_starttime;
m_lasttime = stoptime;
return std::chrono::duration_cast<DURATION>( stoptime - starttime );
}
/**
2020-12-21 15:17:52 +00:00
* @param aSinceLast only get the time since the last time the time was read.
* @return the elapsed time in ms since the timer was started.
*/
double msecs( bool aSinceLast = false )
{
using DUR_MS = std::chrono::duration<double, std::milli>;
return SinceStart<DUR_MS>( aSinceLast ).count();
2016-12-09 11:04:32 +00:00
}
2016-11-04 21:29:42 +00:00
std::string to_string()
{
char tmp[1024];
snprintf( tmp, sizeof( tmp ), "%s: %-6.1fms", m_name.c_str(), msecs() );
return tmp;
}
2016-11-04 21:29:42 +00:00
private:
std::string m_name; // a string printed in message
2016-12-09 11:04:32 +00:00
bool m_running;
2016-12-31 07:15:20 +00:00
using CLOCK = std::chrono::high_resolution_clock;
using TIME_POINT = std::chrono::time_point<CLOCK>;
2016-12-31 07:15:20 +00:00
2019-03-14 08:11:34 +00:00
TIME_POINT m_starttime, m_lasttime, m_stoptime;
2016-11-04 21:29:42 +00:00
};
/**
* A simple RAII class to measure the time of an operation.
*
* On construction, a timer is started, and on destruction, the timer is
* ended, and the time difference is written into the given duration.
*
* For example:
*
* DURATION duration; // select a duration type as needed
* {
* SCOPED_PROF_TIMER<DURATION> timer( duration );
* timed_activity();
* }
* // duration is now the time timed activity took
*
* From C++17, with class template argument deduction, you should be able to
* omit the <DURATION>.
*/
template <typename DURATION>
class SCOPED_PROF_TIMER : public PROF_TIMER
{
public:
SCOPED_PROF_TIMER( DURATION& aDuration ) : PROF_TIMER(), m_duration( aDuration )
{
}
~SCOPED_PROF_TIMER()
{
// update the output
m_duration = m_counter.SinceStart<DURATION>();
}
private:
///< The counter to use to do the profiling
PROF_TIMER m_counter;
///< The duration to update at the end of the scope
DURATION& m_duration;
};
/**
* An alternate way to calculate an elapsed time (in microsecondes) to class PROF_COUNTER
2020-12-21 15:17:52 +00:00
*
* @return an ever increasing indication of elapsed microseconds. Use this by computing
* differences between two calls.
* @author Dick Hollenbeck
*/
unsigned GetRunningMicroSecs();
/**
* A thread-safe event counter
*/
class PROF_COUNTER
{
public:
2021-12-15 20:52:08 +00:00
PROF_COUNTER() :
m_name( "Anonymous" ),
m_count( 0 )
{
}
PROF_COUNTER( const std::string& aName ) :
m_name( aName ),
m_count( 0 )
{
}
unsigned long long Count() const
{
return m_count.load();
}
void Reset()
{
m_count.store( 0 );
}
unsigned long long operator++( int )
{
return m_count++;
}
void Show( std::ostream& aStream = std::cerr )
{
if( m_name.size() )
aStream << m_name << ": ";
aStream << m_count.load();
aStream << std::endl;
}
private:
std::string m_name;
std::atomic_ullong m_count;
};
2019-10-14 10:38:52 +00:00
#endif // TPROFILE_H