summaryrefslogtreecommitdiff
path: root/src/trace/Ring.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 12:07:12 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:09:27 -0300
commit9342e5439f49ae6b5afda02e8c4f12b9691f6612 (patch)
tree1f867d8f608e206bb1aa39fc1f0aec3d024e737f /src/trace/Ring.zig
parent4747ea70ac0d319c8c2f33a07aee4d3ef56ac87e (diff)
downloadcomputeranim-9342e5439f49ae6b5afda02e8c4f12b9691f6612.tar.gz
computeranim-9342e5439f49ae6b5afda02e8c4f12b9691f6612.zip
ring: each side reads the other's counter only when its own view is used up
The consumer cached nothing and re-read head for every record, the producers tail: the two cache lines bounced on each event. From the LLVM runtime's profile: an event through the ring 65 ns to 29 ns.
Diffstat (limited to 'src/trace/Ring.zig')
-rw-r--r--src/trace/Ring.zig14
1 files changed, 12 insertions, 2 deletions
diff --git a/src/trace/Ring.zig b/src/trace/Ring.zig
index b92ae61..766ed63 100644
--- a/src/trace/Ring.zig
+++ b/src/trace/Ring.zig
@@ -55,6 +55,12 @@ pub const Record = struct { ev: *const Event, payload: []const u8 };
fd: linux.fd_t,
hdr: *Header,
data: [*]u8,
+/// The consumer's: the head it last read. It reads the producers' cache
+/// line again only when that is used up.
+seen_head: u64 = 0,
+/// The producers': a tail the consumer has passed, read again only when the
+/// ring looks full against it.
+seen_tail: std.atomic.Value(u64) = .init(0),
/// A new ring in an anonymous memory file. The fd is inheritable: a child
/// maps it with `attach`.
@@ -120,7 +126,8 @@ pub fn emit(r: *Ring, ev: Event, payload: []const u8) void {
}
var at = h.head.load(.monotonic);
while (true) {
- if (at + need - h.tail.load(.acquire) > h.capacity) {
+ if (at + need - r.seen_tail.load(.monotonic) > h.capacity) r.seen_tail.store(h.tail.load(.acquire), .monotonic);
+ if (at + need - r.seen_tail.load(.monotonic) > h.capacity) {
if (h.lossy.load(.monotonic) != 0) {
_ = h.dropped.fetchAdd(1, .monotonic);
return;
@@ -172,7 +179,10 @@ fn sinkEmit(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!voi
pub fn peek(r: *Ring) ?Record {
const h = r.hdr;
const t = h.tail.load(.monotonic);
- if (t == h.head.load(.acquire)) return null;
+ if (t == r.seen_head) {
+ r.seen_head = h.head.load(.acquire);
+ if (t == r.seen_head) return null;
+ }
const rec = r.data + (t & (h.capacity - 1));
const word = @atomicLoad(u64, @as(*const u64, @ptrCast(@alignCast(rec))), .acquire);
if (word & commit_bit == 0) return null;