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@202:    This file copyright 2000-2006 Chris Cannam, Guillaume Laurent,
Chris@202:    and QMUL.
Chris@0: */
Chris@0: 
Chris@0: #include <iostream>
Chris@0: #include "Profiler.h"
Chris@0: 
Chris@658: #include <cstdio>
Chris@658: 
Chris@0: #include <vector>
Chris@0: #include <algorithm>
Chris@183: #include <set>
Chris@183: #include <map>
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@259: void Profiles::accumulate(
Chris@259: #ifndef NO_TIMING
Chris@259:     const char* id, clock_t time, RealTime rt
Chris@259: #else
Chris@259:     const char*, clock_t, RealTime
Chris@259: #endif
Chris@259: )
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@183:     TimePair &lastPair(m_lastCalls[id]);
Chris@183:     lastPair.first = time;
Chris@183:     lastPair.second = rt;
Chris@183: 
Chris@183:     TimePair &worstPair(m_worstCalls[id]);
Chris@183:     if (time > worstPair.first) {
Chris@183:         worstPair.first = time;
Chris@183:     }
Chris@183:     if (rt > worstPair.second) {
Chris@183:         worstPair.second = rt;
Chris@183:     }
Chris@0: #endif
Chris@0: }
Chris@0: 
Chris@183: void Profiles::dump() const
Chris@0: {
Chris@183: #ifndef NO_TIMING
Chris@0: 
Chris@183:     fprintf(stderr, "Profiling points:\n");
Chris@0: 
Chris@183:     fprintf(stderr, "\nBy name:\n");
Chris@183: 
Chris@183:     typedef std::set<const char *, std::less<std::string> > StringSet;
Chris@183: 
Chris@183:     StringSet profileNames;
Chris@183:     for (ProfileMap::const_iterator i = m_profiles.begin();
Chris@183:          i != m_profiles.end(); ++i) {
Chris@183:         profileNames.insert(i->first);
Chris@0:     }
Chris@0: 
Chris@183:     for (StringSet::const_iterator i = profileNames.begin();
Chris@183:          i != profileNames.end(); ++i) {
Chris@0: 
Chris@183:         ProfileMap::const_iterator j = m_profiles.find(*i);
Chris@0: 
Chris@183:         if (j == m_profiles.end()) continue;
Chris@0: 
Chris@183:         const ProfilePair &pp(j->second);
Chris@0: 
Chris@183:         fprintf(stderr, "%s(%d):\n", *i, pp.first);
Chris@183: 
Chris@183:         fprintf(stderr, "\tCPU:  \t%.9g ms/call \t[%d ms total]\n",
Chris@183:                 (((double)pp.second.first * 1000.0 /
Chris@183: 		  (double)pp.first) / CLOCKS_PER_SEC),
Chris@183:                 int((pp.second.first * 1000.0) / CLOCKS_PER_SEC));
Chris@183: 
Chris@183:         fprintf(stderr, "\tReal: \t%s ms      \t[%s ms total]\n",
Chris@183:                 ((pp.second.second / pp.first) * 1000).toString().c_str(),
Chris@183:                 (pp.second.second * 1000).toString().c_str());
Chris@183: 
Chris@183:         WorstCallMap::const_iterator k = m_worstCalls.find(*i);
Chris@183:         if (k == m_worstCalls.end()) continue;
Chris@183:         
Chris@183:         const TimePair &wc(k->second);
Chris@183: 
Chris@183:         fprintf(stderr, "\tWorst:\t%s ms/call \t[%d ms CPU]\n",
Chris@183:                 (wc.second * 1000).toString().c_str(),
Chris@183:                 int((wc.first * 1000.0) / CLOCKS_PER_SEC));
Chris@0:     }
Chris@0: 
Chris@183:     typedef std::multimap<RealTime, const char *> TimeRMap;
Chris@183:     typedef std::multimap<int, const char *> IntRMap;
Chris@183:     
Chris@183:     TimeRMap totmap, avgmap, worstmap;
Chris@183:     IntRMap ncallmap;
Chris@183: 
Chris@183:     for (ProfileMap::const_iterator i = m_profiles.begin();
Chris@183:          i != m_profiles.end(); ++i) {
Chris@183:         totmap.insert(TimeRMap::value_type(i->second.second.second, i->first));
Chris@183:         avgmap.insert(TimeRMap::value_type(i->second.second.second /
Chris@183:                                            i->second.first, i->first));
Chris@183:         ncallmap.insert(IntRMap::value_type(i->second.first, i->first));
Chris@183:     }
Chris@183: 
Chris@183:     for (WorstCallMap::const_iterator i = m_worstCalls.begin();
Chris@183:          i != m_worstCalls.end(); ++i) {
Chris@183:         worstmap.insert(TimeRMap::value_type(i->second.second,
Chris@183:                                              i->first));
Chris@183:     }
Chris@183: 
Chris@183: 
Chris@183:     fprintf(stderr, "\nBy total:\n");
Chris@183:     for (TimeRMap::const_iterator i = totmap.end(); i != totmap.begin(); ) {
Chris@183:         --i;
Chris@183:         fprintf(stderr, "%-40s  %s ms\n", i->second,
Chris@183:                 (i->first * 1000).toString().c_str());
Chris@183:     }
Chris@183: 
Chris@183:     fprintf(stderr, "\nBy average:\n");
Chris@183:     for (TimeRMap::const_iterator i = avgmap.end(); i != avgmap.begin(); ) {
Chris@183:         --i;
Chris@183:         fprintf(stderr, "%-40s  %s ms\n", i->second,
Chris@183:                 (i->first * 1000).toString().c_str());
Chris@183:     }
Chris@183: 
Chris@183:     fprintf(stderr, "\nBy worst case:\n");
Chris@183:     for (TimeRMap::const_iterator i = worstmap.end(); i != worstmap.begin(); ) {
Chris@183:         --i;
Chris@183:         fprintf(stderr, "%-40s  %s ms\n", i->second,
Chris@408:                 (i->first * 1000).toString().c_str());
Chris@183:     }
Chris@183: 
Chris@183:     fprintf(stderr, "\nBy number of calls:\n");
Chris@183:     for (IntRMap::const_iterator i = ncallmap.end(); i != ncallmap.begin(); ) {
Chris@183:         --i;
Chris@183:         fprintf(stderr, "%-40s  %d\n", i->second, i->first);
Chris@183:     }
Chris@183: 
Chris@0: #endif
Chris@0: }
Chris@0: 
Chris@183: #ifndef NO_TIMING    
Chris@183: 
Chris@408: Profiler::Profiler(const char* c, bool showOnDestruct) :
Chris@408:     m_c(c),
Chris@408:     m_showOnDestruct(showOnDestruct),
Chris@408:     m_ended(false)
Chris@0: {
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: }
Chris@0: 
Chris@0: void
Chris@183: Profiler::update() const
Chris@0: {
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: }    
Chris@0: 
Chris@0: Profiler::~Profiler()
Chris@0: {
Chris@408:     if (!m_ended) end();
Chris@408: }
Chris@408: 
Chris@408: void
Chris@408: Profiler::end()
Chris@408: {
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@408: 
Chris@408:     m_ended = true;
Chris@0: }
Chris@0:  
Chris@183: #endif
Chris@183: