HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
collection.h
Go to the documentation of this file.
1 //
2 // Copyright 2018 Pixar
3 //
4 // Licensed under the Apache License, Version 2.0 (the "Apache License")
5 // with the following modification; you may not use this file except in
6 // compliance with the Apache License and the following modification to it:
7 // Section 6. Trademarks. is deleted and replaced with:
8 //
9 // 6. Trademarks. This License does not grant permission to use the trade
10 // names, trademarks, service marks, or product names of the Licensor
11 // and its affiliates, except as required to comply with Section 4(c) of
12 // the License and to reproduce the content of the NOTICE file.
13 //
14 // You may obtain a copy of the Apache License at
15 //
16 // http://www.apache.org/licenses/LICENSE-2.0
17 //
18 // Unless required by applicable law or agreed to in writing, software
19 // distributed under the Apache License with the above modification is
20 // distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21 // KIND, either express or implied. See the Apache License for the specific
22 // language governing permissions and limitations under the Apache License.
23 //
24 
25 #ifndef PXR_BASE_TRACE_COLLECTION_H
26 #define PXR_BASE_TRACE_COLLECTION_H
27 
28 #include "pxr/pxr.h"
29 
30 #include "pxr/base/trace/api.h"
31 #include "pxr/base/trace/event.h"
33 #include "pxr/base/trace/threads.h"
34 
35 #include "pxr/base/tf/mallocTag.h"
36 
37 #include <map>
38 #include <unordered_map>
39 
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 ///
44 /// \class TraceCollection
45 ///
46 /// This class owns lists of TraceEvent instances per thread, and allows
47 /// read access to them.
48 ///
50 public:
51  TF_MALLOC_TAG_NEW("Trace", "TraceCollection");
52 
54 
56  using EventListPtr = std::unique_ptr<EventList>;
57 
58  /// Constructor.
59  TraceCollection() = default;
60 
61  /// Move constructor.
62  TraceCollection(TraceCollection&&) = default;
63 
64  /// Move assignment operator.
66 
67  // Collections should not be copied because TraceEvents contain
68  // pointers to elements in the Key cache.
69  TraceCollection(const TraceCollection&) = delete;
70  TraceCollection& operator=(const TraceCollection&) = delete;
71 
72 
73  /// Appends \p events to the collection. The collection will
74  /// take ownership of the data.
75  TRACE_API void AddToCollection(const TraceThreadId& id, EventListPtr&& events);
76 
77  ////////////////////////////////////////////////////////////////////////
78  ///
79  /// \class Visitor
80  ///
81  /// This interface provides a way to access data a TraceCollection.
82  ///
83  class Visitor {
84  public:
85  /// Destructor
86  TRACE_API virtual ~Visitor();
87 
88  /// Called at the beginning of an iteration.
89  virtual void OnBeginCollection() = 0;
90 
91  /// Called at the end of an iteration.
92  virtual void OnEndCollection() = 0;
93 
94  /// Called before the first event of from the thread with
95  /// \p threadId is encountered.
96  virtual void OnBeginThread(const TraceThreadId& threadId) = 0;
97 
98  /// Called after the last event of from the thread with
99  /// \p threadId is encountered.
100  virtual void OnEndThread(const TraceThreadId& threadId) = 0;
101 
102  /// Called before an event with \p categoryId is visited. If the
103  /// return value is false, the event will be visited.
104  virtual bool AcceptsCategory(TraceCategoryId categoryId) = 0;
105 
106  /// Called for every event \p event with \p key on thread
107  /// \p threadId if AcceptsCategory returns true.
108  virtual void OnEvent(
109  const TraceThreadId& threadId,
110  const TfToken& key,
111  const TraceEvent& event) = 0;
112  };
113 
114  /// Forward iterates over the events of the collection and calls the
115  /// \p visitor callbacks.
116  TRACE_API void Iterate(Visitor& visitor) const;
117 
118  /// Reverse iterates over the events of the collection and calls the
119  /// \p visitor callbacks.
120  TRACE_API void ReverseIterate(Visitor& visitor) const;
121 
122 private:
123  using KeyTokenCache =
124  std::unordered_map<TraceKey, TfToken, TraceKey::HashFunctor>;
125 
126  /// Iterate through threads, then choose either forward or reverse
127  /// iteration for the events in the threads
128  void _Iterate(Visitor& visitor, bool doReverse) const;
129 
130  // Iterate through events in either forward or reverse order, depending on
131  // the templated arguments
132  template <class I>
133  void _IterateEvents(Visitor&, KeyTokenCache&,
134  const TraceThreadId&, I, I) const;
135 
136  using EventTable = std::map<TraceThreadId, EventListPtr>;
137 
138  EventTable _eventsPerThread;
139 };
140 
142 
143 #endif // PXR_BASE_TRACE_COLLECTION_H
virtual void OnEndCollection()=0
Called at the end of an iteration.
TRACE_API void ReverseIterate(Visitor &visitor) const
TraceCollection()=default
Constructor.
struct _cl_event * event
Definition: glcorearb.h:2961
virtual void OnEvent(const TraceThreadId &threadId, const TfToken &key, const TraceEvent &event)=0
TF_MALLOC_TAG_NEW("Trace","TraceCollection")
Definition: token.h:87
virtual TRACE_API ~Visitor()
Destructor.
virtual void OnBeginCollection()=0
Called at the beginning of an iteration.
TRACE_API void Iterate(Visitor &visitor) const
virtual bool AcceptsCategory(TraceCategoryId categoryId)=0
TRACE_API void AddToCollection(const TraceThreadId &id, EventListPtr &&events)
PXR_NAMESPACE_CLOSE_SCOPE PXR_NAMESPACE_OPEN_SCOPE
Definition: path.h:1432
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:91
TraceCollection & operator=(TraceCollection &&)=default
Move assignment operator.
virtual void OnEndThread(const TraceThreadId &threadId)=0
std::unique_ptr< EventList > EventListPtr
Definition: collection.h:56
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:44
virtual void OnBeginThread(const TraceThreadId &threadId)=0
#define TRACE_API
Definition: api.h:40