1 //
2 // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // ANGLEPerfTests:
7 // Base class for google test performance tests
8 //
9
10 #ifndef PERF_TESTS_ANGLE_PERF_TEST_H_
11 #define PERF_TESTS_ANGLE_PERF_TEST_H_
12
13 #include <gtest/gtest.h>
14
15 #include <mutex>
16 #include <queue>
17 #include <string>
18 #include <unordered_map>
19 #include <vector>
20
21 #include "platform/PlatformMethods.h"
22 #include "test_utils/angle_test_configs.h"
23 #include "test_utils/angle_test_instantiate.h"
24 #include "test_utils/angle_test_platform.h"
25 #include "third_party/perf/perf_result_reporter.h"
26 #include "util/EGLWindow.h"
27 #include "util/OSWindow.h"
28 #include "util/Timer.h"
29 #include "util/util_gl.h"
30
31 class Event;
32
33 #if !defined(ASSERT_GL_NO_ERROR)
34 # define ASSERT_GL_NO_ERROR() ASSERT_EQ(static_cast<GLenum>(GL_NO_ERROR), glGetError())
35 #endif // !defined(ASSERT_GL_NO_ERROR)
36
37 #if !defined(ASSERT_GLENUM_EQ)
38 # define ASSERT_GLENUM_EQ(expected, actual) \
39 ASSERT_EQ(static_cast<GLenum>(expected), static_cast<GLenum>(actual))
40 #endif // !defined(ASSERT_GLENUM_EQ)
41
42 // These are trace events according to Google's "Trace Event Format".
43 // See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
44 // Only a subset of the properties are implemented.
45 struct TraceEvent final
46 {
TraceEventfinal47 TraceEvent() {}
48 TraceEvent(char phaseIn,
49 const char *categoryNameIn,
50 const char *nameIn,
51 double timestampIn,
52 uint32_t tidIn);
53
54 static constexpr uint32_t kMaxNameLen = 64;
55
56 char phase = 0;
57 const char *categoryName = nullptr;
58 char name[kMaxNameLen] = {};
59 double timestamp = 0;
60 uint32_t tid = 0;
61 };
62
63 class ANGLEPerfTest : public testing::Test, angle::NonCopyable
64 {
65 public:
66 ANGLEPerfTest(const std::string &name,
67 const std::string &backend,
68 const std::string &story,
69 unsigned int iterationsPerStep,
70 const char *units = "ns");
71 ~ANGLEPerfTest() override;
72
73 virtual void step() = 0;
74
75 // Called right after the timer starts to let the test initialize other metrics if necessary
startTest()76 virtual void startTest() {}
77 // Called right before timer is stopped to let the test wait for asynchronous operations.
finishTest()78 virtual void finishTest() {}
flush()79 virtual void flush() {}
80
81 // Can be overridden in child tests that require a certain number of steps per trial.
82 virtual int getStepAlignment() const;
83
isRenderTest()84 virtual bool isRenderTest() const { return false; }
85
86 protected:
87 enum class RunTrialPolicy
88 {
89 FinishEveryStep,
90 RunContinuouslyWarmup,
91 RunContinuously,
92 };
93
94 void run();
95 void SetUp() override;
96 void TearDown() override;
97
98 // Normalize a time value according to the number of test trial iterations (mFrameCount)
99 double normalizedTime(size_t value) const;
100
101 // Call if the test step was aborted and the test should stop running.
abortTest()102 void abortTest() { mRunning = false; }
103
getNumStepsPerformed()104 int getNumStepsPerformed() const { return mTrialNumStepsPerformed; }
105
106 void runTrial(double maxRunTime, int maxStepsToRun, RunTrialPolicy runPolicy);
107
108 // Overriden in trace perf tests.
computeGPUTime()109 virtual void computeGPUTime() {}
110
111 void calibrateStepsToRun();
112 int estimateStepsToRun() const;
113
114 void recordIntegerMetric(const char *metric, size_t value, const std::string &units);
115 void recordDoubleMetric(const char *metric, double value, const std::string &units);
116 void addHistogramSample(const char *metric, double value, const std::string &units);
117
118 void processResults();
119 void processClockResult(const char *metric, double resultSeconds);
120 void processMemoryResult(const char *metric, uint64_t resultKB);
121
skipTest(const std::string & reason)122 void skipTest(const std::string &reason)
123 {
124 mSkipTestReason = reason;
125 mSkipTest = true;
126 }
127
failTest(const std::string & reason)128 void failTest(const std::string &reason)
129 {
130 skipTest(reason);
131 FAIL() << reason;
132 }
133
134 void atraceCounter(const char *counterName, int64_t counterValue);
135
136 std::string mName;
137 std::string mBackend;
138 std::string mStory;
139 Timer mTrialTimer;
140 uint64_t mGPUTimeNs;
141 bool mSkipTest;
142 std::string mSkipTestReason;
143 std::unique_ptr<perf_test::PerfResultReporter> mReporter;
144 int mStepsToRun;
145 int mTrialNumStepsPerformed;
146 int mTotalNumStepsPerformed;
147 int mIterationsPerStep;
148 bool mRunning;
149 std::vector<double> mTestTrialResults;
150
151 struct CounterInfo
152 {
153 std::string name;
154 std::vector<GLuint64> samples;
155 };
156 std::map<GLuint, CounterInfo> mPerfCounterInfo;
157 GLuint mPerfMonitor;
158 std::vector<uint64_t> mProcessMemoryUsageKBSamples;
159 };
160
161 enum class SurfaceType
162 {
163 Window,
164 WindowWithVSync,
165 Offscreen,
166 };
167
168 struct RenderTestParams : public angle::PlatformParameters
169 {
170 RenderTestParams();
~RenderTestParamsRenderTestParams171 virtual ~RenderTestParams() {}
172
173 virtual std::string backend() const;
174 virtual std::string story() const;
175 std::string backendAndStory() const;
176
177 EGLint windowWidth = 64;
178 EGLint windowHeight = 64;
179 unsigned int iterationsPerStep = 0;
180 bool trackGpuTime = false;
181 SurfaceType surfaceType = SurfaceType::Window;
182 EGLenum colorSpace = EGL_COLORSPACE_LINEAR;
183 bool multisample = false;
184 EGLint samples = -1;
185 };
186
187 class ANGLERenderTest : public ANGLEPerfTest
188 {
189 public:
190 ANGLERenderTest(const std::string &name,
191 const RenderTestParams &testParams,
192 const char *units = "ns");
193 ~ANGLERenderTest() override;
194
195 void addExtensionPrerequisite(std::string extensionName);
196 void addIntegerPrerequisite(GLenum target, int min);
197
initializeBenchmark()198 virtual void initializeBenchmark() {}
destroyBenchmark()199 virtual void destroyBenchmark() {}
200
201 virtual void drawBenchmark() = 0;
202
203 bool popEvent(Event *event);
204
205 OSWindow *getWindow();
206 GLWindowBase *getGLWindow();
207
208 std::vector<TraceEvent> &getTraceEventBuffer();
209
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)210 virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
211 void onErrorMessage(const char *errorMessage);
212
213 uint32_t getCurrentThreadSerial();
getTraceEventMutex()214 std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
isRenderTest()215 bool isRenderTest() const override { return true; }
216
217 protected:
218 const RenderTestParams &mTestParams;
219
220 void setWebGLCompatibilityEnabled(bool webglCompatibility);
221 void setRobustResourceInit(bool enabled);
222
223 void startGpuTimer();
224 void stopGpuTimer();
225
226 void beginInternalTraceEvent(const char *name);
227 void endInternalTraceEvent(const char *name);
228 void beginGLTraceEvent(const char *name, double hostTimeSec);
229 void endGLTraceEvent(const char *name, double hostTimeSec);
230
disableTestHarnessSwap()231 void disableTestHarnessSwap() { mSwapEnabled = false; }
232 void updatePerfCounters();
233
234 bool mIsTimestampQueryAvailable;
235 bool mEnableDebugCallback = true;
236
237 void startTest() override;
238 void finishTest() override;
239
240 private:
241 void SetUp() override;
242 void TearDown() override;
243
244 void step() override;
245 void computeGPUTime() override;
246
247 void skipTestIfMissingExtensionPrerequisites();
248 void skipTestIfFailsIntegerPrerequisite();
249
250 void initPerfCounters();
251
252 GLWindowBase *mGLWindow;
253 OSWindow *mOSWindow;
254 std::vector<std::string> mExtensionPrerequisites;
255 struct IntegerPrerequisite
256 {
257 GLenum target;
258 int min;
259 };
260 std::vector<IntegerPrerequisite> mIntegerPrerequisites;
261 angle::PlatformMethods mPlatformMethods;
262 ConfigParameters mConfigParams;
263 bool mSwapEnabled;
264
265 struct TimestampSample
266 {
267 GLuint beginQuery;
268 GLuint endQuery;
269 };
270
271 GLuint mCurrentTimestampBeginQuery = 0;
272 std::queue<TimestampSample> mTimestampQueries;
273
274 // Trace event record that can be output.
275 std::vector<TraceEvent> mTraceEventBuffer;
276
277 // Handle to the entry point binding library.
278 std::unique_ptr<angle::Library> mEntryPointsLib;
279
280 std::vector<uint64_t> mThreadIDs;
281 std::mutex mTraceEventMutex;
282 };
283
284 // Mixins.
285 namespace params
286 {
287 template <typename ParamsT>
Offscreen(const ParamsT & input)288 ParamsT Offscreen(const ParamsT &input)
289 {
290 ParamsT output = input;
291 output.surfaceType = SurfaceType::Offscreen;
292 return output;
293 }
294
295 template <typename ParamsT>
NullDevice(const ParamsT & input)296 ParamsT NullDevice(const ParamsT &input)
297 {
298 ParamsT output = input;
299 output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
300 output.trackGpuTime = false;
301 return output;
302 }
303
304 template <typename ParamsT>
Passthrough(const ParamsT & input)305 ParamsT Passthrough(const ParamsT &input)
306 {
307 return input;
308 }
309 } // namespace params
310
311 namespace angle
312 {
313 // Returns the time of the host since the application started in seconds.
314 double GetHostTimeSeconds();
315 } // namespace angle
316 #endif // PERF_TESTS_ANGLE_PERF_TEST_H_
317