#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
-#include <linux/fs.h>
#include <fcntl.h>
#include <string.h>
#include "../smalloc.h"
#include "../minmax.h"
#include "../crc/md5.h"
-#include "../memalign.h"
+#include "../lib/memalign.h"
#include "../os/os.h"
#include "../gettime.h"
#include "../fio_time.h"
static uint64_t cur_offset;
static struct fio_mutex *size_lock;
-static int dev_fd;
+static struct fio_file file;
-static uint64_t get_size(int fd, struct stat *sb)
+static uint64_t get_size(struct fio_file *f, struct stat *sb)
{
uint64_t ret;
if (S_ISBLK(sb->st_mode)) {
- if (ioctl(fd, BLKGETSIZE64, &ret) < 0) {
- perror("ioctl");
+ unsigned long long bytes = 0;
+
+ if (blockdev_size(f, &bytes)) {
+ log_err("dedupe: failed getting bdev size\n");
return 0;
}
+ ret = bytes;
} else
ret = sb->st_size;
ibuf = fio_memalign(blocksize, blocksize);
e = flist_entry(c->extent_list[0].next, struct extent, list);
- if (read_block(dev_fd, cbuf, e->offset))
+ if (read_block(file.fd, cbuf, e->offset))
goto out;
- if (read_block(dev_fd, ibuf, i->offset))
+ if (read_block(file.fd, ibuf, i->offset))
goto out;
ret = memcmp(ibuf, cbuf, blocksize);
for (i = 0; i < nblocks; i++) {
void *thisptr = buf + (i * blocksize);
- if (items)
- items[i].offset = offset;
+ items[i].offset = offset;
crc_buf(thisptr, items[i].hash);
offset += blocksize;
nitems++;
this_items *= blocksize;
tdiff = mtime_since_now(&last_tv);
if (tdiff) {
- this_items /= tdiff;
- printf("%3.2f%% done (%luKB/sec)\r", perc, this_items);
+ this_items = (this_items * 1000) / (tdiff * 1024);
+ printf("%3.2f%% done (%luKiB/sec)\r", perc, this_items);
last_nitems = nitems;
fio_gettime(&last_tv, NULL);
} else
};
}
-static int run_dedupe_threads(int fd, uint64_t dev_size, uint64_t *nextents,
- uint64_t *nchunks)
+static int run_dedupe_threads(struct fio_file *f, uint64_t dev_size,
+ uint64_t *nextents, uint64_t *nchunks)
{
struct worker_thread *threads;
unsigned long nitems, total_items;
threads = malloc(num_threads * sizeof(struct worker_thread));
for (i = 0; i < num_threads; i++) {
- threads[i].fd = fd;
- threads[i].items = 0;
- threads[i].err = 0;
- threads[i].done = 0;
+ memset(&threads[i], 0, sizeof(struct worker_thread));
+ threads[i].fd = f->fd;
err = pthread_create(&threads[i].thread, NULL, thread_fn, &threads[i]);
if (err) {
flags = O_RDONLY;
if (odirect)
- flags |= O_DIRECT;
+ flags |= OS_O_DIRECT;
+
+ memset(&file, 0, sizeof(file));
+ file.file_name = strdup(filename);
- dev_fd = open(filename, flags);
- if (dev_fd == -1) {
+ file.fd = open(filename, flags);
+ if (file.fd == -1) {
perror("open");
- return 1;
+ goto err;
}
- if (fstat(dev_fd, &sb) < 0) {
+ if (fstat(file.fd, &sb) < 0) {
perror("fstat");
- close(dev_fd);
- return 1;
+ goto err;
}
- dev_size = get_size(dev_fd, &sb);
- if (!dev_size) {
- close(dev_fd);
- return 1;
- }
+ dev_size = get_size(&file, &sb);
+ if (!dev_size)
+ goto err;
if (use_bloom) {
uint64_t bloom_entries;
printf("Will check <%s>, size <%llu>, using %u threads\n", filename, (unsigned long long) dev_size, num_threads);
- return run_dedupe_threads(dev_fd, dev_size, nextents, nchunks);
+ return run_dedupe_threads(&file, dev_size, nextents, nchunks);
+err:
+ if (file.fd != -1)
+ close(file.fd);
+ free(file.file_name);
+ return 1;
}
static void show_chunk(struct chunk *c)
double perc, ratio;
printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
- ratio = (double) nextents / (double) nchunks;
- printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
+
+ if (nchunks) {
+ ratio = (double) nextents / (double) nchunks;
+ printf("De-dupe ratio: 1:%3.2f\n", ratio - 1.0);
+ } else
+ printf("De-dupe ratio: 1:infinite\n");
perc = 1.00 - ((double) nchunks / (double) nextents);
perc *= 100.0;
uint64_t nextents = 0, nchunks = 0;
int c, ret;
+ arch_init(argv);
debug_init();
while ((c = getopt(argc, argv, "b:t:d:o:c:p:B:")) != -1) {