|
| 1 | +/** |
| 2 | + * @file instrumentation.hpp |
| 3 | + * @brief Operation metrics collection with RAII scope management |
| 4 | + * |
| 5 | + * Replaces global static counters (e.g., Buffer::copy_count_, |
| 6 | + * CountingAllocator::allocation_count_) with an injectable, non-static |
| 7 | + * seam. Each test or demo creates a local OperationMetrics instance, |
| 8 | + * passes it to the module under observation, and uses the Scope helper |
| 9 | + * for automatic lifecycle management. |
| 10 | + * |
| 11 | + * Key concepts: |
| 12 | + * - Injectability: metrics are passed by pointer, not baked into class state |
| 13 | + * - RAII scope: OperationMetrics::Scope resets on construction, no manual cleanup |
| 14 | + * - Zero overhead: nullptr means "do not observe" |
| 15 | + * - Thread-local by default: each scope owns its own counters |
| 16 | + * |
| 17 | + * @example |
| 18 | + * hpc::instrumentation::OperationMetrics metrics; |
| 19 | + * hpc::instrumentation::OperationMetrics::Scope scope(metrics); |
| 20 | + * Buffer buf(128, &metrics); |
| 21 | + * Buffer copy(buf); // metrics.copy_count == 1 |
| 22 | + */ |
| 23 | + |
| 24 | +#pragma once |
| 25 | + |
| 26 | +#include <cstddef> |
| 27 | +#include <cstdint> |
| 28 | + |
| 29 | +namespace hpc::instrumentation { |
| 30 | + |
| 31 | +/** |
| 32 | + * @brief Generic operation counter for copy/move/allocation events. |
| 33 | + * |
| 34 | + * A single seam that replaces the scattered static counters previously |
| 35 | + * embedded in Buffer and CountingAllocator. By making the counter |
| 36 | + * external and injectable, tests no longer suffer from order-dependent |
| 37 | + * state pollution, and benchmarks pay zero overhead when metrics is |
| 38 | + * nullptr. |
| 39 | + */ |
| 40 | +class OperationMetrics { |
| 41 | +public: |
| 42 | + size_t copy_count = 0; |
| 43 | + size_t move_count = 0; |
| 44 | + size_t allocation_count = 0; |
| 45 | + size_t deallocation_count = 0; |
| 46 | + size_t total_bytes_allocated = 0; |
| 47 | + size_t total_bytes_deallocated = 0; |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Reset all counters to zero. |
| 51 | + */ |
| 52 | + void reset() noexcept { |
| 53 | + copy_count = 0; |
| 54 | + move_count = 0; |
| 55 | + allocation_count = 0; |
| 56 | + deallocation_count = 0; |
| 57 | + total_bytes_allocated = 0; |
| 58 | + total_bytes_deallocated = 0; |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * @brief RAII scope that resets metrics on entry. |
| 63 | + * |
| 64 | + * Guarantees each test block starts from a clean slate without |
| 65 | + * manual reset_counts() calls. |
| 66 | + */ |
| 67 | + class Scope { |
| 68 | + public: |
| 69 | + explicit Scope(OperationMetrics& m) : metrics_(m) { metrics_.reset(); } |
| 70 | + ~Scope() = default; |
| 71 | + |
| 72 | + Scope(const Scope&) = delete; |
| 73 | + Scope& operator=(const Scope&) = delete; |
| 74 | + Scope(Scope&&) = delete; |
| 75 | + Scope& operator=(Scope&&) = delete; |
| 76 | + |
| 77 | + OperationMetrics& metrics() noexcept { return metrics_; } |
| 78 | + const OperationMetrics& metrics() const noexcept { return metrics_; } |
| 79 | + |
| 80 | + private: |
| 81 | + OperationMetrics& metrics_; |
| 82 | + }; |
| 83 | + |
| 84 | + // Named event recorders for type safety and locality |
| 85 | + void record_copy() noexcept { ++copy_count; } |
| 86 | + void record_move() noexcept { ++move_count; } |
| 87 | + void record_allocation(size_t bytes) noexcept { |
| 88 | + ++allocation_count; |
| 89 | + total_bytes_allocated += bytes; |
| 90 | + } |
| 91 | + void record_deallocation(size_t bytes) noexcept { |
| 92 | + ++deallocation_count; |
| 93 | + total_bytes_deallocated += bytes; |
| 94 | + } |
| 95 | +}; |
| 96 | + |
| 97 | +} // namespace hpc::instrumentation |
0 commit comments