authorgravatar for prokop@rdck.devProkop Randáček <prokop@rdck.dev> 2024-08-24 07:59:30+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-08-23 22:59:30-07:00
log9374725088fa077b61d819ed36341b20f8f3bcf7
treed61c3fd66ef0cebbf0037c522eba7e2e3585a519
parentab69482a5ddd7750039a5ba9531a04ac0051aa18
signaturebadge-check Signed by PGP key B5690EEEBB952194

port cachegrind.h to zig (#19241)

* port cachegrind.h to zig * import cachegrind.zig in valgrind.zig * Avoid Redundant Names in Fully-Qualified Namespaces

4 files changed, 67 insertions(+), 32 deletions(-)

lib/std/valgrind.zig+2
...@@ -278,8 +278,10 @@ pub fn monitorCommand(command: [*]u8) bool {...@@ -278,8 +278,10 @@ pub fn monitorCommand(command: [*]u8) bool {
278278
279pub const memcheck = @import("valgrind/memcheck.zig");279pub const memcheck = @import("valgrind/memcheck.zig");
280pub const callgrind = @import("valgrind/callgrind.zig");280pub const callgrind = @import("valgrind/callgrind.zig");
281pub const cachegrind = @import("valgrind/cachegrind.zig");
281282
282test {283test {
283 _ = memcheck;284 _ = memcheck;
284 _ = callgrind;285 _ = callgrind;
286 _ = cachegrind;
285}287}
lib/std/valgrind/cachegrind.zig created+29
...@@ -0,0 +1,29 @@
1const std = @import("../std.zig");
2const valgrind = std.valgrind;
3
4pub const ClientRequest = enum(usize) {
5 StartInstrumentation = valgrind.ToolBase("CG".*),
6 StopInstrumentation,
7};
8
9fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
10 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
11}
12
13fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
14 _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
15}
16
17/// Start Cachegrind instrumentation if not already enabled. Use this in
18/// combination with `std.valgrind.cachegrind.stopInstrumentation` and
19/// `--instr-at-start` to measure only part of a client program's execution.
20pub fn startInstrumentation() void {
21 doClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
22}
23
24/// Stop Cachegrind instrumentation if not already disabled. Use this in
25/// combination with `std.valgrind.cachegrind.startInstrumentation` and
26/// `--instr-at-start` to measure only part of a client program's execution.
27pub fn stopInstrumentation() void {
28 doClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
29}
lib/std/valgrind/callgrind.zig+12-10
...@@ -1,7 +1,7 @@...@@ -1,7 +1,7 @@
1const std = @import("../std.zig");1const std = @import("../std.zig");
2const valgrind = std.valgrind;2const valgrind = std.valgrind;
33
4pub const CallgrindClientRequest = enum(usize) {4pub const ClientRequest = enum(usize) {
5 DumpStats = valgrind.ToolBase("CT".*),5 DumpStats = valgrind.ToolBase("CT".*),
6 ZeroStats,6 ZeroStats,
7 ToggleCollect,7 ToggleCollect,
...@@ -10,17 +10,19 @@ pub const CallgrindClientRequest = enum(usize) {...@@ -10,17 +10,19 @@ pub const CallgrindClientRequest = enum(usize) {
10 StopInstrumentation,10 StopInstrumentation,
11};11};
1212
13fn doCallgrindClientRequestExpr(default: usize, request: CallgrindClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {13pub const CallgrindClientRequest = @compileError("std.valgrind.callgrind.CallgrindClientRequest renamed to std.valgrind.callgrind.ClientRequest");
14
15fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
14 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);16 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
15}17}
1618
17fn doCallgrindClientRequestStmt(request: CallgrindClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {19fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
18 _ = doCallgrindClientRequestExpr(0, request, a1, a2, a3, a4, a5);20 _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
19}21}
2022
21/// Dump current state of cost centers, and zero them afterwards23/// Dump current state of cost centers, and zero them afterwards
22pub fn dumpStats() void {24pub fn dumpStats() void {
23 doCallgrindClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);25 doClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);
24}26}
2527
26/// Dump current state of cost centers, and zero them afterwards.28/// Dump current state of cost centers, and zero them afterwards.
...@@ -28,12 +30,12 @@ pub fn dumpStats() void {...@@ -28,12 +30,12 @@ pub fn dumpStats() void {
28/// the dump. This string is written as a description field into the30/// the dump. This string is written as a description field into the
29/// profile data dump.31/// profile data dump.
30pub fn dumpStatsAt(pos_str: [*:0]const u8) void {32pub fn dumpStatsAt(pos_str: [*:0]const u8) void {
31 doCallgrindClientRequestStmt(.DumpStatsAt, @intFromPtr(pos_str), 0, 0, 0, 0);33 doClientRequestStmt(.DumpStatsAt, @intFromPtr(pos_str), 0, 0, 0, 0);
32}34}
3335
34/// Zero cost centers36/// Zero cost centers
35pub fn zeroStats() void {37pub fn zeroStats() void {
36 doCallgrindClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);38 doClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);
37}39}
3840
39/// Toggles collection state.41/// Toggles collection state.
...@@ -41,7 +43,7 @@ pub fn zeroStats() void {...@@ -41,7 +43,7 @@ pub fn zeroStats() void {
41/// should be noted or if they are to be ignored. Events are noted43/// should be noted or if they are to be ignored. Events are noted
42/// by increment of counters in a cost center44/// by increment of counters in a cost center
43pub fn toggleCollect() void {45pub fn toggleCollect() void {
44 doCallgrindClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);46 doClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);
45}47}
4648
47/// Start full callgrind instrumentation if not already switched on.49/// Start full callgrind instrumentation if not already switched on.
...@@ -49,7 +51,7 @@ pub fn toggleCollect() void {...@@ -49,7 +51,7 @@ pub fn toggleCollect() void {
49/// this will lead to an artificial cache warmup phase afterwards with51/// this will lead to an artificial cache warmup phase afterwards with
50/// cache misses which would not have happened in reality.52/// cache misses which would not have happened in reality.
51pub fn startInstrumentation() void {53pub fn startInstrumentation() void {
52 doCallgrindClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);54 doClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
53}55}
5456
55/// Stop full callgrind instrumentation if not already switched off.57/// Stop full callgrind instrumentation if not already switched off.
...@@ -60,5 +62,5 @@ pub fn startInstrumentation() void {...@@ -60,5 +62,5 @@ pub fn startInstrumentation() void {
60/// To start Callgrind in this mode to ignore the setup phase, use62/// To start Callgrind in this mode to ignore the setup phase, use
61/// the option "--instr-atstart=no".63/// the option "--instr-atstart=no".
62pub fn stopInstrumentation() void {64pub fn stopInstrumentation() void {
63 doCallgrindClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);65 doClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
64}66}
lib/std/valgrind/memcheck.zig+24-22
...@@ -2,7 +2,7 @@ const std = @import("../std.zig");...@@ -2,7 +2,7 @@ const std = @import("../std.zig");
2const testing = std.testing;2const testing = std.testing;
3const valgrind = std.valgrind;3const valgrind = std.valgrind;
44
5pub const MemCheckClientRequest = enum(usize) {5pub const ClientRequest = enum(usize) {
6 MakeMemNoAccess = valgrind.ToolBase("MC".*),6 MakeMemNoAccess = valgrind.ToolBase("MC".*),
7 MakeMemUndefined,7 MakeMemUndefined,
8 MakeMemDefined,8 MakeMemDefined,
...@@ -20,29 +20,31 @@ pub const MemCheckClientRequest = enum(usize) {...@@ -20,29 +20,31 @@ pub const MemCheckClientRequest = enum(usize) {
20 DisableAddrErrorReportingInRange,20 DisableAddrErrorReportingInRange,
21};21};
2222
23fn doMemCheckClientRequestExpr(default: usize, request: MemCheckClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {23pub const MemCheckClientRequest = @compileError("std.valgrind.memcheck.MemCheckClientRequest renamed to std.valgrind.memcheck.ClientRequest");
24
25fn doClientRequestExpr(default: usize, request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) usize {
24 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);26 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
25}27}
2628
27fn doMemCheckClientRequestStmt(request: MemCheckClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {29fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
28 _ = doMemCheckClientRequestExpr(0, request, a1, a2, a3, a4, a5);30 _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
29}31}
3032
31/// Mark memory at qzz.ptr as unaddressable for qzz.len bytes.33/// Mark memory at qzz.ptr as unaddressable for qzz.len bytes.
32pub fn makeMemNoAccess(qzz: []const u8) void {34pub fn makeMemNoAccess(qzz: []const u8) void {
33 _ = doMemCheckClientRequestExpr(0, // default return35 _ = doClientRequestExpr(0, // default return
34 .MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);36 .MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
35}37}
3638
37/// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes.39/// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes.
38pub fn makeMemUndefined(qzz: []const u8) void {40pub fn makeMemUndefined(qzz: []const u8) void {
39 _ = doMemCheckClientRequestExpr(0, // default return41 _ = doClientRequestExpr(0, // default return
40 .MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);42 .MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
41}43}
4244
43/// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes.45/// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes.
44pub fn makeMemDefined(qzz: []const u8) void {46pub fn makeMemDefined(qzz: []const u8) void {
45 _ = doMemCheckClientRequestExpr(0, // default return47 _ = doClientRequestExpr(0, // default return
46 .MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);48 .MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
47}49}
4850
...@@ -50,7 +52,7 @@ pub fn makeMemDefined(qzz: []const u8) void {...@@ -50,7 +52,7 @@ pub fn makeMemDefined(qzz: []const u8) void {
50/// not altered: bytes which are addressable are marked as defined,52/// not altered: bytes which are addressable are marked as defined,
51/// but those which are not addressable are left unchanged.53/// but those which are not addressable are left unchanged.
52pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {54pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
53 _ = doMemCheckClientRequestExpr(0, // default return55 _ = doClientRequestExpr(0, // default return
54 .MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);56 .MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
55}57}
5658
...@@ -59,14 +61,14 @@ pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {...@@ -59,14 +61,14 @@ pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
59/// within the specified memory range. Has no other effect on the61/// within the specified memory range. Has no other effect on the
60/// properties of the memory range.62/// properties of the memory range.
61pub fn createBlock(qzz: []const u8, desc: [*:0]const u8) usize {63pub fn createBlock(qzz: []const u8, desc: [*:0]const u8) usize {
62 return doMemCheckClientRequestExpr(0, // default return64 return doClientRequestExpr(0, // default return
63 .CreateBlock, @intFromPtr(qzz.ptr), qzz.len, @intFromPtr(desc), 0, 0);65 .CreateBlock, @intFromPtr(qzz.ptr), qzz.len, @intFromPtr(desc), 0, 0);
64}66}
6567
66/// Discard a block-description-handle. Returns 1 for an68/// Discard a block-description-handle. Returns 1 for an
67/// invalid handle, 0 for a valid handle.69/// invalid handle, 0 for a valid handle.
68pub fn discard(blkindex: usize) bool {70pub fn discard(blkindex: usize) bool {
69 return doMemCheckClientRequestExpr(0, // default return71 return doClientRequestExpr(0, // default return
70 .Discard, 0, blkindex, 0, 0, 0) != 0;72 .Discard, 0, blkindex, 0, 0, 0) != 0;
71}73}
7274
...@@ -75,7 +77,7 @@ pub fn discard(blkindex: usize) bool {...@@ -75,7 +77,7 @@ pub fn discard(blkindex: usize) bool {
75/// error message and returns the address of the first offending byte.77/// error message and returns the address of the first offending byte.
76/// Otherwise it returns zero.78/// Otherwise it returns zero.
77pub fn checkMemIsAddressable(qzz: []const u8) usize {79pub fn checkMemIsAddressable(qzz: []const u8) usize {
78 return doMemCheckClientRequestExpr(0, .CheckMemIsAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);80 return doClientRequestExpr(0, .CheckMemIsAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
79}81}
8082
81/// Check that memory at qzz.ptr is addressable and defined for83/// Check that memory at qzz.ptr is addressable and defined for
...@@ -83,31 +85,31 @@ pub fn checkMemIsAddressable(qzz: []const u8) usize {...@@ -83,31 +85,31 @@ pub fn checkMemIsAddressable(qzz: []const u8) usize {
83/// established, Valgrind prints an error message and returns the85/// established, Valgrind prints an error message and returns the
84/// address of the first offending byte. Otherwise it returns zero.86/// address of the first offending byte. Otherwise it returns zero.
85pub fn checkMemIsDefined(qzz: []const u8) usize {87pub fn checkMemIsDefined(qzz: []const u8) usize {
86 return doMemCheckClientRequestExpr(0, .CheckMemIsDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);88 return doClientRequestExpr(0, .CheckMemIsDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
87}89}
8890
89/// Do a full memory leak check (like --leak-check=full) mid-execution.91/// Do a full memory leak check (like --leak-check=full) mid-execution.
90pub fn doLeakCheck() void {92pub fn doLeakCheck() void {
91 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);93 doClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);
92}94}
9395
94/// Same as doLeakCheck() but only showing the entries for96/// Same as doLeakCheck() but only showing the entries for
95/// which there was an increase in leaked bytes or leaked nr of blocks97/// which there was an increase in leaked bytes or leaked nr of blocks
96/// since the previous leak search.98/// since the previous leak search.
97pub fn doAddedLeakCheck() void {99pub fn doAddedLeakCheck() void {
98 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);100 doClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);
99}101}
100102
101/// Same as doAddedLeakCheck() but showing entries with103/// Same as doAddedLeakCheck() but showing entries with
102/// increased or decreased leaked bytes/blocks since previous leak104/// increased or decreased leaked bytes/blocks since previous leak
103/// search.105/// search.
104pub fn doChangedLeakCheck() void {106pub fn doChangedLeakCheck() void {
105 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);107 doClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);
106}108}
107109
108/// Do a summary memory leak check (like --leak-check=summary) mid-execution.110/// Do a summary memory leak check (like --leak-check=summary) mid-execution.
109pub fn doQuickLeakCheck() void {111pub fn doQuickLeakCheck() void {
110 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);112 doClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);
111}113}
112114
113/// Return number of leaked, dubious, reachable and suppressed bytes found by115/// Return number of leaked, dubious, reachable and suppressed bytes found by
...@@ -126,7 +128,7 @@ pub fn countLeaks() CountResult {...@@ -126,7 +128,7 @@ pub fn countLeaks() CountResult {
126 .reachable = 0,128 .reachable = 0,
127 .suppressed = 0,129 .suppressed = 0,
128 };130 };
129 doMemCheckClientRequestStmt(131 doClientRequestStmt(
130 .CountLeaks,132 .CountLeaks,
131 @intFromPtr(&res.leaked),133 @intFromPtr(&res.leaked),
132 @intFromPtr(&res.dubious),134 @intFromPtr(&res.dubious),
...@@ -156,7 +158,7 @@ pub fn countLeakBlocks() CountResult {...@@ -156,7 +158,7 @@ pub fn countLeakBlocks() CountResult {
156 .reachable = 0,158 .reachable = 0,
157 .suppressed = 0,159 .suppressed = 0,
158 };160 };
159 doMemCheckClientRequestStmt(161 doClientRequestStmt(
160 .CountLeakBlocks,162 .CountLeakBlocks,
161 @intFromPtr(&res.leaked),163 @intFromPtr(&res.leaked),
162 @intFromPtr(&res.dubious),164 @intFromPtr(&res.dubious),
...@@ -189,7 +191,7 @@ test countLeakBlocks {...@@ -189,7 +191,7 @@ test countLeakBlocks {
189/// impossible to segfault your system by using this call.191/// impossible to segfault your system by using this call.
190pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {192pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
191 std.debug.assert(zzvbits.len >= zza.len / 8);193 std.debug.assert(zzvbits.len >= zza.len / 8);
192 return @as(u2, @intCast(doMemCheckClientRequestExpr(0, .GetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));194 return @as(u2, @intCast(doClientRequestExpr(0, .GetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
193}195}
194196
195/// Set the validity data for addresses zza, copying it197/// Set the validity data for addresses zza, copying it
...@@ -202,17 +204,17 @@ pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {...@@ -202,17 +204,17 @@ pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
202/// impossible to segfault your system by using this call.204/// impossible to segfault your system by using this call.
203pub fn setVbits(zzvbits: []u8, zza: []u8) u2 {205pub fn setVbits(zzvbits: []u8, zza: []u8) u2 {
204 std.debug.assert(zzvbits.len >= zza.len / 8);206 std.debug.assert(zzvbits.len >= zza.len / 8);
205 return @as(u2, @intCast(doMemCheckClientRequestExpr(0, .SetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));207 return @as(u2, @intCast(doClientRequestExpr(0, .SetVbits, @intFromPtr(zza.ptr), @intFromPtr(zzvbits), zza.len, 0, 0)));
206}208}
207209
208/// Disable and re-enable reporting of addressing errors in the210/// Disable and re-enable reporting of addressing errors in the
209/// specified address range.211/// specified address range.
210pub fn disableAddrErrorReportingInRange(qzz: []u8) usize {212pub fn disableAddrErrorReportingInRange(qzz: []u8) usize {
211 return doMemCheckClientRequestExpr(0, // default return213 return doClientRequestExpr(0, // default return
212 .DisableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);214 .DisableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
213}215}
214216
215pub fn enableAddrErrorReportingInRange(qzz: []u8) usize {217pub fn enableAddrErrorReportingInRange(qzz: []u8) usize {
216 return doMemCheckClientRequestExpr(0, // default return218 return doClientRequestExpr(0, // default return
217 .EnableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);219 .EnableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
218}220}