Overte C++ Documentation
Metric.h
1 //
2 // Metric.h
3 // libraries/gpu/src/gpu
4 //
5 // Created by Sam Gateau on 5/17/2017.
6 // Copyright 2017 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_Metric_h
12 #define hifi_gpu_Metric_h
13 
14 #include "Forward.h"
15 
16 namespace gpu {
17 
18 template <typename T>
19 class Metric {
20  std::atomic<T> _value { 0 };
21  std::atomic<T> _maximum { 0 };
22 
23 public:
24  T getValue() { return _value; }
25  T getMaximum() { return _maximum; }
26 
27  void set(T newValue) {
28  _value = newValue;
29  }
30  void increment() {
31  auto total = ++_value;
32  if (total > _maximum.load()) {
33  _maximum = total;
34  }
35  }
36  void decrement() {
37  --_value;
38  }
39 
40  void update(T prevValue, T newValue) {
41  if (prevValue == newValue) {
42  return;
43  }
44  if (newValue > prevValue) {
45  auto total = _value.fetch_add(newValue - prevValue);
46  if (total > _maximum.load()) {
47  _maximum = total;
48  }
49  } else {
50  _value.fetch_sub(prevValue - newValue);
51  }
52  }
53 
54  void reset() {
55  _value = 0;
56  _maximum = 0;
57  }
58 };
59 
60 using ContextMetricCount = Metric<uint32_t>;
61 using ContextMetricSize = Metric<Size>;
62 
63 }
64 #endif