• 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 <string>
17 #include <thread>
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 
84   protected:
85     enum class RunLoopPolicy
86     {
87         FinishEveryStep,
88         RunContinuously,
89     };
90 
91     void run();
92     void SetUp() override;
93     void TearDown() override;
94 
95     // Normalize a time value according to the number of test loop iterations (mFrameCount)
96     double normalizedTime(size_t value) const;
97 
98     // Call if the test step was aborted and the test should stop running.
abortTest()99     void abortTest() { mRunning = false; }
100 
getNumStepsPerformed()101     int getNumStepsPerformed() const { return mTrialNumStepsPerformed; }
102 
103     void doRunLoop(double maxRunTime, int maxStepsToRun, RunLoopPolicy runPolicy);
104 
105     // Overriden in trace perf tests.
saveScreenshot(const std::string & screenshotName)106     virtual void saveScreenshot(const std::string &screenshotName) {}
computeGPUTime()107     virtual void computeGPUTime() {}
108 
109     void calibrateStepsToRun(RunLoopPolicy policy);
110 
111     void processResults();
112     void processClockResult(const char *metric, double resultSeconds);
113     void processMemoryResult(const char *metric, uint64_t resultKB);
114 
skipTest(const std::string & reason)115     void skipTest(const std::string &reason)
116     {
117         mSkipTestReason = reason;
118         mSkipTest       = true;
119     }
120 
failTest(const std::string & reason)121     void failTest(const std::string &reason)
122     {
123         skipTest(reason);
124         FAIL() << reason;
125     }
126 
127     std::string mName;
128     std::string mBackend;
129     std::string mStory;
130     Timer mTimer;
131     uint64_t mGPUTimeNs;
132     bool mSkipTest;
133     std::string mSkipTestReason;
134     std::unique_ptr<perf_test::PerfResultReporter> mReporter;
135     int mStepsToRun;
136     int mTrialNumStepsPerformed;
137     int mTotalNumStepsPerformed;
138     int mIterationsPerStep;
139     bool mRunning;
140     std::vector<double> mTestTrialResults;
141 
142     struct CounterInfo
143     {
144         std::string name;
145         std::vector<GLuint> samples;
146     };
147     std::map<GLuint, CounterInfo> mPerfCounterInfo;
148     std::vector<uint64_t> mProcessMemoryUsageKBSamples;
149 };
150 
151 enum class SurfaceType
152 {
153     Window,
154     WindowWithVSync,
155     Offscreen,
156 };
157 
158 struct RenderTestParams : public angle::PlatformParameters
159 {
~RenderTestParamsRenderTestParams160     virtual ~RenderTestParams() {}
161 
162     virtual std::string backend() const;
163     virtual std::string story() const;
164     std::string backendAndStory() const;
165 
166     EGLint windowWidth             = 64;
167     EGLint windowHeight            = 64;
168     unsigned int iterationsPerStep = 0;
169     bool trackGpuTime              = false;
170     SurfaceType surfaceType        = SurfaceType::Window;
171     EGLenum colorSpace             = EGL_COLORSPACE_LINEAR;
172 };
173 
174 class ANGLERenderTest : public ANGLEPerfTest
175 {
176   public:
177     ANGLERenderTest(const std::string &name,
178                     const RenderTestParams &testParams,
179                     const char *units = "ns");
180     ~ANGLERenderTest() override;
181 
182     void addExtensionPrerequisite(const char *extensionName);
183 
initializeBenchmark()184     virtual void initializeBenchmark() {}
destroyBenchmark()185     virtual void destroyBenchmark() {}
186 
187     virtual void drawBenchmark() = 0;
188 
189     bool popEvent(Event *event);
190 
191     OSWindow *getWindow();
192     GLWindowBase *getGLWindow();
193 
194     std::vector<TraceEvent> &getTraceEventBuffer();
195 
overrideWorkaroundsD3D(angle::FeaturesD3D * featuresD3D)196     virtual void overrideWorkaroundsD3D(angle::FeaturesD3D *featuresD3D) {}
197     void onErrorMessage(const char *errorMessage);
198 
199     uint32_t getCurrentThreadSerial();
getTraceEventMutex()200     std::mutex &getTraceEventMutex() { return mTraceEventMutex; }
201 
202   protected:
203     const RenderTestParams &mTestParams;
204 
205     void setWebGLCompatibilityEnabled(bool webglCompatibility);
206     void setRobustResourceInit(bool enabled);
207 
208     void startGpuTimer();
209     void stopGpuTimer();
210 
211     void beginInternalTraceEvent(const char *name);
212     void endInternalTraceEvent(const char *name);
213     void beginGLTraceEvent(const char *name, double hostTimeSec);
214     void endGLTraceEvent(const char *name, double hostTimeSec);
215 
disableTestHarnessSwap()216     void disableTestHarnessSwap() { mSwapEnabled = false; }
217     void updatePerfCounters();
218 
219     bool mIsTimestampQueryAvailable;
220 
221   private:
222     void SetUp() override;
223     void TearDown() override;
224 
225     void step() override;
226     void startTest() override;
227     void finishTest() override;
228     void computeGPUTime() override;
229 
230     void skipTestIfMissingExtensionPrerequisites();
231 
232     void initPerfCounters();
233 
234     GLWindowBase *mGLWindow;
235     OSWindow *mOSWindow;
236     std::vector<const char *> mExtensionPrerequisites;
237     angle::PlatformMethods mPlatformMethods;
238     ConfigParameters mConfigParams;
239     bool mSwapEnabled;
240 
241     struct TimestampSample
242     {
243         GLuint beginQuery;
244         GLuint endQuery;
245     };
246 
247     GLuint mCurrentTimestampBeginQuery = 0;
248     std::vector<TimestampSample> mTimestampQueries;
249 
250     // Trace event record that can be output.
251     std::vector<TraceEvent> mTraceEventBuffer;
252 
253     // Handle to the entry point binding library.
254     std::unique_ptr<angle::Library> mEntryPointsLib;
255 
256     std::vector<std::thread::id> mThreadIDs;
257     std::mutex mTraceEventMutex;
258 };
259 
260 // Mixins.
261 namespace params
262 {
263 template <typename ParamsT>
Offscreen(const ParamsT & input)264 ParamsT Offscreen(const ParamsT &input)
265 {
266     ParamsT output     = input;
267     output.surfaceType = SurfaceType::Offscreen;
268     return output;
269 }
270 
271 template <typename ParamsT>
NullDevice(const ParamsT & input)272 ParamsT NullDevice(const ParamsT &input)
273 {
274     ParamsT output                  = input;
275     output.eglParameters.deviceType = EGL_PLATFORM_ANGLE_DEVICE_TYPE_NULL_ANGLE;
276     output.trackGpuTime             = false;
277     return output;
278 }
279 
280 template <typename ParamsT>
Passthrough(const ParamsT & input)281 ParamsT Passthrough(const ParamsT &input)
282 {
283     return input;
284 }
285 }  // namespace params
286 
287 namespace angle
288 {
289 // Returns the time of the host since the application started in seconds.
290 double GetHostTimeSeconds();
291 }  // namespace angle
292 #endif  // PERF_TESTS_ANGLE_PERF_TEST_H_
293