2017-08-09 03:29:51 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 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>
|
2017-08-09 03:29:51 +02:00
|
|
|
#include <langinfo.h>
|
|
|
|
#include <locale.h>
|
2019-04-05 21:47:39 +02:00
|
|
|
#include <malloc.h>
|
2017-08-09 03:29:51 +02:00
|
|
|
#include <stdlib.h>
|
2018-09-21 00:03:49 +02:00
|
|
|
#include <unistd.h>
|
2017-08-09 03:29:51 +02:00
|
|
|
|
|
|
|
#include <benchmark/benchmark.h>
|
2023-10-27 02:00:00 +02:00
|
|
|
#include "ScopedDecayTimeRestorer.h"
|
2017-08-09 03:29:51 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
static void MallocFree(benchmark::State& state) {
|
2017-08-09 03:29:51 +02:00
|
|
|
const size_t nbytes = state.range(0);
|
2018-09-21 00:03:49 +02:00
|
|
|
int pagesize = getpagesize();
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
2019-04-05 21:47:39 +02:00
|
|
|
void* ptr;
|
|
|
|
benchmark::DoNotOptimize(ptr = malloc(nbytes));
|
|
|
|
MakeAllocationResident(ptr, nbytes, pagesize);
|
2018-09-21 00:03:49 +02:00
|
|
|
free(ptr);
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
2019-04-05 21:47:39 +02:00
|
|
|
|
|
|
|
static void BM_stdlib_malloc_free_default(benchmark::State& state) {
|
|
|
|
#if defined(__BIONIC__)
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
// The default is expected to be a zero decay time.
|
|
|
|
mallopt(M_DECAY_TIME, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MallocFree(state);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_default, "AT_COMMON_SIZES");
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
static void BM_stdlib_malloc_free_decay1(benchmark::State& state) {
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
mallopt(M_DECAY_TIME, 1);
|
|
|
|
|
|
|
|
MallocFree(state);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_decay1, "AT_COMMON_SIZES");
|
|
|
|
#endif
|
|
|
|
|
2020-01-30 00:21:09 +01:00
|
|
|
static void CallocFree(benchmark::State& state) {
|
|
|
|
const size_t nbytes = state.range(0);
|
|
|
|
int pagesize = getpagesize();
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
void* ptr;
|
|
|
|
benchmark::DoNotOptimize(ptr = calloc(1, nbytes));
|
|
|
|
MakeAllocationResident(ptr, nbytes, pagesize);
|
|
|
|
free(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BM_stdlib_calloc_free_default(benchmark::State& state) {
|
|
|
|
#if defined(__BIONIC__)
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2020-01-30 00:21:09 +01:00
|
|
|
// The default is expected to be a zero decay time.
|
|
|
|
mallopt(M_DECAY_TIME, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CallocFree(state);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_default, "AT_COMMON_SIZES");
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
static void BM_stdlib_calloc_free_decay1(benchmark::State& state) {
|
|
|
|
mallopt(M_DECAY_TIME, 1);
|
|
|
|
|
|
|
|
CallocFree(state);
|
|
|
|
|
|
|
|
mallopt(M_DECAY_TIME, 0);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_decay1, "AT_COMMON_SIZES");
|
|
|
|
#endif
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
static void MallocMultiple(benchmark::State& state, size_t nbytes, size_t numAllocs) {
|
|
|
|
int pagesize = getpagesize();
|
|
|
|
void* ptrs[numAllocs];
|
|
|
|
for (auto _ : state) {
|
|
|
|
for (size_t i = 0; i < numAllocs; i++) {
|
|
|
|
benchmark::DoNotOptimize(ptrs[i] = reinterpret_cast<uint8_t*>(malloc(nbytes)));
|
|
|
|
MakeAllocationResident(ptrs[i], nbytes, pagesize);
|
|
|
|
}
|
|
|
|
state.PauseTiming(); // Stop timers while freeing pointers.
|
|
|
|
for (size_t i = 0; i < numAllocs; i++) {
|
|
|
|
free(ptrs[i]);
|
|
|
|
}
|
|
|
|
state.ResumeTiming();
|
|
|
|
}
|
|
|
|
|
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes) * numAllocs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BM_stdlib_malloc_forty_default(benchmark::State& state) {
|
|
|
|
#if defined(__BIONIC__)
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
// The default is expected to be a zero decay time.
|
|
|
|
mallopt(M_DECAY_TIME, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MallocMultiple(state, state.range(0), 40);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_default, "AT_COMMON_SIZES");
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
void BM_stdlib_malloc_forty_decay1(benchmark::State& state) {
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
mallopt(M_DECAY_TIME, 1);
|
|
|
|
|
|
|
|
MallocMultiple(state, state.range(0), 40);
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_decay1, "AT_COMMON_SIZES");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void BM_stdlib_malloc_multiple_8192_allocs_default(benchmark::State& state) {
|
|
|
|
#if defined(__BIONIC__)
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
// The default is expected to be a zero decay time.
|
|
|
|
mallopt(M_DECAY_TIME, 0);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
MallocMultiple(state, 8192, state.range(0));
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_default, "AT_SMALL_SIZES");
|
|
|
|
|
|
|
|
#if defined(__BIONIC__)
|
|
|
|
void BM_stdlib_malloc_multiple_8192_allocs_decay1(benchmark::State& state) {
|
2023-10-27 02:00:00 +02:00
|
|
|
ScopedDecayTimeRestorer restorer;
|
|
|
|
|
2019-04-05 21:47:39 +02:00
|
|
|
mallopt(M_DECAY_TIME, 1);
|
|
|
|
|
|
|
|
MallocMultiple(state, 8192, state.range(0));
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_decay1, "AT_SMALL_SIZES");
|
|
|
|
#endif
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
static void BM_stdlib_mbstowcs_ascii(benchmark::State& state) {
|
|
|
|
// It doesn't really matter what ASCII character we pick.
|
|
|
|
// The flow through the fast path is the same regardless.
|
|
|
|
const size_t count = 500000;
|
|
|
|
std::vector<char> mbs(count, 'e');
|
|
|
|
std::vector<wchar_t> wcs(count);
|
|
|
|
|
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size()));
|
2017-08-10 01:52:19 +02:00
|
|
|
}
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size()));
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_ascii, "");
|
|
|
|
|
|
|
|
static void BM_stdlib_mbstowcs_wide(benchmark::State& state) {
|
|
|
|
// It doesn't matter much what wide character we pick.
|
|
|
|
// A three-byte character seems pretty representative, and all three byte
|
|
|
|
// characters are the same from the code's perspective.
|
|
|
|
const size_t count = 500000;
|
|
|
|
std::string mbs;
|
|
|
|
for (size_t i = 0; i < count; i++) {
|
|
|
|
mbs += "\xe5\xb1\xb1";
|
|
|
|
}
|
|
|
|
std::vector<wchar_t> wcs(count);
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2018-09-21 00:03:49 +02:00
|
|
|
for (auto _ : state) {
|
2021-11-16 19:51:12 +01:00
|
|
|
benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size()));
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size()));
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
2021-11-16 19:51:12 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_wide, "");
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
static void BM_stdlib_mbrtowc_1(benchmark::State& state) {
|
|
|
|
wchar_t wc;
|
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(mbrtowc(&wc, "e", 1, nullptr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_1, "");
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
static void BM_stdlib_mbrtowc_2(benchmark::State& state) {
|
|
|
|
wchar_t wc;
|
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(mbrtowc(&wc, "\xc3\x9f", 3, nullptr));
|
2017-08-10 01:52:19 +02:00
|
|
|
}
|
2021-11-16 19:51:12 +01:00
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_2, "");
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
static void BM_stdlib_mbrtowc_3(benchmark::State& state) {
|
|
|
|
wchar_t wc;
|
2018-09-21 00:03:49 +02:00
|
|
|
for (auto _ : state) {
|
2021-11-16 19:51:12 +01:00
|
|
|
benchmark::DoNotOptimize(mbrtowc(&wc, "\xe5\xb1\xb1", 3, nullptr));
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
2021-11-16 19:51:12 +01:00
|
|
|
}
|
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_3, "");
|
2017-08-09 03:29:51 +02:00
|
|
|
|
2021-11-16 19:51:12 +01:00
|
|
|
static void BM_stdlib_mbrtowc_4(benchmark::State& state) {
|
|
|
|
wchar_t wc;
|
|
|
|
for (auto _ : state) {
|
|
|
|
benchmark::DoNotOptimize(mbrtowc(&wc, "\xf0\xa4\xad\xa2", 4, nullptr));
|
|
|
|
}
|
2017-08-09 03:29:51 +02:00
|
|
|
}
|
2021-11-16 19:51:12 +01:00
|
|
|
BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_4, "");
|
2017-12-19 17:55:40 +01:00
|
|
|
|
2019-09-26 16:42:23 +02:00
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atoi, atoi(" -123"));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atol, atol(" -123"));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol, strtol(" -123", nullptr, 0));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoll, strtoll(" -123", nullptr, 0));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul, strtoul(" -123", nullptr, 0));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoull, strtoull(" -123", nullptr, 0));
|
2023-12-12 01:57:03 +01:00
|
|
|
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol_hex, strtol("0xdeadbeef", nullptr, 0));
|
|
|
|
BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul_hex, strtoul("0xdeadbeef", nullptr, 0));
|