tiny.profiling.allocation
Defined in tiny.profiling.
API (5)
Actions
Public operations.
Types and contracts
Public types and contracts.
Source
Source: src/profiling/allocation.zig
zig
const std = @import("std");const json = @import("json.zig");pub const Metric = struct { name: []const u8, unit: []const u8, value: u128,};pub const Totals = struct { saw: bool = false, allocation_events: u64 = 0, free_events: u64 = 0, resize_events: u64 = 0, remap_events: u64 = 0, allocated_bytes: u128 = 0, pub fn record(self: *Totals, row: std.json.ObjectMap) void { const kind = json.string(row.get("kind")) orelse return; if (!isEventKind(kind)) return; self.saw = true; if (std.mem.eql(u8, kind, "alloc")) { self.allocation_events +|= 1; self.allocated_bytes += json.asU64(row.get("len")) orelse 0; } else if (std.mem.eql(u8, kind, "free")) { self.free_events +|= 1; } else if (std.mem.eql(u8, kind, "resize")) { self.resize_events +|= 1; if (resizeGrowth(row)) |growth| self.allocated_bytes += growth; } else if (std.mem.eql(u8, kind, "remap")) { self.remap_events +|= 1; if (resizeGrowth(row)) |growth| self.allocated_bytes += growth; } } pub fn metrics(self: Totals) [5]Metric { return .{ .{ .name = "allocation_events", .unit = "count", .value = self.allocation_events }, .{ .name = "free_events", .unit = "count", .value = self.free_events }, .{ .name = "resize_events", .unit = "count", .value = self.resize_events }, .{ .name = "remap_events", .unit = "count", .value = self.remap_events }, .{ .name = "allocated_bytes", .unit = "bytes", .value = self.allocated_bytes }, }; }};pub fn isMetricName(name: []const u8) bool { for ((Totals{}).metrics()) |metric| { if (std.mem.eql(u8, name, metric.name)) return true; } return false;}fn isEventKind(kind: []const u8) bool { return std.mem.eql(u8, kind, "alloc") or std.mem.eql(u8, kind, "free") or std.mem.eql(u8, kind, "resize") or std.mem.eql(u8, kind, "remap") or std.mem.eql(u8, kind, "allocator");}fn resizeGrowth(row: std.json.ObjectMap) ?u64 { const new_len = json.asU64(row.get("new_len")) orelse return null; const old_len = json.asU64(row.get("old_len")) orelse json.asU64(row.get("len")) orelse return null; if (new_len <= old_len) return null; return new_len - old_len;}test "profiling allocation totals summarize events" { var arena = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena.deinit(); const allocator = arena.allocator(); const value = try std.json.parseFromSliceLeaky(std.json.Value, allocator, \\{"kind":"resize","old_len":64,"new_len":96} , .{}); var totals = Totals{}; totals.record(try json.object(value)); try std.testing.expect(totals.saw); try std.testing.expectEqual(@as(u64, 1), totals.resize_events); try std.testing.expectEqual(@as(u128, 32), totals.allocated_bytes); try std.testing.expect(isMetricName("allocated_bytes")); try std.testing.expect(!isMetricName("other"));}Source: src/profiling/root.zig:12
zig
pub const allocation = @import("allocation.zig");Audit
| Definitions | 6 |
|---|---|
| Public names | 6 |
| Members | 9 |
| Version | 26.7.0 |
| Revision | daab053ee433 |