-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.c
979 lines (822 loc) · 28.8 KB
/
main.c
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
#define _GNU_SOURCE
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#define _FILE_OFFSET_BITS 64
#include <sys/stat.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <openssl/sha.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
#include <zlog.h> // log
#include "dedup.h"
#include <stdbool.h>
#include <time.h>
#include <malloc.h>
#include "bplustree.h"
#include "libhashfile.h"
#define PRINTLN printf("\n")
#define LOG_LINE 4096
// ===================================================
// Global Variables
// ===================================================
struct g_args_t {
char *image_filename;
int image_fd;
int RW;
int MAP;
uint32_t fingerprint_size; // in bytes
char *hash_filename;
int hash_fd;
int run_mode;
char *bplustree_filename;
char *dataset_filename;
uint64_t cur_nbd_offset;
zlog_category_t* write_block_category;
zlog_category_t* log_error;
struct bplus_tree *tree;
bool log_on;
bool log_display;
};
struct g_args_t g_args;
struct evaluation_indicators_s {
// write
clock_t timer_detect;
clock_t timer_write_space;
clock_t timer_write_tree;
uint64_t n_chunk_sum;
uint64_t n_chunk_unique;
uint64_t n_chunk_redundant;
uint64_t len_data_sum;
uint64_t len_data_redundant;
uint64_t n_hash_log_entry;
uint64_t n_block_map_entry;
uint64_t n_bucket;
uint64_t n_space;
// read & search
clock_t timer_read_data;
clock_t timer_write_data;
clock_t timer_read_space;
clock_t timer_read_tree;
};
struct evaluation_indicators_s indicators;
static void *zeros;
static struct hash_log_entry *cache;
static uint64_t hash_log_free_list;
static uint64_t data_log_free_offset;
static char* str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
enum mode{
INIT_MODE = 0,
RUN_MODE = 1,
WRITE_MODE = 2,
READ_MODE = 3,
BPTREE_MODE = 4,
SPACE_MODE = 5,
};
// ===================================================
// Tool Functions: Seek
// ===================================================
#define SEEK_TO_BUCKET(fd, i) \
do { \
if (g_args.MAP == BPTREE_MODE ) \
lseek64((fd), (i)*sizeof(hash_bucket), SEEK_SET); \
else if (g_args.MAP == SPACE_MODE) \
lseek64((fd), SPACE_SIZE + (i)*sizeof(hash_bucket), SEEK_SET); \
} while(0)
#define SEEK_TO_HASH_LOG(fd, i) \
do { \
if (g_args.MAP == BPTREE_MODE ) \
lseek64((fd), HASH_INDEX_SIZE + (i) * sizeof(struct hash_log_entry), SEEK_SET); \
else if (g_args.MAP == SPACE_MODE) \
lseek64((fd), SPACE_SIZE + HASH_INDEX_SIZE + (i) * sizeof(struct hash_log_entry), SEEK_SET); \
} while (0)
#define SEEK_TO_SPACE(fd, i) \
do { \
lseek64((fd), (i) * SPACE_LENGTH, SEEK_SET); \
} while (0)
//static void SEEK_TO_SPACE(const int fd, const uint64_t i)
//{
// off_t err = lseek64(fd, i * SPACE_LENGTH, SEEK_SET);
// assert(err != -1);
//}
// ===================================================
// Function Defines
// ===================================================
static void usage()
{
fprintf(stderr, "Options:\n\n");
fprintf(stderr, " -d, --dataset\n" "\tdata set file\n");
fprintf(stderr, " -i, --init\n" "\tspecify the nbd device and init\n\n");
fprintf(stderr, " -a, --hash-file\n" "\tspecify the hash file\n\n");
fprintf(stderr, " -p, --physical-device\n" "\tspecify the physical device or file\n\n");
fprintf(stderr, "\n");
fprintf(stderr, " -b, --btree\n" "\tb+tree mapping mode and specify b+tree db file\n\n");
fprintf(stderr, " -s, --space\n" "\t space mode\n");
fprintf(stderr, "\n");
printf(stderr, " -w, --write\n" "\twrite mode\n");
printf(stderr, " -r, --read\n" "\tread mode\n");
fprintf(stderr, "\n");
fprintf(stderr, " -h, --help\n" "\tdisplay the help infomation\n\n");
exit(0);
}
static int fingerprint_is_zero(char *fingerprint)
{
int i;
for (i = 0; i < g_args.fingerprint_size; i++) {
if (fingerprint[i])
return 0;
}
return 1;
}
static void print_chunk_hash(const uint8_t *hash,
int hash_size_in_bytes)
{
int j;
printf("%.2hhx", hash[0]);
for (j = 1; j < hash_size_in_bytes; j++)
printf(":%.2hhx", hash[j]);
}
static void gen_data(char* dest, uint64_t len)
{
for (int i=0; i<len; i++) {
dest[i] = str[lrand48()%62];
}
}
/**
* Return the bucket which contains the given fingerprint
*/
static int hash_index_get_bucket(char *hash, hash_bucket *bucket)
{
/* We don't need to look at the entire hash, just the last few bytes. */
int32_t *hash_tail = (int32_t *)(hash + g_args.fingerprint_size - sizeof(int32_t));
uint64_t bucket_index = *hash_tail % NBUCKETS;
if (bucket_index > indicators.n_bucket) {
indicators.n_bucket = bucket_index + 1;
}
SEEK_TO_BUCKET(g_args.hash_fd, bucket_index);
ssize_t err = read(g_args.hash_fd, bucket, sizeof(struct hash_index_entry) * ENTRIES_PER_BUCKET);
assert(err == sizeof(struct hash_index_entry) * ENTRIES_PER_BUCKET);
return 0;
}
static int hash_index_put_bucket(char *hash, hash_bucket *bucket)
{
/* We don't need to look at the entire hash, just the last few bytes. */
int32_t *hash_tail = (int32_t *)(hash + g_args.fingerprint_size - sizeof(int32_t));
uint64_t bucket_index = *hash_tail % NBUCKETS;
SEEK_TO_BUCKET(g_args.hash_fd, bucket_index);
ssize_t err = write(g_args.hash_fd, bucket, sizeof(struct hash_index_entry) * ENTRIES_PER_BUCKET);
assert(err == sizeof(struct hash_index_entry) * ENTRIES_PER_BUCKET);
return 0;
}
static int hash_index_insert(char *hash, uint64_t hash_log_address)
{
hash_bucket bucket;
hash_index_get_bucket(hash, &bucket);
for (int i = 0; i < ENTRIES_PER_BUCKET; i++)
if (bucket[i].hash_log_address == 0) {
/* We have found an empty slot. */
memcpy(bucket[i].hash, hash, g_args.fingerprint_size);
bucket[i].hash_log_address = hash_log_address;
hash_index_put_bucket(hash, &bucket);
return 0;
}
/* We failed to find a slot. In the future it would be nice to have a more
* sophisticated hash table that resolves collisions better. But for now we
* just give up. */
assert(0);
}
static int is_space_full(hash_space* space)
{
if (space[ENTRIES_PER_SPACE - 1]->length == 0) {
return 1;
}
return 0;
}
static int hash_get_space(uint64_t offset, hash_space *space)
{
uint64_t space_n = offset / SPACE_LENGTH;
if (space_n > indicators.n_space) {
indicators.n_space = space_n + 1;
}
SEEK_TO_SPACE(g_args.hash_fd, space_n);
ssize_t err = read(g_args.hash_fd, space, sizeof(struct block_map_entry) * ENTRIES_PER_SPACE);
assert(err == sizeof(struct block_map_entry) * ENTRIES_PER_SPACE);
return 0;
}
static int hash_put_space(uint64_t offset, hash_space *space)
{
uint64_t space_n = offset / SPACE_LENGTH;
SEEK_TO_SPACE(g_args.hash_fd, space_n);
ssize_t err = write(g_args.hash_fd, space, sizeof(struct block_map_entry) * ENTRIES_PER_SPACE);
assert(err == sizeof(struct block_map_entry) * ENTRIES_PER_SPACE);
return 0;
}
static int hash_space_insert(uint64_t offset, struct block_map_entry ble)
{
hash_space space;
hash_get_space(offset, &space);
uint64_t index = offset % MAX_BLOCK_SIZE / MIN_BLOCK_SIZE;
space[index] = ble;
hash_put_space(offset, &space);
// for (int i = 0; i < ENTRIES_PER_SPACE; i ++) {
// if (space[i].length == 0) {
// /// We have found an empty slot.
// space[i] = ble;
// hash_put_space(offset, &space);
// return 0;
// }
// }
//
// /// Failed to find a slot.
// assert(0);
}
static uint64_t hash_index_lookup(char *hash)
{
hash_bucket bucket;
hash_index_get_bucket(hash, &bucket);
for (int i = 0; i < ENTRIES_PER_BUCKET; i++)
if (!memcmp(bucket[i].hash, hash, g_args.fingerprint_size))
return bucket[i].hash_log_address;
return -1;
}
/**
* Given a fingerprint, remove the corresponding hash_index_entry in HASH INDEX TABLE
*/
static int hash_index_remove(char *hash)
{
hash_bucket bucket;
hash_index_get_bucket(hash, &bucket);
for (int i = 0; i < ENTRIES_PER_BUCKET; i++)
if (!memcmp(bucket[i].hash, hash, g_args.fingerprint_size)) {
memset(bucket + i, 0, sizeof(struct hash_index_entry));
hash_index_put_bucket(hash, &bucket);
return 0;
}
return -1;
}
static uint64_t hash_log_new()
{
uint64_t new_block = hash_log_free_list;
SEEK_TO_HASH_LOG(g_args.hash_fd, new_block);
ssize_t err = read(g_args.hash_fd, &hash_log_free_list, sizeof(uint64_t));
assert(err == sizeof(uint64_t));
return new_block;
}
/**
* Free a hash_log_entry and change hash_log_free_list to it
*/
static int hash_log_free(uint64_t hash_log_address)
{
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
ssize_t err = write(g_args.hash_fd, &hash_log_free_list, sizeof(uint64_t));
assert(err == sizeof(uint64_t));
hash_log_free_list = hash_log_address;
return 0;
}
static uint64_t physical_block_new(uint64_t block_size)
{
uint64_t offset = data_log_free_offset;
data_log_free_offset += block_size;
return offset;
}
static int physical_block_free(uint64_t offest, uint64_t size)
{
printf("Freed physical block, offset: %lu, size: %lu\n", offest, size);
return 0;
}
/**
* Return the index where the given fingerprint SHOULD be found in
* he cache
*/
static u_int32_t get_cache_index(char *fingerprint)
{
/* It doesn't actually matter which bits we choose, as long as we are
* consistent. So let's treat the first four bytes as an integer and take
* the lower bits of that. */
u_int32_t mask = (1 << CACHE_SIZE) - 1;
u_int32_t result = ((u_int32_t *)fingerprint)[0] & mask;
if (result > 20) {
int i = 0;
}
assert(result < mask);
return result;
}
/**
* Return a HAST LOG TABLE entry in terms of given fingerprint
*/
static struct hash_log_entry lookup_fingerprint(char *fingerprint)
{
ssize_t err;
// Search in CACHE
// u_int32_t index = get_cache_index(fingerprint);
// if (!memcmp(fingerprint, cache[index].fingerprint, g_args.fingerprint_size)) {
// // Awesome, this fingerprint is already cached, so we are good to go.
// return cache[index];
// }
// Didn't hit in cache, so we have to look on disk.
uint64_t hash_log_address = hash_index_lookup(fingerprint);
assert(hash_log_address != (uint64_t)-1);
struct hash_log_entry hle;
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
err = read(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
assert(err == sizeof(struct hash_log_entry));
return hle;
//
//
// // ==========================================
// // update cache
// // ==========================================
// /* Now let's look up everything in the 4K block containing the hash log
// * entry we want. This way we can cache it all for later. */
// hash_log_address = hash_log_address % HASH_LOG_BLOCK_SIZE;
// SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
// struct hash_log_entry h;
//
// for (unsigned i = 0; i < HASH_LOG_BLOCK_SIZE/sizeof(struct hash_log_entry); i++) {
// err = read(g_args.hash_fd, &h, sizeof(struct hash_log_entry));
// assert(err == sizeof(struct hash_log_entry));
//
// u_int32_t j = get_cache_index(h.fingerprint);
//
// if (i == 0 && j != index) {
// int a = 0;
// }
//
// printf("[UPDATE CACHE : %u] | phy off: %lu, block size: %lu\n", index, h.data_log_offset, h.block_size);
//
// memcpy(cache + j, &h, sizeof(struct hash_log_entry));
// }
//
// /* Now we should have looked up the fingerprint we wanted, along with a
// * bunch of others. */
//
// err = memcmp(fingerprint, cache[index].fingerprint, g_args.fingerprint_size);
// assert( err == 0 );
//
//
// return cache[index];
}
/**
* Decrease the ref_count of a physical block
*/
static int decrement_refcount(char *fingerprint)
{
// todo: decrement_refcount
struct hash_log_entry hle;
uint64_t hash_log_address = hash_index_lookup(fingerprint);
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
ssize_t err = read(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
assert(err == sizeof(struct hash_log_entry));
if (hle.ref_count > 1) {
hle.ref_count--;
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
err = write(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
} else {
/* The ref_count is now zero, so we need to do some garbage collection
* here. */
hash_index_remove(fingerprint);
physical_block_free(hle.data_log_offset, hle.block_size);
hash_log_free(hash_log_address);
}
return 0;
}
static uint64_t get_nbd_offset(uint64_t chunk_size)
{
uint64_t offset = g_args.cur_nbd_offset;
g_args.cur_nbd_offset += chunk_size;
return offset;
}
static int read_one_chunk(uint8_t *hash) {
char log_string[LOG_LINE];
char* buf;
ssize_t err;
clock_t start;
struct hash_log_entry hle = lookup_fingerprint((char*)hash);
sprintf(log_string, "[READ] | phy addr: %lu, chunk size: %lu, hash: ", hle.data_log_offset, hle.block_size);
if (g_args.log_display) {
printf(log_string);
print_chunk_hash(hash, g_args.fingerprint_size);
PRINTLN;
}
start = clock();
lseek(g_args.image_fd, hle.data_log_offset, SEEK_SET);
buf = (char*)malloc(hle.block_size);
err = read(g_args.image_fd, buf, hle.block_size);
assert(err == hle.block_size);
indicators.timer_read_data += clock() - start;
free(buf);
return 0;
}
static int read_one_chunk_by_off(uint64_t offset, void* buf)
{
struct block_map_entry ble;
if (g_args.MAP == BPTREE_MODE) {
clock_t read_tree_start = clock();
ble = bplus_tree_get_fuzzy(g_args.tree, offset);
indicators.timer_read_tree += clock() - read_tree_start;
} else {
clock_t read_space_start = clock();
hash_space space;
hash_get_space(offset, &space);
uint64_t index = offset % MAX_BLOCK_SIZE / MIN_BLOCK_SIZE;
ble = space[index];
indicators.timer_read_space += clock() - read_space_start;
}
read_one_chunk((uint8_t *)ble.fingerprit);
}
static int write_to_disk(uint64_t offset, uint64_t size)
{
ssize_t err;
char *buf = (char*)malloc(size);
clock_t start = clock();
gen_data(buf, size);
lseek64(g_args.image_fd, offset, SEEK_SET);
err = write(g_args.image_fd, buf, size);
assert(err == size);
free(buf);
indicators.timer_write_data += clock() - start;
return 0;
}
static int write_one_chunk(uint64_t offset, uint64_t chunk_size, uint8_t *hash)
{
clock_t w_space_start;
clock_t w_tree_start;
clock_t w_detect_start;
ssize_t ret;
char log_line[MAXLINE];
struct block_map_entry bme;
indicators.n_chunk_sum ++;
indicators.len_data_sum += chunk_size;
w_detect_start = clock();
bme.nbd_offset = offset;
bme.length = chunk_size;
memcpy(bme.fingerprit, hash, g_args.fingerprint_size);
indicators.n_block_map_entry ++;
if (g_args.MAP == BPTREE_MODE) {
w_tree_start = clock();
bplus_tree_put(g_args.tree, offset, bme);
indicators.timer_write_tree += clock() - w_tree_start;
}
else if (g_args.MAP == SPACE_MODE) {
w_space_start = clock();
hash_space_insert(offset, bme);
indicators.timer_write_space += clock() - w_space_start;
}
uint64_t hash_log_address;
struct hash_log_entry hle;
// See if this fingerprint is already stored in HASH_LOG
hash_log_address = hash_index_lookup((char*)hash);
indicators.timer_detect += clock() - w_detect_start;
if (hash_log_address == (uint64_t)-1) {
// This chunk is new
indicators.n_hash_log_entry ++;
indicators.n_chunk_unique ++;
memcpy(hle.fingerprint, hash, g_args.fingerprint_size);
hle.data_log_offset = physical_block_new(chunk_size);
hle.ref_count = 1;
hle.block_size = chunk_size;
sprintf(log_line, "[NEW] | nbd_off: %lu, len: %lu, offset: %lu",bme.nbd_offset, chunk_size, hle.data_log_offset);
if (g_args.log_display) {
printf(log_line);
PRINTLN;
}
if (g_args.log_on) {
zlog_info(g_args.write_block_category, log_line);
}
hash_log_address = hash_log_new();
// Update hash index
hash_index_insert((char*)hash, hash_log_address);
// Update hash log
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
ret = write(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
assert(ret == sizeof(struct hash_log_entry));
// We don't really write data to disk.
write_to_disk(hle.data_log_offset, chunk_size);
} else {
// This chunk has already been stored.
indicators.n_chunk_redundant ++;
indicators.len_data_redundant += chunk_size;
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
ret = read(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
assert(ret == sizeof(struct hash_log_entry));
hle.ref_count ++;
SEEK_TO_HASH_LOG(g_args.hash_fd, hash_log_address);
ret = write(g_args.hash_fd, &hle, sizeof(struct hash_log_entry));
assert(ret == sizeof(struct hash_log_entry));
sprintf(log_line, "[REDUNDANT] | nbd_off: %lu, len: %lu, offset: %lu",
bme.nbd_offset, chunk_size, hle.data_log_offset);
if (g_args.log_display) {
printf(log_line);
PRINTLN;
}
if (g_args.log_on) {
zlog_info(g_args.write_block_category, log_line);
}
}
return 0;
}
static void receive_sigint(int sig)
{
printf("Received SIGINT signal! Exiting...\n");
ssize_t ret;
SEEK_TO_HASH_LOG(g_args.hash_fd, 0);
ret = write(g_args.hash_fd, &hash_log_free_list, sizeof(uint64_t));
assert(ret == sizeof(uint64_t));
if (g_args.log_on) {
zlog_fini();
}
if (g_args.MAP == BPTREE_MODE) {
bplus_tree_deinit(g_args.tree);
}
exit(0);
}
static int read_datafile(char *datafile_name)
{
struct hashfile_handle *handle;
const struct chunk_info *ci;
int ret;
uint64_t n_chunks;
handle = hashfile_open(datafile_name);
if (!handle) {
fprintf(stderr, "Error opening dataset: %d!", errno);
return -1;
}
/* Print some information about the hash file */
g_args.fingerprint_size = hashfile_hash_size(handle) / 8;
/* Go over the files in a hashfile */
n_chunks = 0;
while (1) {
ret = hashfile_next_file(handle);
if (ret < 0) {
fprintf(stderr,
"Cannot get next file from dataset: %d!\n",
errno);
return -1;
}
/* exit the loop if it was the last file */
if (ret == 0)
break;
if (g_args.log_display) {
printf("\nFile path: %s\n", hashfile_curfile_path(handle));
printf("File size: %"PRIu64 " B\n",
hashfile_curfile_size(handle));
printf("Chunks number: %" PRIu64 "\n",
hashfile_curfile_numchunks(handle));
}
/* Go over the chunks in the current file */
while (1) {
ci = hashfile_next_chunk(handle);
if (!ci) /* exit the loop if it was the last chunk */
break;
n_chunks ++;
// if (ci->size < 2*1024 || ci->size > 32*1024) {
// continue;
// }
if (g_args.RW == WRITE_MODE) {
write_one_chunk(get_nbd_offset(ci->size), ci->size, ci->hash);
} else if (g_args.RW == READ_MODE) {
read_one_chunk_by_off(get_nbd_offset(ci->size), NULL);
}
// if (n_chunks >= 50)
// return 0;
}
}
hashfile_close(handle);
return 0;
}
void static open_file(void)
{
g_args.hash_fd = open64(g_args.hash_filename, O_CREAT|O_RDWR|O_LARGEFILE, 0755);
assert(g_args.hash_fd != -1);
g_args.image_fd = open64(g_args.image_filename, O_CREAT|O_RDWR|O_LARGEFILE, 0755);
assert(g_args.image_fd != -1);
}
static int init(void)
{
uint64_t i;
ssize_t err;
/// delete old db file
if (access(g_args.image_filename, F_OK) != -1) {
if (remove(g_args.image_filename) == 0) {
printf("Removed existed file %s\n", g_args.image_filename);
} else {
perror("Remove image file");
}
}
if (access(g_args.bplustree_filename, F_OK) != -1) {
if (remove(g_args.bplustree_filename) == 0) {
printf("Removed existed file %s\n", g_args.bplustree_filename);
} else {
perror("Remove B+Tree db file");
}
}
char tree_boot_filename[255];
sprintf(tree_boot_filename, "%s.boot", g_args.bplustree_filename);
if (access(tree_boot_filename, F_OK) != -1) {
if (remove(tree_boot_filename) == 0) {
printf("Removed existed file %s\n", tree_boot_filename);
} else {
perror("Remove B+Tree boot file");
}
}
if (access(g_args.hash_filename, F_OK) != -1) {
if (remove(g_args.hash_filename) == 0) {
printf("Removed existed file %s\n", g_args.hash_filename);
} else {
perror("Remove hash db file");
}
}
printf("Preparing for new db file!\n");
open_file();
/// no matter in space mode or b+tree mode, we only need to init HASH_LOG area.
/// HASH_INDEX and SPACE area will be a blank hole
for (i = 1; i <= N_BLOCKS; i++) {
SEEK_TO_HASH_LOG(g_args.hash_fd, i - 1);
err = write(g_args.hash_fd, &i, sizeof(uint64_t));
assert(err == sizeof(uint64_t));
}
return 0;
}
void set_default_options()
{
g_args.log_display = false;
g_args.log_on = false; // don't log for now
g_args.hash_filename = "./hash.db";
g_args.bplustree_filename = "./bptree.db";
g_args.dataset_filename = "/home/cyril/dataset/kernel/fslhomes-kernel";
g_args.image_filename = "./image";
g_args.RW = READ_MODE;
// g_args.MAP = BPTREE_MODE;
g_args.MAP = SPACE_MODE;
g_args.run_mode = RUN_MODE;
}
void parse_command_line(int argc, char *argv[])
{
/* command line args */
const char *opt_string = "i:a:h:b:d:w:r";
const struct option long_opts[] = {
{"init", no_argument, NULL, 'i'},
{"hash-file", required_argument, NULL, 'a'},
{"help", no_argument, NULL, 'h'},
{"bplustree", no_argument, NULL, 'b'},
{"space", no_argument, NULL, 's'},
{"dataset", required_argument, NULL, 'd'},
{"write", no_argument, NULL, 'w'},
{"read", no_argument, NULL, 'r'},
{NULL, 0, NULL, NULL},
};
set_default_options();
int opt = getopt_long(argc, argv, opt_string, long_opts, NULL);
while( opt != -1 ) {
switch(opt) {
case 'w':
g_args.run_mode = RUN_MODE;
g_args.RW = WRITE_MODE;
break;
case 'r':
g_args.run_mode = RUN_MODE;
g_args.RW = READ_MODE;
break;
case 'a': // hash data file
g_args.hash_filename = optarg;
break;
case 'i': // init mode
g_args.run_mode = INIT_MODE;
break;
case 'b': // b+tree mode
g_args.MAP = BPTREE_MODE;
break;
case 's':
g_args.MAP = SPACE_MODE;
break;
case 'd': // dataset file
g_args.dataset_filename = optarg;
break;
case 'h': // help
default:
usage();
break;
}
opt = getopt_long(argc, argv, opt_string, long_opts, NULL);
}
}
void print_statistic_info()
{
// write mode
if (g_args.RW == WRITE_MODE) {
printf("\n======================= DATA ======================\n");
printf("Redundant chunks: %lu\n", indicators.n_chunk_redundant);
printf("Unique chunks: %lu\n", indicators.n_chunk_unique);
printf("Hash log entry: %lu\n", indicators.n_hash_log_entry);
printf("Sum chunks: %lu\n", indicators.n_chunk_sum);
printf("Redundant len: %.3f M.\n", (float)indicators.len_data_redundant/1024/1024);
printf("Sum len: %.3f M.\n", (float)indicators.len_data_sum/1024/1024);
printf("\n===================== SIZE ======================\n");
if (g_args.MAP == BPTREE_MODE)
printf("Check db file!\n");
else {
printf("Hash Space size: %.3f M.\n",
indicators.n_space * ENTRIES_PER_SPACE * sizeof(struct block_map_entry) / 1024.0f / 1024.0f);
printf("Hash Space size(real): %.3f M.\n", (float)indicators.n_block_map_entry/ sizeof(struct block_map_entry)/1024/1024);
}
printf("Hash Index size: %.3f M.\n",
indicators.n_bucket * ENTRIES_PER_BUCKET * sizeof(struct hash_index_entry)/1024.0f/1024.0f);
printf("Hash Log size: %.3f M.\n", indicators.n_hash_log_entry * sizeof(struct hash_log_entry)/1024.0f/1024.0f);
printf("\n===================== TIME-W ======================\n");
printf("Judge: %.3f s.\n", (float)indicators.timer_detect/CLOCKS_PER_SEC);
if (g_args.MAP == SPACE_MODE) {
printf("Write Space: %.3f s.\n", (float)indicators.timer_write_space/CLOCKS_PER_SEC);
} else {
printf("Write Tree: %.3f s.\n", (float)indicators.timer_write_tree/CLOCKS_PER_SEC);
}
printf("Write data: %.3f s.\n", (float)indicators.timer_write_data/CLOCKS_PER_SEC);
}
// read mode
else {
printf("\n===================== TIME-R ======================\n");
if (g_args.MAP == BPTREE_MODE)
printf("Read Tree: %.3f s.\n", (float)indicators.timer_read_tree/CLOCKS_PER_SEC);
else
printf("Read Space: %.3f s.\n", (float)indicators.timer_read_space/CLOCKS_PER_SEC);
printf("Read data: %.3f s.\n", (float)indicators.timer_read_data/CLOCKS_PER_SEC);
}
}
int main(int argc, char *argv[])
{
/* First, we parse the cmd line */
parse_command_line(argc, argv);
// Init
if (g_args.run_mode == INIT_MODE) {
fprintf(stdout, "Performing Initialization!\n");
init();
if (g_args.MAP == BPTREE_MODE)
g_args.tree = bplus_tree_init(g_args.bplustree_filename, 4096);
if (g_args.MAP == BPTREE_MODE) {
bplus_tree_deinit(g_args.tree);
}
close(g_args.hash_fd);
close(g_args.image_fd);
return 0;
}
ssize_t err;
g_args.cur_nbd_offset = 0;
if (g_args.MAP == BPTREE_MODE)
g_args.tree = bplus_tree_init(g_args.bplustree_filename, 4096);
open_file();
memset(&indicators, 0, sizeof(struct evaluation_indicators_s));
/* By convention the first entry in the hash log is a pointer to the hash
* log free list. Likewise for the data log. */
SEEK_TO_HASH_LOG(g_args.hash_fd, 0);
err = read(g_args.hash_fd, &hash_log_free_list, sizeof(uint64_t));
assert(err == sizeof(uint64_t));
// hash_log_free_list = 1;
/* Listen SIGINT signal */
signal(SIGINT, &receive_sigint);
/// 1 << CACHE_SIZE = 2^CACHE_SIZE
cache = calloc(1 << CACHE_SIZE, sizeof(struct hash_log_entry));
if (g_args.log_on) {
/* Init zlog */
err = zlog_init("../config/zlog.conf");
if(err) {
fprintf(stderr, "zlog init failed\n");
return -1;
}
g_args.write_block_category = zlog_get_category("ds");
if (!g_args.write_block_category) {
fprintf(stderr, "get ds_log_category failed\n");
zlog_fini();
return -2;
}
g_args.log_error = zlog_get_category("ds_error");
if (!g_args.log_error) {
fprintf(stderr, "get log_error_category failed\n");
zlog_fini();
return -2;
}
}
read_datafile(g_args.dataset_filename);
print_statistic_info();
SEEK_TO_HASH_LOG(g_args.hash_fd, 0);
err = write(g_args.hash_fd, &hash_log_free_list, sizeof(uint64_t));
assert(err == sizeof(uint64_t));
if (g_args.log_on) {
zlog_fini();
}
if (g_args.MAP == BPTREE_MODE) {
bplus_tree_deinit(g_args.tree);
}
close(g_args.hash_fd);
close(g_args.image_fd);
return 0;
}