First attempt at having a grammar runtime.
[SXSI/xpathcomp.git] / utils / alarm.ml
index 8874756..d1b7b30 100644 (file)
@@ -2,7 +2,7 @@ let read_procmem pid =
   let cin = open_in (Printf.sprintf "/proc/%i/status" pid) in
   let matchline s =
     try
-      Scanf.sscanf s " VmHWM: %i kB" (fun i -> Some i)
+      Scanf.sscanf s " VmRSS: %i kB" (fun i -> Some i)
     with
       | _ -> None
   in
@@ -12,6 +12,7 @@ let read_procmem pid =
       | None -> loop ()
   in
   let s = try loop() with _ -> -1 in
+       Printf.eprintf "Memory: %i\n%!" s;
   close_in cin;
   s
 ;;
@@ -21,10 +22,14 @@ let rec monitor pid timeout mem =
   if p == 0 then
     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
+    if (Unix.gettimeofday() > timeout)
+    then let () = Printf.eprintf "Timeout reached, killing child process\n%!" in
+        Unix.kill pid Sys.sigkill
+    else if !max_mem >= mem
+    then let () = Printf.eprintf "Memory limit reached, killing child process\n%!" in
+        Unix.kill pid Sys.sigkill
     else
-      let () = Unix.sleep 1 in
+      let () = Unix.sleep 1 in 
       monitor pid timeout mem
 ;;