Skip to content

Commit 47111c4

Browse files
authored
Use prctl to enable/ disable perf for lower overhead (#38)
prctl will enable or disable all benches, so you need to close them after you're done with them otherwise you'll quickly have too many benches and you won't get any results out of your new ones (not sure if the old ones will give results). Co-authored-by: Zentrik <[email protected]>
1 parent ec2d084 commit 47111c4

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

src/LinuxPerf.jl

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,20 @@ function Base.show(io::IO, g::EventGroup)
325325
print(io, "\t", g.event_types[end], ")")
326326
end
327327

328+
const SYS_prctl = Clong(157)
329+
const PR_TASK_PERF_EVENTS_DISABLE = Cint(31)
330+
const PR_TASK_PERF_EVENTS_ENABLE = Cint(32)
331+
332+
# syscall is lower overhead than calling libc's prctl
333+
function enable_all!()
334+
res = ccall(:syscall, Cint, (Clong, Clong...), SYS_prctl, PR_TASK_PERF_EVENTS_ENABLE)
335+
Base.systemerror(:prctl, res < 0)
336+
end
337+
function disable_all!()
338+
res = ccall(:syscall, Cint, (Clong, Clong...), SYS_prctl, PR_TASK_PERF_EVENTS_DISABLE)
339+
Base.systemerror(:prctl, res < 0)
340+
end
341+
328342
const PERF_EVENT_IOC_ENABLE = UInt64(0x2400)
329343
const PERF_EVENT_IOC_DISABLE = UInt64(0x2401)
330344
const PERF_EVENT_IOC_RESET = UInt64(0x2403)
@@ -1068,9 +1082,9 @@ macro pstats(args...)
10681082
@debug dump_groups(groups)
10691083
bench = make_bench_threaded(groups, threads = $(opts.threads))
10701084
try
1071-
enable!(bench)
1085+
enable_all!()
10721086
val = $(esc(expr))
1073-
disable!(bench)
1087+
disable_all!()
10741088
# trick the compiler not to eliminate the code
10751089
stats = rand() < 0 ? val : Stats(bench)
10761090
return stats::Stats

test/runtests.jl

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using LinuxPerf
22
using Test
33

4-
using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventType, EventTypeExt, parse_groups
4+
using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, counters, EventType, EventTypeExt, parse_groups, enable_all!, disable_all!
55

66
@testset "LinuxPerf" begin
77

@@ -21,7 +21,29 @@ using LinuxPerf: make_bench, enable!, disable!, reset!, reasonable_defaults, cou
2121
end
2222
g(zeros(10000))
2323

24-
counters(bench)
24+
results = counters(bench)
25+
close(bench)
26+
27+
true # Succeeded without any exceptions...
28+
end
29+
30+
@test begin
31+
bench = make_bench(reasonable_defaults);
32+
@noinline function g(a)
33+
enable_all!()
34+
c = 0
35+
for x in a
36+
if x > 0
37+
c += 1
38+
end
39+
end
40+
disable_all!()
41+
c
42+
end
43+
g(zeros(10000))
44+
45+
results = counters(bench)
46+
close(bench)
2547

2648
true # Succeeded without any exceptions...
2749
end

0 commit comments

Comments
 (0)