<commit_before>open Core.Std
open Core_extended
module Json = Yojson.Safe

module Make (PI : Process_info.S) = struct
  type result = {
    runtime : Time_ns.Span.t;
    peak_memory : Int64.t;
    status : [`Success | `Killed_memory | `Killed_runtime | `Exit_or_signal of Unix.Exit_or_signal.error ]
  }

  let ticks_per_second = 100
  let sleep_time = Float.(/) 1.0 (Float.of_int ticks_per_second)

  let peak_memory = ref Int64.min_value
  let start_time = ref Time_ns.epoch
  let end_time = ref Time_ns.epoch

  let check_limits (pid: Pid.t) (memory: int option) (time: Time_ns.Span.t option) : [`Ok | `Memory | `Runtime] =
    match PI.of_pid pid with
    | Some info ->
      let passed_memory = Option.value_map memory ~default:`Ok ~f:(fun limit ->
          if info.PI.memory_size <= (Int64.of_int limit) then `Ok else `Memory)
      in
      if passed_memory = `Ok then
        let passed_time = Option.value_map time ~default:`Ok ~f:(fun limit ->
            if Time_ns.diff (Time_ns.now ()) !start_time <= limit then `Ok else `Runtime)
        in
        passed_time
      else passed_memory
    | None -> failwith "ERROR: Getting process info failed.\n"

  let save_stats (pid: Pid.t) : unit =
    match PI.of_pid pid with
    | Some info ->
      if Int64.(>) info.PI.memory_size !peak_memory then
        peak_memory := info.PI.memory_size
    | None -> failwith "ERROR: Getting process info failed.\n"

  let print_stats result =
    begin match result.status with
      | `Success -> printf "Child exited with return code 0.\n";
      | `Exit_or_signal (`Exit_non_zero code) -> printf "Child exited with return code %d.\n" code
      | `Exit_or_signal (`Signal s) -> printf "Child received signal %s.\n" (Signal.to_string s)
      | `Killed_memory -> printf "Child killed for violating memory limit."
      | `Killed_runtime -> printf "Child killed for violating runtime limit."
    end;
    if result.peak_memory <> Int64.min_value then
      printf "Peak memory: %s Mb\n"
        (Int64.(/) !peak_memory (Int64.of_int 1000000) |> Int64.to_string);
    printf "Runtime: %s" (Time_ns.Span.to_short_string result.runtime)

  let print_machine result stdout_fn stderr_fn =
    let read_file fn = In_channel.with_file fn ~f:In_channel.input_all in
    `Assoc [
      "runtime", `Float (Time_ns.Span.to_sec result.runtime);
      "peak_memory", `Intlit (Int64.(/) !peak_memory (Int64.of_int 1000000) |> Int64.to_string);
      "status", `String begin match result.status with
        | `Success -> "success"
        | `Killed_memory -> "killed_memory"
        | `Killed_runtime -> "killed_runtime"
        | `Exit_or_signal _ -> "exited"
      end;
      "stdout", `String (read_file stdout_fn);
      "stderr", `String (read_file stderr_fn);
    ] |> Json.pretty_to_channel ~std:true Out_channel.stdout
<commit_msg>Clean up output from timeout.ml.<commit_after>open Core.Std
open Core_extended
module Json = Yojson.Safe

module Make (PI : Process_info.S) = struct
  type result = {
    runtime : Time_ns.Span.t;
    peak_memory : Int64.t;
    status : [`Success | `Killed_memory | `Killed_runtime | `Exit_or_signal of Unix.Exit_or_signal.error ]
  }

  let ticks_per_second = 100
  let sleep_time = Float.(/) 1.0 (Float.of_int ticks_per_second)

  let peak_memory = ref Int64.min_value
  let start_time = ref Time_ns.epoch
  let end_time = ref Time_ns.epoch

  let check_limits (pid: Pid.t) (memory: int option) (time: Time_ns.Span.t option) : [`Ok | `Memory | `Runtime] =
    match PI.of_pid pid with
    | Some info ->
      let passed_memory = Option.value_map memory ~default:`Ok ~f:(fun limit ->
          if info.PI.memory_size <= (Int64.of_int limit) then `Ok else `Memory)
      in
      if passed_memory = `Ok then
        let passed_time = Option.value_map time ~default:`Ok ~f:(fun limit ->
            if Time_ns.diff (Time_ns.now ()) !start_time <= limit then `Ok else `Runtime)
        in
        passed_time
      else passed_memory
    | None -> failwith "ERROR: Getting process info failed.\n"

  let save_stats (pid: Pid.t) : unit =
    match PI.of_pid pid with
    | Some info ->
      if Int64.(>) info.PI.memory_size !peak_memory then
        peak_memory := info.PI.memory_size
    | None -> failwith "ERROR: Getting process info failed.\n"

  let print_stats result =
    begin match result.status with
      | `Success -> printf "Child exited with return code 0.\n";
      | `Exit_or_signal (`Exit_non_zero code) -> printf "Child exited with return code %d.\n" code
      | `Exit_or_signal (`Signal s) -> printf "Child received signal %s.\n" (Signal.to_string s)
      | `Killed_memory -> printf "Child killed for violating memory limit.\n"
      | `Killed_runtime -> printf "Child killed for violating runtime limit.\n"
    end;
    if result.peak_memory <> Int64.min_value then
      printf "Peak memory: %s Mb\n"
        (Int64.(/) !peak_memory (Int64.of_int 1000000) |> Int64.to_string);
    printf "Runtime: %s" (Time_ns.Span.to_short_string result.runtime)

  let print_machine result stdout_fn stderr_fn =
    let read_file fn = In_channel.with_file fn ~f:In_channel.input_all in
    `Assoc [
      "runtime", `Float (Time_ns.Span.to_sec result.runtime);
      "peak_memory", `Intlit (Int64.(/) !peak_memory (Int64.of_int 1000000) |> Int64.to_string);
      "status", `String begin match result.status with
        | `Success -> "success"
        | `Killed_memory -> "killed_memory"
        | `Killed_runtime -> "killed_runtime"
        | `Exit_or_signal _ -> "exited"
      end;
      "stdout", `String (read_file stdout_fn);
      "stderr", `String (read_file stderr_fn);
    ] |> Json.pretty_to_channel ~std:true Out_channel.stdout

{'fraction_non_alphanumeric': 0.1159062885326757, 'fraction_numerical': 0.014796547472256474, 'mean_word_length': 3.0240963855421685, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4296321', 'n_tokens_mistral': 1975, 'n_tokens_neox': 1858, 'n_words': 522}