blob: 61b51fa54538dc36af5fbcafc0382e6f8ba62a46 [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>
25#include "util.h"
26
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070027static void MallocFree(benchmark::State& state) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070028 const size_t nbytes = state.range(0);
Christopher Ferris4fae7032018-09-20 15:03:49 -070029 int pagesize = getpagesize();
Anders Lewisac4f4b42017-08-08 18:29:51 -070030
Christopher Ferris4fae7032018-09-20 15:03:49 -070031 for (auto _ : state) {
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070032 void* ptr;
33 benchmark::DoNotOptimize(ptr = malloc(nbytes));
34 MakeAllocationResident(ptr, nbytes, pagesize);
Christopher Ferris4fae7032018-09-20 15:03:49 -070035 free(ptr);
Anders Lewisac4f4b42017-08-08 18:29:51 -070036 }
37
38 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
39}
Christopher Ferris7ec2c8a2019-04-05 12:47:39 -070040
41static void BM_stdlib_malloc_free_default(benchmark::State& state) {
42#if defined(__BIONIC__)
43 // The default is expected to be a zero decay time.
44 mallopt(M_DECAY_TIME, 0);
45#endif
46
47 MallocFree(state);
48}
49BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_default, "AT_COMMON_SIZES");
50
51#if defined(__BIONIC__)
52static void BM_stdlib_malloc_free_decay1(benchmark::State& state) {
53 mallopt(M_DECAY_TIME, 1);
54
55 MallocFree(state);
56
57 mallopt(M_DECAY_TIME, 0);
58}
59BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free_decay1, "AT_COMMON_SIZES");
60#endif
61
62static void MallocMultiple(benchmark::State& state, size_t nbytes, size_t numAllocs) {
63 int pagesize = getpagesize();
64 void* ptrs[numAllocs];
65 for (auto _ : state) {
66 for (size_t i = 0; i < numAllocs; i++) {
67 benchmark::DoNotOptimize(ptrs[i] = reinterpret_cast<uint8_t*>(malloc(nbytes)));
68 MakeAllocationResident(ptrs[i], nbytes, pagesize);
69 }
70 state.PauseTiming(); // Stop timers while freeing pointers.
71 for (size_t i = 0; i < numAllocs; i++) {
72 free(ptrs[i]);
73 }
74 state.ResumeTiming();
75 }
76
77 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes) * numAllocs);
78}
79
80void BM_stdlib_malloc_forty_default(benchmark::State& state) {
81
82#if defined(__BIONIC__)
83 // The default is expected to be a zero decay time.
84 mallopt(M_DECAY_TIME, 0);
85#endif
86
87 MallocMultiple(state, state.range(0), 40);
88}
89BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_default, "AT_COMMON_SIZES");
90
91#if defined(__BIONIC__)
92void BM_stdlib_malloc_forty_decay1(benchmark::State& state) {
93 mallopt(M_DECAY_TIME, 1);
94
95 MallocMultiple(state, state.range(0), 40);
96
97 mallopt(M_DECAY_TIME, 0);
98}
99BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_forty_decay1, "AT_COMMON_SIZES");
100#endif
101
102void BM_stdlib_malloc_multiple_8192_allocs_default(benchmark::State& state) {
103#if defined(__BIONIC__)
104 // The default is expected to be a zero decay time.
105 mallopt(M_DECAY_TIME, 0);
106#endif
107
108 MallocMultiple(state, 8192, state.range(0));
109}
110BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_default, "AT_SMALL_SIZES");
111
112#if defined(__BIONIC__)
113void BM_stdlib_malloc_multiple_8192_allocs_decay1(benchmark::State& state) {
114 mallopt(M_DECAY_TIME, 1);
115
116 MallocMultiple(state, 8192, state.range(0));
117
118 mallopt(M_DECAY_TIME, 0);
119}
120BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_multiple_8192_allocs_decay1, "AT_SMALL_SIZES");
121#endif
Anders Lewisac4f4b42017-08-08 18:29:51 -0700122
123static void BM_stdlib_mbstowcs(benchmark::State& state) {
124 const size_t buf_alignment = state.range(0);
125 const size_t widebuf_alignment = state.range(1);
126
127 std::vector<char> buf;
128 std::vector<wchar_t> widebuf;
129
130 setlocale(LC_CTYPE, "C.UTF-8")
131 || setlocale(LC_CTYPE, "en_US.UTF-8")
132 || setlocale(LC_CTYPE, "en_GB.UTF-8")
133 || setlocale(LC_CTYPE, "en.UTF-8")
134 || setlocale(LC_CTYPE, "de_DE-8")
135 || setlocale(LC_CTYPE, "fr_FR-8");
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700136 if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
137 errx(1, "ERROR: unable to set locale in BM_stdlib_mbstowcs");
138 }
Anders Lewisac4f4b42017-08-08 18:29:51 -0700139
140 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
141 wchar_t* widebuf_aligned = GetAlignedPtr(&widebuf, widebuf_alignment, 500000);
142 size_t i, j, k, l;
143 l = 0;
144 for (i=0xc3; i<0xe0; i++)
145 for (j=0x80; j<0xc0; j++)
146 buf[l++] = i, buf[l++] = j;
147 for (i=0xe1; i<0xed; i++)
148 for (j=0x80; j<0xc0; j++)
149 for (k=0x80; k<0xc0; k++)
150 buf[l++] = i, buf[l++] = j, buf[l++] = k;
151 for (i=0xf1; i<0xf4; i++)
152 for (j=0x80; j<0xc0; j++)
153 for (k=0x80; k<0xc0; k++)
154 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
155 buf[l++] = 0;
156
157 volatile size_t c __attribute__((unused)) = 0;
Christopher Ferris4fae7032018-09-20 15:03:49 -0700158 for (auto _ : state) {
Anders Lewisac4f4b42017-08-08 18:29:51 -0700159 c = mbstowcs(widebuf_aligned, buf_aligned, 500000);
160 }
161
162 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
163}
Christopher Ferris858e3362017-11-30 08:53:15 -0800164BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs, "0 0");
Anders Lewisac4f4b42017-08-08 18:29:51 -0700165
166static void BM_stdlib_mbrtowc(benchmark::State& state) {
167 const size_t buf_alignment = state.range(0);
168
169 std::vector<char> buf;
170
171 setlocale(LC_CTYPE, "C.UTF-8")
172 || setlocale(LC_CTYPE, "en_US.UTF-8")
173 || setlocale(LC_CTYPE, "en_GB.UTF-8")
174 || setlocale(LC_CTYPE, "en.UTF-8")
175 || setlocale(LC_CTYPE, "de_DE-8")
176 || setlocale(LC_CTYPE, "fr_FR-8");
Anders Lewisa98a5fb2017-08-09 16:52:19 -0700177 if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
178 errx(1, "ERROR: unable to set locale in BM_stdlib_mbrtowc");
179 }
Anders Lewisac4f4b42017-08-08 18:29:51 -0700180
181 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
182 size_t i, j, k, l;
183 l = 0;
184 for (i=0xc3; i<0xe0; i++)
185 for (j=0x80; j<0xc0; j++)
186 buf[l++] = i, buf[l++] = j;
187 for (i=0xe1; i<0xed; i++)
188 for (j=0x80; j<0xc0; j++)
189 for (k=0x80; k<0xc0; k++)
190 buf[l++] = i, buf[l++] = j, buf[l++] = k;
191 for (i=0xf1; i<0xf4; i++)
192 for (j=0x80; j<0xc0; j++)
193 for (k=0x80; k<0xc0; k++)
194 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
195 buf[l++] = 0;
196
197 wchar_t wc = 0;
Christopher Ferris4fae7032018-09-20 15:03:49 -0700198 for (auto _ : state) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700199 for (j = 0; buf_aligned[j]; j+=mbrtowc(&wc, buf_aligned + j, 4, nullptr)) {
Anders Lewisac4f4b42017-08-08 18:29:51 -0700200 }
201 }
202
203 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
204}
Christopher Ferris858e3362017-11-30 08:53:15 -0800205BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0");
Elliott Hughes7063a832017-12-19 08:55:40 -0800206
Elliott Hughes96705e32019-09-26 07:42:23 -0700207BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atoi, atoi(" -123"));
208BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_atol, atol(" -123"));
209BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtol, strtol(" -123", nullptr, 0));
210BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoll, strtoll(" -123", nullptr, 0));
211BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoul, strtoul(" -123", nullptr, 0));
212BIONIC_TRIVIAL_BENCHMARK(BM_stdlib_strtoull, strtoull(" -123", nullptr, 0));