Marsyas  0.6.0-alpha
/usr/src/RPM/BUILD/marsyas-0.6.0/src/marsyas/debug/recorder.cpp
Go to the documentation of this file.
00001 /*
00002 ** Copyright (C) 1998-2013 George Tzanetakis <gtzan@cs.uvic.ca>
00003 **
00004 ** This program is free software; you can redistribute it and/or modify
00005 ** it under the terms of the GNU General Public License as published by
00006 ** the Free Software Foundation; either version 2 of the License, or
00007 ** (at your option) any later version.
00008 **
00009 ** This program is distributed in the hope that it will be useful,
00010 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 ** GNU General Public License for more details.
00013 **
00014 ** You should have received a copy of the GNU General Public License
00015 ** along with this program; if not, write to the Free Software
00016 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017 */
00018 
00019 #include <marsyas/debug/recorder.h>
00020 #include "timer.h"
00021 
00022 namespace Marsyas { namespace Debug {
00023 
00024 struct Recorder::Observer : public MarSystemObserver
00025 {
00026   MarSystem *system;
00027   std::string path;
00028   Timer timer;
00029   realvec input;
00030   realvec output;
00031   double m_cpu_time;
00032   double m_real_time;
00033 
00034   Observer( MarSystem *system ):
00035     system(system),
00036     path(system->getAbsPath()),
00037     m_cpu_time(0),
00038     m_real_time(0)
00039   {
00040     system->addObserver(this);
00041   }
00042 
00043   ~Observer()
00044   {
00045     system->removeObserver(this);
00046   }
00047 
00048   void preProcess( const realvec &in )
00049   {
00050     input = in;
00051     timer.start();
00052   }
00053 
00054   void postProcess( const realvec &out )
00055   {
00056     timer.measure();
00057     output = out;
00058     m_cpu_time += timer.cpuTime();
00059     m_real_time += timer.realTime();
00060   }
00061 
00062   void reset()
00063   {
00064     m_cpu_time = 0;
00065     m_real_time = 0;
00066   }
00067 
00068   Record::Entry record()
00069   {
00070     return Record::Entry(input, output,
00071                          m_cpu_time, m_real_time);
00072   }
00073 };
00074 
00075 Recorder::Recorder(MarSystem *system):
00076   m_system(system)
00077 {
00078   recursive_add_observer(system);
00079 }
00080 
00081 Recorder::~Recorder()
00082 {
00083   for (Observer *observer : m_observers)
00084   {
00085     delete observer;
00086   }
00087 }
00088 
00089 void Recorder::clear()
00090 {
00091   m_record.clear();
00092 
00093   for (Observer *observer : m_observers)
00094     observer->reset();
00095 }
00096 
00097 void Recorder::recursive_add_observer(MarSystem *system)
00098 {
00099   Observer *observer = new Observer(system);
00100   m_observers.push_back(observer);
00101 
00102   std::vector<MarSystem*> children = system->getChildren();
00103   for (MarSystem *child : children)
00104     recursive_add_observer(child);
00105 }
00106 
00107 void Recorder::commit_observer(Observer *observer)
00108 {
00109   m_record.insert(observer->path, observer->record());
00110 }
00111 
00112 
00113 }} // namespace Marsyas::Debug