19#include "pw_metric/metric.h"
21namespace pw::allocator {
33#define PW_ALLOCATOR_METRICS_DECLARE(metric_name) \
34 template <typename MetricsType, typename = void> \
35 struct has_##metric_name : std::false_type {}; \
36 template <typename MetricsType> \
37 struct has_##metric_name<MetricsType, \
38 std::void_t<decltype(MetricsType::metric_name)>> \
43PW_ALLOCATOR_METRICS_DECLARE(requested_bytes);
44PW_ALLOCATOR_METRICS_DECLARE(peak_requested_bytes);
45PW_ALLOCATOR_METRICS_DECLARE(cumulative_requested_bytes);
49PW_ALLOCATOR_METRICS_DECLARE(allocated_bytes);
50PW_ALLOCATOR_METRICS_DECLARE(peak_allocated_bytes);
51PW_ALLOCATOR_METRICS_DECLARE(cumulative_allocated_bytes);
54PW_ALLOCATOR_METRICS_DECLARE(num_allocations);
55PW_ALLOCATOR_METRICS_DECLARE(num_deallocations);
56PW_ALLOCATOR_METRICS_DECLARE(num_resizes);
57PW_ALLOCATOR_METRICS_DECLARE(num_reallocations);
61PW_ALLOCATOR_METRICS_DECLARE(num_failures);
62PW_ALLOCATOR_METRICS_DECLARE(unfulfilled_bytes);
64#undef PW_ALLOCATOR_METRICS_DECLARE
86#define PW_ALLOCATOR_METRICS_ENABLE(metric_name) \
87 static_assert(!::pw::allocator::has_##metric_name<void>::value); \
88 PW_METRIC(metric_name, #metric_name, 0U)
101 PW_ALLOCATOR_METRICS_ENABLE(requested_bytes);
102 PW_ALLOCATOR_METRICS_ENABLE(peak_requested_bytes);
103 PW_ALLOCATOR_METRICS_ENABLE(cumulative_requested_bytes);
104 PW_ALLOCATOR_METRICS_ENABLE(allocated_bytes);
105 PW_ALLOCATOR_METRICS_ENABLE(peak_allocated_bytes);
106 PW_ALLOCATOR_METRICS_ENABLE(cumulative_allocated_bytes);
107 PW_ALLOCATOR_METRICS_ENABLE(num_allocations);
108 PW_ALLOCATOR_METRICS_ENABLE(num_deallocations);
109 PW_ALLOCATOR_METRICS_ENABLE(num_resizes);
110 PW_ALLOCATOR_METRICS_ENABLE(num_reallocations);
111 PW_ALLOCATOR_METRICS_ENABLE(num_failures);
112 PW_ALLOCATOR_METRICS_ENABLE(unfulfilled_bytes);
123template <
typename MetricsType>
129 const metric::Group& group()
const {
return group_; }
130 metric::Group& group() {
return group_; }
132 const MetricsType& metrics()
const {
return metrics_; }
181 metric::Group group_;
182 MetricsType metrics_;
186inline uint32_t ClampU32(
size_t size) {
187 return static_cast<uint32_t
>(std::min(
188 size,
static_cast<size_t>(std::numeric_limits<uint32_t>::max())));
193template <
typename MetricsType>
194Metrics<MetricsType>::Metrics(metric::Token token) : group_(token) {
196 group_.Add(metrics_.requested_bytes);
198 if constexpr (has_peak_requested_bytes<MetricsType>::value) {
199 group_.Add(metrics_.peak_requested_bytes);
201 if constexpr (has_cumulative_requested_bytes<MetricsType>::value) {
202 group_.Add(metrics_.cumulative_requested_bytes);
204 if constexpr (has_allocated_bytes<MetricsType>::value) {
205 group_.Add(metrics_.allocated_bytes);
207 if constexpr (has_peak_allocated_bytes<MetricsType>::value) {
208 group_.Add(metrics_.peak_allocated_bytes);
210 if constexpr (has_cumulative_allocated_bytes<MetricsType>::value) {
211 group_.Add(metrics_.cumulative_allocated_bytes);
213 if constexpr (has_num_allocations<MetricsType>::value) {
214 group_.Add(metrics_.num_allocations);
216 if constexpr (has_num_deallocations<MetricsType>::value) {
217 group_.Add(metrics_.num_deallocations);
219 if constexpr (has_num_resizes<MetricsType>::value) {
220 group_.Add(metrics_.num_resizes);
222 if constexpr (has_num_reallocations<MetricsType>::value) {
223 group_.Add(metrics_.num_reallocations);
225 if constexpr (has_num_failures<MetricsType>::value) {
226 group_.Add(metrics_.num_failures);
228 if constexpr (has_unfulfilled_bytes<MetricsType>::value) {
229 group_.Add(metrics_.unfulfilled_bytes);
233template <
typename MetricsType>
236 metrics_.requested_bytes.Increment(internal::ClampU32(increase));
237 metrics_.requested_bytes.Decrement(internal::ClampU32(decrease));
239 uint32_t requested_bytes = metrics_.requested_bytes.value();
240 if (metrics_.peak_requested_bytes.value() < requested_bytes) {
241 metrics_.peak_requested_bytes.Set(requested_bytes);
245 if (increase > decrease) {
246 metrics_.cumulative_requested_bytes.Increment(
247 internal::ClampU32(increase - decrease));
253template <
typename MetricsType>
256 metrics_.allocated_bytes.Increment(internal::ClampU32(increase));
257 metrics_.allocated_bytes.Decrement(internal::ClampU32(decrease));
259 uint32_t allocated_bytes = metrics_.allocated_bytes.value();
260 if (metrics_.peak_allocated_bytes.value() < allocated_bytes) {
261 metrics_.peak_allocated_bytes.Set(allocated_bytes);
265 if (increase > decrease) {
266 metrics_.cumulative_allocated_bytes.Increment(
267 internal::ClampU32(increase - decrease));
273template <
typename MetricsType>
276 metrics_.num_allocations.Increment();
280template <
typename MetricsType>
283 metrics_.num_deallocations.Increment();
287template <
typename MetricsType>
290 metrics_.num_resizes.Increment();
294template <
typename MetricsType>
297 metrics_.num_reallocations.Increment();
301template <
typename MetricsType>
304 metrics_.num_failures.Increment();
307 metrics_.unfulfilled_bytes.Increment(internal::ClampU32(requested));
Definition: metrics.h:124
void IncrementDeallocations()
Records that a call to Deallocate was made.
Definition: metrics.h:281
void RecordFailure(size_t requested)
Definition: metrics.h:302
void ModifyRequested(size_t increase, size_t decrease)
Definition: metrics.h:234
void IncrementReallocations()
Records that a call to Reallocate was made.
Definition: metrics.h:295
void IncrementAllocations()
Records that a call to Allocate was made.
Definition: metrics.h:274
void IncrementResizes()
Records that a call to Resize was made.
Definition: metrics.h:288
void ModifyAllocated(size_t increase, size_t decrease)
Definition: metrics.h:254
A predefined metric struct that enables no allocator metrics.
Definition: metrics.h:91
Definition: metrics.h:100