Skip to content

Commit da7880b

Browse files
committed
std: Add valgrind module
1 parent 067968c commit da7880b

File tree

5 files changed

+709
-0
lines changed

5 files changed

+709
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,9 @@ set(ZIG_STD_FILES
667667
"statically_initialized_mutex.zig"
668668
"testing.zig"
669669
"unicode.zig"
670+
"valgrind/callgrind.zig"
671+
"valgrind/index.zig"
672+
"valgrind/memcheck.zig"
670673
"zig/ast.zig"
671674
"zig/index.zig"
672675
"zig/parse.zig"

std/index.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub const rb = @import("rb.zig");
4444
pub const sort = @import("sort.zig");
4545
pub const testing = @import("testing.zig");
4646
pub const unicode = @import("unicode.zig");
47+
pub const valgrind = @import("valgrind/index.zig");
4748
pub const zig = @import("zig/index.zig");
4849

4950
test "std" {

std/valgrind/callgrind.zig

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const std = @import("../index.zig");
2+
const valgrind = std.valgrind;
3+
4+
pub const CallgrindClientRequest = extern enum {
5+
DumpStats = valgrind.ToolBase("CT"),
6+
ZeroStats,
7+
ToggleCollect,
8+
DumpStatsAt,
9+
StartInstrumentation,
10+
StopInstrumentation,
11+
};
12+
13+
fn doCallgrindClientRequestExpr(default: usize, request: CallgrindClientRequest,
14+
a1: usize, a2: usize, a3: usize, a4: usize, a5: usize
15+
) usize
16+
{
17+
return valgrind.doClientRequest(
18+
default,
19+
@intCast(usize, @enumToInt(request)),
20+
a1, a2, a3, a4, a5);
21+
}
22+
23+
fn doCallgrindClientRequestStmt(request: CallgrindClientRequest,
24+
a1: usize, a2: usize, a3: usize, a4: usize, a5: usize
25+
) void
26+
{
27+
_ = doCallgrindClientRequestExpr(0, request, a1, a2, a3, a4, a5);
28+
}
29+
30+
31+
32+
/// Dump current state of cost centers, and zero them afterwards
33+
pub fn dumpStats() void {
34+
doCallgrindClientRequestStmt(CallgrindClientRequest.DumpStats,
35+
0, 0, 0, 0, 0);
36+
}
37+
38+
39+
/// Dump current state of cost centers, and zero them afterwards.
40+
/// The argument is appended to a string stating the reason which triggered
41+
/// the dump. This string is written as a description field into the
42+
/// profile data dump.
43+
pub fn dumpStatsAt(pos_str: [*]u8) void {
44+
doCallgrindClientRequestStmt(CallgrindClientRequest.DumpStatsAt,
45+
@ptrToInt(pos_str),
46+
0, 0, 0, 0);
47+
}
48+
49+
50+
/// Zero cost centers
51+
pub fn zeroStats() void {
52+
doCallgrindClientRequestStmt(CallgrindClientRequest.ZeroStats,
53+
0, 0, 0, 0, 0);
54+
}
55+
56+
57+
/// Toggles collection state.
58+
/// The collection state specifies whether the happening of events
59+
/// should be noted or if they are to be ignored. Events are noted
60+
/// by increment of counters in a cost center
61+
pub fn toggleCollect() void {
62+
doCallgrindClientRequestStmt(CallgrindClientRequest.ToggleCollect,
63+
0, 0, 0, 0, 0);
64+
}
65+
66+
67+
/// Start full callgrind instrumentation if not already switched on.
68+
/// When cache simulation is done, it will flush the simulated cache;
69+
/// this will lead to an artificial cache warmup phase afterwards with
70+
/// cache misses which would not have happened in reality.
71+
pub fn startInstrumentation() void {
72+
doCallgrindClientRequestStmt(CallgrindClientRequest.StartInstrumentation,
73+
0, 0, 0, 0, 0);
74+
}
75+
76+
77+
/// Stop full callgrind instrumentation if not already switched off.
78+
/// This flushes Valgrinds translation cache, and does no additional
79+
/// instrumentation afterwards, which effectivly will run at the same
80+
/// speed as the "none" tool (ie. at minimal slowdown).
81+
/// Use this to bypass Callgrind aggregation for uninteresting code parts.
82+
/// To start Callgrind in this mode to ignore the setup phase, use
83+
/// the option "--instr-atstart=no".
84+
pub fn stopInstrumentation() void {
85+
doCallgrindClientRequestStmt(CallgrindClientRequest.StopInstrumentation,
86+
0, 0, 0, 0, 0);
87+
}

0 commit comments

Comments
 (0)