-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase_cache.cc
More file actions
356 lines (284 loc) · 9.29 KB
/
base_cache.cc
File metadata and controls
356 lines (284 loc) · 9.29 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* ccnSim is a scalable chunk-level simulator for Content Centric
* Networks (CCN), that we developed in the context of ANR Connect
* (http://www.anr-connect.org/)
*
* People:
* Giuseppe Rossini (lead developer, mailto giuseppe.rossini@enst.fr)
* Raffaele Chiocchetti (developer, mailto raffaele.chiocchetti@gmail.com)
* Dario Rossi (occasional debugger, mailto dario.rossi@enst.fr)
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <cmath>
#include "base_cache.h"
#include "core_layer.h"
#include "statistics.h"
#include "content_distribution.h"
#include "ccn_data_m.h"
#include "fix_policy.h"
//<aa>
#include "ideal_blind_policy.h"
#include "costaware_policy.h"
#include "ideal_costaware_policy.h"
#include "error_handling.h"
//</aa>
#include "lcd_policy.h"
#include "never_policy.h"
#include "always_policy.h"
#include "decision_policy.h"
#include "betweenness_centrality.h"
#include "prob_cache.h"
#include "ccnsim.h"
#include "GeographicalLocalityContentDistribution.h"
//Initialization function
void base_cache::initialize(){
nodes = getAncestorPar("n");
level = getAncestorPar("level");
cModule* cdModule = getParentModule()->getParentModule()->getSubmodule("content_distribution");
GeographicalLocalityContentDistribution* cdModule2GLCD = dynamic_cast<GeographicalLocalityContentDistribution*>(cdModule);
// the following assgined different cache sizes to the node sin the network.
// Each nodes is mapped to a cache size which is read from a file.
if(par("cache_budget_file").stdstringValue() == "" )
cache_size = par("C").longValue();
else
{
ifstream f;
string file_path;
file_path = par("cache_budget_file").stdstringValue();
cout<<"--------------------------------------"<<endl;
cout<<"reading files from:"<<file_path<<endl;
cout<<"--------------------------------------"<<endl;
f.open(file_path.c_str());
if(!f){
error("The file representing the cache budget pattern is not provided!!!");
}
for ( string line; getline(f,line);){
char *cstr = new char[line.length() + 1];
strcpy(cstr, line.c_str());
char * pch;
int node_id;
pch = strtok(cstr, " \t\n\r");
node_id = atoi(pch);
if ( node_id == getParentModule ()->getIndex()){
pch = strtok(NULL, " \t\n\r");
cache_size = atoi(pch);
break;
}
}
f.close();
}
cout<<"cache size for node "<<getParentModule ()->getIndex()<<" is set to :"<<cache_size<<endl;
decisor = NULL;
string decision_policy = par("DS");
//Initialize the storage policy
double target_acceptance_ratio;
string target_acceptance_ratio_string;
if (decision_policy.compare("lcd")==0){
decisor = new LCD();
} else if (decision_policy.find("fix")==0){
target_acceptance_ratio_string = decision_policy.substr(3);
target_acceptance_ratio = atof( target_acceptance_ratio_string.c_str() );
decisor = new Fix(target_acceptance_ratio);
}
//<aa>
else if (decision_policy.find("ideal_blind")==0){
decisor = new Ideal_blind(this);
} else if (decision_policy.find("ideal_costaware")==0)
{
target_acceptance_ratio = 0; // I don't need this parameter
decisor = new Ideal_costaware(target_acceptance_ratio, this );
} else if (decision_policy.find("costaware")==0)
{
target_acceptance_ratio_string = decision_policy.substr( strlen("costaware") );
target_acceptance_ratio = atof(target_acceptance_ratio_string.c_str());
decisor = new Costaware(target_acceptance_ratio);
}
//</aa>
else if (decision_policy.find("btw")==0)
{
double db = getAncestorPar("betweenness");
if (fabs(db - 1)<=0.001)
error ("Node %i betwenness not defined.",getIndex());
decisor = new Betweenness(db);
}else if (decision_policy.find("prob_cache")==0)
{
decisor = new prob_cache(cache_size);
} else if (decision_policy.find("never")==0)
{
decisor = new Never();
}
//<aa>
else if (decision_policy.compare("lce")==0 )
{
decisor = new Always();
}
if (decisor==NULL){
std::stringstream ermsg;
ermsg<<"Decision policy \""<<decision_policy<<"\" incorrect";
severe_error(__FILE__,__LINE__,ermsg.str().c_str() );
}
// INPUT_CHECK{
if ( decision_policy.find("fix")==0 ||
( decision_policy.find("costaware")==0 && !decision_policy.find("ideal_costaware")== 0 )
){
if ( strlen( target_acceptance_ratio_string.c_str() ) == 0 ){
std::stringstream ermsg;
ermsg<<"You forgot to insert a valid value of acceptance rate when "<<
"specifying the decision policy. Right examples are fix0.01, costaware0.1";
severe_error(__FILE__,__LINE__,ermsg.str().c_str() );
}
if (target_acceptance_ratio <0){
std::stringstream ermsg;
ermsg<<"target_acceptance_ratio "<<target_acceptance_ratio<<" is not valid. "<<
"target_acceptance_ratio_string="<<target_acceptance_ratio_string<<
"; decision_policy="<<decision_policy;
severe_error(__FILE__,__LINE__,ermsg.str().c_str() );
}
}
// }INPUT_CHECK
//</aa>
//Cache statistics
//--Average
miss = 0;
hit = 0;
//<aa>
decision_yes = decision_no = 0;
//</aa>
//--Per file
cache_stats = new cache_stat_entry[__file_bulk + 1];
//<aa>
#ifdef SEVERE_DEBUG
initialized = true;
#endif
//</aa>
}
void base_cache::finish(){
char name [30];
sprintf ( name, "request_rate[%d]", getIndex());
recordScalar (name, 1.*(hit+miss)/getParentModule()->getParentModule()->getSubmodule("statistics")->par("steady").longValue());
sprintf ( name, "leaving_rate[%d]", getIndex());
recordScalar (name, (miss * 1./(hit+miss))*1.*(hit+miss)/getParentModule()->getParentModule()->getSubmodule("statistics")->par("steady").longValue());
sprintf ( name, "p_hit[%d]", getIndex());
//Average hit rate
recordScalar (name, hit * 1./(hit+miss));
sprintf ( name, "hits[%d]", getIndex());
recordScalar (name, hit );
sprintf ( name, "misses[%d]", getIndex());
recordScalar (name, miss);
//<aa>
sprintf ( name, "decision_yes[%d]", getIndex());
recordScalar (name, decision_yes);
sprintf ( name, "decision_no[%d]", getIndex());
recordScalar (name, decision_no);
sprintf ( name, "decision_ratio[%d]", getIndex());
double decision_ratio = (decision_yes + decision_no == 0 ) ?
0 : (double)decision_yes / (decision_yes + decision_no) ;
recordScalar (name, decision_ratio);
decisor->finish(getIndex(), this);
//</aa>
//Per file hit rate
sprintf ( name, "hit_node[%d]", getIndex());
cOutVector hit_vector(name);
for (uint32_t f = 1; f <= __file_bulk; f++)
hit_vector.recordWithTimestamp(f, cache_stats[f].rate() );
sprintf ( name, "request_node[%d]", getIndex());
cOutVector request_vector(name);
for (uint32_t f = 1; f <= __file_bulk; f++){
request_vector.recordWithTimestamp(f, cache_stats[f].req() );
}
sprintf ( name, "cache_size[%d]", getIndex()); // Total number of received Interest packets.
recordScalar (name,cache_size);
sprintf ( name, "cache_full[%d]", getIndex());
recordScalar (name,full());
}
//Base class function: a data has been received:
void base_cache::store(cMessage *in){
if (cache_size ==0){
//<aa>
decision_no++;
//</aa>
return;
}
if (decisor->data_to_cache((ccn_data*)in ) ){
//<aa>
decision_yes++;
//</aa>
data_store( ( (ccn_data* ) in )->getChunk() ); //store is an interface funtion: each caching node should reimplement that function
//<aa>
decisor->after_insertion_action();
//</aa>
}
//<aa>
else {decision_no++; }
//</aa>
}
//Base class function: lookup for a given data
//it wraps statistics on misses and hits
bool base_cache::lookup(chunk_t chunk ){
bool found = false;
name_t name = __id(chunk);
if (data_lookup(chunk)){
//Average cache statistics(hit)
hit++;
found = true;
//Per file cache statistics(hit)
if (name <= __file_bulk)
cache_stats[name].hit++;
}else{
found = false;
//Average cache statistics(miss)
miss++;
//Per file cache statistics(miss)
if ( name <= __file_bulk )
cache_stats[name].miss++;
}
return found;
}
bool base_cache::fake_lookup(chunk_t chunk){
return data_lookup(chunk);
}
//Clear all the statistics
void base_cache::clear_stat()
{
hit = miss = 0; //local statistics
//<aa>
decision_yes = decision_no = 0;
//</aa>
delete cache_stats;
cache_stats = new cache_stat_entry[__file_bulk+1];
}
//<aa>
uint32_t base_cache::get_decision_yes(){
return decision_yes;
}
uint32_t base_cache::get_decision_no(){
return decision_no;
}
void base_cache::set_decision_yes(uint32_t n){
decision_yes = n;
}
void base_cache::set_decision_no(uint32_t n){
decision_no = n;
}
const DecisionPolicy* base_cache::get_decisor(){
return decisor;
}
#ifdef SEVERE_DEBUG
bool base_cache::is_initialized(){
return initialized;
}
#endif
//</aa>