profiler.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00034 #ifndef __profiler__header__
00035 #define __profiler__header__
00036 
00037 #include "gpucalc/singleton.h"
00038 #include "gpucalc/timer.h"
00039 
00040 namespace gpucalc
00041 {
00042 
00043  const char _ProfilerName [] = "Profiler";
00044 
00045 
00046  class _GpuCalcExport Profiler: public Singleton<Profiler>
00047  {
00048 
00049 
00050   OBJ_DISABLE_COPY(Profiler)
00051   
00052   
00053   public:
00054 
00055    Profiler(): Singleton<Profiler>(_ProfilerName)
00056    {
00057    }
00058    
00059 
00060    ~Profiler()
00061    {
00062    }
00063 
00064 
00065    void beginProfile(const std::string & where)
00066    {
00067     if (!addToContainer(std::make_pair(Timer::getSingleton().getTime(), -1.0), where, mProfileMap))
00068     {
00069      Warning<ERR_DUPLICATE_ITEM>(this, "Profile label \"" + where + "\" already exists. Profiling does not start.", "Profiler::beginProfile()", __FILE__, __LINE__);
00070     }
00071    }
00072 
00073    
00074    void endProfile(const std::string & where)
00075    {
00076     double stop = Timer::getSingleton().getTime();
00077 
00078     ProfileMap::iterator i = mProfileMap.find(where);
00079     if (i == mProfileMap.end())
00080     {
00081      Warning<ERR_ITEM_NOT_FOUND>(this, "Profile label \"" + where + "\" cannot be found.", "Profiler::endProfile()", __FILE__, __LINE__);
00082     }
00083     else
00084     {
00085      i->second.second = stop;// = std::make_pair(i->second.first, stop);
00086     }
00087    }
00088    
00089    static Profiler & getSingleton();
00090 
00091    
00092    static Profiler * getSingletonPtr();
00093 
00094 
00095    double getProfilingTime(const std::string & where)
00096    {
00097     ProfileMap::iterator i = mProfileMap.find(where);
00098     if (i == mProfileMap.end())
00099     {
00100      Warning<ERR_ITEM_NOT_FOUND>(this, "Profile label \"" + where + "\" cannot be found.", "Profiler::endProfile()", __FILE__, __LINE__);
00101      return -1.0;
00102     }
00103     else
00104     {
00105      if (i->second.second > 0.0)
00106      {
00107       double result = i->second.second - i->second.first;
00108       mProfileMap.erase(i);
00109       return result;
00110      }
00111      else
00112      {
00113       Except<ERR_INVALID_STATE>(this, "Profiling \"" + where + "\" is not stopped yet.", "Profiler::getProfilingTime()", __FILE__, __LINE__);
00114       return -1.0;
00115      }
00116     }
00117    }
00118 
00119   private:
00120 
00121    typedef std::pair<double, double> ProfileEntry;
00122    typedef std::map<std::string, ProfileEntry> ProfileMap;
00123    ProfileMap mProfileMap; 
00124 
00125 
00126  };
00127 }
00128 #endif

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6