diff options
author | Anders Lewis <agloo@google.com> | 2017-06-23 15:53:59 -0700 |
---|---|---|
committer | Anders Lewis <agloo@google.com> | 2017-07-12 13:42:23 -0700 |
commit | f4447b9105f2d42acd2bbe0ba1c602ef3562b681 (patch) | |
tree | 74a80edf3fb9f0f025e1a1dff79659dc514dc67b /benchmarks/string_benchmark.cpp | |
parent | 42edca036e5c9ed24017de5fba41d5d1feda61d0 (diff) |
Support memory alignment tests for string_benchmark.cpp
Test: Manually verify that benchmark times are similar and add a unit test.
Change-Id: Idaaeb4c8c3629f797ffd8d0c86c8d7b7b9653181
Diffstat (limited to 'benchmarks/string_benchmark.cpp')
-rw-r--r-- | benchmarks/string_benchmark.cpp | 198 |
1 files changed, 128 insertions, 70 deletions
diff --git a/benchmarks/string_benchmark.cpp b/benchmarks/string_benchmark.cpp index 86a7c359f..2ab65a891 100644 --- a/benchmarks/string_benchmark.cpp +++ b/benchmarks/string_benchmark.cpp @@ -18,188 +18,246 @@ #include <string.h> #include <benchmark/benchmark.h> +#include "util.h" constexpr auto KB = 1024; -#define AT_COMMON_SIZES \ - Arg(8)->Arg(64)->Arg(512)->Arg(1*KB)->Arg(8*KB)->Arg(16*KB)->Arg(32*KB)->Arg(64*KB) +// NOTE: these constants are temporary replacements for AT_COMMON_SIZES until +// the new interface for Bionic benchmarks is implemented. -// TODO: test unaligned operation too? (currently everything will be 8-byte aligned by malloc.) +// Set all four to 0 to test normal alignment. +#define AT_SRC_ALIGN 0 +#define AT_DST_ALIGN 0 + +#define AT_ALIGNED_TWOBUF \ + Args({(8), AT_SRC_ALIGN, AT_DST_ALIGN})->Args({(64), AT_SRC_ALIGN, AT_DST_ALIGN})-> \ + Args({(512), AT_SRC_ALIGN, AT_DST_ALIGN})->Args({(1*KB), AT_SRC_ALIGN, AT_DST_ALIGN})-> \ + Args({(8*KB), AT_SRC_ALIGN, AT_DST_ALIGN})->Args({(16*KB), AT_SRC_ALIGN, AT_DST_ALIGN})-> \ + Args({(32*KB), AT_SRC_ALIGN, AT_DST_ALIGN})->Args({(64*KB), AT_SRC_ALIGN, AT_DST_ALIGN}) + +#define AT_ALIGNED_ONEBUF \ + Args({(8), AT_SRC_ALIGN})->Args({(64), AT_SRC_ALIGN})->Args({(512), AT_SRC_ALIGN})-> \ + Args({(1*KB), AT_SRC_ALIGN})->Args({(8*KB), AT_SRC_ALIGN})->Args({(16*KB), AT_SRC_ALIGN})-> \ + Args({(32*KB), AT_SRC_ALIGN})->Args({(64*KB), AT_SRC_ALIGN}) static void BM_string_memcmp(benchmark::State& state) { const size_t nbytes = state.range(0); - char* src = new char[nbytes]; char* dst = new char[nbytes]; - memset(src, 'x', nbytes); - memset(dst, 'x', nbytes); + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); + char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'x'); volatile int c __attribute__((unused)) = 0; while (state.KeepRunning()) { - c += memcmp(dst, src, nbytes); + c += memcmp(dst_aligned, src_aligned, nbytes); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); - delete[] src; - delete[] dst; } -BENCHMARK(BM_string_memcmp)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memcmp)->AT_ALIGNED_TWOBUF; static void BM_string_memcpy(benchmark::State& state) { const size_t nbytes = state.range(0); - char* src = new char[nbytes]; char* dst = new char[nbytes]; - memset(src, 'x', nbytes); + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); + char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); while (state.KeepRunning()) { - memcpy(dst, src, nbytes); + memcpy(dst_aligned, src_aligned, nbytes); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); - delete[] src; - delete[] dst; } -BENCHMARK(BM_string_memcpy)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memcpy)->AT_ALIGNED_TWOBUF; static void BM_string_memmove_non_overlapping(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> src(nbytes, 'x'); - std::vector<char> dst(nbytes, 'x'); + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); + char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); while (state.KeepRunning()) { - memmove(dst.data(), src.data(), nbytes); + memmove(dst_aligned, src_aligned, nbytes); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_memmove_non_overlapping)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memmove_non_overlapping)->AT_ALIGNED_TWOBUF; static void BM_string_memmove_overlap_dst_before_src(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> buf(nbytes + 1, 'x'); + const size_t alignment = state.range(1); + + std::vector<char> buf(3 * alignment + nbytes + 1, 'x'); + char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); while (state.KeepRunning()) { - memmove(buf.data(), buf.data() + 1, nbytes); // Worst-case overlap. + memmove(buf_aligned, buf_aligned + 1, nbytes); // Worst-case overlap. } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_memmove_overlap_dst_before_src)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memmove_overlap_dst_before_src)->AT_ALIGNED_ONEBUF; static void BM_string_memmove_overlap_src_before_dst(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> buf(nbytes + 1, 'x'); + const size_t alignment = state.range(1); + + std::vector<char> buf; + char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); while (state.KeepRunning()) { - memmove(buf.data() + 1, buf.data(), nbytes); // Worst-case overlap. + memmove(buf_aligned + 1, buf_aligned, nbytes); // Worst-case overlap. } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_memmove_overlap_src_before_dst)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memmove_overlap_src_before_dst)->AT_ALIGNED_ONEBUF; static void BM_string_memset(benchmark::State& state) { const size_t nbytes = state.range(0); - char* dst = new char[nbytes]; + const size_t alignment = state.range(1); + + std::vector<char> buf; + char* buf_aligned = GetAlignedPtr(&buf, alignment, nbytes + 1); while (state.KeepRunning()) { - memset(dst, 0, nbytes); + memset(buf_aligned, 0, nbytes); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); - delete[] dst; } -BENCHMARK(BM_string_memset)->AT_COMMON_SIZES; +BENCHMARK(BM_string_memset)->AT_ALIGNED_ONEBUF; static void BM_string_strlen(benchmark::State& state) { const size_t nbytes = state.range(0); - char* s = new char[nbytes]; - memset(s, 'x', nbytes); - s[nbytes - 1] = 0; + const size_t alignment = state.range(1); + + std::vector<char> buf; + char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x'); + buf_aligned[nbytes - 1] = '\0'; volatile int c __attribute__((unused)) = 0; while (state.KeepRunning()) { - c += strlen(s); + c += strlen(buf_aligned); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); - delete[] s; } -BENCHMARK(BM_string_strlen)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strlen)->AT_ALIGNED_ONEBUF; static void BM_string_strcat_copy_only(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> src(nbytes, 'x'); - std::vector<char> dst(nbytes + 2); - src[nbytes - 1] = '\0'; - dst[0] = 'y'; - dst[1] = 'y'; - dst[2] = '\0'; + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); + char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes + 2); + src_aligned[nbytes - 1] = '\0'; + dst_aligned[0] = 'y'; + dst_aligned[1] = 'y'; + dst_aligned[2] = '\0'; while (state.KeepRunning()) { - strcat(dst.data(), src.data()); - dst[2] = '\0'; + strcat(dst_aligned, src_aligned); + dst_aligned[2] = '\0'; } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_strcat_copy_only)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strcat_copy_only)->AT_ALIGNED_TWOBUF; static void BM_string_strcat_seek_only(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> src(3, 'x'); - std::vector<char> dst(nbytes + 2, 'y'); - src[2] = '\0'; - dst[nbytes - 1] = '\0'; + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, 3, 'x'); + char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes + 2, 'y'); + src_aligned[2] = '\0'; + dst_aligned[nbytes - 1] = '\0'; while (state.KeepRunning()) { - strcat(dst.data(), src.data()); - dst[nbytes - 1] = '\0'; + strcat(dst_aligned, src_aligned); + dst_aligned[nbytes - 1] = '\0'; } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_strcat_seek_only)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strcat_seek_only)->AT_ALIGNED_TWOBUF; static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> src(nbytes / 2, 'x'); - std::vector<char> dst(nbytes / 2, 'y'); - src[nbytes / 2 - 1] = '\0'; - dst[nbytes / 2 - 1] = '\0'; + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes / 2, 'x'); + char* dst_aligned = GetAlignedPtrFilled(&dst, dst_alignment, nbytes, 'y'); + src_aligned[nbytes / 2 - 1] = '\0'; + dst_aligned[nbytes / 2 - 1] = '\0'; while (state.KeepRunning()) { - strcat(dst.data(), src.data()); - dst[nbytes / 2 - 1] = '\0'; + strcat(dst_aligned, src_aligned); + dst_aligned[nbytes / 2 - 1] = '\0'; } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_strcat_half_copy_half_seek)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strcat_half_copy_half_seek)->AT_ALIGNED_TWOBUF; static void BM_string_strcpy(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> src(nbytes, 'x'); - std::vector<char> dst(nbytes); - src[nbytes - 1] = '\0'; + const size_t src_alignment = state.range(1); + const size_t dst_alignment = state.range(2); + + std::vector<char> src; + std::vector<char> dst; + char* src_aligned = GetAlignedPtrFilled(&src, src_alignment, nbytes, 'x'); + char* dst_aligned = GetAlignedPtr(&dst, dst_alignment, nbytes); + src_aligned[nbytes - 1] = '\0'; while (state.KeepRunning()) { - strcpy(dst.data(), src.data()); + strcpy(dst_aligned, src_aligned); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_strcpy)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strcpy)->AT_ALIGNED_TWOBUF; static void BM_string_strcmp(benchmark::State& state) { const size_t nbytes = state.range(0); - std::vector<char> s1(nbytes, 'x'); - std::vector<char> s2(nbytes, 'x'); - s1[nbytes - 1] = '\0'; - s2[nbytes - 1] = '\0'; + const size_t s1_alignment = state.range(1); + const size_t s2_alignment = state.range(2); + + std::vector<char> s1; + std::vector<char> s2; + char* s1_aligned = GetAlignedPtrFilled(&s1, s1_alignment, nbytes, 'x'); + char* s2_aligned = GetAlignedPtrFilled(&s2, s2_alignment, nbytes, 'x'); + s1_aligned[nbytes - 1] = '\0'; + s2_aligned[nbytes - 1] = '\0'; volatile int c __attribute__((unused)); while (state.KeepRunning()) { - c = strcmp(s1.data(), s2.data()); + c = strcmp(s1_aligned, s2_aligned); } state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); } -BENCHMARK(BM_string_strcmp)->AT_COMMON_SIZES; +BENCHMARK(BM_string_strcmp)->AT_ALIGNED_TWOBUF; |