-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcache_manager.cc
More file actions
747 lines (666 loc) · 21.7 KB
/
cache_manager.cc
File metadata and controls
747 lines (666 loc) · 21.7 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
*
* Author: Pramod Kumar(veer) & soheil
*
* Description: Cache_manager.cc
* Manages cache in every client process
*
*/
#include "commons.h"
#include "cache_manager.h"
#include "config.h"
#include "file_server_client.h"
using namespace std;
cache_manager *c_m;
cache_block:: cache_block() {
mutex cb_lock;
dirty = false;
file_name = "";
start_index = 0;
end_index = 0;
data = new char[CACHE_BLOCK_SZ];
}
cache_block::~cache_block(){
if(data != NULL) {
delete data;
dirty_range.clear();
}
}
bool cache_block::clean_cache_block(cache_block *cb){
if (cb->dirty == false) {
cb->file_name.erase();
cb->start_index = 0;
cb->end_index = 0;
} else {
#ifdef DEBUG_FLAG
cout<<"This block is dirty name: " << file_name;
#endif
return false;
}
return true;
}
void harvest_block_file(cache_block *cb) {
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Harvester BLOCK is running";
#endif
pair<int, int> range;
int temp_start = 0, temp_end = 0, server_index = 0 ;
if(cb->dirty == true) {
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Block is Dirty";
#endif
//No need to syncronize this as we are reading and file recepe wont change
vector<string> file_recep = file_dir[cb->file_name]->server_list;
/*TODO: one case to handle, while doing this if another thread append a element */
for (auto dr_it = cb->dirty_range.begin();
dr_it != cb->dirty_range.end();
dr_it++) {
range = *dr_it;
temp_start = range.first;
while(1) {
server_index = (temp_start/(FILE_SERVER_CHUNK_SZ))%file_dir[cb->file_name]->server_list.size();
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Server number = "<< server_index;
#endif
temp_end = (range.second >=
((temp_start/(FILE_SERVER_CHUNK_SZ)+1)*(FILE_SERVER_CHUNK_SZ)))?((temp_start/(FILE_SERVER_CHUNK_SZ)+1)*(FILE_SERVER_CHUNK_SZ) - 1):range.second;
//TODO write_file_to_server function implemention is not done yet
fs_service->fs_write_file_to_server(cb,
temp_start,
temp_end,
file_recep[server_index]);
if(range.second > temp_end) {
/*it means we need to write to more then one fileserver*/
temp_start = temp_end + 1;
} else {
break;
}
}
}
cb->dirty = false;
}
}
cache::cache(){
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Cache constructor is called";
#endif
slots = new cache_block*[ROW];
for(int i =0; i<ROW;i++) {
slots[i] = new cache_block[COLUMN];
}
}
cache_manager::cache_manager() {
//if (obj_cache == NULL) {
obj_cache = new cache;
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Cache Manager constructor is called";
#endif
/* add newly allocated in the free list */
for(int i=0; i<ROW;i++) {
for(int j=0; j<COLUMN;j++) {
free_list.push_back(&obj_cache->slots[i][j]);
}
}
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Free list size= "<<free_list.size();
#endif
//}
/*#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Object is not NULL";
#endif*/
}
cache* cache_manager:: get_cache_obj () {
obj_cache = new cache;
return obj_cache;
}
bool cache_manager::add_to_back_free_list_l (cache_block *cb) {
mutx_free_list.lock();
free_list.push_back(cb);
mutx_free_list.unlock();
return true;
}
bool cache_manager::add_to_front_free_list_l (cache_block *cb) {
mutx_free_list.lock();
free_list.insert(free_list.begin(),cb);
mutx_free_list.unlock();
return true;
}
bool cache_manager::rm_from_free_list_l (cache_block *cb) {
return true;
}
/*As such there is not reverser_iterator defination for "erase"*/
bool cache_manager::rm_from_free_list_l ( vector<cache_block*>::reverse_iterator& it) {
mutx_free_list.lock();
// free_list.erase(it);
mutx_free_list.unlock();
return true;
}
bool cache_manager::rm_from_free_list_l ( vector<cache_block*>::iterator& it) {
mutx_free_list.lock();
free_list.erase(it);
mutx_free_list.unlock();
return true;
}
cache_block* cache_manager::get_free_cache_block () {
cache_block *cb;
mutx_free_list.lock();
auto it = free_list.begin();
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Freelist size = "<< free_list.size();
cout<<"\n";
#endif
cb = *it;
free_list.erase(it);
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Freelist size = "<< free_list.size();
#endif
mutx_free_list.unlock();
return cb;
}
bool cache_manager::add_to_dirty_list_l (cache_block *cb) {
mutx_dirty_list.lock();
dirty_list.push_back(cb);
mutx_dirty_list.unlock();
return true;
}
bool cache_manager::rm_from_dirty_list_l (cache_block *cb) {
return true;
}
bool cache_manager::add_to_allocated_list_l (cache_block *cb) {
return true;
}
bool cache_manager::add_to_front_allocated_list_l (cache_block *cb) {
mutx_allocated_list.lock();
allocated_list.insert(allocated_list.begin(),cb);
add_to_map_fname_chunks_l(cb->file_name,cb);
mutx_allocated_list.unlock();
return true;
}
bool cache_manager::rm_from_allocated_list_l (cache_block *cb) {
mutx_allocated_list.lock();
auto it = find(allocated_list.begin(), allocated_list.end(),cb);
c_m->obj_cache->lru_item_delete(cb);
allocated_list.erase(it);
(*it)->clean_cache_block(cb);
rm_from_map_fname_chunks_l(cb->file_name, cb);
mutx_allocated_list.unlock();
return true;
}
bool cache_manager::rm_from_allocated_list_l ( vector<cache_block*>::reverse_iterator& it) {
mutx_allocated_list.lock();
// allocated_list.erase(it);
mutx_allocated_list.unlock();
return true;
}
bool cache_manager::rm_from_allocated_list_l ( vector<cache_block*>::iterator& it) {
cache_block *cb = *it;
mutx_allocated_list.lock();
allocated_list.erase(it);
(*it)->clean_cache_block(cb);
rm_from_map_fname_chunks_l(cb->file_name, cb);
mutx_allocated_list.unlock();
return true;
}
bool cache_manager::add_to_map_fname_chunks_l (string file_name, cache_block *cb) {
mutx_map_fname_to_chunks.lock();
map_fname_to_chunks[file_name].push_back(cb);
mutx_map_fname_to_chunks.unlock();
return true;
}
bool cache_manager::rm_from_map_fname_chunks_l (string file_name, cache_block* cb) {
mutx_map_fname_to_chunks.lock();
vector<cache_block*> cb_list = map_fname_to_chunks[file_name];
cout<<"cache block to before size "<<cb_list.size();
for(auto it = cb_list.begin(); it!= cb_list.end(); it++) {
if(*it == cb) {
cb_list.erase(it);
}
}
cout<<"cache block removed current size "<<cb_list.size();
mutx_map_fname_to_chunks.unlock();
return true;
}
int sort_by_start_file_chunk_vector(const pair<pair<int,int>,char*> &a, const pair<pair<int,int>,char*> &b){
return (a.first.first < b.first.first) ;
}
int sort_vector_cache_block (const cache_block* a, const cache_block* b){
return (a->start_index < b->start_index);
}
int sort_pair_one_first_element_comp (const pair<int,int> &a, const pair<int,int> &b) {
return (a.first < b.first);
}
int collapse_dirty_list(cache_block *cb) {
vector<pair<int,int>> vec = cb->dirty_range;
sort(vec.begin(),vec.end(),sort_pair_one_first_element_comp);
stack<pair<int,int>> s;
// push the first interval to stack
s.push(vec[0]);
// Start from the next interval and merge if necessary
for (int i = 1 ; i < vec.size(); i++)
{
// get interval from stack top
pair<int, int> top = s.top();
// if current interval is not overlapping with stack top,
// push it to the stack
if (top.second < vec[i].first)
s.push(vec[i]);
// Otherwise update the ending time of top if ending of current
// interval is more
else if (top.second < vec[i].second)
{
top.second = vec[i].second;
s.pop();
s.push(top);
}
}
// Print contents of stack
cout << "\n The Merged Intervals are: ";
vec.erase(vec.begin(),vec.end());
while (!s.empty())
{
pair<int,int> t = s.top();
#ifdef DEBUG_FLAG
cout << "[" << t.first << "," << t.second << "] ";
#endif
vec.insert(vec.begin(), make_pair(t.first, t.second));
s.pop();
}
}
void cache::refer(cache_block* cb) {
if(lru_indexing.find(cb) != lru_indexing.end()) {
lru_list.erase(lru_indexing[cb]);
lru_list.push_front(cb);
lru_indexing[cb] = lru_list.begin();
} else {
lru_list.push_front(cb);
lru_indexing[cb] = lru_list.begin();
}
}
void cache::lru_item_delete(cache_block* cb){
if(lru_indexing.find(cb) != lru_indexing.end()) {
mutex_lru_indexing.lock();
mutex_lru_list.lock();
lru_list.erase(lru_indexing[cb]);
lru_indexing.erase(cb);
mutex_lru_list.unlock();
mutex_lru_indexing.unlock();
}
}
void cache::display_lru_list(){
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<":";
#endif
for(auto it = lru_list.begin(); it!= lru_list.end();it++){
cout<<"\n file_name : "<< (*it)->file_name;
cout<<"\n start_name : "<< (*it)->start_index;
cout<<"\n end_index : "<< (*it)->end_index;
cout<<"\n Dirty : "<< (*it)->dirty;
cout<<"\n dirty list size: "<< (*it)->file_name;
cout<<"\n";
}
}
int cache_manager::read_file (string file_name, char *buf, int start,int end, int *cache_hit) {
/*START and END includes boundries*/
bool all_chunks_available = false;
int current_index = 0;
int size = 0, server_index;
int act_start = start, act_end= end;
int temp_start = 0, temp_end= end;
map<string,pair<int,int>> blocks_to_fetch;
vector<pair<pair<int,int>,char*>> file_chunks;
vector<pair<int,int>> missing_chunks; /**/
char *temp_buf;
vector<cache_block*> cb_list;
sort(map_fname_to_chunks[file_name].begin(),map_fname_to_chunks[file_name].end(), sort_vector_cache_block);
cb_list = map_fname_to_chunks[file_name];
cache_block* cb;
#ifdef DEBUG_FLAG
cout<<"\n\n"<<__func__ <<": Entered read file: Start: "<< start<< " end : "<< end << "cache_hit : " << *cache_hit;
#endif
/* iterate over cache blocks of this file*/
for(auto it = cb_list.begin(); it!= cb_list.end() ;){
cb = *it;
if (cb->start_index > start) {
/*this block is completely out of range*/
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<":" << "partially not in the cache chunk_Start: "<< start << " chunk_end : "<< (cb->start_index>end?end:cb->start_index-1);
#endif
missing_chunks.push_back(make_pair(start, cb->start_index>end?end:(cb->start_index-1)));
start = cb->start_index>end?end:cb->start_index;
if(start == end) {
#ifdef DEBUG_FLAG
cout<<"\n1. finished checking blocks, breaking";
#endif
break;
}
continue;
}
/* some or whole block is available */
if (start>=cb->start_index && start <= cb->end_index) {
if(end <= cb->end_index) {
temp_buf = new char[end-start+1]; // 0 to 199 is 200 but 199-0 is 199
obj_cache->refer(cb);
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Current block start : "<< cb->start_index<< " end: "<< cb->end_index;
cout<<"\n"<<__func__ <<": Current pointer is at :" << start;
#endif
memcpy(temp_buf, cb->data+(start-cb->start_index),end-start+1 );
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Temp buf : "<< temp_buf;
#endif
file_chunks.push_back(make_pair(make_pair(start,end),temp_buf));
start = end;
#ifdef DEBUG_FLAG
cout<<"\n2. finished checking blocks, breaking "<< start << " chunk_end : "<< end;
#endif
break;
} else {
temp_buf = new char[cb->end_index-start+1];
obj_cache->refer(cb);
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Current block start : "<< cb->start_index<< " end: "<< cb->end_index;
cout << " : Current pointer is at :" << start;
#endif
memcpy(temp_buf, cb->data+(start-cb->start_index),cb->end_index-start+1 );
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Temp buf : "<< temp_buf;
#endif
file_chunks.push_back(make_pair(make_pair(start,cb->end_index),temp_buf));
start = cb->end_index + 1;
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Read some part, it will next from other block. read till: " << start;
#endif
}
}
it++;
}
if(end-start !=0) {
/* remaining range is missing */
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Done reading Blocks in cache : Remaining, start: "<< start << " end: "<< end ;
#endif
missing_chunks.push_back(make_pair(start, end));
}
/* send read request to file server */
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Printing All the missing blocks " <<missing_chunks.size();
#endif
for(auto it = missing_chunks.begin(); it != missing_chunks.end(); it++) {
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": Requesting data from Server: start " <<it->first;
cout<<"\n"<<__func__ <<": Requesting data from Server: end " <<it->second;
#endif
all_chunks_available = false;
pair<int,int> start_end;
start_end = *it;
temp_start = start_end.first;
char *temp_buf = NULL;
cache_block *cb = NULL;
string server_name ;
#ifdef DEBUG_FLAG
cout<<"\n " <<" "<< start_end.first <<" -- "<< start_end.second;
#endif
while(1) {
server_index = (temp_start/(FILE_SERVER_CHUNK_SZ))%file_dir[file_name]->server_list.size();
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Server number = "<< server_index;
#endif
temp_end = (start_end.second >=
((temp_start/(FILE_SERVER_CHUNK_SZ)+1)*(FILE_SERVER_CHUNK_SZ)))?((temp_start/(FILE_SERVER_CHUNK_SZ)+1)*(FILE_SERVER_CHUNK_SZ) - 1):start_end.second;
temp_buf = new char[temp_end-temp_start +1];
#ifdef DEBUG_FLAG
cout<<"\n Sending to server: " << server_index;
cout<<"\n start: " << temp_start << " end: "<< temp_end;
#endif
server_name = (file_dir[file_name])->server_list[server_index];
if(fs_service->fs_read_file_to_server(file_name, temp_buf,
temp_start,
temp_end,
server_name)) {
file_chunks.push_back(make_pair(make_pair(temp_start,temp_end),temp_buf));
cb = get_free_cache_block();
memcpy(cb->data, temp_buf, temp_end-temp_start+1);
cb->start_index = temp_start;
cb->end_index = temp_end;
cb->file_name = file_name;
cb->dirty = false;
add_to_front_allocated_list_l(cb);
obj_cache->refer(cb); /*For LRU*/
} else {
cout<<"\nerror while reading file from the file server";
}
if(start_end.second > temp_end) {
/*it means we need to write to more then one fileserve r*/
temp_start = temp_end + 1;
} else {
break;
}
}
}
sort(file_chunks.begin(), file_chunks.end(), sort_by_start_file_chunk_vector);
int current = 0;
#ifdef DEBUG_FLAG
cout<<"\n" <<" Got all required chunks, Here the list: ";
#endif
for(auto it = file_chunks.begin(); it!= file_chunks.end(); it++) {
#ifdef DEBUG_FLAG
cout<<"\n "<< (*it).first.first <<" -- " << (*it).first.second << " -- " <<(*it).second;
#endif
memcpy(buf+current, (*it).second,(*it).first.second - (*it).first.first + 1);
current = current + ((*it).first.second - (*it).first.first) + 1;
delete ((*it).second);
}
if(all_chunks_available == false) {
*cache_hit = 0;
}
else{
*cache_hit=1;
}
#ifdef DEBUG_FLAG
cout<< "\n Read and returing size: "<<current;
cout<<"\n cache_hit : "<<cache_hit;
cout<< "\n FILE CONTENT: \n";
cout<<buf;
#endif
return current;
}
bool cache_manager::write_file (string file_name, const void *buf, int start,int end, int *cache_hit) {
/* go through the cache, if available overwrite it otherwise create a new block and and add the remaining */
bool all_chunks_available = false;
int current_index = 0;
int size = 0;
int act_start = start, act_end= end;
int temp_start = 0, temp_end= 0;
map<string,pair<int,int>> blocks_to_fetch;
vector<pair<pair<int,int>,char*>> file_chunks;
vector<pair<int,int>> missing_chunks; /**/
char *temp_buf = (char*) buf ;
vector<cache_block*> cb_list;
sort(map_fname_to_chunks[file_name].begin(),map_fname_to_chunks[file_name].end(), sort_vector_cache_block);
cb_list = map_fname_to_chunks[file_name];
cache_block* cb;
int current_written_sz = 0;
#ifdef DEBUG_FLAG
cout<<"\n\n"<< __func__ <<": Starting Processing";
#endif
for(auto it = cb_list.begin(); it!= cb_list.end() ;){
cb = *it;
if (cb->start_index > start) {
/*this block is completely out of range*/
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__ <<": ";
#endif
cb = get_free_cache_block();
memcpy(cb->data, temp_buf+current_written_sz, cb->start_index>end?end:(cb->start_index-1));
obj_cache->refer(cb); /*For LRU*/
current_written_sz = current_written_sz + cb->start_index>end?end:(cb->start_index); // not putting -1 as it will be use to more 1 more position where next time it will write
cb->start_index = start;
cb->end_index = cb->start_index>end?end:(cb->start_index-1);
cb->file_name = file_name;
cb->dirty = true;
cb->dirty_range.push_back(make_pair(start, cb->start_index>end?end:(cb->start_index-1)));
add_to_front_allocated_list_l(cb);
add_to_dirty_list_l(cb);
obj_cache->refer(cb); /*For LRU*/
start = cb->start_index>end?end:cb->start_index;
if(start == end) {
#ifdef DEBUG_FLAG
cout<<"\n1. finished writting the blocks, breaking";
#endif
break;
}
continue;
}
/* some or whole block is available */
if (start>=cb->start_index && start <= cb->end_index) {
if(end <= cb->end_index) {
/*Over will happen compeletly inside this block*/
memcpy(cb->data+(cb->start_index-start), temp_buf+current_written_sz,end-start+1 );
obj_cache->refer(cb); /*For LRU*/
current_written_sz = current_written_sz + end-start+1;
cb->dirty_range.push_back(make_pair(start, end));
start = end;
#ifdef DEBUG_FLAG
cout<<"\n2. finished checking blocks, breaking";
#endif
break;
} else {
/* partiall write will happen here and overflow to next block */
memcpy(cb->data+(cb->start_index-start),temp_buf+current_written_sz, cb->end_index-start+1 );
obj_cache->refer(cb); /*For LRU*/
current_written_sz = current_written_sz + cb->end_index-start+1;
file_chunks.push_back(make_pair(make_pair(start,end),temp_buf));
start = cb->end_index + 1;
}
collapse_dirty_list(cb);
obj_cache->refer(cb); /*For LRU*/
}
it++;
}
int i = 0;
if(start< end+1) {
/*looks like append*/
while( start <= end) {
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Getting Free Block From Cache iterationi: " << i++ << " , current size : "<<current_written_sz;
#endif
cb = get_free_cache_block();
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Doing Memcpy";
#endif
int size_to_copy = (CACHE_BLOCK_SZ)>end-start? (end-start+1): ((CACHE_BLOCK_SZ));
cout <<"\n size of copy " <<size_to_copy;
memcpy(cb->data, temp_buf+current_written_sz, CACHE_BLOCK_SZ>end-start?end-start+1:(CACHE_BLOCK_SZ+1));
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Memcpy is finished : data size "<< (CACHE_BLOCK_SZ>end-start?end-start+1:(CACHE_BLOCK_SZ));
cout<<"\n"<<__func__<<cb->data;
cout<<"\n";
#endif
current_written_sz = current_written_sz + CACHE_BLOCK_SZ>end-start?end-start+1:((CACHE_BLOCK_SZ));
// not putting -1 as it will be use to more 1 more position where next time it will write
cb->start_index = start;
cb->end_index = CACHE_BLOCK_SZ>end-start?end:((CACHE_BLOCK_SZ)+start-1);
cb->file_name = file_name;
cb->dirty = true;
cb->dirty_range.push_back(make_pair(start, CACHE_BLOCK_SZ>end-start?end:((CACHE_BLOCK_SZ)+start-1)));
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" Cache is added start= "<<cb->start_index <<" end= "<<cb->end_index <<" filename= "<<cb->file_name <<" dirty = "<<cb->dirty;
cout<<"\n";
#endif
add_to_front_allocated_list_l(cb);
add_to_dirty_list_l(cb);
obj_cache->refer(cb); /*For LRU*/
start = CACHE_BLOCK_SZ>end-start?end+1:((CACHE_BLOCK_SZ)+start);
#ifdef DEBUG_FLAG
cout<<"\n";
#endif
}
}
cout<<"\n";
}
bool cache_manager::clean_file (string file_name, string operation) {
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<" cleaning file from cache -> "<<file_name;
#endif
/* harvest dirty blocks for this file and clean it from the cache*/
if(operation == "close") {
auto it=c_m->obj_cache->lru_list.begin();
while(it!=c_m->obj_cache->lru_list.end()){
cache_block *cb=*it;
if(cb->file_name.compare(file_name)==0){
if(cb->dirty == false) {
#ifdef DEBUG_FLAG
cout<<"\n fluser: Non-dirty filename:"<<cb->file_name ;
#endif
it++;
c_m->add_to_back_free_list_l (cb);
c_m->rm_from_allocated_list_l (cb);
} else {
#ifdef DEBUG_FLAG
cout<<"\n fluser: Dirty block filename:"<<cb->file_name;
#endif
harvest_block_file(cb);
c_m->mutx_dirty_list.lock();
for(auto it2 = c_m->dirty_list.begin(); it2 != c_m->dirty_list.end(); ) {
cache_block *cb2 = *it;
if(cb2->dirty == false) {
c_m->dirty_list.erase(it2);
} else {
it2++;
}
}
c_m->mutx_dirty_list.unlock();
/* Remove from the dirty list */
it++;
c_m->add_to_back_free_list_l (cb);
c_m->rm_from_allocated_list_l (cb);
}
//c_m->obj_cache->lru_item_delete(cb);
}
else ++it;
}
} else {
auto it=c_m->obj_cache->lru_list.begin();
while(it!=c_m->obj_cache->lru_list.end()){
cache_block *cb=*it;
#ifdef DEBUG_FLAG
cout<<"\n"<<__func__<<"Cleaning file ->"<<cb->file_name ;
cout<<"\n";
cout<<"\n";
#endif
if(cb->file_name.compare(file_name)==0){
if(cb->dirty == false) {
#ifdef DEBUG_FLAG
cout<<"\n fluser: Non-dirty filename:"<<cb->file_name ;
#endif
it++;
c_m->add_to_back_free_list_l (cb);
c_m->rm_from_allocated_list_l (cb);
} else {
#ifdef DEBUG_FLAG
cout<<"\n fluser: Dirty block filename:"<<cb->file_name;
#endif
//harvest_block_file(cb);
c_m->mutx_dirty_list.lock();
for(auto it2 = c_m->dirty_list.begin(); it2 != c_m->dirty_list.end(); ) {
cache_block *cb2 = *it;
if(cb2->file_name.compare(file_name)==0) {
c_m->dirty_list.erase(it2);
} else {
it2++;
}
}
c_m->mutx_dirty_list.unlock();
/* Remove from the dirty list */
it++;
if(cb->dirty==true)
cb->dirty==false;
c_m->add_to_back_free_list_l (cb);
c_m->rm_from_allocated_list_l (cb);
}
//c_m->obj_cache->lru_item_delete(cb);
}
else ++it;
}
}
}