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"
29 struct timeval *fio_tv = NULL;
30 unsigned int fio_debug = 0;
32 void __dprint(int type, const char *str, ...)
36 struct worker_thread {
50 struct flist_head list;
55 struct rb_node rb_node;
56 struct flist_head extent_list;
58 uint32_t hash[MD5_HASH_WORDS];
63 uint32_t hash[MD5_HASH_WORDS];
66 static struct rb_root rb_root;
67 static struct fio_mutex *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;
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);
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.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 void insert_chunk(struct item *i)
177 struct rb_node **p, *parent;
181 p = &rb_root.rb_node;
186 c = rb_entry(parent, struct chunk, rb_node);
187 diff = memcmp(i->hash, c->hash, sizeof(i->hash));
195 if (!collision_check)
198 fio_mutex_up(rb_lock);
199 ret = col_check(c, i);
200 fio_mutex_down(rb_lock);
209 c = malloc(sizeof(*c));
210 RB_CLEAR_NODE(&c->rb_node);
211 INIT_FLIST_HEAD(&c->extent_list);
213 memcpy(c->hash, i->hash, sizeof(i->hash));
214 rb_link_node(&c->rb_node, parent, p);
215 rb_insert_color(&c->rb_node, &rb_root);
220 static void insert_chunks(struct item *items, unsigned int nitems)
224 fio_mutex_down(rb_lock);
226 for (i = 0; i < nitems; i++)
227 insert_chunk(&items[i]);
229 fio_mutex_up(rb_lock);
232 static void crc_buf(void *buf, uint32_t *hash)
234 struct fio_md5_ctx ctx = { .hash = hash };
237 fio_md5_update(&ctx, buf, blocksize);
241 static int do_work(struct worker_thread *thread, void *buf)
243 unsigned int nblocks, i;
245 int err = 0, nitems = 0;
248 nblocks = thread->size / blocksize;
249 offset = thread->cur_offset;
250 items = malloc(sizeof(*items) * nblocks);
252 for (i = 0; i < nblocks; i++) {
253 if (read_block(thread->fd, buf, offset))
255 items[i].offset = offset;
256 crc_buf(buf, items[i].hash);
261 insert_chunks(items, nitems);
262 thread->items += nitems;
267 static void *thread_fn(void *data)
269 struct worker_thread *thread = data;
272 buf = fio_memalign(blocksize, blocksize);
275 if (get_work(&thread->cur_offset, &thread->size)) {
279 if (do_work(thread, buf)) {
286 fio_memfree(buf, blocksize);
290 static int __dedupe_check(int fd, uint64_t dev_size)
292 struct worker_thread *threads;
293 unsigned long nitems, total_items;
296 total_size = dev_size;
297 total_items = dev_size / blocksize;
299 size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
301 threads = malloc(num_threads * sizeof(struct worker_thread));
302 for (i = 0; i < num_threads; i++) {
304 threads[i].items = 0;
308 err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
310 log_err("fio: thread startup failed\n");
315 while (print_progress) {
320 for (i = 0; i < num_threads; i++) {
321 nitems += threads[i].items;
322 some_done = threads[i].done;
330 perc = (float) nitems / (float) total_items;
332 printf("%3.2f%% done\r", perc);
338 for (i = 0; i < num_threads; i++) {
340 pthread_join(threads[i].thread, &ret);
341 nitems += threads[i].items;
344 printf("Threads(%u): %lu items processed\n", num_threads, nitems);
346 fio_mutex_remove(size_lock);
350 static int dedupe_check(const char *filename)
360 dev_fd = open(filename, flags);
366 if (fstat(dev_fd, &sb) < 0) {
372 dev_size = get_size(dev_fd, &sb);
378 printf("Will check <%s>, size <%llu>\n", filename, (unsigned long long) dev_size);
380 return __dedupe_check(dev_fd, dev_size);
383 static void show_chunk(struct chunk *c)
385 struct flist_head *n;
388 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);
389 flist_for_each(n, &c->extent_list) {
390 e = flist_entry(n, struct extent, list);
391 printf("\toffset %llu\n", (unsigned long long) e->offset);
395 static void iter_rb_tree(void)
402 nchunks = nextents = 0;
404 n = rb_first(&rb_root);
411 c = rb_entry(n, struct chunk, rb_node);
413 nextents += c->count;
418 } while ((n = rb_next(n)) != NULL);
420 printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
421 printf("De-dupe factor: %3.2f\n", (double) nextents / (double) nchunks);
423 perc = 1.00 - ((double) nchunks / (double) nextents);
425 printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
428 static int usage(char *argv[])
430 log_err("Check for dedupable blocks on a device/file\n\n");
431 log_err("%s: [options] <device or file>\n", argv[0]);
432 log_err("\t-b\tChunk size to use\n");
433 log_err("\t-t\tNumber of threads to use\n");
434 log_err("\t-d\tFull extent/chunk debug output\n");
435 log_err("\t-o\tUse O_DIRECT\n");
436 log_err("\t-c\tFull collision check\n");
437 log_err("\t-p\tPrint progress indicator\n");
441 int main(int argc, char *argv[])
445 while ((c = getopt(argc, argv, "b:t:d:o:c:p:")) != -1) {
448 blocksize = atoi(optarg);
451 num_threads = atoi(optarg);
454 dump_output = atoi(optarg);
457 odirect = atoi(optarg);
460 collision_check = atoi(optarg);
463 print_progress = atoi(optarg);
472 num_threads = cpus_online();
480 rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED);
482 ret = dedupe_check(argv[optind]);