@@ -107,8 +107,8 @@ function _run(b::Benchmark, p::Parameters; verbose=false, pad="", kwargs...)
107
107
trial = Trial (params)
108
108
params. gcsample && gcscrub ()
109
109
s = b. samplefunc (b. quote_vals, params)
110
- push! (trial, s[1 : ( end - 1 ) ]. .. )
111
- return_val = s[ end ]
110
+ push! (trial, s[1 : end - 1 ]. .. )
111
+ return_val = s. __return_val
112
112
iters = 2
113
113
while (Base. time () - start_time) < params. seconds && iters ≤ params. samples
114
114
params. gcsample && gcscrub ()
@@ -533,50 +533,66 @@ function generate_benchmark_definition(
533
533
core_body = :($ (core); $ (returns))
534
534
end
535
535
@static if isdefined (Base, :donotdelete )
536
- invocation = :(
537
- let x = $ invocation
538
- Base. donotdelete (x)
539
- x
540
- end
541
- )
536
+ invocation = :(let x = $ invocation
537
+ Base. donotdelete (x)
538
+ x
539
+ end )
542
540
end
543
- return Core. eval (
544
- eval_module,
545
- quote
546
- @noinline $ (signature_def) = begin
547
- $ (core_body)
541
+ experimental_enable_linux_perf = true # TODO : take this as input from the user
542
+ # TODO : let the user actually provide these options.
543
+ linux_perf_opts = LinuxPerf. parse_pstats_options ([])
544
+ return Core. eval (eval_module, quote
545
+ @noinline $ (signature_def) = begin $ (core_body) end
546
+ @noinline function $ (samplefunc)($ (Expr (:tuple , quote_vars... )), __params:: $BenchmarkTools.Parameters )
547
+ $ (setup)
548
+ __evals = __params. evals
549
+ __gc_start = Base. gc_num ()
550
+ __start_time = time_ns ()
551
+ __return_val = $ (invocation)
552
+ for __iter in 2 : __evals
553
+ $ (invocation)
548
554
end
549
- @noinline function $ (samplefunc)(
550
- $ (Expr (:tuple , quote_vars... )), __params:: $BenchmarkTools.Parameters
551
- )
552
- $ (setup)
553
- __evals = __params. evals
554
- __gc_start = Base. gc_num ()
555
- __start_time = time_ns ()
556
- __return_val = $ (invocation)
557
- for __iter in 2 : __evals
558
- $ (invocation)
559
- end
560
- __sample_time = time_ns () - __start_time
561
- __gcdiff = Base. GC_Diff (Base. gc_num (), __gc_start)
562
- $ (teardown)
563
- __time = max ((__sample_time / __evals) - __params. overhead, 0.001 )
564
- __gctime = max ((__gcdiff. total_time / __evals) - __params. overhead, 0.0 )
565
- __memory = Int (Base. fld (__gcdiff. allocd, __evals))
566
- __allocs = Int (
567
- Base. fld (
568
- __gcdiff. malloc +
569
- __gcdiff. realloc +
570
- __gcdiff. poolalloc +
571
- __gcdiff. bigalloc,
572
- __evals,
573
- ),
555
+ __sample_time = time_ns () - __start_time
556
+ __gcdiff = Base. GC_Diff (Base. gc_num (), __gc_start)
557
+ $ (teardown)
558
+ __time = max ((__sample_time / __evals) - __params. overhead, 0.001 )
559
+ __gctime = max ((__gcdiff. total_time / __evals) - __params. overhead, 0.0 )
560
+ __memory = Int (Base. fld (__gcdiff. allocd, __evals))
561
+ __allocs = Int (Base. fld (__gcdiff. malloc + __gcdiff. realloc +
562
+ __gcdiff. poolalloc + __gcdiff. bigalloc,
563
+ __evals))
564
+ if $ (experimental_enable_linux_perf)
565
+ # Based on https://github.com/JuliaPerf/LinuxPerf.jl/blob/a7fee0ff261a5b5ce7a903af7b38d1b5c27dd931/src/LinuxPerf.jl#L1043-L1061
566
+ __linux_perf_groups = LinuxPerf. set_default_spaces (
567
+ $ (linux_perf_opts. events),
568
+ $ (linux_perf_opts. spaces),
569
+ )
570
+ __linux_perf_bench = LinuxPerf. make_bench_threaded (
571
+ __linux_perf_groups;
572
+ threads = $ (linux_perf_opts. threads),
574
573
)
575
- return __time, __gctime, __memory, __allocs, __return_val
574
+ LinuxPerf. enable! (__linux_perf_bench)
575
+ # We'll just run it one time.
576
+ __return_val_2 = $ (invocation)
577
+ LinuxPerf. disable! (__linux_perf_bench)
578
+ # trick the compiler not to eliminate the code
579
+ if rand () < 0
580
+ __linux_perf_stats = __return_val_2
581
+ else
582
+ __linux_perf_stats = LinuxPerf. Stats (__linux_perf_bench)
583
+ end
576
584
end
577
- $ BenchmarkTools. Benchmark ($ (samplefunc), $ (quote_vals), $ (params))
578
- end ,
579
- )
585
+ return (;
586
+ __time,
587
+ __gctime,
588
+ __memory,
589
+ __allocs,
590
+ __return_val,
591
+ __linux_perf_stats,
592
+ )
593
+ end
594
+ $ BenchmarkTools. Benchmark ($ (samplefunc), $ (quote_vals), $ (params))
595
+ end )
580
596
end
581
597
582
598
# #####################
0 commit comments