Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
graph_runtime_probe.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <cstddef>
5#include <cstdint>
6
7namespace pulp::graph {
8
10 std::uint64_t sequence = 0;
11 std::uint64_t timestamp_samples = 0;
12 std::uint32_t edge_index = 0;
13 std::uint32_t frames = 0;
14 float peak = 0.0f;
15 float rms = 0.0f;
16};
17
18// Single producer / single consumer bounded ring. An unattached probe is a
19// no-op, keeping optional instrumentation allocation-free and lock-free.
20template <std::size_t Capacity = 256> class GraphRuntimeProbe {
21 static_assert(Capacity >= 2 && (Capacity & (Capacity - 1)) == 0,
22 "probe capacity must be a power of two");
23
24 public:
25 void attach() noexcept {
26 attached_.store(true, std::memory_order_release);
27 }
28 void detach() noexcept {
29 attached_.store(false, std::memory_order_release);
30 }
31 bool attached() const noexcept {
32 return attached_.load(std::memory_order_acquire);
33 }
34
35 bool try_push(GraphRuntimeProbeSample sample) noexcept {
36 if (!attached())
37 return false;
38 const auto write = write_.load(std::memory_order_relaxed);
39 const auto read = read_.load(std::memory_order_acquire);
40 if (write - read == Capacity) {
41 dropped_.fetch_add(1, std::memory_order_relaxed);
42 return false;
43 }
44 sample.sequence = write;
45 slots_[write & (Capacity - 1)] = sample;
46 write_.store(write + 1, std::memory_order_release);
47 return true;
48 }
49
50 bool try_pop(GraphRuntimeProbeSample& sample) noexcept {
51 const auto read = read_.load(std::memory_order_relaxed);
52 if (read == write_.load(std::memory_order_acquire))
53 return false;
54 sample = slots_[read & (Capacity - 1)];
55 read_.store(read + 1, std::memory_order_release);
56 return true;
57 }
58
59 std::uint64_t dropped() const noexcept {
60 return dropped_.load(std::memory_order_relaxed);
61 }
62
63 private:
64 alignas(64) std::atomic<std::uint64_t> write_{0};
65 alignas(64) std::atomic<std::uint64_t> read_{0};
66 std::atomic<std::uint64_t> dropped_{0};
67 std::atomic<bool> attached_{false};
68 GraphRuntimeProbeSample slots_[Capacity]{};
69};
70
71} // namespace pulp::graph
Definition graph_runtime_probe.hpp:20
bool try_pop(GraphRuntimeProbeSample &sample) noexcept
Definition graph_runtime_probe.hpp:50
std::uint64_t dropped() const noexcept
Definition graph_runtime_probe.hpp:59
void attach() noexcept
Definition graph_runtime_probe.hpp:25
bool try_push(GraphRuntimeProbeSample sample) noexcept
Definition graph_runtime_probe.hpp:35
void detach() noexcept
Definition graph_runtime_probe.hpp:28
bool attached() const noexcept
Definition graph_runtime_probe.hpp:31
Definition graph_runtime_buffer_assignment.hpp:8
Definition graph_runtime_probe.hpp:9
std::uint64_t sequence
Definition graph_runtime_probe.hpp:10
float peak
Definition graph_runtime_probe.hpp:14
float rms
Definition graph_runtime_probe.hpp:15
std::uint32_t frames
Definition graph_runtime_probe.hpp:13
std::uint32_t edge_index
Definition graph_runtime_probe.hpp:12
std::uint64_t timestamp_samples
Definition graph_runtime_probe.hpp:11