-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclock.cpp
68 lines (53 loc) · 1.73 KB
/
clock.cpp
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
/*
* Simulator clock
*/
#include "clock.h"
using namespace std;
Clock::Clock(simtime_picosec estimate)
: EventSource("clock"),
_estimate(estimate),
_elapsedRT(0.0),
_simTime(0),
_nEvents(0),
_realTime(0.0),
_ticks(0)
{
clock_gettime(CLOCK_MONOTONIC, &_lastTick);
EventList::Get().sourceIsPendingRel(*this, _estimate);
}
void
Clock::doNextEvent()
{
_ticks++;
// Find time elaped since last tick in ms.
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
double elapsed = (ts.tv_sec - _lastTick.tv_sec) +
(ts.tv_nsec - _lastTick.tv_nsec) / 1000000000.0;
_lastTick = ts;
_elapsedRT += elapsed;
// _estimate : elapsed :: ? : 1sec
double newEstimate = (_estimate / elapsed);
// Use some sort of interpolation for _estimate?
_estimate = newEstimate;
if (_ticks == 10) {
_ticks = 0;
uint64_t eventsPerSec = (uint64_t)(EventList::Get()._nEventsProcessed - _nEvents)
/ (_elapsedRT - _realTime) / 1000;
_nEvents = EventList::Get()._nEventsProcessed;
_realTime = _elapsedRT;
simtime_picosec current_ts = EventList::Get().now();
simtime_picosec simDiff = current_ts - _simTime;
_simTime = current_ts;
fprintf(stderr, "| simtime %9.6lf | realtime %6.1lf | speed %8.6lf @ %5lu Kops/s |\n",
timeAsSec(current_ts), _elapsedRT, timeAsSec(simDiff), eventsPerSec);
if (DEBUG_HTSIM) {
for (auto x : EventList::Get()._stats) {
cerr << x.first << "\t" << x.second.first << "\t" << x.second.second << endl;
}
}
} else {
fprintf(stderr, ".");
}
EventList::Get().sourceIsPendingRel(*this, _estimate);
}