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 
28  class Query {
29  public:
30  using Handler = std::function<void(const Query&)>;
31 
32  Query(const Handler& returnHandler, const std::string& name = "gpu::query");
33  ~Query();
34 
35  double getGPUElapsedTime() const;
36  double getBatchElapsedTime() const;
37 
38  const std::string& getName() const { return _name; }
39 
40  // Only for gpu::Context
41  const GPUObjectPointer gpuObject {};
42  void triggerReturnHandler(uint64_t queryResult, uint64_t batchElapsedTime);
43  protected:
44  Handler _returnHandler;
45 
46  const std::string _name;
47  uint64_t _queryResult { 0 };
48  uint64_t _usecBatchElapsedTime { 0 };
49 
50  friend class Serializer;
51  friend class Deserializer;
52  };
53 
54  typedef std::shared_ptr<Query> QueryPointer;
55  typedef std::vector< QueryPointer > Queries;
56 
57 
58  // gpu RangeTimer is just returning an estimate of the time taken by a chunck of work delimited by the
59  // begin and end calls repeated for several times.
60  // The result is always a late average of the time spent for that same task a few cycles ago.
61  class RangeTimer {
62  public:
63  RangeTimer(const std::string& name);
64  void begin(gpu::Batch& batch);
65  void end(gpu::Batch& batch);
66 
67  double getGPUAverage() const;
68  double getBatchAverage() const;
69 
70  const std::string& name() const { return _name; }
71 
72  protected:
73 
74  static const int QUERY_QUEUE_SIZE { 4 };
75 
76  const std::string _name;
77  gpu::Queries _timerQueries;
78  int _headIndex = -1;
79  int _tailIndex = -1;
80 
81  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageGPU;
82  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageBatch;
83 
84  int rangeIndex(int index) const { return (index % QUERY_QUEUE_SIZE); }
85  };
86 
87  using RangeTimerPointer = std::shared_ptr<RangeTimer>;
88 
89 };
90 
91 #endif