• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 mStepsToRun;
143     int mTrialNumStepsPerformed;
144     int mTotalNumStepsPerformed;
145     int mIterationsPerStep;
146     bool mRunning;
147     std::vector<double> mTestTrialResults;
148 
149     struct CounterInfo
150     {
151         std::string name;
152         std::vector<GLuint64> samples;
153     };
154     std::map<GLuint, CounterInfo> mPerfCounterInfo;
155     std::vector<uint64_t> mProcessMemoryUsageKBSamples;
156 };
157 
158 enum class SurfaceType
159 {
160     Window,
161     WindowWithVSync,
162     Offscreen,
163 };
164 
165 struct RenderTestParams : public angle::PlatformParameters
166 {
167     RenderTestParams();
~RenderTestParamsRenderTestParams168     virtual ~RenderTestParams() {}
169 
170     virtual std::string backend() const;
171     virtual std::string story() const;
172     std::string backendAndStory() const;
173 
174     EGLint windowWidth             = 64;
175     EGLint windowHeight            = 64;
176     unsigned int iterationsPerStep = 0;
177     bool trackGpuTime              = false;
178     SurfaceType surfaceType        = SurfaceType::Window;
179     EGLenum colorSpace             = EGL_COLORSPACE_LINEAR;
180     bool multisample               = false;
181     EGLint samples                 = -1;
182 };
183 
184 class ANGLERenderTest : public ANGLEPerfTest
185 {
186   public:
187     ANGLERenderTest(const std::string &name,
188                     const RenderTestParams &testParams,
189                     const char *units = "ns");
190     ~ANGLERenderTest() override;
191 
192     void addExtensionPrerequisite(std::string extensionName);
193     void addIntegerPrerequisite(GLenum target, int min);
194 
initializeBenchmark()195     virtual void initializeBenchmark() {}
destroyBenchmark()196     virtual void destroyBenchmark() {}
197 
198     virtual void drawBenchmark() = 0;
199 
200     bool popEvent(Event *event);
201 
202     OSWindow *getWindow();
203     GLWindowBase *getGLWindow();
204 
205     std::vector<TraceEvent> &getTraceEventBuffer();
206 
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)207     virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
208     void onErrorMessage(const char *errorMessage);
209 
210     uint32_t getCurrentThreadSerial();
getTraceEventMutex()211     std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
isRenderTest()212     bool isRenderTest() const override { return true; }
213 
214   protected:
215     const RenderTestParams &mTestParams;
216 
217     void setWebGLCompatibilityEnabled(bool webglCompatibility);
218     void setRobustResourceInit(bool enabled);
219 
220     void startGpuTimer();
221     void stopGpuTimer();
222 
223     void beginInternalTraceEvent(const char *name);
224     void endInternalTraceEvent(const char *name);
225     void beginGLTraceEvent(const char *name, double hostTimeSec);
226     void endGLTraceEvent(const char *name, double hostTimeSec);
227 
disableTestHarnessSwap()228     void disableTestHarnessSwap() { mSwapEnabled = false; }
229     void updatePerfCounters();
230 
231     bool mIsTimestampQueryAvailable;
232     bool mEnableDebugCallback = true;
233 
234   private:
235     void SetUp() override;
236     void TearDown() override;
237 
238     void step() override;
239     void startTest() override;
240     void finishTest() override;
241     void computeGPUTime() override;
242 
243     void skipTestIfMissingExtensionPrerequisites();
244     void skipTestIfFailsIntegerPrerequisite();
245 
246     void initPerfCounters();
247 
248     GLWindowBase *mGLWindow;
249     OSWindow *mOSWindow;
250     std::vector<std::string> mExtensionPrerequisites;
251     struct IntegerPrerequisite
252     {
253         GLenum target;
254         int min;
255     };
256     std::vector<IntegerPrerequisite> mIntegerPrerequisites;
257     angle::PlatformMethods mPlatformMethods;
258     ConfigParameters mConfigParams;
259     bool mSwapEnabled;
260 
261     struct TimestampSample
262     {
263         GLuint beginQuery;
264         GLuint endQuery;
265     };
266 
267     GLuint mCurrentTimestampBeginQuery = 0;
268     std::queue<TimestampSample> mTimestampQueries;
269 
270     // Trace event record that can be output.
271     std::vector<TraceEvent> mTraceEventBuffer;
272 
273     // Handle to the entry point binding library.
274     std::unique_ptr<angle::Library> mEntryPointsLib;
275 
276     std::vector<uint64_t> mThreadIDs;
277     std::mutex mTraceEventMutex;
278 };
279 
280 // Mixins.
281 namespace params
282 {
283 template <typename ParamsT>
Offscreen(const ParamsT & input)284 ParamsT Offscreen(const ParamsT &input)
285 {
286     ParamsT output     = input;
287     output.surfaceType = SurfaceType::Offscreen;
288     return output;
289 }
290 
291 template <typename ParamsT>
NullDevice(const ParamsT & input)292 ParamsT NullDevice(const ParamsT &input)
293 {
294     ParamsT output                  = input;
295     output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
296     output.trackGpuTime             = false;
297     return output;
298 }
299 
300 template <typename ParamsT>
Passthrough(const ParamsT & input)301 ParamsT Passthrough(const ParamsT &input)
302 {
303     return input;
304 }
305 }  // namespace params
306 
307 namespace angle
308 {
309 // Returns the time of the host since the application started in seconds.
310 double GetHostTimeSeconds();
311 }  // namespace angle
312 #endif  // PERF_TESTS_ANGLE_PERF_TEST_H_
313