blob: 7330dc42349c3e520d4da06d9621e9104911c72f [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>
20#include <stdlib.h>
Christopher Ferris4fae7032018-09-20 15:03:49 -070021#include <unistd.h>
Anders Lewisac4f4b42017-08-08 18:29:51 -070022
23#include <benchmark/benchmark.h>
24#include "util.h"
25
26static void BM_stdlib_malloc_free(benchmark::State& state) {
27 const size_t nbytes = state.range(0);
Christopher Ferris4fae7032018-09-20 15:03:49 -070028 int pagesize = getpagesize();
Anders Lewisac4f4b42017-08-08 18:29:51 -070029
Christopher Ferris4fae7032018-09-20 15:03:49 -070030 void* ptr;
31 for (auto _ : state) {
32 ptr = malloc(nbytes);
33 // Make the entire allocation resident.
34 uint8_t* data = reinterpret_cast<uint8_t*>(ptr);
35 for (size_t i = 0; i < nbytes; i += pagesize) {
36 data[i] = 1;
37 }
38 free(ptr);
Anders Lewisac4f4b42017-08-08 18:29:51 -070039 }
40
41 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes));
42}
Christopher Ferris858e3362017-11-30 08:53:15 -080043BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free, "AT_COMMON_SIZES");
Anders Lewisac4f4b42017-08-08 18:29:51 -070044
45static void BM_stdlib_mbstowcs(benchmark::State& state) {
46 const size_t buf_alignment = state.range(0);
47 const size_t widebuf_alignment = state.range(1);
48
49 std::vector<char> buf;
50 std::vector<wchar_t> widebuf;
51
52 setlocale(LC_CTYPE, "C.UTF-8")
53 || setlocale(LC_CTYPE, "en_US.UTF-8")
54 || setlocale(LC_CTYPE, "en_GB.UTF-8")
55 || setlocale(LC_CTYPE, "en.UTF-8")
56 || setlocale(LC_CTYPE, "de_DE-8")
57 || setlocale(LC_CTYPE, "fr_FR-8");
Anders Lewisa98a5fb2017-08-09 16:52:19 -070058 if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
59 errx(1, "ERROR: unable to set locale in BM_stdlib_mbstowcs");
60 }
Anders Lewisac4f4b42017-08-08 18:29:51 -070061
62 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
63 wchar_t* widebuf_aligned = GetAlignedPtr(&widebuf, widebuf_alignment, 500000);
64 size_t i, j, k, l;
65 l = 0;
66 for (i=0xc3; i<0xe0; i++)
67 for (j=0x80; j<0xc0; j++)
68 buf[l++] = i, buf[l++] = j;
69 for (i=0xe1; i<0xed; i++)
70 for (j=0x80; j<0xc0; j++)
71 for (k=0x80; k<0xc0; k++)
72 buf[l++] = i, buf[l++] = j, buf[l++] = k;
73 for (i=0xf1; i<0xf4; i++)
74 for (j=0x80; j<0xc0; j++)
75 for (k=0x80; k<0xc0; k++)
76 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
77 buf[l++] = 0;
78
79 volatile size_t c __attribute__((unused)) = 0;
Christopher Ferris4fae7032018-09-20 15:03:49 -070080 for (auto _ : state) {
Anders Lewisac4f4b42017-08-08 18:29:51 -070081 c = mbstowcs(widebuf_aligned, buf_aligned, 500000);
82 }
83
84 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
85}
Christopher Ferris858e3362017-11-30 08:53:15 -080086BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs, "0 0");
Anders Lewisac4f4b42017-08-08 18:29:51 -070087
88static void BM_stdlib_mbrtowc(benchmark::State& state) {
89 const size_t buf_alignment = state.range(0);
90
91 std::vector<char> buf;
92
93 setlocale(LC_CTYPE, "C.UTF-8")
94 || setlocale(LC_CTYPE, "en_US.UTF-8")
95 || setlocale(LC_CTYPE, "en_GB.UTF-8")
96 || setlocale(LC_CTYPE, "en.UTF-8")
97 || setlocale(LC_CTYPE, "de_DE-8")
98 || setlocale(LC_CTYPE, "fr_FR-8");
Anders Lewisa98a5fb2017-08-09 16:52:19 -070099 if (strcmp(nl_langinfo(CODESET), "UTF-8")) {
100 errx(1, "ERROR: unable to set locale in BM_stdlib_mbrtowc");
101 }
Anders Lewisac4f4b42017-08-08 18:29:51 -0700102
103 char* buf_aligned = GetAlignedPtr(&buf, buf_alignment, 500000);
104 size_t i, j, k, l;
105 l = 0;
106 for (i=0xc3; i<0xe0; i++)
107 for (j=0x80; j<0xc0; j++)
108 buf[l++] = i, buf[l++] = j;
109 for (i=0xe1; i<0xed; i++)
110 for (j=0x80; j<0xc0; j++)
111 for (k=0x80; k<0xc0; k++)
112 buf[l++] = i, buf[l++] = j, buf[l++] = k;
113 for (i=0xf1; i<0xf4; i++)
114 for (j=0x80; j<0xc0; j++)
115 for (k=0x80; k<0xc0; k++)
116 buf[l++] = i, buf[l++] = j, buf[l++] = 0x80, buf[l++] = k;
117 buf[l++] = 0;
118
119 wchar_t wc = 0;
Christopher Ferris4fae7032018-09-20 15:03:49 -0700120 for (auto _ : state) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700121 for (j = 0; buf_aligned[j]; j+=mbrtowc(&wc, buf_aligned + j, 4, nullptr)) {
Anders Lewisac4f4b42017-08-08 18:29:51 -0700122 }
123 }
124
125 state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000));
126}
Christopher Ferris858e3362017-11-30 08:53:15 -0800127BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0");
Elliott Hughes7063a832017-12-19 08:55:40 -0800128
129void BM_stdlib_atoi(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700130 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800131 benchmark::DoNotOptimize(atoi(" -123"));
132 }
133}
134BIONIC_BENCHMARK(BM_stdlib_atoi);
135
136void BM_stdlib_atol(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700137 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800138 benchmark::DoNotOptimize(atol(" -123"));
139 }
140}
141BIONIC_BENCHMARK(BM_stdlib_atol);
142
143void BM_stdlib_strtol(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700144 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800145 benchmark::DoNotOptimize(strtol(" -123", nullptr, 0));
146 }
147}
148BIONIC_BENCHMARK(BM_stdlib_strtol);
149
150void BM_stdlib_strtoll(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700151 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800152 benchmark::DoNotOptimize(strtoll(" -123", nullptr, 0));
153 }
154}
155BIONIC_BENCHMARK(BM_stdlib_strtoll);
156
157void BM_stdlib_strtoul(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700158 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800159 benchmark::DoNotOptimize(strtoul(" -123", nullptr, 0));
160 }
161}
162BIONIC_BENCHMARK(BM_stdlib_strtoul);
163
164void BM_stdlib_strtoull(benchmark::State& state) {
Christopher Ferris4fae7032018-09-20 15:03:49 -0700165 for (auto _ : state) {
Elliott Hughes7063a832017-12-19 08:55:40 -0800166 benchmark::DoNotOptimize(strtoull(" -123", nullptr, 0));
167 }
168}
169BIONIC_BENCHMARK(BM_stdlib_strtoull);