tiny.profiling.record
Defined in tiny.profiling.
API (29)
Actions
Public operations.
appendWorkloadcollectMetadatadefaultRunIdexecutionPathsfinishinitmakePathsmakeWorkloadPathsprefixedRunIdresetPathssanitizeSegmentworkloadPaths
Types and contracts
Public types and contracts.
ExecutionPathsMeasuredExecutionMetadataPathsResetResetPathsResetPhaseSetupSummaryWarmupPathsWorkloadWorkloadPaths
Values and defaults
Public values and defaults.
Source
Source: src/profiling/record.zig
zig
const std = @import("std");const builtin = @import("builtin");const capture = @import("capture");const pretty = @import("pretty");const sys = @import("sys");const catalog = @import("root.zig").catalog;const environment = @import("root.zig").environment;const execute = @import("root.zig").execute;const host = @import("root.zig").host;const json_util = @import("root.zig").json;const order = @import("root.zig").order;const plan = @import("root.zig").plan;const perturbation = @import("root.zig").perturbation;const reducer = @import("root.zig").reduction;const pretty_json = pretty.json;pub const run_schema = "tiny.profiling.run/v1";pub const workload_schema = "tiny.profiling.workload/v4";pub const default_output_dir = "zig-out/profiling";pub const retention_policy = "local-kept-until-cleaned-ci-14-days";pub const maximum_reset_receipts: usize = host.process.max_executions * 2;pub const Metadata = struct { run_id: []const u8, artifact_root: []const u8, suite: catalog.Suite, filters: []const []const u8, workload_count: usize, started_unix_ns: i128, git_sha: ?[]const u8, git_branch: ?[]const u8, git_dirty: ?bool, zig_version: []const u8, host: environment.Host, host_state_start: host.state.Snapshot = .{}, optimize: std.builtin.OptimizeMode,};pub const Paths = struct { root: []const u8, manifest: []const u8, results: []const u8, workloads: []const u8,};pub const WorkloadPaths = struct { root: []const u8, command: []const u8, result: []const u8, stdout: []const u8, stderr: []const u8, setup_stdout: []const u8, setup_stderr: []const u8, prepare_stdout: []const u8, prepare_stderr: []const u8, bench_jsonl: []const u8, coz_jsonl: []const u8, coz_analysis: []const u8, tracy_jsonl: []const u8, tracy_summary: []const u8, allocations: []const u8, allocations_summary: []const u8, allocation_repetitions: []const u8, structured: []const u8, warmup: WarmupPaths,};pub const ExecutionPaths = struct { root: []const u8, stdout: []const u8, stderr: []const u8, bench_jsonl: []const u8, coz_jsonl: []const u8, coz_analysis: []const u8, tracy_jsonl: []const u8, tracy_summary: []const u8, allocations: []const u8, structured: []const u8,};pub const MeasuredExecution = struct { index: usize, result: execute.Result, paths: ?ExecutionPaths, acquisition_position: ?usize = null, structured_rows: usize = 0, structured_parse_errors: usize = 0,};pub const WarmupPaths = struct { root: []const u8, stdout: []const u8, stderr: []const u8, bench_jsonl: []const u8, coz_jsonl: []const u8, coz_analysis: []const u8, tracy_jsonl: []const u8, tracy_summary: []const u8, allocations: []const u8,};pub const Setup = struct { command: []const u8, pid: ?u64, exit_code: i64, wall_ns: u64, stdout_path: []const u8, stderr_path: []const u8,};pub const ResetPhase = enum { warmup, measurement, counter, capture, capture_control,};pub const ResetPaths = struct { stdout: []const u8, stderr: []const u8,};pub const Reset = struct { sequence: usize, phase: ResetPhase, phase_index: usize, command: execute.CommandResult,};pub const Workload = struct { index: usize, acquisition: order.Context, workload: catalog.Workload, runtime: catalog.Runtime, argv: []const []const u8, command: []const u8, cwd: ?[]const u8 = null, scope: []const u8 = host.scope, causal: bool = false, tracy: bool = false, trace_allocations: bool = false, bench_min_time_ns: ?u64 = null, causal_filter: ?[]const u8 = null, setup: ?Setup = null, prepare: ?execute.CommandResult = null, artifact_root: []const u8, command_path: []const u8, result_path: []const u8, stdout_path: []const u8, stderr_path: []const u8, bench_jsonl_path: []const u8, coz_jsonl_path: []const u8, coz_analysis_path: []const u8, tracy_jsonl_path: []const u8, tracy_summary_path: []const u8, allocations_path: []const u8, allocation_repetitions_path: []const u8, structured_path: []const u8, structured_rows: usize, structured_parse_errors: usize, started_unix_ns: i128, finished_unix_ns: i128, execution: execute.Result, executions: []const MeasuredExecution = &.{}, benchmark_process_domain: reducer.DomainDeclaration, benchmark_process_reduction: reducer.Result, host_state_windows: []const host.state.Window = &.{}, warmup_executions: []const execute.Result = &.{}, warmup_paths: ?WarmupPaths = null, resets: []const Reset = &.{}, perturbation: ?perturbation.Measurement = null, captures: []const host.Capture = &.{},};pub const Summary = struct { selected: usize, ran: usize, passed: usize, failed: usize, stopped_early: bool, exit_code: u8, started_unix_ns: i128, finished_unix_ns: i128, wall_ns: u64,};pub fn defaultRunId(allocator: std.mem.Allocator, process_io: std.Io) ![]const u8 { return try prefixedRunId(allocator, process_io, "run");}pub fn prefixedRunId(allocator: std.mem.Allocator, process_io: std.Io, prefix: []const u8) ![]const u8 { const millis = sys.time.realMilliTimestamp(); if (try gitCapture(allocator, process_io, &.{ "git", "rev-parse", "--short=12", "HEAD" })) |sha| { return try std.fmt.allocPrint(allocator, "{s}-{d}-{s}", .{ prefix, millis, sha }); } return try std.fmt.allocPrint(allocator, "{s}-{d}", .{ prefix, millis });}pub fn makePaths(allocator: std.mem.Allocator, output_dir: []const u8, run_id: []const u8) !Paths { const root = try std.fs.path.join(allocator, &.{ output_dir, run_id }); return .{ .root = root, .manifest = try std.fs.path.join(allocator, &.{ root, "manifest.json" }), .results = try std.fs.path.join(allocator, &.{ root, "results.jsonl" }), .workloads = try std.fs.path.join(allocator, &.{ root, "workloads" }), };}pub fn collectMetadata( allocator: std.mem.Allocator, process_io: std.Io, paths: Paths, selection: plan.Selection, optimize: std.builtin.OptimizeMode,) !Metadata { return .{ .run_id = std.fs.path.basename(paths.root), .artifact_root = paths.root, .suite = selection.suite, .filters = selection.filters, .workload_count = selection.count(), .started_unix_ns = sys.time.realNanoTimestamp(), .git_sha = try gitCapture(allocator, process_io, &.{ "git", "rev-parse", "HEAD" }), .git_branch = try gitCapture(allocator, process_io, &.{ "git", "branch", "--show-current" }), .git_dirty = try gitDirty(allocator, process_io), .zig_version = builtin.zig_version_string, .host = try environment.collect(allocator, process_io), .host_state_start = host.state.capture(), .optimize = optimize, };}pub fn init(allocator: std.mem.Allocator, paths: Paths, metadata: Metadata) !void { try sys.fs.createDirPath(paths.workloads); try appendRunStart(allocator, paths, metadata);}pub fn workloadPaths(allocator: std.mem.Allocator, paths: Paths, name: []const u8) !WorkloadPaths { const segment = try sanitizeSegment(allocator, name); const root = try std.fs.path.join(allocator, &.{ paths.workloads, segment }); const warmup_root = try std.fs.path.join(allocator, &.{ root, "warmup" }); return .{ .root = root, .command = try std.fs.path.join(allocator, &.{ root, "command.json" }), .result = try std.fs.path.join(allocator, &.{ root, "result.json" }), .stdout = try std.fs.path.join(allocator, &.{ root, "stdout.txt" }), .stderr = try std.fs.path.join(allocator, &.{ root, "stderr.txt" }), .setup_stdout = try std.fs.path.join(allocator, &.{ root, "setup.stdout.txt" }), .setup_stderr = try std.fs.path.join(allocator, &.{ root, "setup.stderr.txt" }), .prepare_stdout = try std.fs.path.join(allocator, &.{ root, "prepare.stdout.txt" }), .prepare_stderr = try std.fs.path.join(allocator, &.{ root, "prepare.stderr.txt" }), .bench_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.jsonl" }), .coz_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.coz.jsonl" }), .coz_analysis = try std.fs.path.join(allocator, &.{ root, "bench.coz.analysis.json" }), .tracy_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.tracy.jsonl" }), .tracy_summary = try std.fs.path.join(allocator, &.{ root, "bench.tracy.summary.jsonl" }), .allocations = try std.fs.path.join(allocator, &.{ root, "allocations.jsonl" }), .allocations_summary = try std.fs.path.join( allocator, &.{ root, "allocations.summary.jsonl" }, ), .allocation_repetitions = try std.fs.path.join( allocator, &.{ root, "allocations.repetitions.jsonl" }, ), .structured = try std.fs.path.join(allocator, &.{ root, "structured.jsonl" }), .warmup = .{ .root = warmup_root, .stdout = try std.fs.path.join(allocator, &.{ warmup_root, "stdout.txt" }), .stderr = try std.fs.path.join(allocator, &.{ warmup_root, "stderr.txt" }), .bench_jsonl = try std.fs.path.join(allocator, &.{ warmup_root, "bench.jsonl" }), .coz_jsonl = try std.fs.path.join(allocator, &.{ warmup_root, "bench.coz.jsonl" }), .coz_analysis = try std.fs.path.join(allocator, &.{ warmup_root, "bench.coz.analysis.json" }), .tracy_jsonl = try std.fs.path.join(allocator, &.{ warmup_root, "bench.tracy.jsonl" }), .tracy_summary = try std.fs.path.join(allocator, &.{ warmup_root, "bench.tracy.summary.jsonl" }), .allocations = try std.fs.path.join(allocator, &.{ warmup_root, "allocations.jsonl" }), }, };}pub fn makeWorkloadPaths(allocator: std.mem.Allocator, paths: Paths, name: []const u8) !WorkloadPaths { const result = try workloadPaths(allocator, paths, name); try sys.fs.createDirPath(result.root); return result;}pub fn executionPaths( allocator: std.mem.Allocator, workload_paths: WorkloadPaths, execution_count: usize, execution_index: usize,) !ExecutionPaths { if (execution_count == 0 or execution_count > host.process.max_executions) { return error.InvalidExecutionArtifactCount; } if (execution_index == 0 or execution_index > execution_count) { return error.InvalidExecutionArtifactIndex; } if (execution_count == 1) return rootExecutionPaths(workload_paths); const segment = try std.fmt.allocPrint(allocator, "{d:0>3}", .{execution_index}); defer allocator.free(segment); const root = try std.fs.path.join( allocator, &.{ workload_paths.root, "executions", segment }, ); return try pathsAtRoot(allocator, root);}fn rootExecutionPaths(paths: WorkloadPaths) ExecutionPaths { return .{ .root = paths.root, .stdout = paths.stdout, .stderr = paths.stderr, .bench_jsonl = paths.bench_jsonl, .coz_jsonl = paths.coz_jsonl, .coz_analysis = paths.coz_analysis, .tracy_jsonl = paths.tracy_jsonl, .tracy_summary = paths.tracy_summary, .allocations = paths.allocations, .structured = paths.structured, };}fn pathsAtRoot( allocator: std.mem.Allocator, root: []const u8,) !ExecutionPaths { return .{ .root = root, .stdout = try std.fs.path.join(allocator, &.{ root, "stdout.txt" }), .stderr = try std.fs.path.join(allocator, &.{ root, "stderr.txt" }), .bench_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.jsonl" }), .coz_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.coz.jsonl" }), .coz_analysis = try std.fs.path.join( allocator, &.{ root, "bench.coz.analysis.json" }, ), .tracy_jsonl = try std.fs.path.join(allocator, &.{ root, "bench.tracy.jsonl" }), .tracy_summary = try std.fs.path.join( allocator, &.{ root, "bench.tracy.summary.jsonl" }, ), .allocations = try std.fs.path.join(allocator, &.{ root, "allocations.jsonl" }), .structured = try std.fs.path.join(allocator, &.{ root, "structured.jsonl" }), };}pub fn resetPaths( allocator: std.mem.Allocator, workload_root: []const u8, sequence: usize, phase: ResetPhase, phase_index: usize,) !ResetPaths { std.debug.assert(workload_root.len > 0); std.debug.assert(sequence > 0); std.debug.assert(sequence <= maximum_reset_receipts); std.debug.assert(phase_index > 0); const root = try std.fs.path.join(allocator, &.{ workload_root, "resets" }); try sys.fs.createDirPath(root); const prefix = try std.fmt.allocPrint( allocator, "{d:0>3}-{s}-{d:0>3}", .{ sequence, @tagName(phase), phase_index }, ); return .{ .stdout = try std.fs.path.join( allocator, &.{ root, try std.fmt.allocPrint(allocator, "{s}.stdout.txt", .{prefix}) }, ), .stderr = try std.fs.path.join( allocator, &.{ root, try std.fmt.allocPrint(allocator, "{s}.stderr.txt", .{prefix}) }, ), };}pub fn appendWorkload(allocator: std.mem.Allocator, paths: Paths, metadata: Metadata, value: Workload) !void { const line = try renderJsonLine(allocator, writeWorkloadJson, .{ metadata, value }); defer allocator.free(line); try sys.fs.appendFile(paths.results, &.{line}); const result_json = try renderJsonDocument(allocator, writeWorkloadJson, .{ metadata, value }); defer allocator.free(result_json); try sys.fs.writeFile(value.result_path, result_json); const command_json = try renderJsonDocument(allocator, writeCommandJson, .{value}); defer allocator.free(command_json); try sys.fs.writeFile(value.command_path, command_json);}pub fn finish(allocator: std.mem.Allocator, paths: Paths, metadata: Metadata, summary: Summary) !void { const line = try renderJsonLine(allocator, writeRunEndJson, .{ metadata, summary }); defer allocator.free(line); try sys.fs.appendFile(paths.results, &.{line}); const manifest = try renderJsonDocument(allocator, writeManifestJson, .{ metadata, summary, paths }); defer allocator.free(manifest); try sys.fs.writeFile(paths.manifest, manifest);}fn appendRunStart(allocator: std.mem.Allocator, paths: Paths, metadata: Metadata) !void { const line = try renderJsonLine(allocator, writeRunStartJson, .{metadata}); defer allocator.free(line); try sys.fs.writeFile(paths.results, line);}fn renderJsonLine( allocator: std.mem.Allocator, comptime writer_fn: anytype, args: anytype,) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); errdefer out.deinit(); var json = pretty_json.Writer.init(&out.writer, .minified); try @call(.auto, writer_fn, .{&json} ++ args); try out.writer.writeByte('\n'); return try out.toOwnedSlice();}fn renderJsonDocument( allocator: std.mem.Allocator, comptime writer_fn: anytype, args: anytype,) ![]const u8 { var out: std.Io.Writer.Allocating = .init(allocator); errdefer out.deinit(); var json = pretty_json.Writer.init(&out.writer, .minified); try @call(.auto, writer_fn, .{&json} ++ args); try out.writer.writeByte('\n'); return try out.toOwnedSlice();}fn writeRunStartJson(out: *pretty_json.Writer, metadata: Metadata) !void { try out.beginObject(); try writeRunHeader(out, metadata, "run_start"); try out.objectField("selection"); try writeSelectionJson(out, metadata); try out.objectField("environment"); try writeEnvironmentJson(out, metadata); try out.objectField("artifacts"); try out.beginObject(); try out.objectField("root"); try out.write(metadata.artifact_root); try out.objectField("results"); try out.write("results.jsonl"); try out.objectField("manifest"); try out.write("manifest.json"); try out.objectField("retention"); try writeRetentionJson(out); try out.endObject(); try out.endObject();}fn writeRunEndJson(out: *pretty_json.Writer, metadata: Metadata, summary: Summary) !void { try out.beginObject(); try writeRunHeader(out, metadata, "run_end"); try out.objectField("summary"); try writeSummaryJson(out, summary); try out.endObject();}fn writeManifestJson(out: *pretty_json.Writer, metadata: Metadata, summary: Summary, paths: Paths) !void { try out.beginObject(); try writeRunHeader(out, metadata, "manifest"); try out.objectField("selection"); try writeSelectionJson(out, metadata); try out.objectField("environment"); try writeEnvironmentJson(out, metadata); try out.objectField("summary"); try writeSummaryJson(out, summary); try out.objectField("artifacts"); try out.beginObject(); try out.objectField("root"); try out.write(paths.root); try out.objectField("results"); try out.write(paths.results); try out.objectField("manifest"); try out.write(paths.manifest); try out.objectField("workloads"); try out.write(paths.workloads); try out.objectField("retention"); try writeRetentionJson(out); try out.endObject(); try out.endObject();}fn writeRetentionJson(out: *pretty_json.Writer) !void { try out.beginObject(); try out.objectField("policy"); try out.write(retention_policy); try out.objectField("local"); try out.write("kept under zig-out/profiling until cleaned by the developer or workspace"); try out.objectField("ci_upload"); try out.write("uploaded as the profiling workflow artifact with 14 day retention"); try out.endObject();}fn writeRunHeader(out: *pretty_json.Writer, metadata: Metadata, event: []const u8) !void { try out.objectField("schema"); try out.write(run_schema); try out.objectField("event"); try out.write(event); try out.objectField("run_id"); try out.write(metadata.run_id); try out.objectField("started_unix_ns"); try out.write(metadata.started_unix_ns);}fn writeSelectionJson(out: *pretty_json.Writer, metadata: Metadata) !void { try out.beginObject(); try out.objectField("suite"); try out.write(metadata.suite.name()); try out.objectField("filters"); try out.beginArray(); for (metadata.filters) |filter| try out.write(filter); try out.endArray(); try out.objectField("workload_count"); try out.write(metadata.workload_count); try out.endObject();}fn writeEnvironmentJson(out: *pretty_json.Writer, metadata: Metadata) !void { try out.beginObject(); try out.objectField("git"); try out.beginObject(); try out.objectField("commit"); try out.write(metadata.git_sha); try out.objectField("branch"); try out.write(metadata.git_branch); try out.objectField("dirty"); try out.write(metadata.git_dirty); try out.endObject(); try out.objectField("zig"); try out.beginObject(); try out.objectField("version"); try out.write(metadata.zig_version); try out.endObject(); try out.objectField("build"); try out.beginObject(); try out.objectField("optimize"); try out.write(optimizeName(metadata.optimize)); try out.endObject(); try out.objectField("host"); try environment.writeJson(out, metadata.host); try out.objectField("host_state_start"); try host.state.writeJson(out, metadata.host_state_start); try out.endObject();}fn optimizeName(optimize: std.builtin.OptimizeMode) []const u8 { return switch (optimize) { .debug => "Debug", .safe => "ReleaseSafe", .fast => "ReleaseFast", .small => "ReleaseSmall", };}fn writeSummaryJson(out: *pretty_json.Writer, summary: Summary) !void { try out.beginObject(); try out.objectField("selected"); try out.write(summary.selected); try out.objectField("ran"); try out.write(summary.ran); try out.objectField("passed"); try out.write(summary.passed); try out.objectField("failed"); try out.write(summary.failed); try out.objectField("stopped_early"); try out.write(summary.stopped_early); try out.objectField("exit_code"); try out.write(summary.exit_code); try out.objectField("started_unix_ns"); try out.write(summary.started_unix_ns); try out.objectField("finished_unix_ns"); try out.write(summary.finished_unix_ns); try out.objectField("wall_ns"); try out.write(summary.wall_ns); try out.endObject();}fn writeWorkloadJson(out: *pretty_json.Writer, metadata: Metadata, value: Workload) !void { try out.beginObject(); try out.objectField("schema"); try out.write(workload_schema); try out.objectField("event"); try out.write("workload"); try out.objectField("run_id"); try out.write(metadata.run_id); try out.objectField("index"); try out.write(value.index); try out.objectField("acquisition"); try order.writeJson(out, value.acquisition); try out.objectField("workload"); try writeCatalogWorkloadJson(out, value.workload); try out.objectField("runtime"); try writeRuntimeJson(out, value.runtime); try out.objectField("command"); try writeCommandJson(out, value); try writeWorkloadConfigurationFields(out, value); try out.objectField("setup"); try writeSetupJson(out, value.setup); try out.objectField("prepare"); try writePrepareJson(out, value.prepare); try out.objectField("status"); try writeStatusJson(out, value.execution); try out.objectField("timing"); try writeTimingJson(out, value); try out.objectField("resources"); try out.beginObject(); try writeResourceUsageFields(out, value.execution); try out.endObject(); try out.objectField("measurement"); try writeMeasurementJson( out, value.executions, value.acquisition, value.trace_allocations, value.artifact_root, value.benchmark_process_domain, value.benchmark_process_reduction, ); try out.objectField("host_state"); try host.state.writeWindowsJson(out, value.host_state_windows); try out.objectField("warmup"); try writeWarmupJson(out, value.warmup_executions, value.warmup_paths); try out.objectField("resets"); try writeResetsJson(out, value.resets); try out.objectField("capture_perturbation"); try writeCapturePerturbationJson(out, value.perturbation); try out.objectField("artifacts"); try writeArtifactsJson(out, value); try out.objectField("captures"); try writeCapturesJson(out, value.captures); try out.endObject();}fn writeWorkloadConfigurationFields(out: *pretty_json.Writer, value: Workload) !void { try out.objectField("scope"); try out.write(value.scope); try out.objectField("causal"); try out.write(value.causal); try out.objectField("tracy"); try out.write(value.tracy); try out.objectField("trace_allocations"); try out.write(value.trace_allocations); if (value.bench_min_time_ns) |min_time_ns| { try out.objectField("bench_min_time_ns"); try out.write(min_time_ns); } if (value.causal_filter) |filter| { try out.objectField("causal_filter"); try out.write(filter); }}fn writeSetupJson(out: *pretty_json.Writer, setup: ?Setup) !void { if (setup) |actual| { try out.beginObject(); try out.objectField("command"); try out.write(actual.command); try out.objectField("pid"); try out.write(actual.pid); try out.objectField("exit_code"); try out.write(actual.exit_code); try out.objectField("wall_ns"); try out.write(actual.wall_ns); try out.objectField("stdout"); try out.write(actual.stdout_path); try out.objectField("stderr"); try out.write(actual.stderr_path); try out.endObject(); } else { try out.write(null); }}fn writePrepareJson( out: *pretty_json.Writer, prepare: ?execute.CommandResult,) !void { const actual = prepare orelse { try out.write(null); return; }; try out.beginObject(); try out.objectField("policy"); try out.write("once_after_successful_build_setup_before_acquisitions"); try out.objectField("timing"); try out.write("outside_measured_child_wait"); try writeRecordedCommandFields(out, actual); try out.endObject();}fn writeStatusJson(out: *pretty_json.Writer, result: execute.Result) !void { try out.beginObject(); try out.objectField("state"); try out.write(if (result.exit_code == 0) "passed" else "failed"); try out.objectField("pid"); try out.write(result.pid); try out.objectField("exit_code"); try out.write(result.exit_code); try out.endObject();}fn writeTimingJson(out: *pretty_json.Writer, value: Workload) !void { try out.beginObject(); try out.objectField("started_unix_ns"); try out.write(value.started_unix_ns); try out.objectField("finished_unix_ns"); try out.write(value.finished_unix_ns); try out.objectField("wall_ns"); try out.write(value.execution.wall_ns); try out.endObject();}fn writeArtifactsJson(out: *pretty_json.Writer, value: Workload) !void { try out.beginObject(); try out.objectField("root"); try out.write(value.artifact_root); try out.objectField("command"); try out.write(value.command_path); try out.objectField("result"); try out.write(value.result_path); try out.objectField("allocation_repetitions"); try out.write(value.allocation_repetitions_path); if (measuredArtifactMode(value.executions) == .profiler_capture_contract) { try writeProfilerArtifactsJson(out, value); } try out.endObject();}fn writeProfilerArtifactsJson(out: *pretty_json.Writer, value: Workload) !void { inline for (.{ .{ "stdout", value.stdout_path }, .{ "stderr", value.stderr_path }, .{ "bench_jsonl", value.bench_jsonl_path }, .{ "coz_jsonl", value.coz_jsonl_path }, .{ "coz_analysis", value.coz_analysis_path }, .{ "tracy_jsonl", value.tracy_jsonl_path }, .{ "tracy_summary", value.tracy_summary_path }, .{ "allocations", value.allocations_path }, .{ "structured", value.structured_path }, }) |field| { try out.objectField(field[0]); try out.write(field[1]); } try out.objectField("structured_rows"); try out.write(value.structured_rows); try out.objectField("structured_parse_errors"); try out.write(value.structured_parse_errors);}fn writeCapturesJson(out: *pretty_json.Writer, captures: []const host.Capture) !void { try out.beginArray(); for (captures) |row| { try out.beginObject(); try out.objectField("kind"); try out.write(row.kind); try out.objectField("tool"); try out.write(row.tool); try out.objectField("tool_version"); try out.write(row.tool_version); try out.objectField("scope"); try out.write(row.scope); try out.objectField("capture"); try out.write(row.capture_path); try out.objectField("summary"); try out.write(row.summary_path); try out.objectField("state"); try out.write(row.state); try out.objectField("caveat_kind"); try out.write(row.caveat_kind); try out.objectField("caveat_message"); try out.write(row.caveat_message); try out.endObject(); } try out.endArray();}fn writeCapturePerturbationJson( out: *pretty_json.Writer, measurement: ?perturbation.Measurement,) !void { const actual = measurement orelse { try out.write(null); return; }; std.debug.assert(actual.pairs.len <= perturbation.max_pairs); std.debug.assert(actual.configuration.any()); try out.beginObject(); try out.objectField("state"); try out.write(@tagName(actual.state)); try out.objectField("method"); try out.write("same_build_matched_control_capture"); try out.objectField("effect_method"); try out.write(perturbation.effect_method); try out.objectField("effect_bootstrap_iterations"); try out.write(capture.compare.effect.bootstrap_iterations); try out.objectField("effect_confidence_per_mille"); try out.write(capture.compare.effect.confidence_per_mille); try out.objectField("pair_count_requested"); try out.write(actual.pairs.len); try out.objectField("base_seed"); try out.write(actual.base_seed); try out.objectField("workload_seed"); try out.write(actual.workload_seed); try out.objectField("order"); try out.write("seeded_balanced_within_pair"); try out.objectField("setup_reuse"); try out.write("single_setup"); try out.objectField("binary_identity"); try out.write("same_built_binary"); try out.objectField("capture_artifact_retention"); try out.write("last_capture_execution"); try out.objectField("control_output_retention"); try out.write("last_control_execution"); try out.objectField("active_configuration"); try out.beginObject(); try out.objectField("host_kind"); try out.write(actual.configuration.host_kind); try out.objectField("tracy"); try out.write(actual.configuration.tracy); try out.objectField("allocations"); try out.write(actual.configuration.allocations); try out.endObject(); try out.objectField("control_artifacts"); try writeControlArtifactsJson(out, actual); try out.objectField("pairs"); try out.beginArray(); for (actual.pairs) |pair| { try out.beginObject(); try out.objectField("index"); try out.write(pair.index); try out.objectField("order"); try out.write(@tagName(pair.order)); try out.objectField("control"); try writeExecutionResultJson(out, pair.control); try out.objectField("capture"); try writeExecutionResultJson(out, pair.capture); try out.endObject(); } try out.endArray(); try out.endObject();}fn writeControlArtifactsJson( out: *pretty_json.Writer, measurement: perturbation.Measurement,) !void { const paths = measurement.control_artifacts; try out.beginObject(); try out.objectField("stdout"); try out.write(paths.stdout); try out.objectField("stderr"); try out.write(paths.stderr); try out.objectField("bench_jsonl"); try out.write(paths.bench_jsonl); try out.objectField("coz_jsonl"); try out.write(paths.coz_jsonl); try out.objectField("coz_analysis"); try out.write(paths.coz_analysis); try out.objectField("tracy_jsonl"); try out.write(paths.tracy_jsonl); try out.objectField("tracy_summary"); try out.write(paths.tracy_summary); try out.objectField("allocations"); try out.write(paths.allocations); try out.objectField("structured"); try out.write(paths.structured); try out.objectField("structured_rows"); try out.write(measurement.control_structured_rows); try out.objectField("structured_parse_errors"); try out.write(measurement.control_structured_parse_errors); try out.endObject();}fn writeExecutionResultJson( out: *pretty_json.Writer, result: ?execute.Result,) !void { const actual = result orelse { try out.write(null); return; }; try out.beginObject(); try writeSpawnStateJson(out, actual); try out.objectField("pid"); try out.write(actual.pid); try out.objectField("exit_code"); try out.write(actual.exit_code); try out.objectField("wall_ns"); try out.write(actual.wall_ns); try writeResourceUsageFields(out, actual); try out.endObject();}fn writeSpawnStateJson(out: *pretty_json.Writer, result: execute.Result) !void { try out.objectField("spawn_state"); try out.write(if (result.pid == null) "spawn_failed" else "spawned");}fn writeResourceUsageFields(out: *pretty_json.Writer, result: execute.Result) !void { try out.objectField("resource_usage_source"); if (result.resource_usage_source) |source| { try out.write(@tagName(source)); } else { try out.write(null); } try out.objectField("max_rss_kib"); try out.write(result.maxrss_kib); try out.objectField("user_s"); try out.write(result.user_s); try out.objectField("system_s"); try out.write(result.system_s); try out.objectField("minor_page_faults"); try out.write(result.minor_page_faults); try out.objectField("major_page_faults"); try out.write(result.major_page_faults); try out.objectField("voluntary_context_switches"); try out.write(result.voluntary_context_switches); try out.objectField("involuntary_context_switches"); try out.write(result.involuntary_context_switches);}fn writeAggregateReductionJson(out: *pretty_json.Writer) !void { try out.beginObject(); try out.objectField("pid"); try out.write("null_for_multiple_executions_or_external_failure"); try out.objectField("wall_ns"); try out.write("sum"); try out.objectField("resource_usage_source"); try out.write("require_equal"); try out.objectField("max_rss_kib"); try out.write("maximum"); const sum_fields = [_][]const u8{ "user_s", "system_s", "minor_page_faults", "major_page_faults", "voluntary_context_switches", "involuntary_context_switches", }; for (sum_fields) |field| { try out.objectField(field); try out.write("sum"); } try out.endObject();}fn writeMeasurementJson( out: *pretty_json.Writer, executions: []const MeasuredExecution, acquisition: order.Context, trace_allocations: bool, workload_root: []const u8, benchmark_process_domain: reducer.DomainDeclaration, benchmark_process_reduction: reducer.Result,) !void { std.debug.assert(executions.len <= host.process.max_executions); try out.beginObject(); try out.objectField("execution_count"); try out.write(executions.len); try out.objectField("acquisition_order"); try out.write(acquisition.method().acquisitionName()); try out.objectField("workload_output_retention"); const artifact_mode = measuredArtifactMode(executions); try out.write(artifact_mode.name()); try out.objectField("measured_artifact_layout"); try out.write(measuredArtifactLayout(executions, artifact_mode, workload_root)); try out.objectField("allocation_summary_retention"); try out.write(if (!trace_allocations) "disabled" else if (artifact_mode == .no_measured_execution) "no_measured_execution" else if (artifact_mode == .profiler_capture_contract) "profiler_capture_contract" else if (executions.len > 1) "all_measured_executions" else "single_measured_execution"); try out.objectField("aggregate_reduction"); try writeAggregateReductionJson(out); try out.objectField("benchmark_process_domain"); try reducer.writeDomainJson(out, benchmark_process_domain); try out.objectField("benchmark_process_reduction"); try reducer.writeJson(out, benchmark_process_reduction); try out.objectField("executions"); try out.beginArray(); for (executions) |execution| { try out.beginObject(); try out.objectField("index"); try out.write(execution.index); if (execution.acquisition_position) |position| { try out.objectField("acquisition_position"); try out.write(position); } try writeSpawnStateJson(out, execution.result); try out.objectField("pid"); try out.write(execution.result.pid); try out.objectField("exit_code"); try out.write(execution.result.exit_code); try out.objectField("wall_ns"); try out.write(execution.result.wall_ns); try writeResourceUsageFields(out, execution.result); try out.objectField("artifacts"); try writeExecutionArtifactsJson(out, execution); try out.endObject(); } try out.endArray(); try out.endObject();}fn writeExecutionArtifactsJson( out: *pretty_json.Writer, execution: MeasuredExecution,) !void { const paths = execution.paths orelse { try out.write(null); return; }; try out.beginObject(); inline for (.{ .{ "root", paths.root }, .{ "stdout", paths.stdout }, .{ "stderr", paths.stderr }, .{ "bench_jsonl", paths.bench_jsonl }, .{ "coz_jsonl", paths.coz_jsonl }, .{ "coz_analysis", paths.coz_analysis }, .{ "tracy_jsonl", paths.tracy_jsonl }, .{ "tracy_summary", paths.tracy_summary }, .{ "allocations", paths.allocations }, .{ "structured", paths.structured }, }) |field| { try out.objectField(field[0]); try out.write(field[1]); } try out.objectField("structured_rows"); try out.write(execution.structured_rows); try out.objectField("structured_parse_errors"); try out.write(execution.structured_parse_errors); try out.endObject();}const MeasuredArtifactMode = enum { all_measured_executions, profiler_capture_contract, no_measured_execution, fn name(self: MeasuredArtifactMode) []const u8 { return @tagName(self); }};fn measuredArtifactMode(executions: []const MeasuredExecution) MeasuredArtifactMode { if (executions.len == 0) return .no_measured_execution; var retained: usize = 0; for (executions) |execution| { if (execution.paths != null) retained += 1; } if (retained == executions.len) return .all_measured_executions; std.debug.assert(retained == 0); return .profiler_capture_contract;}fn measuredArtifactLayout( executions: []const MeasuredExecution, mode: MeasuredArtifactMode, workload_root: []const u8,) []const u8 { if (mode != .all_measured_executions) return "none"; const first = executions[0].paths.?; if (std.mem.eql(u8, first.root, workload_root)) return "workload_root"; for (executions) |execution| { const paths = execution.paths.?; std.debug.assert(!std.mem.eql(u8, paths.root, workload_root)); } return "numbered_execution_directories";}fn writeResetsJson(out: *pretty_json.Writer, resets: []const Reset) !void { std.debug.assert(resets.len <= maximum_reset_receipts); try out.beginObject(); try out.objectField("policy"); try out.write("before_each_warmup_and_measured_child"); try out.objectField("timing"); try out.write("outside_measured_child_wait"); try out.objectField("receipt_count"); try out.write(resets.len); try out.objectField("receipts"); try out.beginArray(); for (resets) |reset| { std.debug.assert(reset.sequence > 0); std.debug.assert(reset.phase_index > 0); try out.beginObject(); try out.objectField("sequence"); try out.write(reset.sequence); try out.objectField("phase"); try out.write(@tagName(reset.phase)); try out.objectField("phase_index"); try out.write(reset.phase_index); try writeRecordedCommandFields(out, reset.command); try out.endObject(); } try out.endArray(); try out.endObject();}fn writeRecordedCommandFields( out: *pretty_json.Writer, command: execute.CommandResult,) !void { try out.objectField("command"); try out.beginObject(); try out.objectField("text"); try out.write(command.command); try out.objectField("cwd"); try out.write(command.cwd); try out.objectField("argv"); try out.write(command.argv); try out.endObject(); try out.objectField("stdout"); try out.write(command.stdout_path); try out.objectField("stderr"); try out.write(command.stderr_path); try out.objectField("execution"); try writeExecutionResultJson(out, command.execution);}fn writeWarmupJson( out: *pretty_json.Writer, executions: []const execute.Result, paths: ?WarmupPaths,) !void { std.debug.assert(executions.len <= host.process.max_executions); try out.beginObject(); try out.objectField("purpose"); try out.write("unmeasured_process_preconditioning"); try out.objectField("execution_count"); try out.write(executions.len); try out.objectField("acquisition_order"); try out.write("fixed_warmup_index_order"); try out.objectField("workload_output_retention"); try out.write("last_warmup_execution"); try out.objectField("executions"); try writeExecutionResultsJson(out, executions); try out.objectField("artifacts"); if (paths) |actual| { try out.beginObject(); try out.objectField("root"); try out.write(actual.root); try out.objectField("stdout"); try out.write(actual.stdout); try out.objectField("stderr"); try out.write(actual.stderr); try out.objectField("bench_jsonl"); try out.write(actual.bench_jsonl); try out.objectField("coz_jsonl"); try out.write(actual.coz_jsonl); try out.objectField("coz_analysis"); try out.write(actual.coz_analysis); try out.objectField("tracy_jsonl"); try out.write(actual.tracy_jsonl); try out.objectField("tracy_summary"); try out.write(actual.tracy_summary); try out.objectField("allocations"); try out.write(actual.allocations); try out.endObject(); } else { try out.write(null); } try out.endObject();}fn writeExecutionResultsJson( out: *pretty_json.Writer, executions: []const execute.Result,) !void { try out.beginArray(); for (executions, 0..) |execution, index| { try out.beginObject(); try out.objectField("index"); try out.write(index + 1); try out.objectField("pid"); try out.write(execution.pid); try out.objectField("exit_code"); try out.write(execution.exit_code); try out.objectField("wall_ns"); try out.write(execution.wall_ns); try writeResourceUsageFields(out, execution); try out.endObject(); } try out.endArray();}fn writeCatalogWorkloadJson(out: *pretty_json.Writer, workload: 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.objectField("local_step"); try out.write(workload.localStep()); try out.objectField("cwd"); try out.write(workload.cwd); try out.objectField("summary"); try out.write(workload.summary); try out.objectField("tier"); try out.write(@tagName(workload.tier)); try out.objectField("surface"); try out.write(@tagName(workload.surface)); try out.objectField("forwards_args"); try out.write(workload.forwards_args); try out.objectField("bin"); try writeCatalogBinJson(out, workload.bin); try out.objectField("prepare"); try writeCatalogCommandJson(out, workload.prepare); try out.objectField("reset"); try writeCatalogCommandJson(out, workload.reset); try out.objectField("environment"); try writeEnvironmentOverridesJson(out, workload.environment); try out.objectField("allocation_tracking"); try out.beginObject(); try out.objectField("counters"); try out.write(@tagName(workload.allocation_tracking.counters)); try out.objectField("trace"); try out.write(@tagName(workload.allocation_tracking.trace)); try out.objectField("baseline"); try out.write(@tagName(workload.allocationBaselinePolicy())); try out.objectField("regression_budget_percent"); try out.write(workload.allocationBudgetPercent()); try out.endObject(); try out.objectField("trace_allocations"); try out.write(workload.tracesAllocationsByDefault()); try out.objectField("thresholds"); try out.beginObject(); try out.objectField("wall_percent"); try out.write(workload.wall_threshold_percent); try out.objectField("rss_percent"); try out.write(workload.rss_threshold_percent); try out.objectField("metric_percent"); try out.write(workload.metric_threshold_percent); try out.endObject(); try out.endObject();}fn writeRuntimeJson(out: *pretty_json.Writer, runtime: catalog.Runtime) !void { try out.beginObject(); try out.objectField("workload_root"); try out.write(runtime.workload_root); try out.objectField("bin_args"); try out.write(runtime.bin_args); try out.objectField("prepare"); try writeCatalogCommandJson(out, runtime.prepare); try out.objectField("reset"); try writeCatalogCommandJson(out, runtime.reset); try out.objectField("environment"); try writeEnvironmentOverridesJson(out, runtime.environment); try out.endObject();}fn writeCatalogBinJson(out: *pretty_json.Writer, optional: ?catalog.Bin) !void { const bin = optional orelse { try out.write(null); return; }; try out.beginObject(); try out.objectField("step"); try out.write(bin.step); try out.objectField("path"); try out.write(bin.path); try out.objectField("args"); try out.write(bin.args); try out.objectField("cwd"); try out.write(bin.cwd); try out.endObject();}fn writeEnvironmentOverridesJson( out: *pretty_json.Writer, overrides: []const catalog.EnvironmentOverride,) !void { try out.beginArray(); for (overrides) |entry| { try out.beginObject(); try out.objectField("name"); try out.write(entry.name); try out.objectField("operation"); try out.write(entry.operation()); if (entry.value) |value| { try out.objectField("value"); try out.write(value); } try out.endObject(); } try out.endArray();}fn writeCatalogCommandJson( out: *pretty_json.Writer, command: ?catalog.Command,) !void { const actual = command orelse { try out.write(null); return; }; try out.beginObject(); try out.objectField("argv"); try out.write(actual.argv); try out.objectField("cwd"); try out.write(actual.cwd); try out.endObject();}fn writeCommandJson(out: *pretty_json.Writer, value: Workload) !void { try out.beginObject(); try out.objectField("text"); try out.write(value.command); try out.objectField("cwd"); try out.write(value.cwd orelse value.workload.cwd); try out.objectField("argv"); try out.beginArray(); for (value.argv) |arg| try out.write(arg); try out.endArray(); try out.endObject();}fn gitCapture(allocator: std.mem.Allocator, process_io: std.Io, argv: []const []const u8) !?[]const u8 { const result = sys.process.run(allocator, process_io, .{ .argv = argv, .stdout_limit = .limited(4096), .stderr_limit = .limited(4096), }) catch return null; defer allocator.free(result.stderr); if (sys.process.exitCode(result.term) != 0) { allocator.free(result.stdout); return null; } const trimmed = std.mem.trim(u8, result.stdout, " \t\r\n"); const copy = try allocator.dupe(u8, trimmed); allocator.free(result.stdout); return copy;}fn gitDirty(allocator: std.mem.Allocator, process_io: std.Io) !?bool { const result = sys.process.run(allocator, process_io, .{ .argv = &.{ "git", "status", "--porcelain" }, .stdout_limit = .limited(1024 * 1024), .stderr_limit = .limited(4096), }) catch return null; defer allocator.free(result.stdout); defer allocator.free(result.stderr); if (sys.process.exitCode(result.term) != 0) return null; return result.stdout.len != 0;}pub fn sanitizeSegment(allocator: std.mem.Allocator, value: []const u8) ![]const u8 { const result = try allocator.alloc(u8, value.len); for (value, 0..) |byte, index| { result[index] = if (std.ascii.isAlphanumeric(byte) or byte == '.' or byte == '_' or byte == '-') byte else '_'; } return result;}test "profiling record builds stable artifact paths" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const allocator = arena_state.allocator(); const paths = try makePaths(allocator, "zig-out/profiling", "run-test"); try std.testing.expectEqualStrings("zig-out/profiling/run-test", paths.root); try std.testing.expectEqualStrings("zig-out/profiling/run-test/results.jsonl", paths.results); const workload_paths = try workloadPaths(allocator, paths, "w"); try std.testing.expectEqualStrings( "zig-out/profiling/run-test/workloads/w/warmup/stdout.txt", workload_paths.warmup.stdout, ); try std.testing.expectEqualStrings( "zig-out/profiling/run-test/workloads/w/prepare.stdout.txt", workload_paths.prepare_stdout, ); const single = try executionPaths(allocator, workload_paths, 1, 1); try std.testing.expectEqualStrings(workload_paths.stdout, single.stdout); try std.testing.expectEqualStrings(workload_paths.structured, single.structured); const repeated = try executionPaths(allocator, workload_paths, 100, 100); try std.testing.expectEqualStrings( "zig-out/profiling/run-test/workloads/w/executions/100/stdout.txt", repeated.stdout, ); try std.testing.expectEqualStrings( "zig-out/profiling/run-test/workloads/w/executions/100/structured.jsonl", repeated.structured, ); try std.testing.expectError( error.InvalidExecutionArtifactCount, executionPaths(allocator, workload_paths, 0, 1), ); try std.testing.expectError( error.InvalidExecutionArtifactCount, executionPaths(allocator, workload_paths, 101, 1), ); try std.testing.expectError( error.InvalidExecutionArtifactIndex, executionPaths(allocator, workload_paths, 2, 0), ); try std.testing.expectError( error.InvalidExecutionArtifactIndex, executionPaths(allocator, workload_paths, 100, 101), ); const reset = try resetPaths(allocator, workload_paths.root, 2, .capture, 1); try std.testing.expectEqualStrings( "zig-out/profiling/run-test/workloads/w/resets/002-capture-001.stdout.txt", reset.stdout, );}test "profiling record bounds reset receipts for maximum warmup and acquisition counts" { try std.testing.expectEqual(@as(usize, 200), maximum_reset_receipts); try std.testing.expectEqual( host.process.max_executions * 2, maximum_reset_receipts, );}test "profiling record bounds measured execution artifacts" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const allocator = arena_state.allocator(); const paths = try workloadPaths( allocator, try makePaths(allocator, "zig-out/profiling", "run-test"), "w", ); var roots: [host.process.max_executions][]const u8 = undefined; for (&roots, 0..) |*root, index| { const first = try executionPaths(allocator, paths, roots.len, index + 1); const second = try executionPaths(allocator, paths, roots.len, index + 1); try std.testing.expectEqualStrings(first.root, second.root); for (roots[0..index]) |previous| { try std.testing.expect(!std.mem.eql(u8, previous, first.root)); } root.* = first.root; } try std.testing.expect(std.mem.endsWith(u8, roots[0], "/executions/001")); try std.testing.expect(std.mem.endsWith(u8, roots[99], "/executions/100"));}test "profiling record sanitizes workload path segments" { const allocator = std.testing.allocator; const segment = try sanitizeSegment(allocator, "a/b c"); defer allocator.free(segment); try std.testing.expectEqualStrings("a_b_c", segment);}test "profiling record writes the selected optimization mode" { const allocator = std.testing.allocator; const metadata = Metadata{ .run_id = "run-a", .artifact_root = "zig-out/profiling/run-a", .suite = .smoke, .filters = &.{}, .workload_count = 1, .started_unix_ns = 1, .git_sha = null, .git_branch = null, .git_dirty = null, .zig_version = "0.16.0", .host = .{ .hostname = "bench-a", .os = "linux", .kernel = "7.0.11", .arch = "x86_64", .cpu_model = "AMD Ryzen 7 7800X3D", .cpu_count = 8, .cpu_frequency_policy = "policy-a", .process_cpu_affinity = "0-7", .process_memory_affinity = "0", }, .host_state_start = .{ .realtime_unix_ns = 2, .monotonic_ns = 3, .observer_cpu_id = 4, .observer_cpu_frequency_khz = 4_800_000, }, .optimize = .fast, }; const text = try renderJsonDocument(allocator, writeRunStartJson, .{metadata}); defer allocator.free(text); try std.testing.expect( std.mem.indexOf(u8, text, "\"build\":{\"optimize\":\"ReleaseFast\"}") != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"hostname\":\"bench-a\"") != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"cpu_frequency_policy\":\"policy-a\"") != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"process_cpu_affinity\":\"0-7\"") != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"process_memory_affinity\":\"0\"") != null, ); try std.testing.expect( std.mem.indexOf( u8, text, "\"schema\":\"tiny.profiling.host-state/v1\"", ) != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"observer_cpu_id\":4") != null, );}test "profiling record retains external profiler versions" { const allocator = std.testing.allocator; const captures = [_]host.Capture{.{ .kind = host.counters.kind, .tool = host.counters.tool, .tool_version = "perf version 7.0", .capture_path = "perf-stat.csv", .summary_path = "perf-stat.summary.json", .state = "summary_written", }}; const text = try renderJsonDocument(allocator, writeCapturesJson, .{&captures}); defer allocator.free(text); try std.testing.expect( std.mem.indexOf(u8, text, "\"tool_version\":\"perf version 7.0\"") != null, );}fn mappedExecutionFixture( index: usize, position: usize, paths: ExecutionPaths,) MeasuredExecution { const second = index == 2; return .{ .index = index, .acquisition_position = position, .paths = paths, .result = .{ .pid = 100 + index, .exit_code = 0, .wall_ns = if (second) 30 else 10, .resource_usage_source = .wait4_rusage, .maxrss_kib = if (second) 40 else 20, .user_s = if (second) 0.3 else 0.1, .system_s = if (second) 0.4 else 0.2, .minor_page_faults = if (second) 13 else 11, .major_page_faults = if (second) 2 else 1, .voluntary_context_switches = if (second) 5 else 3, .involuntary_context_switches = if (second) 4 else 2, }, .structured_rows = if (second) 7 else 0, .structured_parse_errors = if (second) 1 else 0, };}fn expectMappedMeasurementPolicy(text: []const u8) !void { try std.testing.expect(std.mem.indexOf(u8, text, "\"execution_count\":2") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"pid\":101") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"pid\":102") != null); try std.testing.expect( std.mem.indexOf(u8, text, "\"acquisition_order\":\"random_interleaved\"") != null, ); try std.testing.expect( std.mem.indexOf( u8, text, "\"workload_output_retention\":\"all_measured_executions\"", ) != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"allocation_trace\":true") != null, ); try std.testing.expect( std.mem.indexOf( u8, text, "\"allocation_summary_retention\":\"all_measured_executions\"", ) != null, ); try std.testing.expect( std.mem.indexOf(u8, text, "\"resource_usage_source\":\"require_equal\"") != null, ); try std.testing.expect(std.mem.indexOf(u8, text, "\"minor_page_faults\":\"sum\"") != null); try std.testing.expect( std.mem.indexOf(u8, text, "\"resource_usage_source\":\"wait4_rusage\"") != null, ); try std.testing.expect(std.mem.indexOf(u8, text, "\"voluntary_context_switches\":3") != null); try std.testing.expect(std.mem.indexOf( u8, text, "\"root\":\"zig-out/profiling/run-test/workloads/w/executions/001\"", ) != null); try std.testing.expect(std.mem.indexOf( u8, text, "\"structured_rows\":7,\"structured_parse_errors\":1", ) != null);}fn expectMappedMeasurementRows( allocator: std.mem.Allocator, text: []const u8,) !void { const document = try std.json.parseFromSliceLeaky( std.json.Value, allocator, text, .{}, ); const measurement_object = try json_util.object(document); const rows = try json_util.array(measurement_object.get("executions").?); try std.testing.expectEqual(@as(usize, 2), rows.items.len); const first = try json_util.object(rows.items[0]); const second = try json_util.object(rows.items[1]); try std.testing.expectEqual(@as(u64, 101), json_util.asU64(first.get("pid")).?); try std.testing.expectEqual( @as(u64, 1), json_util.asU64(first.get("acquisition_position")).?, ); try std.testing.expectEqual(@as(u64, 102), json_util.asU64(second.get("pid")).?); try std.testing.expectEqual( @as(u64, 4), json_util.asU64(second.get("acquisition_position")).?, ); const first_artifacts = try json_util.object(first.get("artifacts").?); const second_artifacts = try json_util.object(second.get("artifacts").?); try std.testing.expect(std.mem.endsWith( u8, json_util.string(first_artifacts.get("root")).?, "/001", )); try std.testing.expect(std.mem.endsWith( u8, json_util.string(second_artifacts.get("root")).?, "/002", )); try std.testing.expectEqual( @as(u64, 7), json_util.asU64(second_artifacts.get("structured_rows")).?, );}test "profiling record maps measured execution artifacts to each process" { var arena_state = std.heap.ArenaAllocator.init(std.testing.allocator); defer arena_state.deinit(); const allocator = arena_state.allocator(); const paths = try workloadPaths( allocator, try makePaths(allocator, "zig-out/profiling", "run-test"), "w", ); const positions = [_]usize{ 1, 4 }; const acquisition = order.Context{ .random_interleaved = .{ .seed = 42, .repeat_count = 2, .workload_count = 2, .selected_workloads = &.{ "w", "x" }, .positions = &positions, } }; const executions = [_]MeasuredExecution{ mappedExecutionFixture(1, positions[0], try executionPaths(allocator, paths, 2, 1)), mappedExecutionFixture(2, positions[1], try executionPaths(allocator, paths, 2, 2)), }; const text = try renderJsonDocument( allocator, writeMeasurementJson, .{ executions[0..], acquisition, true, paths.root, reducer.DomainDeclaration{ .benchmark_surface = true, .direct_execution = true, .host_profiler = false, .tracy = false, .causal = false, .allocation_trace = true, .capture_control = false, }, reducer.Result{ .not_applicable = .{ .reason = .outside_direct_benchmark_domain, .process_count = 2, } }, }, ); defer allocator.free(text); try expectMappedMeasurementPolicy(text); try expectMappedMeasurementRows(allocator, text);}test "profiling record writes v4 waited-command resource evidence" { const allocator = std.testing.allocator; const text = try renderJsonDocument( allocator, writeExecutionResultJson, .{execute.Result{ .pid = 404, .exit_code = 0, .wall_ns = 50, .resource_usage_source = .wait4_rusage, .maxrss_kib = 40, .user_s = 0.1, .system_s = 0.2, .minor_page_faults = 96, .major_page_faults = 1, .voluntary_context_switches = 2, .involuntary_context_switches = 1, }}, ); defer allocator.free(text); try std.testing.expectEqualStrings("tiny.profiling.workload/v4", workload_schema); try std.testing.expect(std.mem.indexOf(u8, text, "\"pid\":404") != null); try std.testing.expect( std.mem.indexOf(u8, text, "\"resource_usage_source\":\"wait4_rusage\"") != null, ); try std.testing.expect(std.mem.indexOf(u8, text, "\"minor_page_faults\":96") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"major_page_faults\":1") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"voluntary_context_switches\":2") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"involuntary_context_switches\":1") != null);}test "profiling record retains setup child process identity" { const allocator = std.testing.allocator; const text = try renderJsonDocument( allocator, writeSetupJson, .{Setup{ .command = "zig build bench-bin", .pid = 303, .exit_code = 0, .wall_ns = 12, .stdout_path = "setup.stdout.txt", .stderr_path = "setup.stderr.txt", }}, ); defer allocator.free(text); try std.testing.expect(std.mem.indexOf( u8, text, "\"command\":\"zig build bench-bin\",\"pid\":303", ) != null);}test "profiling record writes exact acquisition context" { const allocator = std.testing.allocator; const text = try renderJsonDocument( allocator, order.writeJson, .{order.Context{ .blocked = .{ .position = 3, .workload_count = 4, .predecessors = &.{ "a", "b" }, } }}, ); defer allocator.free(text); try std.testing.expectEqualStrings( "{\"position\":3,\"workload_count\":4,\"predecessors\":[\"a\",\"b\"]}\n", text, );}test "profiling record writes exact interleaved acquisition context" { const allocator = std.testing.allocator; const text = try renderJsonDocument( allocator, order.writeJson, .{order.Context{ .random_interleaved = .{ .seed = 42, .repeat_count = 3, .workload_count = 2, .selected_workloads = &.{ "a", "b" }, .positions = &.{ 1, 3, 6 }, } }}, ); defer allocator.free(text); try std.testing.expectEqualStrings( "{\"method\":\"random_interleaved\",\"schedule_algorithm\":\"xoshiro256_flat_multiset_fisher_yates\",\"setup_order\":\"selected_workload_order_before_measurement\",\"warmup_placement\":\"before_first_scheduled_measurement\",\"failure_policy\":\"skip_failed_workload_remaining_positions\",\"seed\":42,\"repeat_count\":3,\"workload_count\":2,\"selected_workloads\":[\"a\",\"b\"],\"positions\":[1,3,6]}\n", text, );}test "profiling record retains unmeasured warmup executions separately" { const allocator = std.testing.allocator; const executions = [_]execute.Result{ .{ .exit_code = 0, .wall_ns = 5 }, .{ .exit_code = 0, .wall_ns = 7 }, }; const paths = WarmupPaths{ .root = "workloads/w/warmup", .stdout = "workloads/w/warmup/stdout.txt", .stderr = "workloads/w/warmup/stderr.txt", .bench_jsonl = "workloads/w/warmup/bench.jsonl", .coz_jsonl = "workloads/w/warmup/bench.coz.jsonl", .coz_analysis = "workloads/w/warmup/bench.coz.analysis.json", .tracy_jsonl = "workloads/w/warmup/bench.tracy.jsonl", .tracy_summary = "workloads/w/warmup/bench.tracy.summary.jsonl", .allocations = "workloads/w/warmup/allocations.jsonl", }; const text = try renderJsonDocument( allocator, writeWarmupJson, .{ executions[0..], @as(?WarmupPaths, paths) }, ); defer allocator.free(text); try std.testing.expect(std.mem.indexOf(u8, text, "\"purpose\":\"unmeasured_process_preconditioning\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"execution_count\":2") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"workload_output_retention\":\"last_warmup_execution\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"wall_ns\":5") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"stdout\":\"workloads/w/warmup/stdout.txt\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "aggregate_reduction") == null);}test "profiling record separates runtime preparation from catalog templates" { const allocator = std.testing.allocator; const workload = catalog.Workload{ .name = "fixture", .package = "lib/fixture", .step = "fixture-bench", .summary = "fixture", .tier = .smoke, .surface = .benchmark, .bin = .{ .path = "zig-out/bin/fixture", .args = &.{ "consume", "{workload_root}/live" }, }, .prepare = .{ .argv = &.{ "zig-out/bin/fixture-helper", "prepare", "{workload_root}" }, }, .reset = .{ .argv = &.{ "zig-out/bin/fixture-helper", "reset", "{workload_root}" }, }, .environment = &.{ .{ .name = "HOME", .value = "{workload_root}/home" }, .{ .name = "FIXTURE_OBSERVATION_PATH" }, }, }; const catalog_text = try renderJsonDocument( allocator, writeCatalogWorkloadJson, .{workload}, ); defer allocator.free(catalog_text); try std.testing.expect(std.mem.indexOf( u8, catalog_text, "\"args\":[\"consume\",\"{workload_root}/live\"]", ) != null); try std.testing.expect(std.mem.indexOf( u8, catalog_text, "\"prepare\":{\"argv\":[\"zig-out/bin/fixture-helper\",\"prepare\",\"{workload_root}\"]", ) != null); try std.testing.expect(std.mem.indexOf( u8, catalog_text, "\"reset\":{\"argv\":[\"zig-out/bin/fixture-helper\",\"reset\",\"{workload_root}\"]", ) != null); try std.testing.expect(std.mem.indexOf( u8, catalog_text, "\"name\":\"HOME\",\"operation\":\"set\",\"value\":\"{workload_root}/home\"", ) != null); try std.testing.expect(std.mem.indexOf( u8, catalog_text, "\"name\":\"FIXTURE_OBSERVATION_PATH\",\"operation\":\"unset\"", ) != null); const runtime = catalog.Runtime{ .workload_root = "/tmp/profile/run/workloads/fixture", .bin_args = &.{ "consume", "/tmp/profile/run/workloads/fixture/live" }, .prepare = .{ .argv = &.{ "zig-out/bin/fixture-helper", "prepare", "/tmp/profile/run/workloads/fixture", }, .cwd = "lib/fixture", }, .reset = .{ .argv = &.{ "zig-out/bin/fixture-helper", "reset", "/tmp/profile/run/workloads/fixture", }, .cwd = "lib/fixture", }, .environment = &.{ .{ .name = "HOME", .value = "/tmp/profile/run/workloads/fixture/home" }, .{ .name = "FIXTURE_OBSERVATION_PATH" }, }, }; const runtime_text = try renderJsonDocument( allocator, writeRuntimeJson, .{runtime}, ); defer allocator.free(runtime_text); try std.testing.expect(std.mem.indexOf( u8, runtime_text, "\"workload_root\":\"/tmp/profile/run/workloads/fixture\"", ) != null); try std.testing.expect(std.mem.indexOf( u8, runtime_text, "\"value\":\"/tmp/profile/run/workloads/fixture/home\"", ) != null); const prepare = execute.CommandResult{ .command = "zig-out/bin/fixture-helper prepare /tmp/profile/run/workloads/fixture", .argv = runtime.prepare.?.argv, .cwd = runtime.prepare.?.cwd, .stdout_path = "prepare.stdout.txt", .stderr_path = "prepare.stderr.txt", .execution = .{ .pid = 404, .exit_code = 0, .wall_ns = 11 }, }; const prepare_text = try renderJsonDocument( allocator, writePrepareJson, .{@as(?execute.CommandResult, prepare)}, ); defer allocator.free(prepare_text); try std.testing.expect(std.mem.indexOf(u8, prepare_text, "\"pid\":404") != null); try std.testing.expect(std.mem.indexOf( u8, prepare_text, "\"stdout\":\"prepare.stdout.txt\"", ) != null); try std.testing.expect(std.mem.indexOf(u8, prepare_text, "\"wall_ns\":11") != null); const resets = [_]Reset{.{ .sequence = 1, .phase = .measurement, .phase_index = 1, .command = .{ .command = "/bin/true", .argv = &.{"/bin/true"}, .cwd = "/tmp/fixture", .stdout_path = "resets/001.stdout.txt", .stderr_path = "resets/001.stderr.txt", .execution = .{ .pid = 505, .exit_code = 0, .wall_ns = 12 }, }, }}; const reset_text = try renderJsonDocument( allocator, writeResetsJson, .{resets[0..]}, ); defer allocator.free(reset_text); try std.testing.expect(std.mem.indexOf(u8, reset_text, "\"receipt_count\":1") != null); try std.testing.expect(std.mem.indexOf(u8, reset_text, "\"pid\":505") != null); try std.testing.expect(std.mem.indexOf(u8, reset_text, "\"stdout\":\"resets/001.stdout.txt\"") != null); try std.testing.expect(std.mem.indexOf(u8, reset_text, "\"wall_ns\":12") != null);}test "profiling record retains capture controls and matched execution order" { const allocator = std.testing.allocator; const pairs = [_]perturbation.Pair{.{ .index = 1, .order = .capture_first, .control = .{ .pid = 201, .exit_code = 0, .wall_ns = 10 }, .capture = .{ .pid = 202, .exit_code = 0, .wall_ns = 12 }, }}; const text = try renderJsonDocument( allocator, writeCapturePerturbationJson, .{@as(?perturbation.Measurement, .{ .state = .complete, .base_seed = 7, .workload_seed = 8, .pairs = &pairs, .configuration = .{ .tracy = true }, .control_artifacts = .{ .stdout = "control.stdout.txt", .stderr = "control.stderr.txt", .bench_jsonl = "control.bench.jsonl", .coz_jsonl = "control.coz.jsonl", .coz_analysis = "control.coz.analysis.json", .tracy_jsonl = "control.tracy.jsonl", .tracy_summary = "control.tracy.summary.jsonl", .allocations = "control.allocations.jsonl", .structured = "control.structured.jsonl", }, .control_structured_rows = 2, })}, ); defer allocator.free(text); try std.testing.expect(std.mem.indexOf(u8, text, "\"method\":\"same_build_matched_control_capture\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"binary_identity\":\"same_built_binary\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"control_output_retention\":\"last_control_execution\"") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"active_configuration\":{\"host_kind\":null,\"tracy\":true,\"allocations\":false}") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"structured_rows\":2") != null); try std.testing.expect(std.mem.indexOf(u8, text, "\"index\":1,\"order\":\"capture_first\"") != null); try std.testing.expect(std.mem.indexOf( u8, text, "\"control\":{\"spawn_state\":\"spawned\",\"pid\":201," ++ "\"exit_code\":0,\"wall_ns\":10", ) != null); try std.testing.expect(std.mem.indexOf( u8, text, "\"capture\":{\"spawn_state\":\"spawned\",\"pid\":202," ++ "\"exit_code\":0,\"wall_ns\":12", ) != null);}Source: src/profiling/root.zig:39
zig
pub const record = @import("record.zig");Complete caller list for record.executionPaths
11 direct callers.
src.profiling.analyze.fixture.data.writeReductionTestRun[function] — private; no exact target atsrc/profiling/analyze/fixture/data.zig:258in nearest public ownersrc.profiling.analyze.fixture.datasrc.profiling.command.planRunDryRunWorkload[function] — private; no exact target atsrc/profiling/command.zig:754in nearest public ownertiny.profiling.commandsrc.profiling.command.writeRunPlanExecutionArtifacts[function] — private; no exact target atsrc/profiling/command.zig:1075in nearest public ownertiny.profiling.commandsrc.profiling.driver.artifact.test_profiling_driver_validates_each_retained_Tracy_execution[function] — test; no exact target atsrc/profiling/driver/artifact.zig:358in nearest public ownertiny.profiling.driver.artifacttiny.profiling.driver.measure.runMeasuredWorkload[function] atsrc/profiling/driver/measure.zig:17src.profiling.driver.schedule.runInterleavedEntry[function] — private; no exact target atsrc/profiling/driver/schedule.zig:140in nearest public ownertiny.profiling.driver.schedulesrc.profiling.driver.test.test_profiling_driver_excludes_a_later_reset_failure_from_fixed_measurements[function] — test; no exact target atsrc/profiling/driver/test.zig:466in nearest public ownersrc.profiling.driver.testsrc.profiling.driver.test.test_profiling_driver_retains_measured_execution_artifacts_after_partial_failure[function] — test; no exact target atsrc/profiling/driver/test.zig:56in nearest public ownersrc.profiling.driver.testsrc.profiling.record.test_profiling_record_bounds_measured_execution_artifacts[function] — test; no exact target atsrc/profiling/record.zig:1432in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_builds_stable_artifact_paths[function] — test; no exact target atsrc/profiling/record.zig:1371in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_maps_measured_execution_artifacts_to_each_process[function] — test; no exact target atsrc/profiling/record.zig:1655in nearest public ownertiny.profiling.record
Complete caller list for record.makePaths
10 direct callers.
src.profiling.analyze.fixture.data.writeReductionTestRun[function] — private; no exact target atsrc/profiling/analyze/fixture/data.zig:258in nearest public ownersrc.profiling.analyze.fixture.datasrc.profiling.command.test_profiling_CLI_falls_back_from_unavailable_counter_lane_artifact_counts[function] — test; no exact target atsrc/profiling/command.zig:1292in nearest public ownertiny.profiling.commandsrc.profiling.command.test_profiling_CLI_plans_capture-control_artifact_contract[function] — test; no exact target atsrc/profiling/command.zig:1321in nearest public ownertiny.profiling.commandsrc.profiling.command.test_profiling_CLI_writes_measured_execution_artifacts_dry_run[function] — test; no exact target atsrc/profiling/command.zig:1223in nearest public ownertiny.profiling.commandsrc.profiling.command.writeRunDryRunText[function] — private; no exact target atsrc/profiling/command.zig:571in nearest public ownertiny.profiling.commandsrc.profiling.command.writeRunPlanJson[function] — private; no exact target atsrc/profiling/command.zig:961in nearest public ownertiny.profiling.commandtiny.profiling.driver.run.executeRun[function] atsrc/profiling/driver/run.zig:31src.profiling.record.test_profiling_record_bounds_measured_execution_artifacts[function] — test; no exact target atsrc/profiling/record.zig:1432in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_builds_stable_artifact_paths[function] — test; no exact target atsrc/profiling/record.zig:1371in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_maps_measured_execution_artifacts_to_each_process[function] — test; no exact target atsrc/profiling/record.zig:1655in nearest public ownertiny.profiling.record
Complete caller list for record.workloadPaths
8 direct callers.
src.profiling.command.planRunDryRunWorkload[function] — private; no exact target atsrc/profiling/command.zig:754in nearest public ownertiny.profiling.commandsrc.profiling.command.test_profiling_CLI_falls_back_from_unavailable_counter_lane_artifact_counts[function] — test; no exact target atsrc/profiling/command.zig:1292in nearest public ownertiny.profiling.commandsrc.profiling.command.writeRunPlanWorkloadsJson[function] — private; no exact target atsrc/profiling/command.zig:1022in nearest public ownertiny.profiling.commandtiny.profiling.record.makeWorkloadPaths[function] atsrc/profiling/record.zig:284src.profiling.record.test_profiling_record_bounds_measured_execution_artifacts[function] — test; no exact target atsrc/profiling/record.zig:1432in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_builds_stable_artifact_paths[function] — test; no exact target atsrc/profiling/record.zig:1371in nearest public ownertiny.profiling.recordsrc.profiling.record.test_profiling_record_maps_measured_execution_artifacts_to_each_process[function] — test; no exact target atsrc/profiling/record.zig:1655in nearest public ownertiny.profiling.recordtiny.profiling.report.coverage.writeRunArtifacts[function] atsrc/profiling/report/coverage.zig:72
Audit
| Definitions | 30 |
|---|---|
| Public names | 30 |
| Members | 129 |
| Version | 26.7.0 |
| Revision | daab053ee433 |