blob: 7680b406c788dc8b6435cd2f9832b92c0a3ec2de [file] [log] [blame]
Anders Lewisac4f4b42017-08-08 18:29:51 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Anders Lewisa98a5fb2017-08-09 16:52:19 -070017#include <err.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070018#include <langinfo.h>
19#include <locale.h>
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070020#include <malloc.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070021#include <stdlib.h>
Christopher Ferris4fae7032018-09-20 15:03:49 -070022#include <unistd.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070023
24#include <benchmark/benchmark.h>
Christopher Ferrisb4e560e2023-10-26 17:00:00 -070025#include "ScopedDecayTimeRestorer.h"
Anders Lewisac4f4b42017-08-08 18:29:51 -070026#include "util.h"
27
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070028static void MallocFree(benchmark::State& state) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070029 const size_t nbytes = state.range(0);
Christopher Ferris4fae7032018-09-20 15:03:49 -070030 int pagesize = getpagesize();
Anders Lewisac4f4b42017-08-08 18:29:51 -070031
Christopher Ferris4fae7032018-09-20 15:03:49 -070032 for (auto _ : state) {
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070033 void* ptr;
34 benchmark::DoNotOptimize(ptr = malloc(nbytes));
35 MakeAllocationResident(ptr, nbytes, pagesize);
Christopher Ferris4fae7032018-09-20 15:03:49 -070036 free(ptr);
Anders Lewisac4f4b42017-08-08 18:29:51 -070037 }
38
39 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
40}
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070041
42static void BM_stdlib_malloc_free_default(benchmark::State& state) {
43#if defined(__BIONIC__)
Christopher Ferrisb4e560e2023-10-26 17:00:00 -070044 ScopedDecayTimeRestorer restorer;
45
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070046 // The default is expected to be a zero decay time.
47 mallopt(M_DECAY_TIME, 0);
48#endif
49
50 MallocFree(state);
51}
52BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_default, "AT_COMMON_SIZES");
53
54#if defined(__BIONIC__)
55static void BM_stdlib_malloc_free_decay1(benchmark::State& state) {
Christopher Ferrisb4e560e2023-10-26 17:00:00 -070056 ScopedDecayTimeRestorer restorer;
57
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070058 mallopt(M_DECAY_TIME, 1);
59
60 MallocFree(state);
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070061}
62BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_decay1, "AT_COMMON_SIZES");
63#endif
64
Christopher Ferris89379352020-01-29 15:21:09 -080065static void CallocFree(benchmark::State& state) {
66 const size_t nbytes = state.range(0);
67 int pagesize = getpagesize();
68
69 for (auto _ : state) {
70 void* ptr;
71 benchmark::DoNotOptimize(ptr = calloc(1, nbytes));
72 MakeAllocationResident(ptr, nbytes, pagesize);
73 free(ptr);
74 }
75
76 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
77}
78
79static void BM_stdlib_calloc_free_default(benchmark::State& state) {
80#if defined(__BIONIC__)
Christopher Ferrisb4e560e2023-10-26 17:00:00 -070081 ScopedDecayTimeRestorer restorer;
82
Christopher Ferris89379352020-01-29 15:21:09 -080083 // The default is expected to be a zero decay time.
84 mallopt(M_DECAY_TIME, 0);
85#endif
86
87 CallocFree(state);
88}
89BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_default, "AT_COMMON_SIZES");
90
91#if defined(__BIONIC__)
92static void BM_stdlib_calloc_free_decay1(benchmark::State& state) {
93 mallopt(M_DECAY_TIME, 1);
94
95 CallocFree(state);
96
97 mallopt(M_DECAY_TIME, 0);
98}
99BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_decay1, "AT_COMMON_SIZES");
100#endif
101
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700102static void MallocMultiple(benchmark::State& state, size_t nbytes, size_t numAllocs) {
103 int pagesize = getpagesize();
104 void* ptrs[numAllocs];
105 for (auto _ : state) {
106 for (size_t i = 0; i < numAllocs; i++) {
107 benchmark::DoNotOptimize(ptrs[i] = reinterpret_cast<uint8_t*>(malloc(nbytes)));
108 MakeAllocationResident(ptrs[i], nbytes, pagesize);
109 }
110 state.PauseTiming(); // Stop timers while freeing pointers.
111 for (size_t i = 0; i < numAllocs; i++) {
112 free(ptrs[i]);
113 }
114 state.ResumeTiming();
115 }
116
117 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes) * numAllocs);
118}
119
120void BM_stdlib_malloc_forty_default(benchmark::State& state) {
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700121#if defined(__BIONIC__)
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700122 ScopedDecayTimeRestorer restorer;
123
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700124 // The default is expected to be a zero decay time.
125 mallopt(M_DECAY_TIME, 0);
126#endif
127
128 MallocMultiple(state, state.range(0), 40);
129}
130BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_default, "AT_COMMON_SIZES");
131
132#if defined(__BIONIC__)
133void BM_stdlib_malloc_forty_decay1(benchmark::State& state) {
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700134 ScopedDecayTimeRestorer restorer;
135
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700136 mallopt(M_DECAY_TIME, 1);
137
138 MallocMultiple(state, state.range(0), 40);
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700139}
140BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_decay1, "AT_COMMON_SIZES");
141#endif
142
143void BM_stdlib_malloc_multiple_8192_allocs_default(benchmark::State& state) {
144#if defined(__BIONIC__)
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700145 ScopedDecayTimeRestorer restorer;
146
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700147 // The default is expected to be a zero decay time.
148 mallopt(M_DECAY_TIME, 0);
149#endif
150
151 MallocMultiple(state, 8192, state.range(0));
152}
153BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_default, "AT_SMALL_SIZES");
154
155#if defined(__BIONIC__)
156void BM_stdlib_malloc_multiple_8192_allocs_decay1(benchmark::State& state) {
Christopher Ferrisb4e560e2023-10-26 17:00:00 -0700157 ScopedDecayTimeRestorer restorer;
158
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700159 mallopt(M_DECAY_TIME, 1);
160
161 MallocMultiple(state, 8192, state.range(0));
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -0700162}
163BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_decay1, "AT_SMALL_SIZES");
164#endif
Anders Lewisac4f4b42017-08-08 18:29:51 -0700165
Elliott Hughes09bf4322021-11-16 10:51:12 -0800166static void BM_stdlib_mbstowcs_ascii(benchmark::State& state) {
167 // It doesn't really matter what ASCII character we pick.
168 // The flow through the fast path is the same regardless.
169 const size_t count = 500000;
170 std::vector<char> mbs(count, 'e');
171 std::vector<wchar_t> wcs(count);
Anders Lewisac4f4b42017-08-08 18:29:51 -0700172
Christopher Ferris4fae7032018-09-20 15:03:49 -0700173 for (auto _ : state) {
Elliott Hughes09bf4322021-11-16 10:51:12 -0800174 benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size()));
Anders Lewisac4f4b42017-08-08 18:29:51 -0700175 }
176
Elliott Hughes09bf4322021-11-16 10:51:12 -0800177 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size()));
Anders Lewisac4f4b42017-08-08 18:29:51 -0700178}
Elliott Hughes09bf4322021-11-16 10:51:12 -0800179BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_ascii, "");
Anders Lewisac4f4b42017-08-08 18:29:51 -0700180
Elliott Hughes09bf4322021-11-16 10:51:12 -0800181static void BM_stdlib_mbstowcs_wide(benchmark::State& state) {
182 // It doesn't matter much what wide character we pick.
183 // A three-byte character seems pretty representative, and all three byte
184 // characters are the same from the code's perspective.
185 const size_t count = 500000;
186 std::string mbs;
187 for (size_t i = 0; i < count; i++) {
188 mbs += "\xe5\xb1\xb1";
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700189 }
Elliott Hughes09bf4322021-11-16 10:51:12 -0800190 std::vector<wchar_t> wcs(count);
Anders Lewisac4f4b42017-08-08 18:29:51 -0700191
Christopher Ferris4fae7032018-09-20 15:03:49 -0700192 for (auto _ : state) {
Elliott Hughes09bf4322021-11-16 10:51:12 -0800193 benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size()));
Anders Lewisac4f4b42017-08-08 18:29:51 -0700194 }
195
Elliott Hughes09bf4322021-11-16 10:51:12 -0800196 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size()));
Anders Lewisac4f4b42017-08-08 18:29:51 -0700197}
Elliott Hughes09bf4322021-11-16 10:51:12 -0800198BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_wide, "");
199
200static void BM_stdlib_mbrtowc_1(benchmark::State& state) {
201 wchar_t wc;
202 for (auto _ : state) {
203 benchmark::DoNotOptimize(mbrtowc(&wc, "e", 1, nullptr));
204 }
205}
206BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_1, "");
207
208static void BM_stdlib_mbrtowc_2(benchmark::State& state) {
209 wchar_t wc;
210 for (auto _ : state) {
211 benchmark::DoNotOptimize(mbrtowc(&wc, "\xc3\x9f", 3, nullptr));
212 }
213}
214BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_2, "");
215
216static void BM_stdlib_mbrtowc_3(benchmark::State& state) {
217 wchar_t wc;
218 for (auto _ : state) {
219 benchmark::DoNotOptimize(mbrtowc(&wc, "\xe5\xb1\xb1", 3, nullptr));
220 }
221}
222BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_3, "");
223
224static void BM_stdlib_mbrtowc_4(benchmark::State& state) {
225 wchar_t wc;
226 for (auto _ : state) {
227 benchmark::DoNotOptimize(mbrtowc(&wc, "\xf0\xa4\xad\xa2", 4, nullptr));
228 }
229}
230BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_4, "");
Elliott Hughes7063a832017-12-19 08:55:40 -0800231
Elliott Hughes96705e32019-09-26 07:42:23 -0700232BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atoi, atoi(" -123"));
233BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atol, atol(" -123"));
234BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol, strtol(" -123", nullptr, 0));
235BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoll, strtoll(" -123", nullptr, 0));
236BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul, strtoul(" -123", nullptr, 0));
237BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoull, strtoull(" -123", nullptr, 0));
Elliott Hughes4e620552023-12-11 16:57:03 -0800238
239BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol_hex, strtol("0xdeadbeef", nullptr, 0));
240BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul_hex, strtoul("0xdeadbeef", nullptr, 0));