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 
32 Profiles* Profiles::m_instance = nullptr;
33 
getInstance()34 Profiles* Profiles::getInstance()
35 {
36     if (!m_instance) m_instance = new Profiles();
37 
38     return m_instance;
39 }
40 
Profiles()41 Profiles::Profiles()
42 {
43 }
44 
~Profiles()45 Profiles::~Profiles()
46 {
47     dump();
48 }
49 
50 #ifndef NO_TIMING
accumulate(const char * id,clock_t time,RealTime rt)51 void Profiles::accumulate(
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 
dump() const74 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 total:\n");
142     for (TimeRMap::const_iterator i = totmap.end(); i != totmap.begin(); ) {
143         --i;
144         fprintf(stderr, "%-40s  %s ms\n", i->second,
145                 (i->first * 1000).toString().c_str());
146     }
147 
148     fprintf(stderr, "\nBy average:\n");
149     for (TimeRMap::const_iterator i = avgmap.end(); i != avgmap.begin(); ) {
150         --i;
151         fprintf(stderr, "%-40s  %s ms\n", i->second,
152                 (i->first * 1000).toString().c_str());
153     }
154 
155     fprintf(stderr, "\nBy worst case:\n");
156     for (TimeRMap::const_iterator i = worstmap.end(); i != worstmap.begin(); ) {
157         --i;
158         fprintf(stderr, "%-40s  %s ms\n", i->second,
159                 (i->first * 1000).toString().c_str());
160     }
161 
162     fprintf(stderr, "\nBy number of calls:\n");
163     for (IntRMap::const_iterator i = ncallmap.end(); i != ncallmap.begin(); ) {
164         --i;
165         fprintf(stderr, "%-40s  %d\n", i->second, i->first);
166     }
167 
168 #endif
169 }
170 
171 #ifndef NO_TIMING
172 
Profiler(const char * c,bool showOnDestruct)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);
182     m_startTime = RealTime::fromTimeval(tv);
183 }
184 
185 void
update() const186 Profiler::update() const
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 
~Profiler()199 Profiler::~Profiler()
200 {
201     if (!m_ended) end();
202 }
203 
204 void
end()205 Profiler::end()
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 
225