gwnavruntime/querysystem/queryqueue.h Source File

queryqueue.h
Go to the documentation of this file.
1 /*
2 * Copyright 2015 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 // primary contact: JAPA - secondary contact: GUAL
8 #ifndef Navigation_QueryQueue_H
9 #define Navigation_QueryQueue_H
10 
18 
19 
20 namespace Kaim
21 {
22 
23 class VisualDebugServer;
24 class World;
25 class Database;
26 class IQuery;
27 
30 {
31  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
32 public:
34  : m_msSpentInProcessStat("Spent in Process (ms)")
35  , m_nbQueriesPopped(0)
36  {}
37 
38  FloatStat m_msSpentInProcessStat; // ms spent in Process(), in the last frame
39  KyUInt32 m_nbQueriesPopped; // nb queries done or cancelled in the last frame
40 };
41 
43 class QueryQueueConfig
44 {
45  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
46 public:
49  m_workingMemoryMaxSize(4 * 1024 * 1024), // 4MB
50  m_workingMemoryReallocSize(5 * 1024) // 5KB
51  {}
52 
55  String m_name;
59 };
60 
63 {
64  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
65 public:
66  enum Type { Push = 0, Cancel };
67  QueryQueueCommand(Type type, IQuery* query) : m_type(type), m_query(query) {}
68  Type m_type;
69  Ptr<IQuery> m_query;
70 };
71 
72 
75 class QueryQueue : public RefCountBase<QueryQueue, MemStat_QuerySystem>
76 {
77  KY_CLASS_WITHOUT_COPY(QueryQueue)
78 
79 public:
80  QueryQueue();
81  virtual ~QueryQueue();
82 
83  void Init(const QueryQueueConfig& config);
84 
87  void PushBack(IQuery* query);
88 
91  void Cancel(IQuery* query);
92 
95  void Process();
96 
99  void FlushCommands();
100 
103  void FlushQueries();
104 
105  void SetBudgetMs(KyFloat32 budgetMs);
106  KyFloat32 GetBudgetMs() const;
107 
108  void SetWorkingMemoryMaxSizeInBytes(KyUInt32 sizeInBytes);
109  KyUInt32 GetWorkingMemoryMaxSizeInBytes() const;
110  KyUInt32 GetWorkingMemoryCurrentSize() const;
111 
112  // Get pending Commands
113  KyUInt32 GetCommandCount() const;
114  QueryQueueCommand& GetCommand(KyUInt32 index);
115  const QueryQueueCommand& GetCommand(KyUInt32 index) const;
116 
117  // Get pending Queries
118  KyUInt32 GetQueryCount() const;
119  IQuery* GetQuery(KyUInt32 index) const;
120 
122 
123  const QueryQueueStats& GetStats() const;
124 
125  const String& GetName() const { return m_name; }
126 
127  // Call only in WorldUpdate()
128  void Clear();
129  void CancelAllQueriesAndClearCommands(); // O(GetCount())
130 
132  void DoSendVisualDebug(VisualDebugServer& visualDebugServer, VisualDebugSendChangeEvent changeEvent, KyUInt32 queueIndex, KyUInt32 elementId, KyUInt32 queueArrayProcessMode);
133 
134 protected:
135  void ClearCommandForThisQuery(IQuery* query); // O(GetCommandCount())
136  void CancelQuery(IQuery* query); // O(GetQueryCount())
137  void YieldProcess(KyFloat64 msSpent);
138  IQuery* FindFirstNonNullQuery();
139  IQuery* FindNextNonNullQuery();
140 
141 protected:
142  CircularArray<QueryQueueCommand, MemStat_QuerySystem> m_commands;
143  CircularArray<Ptr<IQuery>, MemStat_QuerySystem > m_queries;
145  WorkingMemory m_workingMemory;
149  String m_name;
150 };
151 
153 KY_INLINE QueryQueue::QueryQueue()
154  : m_firstQueryToProcessIndex(0)
155  , m_msProcessBudget(1.0f)
156  , m_msSpentInProcess(0.0f)
157 {}
159 KY_INLINE QueryQueue::~QueryQueue() { Clear(); }
160 
161 KY_INLINE void QueryQueue::SetBudgetMs(KyFloat32 budgetMs) { m_msProcessBudget = budgetMs; }
162 KY_INLINE void QueryQueue::YieldProcess(KyFloat64 msSpent) { m_msSpentInProcess += (KyFloat32)msSpent; }
163 KY_INLINE KyFloat32 QueryQueue::GetBudgetMs() const { return m_msProcessBudget; }
164 KY_INLINE const QueryQueueStats& QueryQueue::GetStats() const { return m_stats; }
165 KY_INLINE KyUInt32 QueryQueue::GetCommandCount() const { return m_commands.GetCount(); }
166 KY_INLINE QueryQueueCommand& QueryQueue::GetCommand(KyUInt32 index) { return m_commands[index]; }
167 KY_INLINE const QueryQueueCommand& QueryQueue::GetCommand(KyUInt32 index) const { return m_commands[index]; }
168 KY_INLINE KyUInt32 QueryQueue::GetQueryCount() const { return m_queries.GetCount(); }
169 KY_INLINE IQuery* QueryQueue::GetQuery(KyUInt32 index) const { return m_queries[index]; }
170 KY_INLINE KyUInt32 QueryQueue::GetPendingOperationCount() const { return GetQueryCount() + GetCommandCount(); }
171 
172 } // namespace Kaim
173 
174 #endif // Navigation_QueryQueue_H
String m_name
The name of the QueryQueue.
Definition: queryqueue.h:159
KyUInt32 m_firstQueryToProcessIndex
Current index of query to Process. Usefull for successive call to Process. For Internal Use...
Definition: queryqueue.h:154
void Process()
Process the Queries in the QueryQueue until the budget is spent.
void FlushCommands()
When QueryQueue is part of A QueryQueueArray and this QueryQueue runs within the World::Update(), QueryQueue::FlushCommands() is called at the beginning of World::Update().
CircularArray< QueryQueueCommand, MemStat_QuerySystem > m_commands
Postpone PushBack() and Cancel() until next ConsumeCommands().
Definition: queryqueue.h:152
String m_name
The name of the QueryQueue.
Definition: queryqueue.h:57
KyUInt32 GetPendingOperationCount() const
GetCommandCount() + GetQueryCount()
Definition: queryqueue.h:180
void PushBack(IQuery *query)
Must be called from the main thread.
const QueryQueueStats & GetStats() const
Previous value of GetMsSpentInProcess(). Call this for statitics on the time spent in Process()...
Definition: queryqueue.h:174
QueryQueue processes queries in an asynchronous, time-sliced way, within a QueryQueueArray.
Definition: queryqueue.h:79
Class used to provide QueryQueue initialization parameters.
Definition: queryqueue.h:44
class use internal by the QueryQueue to postpone Push/Cancel of Queries until next FlushCommands() st...
Definition: queryqueue.h:64
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:387
void Cancel(IQuery *query)
Must be called from the main thread.
KyFloat32 m_budgetInMilliseconds
The time budget per frame of the QueryQueue. Default value is 1 ms.
Definition: queryqueue.h:58
KyUInt32 m_workingMemoryReallocSize
The additional size of memory in bytes that will be allocated on ReAlloc in the WorkingMemory. Default value is 5 KB.
Definition: queryqueue.h:60
FloatStat maintains current, average, min, max statistics in a sliding window of frames.
Definition: floatstat.h:23
Definition: gamekitcrowddispersion.h:20
void FlushQueries()
When QueryQueue is part of A QueryQueueArray, QueryQueue::FlushQueries() is called in the World::Upda...
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
The VisualDebugServer manages the sending of data to clients.
Definition: visualdebugserver.h:254
QueryQueueStats m_stats
Remember statistics of this QueryQueue. For Visual Debug.
Definition: queryqueue.h:158
KyFloat32 m_msSpentInProcess
Aggregates the actual time spent in Process(), set to zero in FlushQueries().
Definition: queryqueue.h:157
KyUInt32 m_workingMemoryMaxSize
The amount of memory in bytes the WorkingMemory of this QueryQueue is allowed to allocate. Default value is 1 MB.
Definition: queryqueue.h:59
CircularArray< Ptr< IQuery >, MemStat_QuerySystem > m_queries
Queries to process, this includes queries that have been canceled.
Definition: queryqueue.h:153
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36
WorkingMemory m_workingMemory
Sandbox memory used to perform queries; allocates less memory, and in a thread-local manner...
Definition: queryqueue.h:155
This class is used by the Visual Debug system to profileQueryQueue.
Definition: queryqueue.h:29
double KyFloat64
Type used internally to represent a 64-bit floating-point number.
Definition: types.h:44
void DoSendVisualDebug(VisualDebugServer &visualDebugServer, VisualDebugSendChangeEvent changeEvent, KyUInt32 queueIndex, KyUInt32 elementId, KyUInt32 queueArrayProcessMode)
When QueryQueue is part of A QueryQueueArray, call within QueryQueueArray::DoSendVisualDebug().
KyFloat32 m_msProcessBudget
The time in milliseconds this queue is allowed to use in a call to Process().
Definition: queryqueue.h:156
Abstract class for all queries.
Definition: iquery.h:133
float KyFloat32
Type used internally to represent a 32-bit floating-point number.
Definition: types.h:43