blob: ef4892dac8a4c17e98eba3467eccaf02f6d92f81 [file] [log] [blame]
Anders Lewisf4447b92017-06-23 15:53:59 -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
Elliott Hughescbc80ba2018-02-13 14:26:29 -080017#pragma once
Anders Lewisf4447b92017-06-23 15:53:59 -070018
Anders Lewisa7b0f882017-07-24 20:01:13 -070019#include <map>
20#include <mutex>
21#include <string>
Christopher Ferris858e3362017-11-30 08:53:15 -080022#include <utility>
Anders Lewisf4447b92017-06-23 15:53:59 -070023#include <vector>
24
Anders Lewisa7b0f882017-07-24 20:01:13 -070025typedef void (*benchmark_func_t) (void);
26
27extern std::mutex g_map_lock;
28
Christopher Ferris858e3362017-11-30 08:53:15 -080029extern std::map<std::string, std::pair<benchmark_func_t, std::string>> g_str_to_func;
Anders Lewisa7b0f882017-07-24 20:01:13 -070030
Elliott Hughes5cec3772018-01-19 15:45:23 -080031static int __attribute__((unused)) EmplaceBenchmark(const std::string& fn_name, benchmark_func_t fn_ptr, const std::string& arg = "") {
Anders Lewisa7b0f882017-07-24 20:01:13 -070032 g_map_lock.lock();
Christopher Ferris858e3362017-11-30 08:53:15 -080033 g_str_to_func.emplace(std::string(fn_name), std::make_pair(fn_ptr, arg));
Anders Lewisa7b0f882017-07-24 20:01:13 -070034 g_map_lock.unlock();
35 return 0;
36}
37
38#define BIONIC_BENCHMARK(n) \
39 int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n))
40
Christopher Ferris858e3362017-11-30 08:53:15 -080041#define BIONIC_BENCHMARK_WITH_ARG(n, arg) \
42 int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n), arg)
43
Elliott Hughes96705e32019-09-26 07:42:23 -070044#define BIONIC_TRIVIAL_BENCHMARK(__name, __expression) \
45 static void __name(benchmark::State& state) { \
46 for (auto _ : state) { \
47 benchmark::DoNotOptimize(__expression); \
48 } \
49 } \
50 BIONIC_BENCHMARK(__name)
Christopher Ferris858e3362017-11-30 08:53:15 -080051
Anders Lewisa7b0f882017-07-24 20:01:13 -070052constexpr auto KB = 1024;
53
54typedef struct {
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070055 int cpu_to_lock = -1;
56 long num_iterations = 0;
Anders Lewisa7b0f882017-07-24 20:01:13 -070057 std::string xmlpath;
58 std::vector<std::string> extra_benchmarks;
59} bench_opts_t;
60
Anders Lewisf4447b92017-06-23 15:53:59 -070061// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
Anders Lewisa7b0f882017-07-24 20:01:13 -070062char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask);
Anders Lewisf4447b92017-06-23 15:53:59 -070063
Anders Lewisa7b0f882017-07-24 20:01:13 -070064char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes);
Anders Lewisf4447b92017-06-23 15:53:59 -070065
Anders Lewisac4f4b42017-08-08 18:29:51 -070066wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes);
67
Anders Lewisa7b0f882017-07-24 20:01:13 -070068char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte);
Anders Lewisf4447b92017-06-23 15:53:59 -070069
Christopher Ferrisbfb7c762018-05-04 13:27:47 -070070bool LockToCPU(int cpu_to_lock);