| 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 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 17 | #pragma once | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 18 |  | 
| Christopher Ferris | 5a3c920 | 2019-12-04 15:57:07 -0800 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 21 | #include <map> | 
|  | 22 | #include <mutex> | 
|  | 23 | #include <string> | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 24 | #include <utility> | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 25 | #include <vector> | 
|  | 26 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 27 | typedef void (*benchmark_func_t) (void); | 
|  | 28 |  | 
|  | 29 | extern std::mutex g_map_lock; | 
|  | 30 |  | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 31 | 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] | 32 |  | 
| Elliott Hughes | 5cec377 | 2018-01-19 15:45:23 -0800 | [diff] [blame] | 33 | 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] | 34 | g_map_lock.lock(); | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 35 | 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] | 36 | g_map_lock.unlock(); | 
|  | 37 | return 0; | 
|  | 38 | } | 
|  | 39 |  | 
|  | 40 | #define BIONIC_BENCHMARK(n) \ | 
|  | 41 | int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n)) | 
|  | 42 |  | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 43 | #define BIONIC_BENCHMARK_WITH_ARG(n, arg) \ | 
|  | 44 | int _bionic_benchmark_##n __attribute__((unused)) = EmplaceBenchmark(std::string(#n), reinterpret_cast<benchmark_func_t>(n), arg) | 
|  | 45 |  | 
| Elliott Hughes | 96705e3 | 2019-09-26 07:42:23 -0700 | [diff] [blame] | 46 | #define BIONIC_TRIVIAL_BENCHMARK(__name, __expression) \ | 
|  | 47 | static void __name(benchmark::State& state) { \ | 
|  | 48 | for (auto _ : state) { \ | 
|  | 49 | benchmark::DoNotOptimize(__expression); \ | 
|  | 50 | } \ | 
|  | 51 | } \ | 
|  | 52 | BIONIC_BENCHMARK(__name) | 
| Christopher Ferris | 858e336 | 2017-11-30 08:53:15 -0800 | [diff] [blame] | 53 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 54 | constexpr auto KB = 1024; | 
|  | 55 |  | 
|  | 56 | typedef struct { | 
| Christopher Ferris | bfb7c76 | 2018-05-04 13:27:47 -0700 | [diff] [blame] | 57 | int cpu_to_lock = -1; | 
|  | 58 | long num_iterations = 0; | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 59 | std::string xmlpath; | 
|  | 60 | std::vector<std::string> extra_benchmarks; | 
|  | 61 | } bench_opts_t; | 
|  | 62 |  | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 63 | // 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] | 64 | char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 65 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 66 | char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes); | 
| Anders Lewis | f4447b9 | 2017-06-23 15:53:59 -0700 | [diff] [blame] | 67 |  | 
| Anders Lewis | ac4f4b4 | 2017-08-08 18:29:51 -0700 | [diff] [blame] | 68 | wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes); | 
|  | 69 |  | 
| Anders Lewis | a7b0f88 | 2017-07-24 20:01:13 -0700 | [diff] [blame] | 70 | 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] | 71 |  | 
| Christopher Ferris | bfb7c76 | 2018-05-04 13:27:47 -0700 | [diff] [blame] | 72 | bool LockToCPU(int cpu_to_lock); | 
| Christopher Ferris | 5a3c920 | 2019-12-04 15:57:07 -0800 | [diff] [blame] | 73 |  | 
| Elliott Hughes | 3469e7d | 2024-05-15 16:06:07 +0000 | [diff] [blame] | 74 | static inline __attribute__((__always_inline__)) void MakeAllocationResident( | 
| Christopher Ferris | 5a3c920 | 2019-12-04 15:57:07 -0800 | [diff] [blame] | 75 | void* ptr, size_t nbytes, int pagesize) { | 
|  | 76 | uint8_t* data = reinterpret_cast<uint8_t*>(ptr); | 
|  | 77 | for (size_t i = 0; i < nbytes; i += pagesize) { | 
|  | 78 | data[i] = 1; | 
|  | 79 | } | 
|  | 80 | } |