-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
132 lines (114 loc) · 3.76 KB
/
main.cc
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
121
122
123
124
125
126
127
128
129
130
131
132
#include <iostream>
#include <fstream>
#include <sstream>
#include "parser.hh"
#include "workqueue.hh"
const int NUMBER_OF_THREADS = 4;
const int QUEUE_SIZE = 16;
const char *output_file = "Statistics.txt";
int main(int argc, char *argv[])
{
std::mutex mtx;
std::condition_variable cond;
amazon::GlobalStats stats[amazon::NUM_INSTANCES];
amazon::Workqueue::Event event;
amazon::Data data;
if (argc != 2) {
std::cout<< "You should specify the input file - filename" << std::endl;
std::cout<< "usage: zonestats filename" << std::endl;
return -1;
}
std::ifstream infile(argv[1]);
if (! infile.is_open()) {
std::cout<< "Failed to open file" << std::endl;
return -1;
}
if (! infile.good()) {
std::cout<< "File is bad" << std::endl;
return -1;
}
amazon::Workqueue workqueue(QUEUE_SIZE, NUMBER_OF_THREADS, cond, stats);
amazon::Parser::init();
int line_num = 1;
std::string line;
while (getline(infile, line)) {
if (line.size() < 1)
continue;
std::cout<<line<<std::endl;
switch (amazon::Parser::parse(line, data)) {
case amazon::Parser::ErrorCode::BAD_CHARACTER:
std::cout<< "ERROR! line: " << line_num
<< " in the wrong format" << std::endl;
break;
case amazon::Parser::ErrorCode::SLOTS_INCONSISTENT:
std::cout<< "ERROR! line: "
<< line_num
<< " the specified slots do not correspond to those available"
<< std::endl;
break;
case amazon::Parser::ErrorCode::SUCCESS:
std::cout<< "hostId: " <<data.host_id << " instanceType: "
<< amazon::instance_name[data.instance_type] << " slots: "
<< data.nslots << " ";
for (int i = 0; i < data.nslots; i++)
std::cout << " " << data.slot_state[i];
std::cout<<std::endl;
event.type = amazon::Workqueue::ev_type::WORK;
event.data = data;
workqueue.push(event);
break;
}
std::cout<<std::endl;
++line_num;
}
event.type = amazon::Workqueue::ev_type::STOP;
for (int i = 0; i < NUMBER_OF_THREADS; i++)
workqueue.push(event);
std::ofstream outfile(output_file);
if (! outfile.is_open()) {
std::cout<< "Failed to open file" << std::endl;
return -1;
}
if (! outfile.good()) {
std::cout<< "File is bad" << std::endl;
return -1;
}
/* wait for workers to finish */
{
std::unique_lock<std::mutex> lock(mtx);
while (workqueue.active() > 0)
cond.wait(lock);
}
{
int m1_empty = stats[amazon::Instance::M1].empty.load();
int m2_empty = stats[amazon::Instance::M2].empty.load();
int m3_empty = stats[amazon::Instance::M3].empty.load();
std::stringstream empty;
empty << "EMPTY: M1=<" << m1_empty << ">;M2=<" << m2_empty << ">;M3=<" << m3_empty << ">;";
outfile << empty.str() <<std::endl;
}
{
int m1_full = stats[amazon::Instance::M1].full.load();
int m2_full = stats[amazon::Instance::M2].full.load();
int m3_full = stats[amazon::Instance::M3].full.load();
std::stringstream full;
full << "FULL: M1=<" << m1_full << ">;M2=<" << m2_full << ">;M3=<" << m3_full << ">;";
outfile << full.str() <<std::endl;
}
{
int m1_most_filled_first = stats[amazon::Instance::M1].most_filled.first.load();
int m1_most_filled_second = stats[amazon::Instance::M1].most_filled.second.load();
int m2_most_filled_first = stats[amazon::Instance::M2].most_filled.first.load();
int m2_most_filled_second = stats[amazon::Instance::M2].most_filled.second.load();
int m3_most_filled_first = stats[amazon::Instance::M3].most_filled.first.load();
int m3_most_filled_second = stats[amazon::Instance::M3].most_filled.first.load();
std::stringstream most_filled;
most_filled <<"MOST FILLED: M1=<" << m1_most_filled_first
<< "," << m1_most_filled_second << ">;M2=<"
<< m2_most_filled_first << ","
<< m2_most_filled_second << ">;M3=<"
<< m3_most_filled_first << "," << m3_most_filled_second << ">;";
outfile << most_filled.str();
}
return EXIT_SUCCESS;
}