Profiler.cpp
Go to the documentation of this file.
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2 
3 /*
4  Sonic Visualiser
5  An audio file viewer and annotation editor.
6  Centre for Digital Music, Queen Mary, University of London.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version. See the file
12  COPYING included with this distribution for more information.
13 */
14 
15 /*
16  This is a modified version of a source file from the
17  Rosegarden MIDI and audio sequencer and notation editor.
18  This file copyright 2000-2006 Chris Cannam, Guillaume Laurent,
19  and QMUL.
20 */
21 
22 #include <iostream>
23 #include "Profiler.h"
24 
25 #include <cstdio>
26 
27 #include <vector>
28 #include <algorithm>
29 #include <set>
30 #include <map>
31 
33 
35 {
36  if (!m_instance) m_instance = new Profiles();
37 
38  return m_instance;
39 }
40 
42 {
43 }
44 
46 {
47  dump();
48 }
49 
50 #ifndef NO_TIMING
52  const char* id, clock_t time, RealTime rt
53 )
54 {
55  ProfilePair &pair(m_profiles[id]);
56  ++pair.first;
57  pair.second.first += time;
58  pair.second.second = pair.second.second + rt;
59 
60  TimePair &lastPair(m_lastCalls[id]);
61  lastPair.first = time;
62  lastPair.second = rt;
63 
64  TimePair &worstPair(m_worstCalls[id]);
65  if (time > worstPair.first) {
66  worstPair.first = time;
67  }
68  if (rt > worstPair.second) {
69  worstPair.second = rt;
70  }
71 }
72 #endif
73 
74 void Profiles::dump() const
75 {
76 #ifndef NO_TIMING
77 
78  fprintf(stderr, "Profiling points:\n");
79 
80  fprintf(stderr, "\nBy name:\n");
81 
82  typedef std::set<const char *, std::less<std::string> > StringSet;
83 
84  StringSet profileNames;
85  for (ProfileMap::const_iterator i = m_profiles.begin();
86  i != m_profiles.end(); ++i) {
87  profileNames.insert(i->first);
88  }
89 
90  for (StringSet::const_iterator i = profileNames.begin();
91  i != profileNames.end(); ++i) {
92 
93  ProfileMap::const_iterator j = m_profiles.find(*i);
94 
95  if (j == m_profiles.end()) continue;
96 
97  const ProfilePair &pp(j->second);
98 
99  fprintf(stderr, "%s(%d):\n", *i, pp.first);
100 
101  fprintf(stderr, "\tCPU: \t%.9g ms/call \t[%d ms total]\n",
102  (((double)pp.second.first * 1000.0 /
103  (double)pp.first) / CLOCKS_PER_SEC),
104  int((double(pp.second.first) * 1000.0) / CLOCKS_PER_SEC));
105 
106  fprintf(stderr, "\tReal: \t%s ms \t[%s ms total]\n",
107  ((pp.second.second / pp.first) * 1000).toString().c_str(),
108  (pp.second.second * 1000).toString().c_str());
109 
110  WorstCallMap::const_iterator k = m_worstCalls.find(*i);
111  if (k == m_worstCalls.end()) continue;
112 
113  const TimePair &wc(k->second);
114 
115  fprintf(stderr, "\tWorst:\t%s ms/call \t[%d ms CPU]\n",
116  (wc.second * 1000).toString().c_str(),
117  int((double(wc.first) * 1000.0) / CLOCKS_PER_SEC));
118  }
119 
120  typedef std::multimap<RealTime, const char *> TimeRMap;
121  typedef std::multimap<int, const char *> IntRMap;
122 
123  TimeRMap totmap, avgmap, worstmap;
124  IntRMap ncallmap;
125 
126  for (ProfileMap::const_iterator i = m_profiles.begin();
127  i != m_profiles.end(); ++i) {
128  totmap.insert(TimeRMap::value_type(i->second.second.second, i->first));
129  avgmap.insert(TimeRMap::value_type(i->second.second.second /
130  i->second.first, i->first));
131  ncallmap.insert(IntRMap::value_type(i->second.first, i->first));
132  }
133 
134  for (WorstCallMap::const_iterator i = m_worstCalls.begin();
135  i != m_worstCalls.end(); ++i) {
136  worstmap.insert(TimeRMap::value_type(i->second.second,
137  i->first));
138  }
139 
140 
141  fprintf(stderr, "\nBy number of calls:\n");
142  for (IntRMap::const_iterator i = ncallmap.end(); i != ncallmap.begin(); ) {
143  --i;
144  fprintf(stderr, "%-40s %d\n", i->second, i->first);
145  }
146 
147  fprintf(stderr, "\nBy average:\n");
148  for (TimeRMap::const_iterator i = avgmap.end(); i != avgmap.begin(); ) {
149  --i;
150  fprintf(stderr, "%-40s %s ms\n", i->second,
151  (i->first * 1000).toString().c_str());
152  }
153 
154  fprintf(stderr, "\nBy worst case:\n");
155  for (TimeRMap::const_iterator i = worstmap.end(); i != worstmap.begin(); ) {
156  --i;
157  fprintf(stderr, "%-40s %s ms\n", i->second,
158  (i->first * 1000).toString().c_str());
159  }
160 
161  fprintf(stderr, "\nBy total:\n");
162  for (TimeRMap::const_iterator i = totmap.end(); i != totmap.begin(); ) {
163  --i;
164  fprintf(stderr, "%-40s %s ms\n", i->second,
165  (i->first * 1000).toString().c_str());
166  }
167 
168 #endif
169 }
170 
171 #ifndef NO_TIMING
172 
173 Profiler::Profiler(const char* c, bool showOnDestruct) :
174  m_c(c),
175  m_showOnDestruct(showOnDestruct),
176  m_ended(false)
177 {
178  m_startCPU = clock();
179 
180  struct timeval tv;
181  (void)gettimeofday(&tv, 0);
183 }
184 
185 void
187 {
188  clock_t elapsedCPU = clock() - m_startCPU;
189 
190  struct timeval tv;
191  (void)gettimeofday(&tv, 0);
192  RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime;
193 
194  cerr << "Profiler : id = " << m_c
195  << " - elapsed so far = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC)
196  << "ms CPU, " << elapsedTime << " real" << endl;
197 }
198 
200 {
201  if (!m_ended) end();
202 }
203 
204 void
206 {
207  clock_t elapsedCPU = clock() - m_startCPU;
208 
209  struct timeval tv;
210  (void)gettimeofday(&tv, 0);
211  RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime;
212 
213  Profiles::getInstance()->accumulate(m_c, elapsedCPU, elapsedTime);
214 
215  if (m_showOnDestruct)
216  cerr << "Profiler : id = " << m_c
217  << " - elapsed = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC)
218  << "ms CPU, " << elapsedTime << " real" << endl;
219 
220  m_ended = true;
221 }
222 
223 #endif
224 
static RealTime fromTimeval(const struct timeval &)
Definition: RealTimeSV.cpp:90
WorstCallMap m_worstCalls
Definition: Profiler.h:78
Profiles()
Definition: Profiler.cpp:41
void end()
Definition: Profiler.cpp:205
std::pair< clock_t, RealTime > TimePair
Definition: Profiler.h:71
static Profiles * m_instance
Definition: Profiler.h:81
~Profiles()
Definition: Profiler.cpp:45
Profiling classes.
Definition: Profiler.h:56
const char * m_c
Definition: Profiler.h:111
bool m_ended
Definition: Profiler.h:115
RealTime m_startTime
Definition: Profiler.h:113
ProfileMap m_profiles
Definition: Profiler.h:76
LastCallMap m_lastCalls
Definition: Profiler.h:77
clock_t m_startCPU
Definition: Profiler.h:112
static Profiles * getInstance()
Definition: Profiler.cpp:34
std::pair< int, TimePair > ProfilePair
Definition: Profiler.h:72
Profiler(const char *name, bool showOnDestruct=false)
Create a profile point instance that records time consumed against the given profiling point name...
Definition: Profiler.cpp:173
void accumulate(const char *id, clock_t time, RealTime rt)
Definition: Profiler.cpp:51
void dump() const
Definition: Profiler.cpp:74
void update() const
Definition: Profiler.cpp:186
bool m_showOnDestruct
Definition: Profiler.h:114
RealTime represents time values to nanosecond precision with accurate arithmetic and frame-rate conve...
Definition: RealTime.h:42