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,
16 #include "../fio_sem.h"
17 #include "../smalloc.h"
18 #include "../minmax.h"
19 #include "../crc/md5.h"
21 #include "../gettime.h"
22 #include "../fio_time.h"
23 #include "../lib/rbtree.h"
25 #include "../lib/bloom.h"
31 unsigned char *buf_in;
32 unsigned char *buf_out;
35 struct worker_thread {
40 unsigned long long unique_capacity;
49 struct flist_head list;
54 struct fio_rb_node rb_node;
56 uint32_t hash[MD5_HASH_WORDS];
57 struct flist_head extent_list[0];
62 uint32_t hash[MD5_HASH_WORDS];
65 static struct rb_root rb_root;
66 static struct bloom *bloom;
67 static struct fio_sem *rb_lock;
69 static unsigned int blocksize = 4096;
70 static unsigned int num_threads;
71 static unsigned int chunk_size = 1048576;
72 static unsigned int dump_output;
73 static unsigned int odirect;
74 static unsigned int collision_check;
75 static unsigned int print_progress = 1;
76 static unsigned int use_bloom = 1;
77 static unsigned int compression = 0;
79 static uint64_t total_size;
80 static uint64_t cur_offset;
81 static struct fio_sem *size_lock;
83 static struct fio_file file;
85 static uint64_t get_size(struct fio_file *f, struct stat *sb)
89 if (S_ISBLK(sb->st_mode)) {
90 unsigned long long bytes = 0;
92 if (blockdev_size(f, &bytes)) {
93 log_err("dedupe: failed getting bdev size\n");
101 return (ret & ~((uint64_t)blocksize - 1));
104 static int get_work(uint64_t *offset, uint64_t *size)
109 fio_sem_down(size_lock);
111 if (cur_offset < total_size) {
112 *offset = cur_offset;
113 this_chunk = min((uint64_t)chunk_size, total_size - cur_offset);
115 cur_offset += this_chunk;
119 fio_sem_up(size_lock);
123 static int __read_block(int fd, void *buf, off_t offset, size_t count)
127 ret = pread(fd, buf, count, offset);
133 } else if (ret != count) {
134 log_err("dedupe: short read on block\n");
141 static int read_block(int fd, void *buf, off_t offset)
143 return __read_block(fd, buf, offset, blocksize);
146 static void account_unique_capacity(uint64_t offset, uint64_t *unique_capacity,
147 struct zlib_ctrl *zc)
149 z_stream *stream = &zc->stream;
150 unsigned int compressed_len;
153 if (read_block(file.fd, zc->buf_in, offset))
156 stream->next_in = zc->buf_in;
157 stream->avail_in = blocksize;
158 stream->avail_out = deflateBound(stream, blocksize);
159 stream->next_out = zc->buf_out;
161 ret = deflate(stream, Z_FINISH);
162 assert(ret != Z_STREAM_ERROR);
163 compressed_len = blocksize - stream->avail_out;
166 printf("offset 0x%lx compressed to %d blocksize %d ratio %.2f \n",
167 (unsigned long) offset, compressed_len, blocksize,
168 (float)compressed_len / (float)blocksize);
170 *unique_capacity += compressed_len;
171 deflateReset(stream);
174 static void add_item(struct chunk *c, struct item *i)
177 * Save some memory and don't add extent items, if we don't
180 if (dump_output || collision_check) {
183 e = malloc(sizeof(*e));
184 e->offset = i->offset;
185 flist_add_tail(&e->list, &c->extent_list[0]);
191 static int col_check(struct chunk *c, struct item *i)
197 cbuf = fio_memalign(blocksize, blocksize, false);
198 ibuf = fio_memalign(blocksize, blocksize, false);
200 e = flist_entry(c->extent_list[0].next, struct extent, list);
201 if (read_block(file.fd, cbuf, e->offset))
204 if (read_block(file.fd, ibuf, i->offset))
207 ret = memcmp(ibuf, cbuf, blocksize);
209 fio_memfree(cbuf, blocksize, false);
210 fio_memfree(ibuf, blocksize, false);
214 static struct chunk *alloc_chunk(void)
218 if (collision_check || dump_output) {
219 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
220 INIT_FLIST_HEAD(&c->extent_list[0]);
222 c = malloc(sizeof(struct chunk));
228 static void insert_chunk(struct item *i, uint64_t *unique_capacity,
229 struct zlib_ctrl *zc)
231 struct fio_rb_node **p, *parent;
235 p = &rb_root.rb_node;
240 c = rb_entry(parent, struct chunk, rb_node);
241 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
244 } else if (diff > 0) {
249 if (!collision_check)
253 ret = col_check(c, i);
254 fio_sem_down(rb_lock);
264 RB_CLEAR_NODE(&c->rb_node);
266 memcpy(c->hash, i->hash, sizeof(i->hash));
267 rb_link_node(&c->rb_node, parent, p);
268 rb_insert_color(&c->rb_node, &rb_root);
270 account_unique_capacity(i->offset, unique_capacity, zc);
275 static void insert_chunks(struct item *items, unsigned int nitems,
276 uint64_t *ndupes, uint64_t *unique_capacity,
277 struct zlib_ctrl *zc)
281 fio_sem_down(rb_lock);
283 for (i = 0; i < nitems; i++) {
288 s = sizeof(items[i].hash) / sizeof(uint32_t);
289 r = bloom_set(bloom, items[i].hash, s);
292 insert_chunk(&items[i], unique_capacity, zc);
298 static void crc_buf(void *buf, uint32_t *hash)
300 struct fio_md5_ctx ctx = { .hash = hash };
303 fio_md5_update(&ctx, buf, blocksize);
307 static unsigned int read_blocks(int fd, void *buf, off_t offset, size_t size)
309 if (__read_block(fd, buf, offset, size))
312 return size / blocksize;
315 static int do_work(struct worker_thread *thread, void *buf)
317 unsigned int nblocks, i;
321 uint64_t unique_capacity = 0;
324 offset = thread->cur_offset;
326 nblocks = read_blocks(thread->fd, buf, offset,
327 min(thread->size, (uint64_t) chunk_size));
331 items = malloc(sizeof(*items) * nblocks);
333 for (i = 0; i < nblocks; i++) {
334 void *thisptr = buf + (i * blocksize);
336 items[i].offset = offset;
337 crc_buf(thisptr, items[i].hash);
342 insert_chunks(items, nitems, &ndupes, &unique_capacity, &thread->zc);
345 thread->items += nitems;
346 thread->dupes += ndupes;
347 thread->unique_capacity += unique_capacity;
351 static void thread_init_zlib_control(struct worker_thread *thread)
355 z_stream *stream = &thread->zc.stream;
356 stream->zalloc = Z_NULL;
357 stream->zfree = Z_NULL;
358 stream->opaque = Z_NULL;
360 if (deflateInit(stream, Z_DEFAULT_COMPRESSION) != Z_OK)
363 thread->zc.buf_in = fio_memalign(blocksize, blocksize, false);
364 sz = deflateBound(stream, blocksize);
365 thread->zc.buf_out = fio_memalign(blocksize, sz, false);
368 static void *thread_fn(void *data)
370 struct worker_thread *thread = data;
373 buf = fio_memalign(blocksize, chunk_size, false);
374 thread_init_zlib_control(thread);
377 if (get_work(&thread->cur_offset, &thread->size)) {
381 if (do_work(thread, buf)) {
388 fio_memfree(buf, chunk_size, false);
392 static void show_progress(struct worker_thread *threads, unsigned long total)
394 unsigned long last_nitems = 0;
395 struct timespec last_tv;
397 fio_gettime(&last_tv, NULL);
399 while (print_progress) {
400 unsigned long this_items;
401 unsigned long nitems = 0;
407 for (i = 0; i < num_threads; i++) {
408 nitems += threads[i].items;
409 some_done = threads[i].done;
417 perc = (float) nitems / (float) total;
419 this_items = nitems - last_nitems;
420 this_items *= blocksize;
421 tdiff = mtime_since_now(&last_tv);
423 this_items = (this_items * 1000) / (tdiff * 1024);
424 printf("%3.2f%% done (%luKiB/sec)\r", perc, this_items);
425 last_nitems = nitems;
426 fio_gettime(&last_tv, NULL);
428 printf("%3.2f%% done\r", perc);
435 static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size,
436 uint64_t *nextents, uint64_t *nchunks,
437 uint64_t *unique_capacity)
439 struct worker_thread *threads;
440 unsigned long nitems, total_items;
443 total_size = dev_size;
444 total_items = dev_size / blocksize;
446 size_lock = fio_sem_init(FIO_SEM_UNLOCKED);
448 threads = malloc(num_threads * sizeof(struct worker_thread));
449 for (i = 0; i < num_threads; i++) {
450 memset(&threads[i], 0, sizeof(struct worker_thread));
451 threads[i].fd = f->fd;
453 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
455 log_err("fio: thread startup failed\n");
460 show_progress(threads, total_items);
465 *unique_capacity = 0;
466 for (i = 0; i < num_threads; i++) {
468 pthread_join(threads[i].thread, &ret);
469 nitems += threads[i].items;
470 *nchunks += threads[i].dupes;
471 *unique_capacity += threads[i].unique_capacity;
474 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
477 *nchunks = nitems - *nchunks;
479 fio_sem_remove(size_lock);
484 static int dedupe_check(const char *filename, uint64_t *nextents,
485 uint64_t *nchunks, uint64_t *unique_capacity)
493 flags |= OS_O_DIRECT;
495 memset(&file, 0, sizeof(file));
496 file.file_name = strdup(filename);
498 file.fd = open(filename, flags);
504 if (fstat(file.fd, &sb) < 0) {
509 dev_size = get_size(&file, &sb);
514 uint64_t bloom_entries;
516 bloom_entries = 8 * (dev_size / blocksize);
517 bloom = bloom_new(bloom_entries);
520 printf("Will check <%s>, size <%llu>, using %u threads\n", filename,
521 (unsigned long long) dev_size, num_threads);
523 return run_dedupe_threads(&file, dev_size, nextents, nchunks,
528 free(file.file_name);
532 static void show_chunk(struct chunk *c)
534 struct flist_head *n;
537 printf("c hash %8x %8x %8x %8x, count %lu\n", c->hash[0], c->hash[1],
538 c->hash[2], c->hash[3], (unsigned long) c->count);
539 flist_for_each(n, &c->extent_list[0]) {
540 e = flist_entry(n, struct extent, list);
541 printf("\toffset %llu\n", (unsigned long long) e->offset);
545 static const char *capacity_unit[] = {"b","KB", "MB", "GB", "TB", "PB", "EB"};
547 static uint64_t bytes_to_human_readable_unit(uint64_t n, const char **unit_out)
556 *unit_out = capacity_unit[i];
560 static void show_stat(uint64_t nextents, uint64_t nchunks, uint64_t ndupextents,
561 uint64_t unique_capacity)
567 printf("Extents=%lu, Unique extents=%lu", (unsigned long) nextents,
568 (unsigned long) nchunks);
570 printf(" Duplicated extents=%lu", (unsigned long) ndupextents);
574 ratio = (double) nextents / (double) nchunks;
575 printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
577 printf("De-dupe ratio: 1:infinite\n");
581 printf("De-dupe working set at least: %3.2f%%\n",
582 100.0 * (double) ndupextents / (double) nextents);
585 perc = 1.00 - ((double) nchunks / (double) nextents);
587 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
591 uc_human = bytes_to_human_readable_unit(unique_capacity, &unit);
592 printf("Unique capacity %lu%s\n", (unsigned long) uc_human, unit);
596 static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks, uint64_t *ndupextents)
598 struct fio_rb_node *n;
599 *nchunks = *nextents = *ndupextents = 0;
601 n = rb_first(&rb_root);
608 c = rb_entry(n, struct chunk, rb_node);
610 *nextents += c->count;
611 *ndupextents += (c->count > 1);
616 } while ((n = rb_next(n)) != NULL);
619 static int usage(char *argv[])
621 log_err("Check for dedupable blocks on a device/file\n\n");
622 log_err("%s: [options] <device or file>\n", argv[0]);
623 log_err("\t-b\tChunk size to use\n");
624 log_err("\t-t\tNumber of threads to use\n");
625 log_err("\t-d\tFull extent/chunk debug output\n");
626 log_err("\t-o\tUse O_DIRECT\n");
627 log_err("\t-c\tFull collision check\n");
628 log_err("\t-B\tUse probabilistic bloom filter\n");
629 log_err("\t-p\tPrint progress indicator\n");
630 log_err("\t-C\tCalculate compressible size\n");
634 int main(int argc, char *argv[])
636 uint64_t nextents = 0, nchunks = 0, ndupextents = 0, unique_capacity;
642 while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:C:")) != -1) {
645 blocksize = atoi(optarg);
648 num_threads = atoi(optarg);
651 dump_output = atoi(optarg);
654 odirect = atoi(optarg);
657 collision_check = atoi(optarg);
660 print_progress = atoi(optarg);
663 use_bloom = atoi(optarg);
666 compression = atoi(optarg);
674 if (collision_check || dump_output || compression)
678 num_threads = cpus_online();
686 rb_lock = fio_sem_init(FIO_SEM_UNLOCKED);
688 ret = dedupe_check(argv[optind], &nextents, &nchunks, &unique_capacity);
692 iter_rb_tree(&nextents, &nchunks, &ndupextents);
694 show_stat(nextents, nchunks, ndupextents, unique_capacity);
697 fio_sem_remove(rb_lock);