Commit 9374725088

Prokop Randáček <prokop@rdck.dev>
2024-08-24 07:59:30
port cachegrind.h to zig (#19241)
* port cachegrind.h to zig * import cachegrind.zig in valgrind.zig * Avoid Redundant Names in Fully-Qualified Namespaces
1 parent ab69482
Changed files (4)
lib/std/valgrind/cachegrind.zig
@@ -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);
+}
lib/std/valgrind/callgrind.zig
@@ -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,
@@ -10,17 +10,19 @@ 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.
@@ -28,12 +30,12 @@ pub fn dumpStats() void {
 /// 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.
@@ -41,7 +43,7 @@ pub fn zeroStats() void {
 /// 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.
@@ -49,7 +51,7 @@ pub fn toggleCollect() void {
 /// 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.
@@ -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);
 }
lib/std/valgrind/memcheck.zig
@@ -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,
@@ -20,29 +20,31 @@ 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);
 }
 
@@ -50,7 +52,7 @@ pub fn makeMemDefined(qzz: []const u8) void {
 /// 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);
 }
 
@@ -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;
 }
 
@@ -75,7 +77,7 @@ 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
@@ -83,31 +85,31 @@ pub fn checkMemIsAddressable(qzz: []const u8) usize {
 /// 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
@@ -126,7 +128,7 @@ pub fn countLeaks() CountResult {
         .reachable = 0,
         .suppressed = 0,
     };
-    doMemCheckClientRequestStmt(
+    doClientRequestStmt(
         .CountLeaks,
         @intFromPtr(&res.leaked),
         @intFromPtr(&res.dubious),
@@ -156,7 +158,7 @@ pub fn countLeakBlocks() CountResult {
         .reachable = 0,
         .suppressed = 0,
     };
-    doMemCheckClientRequestStmt(
+    doClientRequestStmt(
         .CountLeakBlocks,
         @intFromPtr(&res.leaked),
         @intFromPtr(&res.dubious),
@@ -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
@@ -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);
 }
lib/std/valgrind.zig
@@ -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;
 }