memcpy: add hybrid
[fio.git] / lib / memcpy.c
index f937dd9f3cafd80cc97be8ce8b80138e0671ba91..00e65aa7d50a8e915d66935e28633d64bd24c81a 100644 (file)
@@ -72,16 +72,17 @@ static struct memcpy_test tests[] = {
 struct memcpy_type {
        const char *name;
        unsigned int mask;
-       void (*fn)(struct memcpy_type *, struct memcpy_test *);
+       void (*fn)(struct memcpy_test *);
 };
 
 enum {
        T_MEMCPY        = 1U << 0,
        T_MEMMOVE       = 1U << 1,
        T_SIMPLE        = 1U << 2,
+       T_HYBRID        = 1U << 3,
 };
 
-#define do_test(t, test, fn)   do {                                    \
+#define do_test(test, fn)      do {                                    \
        size_t left, this;                                              \
        void *src, *dst;                                                \
        int i;                                                          \
@@ -102,14 +103,14 @@ enum {
        }                                                               \
 } while (0)
 
-static void t_memcpy(struct memcpy_type *t, struct memcpy_test *test)
+static void t_memcpy(struct memcpy_test *test)
 {
-       do_test(t, test, memcpy);
+       do_test(test, memcpy);
 }
 
-static void t_memmove(struct memcpy_type *t, struct memcpy_test *test)
+static void t_memmove(struct memcpy_test *test)
 {
-       do_test(t, test, memmove);
+       do_test(test, memmove);
 }
 
 static void simple_memcpy(void *dst, void const *src, size_t len)
@@ -121,9 +122,17 @@ static void simple_memcpy(void *dst, void const *src, size_t len)
                *d++ = *s++;
 }
 
-static void t_simple(struct memcpy_type *t, struct memcpy_test *test)
+static void t_simple(struct memcpy_test *test)
 {
-       do_test(t, test, simple_memcpy);
+       do_test(test, simple_memcpy);
+}
+
+static void t_hybrid(struct memcpy_test *test)
+{
+       if (test->size >= 64)
+               do_test(test, simple_memcpy);
+       else
+               do_test(test, memcpy);
 }
 
 static struct memcpy_type t[] = {
@@ -142,7 +151,11 @@ static struct memcpy_type t[] = {
                .mask = T_SIMPLE,
                .fn = t_simple,
        },
-
+       {
+               .name = "hybrid",
+               .mask = T_HYBRID,
+               .fn = t_hybrid,
+       },
        {
                .name = NULL,
        },
@@ -187,10 +200,13 @@ static int setup_tests(void)
        void *src, *dst;
        int i;
 
-       if (posix_memalign(&src, page_size, BUF_SIZE))
-               return 1;
-       if (posix_memalign(&dst, page_size, BUF_SIZE))
+       src = malloc(BUF_SIZE);
+       dst = malloc(BUF_SIZE);
+       if (!src || !dst) {
+               free(src);
+               free(dst);
                return 1;
+       }
 
        init_rand_seed(&state, 0x8989, 0);
        fill_random_buf(&state, src, BUF_SIZE);
@@ -204,6 +220,12 @@ static int setup_tests(void)
        return 0;
 }
 
+static void free_tests(void)
+{
+       free(tests[0].src);
+       free(tests[0].dst);
+}
+
 int fio_memcpy_test(const char *type)
 {
        unsigned int test_mask = 0;
@@ -239,13 +261,13 @@ int fio_memcpy_test(const char *type)
                 * we've touched the data.
                 */
                usec_spin(100000);
-               t[i].fn(&t[i], &tests[0]);
+               t[i].fn(&tests[0]);
 
                printf("%s\n", t[i].name);
 
                for (j = 0; tests[j].name; j++) {
                        fio_gettime(&ts, NULL);
-                       t[i].fn(&t[i], &tests[j]);
+                       t[i].fn(&tests[j]);
                        usec = utime_since_now(&ts);
 
                        if (usec) {
@@ -259,5 +281,6 @@ int fio_memcpy_test(const char *type)
                }
        }
 
+       free_tests();
        return 0;
 }