forked from mooman/Raymond-Tree-Simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulator.cpp
108 lines (89 loc) · 2.72 KB
/
simulator.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
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
#include "main.h"
#include "queue.h"
#include "event.h"
#include "messenger.h"
#include "RaymondTree.h"
#include "site.h"
#include "simulator.h"
#include "MessageTracker.h"
Simulator::Simulator (int ns, int mt) {
nsites = ns;
max_time = mt * 100;
current_time = 0;
cs_entries = 0;
exit_cs_at = 0;
sync_delays = 0;
timeline = new Queue[max_time];
m = new Messenger(this);
rt = new RaymondTree(this, m);
//build a virtual tree construction
rt->build_tree(nsites);
rt->traverse(rt->get_root());
int i;
s = new Site *[nsites];
for (i = 0; i < nsites; i++) {
s[i] = (Site*) rt->postorder_q->dequeue();
//set Site properties based on the simulator
s[i]->site_id = i;
}
//set the holder variables
for (i = 0; i < nsites; i++) {
if (s[i]->parent == NULL) {
s[i]->holder = s[i]->site_id;
} else {
s[i]->holder = s[i]->parent->site_id;
}
}
holder_status();
}
int Simulator::get_current_time () {
return current_time;
}
void Simulator::new_event (string line) {
Event* e = new Event(line);
timeline[e->get_time()].enqueue(e);
}
void Simulator::new_event (int time, int to, int from, int action, Request* r) {
Event* e = new Event(time, to, from, action, r);
timeline[time].enqueue(e);
}
//print out current "tree"
void Simulator::holder_status () {
cout << endl << "****** Holder Status ********" << endl;
for (int i = 0; i < nsites; i++) {
cout << "Site " << i << ": " << s[i]->holder << endl;
}
cout << "***********************" << endl;
}
void Simulator::mark_exit_cs () {
exit_cs_at = current_time;
}
void Simulator::mark_enter_cs () {
cs_entries++;
sync_delays += current_time - exit_cs_at;
}
void Simulator::start () {
int i, trt, rt = 0;
Event* e;
//start main loop
for (i = current_time = 0; i < max_time; i++, current_time++) {
if (timeline[i].empty()) continue;
cout << "\n\n----------- Time " << current_time << " ----------" << endl;
while (!timeline[i].empty()) {
e = (Event *) timeline[i].dequeue();
s[e->get_site()]->process_event(e);
}
}
//stats
cout << "\n\nFinished simulation...... here are some stats:\n" << endl;
cout << "Average Response Times: " << endl;
for (i = 0; i < nsites; i++) {
trt = s[i]->average_response_time();
rt += trt;
cout << "Site " << i << ": " << trt << endl;
}
cout << "Overall Average: " << ((float) rt / nsites) << endl;
cout << "\nAverage Sync Delay: " << ((float) sync_delays / cs_entries) << endl;
cout << "Messages per site: " << endl;
MessageTracker::getInstance()->print_messages();
}