|
16 | 16 |
|
17 | 17 | mutable struct Benchmark
|
18 | 18 | samplefunc
|
| 19 | + linux_perf_func |
19 | 20 | quote_vals
|
20 | 21 | params::Parameters
|
21 | 22 | end
|
@@ -116,15 +117,21 @@ function _run(b::Benchmark, p::Parameters; verbose=false, pad="", warmup=true, k
|
116 | 117 | b.samplefunc(b.quote_vals, Parameters(params; evals=1)) #warmup sample
|
117 | 118 | end
|
118 | 119 | params.gcsample && gcscrub()
|
119 |
| - trial_contents = b.samplefunc(b.quote_vals, params) |
120 |
| - push!(trial, trial_contents) |
121 |
| - return_val = trial_contents.return_val |
| 120 | + s = b.samplefunc(b.quote_vals, params) |
| 121 | + push!(trial, s[1:(end - 1)]...) |
| 122 | + return_val = s[end] |
122 | 123 | iters = 2
|
123 | 124 | while (Base.time() - start_time) < params.seconds && iters ≤ params.samples
|
124 | 125 | params.gcsample && gcscrub()
|
125 |
| - push!(trial, b.samplefunc(b.quote_vals, params)) |
| 126 | + push!(trial, b.samplefunc(b.quote_vals, params)[1:(end - 1)]...) |
126 | 127 | iters += 1
|
127 | 128 | end
|
| 129 | + |
| 130 | + if p.experimental_enable_linux_perf |
| 131 | + params.gcsample && gcscrub() |
| 132 | + trial.linux_perf_stats = b.linux_perf_func(b.quote_vals, params) |
| 133 | + end |
| 134 | + |
128 | 135 | return trial, return_val
|
129 | 136 | end
|
130 | 137 |
|
@@ -190,7 +197,7 @@ function _lineartrial(b::Benchmark, p::Parameters=b.params; maxevals=RESOLUTION,
|
190 | 197 | for evals in eachindex(estimates)
|
191 | 198 | params.gcsample && gcscrub()
|
192 | 199 | params.evals = evals
|
193 |
| - estimates[evals] = b.samplefunc(b.quote_vals, params).time |
| 200 | + estimates[evals] = first(b.samplefunc(b.quote_vals, params)) |
194 | 201 | completed += 1
|
195 | 202 | ((time() - start_time) > params.seconds) && break
|
196 | 203 | end
|
@@ -520,6 +527,7 @@ function generate_benchmark_definition(
|
520 | 527 | @nospecialize
|
521 | 528 | corefunc = gensym("core")
|
522 | 529 | samplefunc = gensym("sample")
|
| 530 | + linux_perf_func = gensym("perf") |
523 | 531 | type_vars = [gensym() for i in 1:(length(quote_vars) + length(setup_vars))]
|
524 | 532 | signature = Expr(:call, corefunc, quote_vars..., setup_vars...)
|
525 | 533 | signature_def = Expr(
|
@@ -586,64 +594,57 @@ function generate_benchmark_definition(
|
586 | 594 | __evals,
|
587 | 595 | ),
|
588 | 596 | )
|
589 |
| - if $(params.experimental_enable_linux_perf) |
590 |
| - # Based on https://github.com/JuliaPerf/LinuxPerf.jl/blob/a7fee0ff261a5b5ce7a903af7b38d1b5c27dd931/src/LinuxPerf.jl#L1043-L1061 |
591 |
| - __linux_perf_groups = BenchmarkTools.LinuxPerf.set_default_spaces( |
592 |
| - $(params.linux_perf_options.events), |
593 |
| - $(params.linux_perf_options.spaces), |
| 597 | + return __time, __gctime, __memory, __allocs, __return_val |
| 598 | + end |
| 599 | + @noinline function $(linux_perf_func)( |
| 600 | + $(Expr(:tuple, quote_vars...)), __params::$BenchmarkTools.Parameters |
| 601 | + ) |
| 602 | + # Based on https://github.com/JuliaPerf/LinuxPerf.jl/blob/a7fee0ff261a5b5ce7a903af7b38d1b5c27dd931/src/LinuxPerf.jl#L1043-L1061 |
| 603 | + __linux_perf_groups = $LinuxPerf.set_default_spaces( |
| 604 | + eval(__params.linux_perf_options.events), |
| 605 | + eval(__params.linux_perf_options.spaces), |
| 606 | + ) |
| 607 | + __linux_perf_bench = nothing |
| 608 | + try |
| 609 | + __linux_perf_bench = $LinuxPerf.make_bench_threaded( |
| 610 | + __linux_perf_groups; |
| 611 | + threads=eval(__params.linux_perf_options.threads), |
594 | 612 | )
|
595 |
| - __linux_perf_bench = nothing |
596 |
| - try |
597 |
| - __linux_perf_bench = BenchmarkTools.LinuxPerf.make_bench_threaded( |
598 |
| - __linux_perf_groups; |
599 |
| - threads=$(params.linux_perf_options.threads), |
600 |
| - ) |
601 |
| - catch e |
602 |
| - if e isa ErrorException && |
603 |
| - startswith(e.msg, "perf_event_open error : ") |
604 |
| - @warn "Perf is disabled" |
605 |
| - else |
606 |
| - rethrow() |
607 |
| - end |
| 613 | + catch e |
| 614 | + if e isa ErrorException && |
| 615 | + startswith(e.msg, "perf_event_open error : ") |
| 616 | + @warn "Perf is disabled" # Really we only want to do this if we defaulted to running with perf, otherwise we should just throw. |
| 617 | + # Given we now more accurately determine if perf is available can we do away with this hack? |
| 618 | + else |
| 619 | + rethrow() |
608 | 620 | end
|
| 621 | + end |
609 | 622 |
|
610 |
| - if !isnothing(__linux_perf_bench) |
611 |
| - try |
612 |
| - $(setup) |
613 |
| - BenchmarkTools.LinuxPerf.enable!(__linux_perf_bench) |
614 |
| - # We'll just run it one time. |
615 |
| - __return_val_2 = $(invocation) |
616 |
| - BenchmarkTools.LinuxPerf.disable!(__linux_perf_bench) |
617 |
| - # trick the compiler not to eliminate the code |
618 |
| - if rand() < 0 |
619 |
| - __linux_perf_stats = __return_val_2 |
620 |
| - else |
621 |
| - __linux_perf_stats = BenchmarkTools.LinuxPerf.Stats( |
622 |
| - __linux_perf_bench |
623 |
| - ) |
624 |
| - end |
625 |
| - catch |
626 |
| - rethrow() |
627 |
| - finally |
628 |
| - close(__linux_perf_bench) |
629 |
| - $(teardown) |
| 623 | + if !isnothing(__linux_perf_bench) |
| 624 | + $(setup) |
| 625 | + try |
| 626 | + $LinuxPerf.enable!(__linux_perf_bench) |
| 627 | + # We'll just run it one time. |
| 628 | + __return_val_2 = $(invocation) |
| 629 | + $LinuxPerf.disable!(__linux_perf_bench) |
| 630 | + # trick the compiler not to eliminate the code |
| 631 | + if rand() < 0 |
| 632 | + __linux_perf_stats = __return_val_2 |
| 633 | + else |
| 634 | + __linux_perf_stats = $LinuxPerf.Stats(__linux_perf_bench) |
630 | 635 | end
|
| 636 | + return __linux_perf_stats |
| 637 | + catch |
| 638 | + rethrow() |
| 639 | + finally |
| 640 | + close(__linux_perf_bench) |
| 641 | + $(teardown) |
631 | 642 | end
|
632 |
| - else |
633 |
| - __return_val_2 = nothing |
634 |
| - __linux_perf_stats = nothing |
635 | 643 | end
|
636 |
| - return BenchmarkTools.TrialContents( |
637 |
| - __time, |
638 |
| - __gctime, |
639 |
| - __memory, |
640 |
| - __allocs, |
641 |
| - __return_val, |
642 |
| - __return_val_2, |
643 |
| - __linux_perf_stats, |
644 |
| - ) |
645 | 644 | end
|
646 |
| - $BenchmarkTools.Benchmark($(samplefunc), $(quote_vals), $(params)) |
| 645 | + $BenchmarkTools.Benchmark( |
| 646 | + $(samplefunc), $(linux_perf_func), $(quote_vals), $(params) |
| 647 | + ) |
647 | 648 | end,
|
648 | 649 | )
|
649 | 650 | end
|
|
0 commit comments