Chris@49: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@0: Chris@0: /* Chris@52: Sonic Visualiser Chris@52: An audio file viewer and annotation editor. Chris@52: Centre for Digital Music, Queen Mary, University of London. Chris@0: Chris@52: This program is free software; you can redistribute it and/or Chris@52: modify it under the terms of the GNU General Public License as Chris@52: published by the Free Software Foundation; either version 2 of the Chris@52: License, or (at your option) any later version. See the file Chris@52: COPYING included with this distribution for more information. Chris@0: */ Chris@0: Chris@0: /* Chris@0: This is a modified version of a source file from the Chris@0: Rosegarden MIDI and audio sequencer and notation editor. Chris@17: This file copyright 2000-2006 Chris Cannam and Guillaume Laurent. Chris@0: */ Chris@0: Chris@0: #include Chris@0: #include "Profiler.h" Chris@0: Chris@0: #include Chris@0: #include Chris@0: Chris@0: //#define NO_TIMING 1 Chris@0: Chris@0: #ifdef NDEBUG Chris@0: #define NO_TIMING 1 Chris@0: #endif Chris@0: Chris@0: using std::cerr; Chris@0: using std::endl; Chris@0: Chris@0: Profiles* Profiles::m_instance = 0; Chris@0: Chris@0: Profiles* Profiles::getInstance() Chris@0: { Chris@0: if (!m_instance) m_instance = new Profiles(); Chris@0: Chris@0: return m_instance; Chris@0: } Chris@0: Chris@0: Profiles::Profiles() Chris@0: { Chris@0: } Chris@0: Chris@0: Profiles::~Profiles() Chris@0: { Chris@0: dump(); Chris@0: } Chris@0: Chris@0: void Profiles::accumulate(const char* id, clock_t time, RealTime rt) Chris@0: { Chris@0: #ifndef NO_TIMING Chris@0: ProfilePair &pair(m_profiles[id]); Chris@0: ++pair.first; Chris@0: pair.second.first += time; Chris@0: pair.second.second = pair.second.second + rt; Chris@0: Chris@0: TimePair &timePair(m_lastCalls[id]); Chris@0: timePair.first = time; Chris@0: timePair.second = rt; Chris@0: #endif Chris@0: } Chris@0: Chris@0: void Profiles::dump() Chris@0: { Chris@0: #ifndef NO_TIMING Chris@0: cerr << "Profiles::dump() :\n"; Chris@0: Chris@0: // I'm finding these two confusing dumped out in random order, Chris@0: // so I'm going to sort them alphabetically: Chris@0: Chris@0: std::vector profileNames; Chris@0: for (ProfileMap::iterator i = m_profiles.begin(); Chris@0: i != m_profiles.end(); ++i) { Chris@0: profileNames.push_back((*i).first); Chris@0: } Chris@0: Chris@0: std::sort(profileNames.begin(), profileNames.end()); Chris@0: Chris@0: for (std::vector::iterator i = profileNames.begin(); Chris@0: i != profileNames.end(); ++i) { Chris@0: Chris@0: cerr << "-> " << *i << ": CPU: " Chris@0: << m_profiles[*i].first << " calls, " Chris@0: << int((m_profiles[*i].second.first * 1000.0) / CLOCKS_PER_SEC) << "ms, " Chris@0: << (((double)m_profiles[*i].second.first * 1000000.0 / Chris@0: (double)m_profiles[*i].first) / CLOCKS_PER_SEC) << "us/call" Chris@0: << endl; Chris@0: Chris@0: cerr << "-> " << *i << ": real: " Chris@0: << m_profiles[*i].first << " calls, " Chris@0: << m_profiles[*i].second.second << ", " Chris@0: << (m_profiles[*i].second.second / m_profiles[*i].first) Chris@0: << "/call" Chris@0: << endl; Chris@0: Chris@0: cerr << "-> " << *i << ": last: CPU: " Chris@0: << int((m_lastCalls[*i].first * 1000.0) / CLOCKS_PER_SEC) << "ms, " Chris@0: << " real: " Chris@0: << m_lastCalls[*i].second << endl; Chris@0: } Chris@0: Chris@0: cerr << "Profiles::dump() finished\n"; Chris@0: #endif Chris@0: } Chris@0: Chris@0: Profiler::Profiler(const char* c, bool showOnDestruct) Chris@0: : m_c(c), Chris@0: m_showOnDestruct(showOnDestruct) Chris@0: { Chris@0: #ifndef NO_TIMING Chris@0: m_startCPU = clock(); Chris@0: Chris@0: struct timeval tv; Chris@0: (void)gettimeofday(&tv, 0); Chris@26: m_startTime = RealTime::fromTimeval(tv); Chris@0: #endif Chris@0: } Chris@0: Chris@0: void Chris@0: Profiler::update() Chris@0: { Chris@0: #ifndef NO_TIMING Chris@0: clock_t elapsedCPU = clock() - m_startCPU; Chris@0: Chris@0: struct timeval tv; Chris@0: (void)gettimeofday(&tv, 0); Chris@26: RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime; Chris@0: Chris@0: cerr << "Profiler : id = " << m_c Chris@0: << " - elapsed so far = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC) Chris@0: << "ms CPU, " << elapsedTime << " real" << endl; Chris@0: #endif Chris@0: } Chris@0: Chris@0: Profiler::~Profiler() Chris@0: { Chris@0: #ifndef NO_TIMING Chris@0: clock_t elapsedCPU = clock() - m_startCPU; Chris@0: Chris@0: struct timeval tv; Chris@0: (void)gettimeofday(&tv, 0); Chris@26: RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime; Chris@0: Chris@0: Profiles::getInstance()->accumulate(m_c, elapsedCPU, elapsedTime); Chris@0: Chris@0: if (m_showOnDestruct) Chris@0: cerr << "Profiler : id = " << m_c Chris@0: << " - elapsed = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC) Chris@0: << "ms CPU, " << elapsedTime << " real" << endl; Chris@0: #endif Chris@0: } Chris@0: