2012-11-09 00:37:43 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-08-10 01:52:19 +02:00
|
|
|
#include <err.h>
|
2015-02-18 04:58:53 +01:00
|
|
|
#include <stdint.h>
|
2012-11-09 00:37:43 +01:00
|
|
|
#include <string.h>
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
#include <benchmark/benchmark.h>
|
2017-07-25 05:01:13 +02:00
|
|
|
#include <util.h>
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_string_memcmp(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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');
|
2012-11-09 00:37:43 +01:00
|
|
|
|
|
|
|
volatile int c __attribute__((unused)) = 0;
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
c += memcmp(dst_aligned, src_aligned, nbytes);
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memcmp);
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_string_memcpy(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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);
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
memcpy(dst_aligned, src_aligned, nbytes);
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memcpy);
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2017-06-07 22:00:38 +02:00
|
|
|
static void BM_string_memmove_non_overlapping(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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');
|
2012-11-09 23:59:21 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
memmove(dst_aligned, src_aligned, nbytes);
|
2012-11-09 23:59:21 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
2012-11-09 23:59:21 +01:00
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memmove_non_overlapping);
|
2017-06-07 22:00:38 +02:00
|
|
|
|
|
|
|
static void BM_string_memmove_overlap_dst_before_src(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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');
|
2017-06-07 22:00:38 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
memmove(buf_aligned, buf_aligned + 1, nbytes); // Worst-case overlap.
|
2017-06-07 22:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memmove_overlap_dst_before_src);
|
2017-06-07 22:00:38 +02:00
|
|
|
|
|
|
|
static void BM_string_memmove_overlap_src_before_dst(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
const size_t alignment = state.range(1);
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
char* buf_aligned = GetAlignedPtrFilled(&buf, alignment, nbytes + 1, 'x');
|
2017-06-07 22:00:38 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
memmove(buf_aligned + 1, buf_aligned, nbytes); // Worst-case overlap.
|
2017-06-07 22:00:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memmove_overlap_src_before_dst);
|
2012-11-09 23:59:21 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_string_memset(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
const size_t alignment = state.range(1);
|
|
|
|
|
|
|
|
std::vector<char> buf;
|
|
|
|
char* buf_aligned = GetAlignedPtr(&buf, alignment, nbytes + 1);
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
memset(buf_aligned, 0, nbytes);
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_memset);
|
2012-11-09 00:37:43 +01:00
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
static void BM_string_strlen(benchmark::State& state) {
|
2016-11-14 14:16:08 +01:00
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2012-11-09 00:37:43 +01:00
|
|
|
|
|
|
|
volatile int c __attribute__((unused)) = 0;
|
2016-02-17 19:23:52 +01:00
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
c += strlen(buf_aligned);
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 19:23:52 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
2012-11-09 00:37:43 +01:00
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strlen);
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
static void BM_string_strcat_copy_only(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
strcat(dst_aligned, src_aligned);
|
|
|
|
dst_aligned[2] = '\0';
|
2017-06-12 20:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strcat_copy_only);
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
static void BM_string_strcat_seek_only(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
strcat(dst_aligned, src_aligned);
|
|
|
|
dst_aligned[nbytes - 1] = '\0';
|
2017-06-12 20:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strcat_seek_only);
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
static void BM_string_strcat_half_copy_half_seek(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2017-06-12 20:24:01 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
strcat(dst_aligned, src_aligned);
|
|
|
|
dst_aligned[nbytes / 2 - 1] = '\0';
|
2017-06-12 20:24:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strcat_half_copy_half_seek);
|
2017-06-12 21:33:06 +02:00
|
|
|
|
|
|
|
static void BM_string_strcpy(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2017-06-12 21:33:06 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
strcpy(dst_aligned, src_aligned);
|
2017-06-12 21:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strcpy);
|
2017-06-12 21:33:06 +02:00
|
|
|
|
|
|
|
static void BM_string_strcmp(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
2017-06-24 00:53:59 +02:00
|
|
|
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';
|
2017-06-12 21:33:06 +02:00
|
|
|
|
|
|
|
volatile int c __attribute__((unused));
|
|
|
|
while (state.KeepRunning()) {
|
2017-06-24 00:53:59 +02:00
|
|
|
c = strcmp(s1_aligned, s2_aligned);
|
2017-06-12 21:33:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2017-07-25 05:01:13 +02:00
|
|
|
BIONIC_BENCHMARK(BM_string_strcmp);
|
2017-08-09 03:29:51 +02:00
|
|
|
|
|
|
|
static void BM_string_strstr(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
|
|
|
const size_t haystack_alignment = state.range(1);
|
|
|
|
const size_t needle_alignment = state.range(2);
|
|
|
|
|
|
|
|
std::vector<char> haystack;
|
|
|
|
std::vector<char> needle;
|
|
|
|
char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x');
|
|
|
|
char* needle_aligned = GetAlignedPtrFilled(&needle, needle_alignment,
|
|
|
|
std::min(nbytes, static_cast<size_t>(5)), 'x');
|
|
|
|
|
|
|
|
if (nbytes / 4 > 2) {
|
|
|
|
for (size_t i = 0; nbytes / 4 >= 2 && i < nbytes / 4 - 2; i++) {
|
|
|
|
haystack_aligned[4 * i + 3] = 'y';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
haystack_aligned[nbytes - 1] = '\0';
|
|
|
|
needle_aligned[needle.size() - 1] = '\0';
|
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
if (strstr(haystack_aligned, needle_aligned) == nullptr) {
|
2017-08-10 01:52:19 +02:00
|
|
|
errx(1, "ERROR: strstr failed to find valid substring.");
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_string_strstr);
|
|
|
|
|
|
|
|
static void BM_string_strchr(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
|
|
|
const size_t haystack_alignment = state.range(1);
|
|
|
|
|
|
|
|
std::vector<char> haystack;
|
|
|
|
char* haystack_aligned = GetAlignedPtrFilled(&haystack, haystack_alignment, nbytes, 'x');
|
2017-08-10 01:52:19 +02:00
|
|
|
haystack_aligned[nbytes-1] = '\0';
|
2017-08-09 03:29:51 +02:00
|
|
|
|
|
|
|
while (state.KeepRunning()) {
|
|
|
|
if (strchr(haystack_aligned, 'y') != nullptr) {
|
2017-08-10 01:52:19 +02:00
|
|
|
errx(1, "ERROR: strchr found a chr where it should have failed.");
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK(BM_string_strchr);
|