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 | */ |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 8 | #include "TimerData.h" |
| 9 | |
| 10 | #include "BenchTimer.h" |
| 11 | #include <limits> |
| 12 | |
| 13 | using namespace std; |
| 14 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 15 | TimerData::TimerData(int maxNumTimings) |
| 16 | : fMaxNumTimings(maxNumTimings) |
| 17 | , fCurrTiming(0) |
| 18 | , fWallTimes(maxNumTimings) |
| 19 | , fTruncatedWallTimes(maxNumTimings) |
| 20 | , fCpuTimes(maxNumTimings) |
| 21 | , fTruncatedCpuTimes(maxNumTimings) |
| 22 | , fGpuTimes(maxNumTimings){ |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 23 | } |
| 24 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 25 | bool TimerData::appendTimes(BenchTimer* timer) { |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 26 | SkASSERT(timer != NULL); |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 27 | if (fCurrTiming >= fMaxNumTimings) { |
| 28 | return false; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 29 | } |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 30 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 31 | fWallTimes[fCurrTiming] = timer->fWall; |
| 32 | fTruncatedWallTimes[fCurrTiming] = timer->fTruncatedWall; |
| 33 | fCpuTimes[fCurrTiming] = timer->fCpu; |
| 34 | fTruncatedCpuTimes[fCurrTiming] = timer->fTruncatedCpu; |
| 35 | fGpuTimes[fCurrTiming] = timer->fGpu; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 36 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 37 | ++fCurrTiming; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 38 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 39 | return true; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 40 | } |
| 41 | |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 42 | SkString TimerData::getResult(const char* doubleFormat, |
| 43 | Result result, |
| 44 | const char *configName, |
| 45 | uint32_t timerFlags, |
| 46 | int itersPerTiming) { |
| 47 | SkASSERT(itersPerTiming >= 1); |
| 48 | |
| 49 | if (!fCurrTiming) { |
| 50 | return SkString(""); |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 51 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 52 | |
| 53 | int numTimings = fCurrTiming; |
| 54 | |
| 55 | SkString wallStr(" msecs = "); |
| 56 | SkString truncWallStr(" Wmsecs = "); |
| 57 | SkString cpuStr(" cmsecs = "); |
| 58 | SkString truncCpuStr(" Cmsecs = "); |
| 59 | SkString gpuStr(" gmsecs = "); |
| 60 | |
| 61 | double wallMin = std::numeric_limits<double>::max(); |
| 62 | double truncWallMin = std::numeric_limits<double>::max(); |
| 63 | double cpuMin = std::numeric_limits<double>::max(); |
| 64 | double truncCpuMin = std::numeric_limits<double>::max(); |
| 65 | double gpuMin = std::numeric_limits<double>::max(); |
| 66 | |
| 67 | double wallSum = 0; |
| 68 | double truncWallSum = 0; |
| 69 | double cpuSum = 0; |
| 70 | double truncCpuSum = 0; |
| 71 | double gpuSum = 0; |
| 72 | |
| 73 | for (int i = 0; i < numTimings; ++i) { |
| 74 | if (kPerIter_Result == result) { |
robertphillips@google.com | bbd893d | 2013-12-17 16:32:51 +0000 | [diff] [blame] | 75 | wallStr.appendf(doubleFormat, fWallTimes[i] / itersPerTiming); |
| 76 | truncWallStr.appendf(doubleFormat, fTruncatedWallTimes[i] / itersPerTiming); |
| 77 | cpuStr.appendf(doubleFormat, fCpuTimes[i] / itersPerTiming); |
| 78 | truncCpuStr.appendf(doubleFormat, fTruncatedCpuTimes[i] / itersPerTiming); |
| 79 | gpuStr.appendf(doubleFormat, fGpuTimes[i] / itersPerTiming); |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 80 | |
| 81 | if (i != numTimings - 1) { |
| 82 | static const char kSep[] = ", "; |
| 83 | wallStr.append(kSep); |
| 84 | truncWallStr.append(kSep); |
| 85 | cpuStr.append(kSep); |
| 86 | truncCpuStr.append(kSep); |
| 87 | gpuStr.append(kSep); |
| 88 | } |
| 89 | } else if (kMin_Result == result) { |
| 90 | wallMin = SkTMin(wallMin, fWallTimes[i]); |
| 91 | truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]); |
| 92 | cpuMin = SkTMin(cpuMin, fCpuTimes[i]); |
| 93 | truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]); |
| 94 | gpuMin = SkTMin(gpuMin, fGpuTimes[i]); |
| 95 | } else { |
| 96 | SkASSERT(kAvg_Result == result); |
| 97 | wallSum += fWallTimes[i]; |
| 98 | truncWallSum += fTruncatedWallTimes[i]; |
| 99 | cpuSum += fCpuTimes[i]; |
| 100 | truncCpuSum += fTruncatedCpuTimes[i]; |
| 101 | } |
| 102 | |
| 103 | // We always track the GPU sum because whether it is non-zero indicates if valid gpu times |
| 104 | // were recorded at all. |
| 105 | gpuSum += fGpuTimes[i]; |
| 106 | } |
| 107 | |
| 108 | if (kMin_Result == result) { |
| 109 | wallStr.appendf(doubleFormat, wallMin / itersPerTiming); |
| 110 | truncWallStr.appendf(doubleFormat, truncWallMin / itersPerTiming); |
| 111 | cpuStr.appendf(doubleFormat, cpuMin / itersPerTiming); |
| 112 | truncCpuStr.appendf(doubleFormat, truncCpuMin / itersPerTiming); |
| 113 | gpuStr.appendf(doubleFormat, gpuMin / itersPerTiming); |
| 114 | } else if (kAvg_Result == result) { |
| 115 | int divisor = numTimings * itersPerTiming; |
| 116 | wallStr.appendf(doubleFormat, wallSum / divisor); |
| 117 | truncWallStr.appendf(doubleFormat, truncWallSum / divisor); |
| 118 | cpuStr.appendf(doubleFormat, cpuSum / divisor); |
| 119 | truncCpuStr.appendf(doubleFormat, truncCpuSum / divisor); |
| 120 | gpuStr.appendf(doubleFormat, gpuSum / divisor); |
| 121 | } |
| 122 | |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 123 | SkString str; |
| 124 | str.printf(" %4s:", configName); |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 125 | if (timerFlags & kWall_Flag) { |
| 126 | str += wallStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 127 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 128 | if (timerFlags & kTruncatedWall_Flag) { |
| 129 | str += truncWallStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 130 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 131 | if (timerFlags & kCpu_Flag) { |
| 132 | str += cpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 133 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 134 | if (timerFlags & kTruncatedCpu_Flag) { |
| 135 | str += truncCpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 136 | } |
commit-bot@chromium.org | 55fd612 | 2013-07-31 20:00:56 +0000 | [diff] [blame] | 137 | if ((timerFlags & kGpu_Flag) && gpuSum > 0) { |
| 138 | str += gpuStr; |
scroggo@google.com | 9a41252 | 2012-09-07 15:21:18 +0000 | [diff] [blame] | 139 | } |
| 140 | return str; |
| 141 | } |
commit-bot@chromium.org | 37c772a | 2014-05-29 17:10:24 +0000 | [diff] [blame] | 142 | |
| 143 | #ifdef SK_BUILD_JSON_WRITER |
skia.committer@gmail.com | 9681eeb | 2014-05-30 03:06:10 +0000 | [diff] [blame] | 144 | Json::Value TimerData::getJSON(uint32_t timerFlags, |
commit-bot@chromium.org | 37c772a | 2014-05-29 17:10:24 +0000 | [diff] [blame] | 145 | Result result, |
| 146 | int itersPerTiming) { |
| 147 | SkASSERT(itersPerTiming >= 1); |
| 148 | Json::Value dataNode; |
| 149 | Json::Value wallNode, truncWall, cpuNode, truncCpu, gpuNode; |
| 150 | if (!fCurrTiming) { |
| 151 | return dataNode; |
| 152 | } |
| 153 | |
| 154 | int numTimings = fCurrTiming; |
| 155 | |
| 156 | double wallMin = std::numeric_limits<double>::max(); |
| 157 | double truncWallMin = std::numeric_limits<double>::max(); |
| 158 | double cpuMin = std::numeric_limits<double>::max(); |
| 159 | double truncCpuMin = std::numeric_limits<double>::max(); |
| 160 | double gpuMin = std::numeric_limits<double>::max(); |
| 161 | |
| 162 | double wallSum = 0; |
| 163 | double truncWallSum = 0; |
| 164 | double cpuSum = 0; |
| 165 | double truncCpuSum = 0; |
| 166 | double gpuSum = 0; |
| 167 | |
| 168 | for (int i = 0; i < numTimings; ++i) { |
| 169 | if (kPerIter_Result == result) { |
| 170 | wallNode.append(fWallTimes[i] / itersPerTiming); |
| 171 | truncWall.append(fTruncatedWallTimes[i] / itersPerTiming); |
| 172 | cpuNode.append(fCpuTimes[i] / itersPerTiming); |
| 173 | truncCpu.append(fTruncatedCpuTimes[i] / itersPerTiming); |
| 174 | gpuNode.append(fGpuTimes[i] / itersPerTiming); |
| 175 | } else if (kMin_Result == result) { |
| 176 | wallMin = SkTMin(wallMin, fWallTimes[i]); |
| 177 | truncWallMin = SkTMin(truncWallMin, fTruncatedWallTimes[i]); |
| 178 | cpuMin = SkTMin(cpuMin, fCpuTimes[i]); |
| 179 | truncCpuMin = SkTMin(truncCpuMin, fTruncatedCpuTimes[i]); |
| 180 | gpuMin = SkTMin(gpuMin, fGpuTimes[i]); |
| 181 | } else { |
| 182 | SkASSERT(kAvg_Result == result); |
| 183 | wallSum += fWallTimes[i]; |
| 184 | truncWallSum += fTruncatedWallTimes[i]; |
| 185 | cpuSum += fCpuTimes[i]; |
| 186 | truncCpuSum += fTruncatedCpuTimes[i]; |
| 187 | } |
| 188 | |
| 189 | // We always track the GPU sum because whether it is non-zero indicates if valid gpu times |
| 190 | // were recorded at all. |
| 191 | gpuSum += fGpuTimes[i]; |
| 192 | } |
| 193 | |
| 194 | if (kMin_Result == result) { |
| 195 | wallNode.append(wallMin / itersPerTiming); |
| 196 | truncWall.append(truncWallMin / itersPerTiming); |
| 197 | cpuNode.append(cpuMin / itersPerTiming); |
| 198 | truncCpu.append(truncCpuMin / itersPerTiming); |
| 199 | gpuNode.append(gpuMin / itersPerTiming); |
| 200 | } else if (kAvg_Result == result) { |
| 201 | int divisor = numTimings * itersPerTiming; |
| 202 | wallNode.append(wallSum / divisor); |
| 203 | truncWall.append(truncWallSum / divisor); |
| 204 | cpuNode.append(cpuSum / divisor); |
| 205 | truncCpu.append(truncCpuSum / divisor); |
| 206 | gpuNode.append(gpuSum / divisor); |
| 207 | } |
| 208 | |
| 209 | if (timerFlags & kWall_Flag) { |
| 210 | dataNode["wall"] = wallNode; |
| 211 | } |
| 212 | if (timerFlags & kTruncatedWall_Flag) { |
| 213 | dataNode["truncWall"] = truncWall; |
| 214 | } |
| 215 | if (timerFlags & kCpu_Flag) { |
| 216 | dataNode["cpu"] = cpuNode; |
| 217 | } |
| 218 | if (timerFlags & kTruncatedCpu_Flag) { |
| 219 | dataNode["trucCpu"] = truncCpu; |
| 220 | } |
| 221 | if ((timerFlags & kGpu_Flag) && gpuSum > 0) { |
| 222 | dataNode["gpu"] = gpuNode; |
| 223 | } |
| 224 | return dataNode; |
| 225 | } |
| 226 | #endif // SK_BUILD_JSON_WRITER |