2 let cin = open_in (Printf.sprintf "/proc/%i/status" pid) in
5 Scanf.sscanf s " VmRSS: %i kB" (fun i -> Some i)
10 match matchline (input_line cin) with
14 let s = try loop() with _ -> -1 in
15 Printf.eprintf "Memory: %i\n%!" s;
20 let rec monitor pid timeout mem =
21 let p, s = Unix.waitpid [ Unix.WNOHANG ] pid in
23 let current_mem = read_procmem pid in
24 if current_mem >= !max_mem then max_mem := current_mem;
25 if (Unix.gettimeofday() > timeout)
26 then let () = Printf.eprintf "Timeout reached, killing child process\n%!" in
27 Unix.kill pid Sys.sigkill
28 else if !max_mem >= mem
29 then let () = Printf.eprintf "Memory limit reached, killing child process\n%!" in
30 Unix.kill pid Sys.sigkill
32 let () = Unix.sleep 1 in
33 monitor pid timeout mem
37 let run args timeout mem =
38 let pid = Unix.fork () in
40 Unix.execvp args.(0) args
41 else monitor pid timeout mem
45 if Array.length Sys.argv < 4 then exit 1
48 let timeout = Unix.gettimeofday () +. float_of_string Sys.argv.(1) in
49 let mem = int_of_string Sys.argv.(2) in
50 let command = Array.sub Sys.argv 3 ((Array.length Sys.argv) - 3) in
51 run command timeout mem;
52 Printf.printf "Child process used %i kB of memory\n%!" !max_mem;