| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 1 | /* | 
|  | 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 Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 17 | #include <err.h> | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 18 | #include <langinfo.h> | 
|  | 19 | #include <locale.h> | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 20 | #include <malloc.h> | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 21 | #include <stdlib.h> | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 22 | #include <unistd.h> | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 23 |  | 
|  | 24 | #include <benchmark/benchmark.h> | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 25 | #include "ScopedDecayTimeRestorer.h" | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 26 | #include "util.h" | 
|  | 27 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 28 | static void MallocFree(benchmark::State& state) { | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 29 | const size_t nbytes = state.range(0); | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 30 | int pagesize = getpagesize(); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 31 |  | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 32 | for (auto _ : state) { | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 33 | void* ptr; | 
|  | 34 | benchmark::DoNotOptimize(ptr = malloc(nbytes)); | 
|  | 35 | MakeAllocationResident(ptr, nbytes, pagesize); | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 36 | free(ptr); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 37 | } | 
|  | 38 |  | 
|  | 39 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 40 | } | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 41 |  | 
|  | 42 | static void BM_stdlib_malloc_free_default(benchmark::State& state) { | 
|  | 43 | #if defined(__BIONIC__) | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 44 | ScopedDecayTimeRestorer restorer; | 
|  | 45 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 46 | // The default is expected to be a zero decay time. | 
|  | 47 | mallopt(M_DECAY_TIME, 0); | 
|  | 48 | #endif | 
|  | 49 |  | 
|  | 50 | MallocFree(state); | 
|  | 51 | } | 
|  | 52 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_default, "AT_COMMON_SIZES"); | 
|  | 53 |  | 
|  | 54 | #if defined(__BIONIC__) | 
|  | 55 | static void BM_stdlib_malloc_free_decay1(benchmark::State& state) { | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 56 | ScopedDecayTimeRestorer restorer; | 
|  | 57 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 58 | mallopt(M_DECAY_TIME, 1); | 
|  | 59 |  | 
|  | 60 | MallocFree(state); | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 61 | } | 
|  | 62 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_decay1, "AT_COMMON_SIZES"); | 
|  | 63 | #endif | 
|  | 64 |  | 
| Christopher Ferris | 8937935 | 2020-01-29 15:21:09 -0800 | [diff] [blame] | 65 | static 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 |  | 
|  | 79 | static void BM_stdlib_calloc_free_default(benchmark::State& state) { | 
|  | 80 | #if defined(__BIONIC__) | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 81 | ScopedDecayTimeRestorer restorer; | 
|  | 82 |  | 
| Christopher Ferris | 8937935 | 2020-01-29 15:21:09 -0800 | [diff] [blame] | 83 | // The default is expected to be a zero decay time. | 
|  | 84 | mallopt(M_DECAY_TIME, 0); | 
|  | 85 | #endif | 
|  | 86 |  | 
|  | 87 | CallocFree(state); | 
|  | 88 | } | 
|  | 89 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_default, "AT_COMMON_SIZES"); | 
|  | 90 |  | 
|  | 91 | #if defined(__BIONIC__) | 
|  | 92 | static 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 | } | 
|  | 99 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_calloc_free_decay1, "AT_COMMON_SIZES"); | 
|  | 100 | #endif | 
|  | 101 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 102 | static 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 |  | 
|  | 120 | void BM_stdlib_malloc_forty_default(benchmark::State& state) { | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 121 | #if defined(__BIONIC__) | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 122 | ScopedDecayTimeRestorer restorer; | 
|  | 123 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 124 | // 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 | } | 
|  | 130 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_default, "AT_COMMON_SIZES"); | 
|  | 131 |  | 
|  | 132 | #if defined(__BIONIC__) | 
|  | 133 | void BM_stdlib_malloc_forty_decay1(benchmark::State& state) { | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 134 | ScopedDecayTimeRestorer restorer; | 
|  | 135 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 136 | mallopt(M_DECAY_TIME, 1); | 
|  | 137 |  | 
|  | 138 | MallocMultiple(state, state.range(0), 40); | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 139 | } | 
|  | 140 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_decay1, "AT_COMMON_SIZES"); | 
|  | 141 | #endif | 
|  | 142 |  | 
|  | 143 | void BM_stdlib_malloc_multiple_8192_allocs_default(benchmark::State& state) { | 
|  | 144 | #if defined(__BIONIC__) | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 145 | ScopedDecayTimeRestorer restorer; | 
|  | 146 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 147 | // 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 | } | 
|  | 153 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_default, "AT_SMALL_SIZES"); | 
|  | 154 |  | 
|  | 155 | #if defined(__BIONIC__) | 
|  | 156 | void BM_stdlib_malloc_multiple_8192_allocs_decay1(benchmark::State& state) { | 
| Christopher Ferris | b4e560e | 2023-10-26 17:00:00 -0700 | [diff] [blame] | 157 | ScopedDecayTimeRestorer restorer; | 
|  | 158 |  | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 159 | mallopt(M_DECAY_TIME, 1); | 
|  | 160 |  | 
|  | 161 | MallocMultiple(state, 8192, state.range(0)); | 
| Christopher Ferris | 7ec2c8a | 2019-04-05 12:47:39 -0700 | [diff] [blame] | 162 | } | 
|  | 163 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_decay1, "AT_SMALL_SIZES"); | 
|  | 164 | #endif | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 165 |  | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 166 | static 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 Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 172 |  | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 173 | for (auto _ : state) { | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 174 | benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size())); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 175 | } | 
|  | 176 |  | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 177 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size())); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 178 | } | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 179 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_ascii, ""); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 180 |  | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 181 | static 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 Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 189 | } | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 190 | std::vector<wchar_t> wcs(count); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 191 |  | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 192 | for (auto _ : state) { | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 193 | benchmark::DoNotOptimize(mbstowcs(&wcs[0], &mbs[0], wcs.size())); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 194 | } | 
|  | 195 |  | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 196 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(wcs.size())); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 197 | } | 
| Elliott Hughes | 09bf432 | 2021-11-16 10:51:12 -0800 | [diff] [blame] | 198 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs_wide, ""); | 
|  | 199 |  | 
|  | 200 | static 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 | } | 
|  | 206 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_1, ""); | 
|  | 207 |  | 
|  | 208 | static 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 | } | 
|  | 214 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_2, ""); | 
|  | 215 |  | 
|  | 216 | static 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 | } | 
|  | 222 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_3, ""); | 
|  | 223 |  | 
|  | 224 | static 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 | } | 
|  | 230 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc_4, ""); | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 231 |  | 
| Elliott Hughes | 96705e3 | 2019-09-26 07:42:23 -0700 | [diff] [blame] | 232 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atoi, atoi(" -123")); | 
|  | 233 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atol, atol(" -123")); | 
|  | 234 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol, strtol(" -123", nullptr, 0)); | 
|  | 235 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoll, strtoll(" -123", nullptr, 0)); | 
|  | 236 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul, strtoul(" -123", nullptr, 0)); | 
|  | 237 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoull, strtoull(" -123", nullptr, 0)); | 
| Elliott Hughes | 4e62055 | 2023-12-11 16:57:03 -0800 | [diff] [blame] | 238 |  | 
|  | 239 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol_hex, strtol("0xdeadbeef", nullptr, 0)); | 
|  | 240 | BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul_hex, strtoul("0xdeadbeef", nullptr, 0)); |