| 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> | 
|  | 20 | #include <stdlib.h> | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 21 | #include <unistd.h> | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 22 |  | 
|  | 23 | #include <benchmark/benchmark.h> | 
|  | 24 | #include "util.h" | 
|  | 25 |  | 
|  | 26 | static void BM_stdlib_malloc_free(benchmark::State& state) { | 
|  | 27 | const size_t nbytes = state.range(0); | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 28 | int pagesize = getpagesize(); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 29 |  | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 30 | 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 Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 39 | } | 
|  | 40 |  | 
|  | 41 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(nbytes)); | 
|  | 42 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 43 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_malloc_free, "AT_COMMON_SIZES"); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | static 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 Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 58 | if (strcmp(nl_langinfo(CODESET), "UTF-8")) { | 
|  | 59 | errx(1, "ERROR: unable to set locale in BM_stdlib_mbstowcs"); | 
|  | 60 | } | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 61 |  | 
|  | 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 Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 80 | for (auto _ : state) { | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 81 | c = mbstowcs(widebuf_aligned, buf_aligned, 500000); | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000)); | 
|  | 85 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 86 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbstowcs, "0 0"); | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 87 |  | 
|  | 88 | static 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 Lewis | a98a5fb | 2017-08-09 16:52:19 -0700 | [diff] [blame] | 99 | if (strcmp(nl_langinfo(CODESET), "UTF-8")) { | 
|  | 100 | errx(1, "ERROR: unable to set locale in BM_stdlib_mbrtowc"); | 
|  | 101 | } | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 102 |  | 
|  | 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 Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 120 | for (auto _ : state) { | 
| Yi Kong | 32bc0fc | 2018-08-02 17:31:13 -0700 | [diff] [blame] | 121 | for (j = 0; buf_aligned[j]; j+=mbrtowc(&wc, buf_aligned + j, 4, nullptr)) { | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 122 | } | 
|  | 123 | } | 
|  | 124 |  | 
|  | 125 | state.SetBytesProcessed(uint64_t(state.iterations()) * uint64_t(500000)); | 
|  | 126 | } | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 127 | BIONIC_BENCHMARK_WITH_ARG(BM_stdlib_mbrtowc, "0"); | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 128 |  | 
|  | 129 | void BM_stdlib_atoi(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 130 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 131 | benchmark::DoNotOptimize(atoi(" -123")); | 
|  | 132 | } | 
|  | 133 | } | 
|  | 134 | BIONIC_BENCHMARK(BM_stdlib_atoi); | 
|  | 135 |  | 
|  | 136 | void BM_stdlib_atol(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 137 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 138 | benchmark::DoNotOptimize(atol(" -123")); | 
|  | 139 | } | 
|  | 140 | } | 
|  | 141 | BIONIC_BENCHMARK(BM_stdlib_atol); | 
|  | 142 |  | 
|  | 143 | void BM_stdlib_strtol(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 144 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 145 | benchmark::DoNotOptimize(strtol(" -123", nullptr, 0)); | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 | BIONIC_BENCHMARK(BM_stdlib_strtol); | 
|  | 149 |  | 
|  | 150 | void BM_stdlib_strtoll(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 151 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 152 | benchmark::DoNotOptimize(strtoll(" -123", nullptr, 0)); | 
|  | 153 | } | 
|  | 154 | } | 
|  | 155 | BIONIC_BENCHMARK(BM_stdlib_strtoll); | 
|  | 156 |  | 
|  | 157 | void BM_stdlib_strtoul(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 158 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 159 | benchmark::DoNotOptimize(strtoul(" -123", nullptr, 0)); | 
|  | 160 | } | 
|  | 161 | } | 
|  | 162 | BIONIC_BENCHMARK(BM_stdlib_strtoul); | 
|  | 163 |  | 
|  | 164 | void BM_stdlib_strtoull(benchmark::State& state) { | 
| Christopher Ferris | 4fae703 | 2018-09-20 15:03:49 -0700 | [diff] [blame] | 165 | for (auto _ : state) { | 
| Elliott Hughes | 7063a83 | 2017-12-19 08:55:40 -0800 | [diff] [blame] | 166 | benchmark::DoNotOptimize(strtoull(" -123", nullptr, 0)); | 
|  | 167 | } | 
|  | 168 | } | 
|  | 169 | BIONIC_BENCHMARK(BM_stdlib_strtoull); |