diff options
| -rw-r--r-- | src/trace/Ring.zig | 14 |
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; |
