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;
83 static uint64_t get_size(int fd, struct stat *sb)
87 if (S_ISBLK(sb->st_mode)) {
88 if (ioctl(fd, BLKGETSIZE64, &ret) < 0) {
95 return (ret & ~((uint64_t)blocksize - 1));
98 static int get_work(uint64_t *offset, uint64_t *size)
103 fio_mutex_down(size_lock);
105 if (cur_offset < total_size) {
106 *offset = cur_offset;
107 this_chunk = min((uint64_t)chunk_size, total_size - cur_offset);
109 cur_offset += this_chunk;
113 fio_mutex_up(size_lock);
117 static int read_block(int fd, void *buf, off_t offset)
121 ret = pread(fd, buf, blocksize, offset);
127 else if (ret != blocksize) {
128 log_err("dedupe: short read on block\n");
135 static void add_item(struct chunk *c, struct item *i)
138 * Save some memory and don't add extent items, if we don't
141 if (dump_output || collision_check) {
144 e = malloc(sizeof(*e));
145 e->offset = i->offset;
146 flist_add_tail(&e->list, &c->extent_list[0]);
152 static int col_check(struct chunk *c, struct item *i)
158 cbuf = fio_memalign(blocksize, blocksize);
159 ibuf = fio_memalign(blocksize, blocksize);
161 e = flist_entry(c->extent_list[0].next, struct extent, list);
162 if (read_block(dev_fd, cbuf, e->offset))
165 if (read_block(dev_fd, ibuf, i->offset))
168 ret = memcmp(ibuf, cbuf, blocksize);
170 fio_memfree(cbuf, blocksize);
171 fio_memfree(ibuf, blocksize);
175 static struct chunk *alloc_chunk(void)
179 if (collision_check || dump_output) {
180 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
181 INIT_FLIST_HEAD(&c->extent_list[0]);
183 c = malloc(sizeof(struct chunk));
188 static void insert_chunk(struct item *i)
190 struct rb_node **p, *parent;
194 p = &rb_root.rb_node;
199 c = rb_entry(parent, struct chunk, rb_node);
200 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
208 if (!collision_check)
211 fio_mutex_up(rb_lock);
212 ret = col_check(c, i);
213 fio_mutex_down(rb_lock);
223 RB_CLEAR_NODE(&c->rb_node);
225 memcpy(c->hash, i->hash, sizeof(i->hash));
226 rb_link_node(&c->rb_node, parent, p);
227 rb_insert_color(&c->rb_node, &rb_root);
232 static void insert_chunks(struct item *items, unsigned int nitems,
237 fio_mutex_down(rb_lock);
239 for (i = 0; i < nitems; i++) {
244 s = sizeof(items[i].hash) / sizeof(uint32_t);
245 r = bloom_set(bloom, items[i].hash, s);
248 insert_chunk(&items[i]);
251 fio_mutex_up(rb_lock);
254 static void crc_buf(void *buf, uint32_t *hash)
256 struct fio_md5_ctx ctx = { .hash = hash };
259 fio_md5_update(&ctx, buf, blocksize);
263 static int do_work(struct worker_thread *thread, void *buf)
265 unsigned int nblocks, i;
267 int err = 0, nitems = 0;
271 nblocks = thread->size / blocksize;
272 offset = thread->cur_offset;
273 items = malloc(sizeof(*items) * nblocks);
275 for (i = 0; i < nblocks; i++) {
276 if (read_block(thread->fd, buf, offset))
279 items[i].offset = offset;
280 crc_buf(buf, items[i].hash);
285 insert_chunks(items, nitems, &ndupes);
288 thread->items += nitems;
289 thread->dupes += ndupes;
293 static void *thread_fn(void *data)
295 struct worker_thread *thread = data;
298 buf = fio_memalign(blocksize, blocksize);
301 if (get_work(&thread->cur_offset, &thread->size)) {
305 if (do_work(thread, buf)) {
312 fio_memfree(buf, blocksize);
316 static void show_progress(struct worker_thread *threads, unsigned long total)
318 unsigned long last_nitems = 0;
319 struct timeval last_tv;
321 fio_gettime(&last_tv, NULL);
323 while (print_progress) {
324 unsigned long this_items;
325 unsigned long nitems = 0;
331 for (i = 0; i < num_threads; i++) {
332 nitems += threads[i].items;
333 some_done = threads[i].done;
341 perc = (float) nitems / (float) total;
343 this_items = nitems - last_nitems;
344 this_items *= blocksize;
345 tdiff = mtime_since_now(&last_tv);
348 printf("%3.2f%% done (%luKB/sec)\r", perc, this_items);
349 last_nitems = nitems;
350 fio_gettime(&last_tv, NULL);
352 printf("%3.2f%% done\r", perc);
358 static int run_dedupe_threads(int fd, uint64_t dev_size, uint64_t *nextents,
361 struct worker_thread *threads;
362 unsigned long nitems, total_items;
365 total_size = dev_size;
366 total_items = dev_size / blocksize;
368 size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
370 threads = malloc(num_threads * sizeof(struct worker_thread));
371 for (i = 0; i < num_threads; i++) {
373 threads[i].items = 0;
377 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
379 log_err("fio: thread startup failed\n");
384 show_progress(threads, total_items);
389 for (i = 0; i < num_threads; i++) {
391 pthread_join(threads[i].thread, &ret);
392 nitems += threads[i].items;
393 *nchunks += threads[i].dupes;
396 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
399 *nchunks = nitems - *nchunks;
401 fio_mutex_remove(size_lock);
406 static int dedupe_check(const char *filename, uint64_t *nextents,
417 dev_fd = open(filename, flags);
423 if (fstat(dev_fd, &sb) < 0) {
429 dev_size = get_size(dev_fd, &sb);
436 uint64_t bloom_entries;
438 bloom_entries = (3 * dev_size ) / (blocksize * 2);
439 bloom = bloom_new(bloom_entries);
442 printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
444 return run_dedupe_threads(dev_fd, dev_size, nextents, nchunks);
447 static void show_chunk(struct chunk *c)
449 struct flist_head *n;
452 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);
453 flist_for_each(n, &c->extent_list[0]) {
454 e = flist_entry(n, struct extent, list);
455 printf("\toffset %llu\n", (unsigned long long) e->offset);
459 static void show_stat(uint64_t nextents, uint64_t nchunks)
463 printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
464 printf("De-dupe factor: %3.2f\n", (double) nextents / (double) nchunks);
466 perc = 1.00 - ((double) nchunks / (double) nextents);
468 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
472 static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks)
476 *nchunks = *nextents = 0;
478 n = rb_first(&rb_root);
485 c = rb_entry(n, struct chunk, rb_node);
487 *nextents += c->count;
492 } while ((n = rb_next(n)) != NULL);
495 static int usage(char *argv[])
497 log_err("Check for dedupable blocks on a device/file\n\n");
498 log_err("%s: [options] <device or file>\n", argv[0]);
499 log_err("\t-b\tChunk size to use\n");
500 log_err("\t-t\tNumber of threads to use\n");
501 log_err("\t-d\tFull extent/chunk debug output\n");
502 log_err("\t-o\tUse O_DIRECT\n");
503 log_err("\t-c\tFull collision check\n");
504 log_err("\t-B\tUse probabilistic bloom filter\n");
505 log_err("\t-p\tPrint progress indicator\n");
509 int main(int argc, char *argv[])
511 uint64_t nextents = 0, nchunks = 0;
516 while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) {
519 blocksize = atoi(optarg);
522 num_threads = atoi(optarg);
525 dump_output = atoi(optarg);
528 odirect = atoi(optarg);
531 collision_check = atoi(optarg);
534 print_progress = atoi(optarg);
537 use_bloom = atoi(optarg);
545 if (collision_check || dump_output)
549 num_threads = cpus_online();
557 rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
559 ret = dedupe_check(argv[optind], &nextents, &nchunks);
562 iter_rb_tree(&nextents, &nchunks);
564 show_stat(nextents, nchunks);
566 fio_mutex_remove(rb_lock);