-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheventlist.cpp
82 lines (68 loc) · 1.97 KB
/
eventlist.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
* Simulator eventlist
*/
#include "eventlist.h"
using namespace std;
EventList *EventList::instance = NULL;
EventList&
EventList::Get()
{
if (instance == NULL) {
instance = new EventList;
instance->_nEventsProcessed = 0;
instance->_endtime = 0;
instance->_lasteventtime = 0;
}
return *instance;
}
void
EventList::setEndtime(simtime_picosec endtime)
{
_endtime = endtime;
}
bool
EventList::doNextEvent()
{
if (_pendingsources.empty()) {
return false;
}
simtime_picosec nexteventtime = _pendingsources.begin()->first;
EventSource *nextsource = _pendingsources.begin()->second;
_pendingsources.erase(_pendingsources.begin());
assert(nexteventtime >= _lasteventtime);
// set this before calling doNextEvent, so that this::now() is accurate
_lasteventtime = nexteventtime;
// Measure how much time event takes for debugging purposes.
struct timespec t1, t2;
if (DEBUG_HTSIM) {
clock_gettime(CLOCK_MONOTONIC, &t1);
}
// Process the event.
nextsource->doNextEvent();
_nEventsProcessed++;
if (DEBUG_HTSIM) {
clock_gettime(CLOCK_MONOTONIC, &t2);
uint64_t diff = (t2.tv_sec - t1.tv_sec) * 1000000000 + (t2.tv_nsec - t1.tv_nsec);
string id = nextsource->str();
if (id.find_first_of("0123456789") != string::npos) {
id.erase(id.find_first_of("0123456789"));
}
if (_stats.find(id) == _stats.end()) {
_stats[id] = make_pair(1, diff/1.0);
} else {
_stats[id].second = _stats[id].second * _stats[id].first;
_stats[id].first += 1;
_stats[id].second = (_stats[id].second + diff) / _stats[id].first;
}
}
return true;
}
void
EventList::sourceIsPending(EventSource &src,
simtime_picosec when)
{
assert(when >= now());
if (_endtime == 0 || when <= _endtime) {
_pendingsources.insert(make_pair(when, &src));
}
}