• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2019 The Dawn Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TESTS_PERFTESTS_DAWNPERFTESTPLATFORM_H_
16 #define TESTS_PERFTESTS_DAWNPERFTESTPLATFORM_H_
17 
18 #include <dawn_platform/DawnPlatform.h>
19 
20 #include <memory>
21 #include <mutex>
22 #include <thread>
23 #include <unordered_map>
24 #include <vector>
25 
26 namespace utils {
27     class Timer;
28 }
29 
30 class DawnPerfTestPlatform : public dawn_platform::Platform {
31   public:
32     // These are trace events according to Google's "Trace Event Format".
33     // See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU
34     // Only a subset of the properties are implemented.
35     struct TraceEvent final {
TraceEventfinal36         TraceEvent() {
37         }
TraceEventfinal38         TraceEvent(char phaseIn,
39                    dawn_platform::TraceCategory categoryIn,
40                    const char* nameIn,
41                    uint64_t idIn,
42                    double timestampIn)
43             : phase(phaseIn), category(categoryIn), name(nameIn), id(idIn), timestamp(timestampIn) {
44         }
45 
46         char phase = 0;
47         dawn_platform::TraceCategory category;
48         const char* name = nullptr;
49         uint64_t id = 0;
50         std::string threadId;
51         double timestamp = 0;
52     };
53 
54     DawnPerfTestPlatform();
55     ~DawnPerfTestPlatform() override;
56 
57     void EnableTraceEventRecording(bool enable);
58     std::vector<TraceEvent> AcquireTraceEventBuffer();
59 
60   private:
61     const unsigned char* GetTraceCategoryEnabledFlag(
62         dawn_platform::TraceCategory category) override;
63 
64     double MonotonicallyIncreasingTime() override;
65 
66     std::vector<TraceEvent>* GetLocalTraceEventBuffer();
67 
68     uint64_t AddTraceEvent(char phase,
69                            const unsigned char* categoryGroupEnabled,
70                            const char* name,
71                            uint64_t id,
72                            double timestamp,
73                            int numArgs,
74                            const char** argNames,
75                            const unsigned char* argTypes,
76                            const uint64_t* argValues,
77                            unsigned char flags) override;
78 
79     bool mRecordTraceEvents = false;
80     std::unique_ptr<utils::Timer> mTimer;
81 
82     // Trace event record.
83     // Each uses their own trace event buffer, but the PerfTestPlatform owns all of them in
84     // this map. The map stores all of them so we can iterate through them and flush when
85     // AcquireTraceEventBuffer is called.
86     std::unordered_map<std::thread::id, std::unique_ptr<std::vector<TraceEvent>>>
87         mTraceEventBuffers;
88     std::mutex mTraceEventBufferMapMutex;
89 };
90 
91 #endif  // TESTS_PERFTESTS_DAWNPERFTESTPLATFORM_H_
92