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