|
| 1 | +(* |
| 2 | + * Copyright (C) 2024-2025 Semgrep, Inc. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the LICENSE file |
| 5 | + * in the root directory of this source tree. |
| 6 | + *) |
| 7 | + |
| 8 | +let alarm : Gc.alarm option ref = ref None |
| 9 | + |
| 10 | +type config = { |
| 11 | + min_space_overhead : int; |
| 12 | + max_space_overhead : int; |
| 13 | + heap_start_worrying_mb : int; |
| 14 | + heap_really_worry_mb : int; |
| 15 | +} |
| 16 | + |
| 17 | +let space_overhead_of_heap_size config heap_size_mb = |
| 18 | + if config.heap_start_worrying_mb = config.heap_really_worry_mb then begin |
| 19 | + (* Avoid a division by zero *) |
| 20 | + if heap_size_mb > config.heap_start_worrying_mb then |
| 21 | + config.min_space_overhead |
| 22 | + else config.max_space_overhead |
| 23 | + end |
| 24 | + else |
| 25 | + (* Linear interpolation *) |
| 26 | + let heap_size_mb = Float.of_int heap_size_mb in |
| 27 | + let min_space_overhead = Float.of_int config.min_space_overhead in |
| 28 | + let max_space_overhead = Float.of_int config.max_space_overhead in |
| 29 | + let heap_start_worrying_mb = Float.of_int config.heap_start_worrying_mb in |
| 30 | + let heap_really_worry_mb = Float.of_int config.heap_really_worry_mb in |
| 31 | + let rate = |
| 32 | + (min_space_overhead -. max_space_overhead) |
| 33 | + /. (heap_really_worry_mb -. heap_start_worrying_mb) |
| 34 | + in |
| 35 | + let intercept = max_space_overhead -. (rate *. heap_start_worrying_mb) in |
| 36 | + let space_overhead_unbounded = (rate *. heap_size_mb) +. intercept in |
| 37 | + let space_overhead = |
| 38 | + max min_space_overhead (min space_overhead_unbounded max_space_overhead) |
| 39 | + in |
| 40 | + space_overhead |> Float.round |> Float.to_int |
| 41 | + |
| 42 | +let handle_alarm config () = |
| 43 | + let { Gc.heap_words; _ } = Gc.quick_stat () in |
| 44 | + let word_size_bytes = Sys.word_size / 8 in |
| 45 | + let heap_size_mb = heap_words * word_size_bytes / (1_024 * 1_024) in |
| 46 | + let space_overhead = space_overhead_of_heap_size config heap_size_mb in |
| 47 | + Gc.set { (Gc.get ()) with space_overhead } |
| 48 | + |
| 49 | +let setup_dynamic_tuning config = |
| 50 | + if Option.is_some !alarm then |
| 51 | + failwith "Gc_.setup_dynamic_tuning called multiple times."; |
| 52 | + (* Can be the same, but that would be kind of stupid *) |
| 53 | + if config.min_space_overhead > config.max_space_overhead then |
| 54 | + failwith |
| 55 | + "Gc_.setup_dynamic_tuning called with min_space_overhead greater than \ |
| 56 | + max_space_overhead"; |
| 57 | + (* Can be equal if you want a sharp cutover from max space overhead to min! *) |
| 58 | + if config.heap_start_worrying_mb > config.heap_really_worry_mb then |
| 59 | + failwith |
| 60 | + "Gc_.setup_dynamic_tuning called with nonsensical heap size arguments"; |
| 61 | + (* Call this immediately so that we set space_overhead as desired without |
| 62 | + * first having to wait for a major collection to complete. *) |
| 63 | + handle_alarm config (); |
| 64 | + alarm := Some (Gc.create_alarm (handle_alarm config)) |
| 65 | + |
| 66 | +let stop_dynamic_tuning () = |
| 67 | + match !alarm with |
| 68 | + | Some a -> |
| 69 | + Gc.delete_alarm a; |
| 70 | + alarm := None |
| 71 | + | None -> |
| 72 | + failwith |
| 73 | + "Gc_.stop_dynamic_tuning called when dynamic tuning was already \ |
| 74 | + stopped." |
| 75 | + |
| 76 | +module ForTestingDoNotUse = struct |
| 77 | + let space_overhead_of_heap_size = space_overhead_of_heap_size |
| 78 | +end |
0 commit comments