-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.cpp
94 lines (77 loc) · 2.85 KB
/
processor.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
#include "processor.h"
#include <math.h>
Processor::Processor(std::string id, BusController *busController){
cacheController = new CacheController(id, busController);
Processor::id = id;
std::random_device rd;
generator.seed(rd());
}
size_t Processor::getRandomFromDistribution(DistributionType dist, size_t limit){
size_t n;
if (dist == DistributionType::BINOMIAL){
std::binomial_distribution<int> distribution(limit,B_PROB_SUCC);
n = distribution(generator);
} else if (dist == DistributionType::GEOMETRIC){
std::geometric_distribution<int> distribution(G_PROB_SUCC);
n = distribution(generator);
} else if (dist == DistributionType::POISSON){
std::poisson_distribution<int> distribution(P_MEAN);
n = distribution(generator);
} else if (dist == DistributionType::NORMAL){
std::normal_distribution<double> distribution(N_MEAN,N_STDDEV);
n = int(distribution(generator));
} else {
n = 0;
}
//Limit conditions
if (n > limit) return limit;
if (n < 0) return 0;
return n;
}
void Processor::previewDistribution(DistributionType dist){
size_t p[16]={};
for (int i=0; i<10000; ++i) {
size_t n = getRandomFromDistribution(dist, 15);
++p[n];
}
for (int i=0; i<16; ++i){
if (i < 10) std::cout << "0";
std::cout << i << ": " << std::string(p[i]*100/10000,'*') << std::endl;
}
}
CacheBlock* Processor::getCacheBlock(){
return cacheController->getCacheBlock();
}
void Processor::run(bool *clk, std::condition_variable *cv){
std::mutex m;
std::mutex m2;
//Bernoulli Distribution
std::bernoulli_distribution mem_distribution(MEM_ACCESS_PROB);
std::bernoulli_distribution write_distribution(WRITE_PROB);
//Main processor loop
while(1){
//Lock and wait for clock
std::unique_lock<std::mutex> lock(m);
while (!*clk) cv->wait(lock);
if(mem_distribution(generator)){ //Memory acess operation
size_t add = getRandomFromDistribution(CURRENT_DISTRIBUTION, DATA_SIZE);
CacheAddress c_add;
//Direct map
c_add.index = add%BLOCK_SIZE;
c_add.tag = add >> int(log2(BLOCK_SIZE));
if(write_distribution(generator)){ //Write to Memory
guiout = "Write to address " + std::to_string(add);
cacheController->access(c_add, id, AccessType::WRITE);
} else { //Read from Memory
guiout = "Read from address " + std::to_string(add);
cacheController->access(c_add, id, AccessType::READ);
}
} else { //Process operation
guiout = "Process";
cacheController->guiout = "";
}
//Lock and wait for clock
std::unique_lock<std::mutex> lock2(m2);
while (*clk) cv->wait(lock2);
}
}