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#pragma once
18
19#include <map>
20#include <mutex>
21#include <string>
22#include <utility>
23#include <vector>
24
25typedef void (*benchmark_func_t) (void);
26
27extern std::mutex g_map_lock;
28
29extern std::map<std::string, std::pair<benchmark_func_t, std::string>> g_str_to_func;
30
31static int  __attribute__((unused)) EmplaceBenchmark(const std::string& fn_name, benchmark_func_t fn_ptr, const std::string& arg = "") {
32  g_map_lock.lock();
33  g_str_to_func.emplace(std::string(fn_name), std::make_pair(fn_ptr, arg));
34  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
41#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
44
45constexpr auto KB = 1024;
46
47typedef struct {
48  long cpu_to_lock;
49  long num_iterations;
50  std::string xmlpath;
51  std::vector<std::string> extra_benchmarks;
52} bench_opts_t;
53
54// This function returns a pointer less than 2 * alignment + or_mask bytes into the array.
55char* GetAlignedMemory(char* orig_ptr, size_t alignment, size_t or_mask);
56
57char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes);
58
59wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes);
60
61char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fill_byte);
62
63bool LockToCPU(long cpu_to_lock);
64