gwnavruntime/querysystem/queryqueue.h Source File

queryqueue.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 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 #pragma once
8 
16 
17 
18 namespace Kaim
19 {
20 
21 class VisualDebugServer;
22 class World;
23 class Database;
24 class IQuery;
25 
28 {
29  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
30 public:
32  : m_msSpentInProcessStat("Spent in Process (ms)")
33  , m_nbQueriesPopped(0)
34  {}
35 
36  FloatStat m_msSpentInProcessStat; // ms spent in Process(), in the last frame
37  KyUInt32 m_nbQueriesPopped; // nb queries done or cancelled in the last frame
38 };
39 
42 {
43  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
44 public:
47  m_workingMemoryMaxSize(4 * 1024 * 1024), // 4MB
48  m_workingMemoryReallocSize(5 * 1024) // 5KB
49  {}
50 
53  String m_name;
57 };
58 
61 {
62  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_QuerySystem)
63 public:
64  enum Type { Push = 0, Cancel };
65  QueryQueueCommand(Type type, IQuery* query) : m_type(type), m_query(query) {}
66  Type m_type;
67  Ptr<IQuery> m_query;
68 };
69 
70 
73 class QueryQueue : public RefCountBase<QueryQueue, MemStat_QuerySystem>
74 {
76 
77 public:
78  QueryQueue();
79  virtual ~QueryQueue();
80 
81  void Init(const QueryQueueConfig& config);
82 
85  void PushBack(IQuery* query);
86 
89  void Cancel(IQuery* query);
90 
93  void Process();
94 
97  void FlushCommands();
98 
101  void FlushQueries();
102 
103  void SetBudgetMs(KyFloat32 budgetMs);
104  KyFloat32 GetBudgetMs() const;
105 
106  void SetWorkingMemoryMaxSizeInBytes(KyUInt32 sizeInBytes);
107  KyUInt32 GetWorkingMemoryMaxSizeInBytes() const;
108  KyUInt32 GetWorkingMemoryCurrentSize() const;
109 
110  // Get pending Commands
111  KyUInt32 GetCommandCount() const;
112  QueryQueueCommand& GetCommand(KyUInt32 index);
113  const QueryQueueCommand& GetCommand(KyUInt32 index) const;
114 
115  // Get pending Queries
116  KyUInt32 GetQueryCount() const;
117  IQuery* GetQuery(KyUInt32 index) const;
118 
120 
121  const QueryQueueStats& GetStats() const;
122 
123  const String& GetName() const { return m_name; }
124 
125  // Call only in WorldUpdate()
126  void Clear();
127  void CancelAllQueriesAndClearCommands(); // O(GetCount())
128 
130  void DoSendVisualDebug(VisualDebugServer& visualDebugServer, VisualDebugSendChangeEvent changeEvent, KyUInt32 queueIndex, KyUInt32 elementId, KyUInt32 queueArrayProcessMode);
131 
132 protected:
133  void ClearCommandForThisQuery(IQuery* query); // O(GetCommandCount())
134  void CancelQuery(IQuery* query); // O(GetQueryCount())
135  void YieldProcess(KyFloat64 msSpent);
136  IQuery* FindFirstNonNullQuery();
137  IQuery* FindNextNonNullQuery();
138 
139 protected:
140  CircularArray<QueryQueueCommand, MemStat_QuerySystem> m_commands;
141  CircularArray<Ptr<IQuery>, MemStat_QuerySystem > m_queries;
143  WorkingMemory m_workingMemory;
147  String m_name;
148 };
149 
150 
151 KY_INLINE QueryQueue::QueryQueue()
152  : m_firstQueryToProcessIndex(0)
153  , m_msProcessBudget(1.0f)
154  , m_msSpentInProcess(0.0f)
155 {}
156 
157 KY_INLINE QueryQueue::~QueryQueue() { Clear(); }
158 
159 KY_INLINE void QueryQueue::SetBudgetMs(KyFloat32 budgetMs) { m_msProcessBudget = budgetMs; }
160 KY_INLINE void QueryQueue::YieldProcess(KyFloat64 msSpent) { m_msSpentInProcess += (KyFloat32)msSpent; }
161 KY_INLINE KyFloat32 QueryQueue::GetBudgetMs() const { return m_msProcessBudget; }
162 KY_INLINE const QueryQueueStats& QueryQueue::GetStats() const { return m_stats; }
163 KY_INLINE KyUInt32 QueryQueue::GetCommandCount() const { return m_commands.GetCount(); }
164 KY_INLINE QueryQueueCommand& QueryQueue::GetCommand(KyUInt32 index) { return m_commands[index]; }
165 KY_INLINE const QueryQueueCommand& QueryQueue::GetCommand(KyUInt32 index) const { return m_commands[index]; }
166 KY_INLINE KyUInt32 QueryQueue::GetQueryCount() const { return m_queries.GetCount(); }
167 KY_INLINE IQuery* QueryQueue::GetQuery(KyUInt32 index) const { return m_queries[index]; }
168 KY_INLINE KyUInt32 QueryQueue::GetPendingOperationCount() const { return GetQueryCount() + GetCommandCount(); }
169 
170 } // namespace Kaim
171 
String m_name
The name of the QueryQueue.
Definition: queryqueue.h:147
KyUInt32 m_firstQueryToProcessIndex
Current index of query to Process. Usefull for successive call to Process. For Internal Use...
Definition: queryqueue.h:142
void Process()
Process the Queries in the QueryQueue until the budget is spent.
Definition: queryqueue.cpp:57
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
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().
Definition: queryqueue.cpp:172
CircularArray< QueryQueueCommand, MemStat_QuerySystem > m_commands
Postpone PushBack() and Cancel() until next ConsumeCommands().
Definition: queryqueue.h:140
String m_name
The name of the QueryQueue.
Definition: queryqueue.h:53
KyUInt32 GetPendingOperationCount() const
GetCommandCount() + GetQueryCount()
Definition: queryqueue.h:168
void PushBack(IQuery *query)
Must be called from the main thread.
Definition: queryqueue.cpp:31
const QueryQueueStats & GetStats() const
Previous value of GetMsSpentInProcess(). Call this for statitics on the time spent in Process()...
Definition: queryqueue.h:162
QueryQueue processes queries in an asynchronous, time-sliced way, within a QueryQueueArray.
Definition: queryqueue.h:73
Class used to provide QueryQueue initialization parameters.
Definition: queryqueue.h:41
class use internal by the QueryQueue to postpone Push/Cancel of Queries until next FlushCommands() st...
Definition: queryqueue.h:60
#define KY_CLASS_WITHOUT_COPY(ClassName)
Define to forbid copy constructor and copy assignment.
Definition: types.h:196
void Cancel(IQuery *query)
Must be called from the main thread.
Definition: queryqueue.cpp:39
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
KyFloat32 m_budgetInMilliseconds
The time budget per frame of the QueryQueue. Default value is 1 ms.
Definition: queryqueue.h:54
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:56
FloatStat maintains current, average, min, max statistics in a sliding window of frames.
Definition: floatstat.h:19
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
void FlushQueries()
When QueryQueue is part of A QueryQueueArray, QueryQueue::FlushQueries() is called in the World::Upda...
Definition: queryqueue.cpp:132
QueryQueueStats m_stats
Remember statistics of this QueryQueue. For Visual Debug.
Definition: queryqueue.h:146
KyFloat32 m_msSpentInProcess
Aggregates the actual time spent in Process(), set to zero in FlushQueries().
Definition: queryqueue.h:145
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:55
CircularArray< Ptr< IQuery >, MemStat_QuerySystem > m_queries
Queries to process, this includes queries that have been canceled.
Definition: queryqueue.h:141
WorkingMemory m_workingMemory
Sandbox memory used to perform queries; allocates less memory, and in a thread-local manner...
Definition: queryqueue.h:143
This class is used by the Visual Debug system to profileQueryQueue.
Definition: queryqueue.h:27
double KyFloat64
double
Definition: types.h:33
void DoSendVisualDebug(VisualDebugServer &visualDebugServer, VisualDebugSendChangeEvent changeEvent, KyUInt32 queueIndex, KyUInt32 elementId, KyUInt32 queueArrayProcessMode)
When QueryQueue is part of A QueryQueueArray, call within QueryQueueArray::DoSendVisualDebug().
Definition: queryqueue.cpp:157
KyFloat32 m_msProcessBudget
The time in milliseconds this queue is allowed to use in a call to Process().
Definition: queryqueue.h:144
Abstract class for all queries.
Definition: iquery.h:123
float KyFloat32
float
Definition: types.h:32