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 {
278278
279279pub const memcheck = @import("valgrind/memcheck.zig");
280280pub const callgrind = @import("valgrind/callgrind.zig");
281pub const cachegrind = @import("valgrind/cachegrind.zig");
281282
282283test {
283284 _ = memcheck;
284285 _ = callgrind;
286 _ = cachegrind;
285287}
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 @@
11const std = @import("../std.zig");
22const valgrind = std.valgrind;
33
4pub const CallgrindClientRequest = enum(usize) {
4pub const ClientRequest = enum(usize) {
55 DumpStats = valgrind.ToolBase("CT".*),
66 ZeroStats,
77 ToggleCollect,
......@@ -10,17 +10,19 @@ pub const CallgrindClientRequest = enum(usize) {
1010 StopInstrumentation,
1111};
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 {
1416 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
1517}
1618
17fn doCallgrindClientRequestStmt(request: CallgrindClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
18 _ = doCallgrindClientRequestExpr(0, request, a1, a2, a3, a4, a5);
19fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
20 _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
1921}
2022
2123/// Dump current state of cost centers, and zero them afterwards
2224pub fn dumpStats() void {
23 doCallgrindClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);
25 doClientRequestStmt(.DumpStats, 0, 0, 0, 0, 0);
2426}
2527
2628/// Dump current state of cost centers, and zero them afterwards.
......@@ -28,12 +30,12 @@ pub fn dumpStats() void {
2830/// the dump. This string is written as a description field into the
2931/// profile data dump.
3032pub 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);
3234}
3335
3436/// Zero cost centers
3537pub fn zeroStats() void {
36 doCallgrindClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);
38 doClientRequestStmt(.ZeroStats, 0, 0, 0, 0, 0);
3739}
3840
3941/// Toggles collection state.
......@@ -41,7 +43,7 @@ pub fn zeroStats() void {
4143/// should be noted or if they are to be ignored. Events are noted
4244/// by increment of counters in a cost center
4345pub fn toggleCollect() void {
44 doCallgrindClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);
46 doClientRequestStmt(.ToggleCollect, 0, 0, 0, 0, 0);
4547}
4648
4749/// Start full callgrind instrumentation if not already switched on.
......@@ -49,7 +51,7 @@ pub fn toggleCollect() void {
4951/// this will lead to an artificial cache warmup phase afterwards with
5052/// cache misses which would not have happened in reality.
5153pub fn startInstrumentation() void {
52 doCallgrindClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
54 doClientRequestStmt(.StartInstrumentation, 0, 0, 0, 0, 0);
5355}
5456
5557/// Stop full callgrind instrumentation if not already switched off.
......@@ -60,5 +62,5 @@ pub fn startInstrumentation() void {
6062/// To start Callgrind in this mode to ignore the setup phase, use
6163/// the option "--instr-atstart=no".
6264pub fn stopInstrumentation() void {
63 doCallgrindClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
65 doClientRequestStmt(.StopInstrumentation, 0, 0, 0, 0, 0);
6466}
lib/std/valgrind/memcheck.zig+24-22
......@@ -2,7 +2,7 @@ const std = @import("../std.zig");
22const testing = std.testing;
33const valgrind = std.valgrind;
44
5pub const MemCheckClientRequest = enum(usize) {
5pub const ClientRequest = enum(usize) {
66 MakeMemNoAccess = valgrind.ToolBase("MC".*),
77 MakeMemUndefined,
88 MakeMemDefined,
......@@ -20,29 +20,31 @@ pub const MemCheckClientRequest = enum(usize) {
2020 DisableAddrErrorReportingInRange,
2121};
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 {
2426 return valgrind.doClientRequest(default, @as(usize, @intCast(@intFromEnum(request))), a1, a2, a3, a4, a5);
2527}
2628
27fn doMemCheckClientRequestStmt(request: MemCheckClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
28 _ = doMemCheckClientRequestExpr(0, request, a1, a2, a3, a4, a5);
29fn doClientRequestStmt(request: ClientRequest, a1: usize, a2: usize, a3: usize, a4: usize, a5: usize) void {
30 _ = doClientRequestExpr(0, request, a1, a2, a3, a4, a5);
2931}
3032
3133/// Mark memory at qzz.ptr as unaddressable for qzz.len bytes.
3234pub fn makeMemNoAccess(qzz: []const u8) void {
33 _ = doMemCheckClientRequestExpr(0, // default return
35 _ = doClientRequestExpr(0, // default return
3436 .MakeMemNoAccess, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
3537}
3638
3739/// Mark memory at qzz.ptr as addressable but undefined for qzz.len bytes.
3840pub fn makeMemUndefined(qzz: []const u8) void {
39 _ = doMemCheckClientRequestExpr(0, // default return
41 _ = doClientRequestExpr(0, // default return
4042 .MakeMemUndefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
4143}
4244
4345/// Mark memory at qzz.ptr as addressable and defined or qzz.len bytes.
4446pub fn makeMemDefined(qzz: []const u8) void {
45 _ = doMemCheckClientRequestExpr(0, // default return
47 _ = doClientRequestExpr(0, // default return
4648 .MakeMemDefined, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
4749}
4850
......@@ -50,7 +52,7 @@ pub fn makeMemDefined(qzz: []const u8) void {
5052/// not altered: bytes which are addressable are marked as defined,
5153/// but those which are not addressable are left unchanged.
5254pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
53 _ = doMemCheckClientRequestExpr(0, // default return
55 _ = doClientRequestExpr(0, // default return
5456 .MakeMemDefinedIfAddressable, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
5557}
5658
......@@ -59,14 +61,14 @@ pub fn makeMemDefinedIfAddressable(qzz: []const u8) void {
5961/// within the specified memory range. Has no other effect on the
6062/// properties of the memory range.
6163pub fn createBlock(qzz: []const u8, desc: [*:0]const u8) usize {
62 return doMemCheckClientRequestExpr(0, // default return
64 return doClientRequestExpr(0, // default return
6365 .CreateBlock, @intFromPtr(qzz.ptr), qzz.len, @intFromPtr(desc), 0, 0);
6466}
6567
6668/// Discard a block-description-handle. Returns 1 for an
6769/// invalid handle, 0 for a valid handle.
6870pub fn discard(blkindex: usize) bool {
69 return doMemCheckClientRequestExpr(0, // default return
71 return doClientRequestExpr(0, // default return
7072 .Discard, 0, blkindex, 0, 0, 0) != 0;
7173}
7274
......@@ -75,7 +77,7 @@ pub fn discard(blkindex: usize) bool {
7577/// error message and returns the address of the first offending byte.
7678/// Otherwise it returns zero.
7779pub 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);
7981}
8082
8183/// Check that memory at qzz.ptr is addressable and defined for
......@@ -83,31 +85,31 @@ pub fn checkMemIsAddressable(qzz: []const u8) usize {
8385/// established, Valgrind prints an error message and returns the
8486/// address of the first offending byte. Otherwise it returns zero.
8587pub 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);
8789}
8890
8991/// Do a full memory leak check (like --leak-check=full) mid-execution.
9092pub fn doLeakCheck() void {
91 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);
93 doClientRequestStmt(.DO_LEAK_CHECK, 0, 0, 0, 0, 0);
9294}
9395
9496/// Same as doLeakCheck() but only showing the entries for
9597/// which there was an increase in leaked bytes or leaked nr of blocks
9698/// since the previous leak search.
9799pub fn doAddedLeakCheck() void {
98 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);
100 doClientRequestStmt(.DO_LEAK_CHECK, 0, 1, 0, 0, 0);
99101}
100102
101103/// Same as doAddedLeakCheck() but showing entries with
102104/// increased or decreased leaked bytes/blocks since previous leak
103105/// search.
104106pub fn doChangedLeakCheck() void {
105 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);
107 doClientRequestStmt(.DO_LEAK_CHECK, 0, 2, 0, 0, 0);
106108}
107109
108110/// Do a summary memory leak check (like --leak-check=summary) mid-execution.
109111pub fn doQuickLeakCheck() void {
110 doMemCheckClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);
112 doClientRequestStmt(.DO_LEAK_CHECK, 1, 0, 0, 0, 0);
111113}
112114
113115/// Return number of leaked, dubious, reachable and suppressed bytes found by
......@@ -126,7 +128,7 @@ pub fn countLeaks() CountResult {
126128 .reachable = 0,
127129 .suppressed = 0,
128130 };
129 doMemCheckClientRequestStmt(
131 doClientRequestStmt(
130132 .CountLeaks,
131133 @intFromPtr(&res.leaked),
132134 @intFromPtr(&res.dubious),
......@@ -156,7 +158,7 @@ pub fn countLeakBlocks() CountResult {
156158 .reachable = 0,
157159 .suppressed = 0,
158160 };
159 doMemCheckClientRequestStmt(
161 doClientRequestStmt(
160162 .CountLeakBlocks,
161163 @intFromPtr(&res.leaked),
162164 @intFromPtr(&res.dubious),
......@@ -189,7 +191,7 @@ test countLeakBlocks {
189191/// impossible to segfault your system by using this call.
190192pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
191193 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)));
193195}
194196
195197/// Set the validity data for addresses zza, copying it
......@@ -202,17 +204,17 @@ pub fn getVbits(zza: []u8, zzvbits: []u8) u2 {
202204/// impossible to segfault your system by using this call.
203205pub fn setVbits(zzvbits: []u8, zza: []u8) u2 {
204206 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)));
206208}
207209
208210/// Disable and re-enable reporting of addressing errors in the
209211/// specified address range.
210212pub fn disableAddrErrorReportingInRange(qzz: []u8) usize {
211 return doMemCheckClientRequestExpr(0, // default return
213 return doClientRequestExpr(0, // default return
212214 .DisableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
213215}
214216
215217pub fn enableAddrErrorReportingInRange(qzz: []u8) usize {
216 return doMemCheckClientRequestExpr(0, // default return
218 return doClientRequestExpr(0, // default return
217219 .EnableAddrErrorReportingInRange, @intFromPtr(qzz.ptr), qzz.len, 0, 0, 0);
218220}