• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2020-2023. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <benchmark/benchmark.h>
19 #include <stdint.h>
20 
21 #include <map>
22 #include <mutex>
23 #include <string>
24 #include <utility>
25 #include <vector>
26 
27 #ifdef SYSTEM_32_BIT
28 #define LIBACE_PATH "system/lib/platformsdk/libace.z.so"
29 #define LIBGLES_PATH "/system/lib/libGLESv3.so"
30 #else
31 #define LIBACE_PATH "system/lib64/platformsdk/libace.z.so"
32 #define LIBGLES_PATH "/vendor/lib64/chipsetsdk/libGLESv3.so"
33 #define LIBGLES_MALI_PATH "/vendor/lib64/chipsetsdk/libGLES_mali.so"
34 #endif
35 
36 #if defined __APPLE__
37 #define DATA_ROOT "."
38 #else
39 #define DATA_ROOT
40 #endif
41 
42 typedef void (*BenchmarkFunc) (void);
43 typedef void (*ApplyBenchmarkFunc) (benchmark::internal::Benchmark*);
44 
45 extern std::mutex g_benchmarkLock;
46 
47 extern std::map<std::string, std::pair<BenchmarkFunc, std::string>> g_allBenchmarks;
48 
49 extern std::map<std::string, std::pair<BenchmarkFunc, ApplyBenchmarkFunc>> g_applyBenchmarks;
50 
AddApplyBenchmark(const std::string & funcName,BenchmarkFunc funcPtr,ApplyBenchmarkFunc applyFuncPtr)51 static int __attribute__((unused)) AddApplyBenchmark(const std::string& funcName, BenchmarkFunc funcPtr,
52                                                      ApplyBenchmarkFunc applyFuncPtr)
53 {
54     g_benchmarkLock.lock();
55     g_applyBenchmarks.emplace(std::string(funcName), std::make_pair(funcPtr, applyFuncPtr));
56     g_benchmarkLock.unlock();
57     return 0;
58 }
59 
60 static int  __attribute__((unused)) AddBenchmark(const std::string& funcName,
61                                                  BenchmarkFunc funcPtr, const std::string& arg = "")
62 {
63     g_benchmarkLock.lock();
64     g_allBenchmarks.emplace(std::string(funcName), std::make_pair(funcPtr, arg));
65     g_benchmarkLock.unlock();
66     return 0;
67 }
68 
69 #define MUSL_BENCHMARK(n) \
70     int _musl_benchmark_##n __attribute__((unused)) = AddBenchmark(std::string(#n), \
71                                                                    reinterpret_cast<BenchmarkFunc>(n))
72 #define MUSL_BENCHMARK_WITH_ARG(n, arg) \
73     int _musl_benchmark_##n __attribute__((unused)) = AddBenchmark(std::string(#n), \
74                                                                    reinterpret_cast<BenchmarkFunc>(n), arg)
75 
76 #define MUSL_BENCHMARK_WITH_APPLY(n, arg) \
77     int _apply_musl_benchmark_##n __attribute__((unused)) = AddApplyBenchmark(std::string(#n), \
78                                                                    reinterpret_cast<BenchmarkFunc>(n), \
79                                                                    reinterpret_cast<ApplyBenchmarkFunc>(arg))
80 
81 constexpr auto K = 1024;
82 constexpr auto M = 1024 * 1024;
83 constexpr auto G = 1024 * 1024 * 1024;
84 
85 typedef struct bench_opts_t {
86     int cpuNum = -1;
87     long iterNum = 0;
88     std::string jsonPath;
89 } BENCH_OPTS_T;
90 
91 char* AlignUpMemoy(char* origPtr, size_t alignment);
92 
93 char* GetAlignedPtr(std::vector<char>* buf, size_t alignment, size_t nbytes);
94 
95 wchar_t* GetAlignedPtr(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes);
96 
97 char* GetAlignedPtrFilled(std::vector<char>* buf, size_t alignment, size_t nbytes, char fillByte);
98 
99 wchar_t* GetAlignedPtrFilled(std::vector<wchar_t>* buf, size_t alignment, size_t nbytes, wchar_t fillByte);
100 
101 char* ReadJsonFile(const char *fileName);
102 enum JSONError {
103     JSON_SUCCESS = 0,
104     JOSN_ERROR_FILE_READ_FAILED,
105     JOSN_ERROR_JSON_FORMAT,
106 };
107 
108 void open_tcache();
109 
110 #define OPEN_MODE 0666
111