Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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> |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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" |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 26 | #include "../gettime.h" |
| 27 | #include "../fio_time.h" |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 28 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 29 | #include "../lib/bloom.h" |
Jens Axboe | 7a74197 | 2014-09-26 09:58:29 -0600 | [diff] [blame] | 30 | #include "debug.h" |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 31 | |
| 32 | struct worker_thread { |
| 33 | pthread_t thread; |
| 34 | |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 35 | volatile int done; |
| 36 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 37 | int fd; |
| 38 | uint64_t cur_offset; |
| 39 | uint64_t size; |
| 40 | |
| 41 | unsigned long items; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 42 | unsigned long dupes; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 53 | uint64_t count; |
| 54 | uint32_t hash[MD5_HASH_WORDS]; |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 55 | struct flist_head extent_list[0]; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 64 | static struct bloom *bloom; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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; |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 73 | static unsigned int print_progress = 1; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 74 | static unsigned int use_bloom = 1; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 75 | |
| 76 | static uint64_t total_size; |
| 77 | static uint64_t cur_offset; |
| 78 | static struct fio_mutex *size_lock; |
| 79 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 80 | static struct fio_file file; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 81 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 82 | static uint64_t get_size(struct fio_file *f, struct stat *sb) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 83 | { |
| 84 | uint64_t ret; |
| 85 | |
| 86 | if (S_ISBLK(sb->st_mode)) { |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 87 | unsigned long long bytes; |
| 88 | |
| 89 | if (blockdev_size(f, &bytes)) { |
| 90 | log_err("dedupe: failed getting bdev size\n"); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 91 | return 0; |
| 92 | } |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 93 | ret = bytes; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 119 | static int __read_block(int fd, void *buf, off_t offset, size_t count) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 120 | { |
| 121 | ssize_t ret; |
| 122 | |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 123 | ret = pread(fd, buf, count, offset); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 124 | if (ret < 0) { |
| 125 | perror("pread"); |
| 126 | return 1; |
| 127 | } else if (!ret) |
| 128 | return 1; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 129 | else if (ret != count) { |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 130 | log_err("dedupe: short read on block\n"); |
| 131 | return 1; |
| 132 | } |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 137 | static int read_block(int fd, void *buf, off_t offset) |
| 138 | { |
| 139 | return __read_block(fd, buf, offset, blocksize); |
| 140 | } |
| 141 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 142 | static void add_item(struct chunk *c, struct item *i) |
| 143 | { |
Jens Axboe | 4deca36 | 2014-09-24 08:21:15 -0600 | [diff] [blame] | 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; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 150 | |
Jens Axboe | 4deca36 | 2014-09-24 08:21:15 -0600 | [diff] [blame] | 151 | e = malloc(sizeof(*e)); |
| 152 | e->offset = i->offset; |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 153 | flist_add_tail(&e->list, &c->extent_list[0]); |
Jens Axboe | 4deca36 | 2014-09-24 08:21:15 -0600 | [diff] [blame] | 154 | } |
| 155 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 168 | e = flist_entry(c->extent_list[0].next, struct extent, list); |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 169 | if (read_block(file.fd, cbuf, e->offset)) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 170 | goto out; |
| 171 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 172 | if (read_block(file.fd, ibuf, i->offset)) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 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 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 229 | c = alloc_chunk(); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 230 | RB_CLEAR_NODE(&c->rb_node); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 239 | static void insert_chunks(struct item *items, unsigned int nitems, |
| 240 | uint64_t *ndupes) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 241 | { |
| 242 | int i; |
| 243 | |
| 244 | fio_mutex_down(rb_lock); |
| 245 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 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 | } |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 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 | |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 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 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 278 | static int do_work(struct worker_thread *thread, void *buf) |
| 279 | { |
| 280 | unsigned int nblocks, i; |
| 281 | off_t offset; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 282 | int nitems = 0; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 283 | uint64_t ndupes = 0; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 284 | struct item *items; |
| 285 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 286 | offset = thread->cur_offset; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 287 | |
| 288 | nblocks = read_blocks(thread->fd, buf, offset, min(thread->size, (uint64_t)chunk_size)); |
| 289 | if (!nblocks) |
| 290 | return 1; |
| 291 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 292 | items = malloc(sizeof(*items) * nblocks); |
| 293 | |
| 294 | for (i = 0; i < nblocks; i++) { |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 295 | void *thisptr = buf + (i * blocksize); |
| 296 | |
Jens Axboe | bf6238a | 2014-10-14 19:54:21 -0600 | [diff] [blame] | 297 | items[i].offset = offset; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 298 | crc_buf(thisptr, items[i].hash); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 299 | offset += blocksize; |
| 300 | nitems++; |
| 301 | } |
| 302 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 303 | insert_chunks(items, nitems, &ndupes); |
| 304 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 305 | free(items); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 306 | thread->items += nitems; |
| 307 | thread->dupes += ndupes; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 308 | return 0; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static void *thread_fn(void *data) |
| 312 | { |
| 313 | struct worker_thread *thread = data; |
| 314 | void *buf; |
| 315 | |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 316 | buf = fio_memalign(blocksize, chunk_size); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 317 | |
| 318 | do { |
| 319 | if (get_work(&thread->cur_offset, &thread->size)) { |
| 320 | thread->err = 1; |
| 321 | break; |
| 322 | } |
| 323 | if (do_work(thread, buf)) { |
| 324 | thread->err = 1; |
| 325 | break; |
| 326 | } |
| 327 | } while (1); |
| 328 | |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 329 | thread->done = 1; |
Jens Axboe | d393bcd | 2014-09-26 12:47:14 -0600 | [diff] [blame] | 330 | fio_memfree(buf, chunk_size); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 331 | return NULL; |
| 332 | } |
| 333 | |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 334 | static void show_progress(struct worker_thread *threads, unsigned long total) |
| 335 | { |
| 336 | unsigned long last_nitems = 0; |
| 337 | struct timeval last_tv; |
| 338 | |
| 339 | fio_gettime(&last_tv, NULL); |
| 340 | |
| 341 | while (print_progress) { |
| 342 | unsigned long this_items; |
| 343 | unsigned long nitems = 0; |
| 344 | uint64_t tdiff; |
| 345 | float perc; |
Jens Axboe | cb8a41d | 2014-09-28 19:10:24 -0600 | [diff] [blame] | 346 | int some_done = 0; |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 347 | int i; |
| 348 | |
| 349 | for (i = 0; i < num_threads; i++) { |
| 350 | nitems += threads[i].items; |
| 351 | some_done = threads[i].done; |
| 352 | if (some_done) |
| 353 | break; |
| 354 | } |
| 355 | |
| 356 | if (some_done) |
| 357 | break; |
| 358 | |
| 359 | perc = (float) nitems / (float) total; |
| 360 | perc *= 100.0; |
| 361 | this_items = nitems - last_nitems; |
| 362 | this_items *= blocksize; |
| 363 | tdiff = mtime_since_now(&last_tv); |
| 364 | if (tdiff) { |
Jens Axboe | 78340c0 | 2014-10-06 21:00:10 -0600 | [diff] [blame] | 365 | this_items = (this_items * 1000) / (tdiff * 1024); |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 366 | printf("%3.2f%% done (%luKB/sec)\r", perc, this_items); |
| 367 | last_nitems = nitems; |
| 368 | fio_gettime(&last_tv, NULL); |
| 369 | } else |
| 370 | printf("%3.2f%% done\r", perc); |
| 371 | fflush(stdout); |
| 372 | usleep(250000); |
| 373 | }; |
| 374 | } |
| 375 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 376 | static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size, |
| 377 | uint64_t *nextents, uint64_t *nchunks) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 378 | { |
| 379 | struct worker_thread *threads; |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 380 | unsigned long nitems, total_items; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 381 | int i, err = 0; |
| 382 | |
| 383 | total_size = dev_size; |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 384 | total_items = dev_size / blocksize; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 385 | cur_offset = 0; |
| 386 | size_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
| 387 | |
| 388 | threads = malloc(num_threads * sizeof(struct worker_thread)); |
| 389 | for (i = 0; i < num_threads; i++) { |
Jens Axboe | acf7ead | 2014-10-21 13:02:29 -0600 | [diff] [blame] | 390 | memset(&threads[i], 0, sizeof(struct worker_thread)); |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 391 | threads[i].fd = f->fd; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 392 | |
| 393 | err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]); |
| 394 | if (err) { |
| 395 | log_err("fio: thread startup failed\n"); |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 400 | show_progress(threads, total_items); |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 401 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 402 | nitems = 0; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 403 | *nextents = 0; |
| 404 | *nchunks = 1; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 405 | for (i = 0; i < num_threads; i++) { |
| 406 | void *ret; |
| 407 | pthread_join(threads[i].thread, &ret); |
| 408 | nitems += threads[i].items; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 409 | *nchunks += threads[i].dupes; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | printf("Threads(%u): %lu items processed\n", num_threads, nitems); |
| 413 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 414 | *nextents = nitems; |
| 415 | *nchunks = nitems - *nchunks; |
| 416 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 417 | fio_mutex_remove(size_lock); |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 418 | free(threads); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 419 | return err; |
| 420 | } |
| 421 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 422 | static int dedupe_check(const char *filename, uint64_t *nextents, |
| 423 | uint64_t *nchunks) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 424 | { |
| 425 | uint64_t dev_size; |
| 426 | struct stat sb; |
| 427 | int flags; |
| 428 | |
| 429 | flags = O_RDONLY; |
| 430 | if (odirect) |
Jens Axboe | 18a7ca5 | 2014-10-07 08:51:23 -0600 | [diff] [blame] | 431 | flags |= OS_O_DIRECT; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 432 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 433 | memset(&file, 0, sizeof(file)); |
| 434 | file.file_name = strdup(filename); |
| 435 | |
| 436 | file.fd = open(filename, flags); |
| 437 | if (file.fd == -1) { |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 438 | perror("open"); |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 439 | goto err; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 440 | } |
| 441 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 442 | if (fstat(file.fd, &sb) < 0) { |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 443 | perror("fstat"); |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 444 | goto err; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 445 | } |
| 446 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 447 | dev_size = get_size(&file, &sb); |
| 448 | if (!dev_size) |
| 449 | goto err; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 450 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 451 | if (use_bloom) { |
| 452 | uint64_t bloom_entries; |
| 453 | |
Jens Axboe | 2ccf91a | 2014-09-27 21:29:03 -0600 | [diff] [blame] | 454 | bloom_entries = 8 * (dev_size / blocksize); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 455 | bloom = bloom_new(bloom_entries); |
| 456 | } |
| 457 | |
Jens Axboe | 0e79408 | 2014-09-24 09:34:47 -0600 | [diff] [blame] | 458 | printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 459 | |
Jens Axboe | 6d686d9 | 2014-10-07 08:27:19 -0600 | [diff] [blame] | 460 | return run_dedupe_threads(&file, dev_size, nextents, nchunks); |
| 461 | err: |
| 462 | if (file.fd != -1) |
| 463 | close(file.fd); |
| 464 | free(file.file_name); |
| 465 | return 1; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | static void show_chunk(struct chunk *c) |
| 469 | { |
| 470 | struct flist_head *n; |
| 471 | struct extent *e; |
| 472 | |
Jens Axboe | b2a657f | 2014-09-23 16:06:04 -0600 | [diff] [blame] | 473 | 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); |
Jens Axboe | d11a563 | 2014-09-24 09:30:52 -0600 | [diff] [blame] | 474 | flist_for_each(n, &c->extent_list[0]) { |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 475 | e = flist_entry(n, struct extent, list); |
Jens Axboe | b2a657f | 2014-09-23 16:06:04 -0600 | [diff] [blame] | 476 | printf("\toffset %llu\n", (unsigned long long) e->offset); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 480 | static void show_stat(uint64_t nextents, uint64_t nchunks) |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 481 | { |
Jens Axboe | ca79d44 | 2014-09-27 14:12:02 -0600 | [diff] [blame] | 482 | double perc, ratio; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 483 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 484 | printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks); |
Jens Axboe | 463fc15 | 2014-10-06 21:07:25 -0600 | [diff] [blame] | 485 | |
| 486 | if (nchunks) { |
| 487 | ratio = (double) nextents / (double) nchunks; |
| 488 | printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0); |
| 489 | } else |
| 490 | printf("De-dupe ratio: 1:infinite\n"); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 491 | |
| 492 | perc = 1.00 - ((double) nchunks / (double) nextents); |
| 493 | perc *= 100.0; |
| 494 | printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50)); |
| 495 | |
| 496 | } |
| 497 | |
| 498 | static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks) |
| 499 | { |
| 500 | struct rb_node *n; |
| 501 | |
| 502 | *nchunks = *nextents = 0; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 503 | |
| 504 | n = rb_first(&rb_root); |
| 505 | if (!n) |
| 506 | return; |
| 507 | |
| 508 | do { |
| 509 | struct chunk *c; |
| 510 | |
| 511 | c = rb_entry(n, struct chunk, rb_node); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 512 | (*nchunks)++; |
| 513 | *nextents += c->count; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 514 | |
| 515 | if (dump_output) |
| 516 | show_chunk(c); |
| 517 | |
| 518 | } while ((n = rb_next(n)) != NULL); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | static int usage(char *argv[]) |
| 522 | { |
| 523 | log_err("Check for dedupable blocks on a device/file\n\n"); |
| 524 | log_err("%s: [options] <device or file>\n", argv[0]); |
| 525 | log_err("\t-b\tChunk size to use\n"); |
| 526 | log_err("\t-t\tNumber of threads to use\n"); |
| 527 | log_err("\t-d\tFull extent/chunk debug output\n"); |
| 528 | log_err("\t-o\tUse O_DIRECT\n"); |
| 529 | log_err("\t-c\tFull collision check\n"); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 530 | log_err("\t-B\tUse probabilistic bloom filter\n"); |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 531 | log_err("\t-p\tPrint progress indicator\n"); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 532 | return 1; |
| 533 | } |
| 534 | |
| 535 | int main(int argc, char *argv[]) |
| 536 | { |
Jens Axboe | c6f0f39 | 2014-09-26 10:16:53 -0600 | [diff] [blame] | 537 | uint64_t nextents = 0, nchunks = 0; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 538 | int c, ret; |
| 539 | |
Jens Axboe | 7a74197 | 2014-09-26 09:58:29 -0600 | [diff] [blame] | 540 | debug_init(); |
| 541 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 542 | while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) { |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 543 | switch (c) { |
| 544 | case 'b': |
| 545 | blocksize = atoi(optarg); |
| 546 | break; |
| 547 | case 't': |
| 548 | num_threads = atoi(optarg); |
| 549 | break; |
| 550 | case 'd': |
| 551 | dump_output = atoi(optarg); |
| 552 | break; |
| 553 | case 'o': |
| 554 | odirect = atoi(optarg); |
| 555 | break; |
| 556 | case 'c': |
| 557 | collision_check = atoi(optarg); |
| 558 | break; |
Jens Axboe | 3f3415f | 2014-09-23 14:40:48 -0600 | [diff] [blame] | 559 | case 'p': |
| 560 | print_progress = atoi(optarg); |
| 561 | break; |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 562 | case 'B': |
| 563 | use_bloom = atoi(optarg); |
| 564 | break; |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 565 | case '?': |
| 566 | default: |
| 567 | return usage(argv); |
| 568 | } |
| 569 | } |
| 570 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 571 | if (collision_check || dump_output) |
| 572 | use_bloom = 0; |
| 573 | |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 574 | if (!num_threads) |
| 575 | num_threads = cpus_online(); |
| 576 | |
| 577 | if (argc == optind) |
| 578 | return usage(argv); |
| 579 | |
| 580 | sinit(); |
| 581 | |
| 582 | rb_root = RB_ROOT; |
| 583 | rb_lock = fio_mutex_init(FIO_MUTEX_UNLOCKED); |
| 584 | |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 585 | ret = dedupe_check(argv[optind], &nextents, &nchunks); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 586 | |
Jens Axboe | 343334a | 2014-09-27 08:08:24 -0600 | [diff] [blame] | 587 | if (!ret) { |
| 588 | if (!bloom) |
| 589 | iter_rb_tree(&nextents, &nchunks); |
Jens Axboe | 76b9b83 | 2014-09-26 09:51:33 -0600 | [diff] [blame] | 590 | |
Jens Axboe | 343334a | 2014-09-27 08:08:24 -0600 | [diff] [blame] | 591 | show_stat(nextents, nchunks); |
| 592 | } |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 593 | |
Jens Axboe | 997c9c3 | 2014-09-24 09:33:29 -0600 | [diff] [blame] | 594 | fio_mutex_remove(rb_lock); |
Jens Axboe | c6f0f39 | 2014-09-26 10:16:53 -0600 | [diff] [blame] | 595 | if (bloom) |
| 596 | bloom_free(bloom); |
Jens Axboe | 5aa702c | 2014-09-23 14:10:35 -0600 | [diff] [blame] | 597 | scleanup(); |
| 598 | return ret; |
| 599 | } |