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 terms set forth in the LICENSE.txt file available at
5 // https://openusd.org/license.
6 //
7 
8 #ifndef PXR_BASE_TRACE_COLLECTION_H
9 #define PXR_BASE_TRACE_COLLECTION_H
10 
11 #include "pxr/pxr.h"
12 
13 #include "pxr/base/trace/api.h"
14 #include "pxr/base/trace/event.h"
16 #include "pxr/base/trace/threads.h"
17 
18 #include "pxr/base/tf/mallocTag.h"
19 
20 #include <map>
21 #include <unordered_map>
22 
24 
25 ///////////////////////////////////////////////////////////////////////////////
26 ///
27 /// \class TraceCollection
28 ///
29 /// This class owns lists of TraceEvent instances per thread, and allows
30 /// read access to them.
31 ///
33 public:
34  TF_MALLOC_TAG_NEW("Trace", "TraceCollection");
35 
37 
39  using EventListPtr = std::unique_ptr<EventList>;
40 
41  /// Constructor.
42  TraceCollection() = default;
43 
44  /// Move constructor.
45  TraceCollection(TraceCollection&&) = default;
46 
47  /// Move assignment operator.
49 
50  // Collections should not be copied because TraceEvents contain
51  // pointers to elements in the Key cache.
52  TraceCollection(const TraceCollection&) = delete;
53  TraceCollection& operator=(const TraceCollection&) = delete;
54 
55 
56  /// Appends \p events to the collection. The collection will
57  /// take ownership of the data.
58  TRACE_API void AddToCollection(const TraceThreadId& id, EventListPtr&& events);
59 
60  ////////////////////////////////////////////////////////////////////////
61  ///
62  /// \class Visitor
63  ///
64  /// This interface provides a way to access data a TraceCollection.
65  ///
66  class Visitor {
67  public:
68  /// Destructor
69  TRACE_API virtual ~Visitor();
70 
71  /// Called at the beginning of an iteration.
72  virtual void OnBeginCollection() = 0;
73 
74  /// Called at the end of an iteration.
75  virtual void OnEndCollection() = 0;
76 
77  /// Called before the first event of from the thread with
78  /// \p threadId is encountered.
79  virtual void OnBeginThread(const TraceThreadId& threadId) = 0;
80 
81  /// Called after the last event of from the thread with
82  /// \p threadId is encountered.
83  virtual void OnEndThread(const TraceThreadId& threadId) = 0;
84 
85  /// Called before an event with \p categoryId is visited. If the
86  /// return value is false, the event will be visited.
87  virtual bool AcceptsCategory(TraceCategoryId categoryId) = 0;
88 
89  /// Called for every event \p event with \p key on thread
90  /// \p threadId if AcceptsCategory returns true.
91  virtual void OnEvent(
92  const TraceThreadId& threadId,
93  const TfToken& key,
94  const TraceEvent& event) = 0;
95  };
96 
97  /// Forward iterates over the events of the collection and calls the
98  /// \p visitor callbacks.
99  TRACE_API void Iterate(Visitor& visitor) const;
100 
101  /// Reverse iterates over the events of the collection and calls the
102  /// \p visitor callbacks.
103  TRACE_API void ReverseIterate(Visitor& visitor) const;
104 
105 private:
106  using KeyTokenCache =
107  std::unordered_map<TraceKey, TfToken, TraceKey::HashFunctor>;
108 
109  /// Iterate through threads, then choose either forward or reverse
110  /// iteration for the events in the threads
111  void _Iterate(Visitor& visitor, bool doReverse) const;
112 
113  // Iterate through events in either forward or reverse order, depending on
114  // the templated arguments
115  template <class I>
116  void _IterateEvents(Visitor&, KeyTokenCache&,
117  const TraceThreadId&, I, I) const;
118 
119  using EventTable = std::map<TraceThreadId, EventListPtr>;
120 
121  EventTable _eventsPerThread;
122 };
123 
125 
126 #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:70
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:1425
#define PXR_NAMESPACE_CLOSE_SCOPE
Definition: pxr.h:74
TraceCollection & operator=(TraceCollection &&)=default
Move assignment operator.
virtual void OnEndThread(const TraceThreadId &threadId)=0
std::unique_ptr< EventList > EventListPtr
Definition: collection.h:39
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:27
virtual void OnBeginThread(const TraceThreadId &threadId)=0
#define TRACE_API
Definition: api.h:23