Merge souce-cleanup branch into trunk
[SXSI/xpathcomp.git] / utils / alarm.ml
index a12936c..8874756 100644 (file)
@@ -1,4 +1,3 @@
-
 let read_procmem pid =
   let cin = open_in (Printf.sprintf "/proc/%i/status" pid) in
   let matchline s =
@@ -16,11 +15,13 @@ let read_procmem pid =
   close_in cin;
   s
 ;;
-
+let max_mem = ref 0
 let rec monitor pid timeout mem =
   let p, s = Unix.waitpid [ Unix.WNOHANG ] pid in
   if p == 0 then
-    if (Unix.gettimeofday() > timeout || (read_procmem pid) >= mem)
+    let current_mem = read_procmem pid in
+    if current_mem >= !max_mem then max_mem := current_mem;
+    if (Unix.gettimeofday() > timeout || current_mem >= mem)
     then Unix.kill pid Sys.sigkill
     else
       let () = Unix.sleep 1 in
@@ -35,7 +36,7 @@ let run args timeout mem =
   else monitor pid timeout mem
 ;;
 
-let () = 
+let () =
   if Array.length Sys.argv < 4 then exit 1
   else
     try
@@ -43,6 +44,7 @@ let () =
       let mem = int_of_string Sys.argv.(2) in
       let command = Array.sub Sys.argv 3 ((Array.length Sys.argv) - 3) in
       run command timeout mem;
+      Printf.printf "Child process used %i kB of memory\n%!" !max_mem;
       exit 0
     with
        _ -> exit 2