crc/test: if the checksum needs a _final(), do it in the loop
[fio.git] / crc / test.c
index 3773b714782adf9e2eab1eb81fda731ba897a1bf..6453a72bbf336cf2a5717535e091d8dbc27b3fb9 100644 (file)
@@ -17,6 +17,8 @@
 #include "../crc/sha256.h"
 #include "../crc/sha512.h"
 #include "../crc/xxhash.h"
+#include "../crc/murmur3.h"
+#include "../hash.h"
 
 #include "test.h"
 
@@ -26,7 +28,8 @@
 struct test_type {
        const char *name;
        unsigned int mask;
-       uint64_t (*fn)(void);
+       void (*fn)(struct test_type *, void *, size_t);
+       uint32_t output;
 };
 
 enum {
@@ -40,224 +43,129 @@ enum {
        T_SHA256        = 1U << 7,
        T_SHA512        = 1U << 8,
        T_XXHASH        = 1U << 9,
+       T_MURMUR3       = 1U << 10,
+       T_JHASH         = 1U << 11,
 };
 
-static void randomize_buf(void *buf, unsigned int size, int seed)
-{
-       struct frand_state state;
-
-       init_rand_seed(&state, seed);
-       fill_random_buf(&state, buf, size);
-}
-
-static uint64_t t_md5(void)
+static void t_md5(struct test_type *t, void *buf, size_t size)
 {
        uint32_t digest[4];
        struct fio_md5_ctx ctx = { .hash = digest };
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
        fio_md5_init(&ctx);
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
-       for (i = 0; i < NR_CHUNKS; i++)
-               fio_md5_update(&ctx, buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+       for (i = 0; i < NR_CHUNKS; i++) {
+               fio_md5_update(&ctx, buf, size);
+               fio_md5_final(&ctx);
+       }
 }
 
-static uint64_t t_crc64(void)
+static void t_crc64(struct test_type *t, void *buf, size_t size)
 {
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_crc64(buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_crc64(buf, size);
 }
 
-static uint64_t t_crc32(void)
+static void t_crc32(struct test_type *t, void *buf, size_t size)
 {
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_crc32(buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_crc32(buf, size);
 }
 
-static uint64_t t_crc32c(void)
+static void t_crc32c(struct test_type *t, void *buf, size_t size)
 {
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_crc32c(buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_crc32c(buf, size);
 }
 
-static uint64_t t_crc16(void)
+static void t_crc16(struct test_type *t, void *buf, size_t size)
 {
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_crc16(buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_crc16(buf, size);
 }
 
-static uint64_t t_crc7(void)
+static void t_crc7(struct test_type *t, void *buf, size_t size)
 {
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_crc7(buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_crc7(buf, size);
 }
 
-static uint64_t t_sha1(void)
+static void t_sha1(struct test_type *t, void *buf, size_t size)
 {
        uint32_t sha[5];
        struct fio_sha1_ctx ctx = { .H = sha };
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
        fio_sha1_init(&ctx);
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_sha1_update(&ctx, buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+               fio_sha1_update(&ctx, buf, size);
 }
 
-static uint64_t t_sha256(void)
+static void t_sha256(struct test_type *t, void *buf, size_t size)
 {
        uint8_t sha[64];
        struct fio_sha256_ctx ctx = { .buf = sha };
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
        fio_sha256_init(&ctx);
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
-       for (i = 0; i < NR_CHUNKS; i++)
-               fio_sha256_update(&ctx, buf, CHUNK);
-
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+       for (i = 0; i < NR_CHUNKS; i++) {
+               fio_sha256_update(&ctx, buf, size);
+               fio_sha256_final(&ctx);
+       }
 }
 
-static uint64_t t_sha512(void)
+static void t_sha512(struct test_type *t, void *buf, size_t size)
 {
        uint8_t sha[128];
        struct fio_sha512_ctx ctx = { .buf = sha };
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
        fio_sha512_init(&ctx);
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
+       for (i = 0; i < NR_CHUNKS; i++)
+               fio_sha512_update(&ctx, buf, size);
+}
+
+static void t_murmur3(struct test_type *t, void *buf, size_t size)
+{
+       int i;
 
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               fio_sha512_update(&ctx, buf, CHUNK);
+               murmurhash3(buf, size, 0x8989);
+}
 
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+static void t_jhash(struct test_type *t, void *buf, size_t size)
+{
+       int i;
+
+       for (i = 0; i < NR_CHUNKS; i++)
+               t->output += jhash(buf, size, 0x8989);
 }
 
-static uint64_t t_xxhash(void)
+static void t_xxhash(struct test_type *t, void *buf, size_t size)
 {
        void *state;
-       struct timeval s;
-       uint64_t ret;
-       void *buf;
        int i;
 
        state = XXH32_init(0x8989);
 
-       buf = malloc(CHUNK);
-       randomize_buf(buf, CHUNK, 0x8989);
-
-       fio_gettime(&s, NULL);
        for (i = 0; i < NR_CHUNKS; i++)
-               XXH32_update(state, buf, CHUNK);
+               XXH32_update(state, buf, size);
 
-       XXH32_digest(state);
-       ret = utime_since_now(&s);
-       free(buf);
-       return ret;
+       t->output = XXH32_digest(state);
 }
 
 static struct test_type t[] = {
@@ -311,6 +219,16 @@ static struct test_type t[] = {
                .mask = T_XXHASH,
                .fn = t_xxhash,
        },
+       {
+               .name = "murmur3",
+               .mask = T_MURMUR3,
+               .fn = t_murmur3,
+       },
+       {
+               .name = "jhash",
+               .mask = T_JHASH,
+               .fn = t_jhash,
+       },
        {
                .name = NULL,
        },
@@ -345,14 +263,16 @@ static int list_types(void)
        for (i = 0; t[i].name; i++)
                printf("%s\n", t[i].name);
 
-       return 0;
+       return 1;
 }
 
 int fio_crctest(const char *type)
 {
        unsigned int test_mask = 0;
        uint64_t mb = CHUNK * NR_CHUNKS;
-       int i;
+       struct frand_state state;
+       int i, first = 1;
+       void *buf;
 
        crc32c_intel_probe();
 
@@ -363,18 +283,47 @@ int fio_crctest(const char *type)
        else
                test_mask = get_test_mask(type);
 
+       if (!test_mask) {
+               fprintf(stderr, "fio: unknown hash `%s`. Available:\n", type);
+               return list_types();
+       }
+
+       buf = malloc(CHUNK);
+       init_rand_seed(&state, 0x8989);
+       fill_random_buf(&state, buf, CHUNK);
+
        for (i = 0; t[i].name; i++) {
+               struct timeval tv;
                double mb_sec;
                uint64_t usec;
+               char pre[3];
 
                if (!(t[i].mask & test_mask))
                        continue;
 
-               usec = t[i].fn();
+               /*
+                * For first run, make sure CPUs are spun up and that
+                * we've touched the data.
+                */
+               if (first) {
+                       usec_spin(100000);
+                       t[i].fn(&t[i], buf, CHUNK);
+               }
+
+               fio_gettime(&tv, NULL);
+               t[i].fn(&t[i], buf, CHUNK);
+               usec = utime_since_now(&tv);
+
                mb_sec = (double) mb / (double) usec;
                mb_sec /= (1.024 * 1.024);
-               printf("%s:\t%.2f MB/sec\n", t[i].name, mb_sec);
+               if (strlen(t[i].name) >= 7)
+                       sprintf(pre, "\t");
+               else
+                       sprintf(pre, "\t\t");
+               printf("%s:%s%8.2f MB/sec\n", t[i].name, pre, mb_sec);
+               first = 0;
        }
 
+       free(buf);
        return 0;
 }