Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -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 | |
| 17 | #ifndef _BIONIC_BENCHMARKS_UTIL_H_ |
| 18 | #define _BIONIC_BENCHMARKS_UTIL_H_ |
| 19 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 20 | #include <map> |
| 21 | #include <mutex> |
| 22 | #include <string> |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 23 | #include <utility> |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 26 | typedef void (*benchmark_func_t) (void); |
| 27 | |
| 28 | extern std::mutex g_map_lock; |
| 29 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 30 | extern std::map<std::string, std::pair<benchmark_func_t, std::string>> g_str_to_func; |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 31 | |
Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame^] | 32 | static int __attribute__((unused)) EmplaceBenchmark(const std::string& fn_name, benchmark_func_t fn_ptr, const std::string& arg = "") { |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 33 | g_map_lock.lock(); |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 34 | g_str_to_func.emplace(std::string(fn_name), std::make_pair(fn_ptr, arg)); |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 35 | g_map_lock.unlock(); |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | #define BIONIC_BENCHMARK(n) \ |
| 40 | int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n)) |
| 41 | |
Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 42 | #define BIONIC_BENCHMARK_WITH_ARG(n, arg) \ |
| 43 | int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n), arg) |
| 44 | |
| 45 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 46 | constexpr auto KB = 1024; |
| 47 | |
| 48 | typedef struct { |
| 49 | long cpu_to_lock; |
| 50 | long num_iterations; |
| 51 | std::string xmlpath; |
| 52 | std::vector<std::string> extra_benchmarks; |
| 53 | } bench_opts_t; |
| 54 | |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 55 | // This function returns a pointer less than 2 * alignment + or_mask bytes into the array. |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 56 | char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 57 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 58 | char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 59 | |
Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 60 | wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes); |
| 61 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 62 | char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 63 | |
Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 64 | bool LockToCPU(long cpu_to_lock); |
Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 65 | |
| 66 | #endif // _BIONIC_BENCHMARKS_UTIL_H |