scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2012 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
| 9 | #include "TimerData.h" |
| 10 | |
| 11 | #include "BenchTimer.h" |
| 12 | #include <limits> |
| 13 | |
| 14 | using namespace std; |
| 15 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 16 | TimerData::TimerData(int maxNumTimings) |
| 17 | : fMaxNumTimings(maxNumTimings) |
| 18 | , fCurrTiming(0) |
| 19 | , fWallTimes(maxNumTimings) |
| 20 | , fTruncatedWallTimes(maxNumTimings) |
| 21 | , fCpuTimes(maxNumTimings) |
| 22 | , fTruncatedCpuTimes(maxNumTimings) |
| 23 | , fGpuTimes(maxNumTimings){ |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 24 | } |
| 25 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 26 | bool TimerData::appendTimes(BenchTimer* timer) { |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 27 | SkASSERT(timer != NULL); |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 28 | if (fCurrTiming >= fMaxNumTimings) { |
| 29 | return false; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 30 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 31 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 32 | fWallTimes[fCurrTiming] = timer->fWall; |
| 33 | fTruncatedWallTimes[fCurrTiming] = timer->fTruncatedWall; |
| 34 | fCpuTimes[fCurrTiming] = timer->fCpu; |
| 35 | fTruncatedCpuTimes[fCurrTiming] = timer->fTruncatedCpu; |
| 36 | fGpuTimes[fCurrTiming] = timer->fGpu; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 37 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 38 | ++fCurrTiming; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 39 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 40 | return true; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 41 | } |
| 42 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 43 | SkString TimerData::getResult(const char* doubleFormat, |
| 44 | Result result, |
| 45 | const char *configName, |
| 46 | uint32_t timerFlags, |
| 47 | int itersPerTiming) { |
| 48 | SkASSERT(itersPerTiming >= 1); |
| 49 | |
| 50 | if (!fCurrTiming) { |
| 51 | return SkString(""); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 52 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 53 | |
| 54 | int numTimings = fCurrTiming; |
| 55 | |
| 56 | SkString wallStr(" msecs = "); |
| 57 | SkString truncWallStr(" Wmsecs = "); |
| 58 | SkString cpuStr(" cmsecs = "); |
| 59 | SkString truncCpuStr(" Cmsecs = "); |
| 60 | SkString gpuStr(" gmsecs = "); |
| 61 | |
| 62 | double wallMin = std::numeric_limits<double>::max(); |
| 63 | double truncWallMin = std::numeric_limits<double>::max(); |
| 64 | double cpuMin = std::numeric_limits<double>::max(); |
| 65 | double truncCpuMin = std::numeric_limits<double>::max(); |
| 66 | double gpuMin = std::numeric_limits<double>::max(); |
| 67 | |
| 68 | double wallSum = 0; |
| 69 | double truncWallSum = 0; |
| 70 | double cpuSum = 0; |
| 71 | double truncCpuSum = 0; |
| 72 | double gpuSum = 0; |
| 73 | |
| 74 | for (int i = 0; i < numTimings; ++i) { |
| 75 | if (kPerIter_Result == result) { |
| 76 | wallStr.appendf(doubleFormat, fWallTimes[i]); |
| 77 | truncWallStr.appendf(doubleFormat, fTruncatedWallTimes[i]); |
| 78 | cpuStr.appendf(doubleFormat, fCpuTimes[i]); |
| 79 | truncCpuStr.appendf(doubleFormat, fTruncatedCpuTimes[i]); |
| 80 | gpuStr.appendf(doubleFormat, fGpuTimes[i]); |
| 81 | |
| 82 | if (i != numTimings - 1) { |
| 83 | static const char kSep[] = ", "; |
| 84 | wallStr.append(kSep); |
| 85 | truncWallStr.append(kSep); |
| 86 | cpuStr.append(kSep); |
| 87 | truncCpuStr.append(kSep); |
| 88 | gpuStr.append(kSep); |
| 89 | } |
| 90 | } else if (kMin_Result == result) { |
| 91 | wallMin = SkTMin(wallMin, fWallTimes[i]); |
| 92 | truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]); |
| 93 | cpuMin = SkTMin(cpuMin, fCpuTimes[i]); |
| 94 | truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]); |
| 95 | gpuMin = SkTMin(gpuMin, fGpuTimes[i]); |
| 96 | } else { |
| 97 | SkASSERT(kAvg_Result == result); |
| 98 | wallSum += fWallTimes[i]; |
| 99 | truncWallSum += fTruncatedWallTimes[i]; |
| 100 | cpuSum += fCpuTimes[i]; |
| 101 | truncCpuSum += fTruncatedCpuTimes[i]; |
| 102 | } |
| 103 | |
| 104 | // We always track the GPU sum because whether it is non-zero indicates if valid gpu times |
| 105 | // were recorded at all. |
| 106 | gpuSum += fGpuTimes[i]; |
| 107 | } |
| 108 | |
| 109 | if (kMin_Result == result) { |
| 110 | wallStr.appendf(doubleFormat, wallMin / itersPerTiming); |
| 111 | truncWallStr.appendf(doubleFormat, truncWallMin / itersPerTiming); |
| 112 | cpuStr.appendf(doubleFormat, cpuMin / itersPerTiming); |
| 113 | truncCpuStr.appendf(doubleFormat, truncCpuMin / itersPerTiming); |
| 114 | gpuStr.appendf(doubleFormat, gpuMin / itersPerTiming); |
| 115 | } else if (kAvg_Result == result) { |
| 116 | int divisor = numTimings * itersPerTiming; |
| 117 | wallStr.appendf(doubleFormat, wallSum / divisor); |
| 118 | truncWallStr.appendf(doubleFormat, truncWallSum / divisor); |
| 119 | cpuStr.appendf(doubleFormat, cpuSum / divisor); |
| 120 | truncCpuStr.appendf(doubleFormat, truncCpuSum / divisor); |
| 121 | gpuStr.appendf(doubleFormat, gpuSum / divisor); |
| 122 | } |
| 123 | |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 124 | SkString str; |
| 125 | str.printf(" %4s:", configName); |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 126 | if (timerFlags & kWall_Flag) { |
| 127 | str += wallStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 128 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 129 | if (timerFlags & kTruncatedWall_Flag) { |
| 130 | str += truncWallStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 131 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 132 | if (timerFlags & kCpu_Flag) { |
| 133 | str += cpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 134 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 135 | if (timerFlags & kTruncatedCpu_Flag) { |
| 136 | str += truncCpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 137 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 138 | if ((timerFlags & kGpu_Flag) && gpuSum > 0) { |
| 139 | str += gpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 140 | } |
| 141 | return str; |
| 142 | } |