All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
collection.h
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"
15#include "pxr/base/trace/eventList.h"
16#include "pxr/base/trace/threads.h"
17
19
20#include <map>
21#include <unordered_map>
22
23PXR_NAMESPACE_OPEN_SCOPE
24
33public:
34 TF_MALLOC_TAG_NEW("Trace", "TraceCollection");
35
36 using This = TraceCollection;
37
39 using EventListPtr = std::unique_ptr<EventList>;
40
42 TraceCollection() = default;
43
46
49
50 // Collections should not be copied because TraceEvents contain
51 // pointers to elements in the Key cache.
52 TraceCollection(const TraceCollection&) = delete;
54
55
58 TRACE_API void AddToCollection(const TraceThreadId& id, EventListPtr&& events);
59
66 class Visitor {
67 public:
69 TRACE_API virtual ~Visitor();
70
72 virtual void OnBeginCollection() = 0;
73
75 virtual void OnEndCollection() = 0;
76
79 virtual void OnBeginThread(const TraceThreadId& threadId) = 0;
80
83 virtual void OnEndThread(const TraceThreadId& threadId) = 0;
84
87 virtual bool AcceptsCategory(TraceCategoryId categoryId) = 0;
88
91 virtual void OnEvent(
92 const TraceThreadId& threadId,
93 const TfToken& key,
94 const TraceEvent& event) = 0;
95 };
96
99 TRACE_API void Iterate(Visitor& visitor) const;
100
103 TRACE_API void ReverseIterate(Visitor& visitor) const;
104
105private:
106 using KeyTokenCache =
107 std::unordered_map<TraceKey, TfToken, TraceKey::HashFunctor>;
108
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
124PXR_NAMESPACE_CLOSE_SCOPE
125
126#endif // PXR_BASE_TRACE_COLLECTION_H
uint32_t TraceCategoryId
Categories that a TraceReporter can use to filter events.
Definition: category.h:27
Token for efficient comparison, assignment, and hashing of known strings.
Definition: token.h:71
This interface provides a way to access data a TraceCollection.
Definition: collection.h:66
virtual bool AcceptsCategory(TraceCategoryId categoryId)=0
Called before an event with categoryId is visited.
virtual void OnBeginCollection()=0
Called at the beginning of an iteration.
virtual void OnBeginThread(const TraceThreadId &threadId)=0
Called before the first event of from the thread with threadId is encountered.
virtual void OnEndThread(const TraceThreadId &threadId)=0
Called after the last event of from the thread with threadId is encountered.
virtual void OnEndCollection()=0
Called at the end of an iteration.
virtual TRACE_API ~Visitor()
Destructor.
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 class owns lists of TraceEvent instances per thread, and allows read access to them.
Definition: collection.h:32
TRACE_API void Iterate(Visitor &visitor) const
Forward iterates over the events of the collection and calls the visitor callbacks.
TraceCollection(TraceCollection &&)=default
Move constructor.
TRACE_API void AddToCollection(const TraceThreadId &id, EventListPtr &&events)
Appends events to the collection.
TRACE_API void ReverseIterate(Visitor &visitor) const
Reverse iterates over the events of the collection and calls the visitor callbacks.
TraceCollection()=default
Constructor.
TraceCollection & operator=(TraceCollection &&)=default
Move assignment operator.
This represents an event recorded by a TraceCollector.
Definition: event.h:30
This class represents an ordered collection of TraceEvents and the TraceDynamicKeys and data that the...
Definition: eventList.h:29
This class represents an identifier for a thread.
Definition: threads.h:23
#define TF_MALLOC_TAG_NEW(name1, name2)
Enable lib/tf memory management.
Definition: mallocTag.h:475