tiny.profiling.driver.validation
Defined in driver.
API (8)
Actions
Public operations.
buildArgseffectiveBenchControleffectiveOptimizefailureExitCodevalidateInterleavevalidateMeasureRepeatvalidatePreparedChildConfigurationvalidateRequest
Source
Source: src/profiling/driver/root.zig:13
zig
pub const validation = @import("validation.zig");Source: src/profiling/driver/validation.zig
zig
const std = @import("std");const catalog = @import("../root.zig").catalog;const execute = @import("../root.zig").execute;const host = @import("../root.zig").host;const order = @import("../root.zig").order;const plan = @import("../root.zig").plan;const perturbation = @import("../root.zig").perturbation;const Request = @import("root.zig").model.Request;const resetTestWorkload = @import("fixture/root.zig").resetTestWorkload;pub fn validateRequest(request: Request) !void { try request.host_lanes.validate(); try validateMeasureRepeat( request.measure_repeat, request.host_lanes, request.tracy, request.control.causal, ); if (!host.process.validWarmupRepeat(request.warmup_repeat)) { return error.InvalidWarmupRepeat; } try perturbation.validateCompatibility( request.capture_control, request.measure_repeat, request.execution_scope, request.host_lanes, request.tracy, request.trace_allocations, request.control.causal, ); try validateInterleave( request.interleave_seed, request.selection.count(), request.measure_repeat, request.continue_on_failure, ); if (request.control.causal and request.host_lanes.any()) return error.CausalWithHostLane; if (request.tracy and request.execution_scope != .binary) return error.TracyRequiresBinaryScope; if (request.execution_scope == .binary) try validateBinaryScope(request.selection); try validateChildConfigurationScope( request.selection, request.execution_scope, request.host_lanes.requiresDirect(), );}pub fn effectiveBenchControl( control: execute.BenchControl, host_lanes: host.Options,) !execute.BenchControl { var effective = control; if (host_lanes.sampling) |options| { const sampling_min = try options.minimumTimeNs(); effective.min_time_ns = @max(effective.min_time_ns orelse 0, sampling_min); } return effective;}pub fn validateMeasureRepeat( repeat: u32, host_lanes: host.Options, tracy: bool, causal: bool,) !void { if (!host.process.validRepeat(repeat)) { return error.InvalidMeasureRepeat; } if (repeat > 1 and (host_lanes.any() or tracy or causal)) { return error.MeasureRepeatConflictsWithCapture; }}pub fn validateInterleave( seed: ?u64, workload_count: usize, repeat: u32, continue_on_failure: bool,) !void { if (seed == null) return; try order.validateDesign(workload_count, repeat); if (!continue_on_failure) return error.InterleaveRequiresContinueOnFailure;}fn validateBinaryScope(selection: plan.Selection) !void { for (catalog.workloads) |workload| { if (!selection.selected(workload)) continue; if (workload.bin == null) return error.BinaryScopeMissingWorkloadBinary; }}const BinaryScopeRejection = error{ PrepareRequiresBinaryScope, ResetRequiresBinaryScope, EnvironmentRequiresBinaryScope,};fn validateChildConfigurationScope( selection: plan.Selection, scope: execute.Scope, host_requires_direct: bool,) BinaryScopeRejection!void { for (catalog.workloads) |workload| { if (!selection.selected(workload)) continue; try validateWorkloadChildConfigurationScope( workload, scope.directFor(workload) or host_requires_direct, ); }}fn validateWorkloadChildConfigurationScope( workload: catalog.Workload, direct: bool,) BinaryScopeRejection!void { if (direct) return; if (workload.prepare != null) return error.PrepareRequiresBinaryScope; if (workload.reset != null) return error.ResetRequiresBinaryScope; if (workload.environment.len != 0) return error.EnvironmentRequiresBinaryScope;}pub fn validatePreparedChildConfiguration( workload: catalog.Workload, execution_plan: execute.Plan,) !void { const configured = workload.prepare != null or workload.reset != null or workload.environment.len != 0; if (configured and !execution_plan.direct) { return error.ChildConfigurationRequiresDirectPlan; }}pub fn buildArgs( allocator: std.mem.Allocator, host_args: []const []const u8, tracy: bool, optimize: std.builtin.OptimizeMode,) ![]const []const u8 { const tracy_count: usize = if (tracy) 1 else 0; const result = try allocator.alloc([]const u8, host_args.len + tracy_count + 1); result[0] = execute.optimizeBuildArg(optimize); @memcpy(result[1 .. 1 + host_args.len], host_args); if (tracy) result[result.len - 1] = "-Dtracy=true"; return result;}pub fn effectiveOptimize(host_lanes: host.Options) std.builtin.OptimizeMode { return if (host_lanes.coverage != null) .debug else execute.default_optimize;}pub fn failureExitCode(exit_code: i64) u8 { const result: u8 = if (exit_code <= 0) 1 else @intCast(@min(exit_code, 255)); std.debug.assert(result > 0); return result;}test "profiling driver clamps failure exit codes" { try std.testing.expectEqual(@as(u8, 1), failureExitCode(0)); try std.testing.expectEqual(@as(u8, 1), failureExitCode(-9)); try std.testing.expectEqual(@as(u8, 7), failureExitCode(7)); try std.testing.expectEqual(@as(u8, 255), failureExitCode(300));}test "profiling driver derives benchmark time from sampling support" { const derived = try effectiveBenchControl(.{}, .{ .sampling = .{} }); try std.testing.expectEqual(@as(?u64, 4_040_404_041), derived.min_time_ns); const kept = try effectiveBenchControl( .{ .min_time_ns = 30 * std.time.ns_per_s }, .{ .sampling = .{} }, ); try std.testing.expectEqual(@as(?u64, 30 * std.time.ns_per_s), kept.min_time_ns); const plain = try effectiveBenchControl(.{ .causal = true }, .{}); try std.testing.expect(plain.causal); try std.testing.expect(plain.min_time_ns == null);}test "profiling driver binary scope requires catalog binaries" { try validateBinaryScope(.{ .suite = .smoke, .filters = &.{} }); try validateBinaryScope(.{ .suite = .standard, .filters = &.{} }); try std.testing.expectError( error.BinaryScopeMissingWorkloadBinary, validateBinaryScope(.{ .suite = .all, .filters = &.{} }), );}test "profiling driver mixed scope admits step-only and direct-configured workloads" { const all = plan.Selection{ .suite = .all, .filters = &.{} }; try validateChildConfigurationScope(all, .mixed, false); if (validateChildConfigurationScope(all, .step, false)) |_| { return error.TestExpectedBinaryScopeRejection; } else |rejection| switch (rejection) { error.PrepareRequiresBinaryScope, error.ResetRequiresBinaryScope, error.EnvironmentRequiresBinaryScope, => {}, } try validateChildConfigurationScope(all, .step, true);}test "profiling driver requires direct scope for child reset and environment declarations" { var workload = resetTestWorkload(&.{"/bin/true"}, &.{}); try validateWorkloadChildConfigurationScope(workload, true); try std.testing.expectError( error.ResetRequiresBinaryScope, validateWorkloadChildConfigurationScope(workload, false), ); workload.prepare = .{ .argv = &.{"/bin/true"} }; workload.reset = null; try std.testing.expectError( error.PrepareRequiresBinaryScope, validateWorkloadChildConfigurationScope(workload, false), ); workload.prepare = null; workload.environment = &.{.{ .name = "HOME", .value = "/tmp/profile-home" }}; try std.testing.expectError( error.EnvironmentRequiresBinaryScope, validateWorkloadChildConfigurationScope(workload, false), ); workload.bin = null; try validateWorkloadChildConfigurationScope(workload, true); try std.testing.expectError( error.ChildConfigurationRequiresDirectPlan, validatePreparedChildConfiguration(workload, .{ .argv = &.{ "zig", "build", "reset-test" }, .missing_bin = true, }), ); workload.environment = &.{}; workload.prepare = .{ .argv = &.{"/bin/true"} }; try std.testing.expectError( error.ChildConfigurationRequiresDirectPlan, validatePreparedChildConfiguration(workload, .{ .argv = &.{ "zig", "build", "prepare-test" }, .missing_bin = true, }), ); workload.bin = .{ .path = "/bin/true" }; try validatePreparedChildConfiguration(workload, .{ .argv = &.{"/bin/true"}, .direct = true, }); workload.prepare = null; try validateWorkloadChildConfigurationScope(workload, false);}test "profiling driver selects coherent prepared binary builds" { const allocator = std.testing.allocator; const plain = try buildArgs(allocator, &.{}, false, .fast); defer allocator.free(plain); try std.testing.expectEqualSlices([]const u8, &.{"-Doptimize=fast"}, plain); const traced = try buildArgs(allocator, &.{"-Dcpu=x86_64_v3"}, true, .safe); defer allocator.free(traced); try std.testing.expectEqualSlices( []const u8, &.{ "-Doptimize=safe", "-Dcpu=x86_64_v3", "-Dtracy=true" }, traced, ); try std.testing.expectEqual( std.builtin.OptimizeMode.fast, effectiveOptimize(.{}), ); try std.testing.expectEqual( std.builtin.OptimizeMode.debug, effectiveOptimize(.{ .coverage = .{} }), );}test "profiling driver validates ordinary process repetition captures" { try validateMeasureRepeat(1, .{ .sampling = .{} }, true, true); try validateMeasureRepeat(host.process.max_repeat, .{}, false, false); try std.testing.expectError( error.InvalidMeasureRepeat, validateMeasureRepeat(0, .{}, false, false), ); try std.testing.expectError( error.InvalidMeasureRepeat, validateMeasureRepeat(host.process.max_repeat + 1, .{}, false, false), ); try std.testing.expectError( error.MeasureRepeatConflictsWithCapture, validateMeasureRepeat(2, .{ .counters = .{} }, false, false), ); try std.testing.expectError( error.MeasureRepeatConflictsWithCapture, validateMeasureRepeat(2, .{}, true, false), ); try std.testing.expectError( error.MeasureRepeatConflictsWithCapture, validateMeasureRepeat(2, .{}, false, true), );}test "profiling driver validates deterministic repetition interleaving" { try validateInterleave(42, 2, 2, true); try validateInterleave(null, 1, 1, false); try std.testing.expectError( error.InvalidInterleaveWorkloadCount, validateInterleave(42, 1, 2, true), ); try std.testing.expectError( error.InvalidInterleaveRepeat, validateInterleave(42, 2, 1, true), ); try std.testing.expectError( error.InterleaveRequiresContinueOnFailure, validateInterleave(42, 2, 2, false), );}Audit
| Definitions | 9 |
|---|---|
| Public names | 14 |
| Members | 0 |
| Version | 26.7.0 |
| Revision | daab053ee433 |