updates to pscopes

This commit is contained in:
peterino2 2025-11-01 16:21:14 -07:00
parent 054b41da95
commit 2e2ab68178
2 changed files with 29 additions and 23 deletions

2
lib/p2/src/p2.zig vendored
View File

@ -9,6 +9,8 @@ pub const splitSliceWork = utils.splitSliceWork;
pub const BumpArena = @import("structures/bump-arena.zig").BumpArena; pub const BumpArena = @import("structures/bump-arena.zig").BumpArena;
pub const pscopes = @import("structures/pscopes.zig");
pub const dupeString = utils.dupeString; pub const dupeString = utils.dupeString;
pub const xxd = @import("utils/xxd.zig"); pub const xxd = @import("utils/xxd.zig");

View File

@ -1,35 +1,21 @@
// pscopes // pscopes
// //
// a tiny scope based profiling library // a tiny scope based profiling library
//
// as well as contains info for arbitrary timestamping and timestamp resolution // as well as contains info for arbitrary timestamping and timestamp resolution
const timestampTicks = if (builtin.target.cpu.arch == .x86_64) rdtsc else aarch64_ticks; 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 // rdtsc actually gets read
pub inline fn rdtsc() u64 { pub inline fn rdtsc() u64 {
var hi: u32 = 0; var hi: u32 = 0;
var low: u32 = 0; var lo: u32 = 0;
asm ( asm (
\\rdtsc \\rdtsc
: [low] "={eax}" (low), : [lo] "={eax}" (lo),
[hi] "={edx}" (hi), [hi] "={edx}" (hi),
); );
return (@as(u64, hi) << 32) | @as(u64, low); return (@as(u64, hi) << 32) | @as(u64, lo);
} }
fn aarch64_ticks() u64 { 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 { fn countTo1Billion() u32 {
var i: u32 = 0; var i: u32 = 0;
while (i < 1000 * 1000 * 1000) : (i += 1) {} while (i < 1000 * 1000 * 1000) : (i += 1) {}
@ -85,7 +77,8 @@ pub const ScopesContext = struct {
ticksPerMs: ?f64 = null, ticksPerMs: ?f64 = null,
timer: std.time.Timer, 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 .{ return .{
.startTsc = timestampTicks(), .startTsc = timestampTicks(),
.endTsc = null, .endTsc = null,
@ -131,26 +124,37 @@ test "calibrate scoes" {
const scopes = try ScopesContext.create(allocator); const scopes = try ScopesContext.create(allocator);
defer scopes.destroy(); 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(); scopes.calibrate();
std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?});
scopes.calibrate(); scopes.calibrate();
std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?});
scopes.calibrate(); scopes.calibrate();
std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?}); std.debug.print("calibration ticksPerMs = {d}\n", .{scopes.ticksPerMs.?});
{ {
var scope = scopes.timingScope(); var scope = scopes.stampScope();
_ = countTo1Billion(); _ = countTo1Billion();
scope.end(); scope.end();
std.debug.print("time elapsed: {d}\n ", .{scope.duration().?}); 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 std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const pscopes_zig_src = @embedFile("pscopes.zig");