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");
100 return (ret & ~((uint64_t)blocksize - 1));
103 static int get_work(uint64_t *offset, uint64_t *size)
108 fio_sem_down(size_lock);
110 if (cur_offset < total_size) {
111 *offset = cur_offset;
112 this_chunk = min((uint64_t)chunk_size, total_size - cur_offset);
114 cur_offset += this_chunk;
118 fio_sem_up(size_lock);
122 static int __read_block(int fd, void *buf, off_t offset, size_t count)
126 ret = pread(fd, buf, count, offset);
132 else if (ret != count) {
133 log_err("dedupe: short read on block\n");
140 static int read_block(int fd, void *buf, off_t offset)
142 return __read_block(fd, buf, offset, blocksize);
145 static void account_unique_capacity(uint64_t offset, uint64_t *unique_capacity, struct zlib_ctrl *zc)
147 z_stream *stream = &zc->stream;
148 unsigned int compressed_len;
151 if (read_block(file.fd, zc->buf_in, offset))
154 stream->next_in = zc->buf_in;
155 stream->avail_in = blocksize;
156 stream->avail_out = deflateBound(stream, blocksize);
157 stream->next_out = zc->buf_out;
159 ret = deflate(stream, Z_FINISH);
160 assert(ret != Z_STREAM_ERROR);
161 compressed_len = blocksize - stream->avail_out;
164 printf("offset 0x%lx compressed to %d blocksize %d ratio %.2f \n",
165 (unsigned long) offset, compressed_len, blocksize,
166 (float)compressed_len / (float)blocksize);
168 *unique_capacity += compressed_len;
170 deflateReset(stream);
173 static void add_item(struct chunk *c, struct item *i)
176 * Save some memory and don't add extent items, if we don't
179 if (dump_output || collision_check) {
182 e = malloc(sizeof(*e));
183 e->offset = i->offset;
184 flist_add_tail(&e->list, &c->extent_list[0]);
190 static int col_check(struct chunk *c, struct item *i)
196 cbuf = fio_memalign(blocksize, blocksize, false);
197 ibuf = fio_memalign(blocksize, blocksize, false);
199 e = flist_entry(c->extent_list[0].next, struct extent, list);
200 if (read_block(file.fd, cbuf, e->offset))
203 if (read_block(file.fd, ibuf, i->offset))
206 ret = memcmp(ibuf, cbuf, blocksize);
208 fio_memfree(cbuf, blocksize, false);
209 fio_memfree(ibuf, blocksize, false);
213 static struct chunk *alloc_chunk(void)
217 if (collision_check || dump_output) {
218 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
219 INIT_FLIST_HEAD(&c->extent_list[0]);
221 c = malloc(sizeof(struct chunk));
226 static void insert_chunk(struct item *i, uint64_t *unique_capacity, struct zlib_ctrl *zc)
228 struct fio_rb_node **p, *parent;
232 p = &rb_root.rb_node;
237 c = rb_entry(parent, struct chunk, rb_node);
238 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
246 if (!collision_check)
250 ret = col_check(c, i);
251 fio_sem_down(rb_lock);
261 RB_CLEAR_NODE(&c->rb_node);
263 memcpy(c->hash, i->hash, sizeof(i->hash));
264 rb_link_node(&c->rb_node, parent, p);
265 rb_insert_color(&c->rb_node, &rb_root);
267 account_unique_capacity(i->offset, unique_capacity, zc);
272 static void insert_chunks(struct item *items, unsigned int nitems,
273 uint64_t *ndupes, uint64_t *unique_capacity, struct zlib_ctrl *zc)
277 fio_sem_down(rb_lock);
279 for (i = 0; i < nitems; i++) {
284 s = sizeof(items[i].hash) / sizeof(uint32_t);
285 r = bloom_set(bloom, items[i].hash, s);
288 insert_chunk(&items[i], unique_capacity, zc);
294 static void crc_buf(void *buf, uint32_t *hash)
296 struct fio_md5_ctx ctx = { .hash = hash };
299 fio_md5_update(&ctx, buf, blocksize);
303 static unsigned int read_blocks(int fd, void *buf, off_t offset, size_t size)
305 if (__read_block(fd, buf, offset, size))
308 return size / blocksize;
311 static int do_work(struct worker_thread *thread, void *buf)
313 unsigned int nblocks, i;
317 uint64_t unique_capacity = 0;
320 offset = thread->cur_offset;
322 nblocks = read_blocks(thread->fd, buf, offset, min(thread->size, (uint64_t)chunk_size));
326 items = malloc(sizeof(*items) * nblocks);
328 for (i = 0; i < nblocks; i++) {
329 void *thisptr = buf + (i * blocksize);
331 items[i].offset = offset;
332 crc_buf(thisptr, items[i].hash);
337 insert_chunks(items, nitems, &ndupes, &unique_capacity, &thread->zc);
340 thread->items += nitems;
341 thread->dupes += ndupes;
342 thread->unique_capacity += unique_capacity;
346 static void thread_init_zlib_control(struct worker_thread *thread)
348 z_stream *stream = &thread->zc.stream;
349 stream->zalloc = Z_NULL;
350 stream->zfree = Z_NULL;
351 stream->opaque = Z_NULL;
353 if (deflateInit(stream, Z_DEFAULT_COMPRESSION) != Z_OK)
356 thread->zc.buf_in = fio_memalign(blocksize, blocksize, false);
357 thread->zc.buf_out = fio_memalign(blocksize, deflateBound(stream, blocksize), false);
360 static void *thread_fn(void *data)
362 struct worker_thread *thread = data;
365 buf = fio_memalign(blocksize, chunk_size, false);
367 thread_init_zlib_control(thread);
370 if (get_work(&thread->cur_offset, &thread->size)) {
374 if (do_work(thread, buf)) {
381 fio_memfree(buf, chunk_size, false);
385 static void show_progress(struct worker_thread *threads, unsigned long total)
387 unsigned long last_nitems = 0;
388 struct timespec last_tv;
390 fio_gettime(&last_tv, NULL);
392 while (print_progress) {
393 unsigned long this_items;
394 unsigned long nitems = 0;
400 for (i = 0; i < num_threads; i++) {
401 nitems += threads[i].items;
402 some_done = threads[i].done;
410 perc = (float) nitems / (float) total;
412 this_items = nitems - last_nitems;
413 this_items *= blocksize;
414 tdiff = mtime_since_now(&last_tv);
416 this_items = (this_items * 1000) / (tdiff * 1024);
417 printf("%3.2f%% done (%luKiB/sec)\r", perc, this_items);
418 last_nitems = nitems;
419 fio_gettime(&last_tv, NULL);
421 printf("%3.2f%% done\r", perc);
427 static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size,
428 uint64_t *nextents, uint64_t *nchunks, uint64_t *unique_capacity)
430 struct worker_thread *threads;
431 unsigned long nitems, total_items;
434 total_size = dev_size;
435 total_items = dev_size / blocksize;
437 size_lock = fio_sem_init(FIO_SEM_UNLOCKED);
439 threads = malloc(num_threads * sizeof(struct worker_thread));
440 for (i = 0; i < num_threads; i++) {
441 memset(&threads[i], 0, sizeof(struct worker_thread));
442 threads[i].fd = f->fd;
444 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
446 log_err("fio: thread startup failed\n");
451 show_progress(threads, total_items);
456 *unique_capacity = 0;
457 for (i = 0; i < num_threads; i++) {
459 pthread_join(threads[i].thread, &ret);
460 nitems += threads[i].items;
461 *nchunks += threads[i].dupes;
462 *unique_capacity += threads[i].unique_capacity;
465 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
468 *nchunks = nitems - *nchunks;
470 fio_sem_remove(size_lock);
475 static int dedupe_check(const char *filename, uint64_t *nextents,
476 uint64_t *nchunks, uint64_t *unique_capacity)
484 flags |= OS_O_DIRECT;
486 memset(&file, 0, sizeof(file));
487 file.file_name = strdup(filename);
489 file.fd = open(filename, flags);
495 if (fstat(file.fd, &sb) < 0) {
500 dev_size = get_size(&file, &sb);
505 uint64_t bloom_entries;
507 bloom_entries = 8 * (dev_size / blocksize);
508 bloom = bloom_new(bloom_entries);
511 printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
513 return run_dedupe_threads(&file, dev_size, nextents, nchunks, unique_capacity);
517 free(file.file_name);
521 static void show_chunk(struct chunk *c)
523 struct flist_head *n;
526 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);
527 flist_for_each(n, &c->extent_list[0]) {
528 e = flist_entry(n, struct extent, list);
529 printf("\toffset %llu\n", (unsigned long long) e->offset);
533 static const char *capacity_unit[] = {"b","KB", "MB", "GB", "TB", "PB", "EB"};
535 static uint64_t bytes_to_human_readable_unit(uint64_t n, const char **unit_out)
544 *unit_out = capacity_unit[i];
549 static void show_stat(uint64_t nextents, uint64_t nchunks, uint64_t ndupextents, uint64_t unique_capacity)
555 printf("Extents=%lu, Unique extents=%lu", (unsigned long) nextents, (unsigned long) nchunks);
557 printf(" Duplicated extents=%lu", (unsigned long) ndupextents);
561 ratio = (double) nextents / (double) nchunks;
562 printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
564 printf("De-dupe ratio: 1:infinite\n");
567 printf("De-dupe working set at least: %3.2f%%\n", 100.0 * (double) ndupextents / (double) nextents);
569 perc = 1.00 - ((double) nchunks / (double) nextents);
571 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
575 uc_human = bytes_to_human_readable_unit(unique_capacity, &unit);
576 printf("Unique capacity %lu%s\n", (unsigned long) uc_human, unit);
580 static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks, uint64_t *ndupextents)
582 struct fio_rb_node *n;
583 *nchunks = *nextents = *ndupextents = 0;
585 n = rb_first(&rb_root);
592 c = rb_entry(n, struct chunk, rb_node);
594 *nextents += c->count;
595 *ndupextents += (c->count > 1);
600 } while ((n = rb_next(n)) != NULL);
603 static int usage(char *argv[])
605 log_err("Check for dedupable blocks on a device/file\n\n");
606 log_err("%s: [options] <device or file>\n", argv[0]);
607 log_err("\t-b\tChunk size to use\n");
608 log_err("\t-t\tNumber of threads to use\n");
609 log_err("\t-d\tFull extent/chunk debug output\n");
610 log_err("\t-o\tUse O_DIRECT\n");
611 log_err("\t-c\tFull collision check\n");
612 log_err("\t-B\tUse probabilistic bloom filter\n");
613 log_err("\t-p\tPrint progress indicator\n");
614 log_err("\t-C\tCalculate compressible size\n");
618 int main(int argc, char *argv[])
620 uint64_t nextents = 0, nchunks = 0, ndupextents = 0, unique_capacity;
626 while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:C:")) != -1) {
629 blocksize = atoi(optarg);
632 num_threads = atoi(optarg);
635 dump_output = atoi(optarg);
638 odirect = atoi(optarg);
641 collision_check = atoi(optarg);
644 print_progress = atoi(optarg);
647 use_bloom = atoi(optarg);
650 compression = atoi(optarg);
658 if (collision_check || dump_output || compression)
662 num_threads = cpus_online();
670 rb_lock = fio_sem_init(FIO_SEM_UNLOCKED);
672 ret = dedupe_check(argv[optind], &nextents, &nchunks, &unique_capacity);
676 iter_rb_tree(&nextents, &nchunks, &ndupextents);
678 show_stat(nextents, nchunks, ndupextents, unique_capacity);
681 fio_sem_remove(rb_lock);