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 "Benchmark.h" 9#include "SkCanvas.h" 10#include "SkChunkAlloc.h" 11#include "SkPaint.h" 12#include "SkRandom.h" 13#include "SkString.h" 14 15class ChunkAllocBench : public Benchmark { 16 SkString fName; 17 size_t fMinSize; 18public: 19 ChunkAllocBench(size_t minSize) { 20 fMinSize = minSize; 21 fName.printf("chunkalloc_" SK_SIZE_T_SPECIFIER, minSize); 22 } 23 24 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { 25 return backend == kNonRendering_Backend; 26 } 27 28protected: 29 virtual const char* onGetName() SK_OVERRIDE { 30 return fName.c_str(); 31 } 32 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 52private: 53 typedef Benchmark INHERITED; 54}; 55 56DEF_BENCH( return new ChunkAllocBench(64); ) 57DEF_BENCH( return new ChunkAllocBench(8*1024); ) 58 59static int* calloc(size_t num) { 60 return (int*)sk_calloc_throw(num*sizeof(int)); 61} 62 63static 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 70class ZerosBench : public Benchmark { 71 size_t fNum; 72 bool fRead; 73 bool fWrite; 74 bool fUseCalloc; 75 SkString fName; 76public: 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 93 virtual bool isSuitableFor(Backend backend) SK_OVERRIDE { 94 return backend == kNonRendering_Backend; 95 } 96 97protected: 98 virtual const char* onGetName() SK_OVERRIDE { 99 return fName.c_str(); 100 } 101 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? 122DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 0)) 123DEF_BENCH(return new ZerosBench(1024*1024, 0, 0, 1)) 124DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 0)) 125DEF_BENCH(return new ZerosBench(1024*1024, 0, 1, 1)) 126DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 0)) 127DEF_BENCH(return new ZerosBench(1024*1024, 1, 0, 1)) 128DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 0)) 129DEF_BENCH(return new ZerosBench(1024*1024, 1, 1, 1)) 130 131DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 0)) 132DEF_BENCH(return new ZerosBench(256*1024, 0, 0, 1)) 133DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 0)) 134DEF_BENCH(return new ZerosBench(256*1024, 0, 1, 1)) 135DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 0)) 136DEF_BENCH(return new ZerosBench(256*1024, 1, 0, 1)) 137DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 0)) 138DEF_BENCH(return new ZerosBench(256*1024, 1, 1, 1)) 139 140DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 0)) 141DEF_BENCH(return new ZerosBench(4*1024, 0, 0, 1)) 142DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 0)) 143DEF_BENCH(return new ZerosBench(4*1024, 0, 1, 1)) 144DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 0)) 145DEF_BENCH(return new ZerosBench(4*1024, 1, 0, 1)) 146DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 0)) 147DEF_BENCH(return new ZerosBench(4*1024, 1, 1, 1)) 148 149DEF_BENCH(return new ZerosBench(300, 0, 0, 0)) 150DEF_BENCH(return new ZerosBench(300, 0, 0, 1)) 151DEF_BENCH(return new ZerosBench(300, 0, 1, 0)) 152DEF_BENCH(return new ZerosBench(300, 0, 1, 1)) 153DEF_BENCH(return new ZerosBench(300, 1, 0, 0)) 154DEF_BENCH(return new ZerosBench(300, 1, 0, 1)) 155DEF_BENCH(return new ZerosBench(300, 1, 1, 0)) 156DEF_BENCH(return new ZerosBench(300, 1, 1, 1)) 157 158DEF_BENCH(return new ZerosBench(4, 0, 0, 0)) 159DEF_BENCH(return new ZerosBench(4, 0, 0, 1)) 160DEF_BENCH(return new ZerosBench(4, 0, 1, 0)) 161DEF_BENCH(return new ZerosBench(4, 0, 1, 1)) 162DEF_BENCH(return new ZerosBench(4, 1, 0, 0)) 163DEF_BENCH(return new ZerosBench(4, 1, 0, 1)) 164DEF_BENCH(return new ZerosBench(4, 1, 1, 0)) 165DEF_BENCH(return new ZerosBench(4, 1, 1, 1)) 166