1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkBenchmark.h"
9 #include "SkCanvas.h"
10 #include "SkPaint.h"
11 #include "SkRandom.h"
12 #include "SkChunkAlloc.h"
13 #include "SkString.h"
14
15 class ChunkAllocBench : public SkBenchmark {
16 SkString fName;
17 size_t fMinSize;
18 public:
ChunkAllocBench(size_t minSize)19 ChunkAllocBench(size_t minSize) {
20 fMinSize = minSize;
21 fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize);
22 }
23
isSuitableFor(Backend backend)24 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
25 return backend == kNonRendering_Backend;
26 }
27
28 protected:
onGetName()29 virtual const char* onGetName() SK_OVERRIDE {
30 return fName.c_str();
31 }
32
onDraw(const int loops,SkCanvas *)33 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
34 size_t inc = fMinSize >> 4;
35 SkASSERT(inc > 0);
36 size_t total = fMinSize * 64;
37
38 SkChunkAlloc alloc(fMinSize);
39
40 for (int i = 0; i < loops; ++i) {
41 size_t size = 0;
42 int calls = 0;
43 while (size < total) {
44 alloc.allocThrow(inc);
45 size += inc;
46 calls += 1;
47 }
48 alloc.reset();
49 }
50 }
51
52 private:
53 typedef SkBenchmark INHERITED;
54 };
55
56 DEF_BENCH( return new ChunkAllocBench(64); )
57 DEF_BENCH( return new ChunkAllocBench(8*1024); )
58
calloc(size_t num)59 static int* calloc(size_t num) {
60 return (int*)sk_calloc_throw(num*sizeof(int));
61 }
62
malloc_bzero(size_t num)63 static int* malloc_bzero(size_t num) {
64 const size_t bytes = num*sizeof(int);
65 int* ints = (int*)sk_malloc_throw(bytes);
66 sk_bzero(ints, bytes);
67 return ints;
68 }
69
70 class ZerosBench : public SkBenchmark {
71 size_t fNum;
72 bool fRead;
73 bool fWrite;
74 bool fUseCalloc;
75 SkString fName;
76 public:
ZerosBench(size_t num,bool read,bool write,bool useCalloc)77 ZerosBench(size_t num, bool read, bool write, bool useCalloc)
78 : fNum(num)
79 , fRead(read)
80 , fWrite(write)
81 , fUseCalloc(useCalloc) {
82 fName.printf("memory_%s", useCalloc ? "calloc" : "malloc_bzero");
83 if (read && write) {
84 fName.appendf("_rw");
85 } else if (read) {
86 fName.appendf("_r");
87 } else if (write) {
88 fName.appendf("_w");
89 }
90 fName.appendf("_"SK_SIZE_T_SPECIFIER, num);
91 }
92
isSuitableFor(Backend backend)93 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE {
94 return backend == kNonRendering_Backend;
95 }
96
97 protected:
onGetName()98 virtual const char* onGetName() SK_OVERRIDE {
99 return fName.c_str();
100 }
101
onDraw(const int loops,SkCanvas *)102 virtual void onDraw(const int loops, SkCanvas*) SK_OVERRIDE {
103 for (int i = 0; i < loops; i++) {
104 int* zeros = fUseCalloc ? calloc(fNum) : malloc_bzero(fNum);
105 if (fRead) {
106 volatile int x = 15;
107 for (size_t j = 0; j < fNum; j++) {
108 x ^= zeros[j];
109 }
110 }
111 if (fWrite) {
112 for (size_t j = 0; j < fNum; j++) {
113 zeros[j] = 15;
114 }
115 }
116 sk_free(zeros);
117 }
118 }
119 };
120
121 // zero count r w useCalloc?
122 DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0))
123 DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1))
124 DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0))
125 DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1))
126 DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0))
127 DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1))
128 DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0))
129 DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1))
130
131 DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0))
132 DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1))
133 DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0))
134 DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1))
135 DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0))
136 DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1))
137 DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0))
138 DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1))
139
140 DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0))
141 DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1))
142 DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0))
143 DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1))
144 DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0))
145 DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1))
146 DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0))
147 DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1))
148
149 DEF_BENCH(return new ZerosBench(300, 0, 0, 0))
150 DEF_BENCH(return new ZerosBench(300, 0, 0, 1))
151 DEF_BENCH(return new ZerosBench(300, 0, 1, 0))
152 DEF_BENCH(return new ZerosBench(300, 0, 1, 1))
153 DEF_BENCH(return new ZerosBench(300, 1, 0, 0))
154 DEF_BENCH(return new ZerosBench(300, 1, 0, 1))
155 DEF_BENCH(return new ZerosBench(300, 1, 1, 0))
156 DEF_BENCH(return new ZerosBench(300, 1, 1, 1))
157
158 DEF_BENCH(return new ZerosBench(4, 0, 0, 0))
159 DEF_BENCH(return new ZerosBench(4, 0, 0, 1))
160 DEF_BENCH(return new ZerosBench(4, 0, 1, 0))
161 DEF_BENCH(return new ZerosBench(4, 0, 1, 1))
162 DEF_BENCH(return new ZerosBench(4, 1, 0, 0))
163 DEF_BENCH(return new ZerosBench(4, 1, 0, 1))
164 DEF_BENCH(return new ZerosBench(4, 1, 1, 0))
165 DEF_BENCH(return new ZerosBench(4, 1, 1, 1))
166