forked from dasebe/webcachesim
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathwebcachesim.cpp
More file actions
155 lines (119 loc) · 4.51 KB
/
webcachesim.cpp
File metadata and controls
155 lines (119 loc) · 4.51 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
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
#include <fstream>
#include <string>
#include <regex>
#include "caches/lru_variants.h"
//#include "caches/gd_variants.h"
#include "caches/lfo_cache.h"
#include "request.h"
#include "caches/optimal.h"
using namespace std;
uint64_t run_model(vector<SimpleRequest> & prev_requests,
vector<vector<double>> & prev_features,
unique_ptr<Cache> & webcache,
ifstream & infile,
size_t batch_size,
ofstream & outfile) {
uint64_t time, id, size;
uint64_t counter = 0;
uint64_t hit = 0;
while (!infile.eof()) {
if (counter >= batch_size) {
break;
}
infile >> time >> id >> size;
SimpleRequest req(id, size, time);
prev_requests[counter] = req;
vector<double> prev_feature = webcache->get_lfo_feature(&req).get_vector();
if (!prev_feature.empty()) {
prev_features[counter] = prev_feature;
}
if (webcache->lookup(&req)) {
hit++;
outfile << id << ' ' << size << ' ' << '1' << ' ' << '1' << std::endl;
} else {
req.setFeatureVector(prev_feature);
webcache->admit(&req);
if (webcache->lookup(&req)) {
outfile << id << ' ' << size << ' ' << '1' << ' ' << '0' << std::endl;
} else {
outfile << id << ' ' << size << ' ' << '0' << ' ' << '0' << std::endl;
}
}
counter += 1;
}
return hit;
}
void run_simulation(const string path, const string cacheType, const uint64_t cache_size,
bool use_exponential_time_gap, bool use_rl_cache) {
unique_ptr<Cache> webcache = Cache::create_unique(cacheType);
if(webcache == nullptr)
exit(0);
// configure cache size
webcache->setSize(cache_size);
webcache->setUseExponentialTimeGap(use_exponential_time_gap);
webcache->setUseRLCacheFeatures(use_rl_cache);
ifstream infile;
std::ofstream outfile;
size_t batch_size = 1000000;
bool changed_to_lfo = false;
bool read_file_optimal = true;
string od_filepath = "../opt.decisions";
ifstream opt_infile;
vector<SimpleRequest> prev_requests(batch_size);
vector<vector<double >> prev_features(batch_size);
size_t iterations = 0;
infile.open(path);
outfile.open("../cache_decisions.out");
opt_infile.open(od_filepath);
while (!infile.eof()) {
cout << "Iteration: " << iterations << std::endl;
webcache->reset();
uint64_t hits = run_model(prev_requests, prev_features, webcache, infile, batch_size, outfile);
cout << "[+] Number of hits: " << hits << "\n";
if (iterations == 49) {
return;
}
if (prev_features.size() != 0 && prev_requests.size() != 0 && prev_features.size() == prev_requests.size()){
if (!changed_to_lfo) {
webcache = Cache::create_unique("LFO");
webcache->setSize(cache_size);
webcache->setUseExponentialTimeGap(use_exponential_time_gap);
webcache->setUseRLCacheFeatures(use_rl_cache);
changed_to_lfo = !changed_to_lfo;
}
if (iterations % 10 == 0) {
cout << "[+] Computing optimal decisions"<< std::endl;
vector<double> optimal_decisions;
if (read_file_optimal) {
optimal_decisions = getOptimalDecisionsFromFile(batch_size, opt_infile);
} else {
optimal_decisions = getOptimalDecisions(prev_requests, webcache->getSize());
}
cout << "[+] Calling Train Light GBM at iteration " << iterations << endl;
webcache->train_lightgbm(prev_features, optimal_decisions);
}
// prev_features.clear();
// prev_requests.clear();
}
webcache->clear_features();
iterations += 1;
}
}
int main (int argc, char* argv[])
{
// output help if insufficient params
if(argc < 4) {
cerr << "webcachesim traceFile cacheType cacheSizeBytes" << endl;
return 1;
}
const char* path = argv[1];
const string cacheType = argv[2];
const uint64_t cache_size = std::stoull(argv[3]);
bool use_exponential_time_gap = false, use_rl_cache = false;
if(argc == 6){
use_exponential_time_gap = std::stoull(argv[4]);
use_rl_cache = std::stoull(argv[5]);
}
run_simulation(path, cacheType, cache_size, use_exponential_time_gap, use_rl_cache);
return 0;
}