File: Profiler.cpp

package info (click to toggle)
sonic-visualiser 2.5~repack1-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 13,596 kB
  • ctags: 9,853
  • sloc: cpp: 93,843; ansic: 1,138; sh: 1,012; xml: 64; makefile: 35
file content (228 lines) | stat: -rw-r--r-- 6,162 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*-  vi:set ts=8 sts=4 sw=4: */

/*
    Sonic Visualiser
    An audio file viewer and annotation editor.
    Centre for Digital Music, Queen Mary, University of London.
    
    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.  See the file
    COPYING included with this distribution for more information.
*/

/*
   This is a modified version of a source file from the 
   Rosegarden MIDI and audio sequencer and notation editor.
   This file copyright 2000-2006 Chris Cannam, Guillaume Laurent,
   and QMUL.
*/

#include <iostream>
#include "Profiler.h"

#include <cstdio>

#include <vector>
#include <algorithm>
#include <set>
#include <map>

Profiles* Profiles::m_instance = 0;

Profiles* Profiles::getInstance()
{
    if (!m_instance) m_instance = new Profiles();
    
    return m_instance;
}

Profiles::Profiles()
{
}

Profiles::~Profiles()
{
    dump();
}

void Profiles::accumulate(
#ifndef NO_TIMING
    const char* id, clock_t time, RealTime rt
#else
    const char*, clock_t, RealTime
#endif
)
{
#ifndef NO_TIMING    
    ProfilePair &pair(m_profiles[id]);
    ++pair.first;
    pair.second.first += time;
    pair.second.second = pair.second.second + rt;

    TimePair &lastPair(m_lastCalls[id]);
    lastPair.first = time;
    lastPair.second = rt;

    TimePair &worstPair(m_worstCalls[id]);
    if (time > worstPair.first) {
        worstPair.first = time;
    }
    if (rt > worstPair.second) {
        worstPair.second = rt;
    }
#endif
}

void Profiles::dump() const
{
#ifndef NO_TIMING

    fprintf(stderr, "Profiling points:\n");

    fprintf(stderr, "\nBy name:\n");

    typedef std::set<const char *, std::less<std::string> > StringSet;

    StringSet profileNames;
    for (ProfileMap::const_iterator i = m_profiles.begin();
         i != m_profiles.end(); ++i) {
        profileNames.insert(i->first);
    }

    for (StringSet::const_iterator i = profileNames.begin();
         i != profileNames.end(); ++i) {

        ProfileMap::const_iterator j = m_profiles.find(*i);

        if (j == m_profiles.end()) continue;

        const ProfilePair &pp(j->second);

        fprintf(stderr, "%s(%d):\n", *i, pp.first);

        fprintf(stderr, "\tCPU:  \t%.9g ms/call \t[%d ms total]\n",
                (((double)pp.second.first * 1000.0 /
		  (double)pp.first) / CLOCKS_PER_SEC),
                int((double(pp.second.first) * 1000.0) / CLOCKS_PER_SEC));

        fprintf(stderr, "\tReal: \t%s ms      \t[%s ms total]\n",
                ((pp.second.second / pp.first) * 1000).toString().c_str(),
                (pp.second.second * 1000).toString().c_str());

        WorstCallMap::const_iterator k = m_worstCalls.find(*i);
        if (k == m_worstCalls.end()) continue;
        
        const TimePair &wc(k->second);

        fprintf(stderr, "\tWorst:\t%s ms/call \t[%d ms CPU]\n",
                (wc.second * 1000).toString().c_str(),
                int((double(wc.first) * 1000.0) / CLOCKS_PER_SEC));
    }

    typedef std::multimap<RealTime, const char *> TimeRMap;
    typedef std::multimap<int, const char *> IntRMap;
    
    TimeRMap totmap, avgmap, worstmap;
    IntRMap ncallmap;

    for (ProfileMap::const_iterator i = m_profiles.begin();
         i != m_profiles.end(); ++i) {
        totmap.insert(TimeRMap::value_type(i->second.second.second, i->first));
        avgmap.insert(TimeRMap::value_type(i->second.second.second /
                                           i->second.first, i->first));
        ncallmap.insert(IntRMap::value_type(i->second.first, i->first));
    }

    for (WorstCallMap::const_iterator i = m_worstCalls.begin();
         i != m_worstCalls.end(); ++i) {
        worstmap.insert(TimeRMap::value_type(i->second.second,
                                             i->first));
    }


    fprintf(stderr, "\nBy total:\n");
    for (TimeRMap::const_iterator i = totmap.end(); i != totmap.begin(); ) {
        --i;
        fprintf(stderr, "%-40s  %s ms\n", i->second,
                (i->first * 1000).toString().c_str());
    }

    fprintf(stderr, "\nBy average:\n");
    for (TimeRMap::const_iterator i = avgmap.end(); i != avgmap.begin(); ) {
        --i;
        fprintf(stderr, "%-40s  %s ms\n", i->second,
                (i->first * 1000).toString().c_str());
    }

    fprintf(stderr, "\nBy worst case:\n");
    for (TimeRMap::const_iterator i = worstmap.end(); i != worstmap.begin(); ) {
        --i;
        fprintf(stderr, "%-40s  %s ms\n", i->second,
                (i->first * 1000).toString().c_str());
    }

    fprintf(stderr, "\nBy number of calls:\n");
    for (IntRMap::const_iterator i = ncallmap.end(); i != ncallmap.begin(); ) {
        --i;
        fprintf(stderr, "%-40s  %d\n", i->second, i->first);
    }

#endif
}

#ifndef NO_TIMING    

Profiler::Profiler(const char* c, bool showOnDestruct) :
    m_c(c),
    m_showOnDestruct(showOnDestruct),
    m_ended(false)
{
    m_startCPU = clock();

    struct timeval tv;
    (void)gettimeofday(&tv, 0);
    m_startTime = RealTime::fromTimeval(tv);
}

void
Profiler::update() const
{
    clock_t elapsedCPU = clock() - m_startCPU;

    struct timeval tv;
    (void)gettimeofday(&tv, 0);
    RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime;

    cerr << "Profiler : id = " << m_c
	 << " - elapsed so far = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC)
	 << "ms CPU, " << elapsedTime << " real" << endl;
}    

Profiler::~Profiler()
{
    if (!m_ended) end();
}

void
Profiler::end()
{
    clock_t elapsedCPU = clock() - m_startCPU;

    struct timeval tv;
    (void)gettimeofday(&tv, 0);
    RealTime elapsedTime = RealTime::fromTimeval(tv) - m_startTime;

    Profiles::getInstance()->accumulate(m_c, elapsedCPU, elapsedTime);

    if (m_showOnDestruct)
        cerr << "Profiler : id = " << m_c
             << " - elapsed = " << ((elapsedCPU * 1000) / CLOCKS_PER_SEC)
	     << "ms CPU, " << elapsedTime << " real" << endl;

    m_ended = true;
}
 
#endif