1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
(* TEST
modules = "polling.c";
compare_programs = "false";
arch64;
native;
*)
(* This set of tests examine poll insertion behaviour. We do this by requesting
and checking the number of minor collections at various points to determine
whether a poll was correctly added. There are some subtleties because
[caml_empty_minor_heap] will not increment the minor_collections stat if
nothing has been allocated on the minor heap, so we sometimes need to
add an allocation before we call [request_minor_gc]. The [minor_gcs]
function returns the number of minor collections so far without allocating.
ignore(Sys.opaque_identity(ref 41)) is used wherever we want to do an
allocation in order to use some minor heap so the minor collections stat is
incremented.
ignore(Sys.opaque_identity(ref 42)) is used wherever we want an allocation
for the purposes of testing whether a poll would be elided or not.
*)
external request_minor_gc : unit -> unit = "request_minor_gc"
external minor_gcs : unit -> int = "minor_gcs"
(* This function tests that polls are added to loops *)
let polls_added_to_loops () =
let minors_before = minor_gcs () in
request_minor_gc ();
for a = 0 to 1 do
ignore (Sys.opaque_identity 42)
done;
let minors_now = minor_gcs () in
assert (minors_before < minors_now)
(* This function should have no prologue poll but will have
one in the loop. *)
let func_with_added_poll_because_loop () =
(* We do two loop iterations so that the poll is triggered whether
in poll-at-top or poll-at-bottom mode. *)
for a = 0 to Sys.opaque_identity(1) do
ignore (Sys.opaque_identity 42)
done
[@@inline never]
let func_with_no_prologue_poll () =
(* this function does not have indirect or 'forward' tail call nor
does it call a synthesised function with suppressed polls. *)
ignore(Sys.opaque_identity(minor_gcs ()))
[@@inline never]
let prologue_polls_in_functions () =
ignore(Sys.opaque_identity(ref 41));
let minors_before = minor_gcs () in
request_minor_gc ();
func_with_added_poll_because_loop ();
let minors_now = minor_gcs () in
assert (minors_before < minors_now);
ignore(Sys.opaque_identity(ref 41));
let minors_before = minor_gcs () in
request_minor_gc ();
func_with_no_prologue_poll ();
let minors_now = minor_gcs () in
assert (minors_before = minors_now)
(* These next functions test that polls are not added to functions that
unconditionally allocate.
[allocating_func] allocates unconditionally
[allocating_func_if] allocates unconditionally but does so
on two separate branches *)
let allocating_func minors_before =
let minors_now = minor_gcs () in
assert (minors_before = minors_now);
(* No poll yet *)
ignore (Sys.opaque_identity (ref 42));
let minors_now2 = minor_gcs () in
assert (minors_before + 1 = minors_now2);
(* Polled at alloc *)
[@@inline never]
let allocating_func_if minors_before =
let minors_now = minor_gcs () in
assert (minors_before = minors_now);
(* No poll yet *)
if minors_before > 0 then ignore (Sys.opaque_identity (ref 42))
else ignore (Sys.opaque_identity (ref 42));
let minors_now2 = minor_gcs () in
assert (minors_before < minors_now2);
(* Polled at alloc *)
[@@inline never]
let allocating_func_nested_ifs minors_before =
let minors_now = minor_gcs () in
assert (minors_before = minors_now);
(* No poll yet *)
if Sys.opaque_identity(minors_before) > 0 then
if Sys.opaque_identity(minors_before) > 1 then
ignore (Sys.opaque_identity (ref 42))
else
ignore (Sys.opaque_identity (ref 42))
else
if Sys.opaque_identity(minors_before) < 5 then
ignore (Sys.opaque_identity (ref 42))
else
ignore (Sys.opaque_identity (ref 42));
let minors_now2 = minor_gcs () in
assert (minors_before < minors_now2);
(* Polled at alloc *)
[@@inline never]
let allocating_func_match minors_before =
let minors_now = minor_gcs () in
assert (minors_before = minors_now);
(* No poll yet *)
match minors_before with
| 0 -> ignore (Sys.opaque_identity (ref 42))
| _ -> ignore (Sys.opaque_identity (ref 42));
let minors_now2 = minor_gcs () in
assert (minors_before < minors_now2);
(* Polled at alloc *)
[@@inline never]
let polls_not_added_unconditionally_allocating_functions () =
let minors_before = minor_gcs () in
ignore(Sys.opaque_identity(ref 41));
request_minor_gc ();
allocating_func minors_before;
let minors_before = minor_gcs () in
ignore(Sys.opaque_identity(ref 41));
request_minor_gc ();
allocating_func_if minors_before;
let minors_before = minor_gcs () in
ignore(Sys.opaque_identity(ref 41));
request_minor_gc ();
allocating_func_nested_ifs minors_before;
let minors_before = minor_gcs () in
ignore(Sys.opaque_identity(ref 41));
request_minor_gc ();
allocating_func_match minors_before
(* This function tests that polls are not added to the back edge of
where loop bodies allocate unconditionally *)
let polls_not_added_to_allocating_loops () =
let current_minors = ref (minor_gcs ()) in
request_minor_gc ();
for a = 0 to 1 do
(* Since the loop body allocates there should be no poll points *)
let minors_now = minor_gcs () in
assert(minors_now = !current_minors);
ignore(Sys.opaque_identity(ref 42));
let minors_now2 = minor_gcs () in
assert(minors_now < minors_now2);
current_minors := minors_now2;
ignore(Sys.opaque_identity(ref 41));
request_minor_gc ()
done
(* this next set of functions tests that self tail recursive functions
have polls added correctly *)
let rec self_rec_func n =
match n with
| 0 -> 0
| _ ->
begin
let n1 = Sys.opaque_identity(n-1) in
(self_rec_func[@tailcall]) n1
end
let polls_added_to_self_recursive_functions () =
let minors_before = minor_gcs () in
request_minor_gc ();
ignore(self_rec_func 2);
let minors_after = minor_gcs () in
(* should be at least one minor gc from polls in self_rec_func *)
assert(minors_before < minors_after)
(* this pair of mutually recursive functions is to test that a poll is
correctly placed in the first one compiled *)
let rec mut_rec_func_even d =
match d with
| 0 -> 0
| _ -> mut_rec_func_odd (d-1)
and mut_rec_func_odd d =
mut_rec_func_even (d-1)
and mut_rec_func d =
match d with
| n when n mod 2 == 0
-> mut_rec_func_even n
| n -> mut_rec_func_odd n
let polls_added_to_mutually_recursive_functions () =
let minors_before = minor_gcs () in
request_minor_gc ();
ignore(mut_rec_func 3);
let minors_after = minor_gcs () in
(* should be at least one minor gc from polls in mut_rec_func *)
assert(minors_before < minors_after)
(* this is to test that indirect tail calls (which might result in a self
call) have polls inserted in them.
These correspond to Itailcall_ind at Mach *)
let do_indirect_tail_call f n =
f (n-1)
[@@inline never]
let polls_added_to_indirect_tail_calls () =
let f = fun n -> n+1 in
let minors_before = minor_gcs () in
request_minor_gc ();
ignore(do_indirect_tail_call f 3);
let minors_after = minor_gcs () in
(* should be at one minor gc from the poll in do_indirect_tail_call *)
assert(minors_before < minors_after)
(* this is to test that indirect non-tail calls do not have a poll placed
in them. These correspond to Icall_ind at Mach *)
let do_indirect_call f n =
n * f (n-1)
[@@inline never]
let polls_not_added_to_indirect_calls () =
let f = fun n -> n+1 in
let minors_before = minor_gcs () in
request_minor_gc ();
ignore(do_indirect_call f 3);
let minors_after = minor_gcs () in
(* should be at one minor gc from the poll in do_indirect_tail_call *)
assert(minors_before = minors_after)
(* this set of functions tests that we don't poll for immediate
(non-tail) calls. These correspond to Icall_imm at Mach *)
let call_func1 n =
Sys.opaque_identity(n-1)
[@@inline never]
let call_func2 n =
n * (call_func1 (Sys.opaque_identity(n+1)))
[@@inline never]
let polls_not_added_to_immediate_calls () =
let minors_before = minor_gcs () in
request_minor_gc ();
ignore(call_func1 100);
let minors_after = minor_gcs () in
(* should be no minor collections *)
assert(minors_before = minors_after)
let[@inline never][@local never] app minors_before f x y =
let minors_after_prologue = minor_gcs () in
assert(minors_before < minors_after_prologue);
request_minor_gc ();
f x y
let polls_not_added_in_caml_apply () =
let minors_before = minor_gcs() in
request_minor_gc();
ignore(Sys.opaque_identity(app minors_before (fun x y -> x * y) 5 4));
let minors_after = minor_gcs() in
assert(minors_before < minors_after)
let () =
ignore(Sys.opaque_identity(ref 41));
polls_added_to_loops (); (* relies on there being some minor heap usage *)
ignore(Sys.opaque_identity(ref 41));
prologue_polls_in_functions ();
ignore(Sys.opaque_identity(ref 41));
polls_added_to_self_recursive_functions ();
ignore(Sys.opaque_identity(ref 41));
polls_added_to_mutually_recursive_functions ();
ignore(Sys.opaque_identity(ref 41));
polls_added_to_indirect_tail_calls ();
ignore(Sys.opaque_identity(ref 41));
polls_not_added_to_indirect_calls ();
ignore(Sys.opaque_identity(ref 41));
polls_not_added_to_immediate_calls ();
ignore(Sys.opaque_identity(ref 41));
polls_not_added_unconditionally_allocating_functions ();
ignore(Sys.opaque_identity(ref 41));
polls_not_added_to_allocating_loops ();
ignore(Sys.opaque_identity(ref 41));
polls_not_added_in_caml_apply ()
|