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