diff --git a/lib/p2/src/p2.zig b/lib/p2/src/p2.zig index 01ad9c1..a57b924 100644 --- a/lib/p2/src/p2.zig +++ b/lib/p2/src/p2.zig @@ -9,6 +9,8 @@ pub const splitSliceWork = utils.splitSliceWork; pub const BumpArena = @import("structures/bump-arena.zig").BumpArena; +pub const pscopes = @import("structures/pscopes.zig"); + pub const dupeString = utils.dupeString; pub const xxd = @import("utils/xxd.zig"); diff --git a/lib/p2/src/structures/pscopes.zig b/lib/p2/src/structures/pscopes.zig index 1914f96..daa500b 100644 --- a/lib/p2/src/structures/pscopes.zig +++ b/lib/p2/src/structures/pscopes.zig @@ -1,35 +1,21 @@ // pscopes // // a tiny scope based profiling library -// // as well as contains info for arbitrary timestamping and timestamp resolution const timestampTicks = if (builtin.target.cpu.arch == .x86_64) rdtsc else aarch64_ticks; -pub inline fn rdtsc_fenced() u64 { - var hi: u32 = 0; - var low: u32 = 0; - - asm ( - \\rdtsc - \\mfence - : [low] "={eax}" (low), - [hi] "={edx}" (hi), - ); - return (@as(u64, hi) << 32) | @as(u64, low); -} - // rdtsc actually gets read pub inline fn rdtsc() u64 { var hi: u32 = 0; - var low: u32 = 0; + var lo: u32 = 0; asm ( \\rdtsc - : [low] "={eax}" (low), + : [lo] "={eax}" (lo), [hi] "={edx}" (hi), ); - return (@as(u64, hi) << 32) | @as(u64, low); + return (@as(u64, hi) << 32) | @as(u64, lo); } fn aarch64_ticks() u64 { @@ -44,6 +30,12 @@ fn aarch64_freq() u64 { ); } +fn countTo1Million() u32 { + var i: u32 = 0; + while (i < 1000 * 1000) : (i += 1) {} + return i; +} + fn countTo1Billion() u32 { var i: u32 = 0; while (i < 1000 * 1000 * 1000) : (i += 1) {} @@ -85,7 +77,8 @@ pub const ScopesContext = struct { ticksPerMs: ?f64 = null, timer: std.time.Timer, - pub fn timingScope(self: *@This()) TimingScope { + // creates a very simple scope, the only thing it does is store the timestamp, and move on + pub fn stampScope(self: *@This()) TimingScope { return .{ .startTsc = timestampTicks(), .endTsc = null, @@ -131,26 +124,37 @@ test "calibrate scoes" { const scopes = try ScopesContext.create(allocator); defer scopes.destroy(); - scopes.calibrate(); - std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); - scopes.calibrate(); - std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); scopes.calibrate(); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); + scopes.calibrate(); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); + scopes.calibrate(); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); { - var scope = scopes.timingScope(); + var scope = scopes.stampScope(); _ = countTo1Billion(); scope.end(); std.debug.print("time elapsed: {d}\n ", .{scope.duration().?}); } + + { + var z = scopes.stampScope(); + defer z.end(); + _ = countTo1Million(); + } + + { + var z = scopes.stampScope(); + defer z.end(); + _ = countTo1Million(); + } } const std = @import("std"); const builtin = @import("builtin"); +const pscopes_zig_src = @embedFile("pscopes.zig");