-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulate.cpp
More file actions
64 lines (53 loc) · 1.93 KB
/
Copy pathsimulate.cpp
File metadata and controls
64 lines (53 loc) · 1.93 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
#include "simulate.hpp"
using namespace std;
double* simulate(Scheduler* sched, int numCPUBound, int numIOBound, int numCycles) {
List<Process*>* procs = new ArrayList<Process *>();
Process* p = NULL;
int i;
int popNextCalled = 0;
for (i = 1; i <= numCPUBound; i++) {
procs->pushBack((p = new CPUBoundProcess(i)));
sched->addProcess(p);
}
for (; i <= numCPUBound + numIOBound; i++) {
procs->pushBack((p = new IOBoundProcess(i)));
sched->addProcess(p);
}
auto startTime = chrono::system_clock::now();
for(int iter = 1; iter <= numCycles;) {
// cout << iter << endl;
Process* next = sched->popNext(iter);
popNextCalled++;
if (!next) {
iter += 10;
} else {
int cyclePassed = next->useCPU(iter, 10);
iter += cyclePassed;
sched->addProcess(next);
}
}
auto dur = chrono::system_clock::now() - startTime;
double cput_cpubd = 0, wt_cpubd = 0, cput_iobd = 0, wt_iobd = 0;
for (i = 1; i <= numCPUBound; i++) {
Process* p = procs->getFront();
cput_cpubd += p->getCPUTime();
wt_cpubd += p->getWaitTime(numCycles);
procs->popFront();
delete p;
}
for (; i <= numCPUBound + numIOBound; i++) {
Process* p = procs->getFront();
cput_iobd += p->getCPUTime();
wt_iobd += p->getWaitTime(numCycles);
procs->popFront();
delete p;
}
delete procs;
double* res = new double[5];
res[0] = chrono::duration_cast<chrono::nanoseconds>(dur).count() / popNextCalled; // ns simulation / popnext called
res[1] = cput_cpubd / numCPUBound; // avg cpu time of al cpubound procs
res[2] = wt_cpubd / numCPUBound; // avg wait time of al cpubound procs
res[3] = cput_iobd / numIOBound; // avg cpu time of al iobound procs
res[4] = wt_iobd / numIOBound; // avg wait time of al iobound procs
return res;
}