Project

General

Profile

Statistics
| Branch: | Revision:

root / src / envir / speedometer.cc @ a3be1d55

History | View | Annotate | Download (2.58 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  SPEEDOMETER.CC - part of
3
//                     OMNeT++/OMNEST
4
//             Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#include <assert.h>
19
#include "speedometer.h"
20
21
USING_NAMESPACE
22
23
24
Speedometer::Speedometer()
25
{
26
    started = false;
27
}
28
29
void Speedometer::start(simtime_t t)
30
{
31
    // begin 1st interval
32
    events = 0;
33
    gettimeofday(&intvstart_walltime, NULL);
34
    intvstart_simtime = t;
35
36
    last_eventspersec = 0;
37
    last_eventspersimsec = 0;
38
    last_simsecpersec = 0;
39
40
    started = true;
41
}
42
43
void Speedometer::addEvent(simtime_t t)
44
{
45
    // start() mush have been called already
46
    assert(started);
47
48
    events++;
49
    current_simtime = t;
50
}
51
52
unsigned long Speedometer::getMillisSinceIntervalStart()
53
{
54
    // start() mush have been called already
55
    assert(started);
56
57
    timeval now;
58
    gettimeofday(&now, NULL);
59
    return timeval_msec(now - intvstart_walltime);
60
}
61
62
void Speedometer::beginNewInterval()
63
{
64
    // start() mush have been called already
65
    assert(started);
66
67
    timeval now;
68
    gettimeofday(&now, NULL);
69
    unsigned long elapsed_msecs = timeval_msec(now - intvstart_walltime);
70
    if (elapsed_msecs<10 || events==0)
71
    {
72
        // if we're called too often, refuse to give readings as probably
73
        // they'd be very misleading
74
        last_eventspersec = 0;
75
        last_simsecpersec = 0;
76
        last_eventspersimsec = 0;
77
    }
78
    else
79
    {
80
        double elapsed_sec = (double)elapsed_msecs/1000.0;
81
        simtime_t elapsed_simsec = current_simtime-intvstart_simtime;
82
83
        last_eventspersec = events / elapsed_sec;
84
        last_simsecpersec = SIMTIME_DBL(elapsed_simsec) / elapsed_sec;
85
        last_eventspersimsec = (elapsed_simsec==0) ? 0 : (events / SIMTIME_DBL(elapsed_simsec));
86
    }
87
    events = 0;
88
    intvstart_walltime = now;
89
    intvstart_simtime = current_simtime;
90
}
91
92
double Speedometer::getEventsPerSec()
93
{
94
    if (!started) return 0;
95
    return last_eventspersec;
96
}
97
98
double Speedometer::getEventsPerSimSec()
99
{
100
    if (!started) return 0;
101
    return last_eventspersimsec;
102
}
103
104
double Speedometer::getSimSecPerSec()
105
{
106
    if (!started) return 0;
107
    return last_simsecpersec;
108
}