tiny.profiling.ingest.event
Defined in ingest.
API (9)
Actions
Public operations.
Event.assignArrayEvent.assignNumberEvent.benchmarkAllocatedBytesEvent.clearEvent.hasNumberrecord
Types and contracts
Public types and contracts.
Source
Source: src/profiling/ingest/event.zig
zig
const std = @import("std");const profiling = @import("../root.zig");pub const EventKind = enum(u8) { alloc, free, resize, remap, allocator, summary,};pub const Field = enum(u8) { schema, event, kind, summary, sample_ns, mean_ns, median_ns, p95_ns, p99_ns, alloc_bytes, alloc_count, value, mean, median, allocated_bytes, retained_bytes, high_water_live_bytes, len, old_len, new_len, other,};comptime { std.debug.assert(@typeInfo(EventKind).@"enum".field_names.len == 6); std.debug.assert(@typeInfo(Field).@"enum".field_names.len == 21); std.debug.assert(@sizeOf(EventKind) == 1); std.debug.assert(@sizeOf(Field) == 1); std.debug.assert(@typeInfo(Field).@"enum".field_names.len <= @bitSizeOf(u32));}pub const Event = struct { schema_metric: bool = false, event_bench_end: bool = false, summary_memory: bool = false, sample_ns_array: bool = false, numeric_fields: u32 = 0, kind: ?EventKind = null, alloc_bytes: ?u64 = null, allocated_bytes: ?u64 = null, len: ?u64 = null, old_len: ?u64 = null, new_len: ?u64 = null, pub fn clear(self: *Event, field: Field) void { std.debug.assert(@backingInt(field) < 21); self.numeric_fields &= ~fieldBit(field); switch (field) { .schema => self.schema_metric = false, .event => self.event_bench_end = false, .kind => self.kind = null, .summary => self.summary_memory = false, .sample_ns => self.sample_ns_array = false, .alloc_bytes => self.alloc_bytes = null, .allocated_bytes => self.allocated_bytes = null, .len => self.len = null, .old_len => self.old_len = null, .new_len => self.new_len = null, else => {}, } switch (field) { .schema => std.debug.assert(!self.schema_metric), .event => std.debug.assert(!self.event_bench_end), .kind => std.debug.assert(self.kind == null), .summary => std.debug.assert(!self.summary_memory), .sample_ns => std.debug.assert(!self.sample_ns_array), else => {}, } std.debug.assert(!self.hasNumber(field)); std.debug.assert(fieldValue(self.*, field) == null); } pub fn assignNumber(self: *Event, field: Field, bytes: []const u8) void { std.debug.assert(bytes.len > 0); self.numeric_fields |= fieldBit(field); const value = numberAsU64(bytes); switch (field) { .alloc_bytes => self.alloc_bytes = value, .allocated_bytes => self.allocated_bytes = value, .len => self.len = value, .old_len => self.old_len = value, .new_len => self.new_len = value, else => {}, } switch (field) { .alloc_bytes => std.debug.assert(self.alloc_bytes == value), .allocated_bytes => std.debug.assert(self.allocated_bytes == value), .len => std.debug.assert(self.len == value), .old_len => std.debug.assert(self.old_len == value), .new_len => std.debug.assert(self.new_len == value), else => {}, } std.debug.assert(self.hasNumber(field)); } pub fn assignArray(self: *Event, field: Field) void { if (field == .sample_ns) self.sample_ns_array = true; if (field == .sample_ns) std.debug.assert(self.sample_ns_array); } pub fn hasNumber(self: Event, field: Field) bool { return self.numeric_fields & fieldBit(field) != 0; } pub fn benchmarkAllocatedBytes(self: Event) ?u64 { const suite_bytes = self.alloc_bytes orelse return self.allocated_bytes; const metric_bytes = self.allocated_bytes orelse return suite_bytes; return @max(suite_bytes, metric_bytes); }};pub fn record(totals: *profiling.allocation.Totals, value: Event) void { if (!isAllocation(value)) return; const kind = value.kind orelse return; const before = totals.*; totals.saw = true; switch (kind) { .alloc => { totals.allocation_events +|= 1; totals.allocated_bytes += value.len orelse 0; }, .free => totals.free_events +|= 1, .resize => { totals.resize_events +|= 1; recordGrowth(totals, value); }, .remap => { totals.remap_events +|= 1; recordGrowth(totals, value); }, .allocator => {}, .summary => unreachable, } std.debug.assert(totals.saw); std.debug.assert(totals.allocation_events >= before.allocation_events); std.debug.assert(totals.free_events >= before.free_events); std.debug.assert(totals.resize_events >= before.resize_events); std.debug.assert(totals.remap_events >= before.remap_events); std.debug.assert(totals.allocated_bytes >= before.allocated_bytes);}fn isAllocation(value: Event) bool { if (value.schema_metric) return false; const legacy_timing = value.event_bench_end and value.sample_ns_array or value.kind == .summary and hasAnyNumber(value, &.{ .mean_ns, .median_ns, .p95_ns, .p99_ns, }); if (legacy_timing) return false; const legacy_memory = value.event_bench_end and hasAnyNumber(value, &.{ .alloc_bytes, .alloc_count, }) or value.kind == .summary and value.summary_memory and hasAnyNumber(value, &.{ .value, .mean, .median, .alloc_bytes, .alloc_count, .allocated_bytes, .retained_bytes, .high_water_live_bytes, }); if (legacy_memory) return false; return switch (value.kind orelse return false) { .alloc, .free, .resize, .remap, .allocator => true, .summary => false, };}fn hasAnyNumber(value: Event, fields: []const Field) bool { std.debug.assert(fields.len > 0); std.debug.assert(fields.len <= 8); for (fields) |field| { if (value.hasNumber(field)) return true; } return false;}fn numberAsU64(bytes: []const u8) ?u64 { std.debug.assert(bytes.len > 0); if (std.mem.indexOfAny(u8, bytes, ".eE") == null) { return std.fmt.parseInt(u64, bytes, 10) catch null; } const value = std.fmt.parseFloat(f64, bytes) catch return null; if (!std.math.isFinite(value) or value < 0 or @floor(value) != value) return null; if (value >= 18_446_744_073_709_551_616.0) return null; return @intFromFloat(value);}fn recordGrowth(totals: *profiling.allocation.Totals, value: Event) void { std.debug.assert(totals.saw); const new_len = value.new_len orelse return; const old_len = value.old_len orelse value.len orelse return; if (new_len <= old_len) return; totals.allocated_bytes += new_len - old_len;}fn fieldValue(value: Event, field: Field) ?u64 { return switch (field) { .alloc_bytes => value.alloc_bytes, .allocated_bytes => value.allocated_bytes, .len => value.len, .old_len => value.old_len, .new_len => value.new_len, else => null, };}fn fieldBit(field: Field) u32 { const shift: u5 = @intCast(@backingInt(field)); return @as(u32, 1) << shift;}Source: src/profiling/ingest/root.zig:3
zig
pub const event = @import("event.zig");Audit
| Definitions | 10 |
|---|---|
| Public names | 10 |
| Members | 38 |
| Version | 26.7.0 |
| Revision | daab053ee433 |