annotate base/Profiler.h @ 183:146eb9e35baa

* Improve output from Profiler class and make it incur less (no) overhead in release builds with NO_TIMING defined * Fix a lock contention issue in spectrogram * Marginal optimisations elsewhere
author Chris Cannam
date Tue, 10 Oct 2006 14:51:17 +0000
parents 4b2ea82fd0ed
children 91fdc752e540
rev   line source
Chris@49 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@0 2
Chris@0 3 /*
Chris@52 4 Sonic Visualiser
Chris@52 5 An audio file viewer and annotation editor.
Chris@52 6 Centre for Digital Music, Queen Mary, University of London.
Chris@0 7
Chris@52 8 This program is free software; you can redistribute it and/or
Chris@52 9 modify it under the terms of the GNU General Public License as
Chris@52 10 published by the Free Software Foundation; either version 2 of the
Chris@52 11 License, or (at your option) any later version. See the file
Chris@52 12 COPYING included with this distribution for more information.
Chris@0 13 */
Chris@0 14
Chris@0 15 /*
Chris@0 16 This is a modified version of a source file from the
Chris@0 17 Rosegarden MIDI and audio sequencer and notation editor.
Chris@17 18 This file copyright 2000-2006 Chris Cannam and Guillaume Laurent.
Chris@0 19 */
Chris@0 20
Chris@0 21
Chris@0 22 #ifndef _PROFILER_H_
Chris@0 23 #define _PROFILER_H_
Chris@0 24
Chris@150 25 #include "system/System.h"
Chris@0 26
Chris@0 27 #include <ctime>
Chris@0 28 #include <sys/time.h>
Chris@0 29 #include <map>
Chris@0 30
Chris@0 31 #include "RealTime.h"
Chris@0 32
Chris@183 33 //#define NO_TIMING 1
Chris@183 34
Chris@183 35 #define WANT_TIMING 1
Chris@183 36
Chris@183 37 #ifdef NDEBUG
Chris@183 38 #ifndef WANT_TIMING
Chris@183 39 #define NO_TIMING 1
Chris@183 40 #endif
Chris@183 41 #endif
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Profiling classes
Chris@0 45 */
Chris@0 46
Chris@0 47 /**
Chris@0 48 * The class holding all profiling data
Chris@0 49 *
Chris@0 50 * This class is a singleton
Chris@0 51 */
Chris@0 52 class Profiles
Chris@0 53 {
Chris@0 54 public:
Chris@0 55 static Profiles* getInstance();
Chris@0 56 ~Profiles();
Chris@0 57
Chris@0 58 void accumulate(const char* id, clock_t time, RealTime rt);
Chris@183 59 void dump() const;
Chris@0 60
Chris@0 61 protected:
Chris@0 62 Profiles();
Chris@0 63
Chris@0 64 typedef std::pair<clock_t, RealTime> TimePair;
Chris@0 65 typedef std::pair<int, TimePair> ProfilePair;
Chris@0 66 typedef std::map<const char *, ProfilePair> ProfileMap;
Chris@0 67 typedef std::map<const char *, TimePair> LastCallMap;
Chris@183 68 typedef std::map<const char *, TimePair> WorstCallMap;
Chris@0 69 ProfileMap m_profiles;
Chris@0 70 LastCallMap m_lastCalls;
Chris@183 71 WorstCallMap m_worstCalls;
Chris@0 72
Chris@0 73 static Profiles* m_instance;
Chris@0 74 };
Chris@0 75
Chris@183 76 #ifndef NO_TIMING
Chris@183 77
Chris@183 78 /**
Chris@183 79 * Profile point instance class. Construct one of these on the stack
Chris@183 80 * at the start of a function, in order to record the time consumed
Chris@183 81 * within that function. The profiler object should be effectively
Chris@183 82 * optimised out if NO_TIMING is defined, so any overhead in a release
Chris@183 83 * build should be negligible so long as you remember to define that.
Chris@183 84 */
Chris@0 85 class Profiler
Chris@0 86 {
Chris@0 87 public:
Chris@183 88 /**
Chris@183 89 * Create a profile point instance that records time consumed
Chris@183 90 * against the given profiling point name. If showOnDestruct is
Chris@183 91 * true, the time consumed will be printed to stderr when the
Chris@183 92 * object is destroyed; otherwise, only the accumulated, mean and
Chris@183 93 * worst-case times will be shown when the program exits or
Chris@183 94 * Profiles::dump() is called.
Chris@183 95 */
Chris@183 96 Profiler(const char *name, bool showOnDestruct = false);
Chris@0 97 ~Profiler();
Chris@0 98
Chris@183 99 void update() const;
Chris@0 100
Chris@0 101 protected:
Chris@0 102 const char* m_c;
Chris@0 103 clock_t m_startCPU;
Chris@0 104 RealTime m_startTime;
Chris@0 105 bool m_showOnDestruct;
Chris@0 106 };
Chris@183 107
Chris@183 108 #else
Chris@183 109
Chris@183 110 class Profiler
Chris@183 111 {
Chris@183 112 public:
Chris@183 113 Profiler(const char *, bool) { }
Chris@183 114 ~Profiler() { }
Chris@183 115
Chris@183 116 void update() { }
Chris@183 117 };
Chris@0 118
Chris@0 119 #endif
Chris@183 120
Chris@183 121 #endif