All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
collection.h
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"
32 #include "pxr/base/trace/eventList.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 
40 PXR_NAMESPACE_OPEN_SCOPE
41 
50 public:
51  TF_MALLOC_TAG_NEW("Trace", "TraceCollection");
52 
53  using This = TraceCollection;
54 
55  using EventList = TraceEventList;
56  using EventListPtr = std::unique_ptr<EventList>;
57 
59  TraceCollection() = default;
60 
62  TraceCollection(TraceCollection&&) = default;
63 
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 
75  TRACE_API void AddToCollection(const TraceThreadId& id, EventListPtr&& events);
76 
83  class Visitor {
84  public:
86  TRACE_API virtual ~Visitor();
87 
89  virtual void OnBeginCollection() = 0;
90 
92  virtual void OnEndCollection() = 0;
93 
96  virtual void OnBeginThread(const TraceThreadId& threadId) = 0;
97 
100  virtual void OnEndThread(const TraceThreadId& threadId) = 0;
101 
104  virtual bool AcceptsCategory(TraceCategoryId categoryId) = 0;
105 
108  virtual void OnEvent(
109  const TraceThreadId& threadId,
110  const TfToken& key,
111  const TraceEvent& event) = 0;
112  };
113 
116  TRACE_API void Iterate(Visitor& visitor) const;
117 
120  TRACE_API void ReverseIterate(Visitor& visitor) const;
121 
122 private:
123  using KeyTokenCache =
124  std::unordered_map<TraceKey, TfToken, TraceKey::HashFunctor>;
125 
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 
141 PXR_NAMESPACE_CLOSE_SCOPE
142 
143 #endif // PXR_BASE_TRACE_COLLECTION_H
TRACE_API void Iterate(Visitor &visitor) const
Forward iterates over the events of the collection and calls the visitor callbacks.
TraceCollection & operator=(TraceCollection &&)=default
Move assignment operator.
virtual void OnBeginCollection()=0
Called at the beginning of an iteration.
virtual void OnEndCollection()=0
Called at the end of an iteration.
TRACE_API void ReverseIterate(Visitor &visitor) const
Reverse iterates over the events of the collection and calls the visitor callbacks.
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:87
This represents an event recorded by a TraceCollector.
Definition: event.h:47
virtual void OnEvent(const TraceThreadId &threadId, const TfToken &key, const TraceEvent &event)=0
Called for every event event with key on thread threadId if AcceptsCategory returns true...
This interface provides a way to access data a TraceCollection.
Definition: collection.h:83
virtual TRACE_API ~Visitor()
Destructor.
This class represents an ordered collection of TraceEvents and the TraceDynamicKeys and data that the...
Definition: eventList.h:46
virtual void OnEndThread(const TraceThreadId &threadId)=0
Called after the last event of from the thread with threadId is encountered.
This class owns lists of TraceEvent instances per thread, and allows read access to them...
Definition: collection.h:49
virtual bool AcceptsCategory(TraceCategoryId categoryId)=0
Called before an event with categoryId is visited.
TRACE_API void AddToCollection(const TraceThreadId &id, EventListPtr &&events)
Appends events to the collection.
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:44
TraceCollection()=default
Constructor.
virtual void OnBeginThread(const TraceThreadId &threadId)=0
Called before the first event of from the thread with threadId is encountered.