tiny.profiling.ingest.output
Defined in ingest.
API (1)
Actions
Public operations.
Source
Source: src/profiling/ingest/output.zig
zig
const std = @import("std");const pretty = @import("pretty");const sys = @import("sys");const profiling = @import("../root.zig");const ingest = @import("root.zig");const pretty_json = pretty.json;const RowContext = struct { writer: *std.Io.Writer, workload: profiling.catalog.Workload, source: ingest.Source, row_bits: []const u8, candidate_capacity: usize, candidate_index: usize, rows: usize,};pub fn write( paths: ingest.Paths, facts: ingest.Facts, workload: profiling.catalog.Workload, row_bits: []const u8, candidate_capacity: usize, line_storage: []u8, totals: profiling.allocation.Totals, expected: ingest.Summary,) !ingest.Summary { std.debug.assert(facts.sources.len == ingest.model.source_count); std.debug.assert(expected.sources <= ingest.model.source_count); std.debug.assert(candidate_capacity / 8 <= row_bits.len); if (candidate_capacity % 8 > 0) std.debug.assert(candidate_capacity / 8 < row_bits.len); for (facts.sources) |source| { std.debug.assert(source.max_line_bytes <= line_storage.len); } var path_storage: [std.fs.max_path_bytes]u8 = undefined; const pending_path = try std.fmt.bufPrint( &path_storage, "{s}.ingest.pending", .{paths.structured}, ); std.debug.assert(pending_path.len > paths.structured.len); std.debug.assert(pending_path.len <= path_storage.len); var file = try sys.fs.createFile(pending_path, .{ .truncate = true }); var file_open = true; defer if (file_open) file.close(sys.fs.debugIo()); defer sys.fs.deleteFile(pending_path) catch {}; var buffer: [64 * 1024]u8 = undefined; var file_writer = file.writer(sys.fs.debugIo(), &buffer); const sources = paths.sources(); var context = RowContext{ .writer = &file_writer.interface, .workload = workload, .source = undefined, .row_bits = row_bits, .candidate_capacity = candidate_capacity, .candidate_index = 0, .rows = 0, }; try writeOrdinarySources(&context, facts, &sources, line_storage); if (context.candidate_index != candidate_capacity) return error.InputChanged; std.debug.assert(context.candidate_index == candidate_capacity); try writeMetrics(&context, totals, sources[ingest.model.ordinary_source_count]); const allocation_facts = try ingest.inspection.inspectSource( sources[ingest.model.ordinary_source_count], ); if (!facts.sources[ingest.model.ordinary_source_count].eql(allocation_facts)) { return error.InputChanged; } if (context.rows != expected.rows) return error.InputChanged; std.debug.assert(context.rows == expected.rows); try file_writer.interface.flush(); file.close(sys.fs.debugIo()); file_open = false; try sys.fs.rename(pending_path, paths.structured); return expected;}fn writeOrdinarySources( context: *RowContext, facts: ingest.Facts, sources: *const [ingest.model.source_count]ingest.Source, line_storage: []u8,) !void { for (sources[0..ingest.model.ordinary_source_count], 0..) |source, index| { std.debug.assert(index < ingest.model.ordinary_source_count); context.source = source; const actual = try ingest.inspection.visitSource( source, line_storage, context, writeCandidate, ); if (!facts.sources[index].eql(actual)) return error.InputChanged; }}fn writeMetrics( context: *RowContext, totals: profiling.allocation.Totals, source: ingest.Source,) !void { if (!totals.saw) return; for (totals.metrics()) |metric| { try writeMemoryMetricRow(context.writer, context.workload, source, metric); context.rows += 1; } std.debug.assert(totals.metrics().len > 0);}fn writeCandidate(context: *RowContext, raw: []const u8, line_number: usize) !void { std.debug.assert(raw.len >= 2); std.debug.assert(line_number > 0); std.debug.assert(context.candidate_index <= context.candidate_capacity); const index = context.candidate_index; context.candidate_index = std.math.add(usize, index, 1) catch return error.InputChanged; if (index >= context.candidate_capacity) return error.InputChanged; if (!bitIsSet(context.row_bits, index)) return; try writeRow(context.writer, context.workload, context.source, line_number, raw); context.rows = std.math.add(usize, context.rows, 1) catch return error.InputChanged;}fn bitIsSet(bits: []const u8, index: usize) bool { const byte_index = index / 8; const bit_index: u3 = @intCast(index % 8); std.debug.assert(byte_index < bits.len); std.debug.assert(bit_index < 8); return bits[byte_index] & (@as(u8, 1) << bit_index) != 0;}fn writeRow( writer: *std.Io.Writer, workload: profiling.catalog.Workload, source: ingest.Source, line_number: usize, raw: []const u8,) !void { std.debug.assert(raw.len >= 2); std.debug.assert(raw[0] == '{'); std.debug.assert(raw[raw.len - 1] == '}'); std.debug.assert(line_number > 0); var out = pretty_json.Writer.init(writer, .minified); try out.beginObject(); try out.objectField("schema"); try out.write(ingest.schema); try out.objectField("workload"); try writeWorkload(&out, workload); try out.objectField("source"); try out.beginObject(); try out.objectField("kind"); try out.write(source.kind.name()); try out.objectField("path"); try out.write(source.path); try out.objectField("line"); try out.write(line_number); try out.endObject(); try out.objectField("row"); try out.raw(raw); try out.endObject(); try writer.writeByte('\n');}fn writeMemoryMetricRow( writer: *std.Io.Writer, workload: profiling.catalog.Workload, source: ingest.Source, metric: profiling.allocation.Metric,) !void { std.debug.assert(source.kind.isAllocation()); std.debug.assert(metric.name.len > 0); std.debug.assert(metric.unit.len > 0); var out = pretty_json.Writer.init(writer, .minified); try out.beginObject(); try out.objectField("schema"); try out.write(ingest.schema); try out.objectField("workload"); try writeWorkload(&out, workload); try out.objectField("source"); try out.beginObject(); try out.objectField("kind"); try out.write(source.kind.name()); try out.objectField("path"); try out.write(source.path); try out.objectField("line"); try out.write(0); try out.endObject(); try out.objectField("row"); try out.beginObject(); try out.objectField("schema"); try out.write(profiling.schema.metric_schema); try out.objectField("family"); try out.write("memory"); try out.objectField("metric"); try out.write(metric.name); try out.objectField("name"); try out.write(metric.name); try out.objectField("unit"); try out.write(metric.unit); try out.objectField("value"); try out.write(metric.value); try out.endObject(); try out.endObject(); try writer.writeByte('\n');}fn writeWorkload(out: *pretty_json.Writer, workload: profiling.catalog.Workload) !void { try out.beginObject(); try out.objectField("name"); try out.write(workload.name); try out.objectField("package"); try out.write(workload.package); try out.objectField("step"); try out.write(workload.step); try out.endObject();}Source: src/profiling/ingest/root.zig:11
zig
pub const output = @import("output.zig");Audit
| Definitions | 2 |
|---|---|
| Public names | 2 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |