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 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 #ifndef hifi_gpu_Query_h
12 #define hifi_gpu_Query_h
13 
14 #include <assert.h>
15 #include <memory>
16 #include <functional>
17 #include <vector>
18 #include <string>
19 #include <SimpleMovingAverage.h>
20 
21 #include "Format.h"
22 
23 namespace gpu {
24 
25  class Batch;
26 
27  class Query {
28  public:
29  using Handler = std::function<void(const Query&)>;
30 
31  Query(const Handler& returnHandler, const std::string& name = "gpu::query");
32  ~Query();
33 
34  double getGPUElapsedTime() const;
35  double getBatchElapsedTime() const;
36 
37  const std::string& getName() const { return _name; }
38 
39  // Only for gpu::Context
40  const GPUObjectPointer gpuObject {};
41  void triggerReturnHandler(uint64_t queryResult, uint64_t batchElapsedTime);
42  protected:
43  Handler _returnHandler;
44 
45  const std::string _name;
46  uint64_t _queryResult { 0 };
47  uint64_t _usecBatchElapsedTime { 0 };
48 
49  friend class Serializer;
50  friend class Deserializer;
51  };
52 
53  typedef std::shared_ptr<Query> QueryPointer;
54  typedef std::vector< QueryPointer > Queries;
55 
56 
57  // gpu RangeTimer is just returning an estimate of the time taken by a chunck of work delimited by the
58  // begin and end calls repeated for several times.
59  // The result is always a late average of the time spent for that same task a few cycles ago.
60  class RangeTimer {
61  public:
62  RangeTimer(const std::string& name);
63  void begin(gpu::Batch& batch);
64  void end(gpu::Batch& batch);
65 
66  double getGPUAverage() const;
67  double getBatchAverage() const;
68 
69  protected:
70 
71  static const int QUERY_QUEUE_SIZE { 4 };
72 
73  const std::string _name;
74  gpu::Queries _timerQueries;
75  int _headIndex = -1;
76  int _tailIndex = -1;
77 
78  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageGPU;
79  MovingAverage<double, QUERY_QUEUE_SIZE * 2> _movingAverageBatch;
80 
81  int rangeIndex(int index) const { return (index % QUERY_QUEUE_SIZE); }
82  };
83 
84  using RangeTimerPointer = std::shared_ptr<RangeTimer>;
85 
86 };
87 
88 #endif