• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 // The functions we want to benchmark are static, so include the source code.
18 #include "luni/src/main/native/libcore_io_Memory.cpp"
19 
20 #include <benchmark/Benchmark.h>
21 
22 template<typename T, size_t ALIGN>
swap_bench(testing::Benchmark * bench,void (* swap_func)(T *,const T *,size_t),int iters,size_t num_elements)23 void swap_bench(testing::Benchmark* bench, void (*swap_func)(T*, const T*, size_t),
24                 int iters, size_t num_elements) {
25   T* src;
26   T* dst;
27   T* src_elems;
28   T* dst_elems;
29 
30   if (ALIGN) {
31     src_elems = new T[num_elements + 1];
32     dst_elems = new T[num_elements + 1];
33 
34     src = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(src_elems) + ALIGN);
35     dst = reinterpret_cast<T*>(reinterpret_cast<uintptr_t>(dst_elems) + ALIGN);
36   } else {
37     src_elems = new T[num_elements];
38     dst_elems = new T[num_elements];
39 
40     src = src_elems;
41     dst = dst_elems;
42   }
43 
44   memset(dst, 0, sizeof(T) * num_elements);
45   memset(src, 0x12, sizeof(T) * num_elements);
46 
47   bench->StartBenchmarkTiming();
48 
49   for (int i = 0; i < iters; i++) {
50     swap_func(src, dst, num_elements);
51   }
52 
53   bench->StopBenchmarkTiming();
54 
55   delete[] src_elems;
56   delete[] dst_elems;
57 }
58 
59 #define AT_COMMON_VALUES \
60     Arg(10)->Arg(100)->Arg(1000)->Arg(1024*10)->Arg(1024*100)
61 
62 BENCHMARK_WITH_ARG(BM_libcore_swapShorts_aligned, int)->AT_COMMON_VALUES;
Run(int iters,int num_shorts)63 void BM_libcore_swapShorts_aligned::Run(int iters, int num_shorts) {
64   swap_bench<jshort, 0>(this, swapShorts, iters, num_shorts);
65 }
66 
67 BENCHMARK_WITH_ARG(BM_libcore_swapInts_aligned, int)->AT_COMMON_VALUES;
Run(int iters,int num_ints)68 void BM_libcore_swapInts_aligned::Run(int iters, int num_ints) {
69   swap_bench<jint, 0>(this, swapInts, iters, num_ints);
70 }
71 
72 BENCHMARK_WITH_ARG(BM_libcore_swapLongs_aligned, int)->AT_COMMON_VALUES;
Run(int iters,int num_longs)73 void BM_libcore_swapLongs_aligned::Run(int iters, int num_longs) {
74   swap_bench<jlong, 0>(this, swapLongs, iters, num_longs);
75 }
76 
77 BENCHMARK_WITH_ARG(BM_libcore_swapShorts_unaligned1, int)->AT_COMMON_VALUES;
Run(int iters,int num_shorts)78 void BM_libcore_swapShorts_unaligned1::Run(int iters, int num_shorts) {
79   swap_bench<jshort, 1>(this, swapShorts, iters, num_shorts);
80 }
81 
82 BENCHMARK_WITH_ARG(BM_libcore_swapInts_unaligned1, int)->AT_COMMON_VALUES;
Run(int iters,int num_ints)83 void BM_libcore_swapInts_unaligned1::Run(int iters, int num_ints) {
84   swap_bench<jint, 1>(this, swapInts, iters, num_ints);
85 }
86 
87 BENCHMARK_WITH_ARG(BM_libcore_swapLongs_unaligned1, int)->AT_COMMON_VALUES;
Run(int iters,int num_longs)88 void BM_libcore_swapLongs_unaligned1::Run(int iters, int num_longs) {
89   swap_bench<jlong, 1>(this, swapLongs, iters, num_longs);
90 }
91 
92 BENCHMARK_WITH_ARG(BM_libcore_swapShorts_unaligned2, int)->AT_COMMON_VALUES;
Run(int iters,int num_shorts)93 void BM_libcore_swapShorts_unaligned2::Run(int iters, int num_shorts) {
94   swap_bench<jshort, 2>(this, swapShorts, iters, num_shorts);
95 }
96 
97 BENCHMARK_WITH_ARG(BM_libcore_swapInts_unaligned2, int)->AT_COMMON_VALUES;
Run(int iters,int num_ints)98 void BM_libcore_swapInts_unaligned2::Run(int iters, int num_ints) {
99   swap_bench<jint, 2>(this, swapInts, iters, num_ints);
100 }
101 
102 BENCHMARK_WITH_ARG(BM_libcore_swapLongs_unaligned2, int)->AT_COMMON_VALUES;
Run(int iters,int num_longs)103 void BM_libcore_swapLongs_unaligned2::Run(int iters, int num_longs) {
104   swap_bench<jlong, 2>(this, swapLongs, iters, num_longs);
105 }
106