Skip to content

Commit 58f39cc

Browse files
committed
CP-52911: benchmark Timeslice.Runtime functions
Adds a benchmarking test for functions used in `Timeslice.Runtime` module. This functions are meant to have low overhead and should have a minimum of memory allocations. Signed-off-by: Gabriel Buica <[email protected]>
1 parent e80b636 commit 58f39cc

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

ocaml/tests/bench/bench_timeslice.ml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
open Bechamel
2+
3+
let test_maybe_thread_yield () =
4+
Sys.opaque_identity
5+
(Xapi_timeslice.Timeslice.Runtime.maybe_thread_yield
6+
~global_slice_period:10_000_000
7+
)
8+
9+
let test_sched_global_slice () =
10+
Sys.opaque_identity
11+
(Xapi_timeslice.Timeslice.Runtime.sched_global_slice
12+
~global_slice_period:10_000_000
13+
)
14+
15+
let test_tgroups_on ~name f =
16+
let allocate () =
17+
let () = Atomic.set Tgroup.Cgroup.cgroup_dir (Some "") in
18+
let g_cli = Some "cli" |> Tgroup.of_req_originator |> Option.get in
19+
let tg_cli = Tgroup.ThreadGroup.create ~tgroup:g_cli in
20+
let () = tg_cli.thread_count <- Atomic.make 10 in
21+
let tg_authenticated_root =
22+
Tgroup.ThreadGroup.create ~tgroup:Tgroup.Group.authenticated_root
23+
in
24+
let () = tg_authenticated_root.thread_count <- Atomic.make 5 in
25+
Tgroup.ThreadGroup.(add tg_cli ; add tg_authenticated_root)
26+
in
27+
let free = Tgroup.ThreadGroup.destroy in
28+
Test.make_with_resource ~name ~allocate ~free Test.uniq f
29+
30+
let test_with_thread_classified ~name f =
31+
let allocate () =
32+
let () = Atomic.set Tgroup.Cgroup.cgroup_dir (Some "") in
33+
let g_cli = Some "cli" |> Tgroup.of_req_originator |> Option.get in
34+
let tg_cli = Tgroup.ThreadGroup.create ~tgroup:g_cli in
35+
let () = tg_cli.thread_count <- Atomic.make 10 in
36+
let tg_authenticated_root =
37+
Tgroup.ThreadGroup.create ~tgroup:Tgroup.Group.authenticated_root
38+
in
39+
let () = tg_authenticated_root.thread_count <- Atomic.make 1 in
40+
Xapi_stdext_threads.Threadext.ThreadRuntimeContext.(
41+
let thread_ctx = get () in
42+
update
43+
(fun thread_ctx ->
44+
{thread_ctx with tgroup= Tgroup.Group.authenticated_root}
45+
)
46+
thread_ctx
47+
) ;
48+
Tgroup.ThreadGroup.(add tg_cli ; add tg_authenticated_root)
49+
in
50+
let free () =
51+
Tgroup.ThreadGroup.destroy () ;
52+
Xapi_stdext_threads.Threadext.ThreadRuntimeContext.remove ()
53+
in
54+
Test.make_with_resource ~name ~allocate ~free Test.uniq f
55+
56+
let benchmarks =
57+
Test.make_grouped ~name:"timeslice"
58+
[
59+
test_with_thread_classified ~name:"maybe_thread_yield"
60+
(Staged.stage test_maybe_thread_yield)
61+
; test_tgroups_on ~name:"sched_global_slice"
62+
(Staged.stage test_sched_global_slice)
63+
]
64+
65+
let () = Bechamel_simple_cli.cli benchmarks

ocaml/tests/bench/dune

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
(executables
2-
(names bench_tracing bench_uuid bench_throttle2 bench_cached_reads)
3-
(libraries tracing bechamel bechamel-notty notty.unix tracing_export threads.posix fmt notty uuid xapi_aux tests_common log xapi_internal)
2+
(names
3+
bench_timeslice
4+
bench_tracing
5+
bench_uuid
6+
bench_throttle2
7+
bench_cached_reads)
8+
(libraries
9+
mtime
10+
tracing
11+
bechamel
12+
bechamel-notty
13+
notty.unix
14+
tgroup
15+
tracing_export
16+
threads.posix
17+
fmt
18+
notty
19+
uuid
20+
xapi_aux
21+
tests_common
22+
log
23+
xapi_internal
24+
xapi_timeslice
25+
xapi-stdext-threads
26+
)
427
)

0 commit comments

Comments
 (0)