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 bool isCL = false;
186 };
187
188 class ANGLERenderTest : public ANGLEPerfTest
189 {
190 public:
191 ANGLERenderTest(const std::string &name,
192 const RenderTestParams &testParams,
193 const char *units = "ns");
194 ~ANGLERenderTest() override;
195
196 void addExtensionPrerequisite(std::string extensionName);
197 void addIntegerPrerequisite(GLenum target, int min);
198
initializeBenchmark()199 virtual void initializeBenchmark() {}
destroyBenchmark()200 virtual void destroyBenchmark() {}
201
202 virtual void drawBenchmark() = 0;
203
204 bool popEvent(Event *event);
205
206 OSWindow *getWindow();
207 GLWindowBase *getGLWindow();
208
209 std::vector<TraceEvent> &getTraceEventBuffer();
210
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)211 virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
212 void onErrorMessage(const char *errorMessage);
213
214 uint32_t getCurrentThreadSerial();
getTraceEventMutex()215 std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
isRenderTest()216 bool isRenderTest() const override { return true; }
217
218 protected:
219 const RenderTestParams &mTestParams;
220
221 void setWebGLCompatibilityEnabled(bool webglCompatibility);
222 void setRobustResourceInit(bool enabled);
223
224 void startGpuTimer();
225 void stopGpuTimer();
226
227 void beginInternalTraceEvent(const char *name);
228 void endInternalTraceEvent(const char *name);
229 void beginGLTraceEvent(const char *name, double hostTimeSec);
230 void endGLTraceEvent(const char *name, double hostTimeSec);
231
disableTestHarnessSwap()232 void disableTestHarnessSwap() { mSwapEnabled = false; }
233 void updatePerfCounters();
234
235 bool mIsTimestampQueryAvailable;
236 bool mEnableDebugCallback = true;
237
238 void startTest() override;
239 void finishTest() override;
240
241 private:
242 void SetUp() override;
243 void TearDown() override;
244
245 void step() override;
246 void computeGPUTime() override;
247
248 void skipTestIfMissingExtensionPrerequisites();
249 void skipTestIfFailsIntegerPrerequisite();
250
251 void initPerfCounters();
252
253 GLWindowBase *mGLWindow;
254 OSWindow *mOSWindow;
255 std::vector<std::string> mExtensionPrerequisites;
256 struct IntegerPrerequisite
257 {
258 GLenum target;
259 int min;
260 };
261 std::vector<IntegerPrerequisite> mIntegerPrerequisites;
262 angle::PlatformMethods mPlatformMethods;
263 ConfigParameters mConfigParams;
264 bool mSwapEnabled;
265
266 struct TimestampSample
267 {
268 GLuint beginQuery;
269 GLuint endQuery;
270 };
271
272 GLuint mCurrentTimestampBeginQuery = 0;
273 std::queue<TimestampSample> mTimestampQueries;
274
275 // Trace event record that can be output.
276 std::vector<TraceEvent> mTraceEventBuffer;
277
278 // Handle to the entry point binding library.
279 std::unique_ptr<angle::Library> mEntryPointsLib;
280
281 std::vector<uint64_t> mThreadIDs;
282 std::mutex mTraceEventMutex;
283 };
284
285 // Mixins.
286 namespace params
287 {
288 template <typename ParamsT>
Offscreen(const ParamsT & input)289 ParamsT Offscreen(const ParamsT &input)
290 {
291 ParamsT output = input;
292 output.surfaceType = SurfaceType::Offscreen;
293 return output;
294 }
295
296 template <typename ParamsT>
NullDevice(const ParamsT & input)297 ParamsT NullDevice(const ParamsT &input)
298 {
299 ParamsT output = input;
300 output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
301 output.trackGpuTime = false;
302 return output;
303 }
304
305 template <typename ParamsT>
Passthrough(const ParamsT & input)306 ParamsT Passthrough(const ParamsT &input)
307 {
308 return input;
309 }
310 } // namespace params
311
312 namespace angle
313 {
314 // Returns the time of the host since the application started in seconds.
315 double GetHostTimeSeconds();
316 } // namespace angle
317 #endif // PERF_TESTS_ANGLE_PERF_TEST_H_
318