-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart3.cpp
190 lines (162 loc) · 5.38 KB
/
part3.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
* Copyright (c) 2019, Aditya Rohan
* Copyright (c) 2019, Aniket Pandey
*
* Submitted to:
* CS622A: 2019-20 Fall Semester. Assignment 2
*/
#include <experimental/filesystem>
#include <iostream>
#include <fstream>
#include <list>
#include <map>
#include <memory>
#include <utility>
#include <vector>
using namespace std;
namespace fs = std::experimental::filesystem;
typedef long long int_t;
typedef std::vector <int_t> MyClassSet;
typedef std::map <int_t, MyClassSet> MyClassSetMap;
#define BLOCK_OFFSET 6
#define NUM_WAYS 16
#define NUM_SETS 2048
struct cell {
bool present; // Valid bit
int_t address; // Memory address
};
class Cache {
public:
typedef std::shared_ptr<Cache> Ptr;
// ctor
Cache(int num_sets, int num_ways);
// dtor
~Cache() = default;
// Retrieve block from memory on miss.
int_t add_block(int_t address);
// Check hit or miss for req. address
bool check_hit_or_miss(int_t address);
// Evict victim block from cache.
void invalidate_block(int_t address);
// Update LRU table on cache hit.
void update_on_hit(int_t address);
// Count of hits and misses
int_t hits = 0, misses = 0;
private:
// Number of sets in the cache.
int num_sets_;
// Number of ways in the cache.
int num_ways_;
// 2-d matrix, representing the cache org.
std::vector <std::vector <cell> > matrix_;
// List of sets for recording LRU status.
std::vector <std::list <int_t> > lru_set_;
};
//----------------------------------------------------------------------------
Cache::Cache(int num_sets, int num_ways)
: num_sets_(move(num_sets)),
num_ways_(move(num_ways)) {
// Create a 2d array and LRU record for the cache
matrix_.resize(num_sets, vector <cell>(num_ways));
lru_set_.resize(num_sets);
};
int_t Cache::add_block(int_t address) {
int set_num = address % num_sets_;
// Check if a way is free in the set
for (auto &set : matrix_.at(set_num)) {
if (set.present) continue;
// Found an empty slot
set.present = true;
set.address = address;
lru_set_.at(set_num).push_front(address);
return 0;
}
// All 'ways' in the set are valid, evict one
int_t evict_block = lru_set_.at(set_num).back();
lru_set_.at(set_num).pop_back();
for (auto &block : matrix_.at(set_num)) {
if (block.address != evict_block) continue;
// Found the block to be evicted
block.address = address;
lru_set_.at(set_num).push_front(address);
return evict_block;
}
abort(); // Something bad happened
}
bool Cache::check_hit_or_miss(int_t address) {
int set_num = address % num_sets_;
for (auto &block : matrix_.at(set_num)) {
if ((block.address == address) && (block.present))
return true;
}
return false;
}
void Cache::invalidate_block(int_t address) {
int set_num = address % num_sets_;
for (auto &block : matrix_.at(set_num)) {
if (block.address != address) continue;
// Found the block; Invalidate it
block.present = false;
lru_set_.at(set_num).remove(address);
return;
}
abort(); // Something bad happened
}
void Cache::update_on_hit(int_t address) {
int set_num = address % num_sets_;
// Move the block to the top of LRU set
lru_set_.at(set_num).remove(address);
lru_set_.at(set_num).push_front(address);
return;
}
//----------------------------------------------------------------------------
int main(int argc, char const *argv[]) {
int_t tid, block, time = 0;
int_t num_dist = 0, f_dist = 0, fn = 0;
string folder = "traces", output = "result/part3-cdf-lru-misses.out";
for (const auto& tracefile : fs::directory_iterator(folder)) {
std::ifstream infile (tracefile.path());
if (!infile.is_open()) {
cerr << "Tracefile " << tracefile.path().filename().string()
<< " could not be opened\n";
}
Cache::Ptr cache = make_shared<Cache> (NUM_SETS, NUM_WAYS);
MyClassSetMap maps;
map <int_t, int_t> distance;
cout << "Processing file " << tracefile.path().filename().string()
<< endl;
while (infile >> tid >> block) {
block = block >> BLOCK_OFFSET;
// Simulate the block through 2MB 16-Way cache
if (cache->check_hit_or_miss(block)) {
(cache->hits)++;
cache->update_on_hit(block);
} else {
(cache->misses)++;
cache->add_block(block);
maps[block].emplace_back(time);
time++;
}
}
// Segregate all blocks into their respective 'distances'
for(auto& elem : maps) {
for (int i = 0; i < elem.second.size() - 1; ++i) {
int_t cd = elem.second[i+1] - elem.second[i];
distance[cd]++;
num_dist++;
}
}
// Result Compilation
ofstream myfile;
myfile.open (output);
for(auto& elem : distance) {
f_dist += elem.second;
myfile << elem.first << ": " << (float) f_dist/(float)num_dist
<< endl;
}
myfile.close();
cout << "\nNumber of hits: " << cache->hits << "\nNumber of misses: "
<< cache->misses << "\n\n";
}
return 0;
}