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"
31 struct timeval *fio_tv = NULL;
32 unsigned int fio_debug = 0;
34 void __dprint(int type, const char *str, ...)
38 struct worker_thread {
52 struct flist_head list;
57 struct rb_node rb_node;
59 uint32_t hash[MD5_HASH_WORDS];
60 struct flist_head extent_list[0];
65 uint32_t hash[MD5_HASH_WORDS];
68 static struct rb_root rb_root;
69 static struct fio_mutex *rb_lock;
71 static unsigned int blocksize = 4096;
72 static unsigned int num_threads;
73 static unsigned int chunk_size = 1048576;
74 static unsigned int dump_output;
75 static unsigned int odirect;
76 static unsigned int collision_check;
77 static unsigned int print_progress = 1;
79 static uint64_t total_size;
80 static uint64_t cur_offset;
81 static struct fio_mutex *size_lock;
85 static uint64_t get_size(int fd, struct stat *sb)
89 if (S_ISBLK(sb->st_mode)) {
90 if (ioctl(fd, BLKGETSIZE64, &ret) < 0) {
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)
123 ret = pread(fd, buf, blocksize, offset);
129 else if (ret != blocksize) {
130 log_err("dedupe: short read on block\n");
137 static void add_item(struct chunk *c, struct item *i)
140 * Save some memory and don't add extent items, if we don't
143 if (dump_output || collision_check) {
146 e = malloc(sizeof(*e));
147 e->offset = i->offset;
148 flist_add_tail(&e->list, &c->extent_list[0]);
154 static int col_check(struct chunk *c, struct item *i)
160 cbuf = fio_memalign(blocksize, blocksize);
161 ibuf = fio_memalign(blocksize, blocksize);
163 e = flist_entry(c->extent_list[0].next, struct extent, list);
164 if (read_block(dev_fd, cbuf, e->offset))
167 if (read_block(dev_fd, ibuf, i->offset))
170 ret = memcmp(ibuf, cbuf, blocksize);
172 fio_memfree(cbuf, blocksize);
173 fio_memfree(ibuf, blocksize);
177 static struct chunk *alloc_chunk(void)
181 if (collision_check || dump_output) {
182 c = malloc(sizeof(struct chunk) + sizeof(struct flist_head));
183 INIT_FLIST_HEAD(&c->extent_list[0]);
185 c = malloc(sizeof(struct chunk));
190 static void insert_chunk(struct item *i)
192 struct rb_node **p, *parent;
196 p = &rb_root.rb_node;
201 c = rb_entry(parent, struct chunk, rb_node);
202 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
210 if (!collision_check)
213 fio_mutex_up(rb_lock);
214 ret = col_check(c, i);
215 fio_mutex_down(rb_lock);
225 RB_CLEAR_NODE(&c->rb_node);
227 memcpy(c->hash, i->hash, sizeof(i->hash));
228 rb_link_node(&c->rb_node, parent, p);
229 rb_insert_color(&c->rb_node, &rb_root);
234 static void insert_chunks(struct item *items, unsigned int nitems)
238 fio_mutex_down(rb_lock);
240 for (i = 0; i < nitems; i++)
241 insert_chunk(&items[i]);
243 fio_mutex_up(rb_lock);
246 static void crc_buf(void *buf, uint32_t *hash)
248 struct fio_md5_ctx ctx = { .hash = hash };
251 fio_md5_update(&ctx, buf, blocksize);
255 static int do_work(struct worker_thread *thread, void *buf)
257 unsigned int nblocks, i;
259 int err = 0, nitems = 0;
262 nblocks = thread->size / blocksize;
263 offset = thread->cur_offset;
264 items = malloc(sizeof(*items) * nblocks);
266 for (i = 0; i < nblocks; i++) {
267 if (read_block(thread->fd, buf, offset))
269 items[i].offset = offset;
270 crc_buf(buf, items[i].hash);
275 insert_chunks(items, nitems);
276 thread->items += nitems;
281 static void *thread_fn(void *data)
283 struct worker_thread *thread = data;
286 buf = fio_memalign(blocksize, blocksize);
289 if (get_work(&thread->cur_offset, &thread->size)) {
293 if (do_work(thread, buf)) {
300 fio_memfree(buf, blocksize);
304 static void show_progress(struct worker_thread *threads, unsigned long total)
306 unsigned long last_nitems = 0;
307 struct timeval last_tv;
309 fio_gettime(&last_tv, NULL);
311 while (print_progress) {
312 unsigned long this_items;
313 unsigned long nitems = 0;
319 for (i = 0; i < num_threads; i++) {
320 nitems += threads[i].items;
321 some_done = threads[i].done;
329 perc = (float) nitems / (float) total;
331 this_items = nitems - last_nitems;
332 this_items *= blocksize;
333 tdiff = mtime_since_now(&last_tv);
336 printf("%3.2f%% done (%luKB/sec)\r", perc, this_items);
337 last_nitems = nitems;
338 fio_gettime(&last_tv, NULL);
340 printf("%3.2f%% done\r", perc);
346 static int run_dedupe_threads(int fd, uint64_t dev_size)
348 struct worker_thread *threads;
349 unsigned long nitems, total_items;
352 total_size = dev_size;
353 total_items = dev_size / blocksize;
355 size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
357 threads = malloc(num_threads * sizeof(struct worker_thread));
358 for (i = 0; i < num_threads; i++) {
360 threads[i].items = 0;
364 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
366 log_err("fio: thread startup failed\n");
371 show_progress(threads, total_items);
374 for (i = 0; i < num_threads; i++) {
376 pthread_join(threads[i].thread, &ret);
377 nitems += threads[i].items;
380 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
382 fio_mutex_remove(size_lock);
387 static int dedupe_check(const char *filename)
397 dev_fd = open(filename, flags);
403 if (fstat(dev_fd, &sb) < 0) {
409 dev_size = get_size(dev_fd, &sb);
415 printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
417 return run_dedupe_threads(dev_fd, dev_size);
420 static void show_chunk(struct chunk *c)
422 struct flist_head *n;
425 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);
426 flist_for_each(n, &c->extent_list[0]) {
427 e = flist_entry(n, struct extent, list);
428 printf("\toffset %llu\n", (unsigned long long) e->offset);
432 static void iter_rb_tree(void)
439 nchunks = nextents = 0;
441 n = rb_first(&rb_root);
448 c = rb_entry(n, struct chunk, rb_node);
450 nextents += c->count;
455 } while ((n = rb_next(n)) != NULL);
457 printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
458 printf("De-dupe factor: %3.2f\n", (double) nextents / (double) nchunks);
460 perc = 1.00 - ((double) nchunks / (double) nextents);
462 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
465 static int usage(char *argv[])
467 log_err("Check for dedupable blocks on a device/file\n\n");
468 log_err("%s: [options] <device or file>\n", argv[0]);
469 log_err("\t-b\tChunk size to use\n");
470 log_err("\t-t\tNumber of threads to use\n");
471 log_err("\t-d\tFull extent/chunk debug output\n");
472 log_err("\t-o\tUse O_DIRECT\n");
473 log_err("\t-c\tFull collision check\n");
474 log_err("\t-p\tPrint progress indicator\n");
478 int main(int argc, char *argv[])
482 while ((c = getopt(argc, argv, "b:t:d:o:c:p:")) != -1) {
485 blocksize = atoi(optarg);
488 num_threads = atoi(optarg);
491 dump_output = atoi(optarg);
494 odirect = atoi(optarg);
497 collision_check = atoi(optarg);
500 print_progress = atoi(optarg);
509 num_threads = cpus_online();
517 rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
519 ret = dedupe_check(argv[optind]);
523 fio_mutex_remove(rb_lock);