-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemulator.cpp
More file actions
101 lines (92 loc) · 2.58 KB
/
emulator.cpp
File metadata and controls
101 lines (92 loc) · 2.58 KB
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
#include "task.h"
const char* const fileResources = "resources.txt";
const char* const fileJobs = "jobs.txt";
const char* const fileStatistics = "statistics.txt";
const char* const fileSchedule = "schedule.txt";
template<typename T>
void outStat(std::ostream& o, const std::string& name, const T& distrib)
{
o << "=== stat: " << name << "\n";
distrib.toStream(o);
}
void Emulator::start()
{
readData();
execute();
outStatistics();
outSchedule();
}
void Emulator::readData()
{
readFile(fileResources, resourcesStream);
readFile(fileJobs, jobsStream);
}
void Emulator::execute()
{
while (!resourcesStream.empty())
{
LOG("timestamp: " << timestamp);
executeStep();
collectStatistics();
++ timestamp;
}
}
void Emulator::executeStep()
{
std::vector<ResourceTuple> resources = getFor(timestamp, resourcesStream);
std::vector<JobTuple> jobs = getFor(timestamp, jobsStream);
scheduled = std::move(
(!resources.empty() || !jobs.empty()) ? scheduler.newEvent(resources, jobs) : std::vector<JobTuple>());
LOG("scheduled jobs: " << scheduled.size());
jobsSchedule[timestamp] = scheduled;
}
void Emulator::collectStatistics()
{
int workDone = 0;
for(const JobTuple& j: scheduled)
{
int work = j.resources * j.duration;
workDone += work;
workDoneLatencyDistrib.add(work, timestamp - j.timestamp);
resourceAllocatedLatencyDistrib.add(j.resources, timestamp - j.timestamp);
}
workDoneDistrib.add(timestamp, workDone);
const auto& nodes = scheduler.nodes();
int totalResources = 0;
for (const auto& n: nodes)
{
int nNode = n.first;
int nResources = n.second;
totalResources += nResources;
nodesDistrib[nNode].add(timestamp, nResources);
}
totalNodesDistrib.add(timestamp, totalResources);
}
void Emulator::outStatistics()
{
LOG("saving statistics");
std::ofstream o(fileStatistics);
VERIFY(o, "cannot open file");
outStat(o, "work done", workDoneDistrib);
outStat(o, "work done vs latency", workDoneLatencyDistrib);
outStat(o, "resource allocated vs latency", resourceAllocatedLatencyDistrib);
for (const auto& d: nodesDistrib)
outStat(o, "node: " + std::to_string(d.first), d.second);
outStat(o, "overall nodes (total)", totalNodesDistrib);
o << "unscheduled jobs: " << scheduler.unscheduledJobs() << std::endl;
o.close();
}
void Emulator::outSchedule()
{
LOG("saving schedule");
std::ofstream o(fileSchedule);
VERIFY(o, "cannot open file");
for (const auto& s: jobsSchedule)
{
o << "timestamp: " << s.first;
for (const JobTuple& j: s.second)
o << "\n #" << j.id << ": " << j.timestamp << " " << j.resources << " " << j.duration;
o << std::endl;
}
o.close();
}