2 * Small tool to check for dedupable blocks in a file or device. Basically
3 * just scans the filename for extents of the given size, checksums them,
11 #include <sys/types.h>
13 #include <sys/ioctl.h>
18 #include "../lib/rbtree.h"
22 #include "../smalloc.h"
23 #include "../minmax.h"
24 #include "../crc/md5.h"
25 #include "../memalign.h"
27 #include "../gettime.h"
28 #include "../fio_time.h"
30 #include "../lib/bloom.h"
33 struct worker_thread {
48 struct flist_head list;
53 struct rb_node rb_node;
55 uint32_t hash[MD5_HASH_WORDS];
56 struct flist_head extent_list[0];
61 uint32_t hash[MD5_HASH_WORDS];
64 static struct rb_root rb_root;
65 static struct bloom *bloom;
66 static struct fio_mutex *rb_lock;
68 static unsigned int blocksize = 4096;
69 static unsigned int num_threads;
70 static unsigned int chunk_size = 1048576;
71 static unsigned int dump_output;
72 static unsigned int odirect;
73 static unsigned int collision_check;
74 static unsigned int print_progress = 1;
75 static unsigned int use_bloom = 1;
77 static uint64_t total_size;
78 static uint64_t cur_offset;
79 static struct fio_mutex *size_lock;
81 static struct fio_file file;
83 static uint64_t get_size(struct fio_file *f, struct stat *sb)
87 if (S_ISBLK(sb->st_mode)) {
88 unsigned long long bytes;
90 if (blockdev_size(f, &bytes)) {
91 log_err("dedupe: failed getting bdev size\n");
98 return (ret & ~((uint64_t)blocksize - 1));
101 static int get_work(uint64_t *offset, uint64_t *size)
106 fio_mutex_down(size_lock);
108 if (cur_offset < total_size) {
109 *offset = cur_offset;
110 this_chunk = min((uint64_t)chunk_size, total_size - cur_offset);
112 cur_offset += this_chunk;
116 fio_mutex_up(size_lock);
120 static int __read_block(int fd, void *buf, off_t offset, size_t count)
124 ret = pread(fd, buf, count, offset);
130 else if (ret != count) {
131 log_err("dedupe: short read on block\n");
138 static int read_block(int fd, void *buf, off_t offset)
140 return __read_block(fd, buf, offset, blocksize);
143 static void add_item(struct chunk *c, struct item *i)
146 * Save some memory and don't add extent items, if we don't
149 if (dump_output || collision_check) {
152 e = malloc(sizeof(*e));
153 e->offset = i->offset;
154 flist_add_tail(&e->list, &c->extent_list[0]);
160 static int col_check(struct chunk *c, struct item *i)
166 cbuf = fio_memalign(blocksize, blocksize);
167 ibuf = fio_memalign(blocksize, blocksize);
169 e = flist_entry(c->extent_list[0].next, struct extent, list);
170 if (read_block(file.fd, cbuf, e->offset))
173 if (read_block(file.fd, ibuf, i->offset))
176 ret = memcmp(ibuf, cbuf, blocksize);
178 fio_memfree(cbuf, blocksize);
179 fio_memfree(ibuf, blocksize);
183 static struct chunk *alloc_chunk(void)
187 if (collision_check || dump_output) {
188 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
189 INIT_FLIST_HEAD(&c->extent_list[0]);
191 c = malloc(sizeof(struct chunk));
196 static void insert_chunk(struct item *i)
198 struct rb_node **p, *parent;
202 p = &rb_root.rb_node;
207 c = rb_entry(parent, struct chunk, rb_node);
208 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
216 if (!collision_check)
219 fio_mutex_up(rb_lock);
220 ret = col_check(c, i);
221 fio_mutex_down(rb_lock);
231 RB_CLEAR_NODE(&c->rb_node);
233 memcpy(c->hash, i->hash, sizeof(i->hash));
234 rb_link_node(&c->rb_node, parent, p);
235 rb_insert_color(&c->rb_node, &rb_root);
240 static void insert_chunks(struct item *items, unsigned int nitems,
245 fio_mutex_down(rb_lock);
247 for (i = 0; i < nitems; i++) {
252 s = sizeof(items[i].hash) / sizeof(uint32_t);
253 r = bloom_set(bloom, items[i].hash, s);
256 insert_chunk(&items[i]);
259 fio_mutex_up(rb_lock);
262 static void crc_buf(void *buf, uint32_t *hash)
264 struct fio_md5_ctx ctx = { .hash = hash };
267 fio_md5_update(&ctx, buf, blocksize);
271 static unsigned int read_blocks(int fd, void *buf, off_t offset, size_t size)
273 if (__read_block(fd, buf, offset, size))
276 return size / blocksize;
279 static int do_work(struct worker_thread *thread, void *buf)
281 unsigned int nblocks, i;
287 offset = thread->cur_offset;
289 nblocks = read_blocks(thread->fd, buf, offset, min(thread->size, (uint64_t)chunk_size));
293 items = malloc(sizeof(*items) * nblocks);
295 for (i = 0; i < nblocks; i++) {
296 void *thisptr = buf + (i * blocksize);
299 items[i].offset = offset;
300 crc_buf(thisptr, items[i].hash);
305 insert_chunks(items, nitems, &ndupes);
308 thread->items += nitems;
309 thread->dupes += ndupes;
313 static void *thread_fn(void *data)
315 struct worker_thread *thread = data;
318 buf = fio_memalign(blocksize, chunk_size);
321 if (get_work(&thread->cur_offset, &thread->size)) {
325 if (do_work(thread, buf)) {
332 fio_memfree(buf, chunk_size);
336 static void show_progress(struct worker_thread *threads, unsigned long total)
338 unsigned long last_nitems = 0;
339 struct timeval last_tv;
341 fio_gettime(&last_tv, NULL);
343 while (print_progress) {
344 unsigned long this_items;
345 unsigned long nitems = 0;
351 for (i = 0; i < num_threads; i++) {
352 nitems += threads[i].items;
353 some_done = threads[i].done;
361 perc = (float) nitems / (float) total;
363 this_items = nitems - last_nitems;
364 this_items *= blocksize;
365 tdiff = mtime_since_now(&last_tv);
367 this_items = (this_items * 1000) / (tdiff * 1024);
368 printf("%3.2f%% done (%luKB/sec)\r", perc, this_items);
369 last_nitems = nitems;
370 fio_gettime(&last_tv, NULL);
372 printf("%3.2f%% done\r", perc);
378 static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size,
379 uint64_t *nextents, uint64_t *nchunks)
381 struct worker_thread *threads;
382 unsigned long nitems, total_items;
385 total_size = dev_size;
386 total_items = dev_size / blocksize;
388 size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
390 threads = malloc(num_threads * sizeof(struct worker_thread));
391 for (i = 0; i < num_threads; i++) {
392 threads[i].fd = f->fd;
393 threads[i].items = 0;
397 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
399 log_err("fio: thread startup failed\n");
404 show_progress(threads, total_items);
409 for (i = 0; i < num_threads; i++) {
411 pthread_join(threads[i].thread, &ret);
412 nitems += threads[i].items;
413 *nchunks += threads[i].dupes;
416 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
419 *nchunks = nitems - *nchunks;
421 fio_mutex_remove(size_lock);
426 static int dedupe_check(const char *filename, uint64_t *nextents,
437 memset(&file, 0, sizeof(file));
438 file.file_name = strdup(filename);
440 file.fd = open(filename, flags);
446 if (fstat(file.fd, &sb) < 0) {
451 dev_size = get_size(&file, &sb);
456 uint64_t bloom_entries;
458 bloom_entries = 8 * (dev_size / blocksize);
459 bloom = bloom_new(bloom_entries);
462 printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
464 return run_dedupe_threads(&file, dev_size, nextents, nchunks);
468 free(file.file_name);
472 static void show_chunk(struct chunk *c)
474 struct flist_head *n;
477 printf("c hash %8x %8x %8x %8x, count %lu\n", c->hash[0], c->hash[1], c->hash[2], c->hash[3], (unsigned long) c->count);
478 flist_for_each(n, &c->extent_list[0]) {
479 e = flist_entry(n, struct extent, list);
480 printf("\toffset %llu\n", (unsigned long long) e->offset);
484 static void show_stat(uint64_t nextents, uint64_t nchunks)
488 printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
491 ratio = (double) nextents / (double) nchunks;
492 printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
494 printf("De-dupe ratio: 1:infinite\n");
496 perc = 1.00 - ((double) nchunks / (double) nextents);
498 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
502 static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks)
506 *nchunks = *nextents = 0;
508 n = rb_first(&rb_root);
515 c = rb_entry(n, struct chunk, rb_node);
517 *nextents += c->count;
522 } while ((n = rb_next(n)) != NULL);
525 static int usage(char *argv[])
527 log_err("Check for dedupable blocks on a device/file\n\n");
528 log_err("%s: [options] <device or file>\n", argv[0]);
529 log_err("\t-b\tChunk size to use\n");
530 log_err("\t-t\tNumber of threads to use\n");
531 log_err("\t-d\tFull extent/chunk debug output\n");
532 log_err("\t-o\tUse O_DIRECT\n");
533 log_err("\t-c\tFull collision check\n");
534 log_err("\t-B\tUse probabilistic bloom filter\n");
535 log_err("\t-p\tPrint progress indicator\n");
539 int main(int argc, char *argv[])
541 uint64_t nextents = 0, nchunks = 0;
546 while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) {
549 blocksize = atoi(optarg);
552 num_threads = atoi(optarg);
555 dump_output = atoi(optarg);
558 odirect = atoi(optarg);
561 collision_check = atoi(optarg);
564 print_progress = atoi(optarg);
567 use_bloom = atoi(optarg);
575 if (collision_check || dump_output)
579 num_threads = cpus_online();
587 rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
589 ret = dedupe_check(argv[optind], &nextents, &nchunks);
593 iter_rb_tree(&nextents, &nchunks);
595 show_stat(nextents, nchunks);
598 fio_mutex_remove(rb_lock);