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 {
21 template <typename T>
22 class Metric {
23  std::atomic<T> _value { 0 };
24  std::atomic<T> _maximum { 0 };
25 
26 public:
30  T getValue() { return _value; }
31 
35  T getMaximum() { return _maximum; }
36 
43  void set(T newValue) {
44  _value = newValue;
45  }
46 
50  void increment() {
51  auto total = ++_value;
52  if (total > _maximum.load()) {
53  _maximum = total;
54  }
55  }
56 
60  void decrement() {
61  --_value;
62  }
63 
71  void update(T prevValue, T newValue) {
72  if (prevValue == newValue) {
73  return;
74  }
75  if (newValue > prevValue) {
76  auto total = _value.fetch_add(newValue - prevValue);
77  if (total > _maximum.load()) {
78  _maximum = total;
79  }
80  } else {
81  _value.fetch_sub(prevValue - newValue);
82  }
83  }
84 
88  void reset() {
89  _value = 0;
90  _maximum = 0;
91  }
92 };
93 
94 using ContextMetricCount = Metric<uint32_t>;
95 using ContextMetricSize = Metric<Size>;
96 
97 }
98 #endif
Definition: Metric.h:22
void set(T newValue)
Set metric to a new value.
Definition: Metric.h:43
void update(T prevValue, T newValue)
Atomically subtract previous value from the metric and add new one.
Definition: Metric.h:71
void decrement()
Decrement stored value by one.
Definition: Metric.h:60
T getMaximum()
Definition: Metric.h:35
T getValue()
Definition: Metric.h:30
void increment()
Increment stored value by one.
Definition: Metric.h:50
void reset()
Resets current metric value and maximum value to zero.
Definition: Metric.h:88