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
14using namespace std;
15
16TimerData::TimerData(int maxNumTimings)
17: fMaxNumTimings(maxNumTimings)
18, fCurrTiming(0)
19, fWallTimes(maxNumTimings)
20, fTruncatedWallTimes(maxNumTimings)
21, fCpuTimes(maxNumTimings)
22, fTruncatedCpuTimes(maxNumTimings)
23, fGpuTimes(maxNumTimings){
24}
25
26bool TimerData::appendTimes(BenchTimer* timer) {
27    SkASSERT(timer != NULL);
28    if (fCurrTiming >= fMaxNumTimings) {
29        return false;
30    }
31
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;
37
38    ++fCurrTiming;
39
40    return true;
41}
42
43SkString 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("");
52    }
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
124    SkString str;
125    str.printf("  %4s:", configName);
126    if (timerFlags & kWall_Flag) {
127        str += wallStr;
128    }
129    if (timerFlags & kTruncatedWall_Flag) {
130        str += truncWallStr;
131    }
132    if (timerFlags & kCpu_Flag) {
133        str += cpuStr;
134    }
135    if (timerFlags & kTruncatedCpu_Flag) {
136        str += truncCpuStr;
137    }
138    if ((timerFlags & kGpu_Flag) && gpuSum > 0) {
139        str += gpuStr;
140    }
141    return str;
142}
143