forked from ParaGroup/p3arsec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_ff_farm.cpp
1311 lines (1161 loc) · 45.8 KB
/
encoder_ff_farm.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
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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Decoder for dedup files
*
* Copyright 2010 Princeton University.
* All rights reserved.
*
* Originally written by Minlan Yu.
* Largely rewritten by Christian Bienia.
* FastFlow version by Daniele De Sensi (d.desensi.software@gmail.com)
*/
/*
* The pipeline model for Encode is Fragment->FragmentRefine->Deduplicate->Compress->Reorder
* Each stage has basically three steps:
* 1. fetch a group of items from the queue
* 2. process the items
* 3. put them in the queue for the next stage
*/
#ifdef ENABLE_FF
#include <assert.h>
#include <strings.h>
#include <math.h>
#include <limits.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
extern "C" {
#include "util.h"
#include "dedupdef.h"
#include "encoder.h"
#include "debug.h"
#include "hashtable.h"
#include "config.h"
#include "rabin.h"
#include "mbuffer.h"
#include "queue.h"
#include "binheap.h"
#include "tree.h"
}
#include <iostream>
#ifdef ENABLE_NORNIR
#include <instrumenter.hpp>
#include <stdlib.h>
#include <iostream>
std::string getParametersPath(){
return std::string(getenv("PARSECDIR")) + std::string("/parameters.xml");
}
nornir::Instrumenter* instr;
#endif //ENABLE_NORNIR
#include <ff/farm.hpp>
#include <ff/pipeline.hpp>
#ifdef ENABLE_GZIP_COMPRESSION
#include <zlib.h>
#endif //ENABLE_GZIP_COMPRESSION
#ifdef ENABLE_BZIP2_COMPRESSION
#include <bzlib.h>
#endif //ENABLE_BZIP2_COMPRESSION
#ifdef ENABLE_PARSEC_HOOKS
#include <hooks.h>
#endif //ENABLE_PARSEC_HOOKS
#define INITIAL_SEARCH_TREE_SIZE 4096
//The configuration block defined in main
extern config_t * conf;
//Hash table data structure & utility functions
struct hashtable *cache;
static unsigned int hash_from_key_fn( void *k ) {
//NOTE: sha1 sum is integer-aligned
return ((unsigned int *)k)[0];
}
static int keys_equal_fn ( void *key1, void *key2 ) {
return (memcmp(key1, key2, SHA1_LEN) == 0);
}
//Arguments to pass to each thread
struct thread_args {
//file descriptor, first pipeline stage only
int fd;
//input file buffer, first pipeline stage & preloading only
struct {
void *buffer;
size_t size;
} input_file;
};
#ifdef ENABLE_STATISTICS
//Keep track of block granularity with 2^CHUNK_GRANULARITY_POW resolution (for statistics)
#define CHUNK_GRANULARITY_POW (7)
//Number of blocks to distinguish, CHUNK_MAX_NUM * 2^CHUNK_GRANULARITY_POW is biggest block being recognized (for statistics)
#define CHUNK_MAX_NUM (8*32)
//Map a chunk size to a statistics array slot
#define CHUNK_SIZE_TO_SLOT(s) ( ((s)>>(CHUNK_GRANULARITY_POW)) >= (CHUNK_MAX_NUM) ? (CHUNK_MAX_NUM)-1 : ((s)>>(CHUNK_GRANULARITY_POW)) )
//Get the average size of a chunk from a statistics array slot
#define SLOT_TO_CHUNK_SIZE(s) ( (s)*(1<<(CHUNK_GRANULARITY_POW)) + (1<<((CHUNK_GRANULARITY_POW)-1)) )
//Deduplication statistics (only used if ENABLE_STATISTICS is defined)
typedef struct {
/* Cumulative sizes */
size_t total_input; //Total size of input in bytes
size_t total_dedup; //Total size of input without duplicate blocks (after global compression) in bytes
size_t total_compressed; //Total size of input stream after local compression in bytes
size_t total_output; //Total size of output in bytes (with overhead) in bytes
/* Size distribution & other properties */
unsigned int nChunks[CHUNK_MAX_NUM]; //Coarse-granular size distribution of data chunks
unsigned int nDuplicates; //Total number of duplicate blocks
} stats_t;
//Initialize a statistics record
static void init_stats(stats_t *s) {
int i;
assert(s!=NULL);
s->total_input = 0;
s->total_dedup = 0;
s->total_compressed = 0;
s->total_output = 0;
for(i=0; i<CHUNK_MAX_NUM; i++) {
s->nChunks[i] = 0;
}
s->nDuplicates = 0;
}
//Merge two statistics records: s1=s1+s2
static void merge_stats(stats_t *s1, stats_t *s2) {
int i;
assert(s1!=NULL);
assert(s2!=NULL);
s1->total_input += s2->total_input;
s1->total_dedup += s2->total_dedup;
s1->total_compressed += s2->total_compressed;
s1->total_output += s2->total_output;
for(i=0; i<CHUNK_MAX_NUM; i++) {
s1->nChunks[i] += s2->nChunks[i];
}
s1->nDuplicates += s2->nDuplicates;
}
//Print statistics
static void print_stats(stats_t *s) {
const unsigned int unit_str_size = 7; //elements in unit_str array
const char *unit_str[] = {"Bytes", "KB", "MB", "GB", "TB", "PB", "EB"};
unsigned int unit_idx = 0;
size_t unit_div = 1;
assert(s!=NULL);
//determine most suitable unit to use
for(unit_idx=0; unit_idx<unit_str_size; unit_idx++) {
unsigned int unit_div_next = unit_div * 1024;
if(s->total_input / unit_div_next <= 0) break;
if(s->total_dedup / unit_div_next <= 0) break;
if(s->total_compressed / unit_div_next <= 0) break;
if(s->total_output / unit_div_next <= 0) break;
unit_div = unit_div_next;
}
printf("Total input size: %14.2f %s\n", (float)(s->total_input)/(float)(unit_div), unit_str[unit_idx]);
printf("Total output size: %14.2f %s\n", (float)(s->total_output)/(float)(unit_div), unit_str[unit_idx]);
printf("Effective compression factor: %14.2fx\n", (float)(s->total_input)/(float)(s->total_output));
printf("\n");
//Total number of chunks
unsigned int i;
unsigned int nTotalChunks=0;
for(i=0; i<CHUNK_MAX_NUM; i++) nTotalChunks+= s->nChunks[i];
//Average size of chunks
float mean_size = 0.0;
for(i=0; i<CHUNK_MAX_NUM; i++) mean_size += (float)(SLOT_TO_CHUNK_SIZE(i)) * (float)(s->nChunks[i]);
mean_size = mean_size / (float)nTotalChunks;
//Variance of chunk size
float var_size = 0.0;
for(i=0; i<CHUNK_MAX_NUM; i++) var_size += (mean_size - (float)(SLOT_TO_CHUNK_SIZE(i))) *
(mean_size - (float)(SLOT_TO_CHUNK_SIZE(i))) *
(float)(s->nChunks[i]);
printf("Mean data chunk size: %14.2f %s (stddev: %.2f %s)\n", mean_size / 1024.0, "KB", sqrtf(var_size) / 1024.0, "KB");
printf("Amount of duplicate chunks: %14.2f%%\n", 100.0*(float)(s->nDuplicates)/(float)(nTotalChunks));
printf("Data size after deduplication: %14.2f %s (compression factor: %.2fx)\n", (float)(s->total_dedup)/(float)(unit_div), unit_str[unit_idx], (float)(s->total_input)/(float)(s->total_dedup));
printf("Data size after compression: %14.2f %s (compression factor: %.2fx)\n", (float)(s->total_compressed)/(float)(unit_div), unit_str[unit_idx], (float)(s->total_dedup)/(float)(s->total_compressed));
printf("Output overhead: %14.2f%%\n", 100.0*(float)(s->total_output-s->total_compressed)/(float)(s->total_output));
}
//variable with global statistics
stats_t stats;
#endif //ENABLE_STATISTICS
//Simple write utility function
static int write_file(int fd, u_char type, u_long len, u_char * content) {
if (xwrite(fd, &type, sizeof(type)) < 0){
perror("xwrite:");
EXIT_TRACE("xwrite type fails\n");
return -1;
}
if (xwrite(fd, &len, sizeof(len)) < 0){
EXIT_TRACE("xwrite content fails\n");
}
if (xwrite(fd, content, len) < 0){
EXIT_TRACE("xwrite content fails\n");
}
return 0;
}
/*
* Helper function that creates and initializes the output file
* Takes the file name to use as input and returns the file handle
* The output file can be used to write chunks without any further steps
*/
static int create_output_file(char *outfile) {
int fd;
//Create output file
fd = open(outfile, O_CREAT|O_TRUNC|O_WRONLY|O_TRUNC, S_IRGRP | S_IWUSR | S_IRUSR | S_IROTH);
if (fd < 0) {
EXIT_TRACE("Cannot open output file.");
}
//Write header
if (write_header(fd, conf->compress_type)) {
EXIT_TRACE("Cannot write output file header.\n");
}
return fd;
}
/*
* Helper function that writes a chunk to an output file depending on
* its state. The function will write the SHA1 sum if the chunk has
* already been written before, or it will write the compressed data
* of the chunk if it has not been written yet.
*
* This function will block if the compressed data is not available yet.
* This function might update the state of the chunk if there are any changes.
*/
//NOTE: The parallel version checks the state of each chunk to make sure the
// relevant data is available. If it is not then the function waits.
static void write_chunk_to_file(int fd, chunk_t *chunk) {
assert(chunk!=NULL);
//Find original chunk
if(chunk->header.isDuplicate) chunk = chunk->compressed_data_ref;
pthread_mutex_lock(&chunk->header.lock);
while(chunk->header.state == CHUNK_STATE_UNCOMPRESSED) {
pthread_cond_wait(&chunk->header.update, &chunk->header.lock);
}
//state is now guaranteed to be either COMPRESSED or FLUSHED
if(chunk->header.state == CHUNK_STATE_COMPRESSED) {
//Chunk data has not been written yet, do so now
write_file(fd, TYPE_COMPRESS, chunk->compressed_data.n, (u_char*) chunk->compressed_data.ptr);
mbuffer_free(&chunk->compressed_data);
chunk->header.state = CHUNK_STATE_FLUSHED;
} else {
//Chunk data has been written to file before, just write SHA1
write_file(fd, TYPE_FINGERPRINT, SHA1_LEN, (unsigned char *)(chunk->sha1));
}
pthread_mutex_unlock(&chunk->header.lock);
}
int rf_win;
int rf_win_dataprocess;
/*
* Computational kernel of compression stage
*
* Actions performed:
* - Compress a data chunk
*/
static void sub_Compress(chunk_t *chunk) {
size_t n;
int r;
assert(chunk!=NULL);
//compress the item and add it to the database
pthread_mutex_lock(&chunk->header.lock);
assert(chunk->header.state == CHUNK_STATE_UNCOMPRESSED);
switch (conf->compress_type) {
case COMPRESS_NONE:
//Simply duplicate the data
n = chunk->uncompressed_data.n;
r = mbuffer_create(&chunk->compressed_data, n);
if(r != 0) {
EXIT_TRACE("Creation of compression buffer failed.\n");
}
//copy the block
memcpy(chunk->compressed_data.ptr, chunk->uncompressed_data.ptr, chunk->uncompressed_data.n);
break;
#ifdef ENABLE_GZIP_COMPRESSION
case COMPRESS_GZIP:
//Gzip compression buffer must be at least 0.1% larger than source buffer plus 12 bytes
n = chunk->uncompressed_data.n + (chunk->uncompressed_data.n >> 9) + 12;
r = mbuffer_create(&chunk->compressed_data, n);
if(r != 0) {
EXIT_TRACE("Creation of compression buffer failed.\n");
}
//compress the block
r = compress((u_char*) chunk->compressed_data.ptr, &n, (u_char*) chunk->uncompressed_data.ptr, chunk->uncompressed_data.n);
if (r != Z_OK) {
EXIT_TRACE("Compression failed\n");
}
//Shrink buffer to actual size
if(n < chunk->compressed_data.n) {
r = mbuffer_realloc(&chunk->compressed_data, n);
assert(r == 0);
}
break;
#endif //ENABLE_GZIP_COMPRESSION
#ifdef ENABLE_BZIP2_COMPRESSION
case COMPRESS_BZIP2:
//Bzip compression buffer must be at least 1% larger than source buffer plus 600 bytes
n = chunk->uncompressed_data.n + (chunk->uncompressed_data.n >> 6) + 600;
r = mbuffer_create(&chunk->compressed_data, n);
if(r != 0) {
EXIT_TRACE("Creation of compression buffer failed.\n");
}
//compress the block
unsigned int int_n = n;
r = BZ2_bzBuffToBuffCompress(chunk->compressed_data.ptr, &int_n, chunk->uncompressed_data.ptr, chunk->uncompressed_data.n, 9, 0, 30);
n = int_n;
if (r != BZ_OK) {
EXIT_TRACE("Compression failed\n");
}
//Shrink buffer to actual size
if(n < chunk->compressed_data.n) {
r = mbuffer_realloc(&chunk->compressed_data, n);
assert(r == 0);
}
break;
#endif //ENABLE_BZIP2_COMPRESSION
default:
EXIT_TRACE("Compression type not implemented.\n");
break;
}
mbuffer_free(&chunk->uncompressed_data);
chunk->header.state = CHUNK_STATE_COMPRESSED;
pthread_cond_broadcast(&chunk->header.update);
pthread_mutex_unlock(&chunk->header.lock);
return;
}
/*
* Pipeline stage function of compression stage
*
* Actions performed:
* - Dequeue items from compression queue
* - Execute compression kernel for each item
* - Enqueue each item into send queue
*/
class Compress{
private:
int r;
ringbuffer_t *send_buf;
#ifdef ENABLE_STATISTICS
stats_t *thread_stats;
#endif
public:
Compress(){
#ifdef ENABLE_STATISTICS
thread_stats = (stats_t*) malloc(sizeof(stats_t));
if(thread_stats == NULL) EXIT_TRACE("Memory allocation failed.\n");
init_stats(thread_stats);
#endif //ENABLE_STATISTICS
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
r = 0;
r += ringbuffer_init(send_buf, ITEM_PER_INSERT);
assert(r==0);
}
#ifdef ENABLE_STATISTICS
stats_t* getStats() const{return thread_stats;}
#endif
std::vector<ringbuffer_t*> compute(std::vector<ringbuffer_t*> recv_buf_v) {
std::vector<ringbuffer_t*> dataOut;
for(ringbuffer_t* recv_buf : recv_buf_v){
bool isLast = ringbuffer_isLast(recv_buf);
while(!ringbuffer_isEmpty(recv_buf)){
//fetch one item
chunk_t * chunk = (chunk_t *)ringbuffer_remove(recv_buf);
assert(chunk!=NULL);
if(chunk->header.isDuplicate){
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
//put the item in the next queue for the write thread
if (ringbuffer_isFull(send_buf)) {
dataOut.push_back(send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
ringbuffer_init(send_buf, ITEM_PER_INSERT);
}
continue;
}
sub_Compress(chunk);
#ifdef ENABLE_STATISTICS
thread_stats->total_compressed += chunk->compressed_data.n;
#endif //ENABLE_STATISTICS
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
//put the item in the next queue for the write thread
if (ringbuffer_isFull(send_buf)) {
dataOut.push_back(send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
ringbuffer_init(send_buf, ITEM_PER_INSERT);
}
}
ringbuffer_destroy(recv_buf);
free(recv_buf);
if(isLast){
//empty buffers
ringbuffer_setLast(send_buf);
dataOut.push_back(send_buf);
}
}
return dataOut;
}
};
/*
* Computational kernel of deduplication stage
*
* Actions performed:
* - Calculate SHA1 signature for each incoming data chunk
* - Perform database lookup to determine chunk redundancy status
* - On miss add chunk to database
* - Returns chunk redundancy status
*/
static int sub_Deduplicate(chunk_t *chunk) {
int isDuplicate;
chunk_t *entry;
assert(chunk!=NULL);
assert(chunk->uncompressed_data.ptr!=NULL);
SHA1_Digest(chunk->uncompressed_data.ptr, chunk->uncompressed_data.n, (unsigned char *)(chunk->sha1));
//Query database to determine whether we've seen the data chunk before
pthread_mutex_t *ht_lock = hashtable_getlock(cache, (void *)(chunk->sha1));
pthread_mutex_lock(ht_lock);
entry = (chunk_t *)hashtable_search(cache, (void *)(chunk->sha1));
isDuplicate = (entry != NULL);
chunk->header.isDuplicate = isDuplicate;
if (!isDuplicate) {
// Cache miss: Create entry in hash table and forward data to compression stage
pthread_mutex_init(&chunk->header.lock, NULL);
pthread_cond_init(&chunk->header.update, NULL);
//NOTE: chunk->compressed_data.buffer will be computed in compression stage
if (hashtable_insert(cache, (void *)(chunk->sha1), (void *)chunk) == 0) {
EXIT_TRACE("hashtable_insert failed");
}
} else {
// Cache hit: Skipping compression stage
chunk->compressed_data_ref = entry;
mbuffer_free(&chunk->uncompressed_data);
}
pthread_mutex_unlock(ht_lock);
return isDuplicate;
}
/*
* Pipeline stage function of deduplication stage
*
* Actions performed:
* - Take input data from fragmentation stages
* - Execute deduplication kernel for each data chunk
* - Route resulting package either to compression stage or to reorder stage, depending on deduplication status
*/
class Deduplicate{
private:
int r;
ringbuffer_t *send_buf;
#ifdef ENABLE_STATISTICS
stats_t *thread_stats;
#endif //ENABLE_STATISTICS
public:
Deduplicate(){
#ifdef ENABLE_STATISTICS
thread_stats = (stats_t*) malloc(sizeof(stats_t));
if(thread_stats == NULL) {
EXIT_TRACE("Memory allocation failed.\n");
}
init_stats(thread_stats);
#endif //ENABLE_STATISTICS
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
r=0;
r += ringbuffer_init(send_buf, ITEM_PER_INSERT);
assert(r==0);
}
#ifdef ENABLE_STATISTICS
stats_t* getStats() const{return thread_stats;}
#endif
std::vector<ringbuffer_t*> compute(std::vector<ringbuffer_t*> recv_buf_v) {
std::vector<ringbuffer_t*> dataOut;
for(ringbuffer_t* recv_buf : recv_buf_v){
bool isLast = ringbuffer_isLast(recv_buf);
while(!ringbuffer_isEmpty(recv_buf)){
//get one chunk
chunk_t *chunk = (chunk_t *)ringbuffer_remove(recv_buf);
assert(chunk!=NULL);
//Do the processing
int isDuplicate = sub_Deduplicate(chunk);
#ifdef ENABLE_STATISTICS
if(isDuplicate) {
thread_stats->nDuplicates++;
} else {
thread_stats->total_dedup += chunk->uncompressed_data.n;
}
#endif //ENABLE_STATISTICS
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
if (ringbuffer_isFull(send_buf)) {
dataOut.push_back(send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
ringbuffer_init(send_buf, ITEM_PER_INSERT);
}
}
ringbuffer_destroy(recv_buf);
free(recv_buf);
if(isLast){
//empty buffers
// Is sufficient to set as last the buffer to compress.
// Compress will generate the buffer marked as last for reorder stage.
ringbuffer_setLast(send_buf);
dataOut.push_back(send_buf);
}
}
return dataOut;
}
};
/*
* Pipeline stage function and computational kernel of refinement stage
*
* Actions performed:
* - Take coarse chunks from fragmentation stage
* - Partition data block into smaller chunks with Rabin rolling fingerprints
* - Send resulting data chunks to deduplication stage
*
* Notes:
* - Allocates mbuffers for fine-granular chunks
*/
class FragmentRefine{
private:
ringbuffer_t *send_buf;
int r;
u32int * rabintab;
u32int * rabinwintab;
#ifdef ENABLE_STATISTICS
stats_t *thread_stats;
#endif
public:
FragmentRefine(){
rabintab = (u32int*) malloc(256*sizeof rabintab[0]);
rabinwintab = (u32int*) malloc(256*sizeof rabintab[0]);
if(rabintab == NULL || rabinwintab == NULL) {
EXIT_TRACE("Memory allocation failed.\n");
}
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
r=0;
r += ringbuffer_init(send_buf, CHUNK_ANCHOR_PER_INSERT);
assert(r==0);
#ifdef ENABLE_STATISTICS
thread_stats = (stats_t*) malloc(sizeof(stats_t));
if(thread_stats == NULL) {
EXIT_TRACE("Memory allocation failed.\n");
}
init_stats(thread_stats);
#endif //ENABLE_STATISTICS
}
~FragmentRefine(){
free(rabintab);
free(rabinwintab);
}
#ifdef ENABLE_STATISTICS
stats_t* getStats() const{return thread_stats;}
#endif
std::vector<ringbuffer_t*> compute(ringbuffer_t* recv_buf) {
std::vector<ringbuffer_t*> dataOut;
bool isLast = ringbuffer_isLast(recv_buf);
while(!ringbuffer_isEmpty(recv_buf)){
//get one item
chunk_t *chunk = (chunk_t *)ringbuffer_remove(recv_buf);
assert(chunk!=NULL);
rabininit(rf_win, rabintab, rabinwintab);
int split;
sequence_number_t chcount = 0;
do {
//Find next anchor with Rabin fingerprint
int offset = rabinseg((uchar*) chunk->uncompressed_data.ptr, chunk->uncompressed_data.n, rf_win, rabintab, rabinwintab);
//Can we split the buffer?
if(offset < chunk->uncompressed_data.n) {
//Allocate a new chunk and create a new memory buffer
chunk_t *temp = (chunk_t *)malloc(sizeof(chunk_t));
if(temp==NULL) EXIT_TRACE("Memory allocation failed.\n");
temp->header.state = chunk->header.state;
temp->sequence.l1num = chunk->sequence.l1num;
//split it into two pieces
r = mbuffer_split(&chunk->uncompressed_data, &temp->uncompressed_data, offset);
if(r!=0) EXIT_TRACE("Unable to split memory buffer.\n");
//Set correct state and sequence numbers
chunk->sequence.l2num = chcount;
chunk->isLastL2Chunk = FALSE;
chcount++;
#ifdef ENABLE_STATISTICS
//update statistics
thread_stats->nChunks[CHUNK_SIZE_TO_SLOT(chunk->uncompressed_data.n)]++;
#endif //ENABLE_STATISTICS
//put it into send buffer
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
if (ringbuffer_isFull(send_buf)) {
dataOut.push_back(send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
ringbuffer_init(send_buf, CHUNK_ANCHOR_PER_INSERT);
}
//prepare for next iteration
chunk = temp;
split = 1;
} else {
//End of buffer reached, don't split but simply enqueue it
//Set correct state and sequence numbers
chunk->sequence.l2num = chcount;
chunk->isLastL2Chunk = TRUE;
chcount++;
#ifdef ENABLE_STATISTICS
//update statistics
thread_stats->nChunks[CHUNK_SIZE_TO_SLOT(chunk->uncompressed_data.n)]++;
#endif //ENABLE_STATISTICS
//put it into send buffer
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
if (ringbuffer_isFull(send_buf)) {
dataOut.push_back(send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
ringbuffer_init(send_buf, CHUNK_ANCHOR_PER_INSERT);
}
//prepare for next iteration
chunk = NULL;
split = 0;
}
} while(split);
}
ringbuffer_destroy(recv_buf);
free(recv_buf);
//drain buffer
if(isLast) {
ringbuffer_setLast(send_buf);
dataOut.push_back(send_buf);
}
return dataOut;
}
};
/*
* Pipeline stage function of fragmentation stage
*
* Actions performed:
* - Read data from file (or preloading buffer)
* - Perform coarse-grained chunking
* - Send coarse chunks to refinement stages for further processing
*
* Notes:
* This pipeline stage is a bottleneck because it is inherently serial. We
* therefore perform only coarse chunking and pass on the data block as fast
* as possible so that there are no delays that might decrease scalability.
* With very large numbers of threads this stage will not be able to keep up
* which will eventually limit scalability. A solution to this is to increase
* the size of coarse-grained chunks with a comparable increase in total
* input size.
*/
class Fragment: public ff::ff_node{
struct thread_args *args;
size_t nw;
ff::ff_loadbalancer* lb;
public:
Fragment(struct thread_args* targs, size_t numWorkers, ff::ff_loadbalancer* l):
args(targs), nw(numWorkers), lb(l){;}
void* svc(void *){
size_t preloading_buffer_seek = 0;
int fd = args->fd;
int i;
ringbuffer_t* send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
sequence_number_t anchorcount = 0;
int r;
chunk_t *temp = NULL;
chunk_t *chunk = NULL;
u32int * rabintab = (u32int*) malloc(256*sizeof rabintab[0]);
u32int * rabinwintab = (u32int*) malloc(256*sizeof rabintab[0]);
if(rabintab == NULL || rabinwintab == NULL) {
EXIT_TRACE("Memory allocation failed.\n");
}
r = ringbuffer_init(send_buf, ANCHOR_DATA_PER_INSERT);
assert(r==0);
rf_win_dataprocess = 0;
rabininit(rf_win_dataprocess, rabintab, rabinwintab);
//Sanity check
if(MAXBUF < 8 * ANCHOR_JUMP) {
printf("WARNING: I/O buffer size is very small. Performance degraded.\n");
fflush(NULL);
}
//read from input file / buffer
while (1) {
size_t bytes_left; //amount of data left over in last_mbuffer from previous iteration
//Check how much data left over from previous iteration resp. create an initial chunk
if(temp != NULL) {
bytes_left = temp->uncompressed_data.n;
} else {
bytes_left = 0;
}
//Make sure that system supports new buffer size
if(MAXBUF+bytes_left > SSIZE_MAX) {
EXIT_TRACE("Input buffer size exceeds system maximum.\n");
}
//Allocate a new chunk and create a new memory buffer
chunk = (chunk_t *)malloc(sizeof(chunk_t));
if(chunk==NULL) EXIT_TRACE("Memory allocation failed.\n");
r = mbuffer_create(&chunk->uncompressed_data, MAXBUF+bytes_left);
if(r!=0) {
EXIT_TRACE("Unable to initialize memory buffer.\n");
}
if(bytes_left > 0) {
//FIXME: Short-circuit this if no more data available
//"Extension" of existing buffer, copy sequence number and left over data to beginning of new buffer
chunk->header.state = CHUNK_STATE_UNCOMPRESSED;
chunk->sequence.l1num = temp->sequence.l1num;
//NOTE: We cannot safely extend the current memory region because it has already been given to another thread
memcpy(chunk->uncompressed_data.ptr, temp->uncompressed_data.ptr, temp->uncompressed_data.n);
mbuffer_free(&temp->uncompressed_data);
free(temp);
temp = NULL;
} else {
//brand new mbuffer, increment sequence number
chunk->header.state = CHUNK_STATE_UNCOMPRESSED;
chunk->sequence.l1num = anchorcount;
anchorcount++;
}
//Read data until buffer full
size_t bytes_read=0;
if(conf->preloading) {
size_t max_read = MIN(MAXBUF, args->input_file.size-preloading_buffer_seek);
memcpy(chunk->uncompressed_data.ptr+bytes_left, args->input_file.buffer+preloading_buffer_seek, max_read);
bytes_read = max_read;
preloading_buffer_seek += max_read;
} else {
while(bytes_read < MAXBUF) {
r = read(fd, chunk->uncompressed_data.ptr+bytes_left+bytes_read, MAXBUF-bytes_read);
if(r<0) switch(errno) {
case EAGAIN:
EXIT_TRACE("I/O error: No data available\n");break;
case EBADF:
EXIT_TRACE("I/O error: Invalid file descriptor\n");break;
case EFAULT:
EXIT_TRACE("I/O error: Buffer out of range\n");break;
case EINTR:
EXIT_TRACE("I/O error: Interruption\n");break;
case EINVAL:
EXIT_TRACE("I/O error: Unable to read from file descriptor\n");break;
case EIO:
EXIT_TRACE("I/O error: Generic I/O error\n");break;
case EISDIR:
EXIT_TRACE("I/O error: Cannot read from a directory\n");break;
default:
EXIT_TRACE("I/O error: Unrecognized error\n");break;
}
if(r==0) break;
bytes_read += r;
}
}
//No data left over from last iteration and also nothing new read in, simply clean up and quit
if(bytes_left + bytes_read == 0) {
mbuffer_free(&chunk->uncompressed_data);
free(chunk);
chunk = NULL;
break;
}
//Shrink buffer to actual size
if(bytes_left+bytes_read < chunk->uncompressed_data.n) {
r = mbuffer_realloc(&chunk->uncompressed_data, bytes_left+bytes_read);
assert(r == 0);
}
//Check whether any new data was read in, enqueue last chunk if not
if(bytes_read == 0) {
//put it into send buffer
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
//NOTE: No need to empty a full send_buf, we will break now and pass everything on to the queue
break;
}
//partition input block into large, coarse-granular chunks
int split;
do {
split = 0;
//Try to split the buffer at least ANCHOR_JUMP bytes away from its beginning
if(ANCHOR_JUMP < chunk->uncompressed_data.n) {
int offset = rabinseg(chunk->uncompressed_data.ptr + ANCHOR_JUMP, chunk->uncompressed_data.n - ANCHOR_JUMP, rf_win_dataprocess, rabintab, rabinwintab);
//Did we find a split location?
if(offset == 0) {
//Split found at the very beginning of the buffer (should never happen due to technical limitations)
assert(0);
split = 0;
} else if(offset + ANCHOR_JUMP < chunk->uncompressed_data.n) {
//Split found somewhere in the middle of the buffer
//Allocate a new chunk and create a new memory buffer
temp = (chunk_t *)malloc(sizeof(chunk_t));
if(temp==NULL) EXIT_TRACE("Memory allocation failed.\n");
//split it into two pieces
r = mbuffer_split(&chunk->uncompressed_data, &temp->uncompressed_data, offset + ANCHOR_JUMP);
if(r!=0) EXIT_TRACE("Unable to split memory buffer.\n");
temp->header.state = CHUNK_STATE_UNCOMPRESSED;
temp->sequence.l1num = anchorcount;
anchorcount++;
//put it into send buffer
r = ringbuffer_insert(send_buf, chunk);
assert(r==0);
//send a group of items into the next queue in round-robin fashion
if(ringbuffer_isFull(send_buf)) {
ff_send_out((void*) send_buf);
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
r = ringbuffer_init(send_buf, ANCHOR_DATA_PER_INSERT);
}
//prepare for next iteration
chunk = temp;
temp = NULL;
split = 1;
} else {
//Due to technical limitations we can't distinguish the cases "no split" and "split at end of buffer"
//This will result in some unnecessary (and unlikely) work but yields the correct result eventually.
temp = chunk;
chunk = NULL;
split = 0;
}
} else {
//NOTE: We don't process the stub, instead we try to read in more data so we might be able to find a proper split.
// Only once the end of the file is reached do we get a genuine stub which will be enqueued right after the read operation.
temp = chunk;
chunk = NULL;
split = 0;
}
} while(split);
}
//drain buffer
ringbuffer_setLast(send_buf);
while(!lb->ff_send_out_to((void*) send_buf, 0)){;}
// Send the last buffer also to the other workers (nw - 1)
for(size_t i = 1; i < nw; i++){
send_buf = (ringbuffer_t*) malloc(sizeof(ringbuffer_t));
r = ringbuffer_init(send_buf, ANCHOR_DATA_PER_INSERT);
ringbuffer_setLast(send_buf);
while(!lb->ff_send_out_to((void*) send_buf, i)){;}
}
free(rabintab);
free(rabinwintab);
return EOS;
}
};
/*
* Pipeline stage function of reorder stage
*
* Actions performed:
* - Receive chunks from compression and deduplication stage
* - Check sequence number of each chunk to determine correct order
* - Cache chunks that arrive out-of-order until predecessors are available
* - Write chunks in-order to file (or preloading buffer)
*
* Notes:
* - This function blocks if the compression stage has not finished supplying
* the compressed data for a duplicate chunk.
*/
class Reorder: public ff::ff_node{
private:
struct thread_args *args;
int fd;
ringbuffer_t *recv_buf;
chunk_t *chunk;
SearchTree T;
Position pos;
struct tree_element tele;
sequence_t next;
sequence_number_t *chunks_per_anchor;
unsigned int chunks_per_anchor_max;
int r;
int i;
size_t nw;
size_t terminated_w;
public:
Reorder(size_t n):fd(0), recv_buf(NULL), chunk(NULL), r(0), i(0),
nw(n), terminated_w(0){
T = TreeMakeEmpty(NULL);
pos = NULL;
sequence_reset(&next);
//We perform global anchoring in the first stage and refine the anchoring
//in the second stage. This array keeps track of the number of chunks in
//a coarse chunk.
chunks_per_anchor_max = 1024;
chunks_per_anchor = malloc(chunks_per_anchor_max * sizeof(sequence_number_t));
if(chunks_per_anchor == NULL) EXIT_TRACE("Error allocating memory\n");
memset(chunks_per_anchor, 0, chunks_per_anchor_max * sizeof(sequence_number_t));
assert(r==0);
fd = create_output_file(conf->outfile);
}
void *svc(void * task) {
#ifdef ENABLE_NORNIR
instr->begin();
#endif //ENABLE_NORNIR
recv_buf = (ringbuffer_t*) task;
bool isLast = ringbuffer_isLast(recv_buf);
while(!ringbuffer_isEmpty(recv_buf)) {
chunk = (chunk_t*)ringbuffer_remove(recv_buf);
if (chunk == NULL){
break;
}
//Double size of sequence number array if necessary
if (chunk->sequence.l1num >= chunks_per_anchor_max) {
chunks_per_anchor = realloc(chunks_per_anchor, 2 * chunks_per_anchor_max * sizeof(sequence_number_t));
if (chunks_per_anchor == NULL)
EXIT_TRACE("Error allocating memory\n");
memset(&chunks_per_anchor[chunks_per_anchor_max], 0, chunks_per_anchor_max * sizeof(sequence_number_t));
chunks_per_anchor_max *= 2;
}
//Update expected L2 sequence number
if (chunk->isLastL2Chunk) {
assert(chunks_per_anchor[chunk->sequence.l1num] == 0);