Skip to content

Commit

Permalink
port cachegrind.h to zig (#19241)
Browse files Browse the repository at this point in the history
* port cachegrind.h to zig
* import cachegrind.zig in valgrind.zig
* Avoid Redundant Names in Fully-Qualified Namespaces
  • Loading branch information
ProkopRandacek authored Aug 24, 2024
1 parent ab69482 commit 9374725
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 32 deletions.
2 changes: 2 additions & 0 deletions lib/std/valgrind.zig
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,10 @@ pub fn monitorCommand(command: [*]u8) bool {

pub const memcheck = @import("valgrind/memcheck.zig");
pub const callgrind = @import("valgrind/callgrind.zig");
pub const cachegrind = @import("valgrind/cachegrind.zig");

test {
_ = memcheck;
_ = callgrind;
_ = cachegrind;
}
29 changes: 29 additions & 0 deletions lib/std/valgrind/cachegrind.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const std = @import("../std.zig");
const valgrind = std.valgrind;

pub const ClientRequest = enum(usize) {
StartInstrumentation = valgrind.ToolBase("CG".*),
StopInstrumentation,
};

fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
}

fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
_ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
}

/// Start Cachegrind instrumentation if not already enabled. Use this in
/// combination with `std.valgrind.cachegrind.stopInstrumentation` and
/// `--instr-at-start` to measure only part of a client program's execution.
pub fn startInstrumentation() void {
doClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
}

/// Stop Cachegrind instrumentation if not already disabled. Use this in
/// combination with `std.valgrind.cachegrind.startInstrumentation` and
/// `--instr-at-start` to measure only part of a client program's execution.
pub fn stopInstrumentation() void {
doClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
}
22 changes: 12 additions & 10 deletions lib/std/valgrind/callgrind.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const std = @import("../std.zig");
const valgrind = std.valgrind;

pub const CallgrindClientRequest = enum(usize) {
pub const ClientRequest = enum(usize) {
DumpStats = valgrind.ToolBase("CT".*),
ZeroStats,
ToggleCollect,
Expand All @@ -10,46 +10,48 @@ pub const CallgrindClientRequest = enum(usize) {
StopInstrumentation,
};

fn doCallgrindClientRequestExpr(default: usize, request: CallgrindClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
pub const CallgrindClientRequest = @compileError("std.valgrind.callgrind.CallgrindClientRequest renamed to std.valgrind.callgrind.ClientRequest");

fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
}

fn doCallgrindClientRequestStmt(request: CallgrindClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
_ = doCallgrindClientRequestExpr(0, request, a1, a2, a3, a4, a5);
fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
_ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
}

/// Dump current state of cost centers, and zero them afterwards
pub fn dumpStats() void {
doCallgrindClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);
doClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);
}

/// Dump current state of cost centers, and zero them afterwards.
/// The argument is appended to a string stating the reason which triggered
/// the dump. This string is written as a description field into the
/// profile data dump.
pub fn dumpStatsAt(pos_str: [*:0]const u8) void {
doCallgrindClientRequestStmt(.DumpStatsAt, @intFromPtr(pos_str), 0, 0, 0, 0);
doClientRequestStmt(.DumpStatsAt, @intFromPtr(pos_str), 0, 0, 0, 0);
}

/// Zero cost centers
pub fn zeroStats() void {
doCallgrindClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);
doClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);
}

/// Toggles collection state.
/// The collection state specifies whether the happening of events
/// should be noted or if they are to be ignored. Events are noted
/// by increment of counters in a cost center
pub fn toggleCollect() void {
doCallgrindClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);
doClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);
}

/// Start full callgrind instrumentation if not already switched on.
/// When cache simulation is done, it will flush the simulated cache;
/// this will lead to an artificial cache warmup phase afterwards with
/// cache misses which would not have happened in reality.
pub fn startInstrumentation() void {
doCallgrindClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
doClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
}

