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 RunContinuously,
91 };
92
93 void run();
94 void SetUp() override;
95 void TearDown() override;
96
97 // Normalize a time value according to the number of test trial iterations (mFrameCount)
98 double normalizedTime(size_t value) const;
99
100 // Call if the test step was aborted and the test should stop running.
abortTest()101 void abortTest() { mRunning = false; }
102
getNumStepsPerformed()103 int getNumStepsPerformed() const { return mTrialNumStepsPerformed; }
104
105 void runTrial(double maxRunTime, int maxStepsToRun, RunTrialPolicy runPolicy);
106
107 // Overriden in trace perf tests.
saveScreenshot(const std::string & screenshotName)108 virtual void saveScreenshot(const std::string &screenshotName) {}
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 std::string mName;
135 std::string mBackend;
136 std::string mStory;
137 Timer mTrialTimer;
138 uint64_t mGPUTimeNs;
139 bool mSkipTest;
140 std::string mSkipTestReason;
141 std::unique_ptr<perf_test::PerfResultReporter> mReporter;
142 int mWarmupSteps;
143 int mStepsToRun;
144 int mTrialTimeLimitSeconds;
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 std::vector<uint64_t> mProcessMemoryUsageKBSamples;
158 };
159
160 enum class SurfaceType
161 {
162 Window,
163 WindowWithVSync,
164 Offscreen,
165 };
166
167 struct RenderTestParams : public angle::PlatformParameters
168 {
169 RenderTestParams();
~RenderTestParamsRenderTestParams170 virtual ~RenderTestParams() {}
171
172 virtual std::string backend() const;
173 virtual std::string story() const;
174 std::string backendAndStory() const;
175
176 EGLint windowWidth = 64;
177 EGLint windowHeight = 64;
178 unsigned int iterationsPerStep = 0;
179 bool trackGpuTime = false;
180 SurfaceType surfaceType = SurfaceType::Window;
181 EGLenum colorSpace = EGL_COLORSPACE_LINEAR;
182 bool multisample = false;
183 EGLint samples = -1;
184 };
185
186 class ANGLERenderTest : public ANGLEPerfTest
187 {
188 public:
189 ANGLERenderTest(const std::string &name,
190 const RenderTestParams &testParams,
191 const char *units = "ns");
192 ~ANGLERenderTest() override;
193
194 void addExtensionPrerequisite(std::string extensionName);
195 void addIntegerPrerequisite(GLenum target, int min);
196
initializeBenchmark()197 virtual void initializeBenchmark() {}
destroyBenchmark()198 virtual void destroyBenchmark() {}
199
200 virtual void drawBenchmark() = 0;
201
202 bool popEvent(Event *event);
203
204 OSWindow *getWindow();
205 GLWindowBase *getGLWindow();
206
207 std::vector<TraceEvent> &getTraceEventBuffer();
208
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)209 virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
210 void onErrorMessage(const char *errorMessage);
211
212 uint32_t getCurrentThreadSerial();
getTraceEventMutex()213 std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
isRenderTest()214 bool isRenderTest() const override { return true; }
215
216 protected:
217 const RenderTestParams &mTestParams;
218
219 void setWebGLCompatibilityEnabled(bool webglCompatibility);
220 void setRobustResourceInit(bool enabled);
221
222 void startGpuTimer();
223 void stopGpuTimer();
224
225 void beginInternalTraceEvent(const char *name);
226 void endInternalTraceEvent(const char *name);
227 void beginGLTraceEvent(const char *name, double hostTimeSec);
228 void endGLTraceEvent(const char *name, double hostTimeSec);
229
disableTestHarnessSwap()230 void disableTestHarnessSwap() { mSwapEnabled = false; }
231 void updatePerfCounters();
232
233 bool mIsTimestampQueryAvailable;
234 bool mEnableDebugCallback = true;
235
236 private:
237 void SetUp() override;
238 void TearDown() override;
239
240 void step() override;
241 void startTest() override;
242 void finishTest() override;
243 void computeGPUTime() override;
244
245 void skipTestIfMissingExtensionPrerequisites();
246 void skipTestIfFailsIntegerPrerequisite();
247
248 void initPerfCounters();
249
250 GLWindowBase *mGLWindow;
251 OSWindow *mOSWindow;
252 std::vector<std::string> mExtensionPrerequisites;
253 struct IntegerPrerequisite
254 {
255 GLenum target;
256 int min;
257 };
258 std::vector<IntegerPrerequisite> mIntegerPrerequisites;
259 angle::PlatformMethods mPlatformMethods;
260 ConfigParameters mConfigParams;
261 bool mSwapEnabled;
262
263 struct TimestampSample
264 {
265 GLuint beginQuery;
266 GLuint endQuery;
267 };
268
269 GLuint mCurrentTimestampBeginQuery = 0;
270 std::queue<TimestampSample> mTimestampQueries;
271
272 // Trace event record that can be output.
273 std::vector<TraceEvent> mTraceEventBuffer;
274
275 // Handle to the entry point binding library.
276 std::unique_ptr<angle::Library> mEntryPointsLib;
277
278 std::vector<uint64_t> mThreadIDs;
279 std::mutex mTraceEventMutex;
280 };
281
282 // Mixins.
283 namespace params
284 {
285 template <typename ParamsT>
Offscreen(const ParamsT & input)286 ParamsT Offscreen(const ParamsT &input)
287 {
288 ParamsT output = input;
289 output.surfaceType = SurfaceType::Offscreen;
290 return output;
291 }
292
293 template <typename ParamsT>
NullDevice(const ParamsT & input)294 ParamsT NullDevice(const ParamsT &input)
295 {
296 ParamsT output = input;
297 output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
298 output.trackGpuTime = false;
299 return output;
300 }
301
302 template <typename ParamsT>
Passthrough(const ParamsT & input)303 ParamsT Passthrough(const ParamsT &input)
304 {
305 return input;
306 }
307 } // namespace params
308
309 namespace angle
310 {
311 // Returns the time of the host since the application started in seconds.
312 double GetHostTimeSeconds();
313 } // namespace angle
314 #endif // PERF_TESTS_ANGLE_PERF_TEST_H_
315