Commit | Line | Data |
---|---|---|
bf481692 JA |
1 | /* |
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, | |
4 | * and orders them up. | |
5 | */ | |
6 | #include <stdio.h> | |
7 | #include <stdio.h> | |
8 | #include <unistd.h> | |
9 | #include <inttypes.h> | |
10 | #include <assert.h> | |
11 | #include <sys/types.h> | |
12 | #include <sys/stat.h> | |
13 | #include <sys/ioctl.h> | |
bf481692 JA |
14 | #include <fcntl.h> |
15 | #include <string.h> | |
16 | ||
17 | #include "../lib/rbtree.h" | |
18 | #include "../flist.h" | |
19 | #include "../log.h" | |
20 | #include "../mutex.h" | |
21 | #include "../smalloc.h" | |
22 | #include "../minmax.h" | |
23 | #include "../crc/md5.h" | |
24 | #include "../memalign.h" | |
25 | #include "../os/os.h" | |
5a155943 JA |
26 | #include "../gettime.h" |
27 | #include "../fio_time.h" | |
bf481692 | 28 | |
fa88cd09 | 29 | #include "../lib/bloom.h" |
9077ee4a | 30 | #include "debug.h" |
bf481692 JA |
31 | |
32 | struct worker_thread { | |
33 | pthread_t thread; | |
34 | ||
4d53d6c0 JA |
35 | volatile int done; |
36 | ||
bf481692 JA |
37 | int fd; |
38 | uint64_t cur_offset; | |
39 | uint64_t size; | |
40 | ||
41 | unsigned long items; | |
fa88cd09 | 42 | unsigned long dupes; |
bf481692 JA |
43 | int err; |
44 | }; | |
45 | ||
46 | struct extent { | |
47 | struct flist_head list; | |
48 | uint64_t offset; | |
49 | }; | |
50 | ||
51 | struct chunk { | |
52 | struct rb_node rb_node; | |
bf481692 JA |
53 | uint64_t count; |
54 | uint32_t hash[MD5_HASH_WORDS]; | |
5a155943 | 55 | struct flist_head extent_list[0]; |
bf481692 JA |
56 | }; |
57 | ||
58 | struct item { | |
59 | uint64_t offset; | |
60 | uint32_t hash[MD5_HASH_WORDS]; | |
61 | }; | |
62 | ||
63 | static struct rb_root rb_root; | |
fa88cd09 | 64 | static struct bloom *bloom; |
bf481692 JA |
65 | static struct fio_mutex *rb_lock; |
66 | ||
67 | static unsigned int blocksize = 4096; | |
68 | static unsigned int num_threads; | |
69 | static unsigned int chunk_size = 1048576; | |
70 | static unsigned int dump_output; | |
71 | static unsigned int odirect; | |
72 | static unsigned int collision_check; | |
4d53d6c0 | 73 | static unsigned int print_progress = 1; |
fa88cd09 | 74 | static unsigned int use_bloom = 1; |
bf481692 JA |
75 | |
76 | static uint64_t total_size; | |
77 | static uint64_t cur_offset; | |
78 | static struct fio_mutex *size_lock; | |
79 | ||
d08a6886 | 80 | static struct fio_file file; |
bf481692 | 81 | |
d08a6886 | 82 | static uint64_t get_size(struct fio_file *f, struct stat *sb) |
bf481692 JA |
83 | { |
84 | uint64_t ret; | |
85 | ||
86 | if (S_ISBLK(sb->st_mode)) { | |
d08a6886 JA |
87 | unsigned long long bytes; |
88 | ||
89 | if (blockdev_size(f, &bytes)) { | |
90 | log_err("dedupe: failed getting bdev size\n"); | |
bf481692 JA |
91 | return 0; |
92 | } | |
d08a6886 | 93 | ret = bytes; |
bf481692 JA |
94 | } else |
95 | ret = sb->st_size; | |
96 | ||
97 | return (ret & ~((uint64_t)blocksize - 1)); | |
98 | } | |
99 | ||
100 | static int get_work(uint64_t *offset, uint64_t *size) | |
101 | { | |
102 | uint64_t this_chunk; | |
103 | int ret = 1; | |
104 | ||
105 | fio_mutex_down(size_lock); | |
106 | ||
107 | if (cur_offset < total_size) { | |
108 | *offset = cur_offset; | |
109 | this_chunk = min((uint64_t)chunk_size, total_size - cur_offset); | |
110 | *size = this_chunk; | |
111 | cur_offset += this_chunk; | |
112 | ret = 0; | |
113 | } | |
114 | ||
115 | fio_mutex_up(size_lock); | |
116 | return ret; | |
117 | } | |
118 | ||
13793d02 | 119 | static int __read_block(int fd, void *buf, off_t offset, size_t count) |
bf481692 JA |
120 | { |
121 | ssize_t ret; | |
122 | ||
13793d02 | 123 | ret = pread(fd, buf, count, offset); |
bf481692 JA |
124 | if (ret < 0) { |
125 | perror("pread"); | |
126 | return 1; | |
127 | } else if (!ret) | |
128 | return 1; | |
13793d02 | 129 | else if (ret != count) { |
bf481692 JA |
130 | log_err("dedupe: short read on block\n"); |
131 | return 1; | |
132 | } | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
13793d02 JA |
137 | static int read_block(int fd, void *buf, off_t offset) |
138 | { | |
139 | return __read_block(fd, buf, offset, blocksize); | |
140 | } | |
141 | ||
bf481692 JA |
142 | static void add_item(struct chunk *c, struct item *i) |
143 | { | |
87818659 JA |
144 | /* |
145 | * Save some memory and don't add extent items, if we don't | |
146 | * use them. | |
147 | */ | |
148 | if (dump_output || collision_check) { | |
149 | struct extent *e; | |
150 | ||
151 | e = malloc(sizeof(*e)); | |
152 | e->offset = i->offset; | |
5a155943 | 153 | flist_add_tail(&e->list, &c->extent_list[0]); |
87818659 | 154 | } |
bf481692 | 155 | |
bf481692 JA |
156 | c->count++; |
157 | } | |
158 | ||
159 | static int col_check(struct chunk *c, struct item *i) | |
160 | { | |
161 | struct extent *e; | |
162 | char *cbuf, *ibuf; | |
163 | int ret = 1; | |
164 | ||
165 | cbuf = fio_memalign(blocksize, blocksize); | |
166 | ibuf = fio_memalign(blocksize, blocksize); | |
167 | ||
5a155943 | 168 | e = flist_entry(c->extent_list[0].next, struct extent, list); |
d08a6886 | 169 | if (read_block(file.fd, cbuf, e->offset)) |
bf481692 JA |
170 | goto out; |
171 | ||
d08a6886 | 172 | if (read_block(file.fd, ibuf, i->offset)) |
bf481692 JA |
173 | goto out; |
174 | ||
175 | ret = memcmp(ibuf, cbuf, blocksize); | |
176 | out: | |
177 | fio_memfree(cbuf, blocksize); | |
178 | fio_memfree(ibuf, blocksize); | |
179 | return ret; | |
180 | } | |
181 | ||
5a155943 JA |
182 | static struct chunk *alloc_chunk(void) |
183 | { | |
184 | struct chunk *c; | |
185 | ||
186 | if (collision_check || dump_output) { | |
187 | c = malloc(sizeof(struct chunk) + sizeof(struct flist_head)); | |
188 | INIT_FLIST_HEAD(&c->extent_list[0]); | |
189 | } else | |
190 | c = malloc(sizeof(struct chunk)); | |
191 | ||
192 | return c; | |
193 | } | |
194 | ||
bf481692 JA |
195 | static void insert_chunk(struct item *i) |
196 | { | |
197 | struct rb_node **p, *parent; | |
198 | struct chunk *c; | |
199 | int diff; | |
200 | ||
201 | p = &rb_root.rb_node; | |
202 | parent = NULL; | |
203 | while (*p) { | |
204 | parent = *p; | |
205 | ||
206 | c = rb_entry(parent, struct chunk, rb_node); | |
207 | diff = memcmp(i->hash, c->hash, sizeof(i->hash)); | |
208 | if (diff < 0) | |
209 | p = &(*p)->rb_left; | |
210 | else if (diff > 0) | |
211 | p = &(*p)->rb_right; | |
212 | else { | |
213 | int ret; | |
214 | ||
215 | if (!collision_check) | |
216 | goto add; | |
217 | ||
218 | fio_mutex_up(rb_lock); | |
219 | ret = col_check(c, i); | |
220 | fio_mutex_down(rb_lock); | |
221 | ||
222 | if (!ret) | |
223 | goto add; | |
224 | ||
225 | p = &(*p)->rb_right; | |
226 | } | |
227 | } | |
228 | ||
5a155943 | 229 | c = alloc_chunk(); |
bf481692 | 230 | RB_CLEAR_NODE(&c->rb_node); |
bf481692 JA |
231 | c->count = 0; |
232 | memcpy(c->hash, i->hash, sizeof(i->hash)); | |
233 | rb_link_node(&c->rb_node, parent, p); | |
234 | rb_insert_color(&c->rb_node, &rb_root); | |
235 | add: | |
236 | add_item(c, i); | |
237 | } | |
238 | ||
fa88cd09 JA |
239 | static void insert_chunks(struct item *items, unsigned int nitems, |
240 | uint64_t *ndupes) | |
bf481692 JA |
241 | { |
242 | int i; | |
243 | ||
244 | fio_mutex_down(rb_lock); | |
245 | ||
fa88cd09 JA |
246 | for (i = 0; i < nitems; i++) { |
247 | if (bloom) { | |
248 | unsigned int s; | |
249 | int r; | |
250 | ||
251 | s = sizeof(items[i].hash) / sizeof(uint32_t); | |
252 | r = bloom_set(bloom, items[i].hash, s); | |
253 | *ndupes += r; | |
254 | } else | |
255 | insert_chunk(&items[i]); | |
256 | } | |
bf481692 JA |
257 | |
258 | fio_mutex_up(rb_lock); | |
259 | } | |
260 | ||
261 | static void crc_buf(void *buf, uint32_t *hash) | |
262 | { | |
263 | struct fio_md5_ctx ctx = { .hash = hash }; | |
264 | ||
265 | fio_md5_init(&ctx); | |
266 | fio_md5_update(&ctx, buf, blocksize); | |
267 | fio_md5_final(&ctx); | |
268 | } | |
269 | ||
13793d02 JA |
270 | static unsigned int read_blocks(int fd, void *buf, off_t offset, size_t size) |
271 | { | |
272 | if (__read_block(fd, buf, offset, size)) | |
273 | return 0; | |
274 | ||
275 | return size / blocksize; | |
276 | } | |
277 | ||
bf481692 JA |
278 | static int do_work(struct worker_thread *thread, void *buf) |
279 | { | |
280 | unsigned int nblocks, i; | |
281 | off_t offset; | |
13793d02 | 282 | int nitems = 0; |
fa88cd09 | 283 | uint64_t ndupes = 0; |
bf481692 JA |
284 | struct item *items; |
285 | ||
bf481692 | 286 | offset = thread->cur_offset; |
13793d02 JA |
287 | |
288 | nblocks = read_blocks(thread->fd, buf, offset, min(thread->size, (uint64_t)chunk_size)); | |
289 | if (!nblocks) | |
290 | return 1; | |
291 | ||
bf481692 JA |
292 | items = malloc(sizeof(*items) * nblocks); |
293 | ||
294 | for (i = 0; i < nblocks; i++) { | |
13793d02 JA |
295 | void *thisptr = buf + (i * blocksize); |
296 | ||
fa88cd09 JA |
297 | if (items) |
298 | items[i].offset = offset; | |
13793d02 | 299 | crc_buf(thisptr, items[i].hash); |
bf481692 JA |
300 | offset += blocksize; |
301 | nitems++; | |
302 | } | |
303 | ||
fa88cd09 JA |
304 | insert_chunks(items, nitems, &ndupes); |
305 | ||
bf481692 | 306 | free(items); |
fa88cd09 JA |
307 | thread->items += nitems; |
308 | thread->dupes += ndupes; | |
13793d02 | 309 | return 0; |
bf481692 JA |
310 | } |
311 | ||
312 | static void *thread_fn(void *data) | |
313 | { | |
314 | struct worker_thread *thread = data; | |
315 | void *buf; | |
316 | ||
13793d02 | 317 | buf = fio_memalign(blocksize, chunk_size); |
bf481692 JA |
318 | |
319 | do { | |
320 | if (get_work(&thread->cur_offset, &thread->size)) { | |
321 | thread->err = 1; | |
322 | break; | |
323 | } | |
324 | if (do_work(thread, buf)) { | |
325 | thread->err = 1; | |
326 | break; | |
327 | } | |
328 | } while (1); | |
329 | ||
4d53d6c0 | 330 | thread->done = 1; |
13793d02 | 331 | fio_memfree(buf, chunk_size); |
bf481692 JA |
332 | return NULL; |
333 | } | |
334 | ||
5a155943 JA |
335 | static void show_progress(struct worker_thread *threads, unsigned long total) |
336 | { | |
337 | unsigned long last_nitems = 0; | |
338 | struct timeval last_tv; | |
339 | ||
340 | fio_gettime(&last_tv, NULL); | |
341 | ||
342 | while (print_progress) { | |
343 | unsigned long this_items; | |
344 | unsigned long nitems = 0; | |
345 | uint64_t tdiff; | |
346 | float perc; | |
423adcc7 | 347 | int some_done = 0; |
5a155943 JA |
348 | int i; |
349 | ||
350 | for (i = 0; i < num_threads; i++) { | |
351 | nitems += threads[i].items; | |
352 | some_done = threads[i].done; | |
353 | if (some_done) | |
354 | break; | |
355 | } | |
356 | ||
357 | if (some_done) | |
358 | break; | |
359 | ||
360 | perc = (float) nitems / (float) total; | |
361 | perc *= 100.0; | |
362 | this_items = nitems - last_nitems; | |
363 | this_items *= blocksize; | |
364 | tdiff = mtime_since_now(&last_tv); | |
365 | if (tdiff) { | |
45b56027 | 366 | this_items = (this_items * 1000) / (tdiff * 1024); |
5a155943 JA |
367 | printf("%3.2f%% done (%luKB/sec)\r", perc, this_items); |
368 | last_nitems = nitems; | |
369 | fio_gettime(&last_tv, NULL); | |
370 | } else | |
371 | printf("%3.2f%% done\r", perc); | |
372 | fflush(stdout); | |
373 | usleep(250000); | |
374 | }; | |
375 | } | |
376 | ||
d08a6886 JA |
377 | static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size, |
378 | uint64_t *nextents, uint64_t *nchunks) | |
bf481692 JA |
379 | { |
380 | struct worker_thread *threads; | |
4d53d6c0 | 381 | unsigned long nitems, total_items; |
bf481692 JA |
382 | int i, err = 0; |
383 | ||
384 | total_size = dev_size; | |
4d53d6c0 | 385 | total_items = dev_size / blocksize; |
bf481692 JA |
386 | cur_offset = 0; |
387 | size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); | |
388 | ||
389 | threads = malloc(num_threads * sizeof(struct worker_thread)); | |
390 | for (i = 0; i < num_threads; i++) { | |
d08a6886 | 391 | threads[i].fd = f->fd; |
bf481692 JA |
392 | threads[i].items = 0; |
393 | threads[i].err = 0; | |
4d53d6c0 | 394 | threads[i].done = 0; |
bf481692 JA |
395 | |
396 | err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]); | |
397 | if (err) { | |
398 | log_err("fio: thread startup failed\n"); | |
399 | break; | |
400 | } | |
401 | } | |
402 | ||
5a155943 | 403 | show_progress(threads, total_items); |
4d53d6c0 | 404 | |
bf481692 | 405 | nitems = 0; |
fa88cd09 JA |
406 | *nextents = 0; |
407 | *nchunks = 1; | |
bf481692 JA |
408 | for (i = 0; i < num_threads; i++) { |
409 | void *ret; | |
410 | pthread_join(threads[i].thread, &ret); | |
411 | nitems += threads[i].items; | |
fa88cd09 | 412 | *nchunks += threads[i].dupes; |
bf481692 JA |
413 | } |
414 | ||
415 | printf("Threads(%u): %lu items processed\n", num_threads, nitems); | |
416 | ||
fa88cd09 JA |
417 | *nextents = nitems; |
418 | *nchunks = nitems - *nchunks; | |
419 | ||
bf481692 | 420 | fio_mutex_remove(size_lock); |
5a155943 | 421 | free(threads); |
bf481692 JA |
422 | return err; |
423 | } | |
424 | ||
fa88cd09 JA |
425 | static int dedupe_check(const char *filename, uint64_t *nextents, |
426 | uint64_t *nchunks) | |
bf481692 JA |
427 | { |
428 | uint64_t dev_size; | |
429 | struct stat sb; | |
430 | int flags; | |
431 | ||
432 | flags = O_RDONLY; | |
433 | if (odirect) | |
a0ae50a6 | 434 | flags |= OS_O_DIRECT; |
bf481692 | 435 | |
d08a6886 JA |
436 | memset(&file, 0, sizeof(file)); |
437 | file.file_name = strdup(filename); | |
438 | ||
439 | file.fd = open(filename, flags); | |
440 | if (file.fd == -1) { | |
bf481692 | 441 | perror("open"); |
d08a6886 | 442 | goto err; |
bf481692 JA |
443 | } |
444 | ||
d08a6886 | 445 | if (fstat(file.fd, &sb) < 0) { |
bf481692 | 446 | perror("fstat"); |
d08a6886 | 447 | goto err; |
bf481692 JA |
448 | } |
449 | ||
d08a6886 JA |
450 | dev_size = get_size(&file, &sb); |
451 | if (!dev_size) | |
452 | goto err; | |
bf481692 | 453 | |
fa88cd09 JA |
454 | if (use_bloom) { |
455 | uint64_t bloom_entries; | |
456 | ||
bc095aab | 457 | bloom_entries = 8 * (dev_size / blocksize); |
fa88cd09 JA |
458 | bloom = bloom_new(bloom_entries); |
459 | } | |
460 | ||
f7bd62dd | 461 | printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads); |
bf481692 | 462 | |
d08a6886 JA |
463 | return run_dedupe_threads(&file, dev_size, nextents, nchunks); |
464 | err: | |
465 | if (file.fd != -1) | |
466 | close(file.fd); | |
467 | free(file.file_name); | |
468 | return 1; | |
bf481692 JA |
469 | } |
470 | ||
471 | static void show_chunk(struct chunk *c) | |
472 | { | |
473 | struct flist_head *n; | |
474 | struct extent *e; | |
475 | ||
761c2729 | 476 | 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); |
5a155943 | 477 | flist_for_each(n, &c->extent_list[0]) { |
bf481692 | 478 | e = flist_entry(n, struct extent, list); |
761c2729 | 479 | printf("\toffset %llu\n", (unsigned long long) e->offset); |
bf481692 JA |
480 | } |
481 | } | |
482 | ||
fa88cd09 | 483 | static void show_stat(uint64_t nextents, uint64_t nchunks) |
bf481692 | 484 | { |
9ca144fe | 485 | double perc, ratio; |
bf481692 | 486 | |
fa88cd09 | 487 | printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks); |
15e3ca5b JA |
488 | |
489 | if (nchunks) { | |
490 | ratio = (double) nextents / (double) nchunks; | |
491 | printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0); | |
492 | } else | |
493 | printf("De-dupe ratio: 1:infinite\n"); | |
fa88cd09 JA |
494 | |
495 | perc = 1.00 - ((double) nchunks / (double) nextents); | |
496 | perc *= 100.0; | |
497 | printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50)); | |
498 | ||
499 | } | |
500 | ||
501 | static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks) | |
502 | { | |
503 | struct rb_node *n; | |
504 | ||
505 | *nchunks = *nextents = 0; | |
bf481692 JA |
506 | |
507 | n = rb_first(&rb_root); | |
508 | if (!n) | |
509 | return; | |
510 | ||
511 | do { | |
512 | struct chunk *c; | |
513 | ||
514 | c = rb_entry(n, struct chunk, rb_node); | |
fa88cd09 JA |
515 | (*nchunks)++; |
516 | *nextents += c->count; | |
bf481692 JA |
517 | |
518 | if (dump_output) | |
519 | show_chunk(c); | |
520 | ||
521 | } while ((n = rb_next(n)) != NULL); | |
bf481692 JA |
522 | } |
523 | ||
524 | static int usage(char *argv[]) | |
525 | { | |
526 | log_err("Check for dedupable blocks on a device/file\n\n"); | |
527 | log_err("%s: [options] <device or file>\n", argv[0]); | |
528 | log_err("\t-b\tChunk size to use\n"); | |
529 | log_err("\t-t\tNumber of threads to use\n"); | |
530 | log_err("\t-d\tFull extent/chunk debug output\n"); | |
531 | log_err("\t-o\tUse O_DIRECT\n"); | |
532 | log_err("\t-c\tFull collision check\n"); | |
fa88cd09 | 533 | log_err("\t-B\tUse probabilistic bloom filter\n"); |
4d53d6c0 | 534 | log_err("\t-p\tPrint progress indicator\n"); |
bf481692 JA |
535 | return 1; |
536 | } | |
537 | ||
538 | int main(int argc, char *argv[]) | |
539 | { | |
9b36c122 | 540 | uint64_t nextents = 0, nchunks = 0; |
bf481692 JA |
541 | int c, ret; |
542 | ||
9077ee4a JA |
543 | debug_init(); |
544 | ||
fa88cd09 | 545 | while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) { |
bf481692 JA |
546 | switch (c) { |
547 | case 'b': | |
548 | blocksize = atoi(optarg); | |
549 | break; | |
550 | case 't': | |
551 | num_threads = atoi(optarg); | |
552 | break; | |
553 | case 'd': | |
554 | dump_output = atoi(optarg); | |
555 | break; | |
556 | case 'o': | |
557 | odirect = atoi(optarg); | |
558 | break; | |
559 | case 'c': | |
560 | collision_check = atoi(optarg); | |
561 | break; | |
4d53d6c0 JA |
562 | case 'p': |
563 | print_progress = atoi(optarg); | |
564 | break; | |
fa88cd09 JA |
565 | case 'B': |
566 | use_bloom = atoi(optarg); | |
567 | break; | |
bf481692 JA |
568 | case '?': |
569 | default: | |
570 | return usage(argv); | |
571 | } | |
572 | } | |
573 | ||
fa88cd09 JA |
574 | if (collision_check || dump_output) |
575 | use_bloom = 0; | |
576 | ||
bf481692 JA |
577 | if (!num_threads) |
578 | num_threads = cpus_online(); | |
579 | ||
580 | if (argc == optind) | |
581 | return usage(argv); | |
582 | ||
583 | sinit(); | |
584 | ||
585 | rb_root = RB_ROOT; | |
586 | rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); | |
587 | ||
fa88cd09 JA |
588 | ret = dedupe_check(argv[optind], &nextents, &nchunks); |
589 | ||
179b3ead JA |
590 | if (!ret) { |
591 | if (!bloom) | |
592 | iter_rb_tree(&nextents, &nchunks); | |
bf481692 | 593 | |
179b3ead JA |
594 | show_stat(nextents, nchunks); |
595 | } | |
bf481692 | 596 | |
1956a9e5 | 597 | fio_mutex_remove(rb_lock); |
9b36c122 JA |
598 | if (bloom) |
599 | bloom_free(bloom); | |
bf481692 JA |
600 | scleanup(); |
601 | return ret; | |
602 | } |