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>
17 #include "../lib/rbtree.h"
21 #include "../smalloc.h"
22 #include "../minmax.h"
23 #include "../crc/md5.h"
24 #include "../memalign.h"
26 #include "../gettime.h"
27 #include "../fio_time.h"
29 #include "../lib/bloom.h"
32 struct worker_thread {
47 struct flist_head list;
52 struct rb_node rb_node;
54 uint32_t hash[MD5_HASH_WORDS];
55 struct flist_head extent_list[0];
60 uint32_t hash[MD5_HASH_WORDS];
63 static struct rb_root rb_root;
64 static struct bloom *bloom;
65 static struct fio_mutex *rb_lock;
67 static unsigned int blocksize = 4096;
68 static unsigned int num_threads;
69 static unsigned int chunk_size = 1048576;
70 static unsigned int dump_output;
71 static unsigned int odirect;
72 static unsigned int collision_check;
73 static unsigned int print_progress = 1;
74 static unsigned int use_bloom = 1;
76 static uint64_t total_size;
77 static uint64_t cur_offset;
78 static struct fio_mutex *size_lock;
80 static struct fio_file file;
82 static uint64_t get_size(struct fio_file *f, struct stat *sb)
86 if (S_ISBLK(sb->st_mode)) {
87 unsigned long long bytes;
89 if (blockdev_size(f, &bytes)) {
90 log_err("dedupe: failed getting bdev size\n");
97 return (ret & ~((uint64_t)blocksize - 1));
100 static int get_work(uint64_t *offset, uint64_t *size)
105 fio_mutex_down(size_lock);
107 if (cur_offset < total_size) {
108 *offset = cur_offset;
109 this_chunk = min((uint64_t)chunk_size, total_size - cur_offset);
111 cur_offset += this_chunk;
115 fio_mutex_up(size_lock);
119 static int __read_block(int fd, void *buf, off_t offset, size_t count)
123 ret = pread(fd, buf, count, offset);
129 else if (ret != count) {
130 log_err("dedupe: short read on block\n");
137 static int read_block(int fd, void *buf, off_t offset)
139 return __read_block(fd, buf, offset, blocksize);
142 static void add_item(struct chunk *c, struct item *i)
145 * Save some memory and don't add extent items, if we don't
148 if (dump_output || collision_check) {
151 e = malloc(sizeof(*e));
152 e->offset = i->offset;
153 flist_add_tail(&e->list, &c->extent_list[0]);
159 static int col_check(struct chunk *c, struct item *i)
165 cbuf = fio_memalign(blocksize, blocksize);
166 ibuf = fio_memalign(blocksize, blocksize);
168 e = flist_entry(c->extent_list[0].next, struct extent, list);
169 if (read_block(file.fd, cbuf, e->offset))
172 if (read_block(file.fd, ibuf, i->offset))
175 ret = memcmp(ibuf, cbuf, blocksize);
177 fio_memfree(cbuf, blocksize);
178 fio_memfree(ibuf, blocksize);
182 static struct chunk *alloc_chunk(void)
186 if (collision_check || dump_output) {
187 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
188 INIT_FLIST_HEAD(&c->extent_list[0]);
190 c = malloc(sizeof(struct chunk));
195 static void insert_chunk(struct item *i)
197 struct rb_node **p, *parent;
201 p = &rb_root.rb_node;
206 c = rb_entry(parent, struct chunk, rb_node);
207 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
215 if (!collision_check)
218 fio_mutex_up(rb_lock);
219 ret = col_check(c, i);
220 fio_mutex_down(rb_lock);
230 RB_CLEAR_NODE(&c->rb_node);
232 memcpy(c->hash, i->hash, sizeof(i->hash));
233 rb_link_node(&c->rb_node, parent, p);
234 rb_insert_color(&c->rb_node, &rb_root);
239 static void insert_chunks(struct item *items, unsigned int nitems,
244 fio_mutex_down(rb_lock);
246 for (i = 0; i < nitems; i++) {
251 s = sizeof(items[i].hash) / sizeof(uint32_t);
252 r = bloom_set(bloom, items[i].hash, s);
255 insert_chunk(&items[i]);
258 fio_mutex_up(rb_lock);
261 static void crc_buf(void *buf, uint32_t *hash)
263 struct fio_md5_ctx ctx = { .hash = hash };
266 fio_md5_update(&ctx, buf, blocksize);
270 static unsigned int read_blocks(int fd, void *buf, off_t offset, size_t size)
272 if (__read_block(fd, buf, offset, size))
275 return size / blocksize;
278 static int do_work(struct worker_thread *thread, void *buf)
280 unsigned int nblocks, i;
286 offset = thread->cur_offset;
288 nblocks = read_blocks(thread->fd, buf, offset, min(thread->size, (uint64_t)chunk_size));
292 items = malloc(sizeof(*items) * nblocks);
294 for (i = 0; i < nblocks; i++) {
295 void *thisptr = buf + (i * blocksize);
298 items[i].offset = offset;
299 crc_buf(thisptr, items[i].hash);
304 insert_chunks(items, nitems, &ndupes);
307 thread->items += nitems;
308 thread->dupes += ndupes;
312 static void *thread_fn(void *data)
314 struct worker_thread *thread = data;
317 buf = fio_memalign(blocksize, chunk_size);
320 if (get_work(&thread->cur_offset, &thread->size)) {
324 if (do_work(thread, buf)) {
331 fio_memfree(buf, chunk_size);
335 static void show_progress(struct worker_thread *threads, unsigned long total)
337 unsigned long last_nitems = 0;
338 struct timeval last_tv;
340 fio_gettime(&last_tv, NULL);
342 while (print_progress) {
343 unsigned long this_items;
344 unsigned long nitems = 0;
350 for (i = 0; i < num_threads; i++) {
351 nitems += threads[i].items;
352 some_done = threads[i].done;
360 perc = (float) nitems / (float) total;
362 this_items = nitems - last_nitems;
363 this_items *= blocksize;
364 tdiff = mtime_since_now(&last_tv);
366 this_items = (this_items * 1000) / (tdiff * 1024);
367 printf("%3.2f%% done (%luKB/sec)\r", perc, this_items);
368 last_nitems = nitems;
369 fio_gettime(&last_tv, NULL);
371 printf("%3.2f%% done\r", perc);
377 static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size,
378 uint64_t *nextents, uint64_t *nchunks)
380 struct worker_thread *threads;
381 unsigned long nitems, total_items;
384 total_size = dev_size;
385 total_items = dev_size / blocksize;
387 size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
389 threads = malloc(num_threads * sizeof(struct worker_thread));
390 for (i = 0; i < num_threads; i++) {
391 threads[i].fd = f->fd;
392 threads[i].items = 0;
396 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
398 log_err("fio: thread startup failed\n");
403 show_progress(threads, total_items);
408 for (i = 0; i < num_threads; i++) {
410 pthread_join(threads[i].thread, &ret);
411 nitems += threads[i].items;
412 *nchunks += threads[i].dupes;
415 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
418 *nchunks = nitems - *nchunks;
420 fio_mutex_remove(size_lock);
425 static int dedupe_check(const char *filename, uint64_t *nextents,
436 memset(&file, 0, sizeof(file));
437 file.file_name = strdup(filename);
439 file.fd = open(filename, flags);
445 if (fstat(file.fd, &sb) < 0) {
450 dev_size = get_size(&file, &sb);
455 uint64_t bloom_entries;
457 bloom_entries = 8 * (dev_size / blocksize);
458 bloom = bloom_new(bloom_entries);
461 printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
463 return run_dedupe_threads(&file, dev_size, nextents, nchunks);
467 free(file.file_name);
471 static void show_chunk(struct chunk *c)
473 struct flist_head *n;
476 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);
477 flist_for_each(n, &c->extent_list[0]) {
478 e = flist_entry(n, struct extent, list);
479 printf("\toffset %llu\n", (unsigned long long) e->offset);
483 static void show_stat(uint64_t nextents, uint64_t nchunks)
487 printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
490 ratio = (double) nextents / (double) nchunks;
491 printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
493 printf("De-dupe ratio: 1:infinite\n");
495 perc = 1.00 - ((double) nchunks / (double) nextents);
497 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
501 static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks)
505 *nchunks = *nextents = 0;
507 n = rb_first(&rb_root);
514 c = rb_entry(n, struct chunk, rb_node);
516 *nextents += c->count;
521 } while ((n = rb_next(n)) != NULL);
524 static int usage(char *argv[])
526 log_err("Check for dedupable blocks on a device/file\n\n");
527 log_err("%s: [options] <device or file>\n", argv[0]);
528 log_err("\t-b\tChunk size to use\n");
529 log_err("\t-t\tNumber of threads to use\n");
530 log_err("\t-d\tFull extent/chunk debug output\n");
531 log_err("\t-o\tUse O_DIRECT\n");
532 log_err("\t-c\tFull collision check\n");
533 log_err("\t-B\tUse probabilistic bloom filter\n");
534 log_err("\t-p\tPrint progress indicator\n");
538 int main(int argc, char *argv[])
540 uint64_t nextents = 0, nchunks = 0;
545 while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) {
548 blocksize = atoi(optarg);
551 num_threads = atoi(optarg);
554 dump_output = atoi(optarg);
557 odirect = atoi(optarg);
560 collision_check = atoi(optarg);
563 print_progress = atoi(optarg);
566 use_bloom = atoi(optarg);
574 if (collision_check || dump_output)
578 num_threads = cpus_online();
586 rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
588 ret = dedupe_check(argv[optind], &nextents, &nchunks);
592 iter_rb_tree(&nextents, &nchunks);
594 show_stat(nextents, nchunks);
597 fio_mutex_remove(rb_lock);