Merge branch 'patch-1' of https://github.com/antroseco/fio
[fio.git] / t / dedupe.c
index 1b4277c1b9d3c17b20dccb9df16e7092552f4123..8b659c76c71caca57be3e80c9b4ed94077fb79a8 100644 (file)
@@ -3,24 +3,20 @@
  * just scans the filename for extents of the given size, checksums them,
  * and orders them up.
  */
+#include <fcntl.h>
+#include <inttypes.h>
 #include <stdio.h>
-#include <stdio.h>
+#include <string.h>
 #include <unistd.h>
-#include <inttypes.h>
-#include <assert.h>
-#include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/ioctl.h>
-#include <fcntl.h>
-#include <string.h>
 
+#include "../fio.h"
 #include "../flist.h"
 #include "../log.h"
 #include "../fio_sem.h"
 #include "../smalloc.h"
 #include "../minmax.h"
 #include "../crc/md5.h"
-#include "../lib/memalign.h"
 #include "../os/os.h"
 #include "../gettime.h"
 #include "../fio_time.h"
@@ -162,8 +158,8 @@ static int col_check(struct chunk *c, struct item *i)
        char *cbuf, *ibuf;
        int ret = 1;
 
-       cbuf = fio_memalign(blocksize, blocksize);
-       ibuf = fio_memalign(blocksize, blocksize);
+       cbuf = fio_memalign(blocksize, blocksize, false);
+       ibuf = fio_memalign(blocksize, blocksize, false);
 
        e = flist_entry(c->extent_list[0].next, struct extent, list);
        if (read_block(file.fd, cbuf, e->offset))
@@ -174,8 +170,8 @@ static int col_check(struct chunk *c, struct item *i)
 
        ret = memcmp(ibuf, cbuf, blocksize);
 out:
-       fio_memfree(cbuf, blocksize);
-       fio_memfree(ibuf, blocksize);
+       fio_memfree(cbuf, blocksize, false);
+       fio_memfree(ibuf, blocksize, false);
        return ret;
 }
 
@@ -313,7 +309,7 @@ static void *thread_fn(void *data)
        struct worker_thread *thread = data;
        void *buf;
 
-       buf = fio_memalign(blocksize, chunk_size);
+       buf = fio_memalign(blocksize, chunk_size, false);
 
        do {
                if (get_work(&thread->cur_offset, &thread->size)) {
@@ -327,7 +323,7 @@ static void *thread_fn(void *data)
        } while (1);
 
        thread->done = 1;
-       fio_memfree(buf, chunk_size);
+       fio_memfree(buf, chunk_size, false);
        return NULL;
 }
 
@@ -477,11 +473,14 @@ static void show_chunk(struct chunk *c)
        }
 }
 
-static void show_stat(uint64_t nextents, uint64_t nchunks)
+static void show_stat(uint64_t nextents, uint64_t nchunks, uint64_t ndupextents)
 {
        double perc, ratio;
 
-       printf("Extents=%lu, Unique extents=%lu\n", (unsigned long) nextents, (unsigned long) nchunks);
+       printf("Extents=%lu, Unique extents=%lu", (unsigned long) nextents, (unsigned long) nchunks);
+       if (!bloom)
+               printf(" Duplicated extents=%lu", (unsigned long) ndupextents);
+       printf("\n");
 
        if (nchunks) {
                ratio = (double) nextents / (double) nchunks;
@@ -489,17 +488,20 @@ static void show_stat(uint64_t nextents, uint64_t nchunks)
        } else
                printf("De-dupe ratio: 1:infinite\n");
 
+       if (ndupextents)
+               printf("De-dupe working set at least: %3.2f%%\n", 100.0 * (double) ndupextents / (double) nextents);
+
        perc = 1.00 - ((double) nchunks / (double) nextents);
        perc *= 100.0;
        printf("Fio setting: dedupe_percentage=%u\n", (int) (perc + 0.50));
 
 }
 
-static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks)
+static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks, uint64_t *ndupextents)
 {
        struct fio_rb_node *n;
 
-       *nchunks = *nextents = 0;
+       *nchunks = *nextents = *ndupextents = 0;
 
        n = rb_first(&rb_root);
        if (!n)
@@ -511,6 +513,7 @@ static void iter_rb_tree(uint64_t *nextents, uint64_t *nchunks)
                c = rb_entry(n, struct chunk, rb_node);
                (*nchunks)++;
                *nextents += c->count;
+               *ndupextents += (c->count > 1);
 
                if (dump_output)
                        show_chunk(c);
@@ -534,7 +537,7 @@ static int usage(char *argv[])
 
 int main(int argc, char *argv[])
 {
-       uint64_t nextents = 0, nchunks = 0;
+       uint64_t nextents = 0, nchunks = 0, ndupextents = 0;
        int c, ret;
 
        arch_init(argv);
@@ -587,9 +590,9 @@ int main(int argc, char *argv[])
 
        if (!ret) {
                if (!bloom)
-                       iter_rb_tree(&nextents, &nchunks);
+                       iter_rb_tree(&nextents, &nchunks, &ndupextents);
 
-               show_stat(nextents, nchunks);
+               show_stat(nextents, nchunks, ndupextents);
        }
 
        fio_sem_remove(rb_lock);