Overte C++ Documentation
Query.h
1 //
2 // Query.h
3 // interface/src/gpu
4 //
5 // Created by Niraj Venkat on 7/7/2015.
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2024 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 #ifndef hifi_gpu_Query_h
13 #define hifi_gpu_Query_h
14 
15 #include <assert.h>
16 #include <memory>
17 #include <functional>
18 #include <vector>
19 #include <string>
20 #include <SimpleMovingAverage.h>
21 
22 #include "Format.h"
23 
24 namespace gpu {
25 
26  class Batch;
27 
31  class Query {
32  public:
33  using Handler = std::function<void(const Query&)>;
34 
39  Query(const Handler& returnHandler, const std::string& name = "gpu::query");
40  ~Query();
41 
45  double getGPUElapsedTime() const;
46 
51  double getBatchElapsedTime() const;
52 
56  const std::string& getName() const { return _name; }
57 
60 
67  void triggerReturnHandler(uint64_t queryResult, uint64_t batchElapsedTime);
68  protected:
69 
71  Handler _returnHandler;
72 
74  const std::string _name;
75 
77  uint64_t _queryResult { 0 };
78 
80  uint64_t _nsecBatchElapsedTime { 0 };
81 
82  friend class Serializer;
83  friend class Deserializer;
84  };
85 
86  typedef std::shared_ptr<Query> QueryPointer;
87  typedef std::vector< QueryPointer > Queries;
88 
89 
95  class RangeTimer {
96  public:
97  RangeTimer(const std::string& name);
98 
104  void begin(gpu::Batch& batch);
105 
111  void end(gpu::Batch& batch);
112 
116  double getGPUAverage() const;
117 
122  double getBatchAverage() const;
123 
127  const std::string& name() const { return _name; }
128 
129  protected:
130 
132  static const int QUERY_QUEUE_SIZE { 4 };
133 
135  const std::string _name;
136 
138  gpu::Queries _timerQueries;
139 
141  int _headIndex = -1;
142 
145  int _tailIndex = -1;
146 
148  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageGPU;
149 
151  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageBatch;
152 
154  int rangeIndex(int index) const { return (index % QUERY_QUEUE_SIZE); }
155  };
156 
157  using RangeTimerPointer = std::shared_ptr<RangeTimer>;
158 
159 };
160 
161 #endif
Definition: gpu/src/gpu/Forward.h:134
Definition: Query.h:31
double getBatchElapsedTime() const
Definition: Query.cpp:31
Handler _returnHandler
Function to call when query results are available.
Definition: Query.h:71
uint64_t _nsecBatchElapsedTime
Batch time elapsed in nanoseconds.
Definition: Query.h:80
const GPUObjectPointer gpuObject
Only for gpu::Context.
Definition: Query.h:59
Query(const Handler &returnHandler, const std::string &name="gpu::query")
Definition: Query.cpp:18
void triggerReturnHandler(uint64_t queryResult, uint64_t batchElapsedTime)
Saves query results and calls the callback function.
Definition: Query.cpp:35
double getGPUElapsedTime() const
Definition: Query.cpp:28
const std::string _name
Name of the query.
Definition: Query.h:74
const std::string & getName() const
Definition: Query.h:56
uint64_t _queryResult
Query result - GPU elapsed time in nanoseconds.
Definition: Query.h:77
Definition: Query.h:95
double getGPUAverage() const
Definition: Query.cpp:76
int _tailIndex
Definition: Query.h:145
void end(gpu::Batch &batch)
Adds a batch command to get query.
Definition: Query.cpp:61
const std::string _name
Name of the range timer.
Definition: Query.h:135
const std::string & name() const
Definition: Query.h:127
MovingAverage< double, QUERY_QUEUE_SIZE *2 > _movingAverageGPU
Average of the time measured on GPU in millseconds.
Definition: Query.h:148
int rangeIndex(int index) const
Calculates index in the circular buffer where the queries are stored.
Definition: Query.h:154
void begin(gpu::Batch &batch)
Adds a batch command to begin query.
Definition: Query.cpp:57
gpu::Queries _timerQueries
Query objects used for this range timer.
Definition: Query.h:138
MovingAverage< double, QUERY_QUEUE_SIZE *2 > _movingAverageBatch
Average of the time between do_beginQuery and do_endQuery measured when the batch is executed.
Definition: Query.h:151
static const int QUERY_QUEUE_SIZE
Number of query objects in this range timer.
Definition: Query.h:132
double getBatchAverage() const
Definition: Query.cpp:80
int _headIndex
Used for circular buffer. Index of the last query that was executed can be calculated from this using...
Definition: Query.h:141