aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/speedometer.cpp
blob: 75e424e22777bba2372f955c3718c807f5aa7ddd (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
#include "bu/speedometer.h"
#include "bu/mutexlocker.h"
#include "bu/sio.h"

#include <sys/time.h>
#include <string.h>

Bu::Speedometer::Speedometer( int64_t iBucketMs, int32_t iBucketCount ) :
    aiBucket( NULL ),
    iTotal( 0 ),
    iCapacity( iBucketCount ),
    iFill( 0 ),
    iStart( 0 ),
    iCurrent( 0 ),
    iBucketMs( iBucketMs ),
    iStartTime( -1 ),
    iWindowSizeMs( iBucketMs*iBucketCount ),
    iLastUpdate( -1 )
{
    aiBucket = new uint32_t[iCapacity];
    memset( aiBucket, 0, sizeof(uint32_t)*iCapacity );
}

Bu::Speedometer::~Speedometer()
{
    delete[] aiBucket;
    aiBucket = NULL;
}

void Bu::Speedometer::addEvent()
{
    Bu::MutexLocker l( mLock );
    int64_t iNow = getCurrentTime();
    _updateBuckets( iNow );
    if( iStartTime < 0 )
    {
        iStartTime = iNow;
        iFill = 1;
        aiBucket[iCurrent] = 1;
        iTotal = 1;
    }
    else
    {
        aiBucket[iCurrent]++;
        iTotal++;
    }
    iLastUpdate = iNow;
}

void Bu::Speedometer::debug()
{
    Bu::MutexLocker l( mLock );
    Bu::print("%1 of %2: %3:%4: ").
        arg( iFill ).arg( iCapacity ).arg( iStart ).arg( iCurrent );
    for( int j = 0; j < iFill; j++ )
    {
        Bu::print("%1 | ").arg( aiBucket[(j+iStart)%iCapacity] );
    }
    Bu::println(" total = %1").arg( iTotal );
}

void Bu::Speedometer::_updateBuckets( int64_t iNow )
{
    if( iNow > iLastUpdate+iWindowSizeMs )
    {
        iLastUpdate = iStartTime = -1;
        iFill = 0;
        iStart = iCurrent = 0;
        aiBucket[iCurrent] = 0;
        iTotal = 0;
    }
    else 
    {
        int64_t iSkip = (iNow-(iStartTime+iBucketMs*iFill));
        if( iSkip < 0 )
            return;
        iSkip = (iSkip/iBucketMs) + ((iSkip%iBucketMs)?(1):(0));

        for( int64_t j = 0; j < iSkip; j++ )
        {
            iCurrent = (iCurrent+1)%iCapacity;
            if( iFill < iCapacity )
            {
                iFill++;
            }
            else
            {
                iTotal -= aiBucket[iStart];
                aiBucket[iStart] = 0;
                iStart = (iStart+1)%iCapacity;
                iStartTime += iBucketMs;
            }
        }
    }
}

double Bu::Speedometer::getSpeed()
{
    Bu::MutexLocker l( mLock );
    _updateBuckets( getCurrentTime() );

    if( iStartTime < 0 )
        return 0.0;

    return (double)iTotal / (double)(iFill*iBucketMs);
}

int32_t Bu::Speedometer::getTotalEvents()
{
    Bu::MutexLocker l( mLock );
    return iTotal;
}

int64_t Bu::Speedometer::getCurrentTime() const
{
    struct timeval tv;
    gettimeofday( &tv, NULL );
    return tv.tv_sec*1000 + tv.tv_usec/1000;
}