/// Stop full callgrind instrumentation if not already switched off.
Expand All @@ -60,5 +62,5 @@ pub fn startInstrumentation() void {
/// To start Callgrind in this mode to ignore the setup phase, use
/// the option "--instr-atstart=no".
pub fn stopInstrumentation() void {
doCallgrindClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
doClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
}
46 changes: 24 additions & 22 deletions lib/std/valgrind/memcheck.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("../std.zig");
const testing = std.testing;
const valgrind = std.valgrind;

pub const MemCheckClientRequest = enum(usize) {
pub const ClientRequest = enum(usize) {
MakeMemNoAccess = valgrind.ToolBase("MC".*),
MakeMemUndefined,
MakeMemDefined,
Expand All @@ -20,37 +20,39 @@ pub const MemCheckClientRequest = enum(usize) {
DisableAddrErrorReportingInRange,
};

fn doMemCheckClientRequestExpr(default: usize, request: MemCheckClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
pub const MemCheckClientRequest = @compileError("std.valgrind.memcheck.MemCheckClientRequest renamed to std.valgrind.memcheck.ClientRequest");

fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
}

fn doMemCheckClientRequestStmt(request: MemCheckClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
_ = doMemCheckClientRequestExpr(0, request, a1, a2, a3, a4, a5);
fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
_ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
}

/// Mark memory at qzz.ptr as unaddressable for qzz.len bytes.
pub fn makeMemNoAccess(qzz: []const u8) void {
_ = doMemCheckClientRequestExpr(0, // default return
_ = doClientRequestExpr(0, // default return
.MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

/// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes.
pub fn makeMemUndefined(qzz: []const u8) void {
_ = doMemCheckClientRequestExpr(0, // default return
_ = doClientRequestExpr(0, // default return
.MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

/// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes.
pub fn makeMemDefined(qzz: []const u8) void {
_ = doMemCheckClientRequestExpr(0, // default return
_ = doClientRequestExpr(0, // default return
.MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

/// Similar to makeMemDefined except that addressability is
/// not altered: bytes which are addressable are marked as defined,
/// but those which are not addressable are left unchanged.
pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
_ = doMemCheckClientRequestExpr(0, // default return
_ = doClientRequestExpr(0, // default return
.MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

Expand All @@ -59,14 +61,14 @@ pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
/// within the specified memory range. Has no other effect on the
/// properties of the memory range.
pub fn createBlock(qzz: []const u8, desc: [*:0]const u8) usize {
return doMemCheckClientRequestExpr(0, // default return
return doClientRequestExpr(0, // default return
.CreateBlock, @intFromPtr(qzz.ptr), qzz.len, @intFromPtr(desc), 0, 0);
}

/// Discard a block-description-handle. Returns 1 for an
/// invalid handle, 0 for a valid handle.
pub fn discard(blkindex: usize) bool {
return doMemCheckClientRequestExpr(0, // default return
return doClientRequestExpr(0, // default return
.Discard, 0, blkindex, 0, 0, 0) != 0;
}

Expand All @@ -75,39 +77,39 @@ pub fn discard(blkindex: usize) bool {
/// error message and returns the address of the first offending byte.
/// Otherwise it returns zero.
pub fn checkMemIsAddressable(qzz: []const u8) usize {
return doMemCheckClientRequestExpr(0, .CheckMemIsAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
return doClientRequestExpr(0, .CheckMemIsAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

/// Check that memory at qzz.ptr is addressable and defined for
/// qzz.len bytes. If suitable addressability and definedness are not
/// established, Valgrind prints an error message and returns the
/// address of the first offending byte. Otherwise it returns zero.
pub fn checkMemIsDefined(qzz: []const u8) usize {
return doMemCheckClientRequestExpr(0, .CheckMemIsDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
return doClientRequestExpr(0, .CheckMemIsDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

/// Do a full memory leak check (like --leak-check=full) mid-execution.
pub fn doLeakCheck() void {
doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);
doClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);
}

/// Same as doLeakCheck() but only showing the entries for
/// which there was an increase in leaked bytes or leaked nr of blocks
/// since the previous leak search.
pub fn doAddedLeakCheck() void {
doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);
doClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);
}

/// Same as doAddedLeakCheck() but showing entries with
/// increased or decreased leaked bytes/blocks since previous leak
/// search.
pub fn doChangedLeakCheck() void {
doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);
doClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);
}

/// Do a summary memory leak check (like --leak-check=summary) mid-execution.
pub fn doQuickLeakCheck() void {
doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);
doClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);
}

/// Return number of leaked, dubious, reachable and suppressed bytes found by
Expand All @@ -126,7 +128,7 @@ pub fn countLeaks() CountResult {
.reachable = 0,
.suppressed = 0,
};
doMemCheckClientRequestStmt(
doClientRequestStmt(
.CountLeaks,
@intFromPtr(&res.leaked),
@intFromPtr(&res.dubious),
Expand Down Expand Up @@ -156,7 +158,7 @@ pub fn countLeakBlocks() CountResult {
.reachable = 0,
.suppressed = 0,
};
doMemCheckClientRequestStmt(
doClientRequestStmt(
.CountLeakBlocks,
@intFromPtr(&res.leaked),
@intFromPtr(&res.dubious),
Expand Down Expand Up @@ -189,7 +191,7 @@ test countLeakBlocks {
/// impossible to segfault your system by using this call.
pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
std.debug.assert(zzvbits.len >= zza.len / 8);
return @as(u2, @intCast(doMemCheckClientRequestExpr(0, .GetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
return @as(u2, @intCast(doClientRequestExpr(0, .GetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
}

/// Set the validity data for addresses zza, copying it
Expand All @@ -202,17 +204,17 @@ pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
/// impossible to segfault your system by using this call.
pub fn setVbits(zzvbits: []u8, zza: []u8) u2 {
std.debug.assert(zzvbits.len >= zza.len / 8);
return @as(u2, @intCast(doMemCheckClientRequestExpr(0, .SetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
return @as(u2, @intCast(doClientRequestExpr(0, .SetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
}

/// Disable and re-enable reporting of addressing errors in the
/// specified address range.
pub fn disableAddrErrorReportingInRange(qzz: []u8) usize {
return doMemCheckClientRequestExpr(0, // default return
return doClientRequestExpr(0, // default return
.DisableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

pub fn enableAddrErrorReportingInRange(qzz: []u8) usize {
return doMemCheckClientRequestExpr(0, // default return
return doClientRequestExpr(0, // default return
.EnableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
}

0 comments on commit 9374725

Please sign in to comment.