// CpuTimerTuple.cpp #include "CpuTimerTuple.h" #include "TupleManager.h" using std::map; using std::less; using std::string; //********************************************************************** // Constructor. CpuTimerTuple::CpuTimerTuple(string name) : _tup(TupleManager::tuple(name)), _is_running(false) { _tup.columnDirect("event", 1); _tup.columnDirect("time", 0.0); } //********************************************************************** // Destructor. // If running, then write the final time. CpuTimerTuple::~CpuTimerTuple() { if ( is_running() ) { stop(); } } //********************************************************************** // Start the clock. // If running restarti and return nonzero. int CpuTimerTuple::start(int event) { bool already_started = is_running(); _event = event; _is_running = true; _timer.reset(); // Stop all subclocks and zero their sums. for ( TimerMap::const_iterator itimer = _timers.begin(); itimer != _timers.end(); ++itimer ) { string name = itimer->first; _runflags[name] = false; _times[name] = 0.0; } if ( already_started ) { return 1; } return 0; } //********************************************************************** // Stop the clock and record the time. int CpuTimerTuple::stop() { // Error if clock is not running. if ( ! is_running() ) { return 1; } // Update the total time. double time = _timer.elapsed_time(); _is_running = false; _tup.capture("event", _event ); _tup.capture("time", time); // Loop over subclocks. for ( TimerMap::const_iterator itimer = _timers.begin(); itimer != _timers.end(); ++itimer ) { string name = itimer->first; bool& running = _runflags[name]; // Stop any clock that is running. if ( running ) { stop(name); } _tup.capture(name.c_str(), _times[name]); } // Update tuple. _tup.storeCapturedData(); _tup.clearData(); return 0; } //********************************************************************** // Return if the clock is running. bool CpuTimerTuple::is_running() const { return _is_running; } //********************************************************************** // Add a subclock. int CpuTimerTuple::add_subclock(string name) { TimerMap::const_iterator itimer = _timers.find(name); if ( itimer != _timers.end() ) { return 1; } _timers[name] = CpuTimer(); _runflags[name] = false; _times[name] = 0.0; double time = 0.0; _tup.columnDirect(name.c_str(), time); return 0; } //********************************************************************** // Start a subclock. int CpuTimerTuple::start(string name) { // Check clock is present. TimerMap::const_iterator itimer = _timers.find(name); if ( itimer == _timers.end() ) { return 1; } // Check clock is not already running. bool& running = _runflags[name]; if ( running ) { return 2; } // Start the clock. running = true; _timers[name].reset(); return 0; } //********************************************************************** // Stop a subclock. int CpuTimerTuple::stop(string name) { // Check clock is present. TimerMap::const_iterator itimer = _timers.find(name); if ( itimer == _timers.end() ) { return 1; } // Check clock is running. bool& running = _runflags[name]; if ( ! running ) { return 2; } // Update the total time for this clock. _times[name] += _timers[name].elapsed_time(); _runflags[name] = false; return 0; } //********************************************************************** // Return if a subclock is defined. bool CpuTimerTuple::is_defined(string name) const { FlagMap::const_iterator iflag = _runflags.find(name); return iflag != _runflags.end(); } //********************************************************************** // Return if a subclock is running. bool CpuTimerTuple::is_running(string name) const { // Check clock is present. FlagMap::const_iterator iflag = _runflags.find(name); if ( iflag == _runflags.end() ) { return false; } return iflag->second; } //**********************************************************************