aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/profiler.cpp
blob: 1b176b96c36c3a334d53440b6dabbfded3c8669c (plain)
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
#include "bu/profiler.h"
#include "bu/mutexlocker.h"
#include "bu/sio.h"

#include <sys/time.h>

Bu::Profiler::Profiler()
{
}

Bu::Profiler::~Profiler()
{
    printReport();
}

uint64_t Bu::Profiler::getMicroTime()
{
    struct timeval tv;
    gettimeofday( &tv, NULL );
    return ((uint64_t)tv.tv_sec)*1000000 + ((uint64_t)tv.tv_usec);
}

void Bu::Profiler::startEvent( const Bu::Blob &bName )
{
    Bu::MutexLocker l( mLock );
    if( hEvent.has( bName ) )
    {
        hEvent.get( bName ).append( Event() );
    }
    else
    {
        hEvent.insert( bName, EventList( Event() ) );
    }
}

void Bu::Profiler::endEvent( const Bu::Blob &bName )
{
    Bu::MutexLocker l( mLock );
    if( !hEvent.has( bName ) )
        return;

    if( hEvent.get( bName ).isEmpty() )
        return;

    hEvent.get( bName ).last().end();
}

void Bu::Profiler::printReport() const
{
    class Breakdown
    {
    public:
        Breakdown( const Bu::Blob &bName ) :
            bName( bName ), uCount( 0 ), uTotal( 0 ), uMin( 0 ), uMax( 0 ) { }
        Breakdown( const Breakdown &rSrc ) :
            bName( rSrc.bName ), uCount( rSrc.uCount ), uTotal( rSrc.uTotal ),
            uMin( rSrc.uMin ), uMax( rSrc.uMax ) { }
        ~Breakdown() { }

        bool operator<( const Breakdown &rhs ) const
        {
            return uTotal > rhs.uTotal;
        }
        Breakdown &operator=( const Breakdown &rhs )
        {
            bName = rhs.bName;
            uCount = rhs.uCount;
            uTotal = rhs.uTotal;
            uMin = rhs.uMin;
            uMax = rhs.uMax;
            return *this;
        }
        
        void add( uint64_t uSpan )
        {
            if( uCount == 0 )
            {
                uMin = uMax = uSpan;
            }
            else
            {
                if( uMin > uSpan )
                    uMin = uSpan;
                if( uMax < uSpan )
                    uMax = uSpan;

            }
            uCount++;
            uTotal += uSpan;
        }

        Bu::Blob bName;
        uint64_t uCount;
        uint64_t uTotal;
        uint64_t uMin;
        uint64_t uMax;
    };
    Bu::MutexLocker l( mLock );

    int iTitleLen = 0;
    Bu::List<Breakdown> lBreakdown;
    for( EventListHash::const_iterator iType = hEvent.begin(); iType; iType++ )
    {
        Breakdown b( iType.getKey() );
        if( iTitleLen < b.bName.getSize() )
            iTitleLen = b.bName.getSize();
        for( EventList::const_iterator iEv = iType.getValue().begin();
                iEv; iEv++ )
        {
            b.add( (*iEv).getSpan() );
        }
        lBreakdown.append( b );
    }

    lBreakdown.sort();

    Bu::println("%1 %2 %3 %4 %5 %6")
        .arg("Title",   Bu::Fmt(iTitleLen) )
        .arg("Cnt",     Bu::Fmt(5) )
        .arg("Total",   Bu::Fmt(9) )
        .arg("Avg",     Bu::Fmt(9) )
        .arg("Max",     Bu::Fmt(9) )
        .arg("Min",     Bu::Fmt(9) );
    for( Bu::List<Breakdown>::iterator i = lBreakdown.begin(); i; i++ )
    {
        Bu::println("%1 %2 %3.%4 %5.%6 %7.%8 %9.%10")
            .arg( (*i).bName.getData(), Bu::Fmt(iTitleLen) )
            .arg( (*i).uCount, Bu::Fmt(5) )
            .arg( ((*i).uTotal/1000000), Bu::Fmt(5).right() )
            .arg( ((*i).uTotal/1000)%1000, Bu::Fmt(3).fill('0').right() )
            .arg( ((*i).uTotal/(*i).uCount)/1000000, Bu::Fmt(5).right() )
            .arg( (((*i).uTotal/(*i).uCount)/1000)%1000, Bu::Fmt(3).right().fill('0') )
            .arg( (*i).uMax/1000000, Bu::Fmt(5).right() )
            .arg( ((*i).uMax/1000)%1000, Bu::Fmt(3).right().fill('0') )
            .arg( (*i).uMin/1000000, Bu::Fmt(5).right() )
            .arg( ((*i).uMin/1000)%1000, Bu::Fmt(3).right().fill('0') )
            ;
    }
}

Bu::Profiler::Event::Event() :
    iStart( Bu::Profiler::getMicroTime() ),
    iStop( 0 )
{
}

Bu::Profiler::Event::Event( const Event &rSrc ) :
    iStart( rSrc.iStart ),
    iStop( rSrc.iStop )
{
}

Bu::Profiler::Event::~Event()
{
}

void Bu::Profiler::Event::end()
{
    if( iStop == 0 )
        iStop = Bu::Profiler::getMicroTime();
}

bool Bu::Profiler::Event::hasEnded() const
{
    return iStop != 0;
}

uint64_t Bu::Profiler::Event::getStart() const
{
    return iStart;
}

uint64_t Bu::Profiler::Event::getEnd() const
{
    return iStop;
}

uint64_t Bu::Profiler::Event::getSpan() const
{
    if( iStop < iStart )
        return 0;
    return iStop-iStart;
}