Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 117 lines (94 sloc) 3.477 kb
fccc685 Initial open-source release
MLstate authored
1 (*
2 Copyright © 2011 MLstate
3
4 This file is part of OPA.
5
6 OPA is free software: you can redistribute it and/or modify it under the
7 terms of the GNU Affero General Public License, version 3, as published by
8 the Free Software Foundation.
9
10 OPA is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
13 more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with OPA. If not, see <http://www.gnu.org/licenses/>.
17 *)
18
19 type options = {
20 opt_memory_limit : float;
21 opt_fd_limit: int;
22 opt_freq: float;
23 }
24
25 type t = {
26 memory_limit : float;
27 fd_limit: int;
28 freq: Time.t;
29 }
30
31
32 let name = "WatchDog"
33
34 let version = "0.0"
35
36 let default_options =
37 {
38 opt_memory_limit= 1000.;
39 opt_fd_limit= 1024;
40 opt_freq=5.;
41 }
42
43 let spec_args _name = []
44
45 let make _name opts _sched =
46 {
47 memory_limit=opts.opt_memory_limit;
48 fd_limit=opts.opt_fd_limit;
49 freq=Time.seconds_float opts.opt_freq;
50 }
51
52 let get_ports _t _sched = ["",`None]
53
54 let get_description _t _sched = `Watchdog
55
56 let memory_consumption () =
57 (* let minor_words,promoted_words,major_words = Gc.counters () in *)
58 (* let words = (minor_words +. major_words -. promoted_words) in *)
59 let stat = Gc.stat () in
60 let words = float_of_int(stat.Gc.heap_words + stat.Gc.stack_size) in
61 (* let words = float_of_string(Sys.command ("ps --no-headers -o size "^(string_of_int(Unix.getpid ())))) in *)
62 words /.(128.*.1024.)
63 (* 1/131072 = 8 /(1024*1024) (8: word size on 64) *)
64 (* let fd = Unix.openfile "/proc/self/stat" [Unix.O_RDONLY] 0o640 in *)
65 (* let conn = Scheduler.make_connection sched (Scheduler.File fd) in *)
66 (* let *)
67
68
69 let fd_consumption sched = Scheduler.nb_of_connection sched
70
71
72 let run t sched =
73 Logger.info "[WatchDog] start";
74 let too_much () =
75 let mc = memory_consumption () in
76 let fdc = fd_consumption sched in
77 Logger.info "[WatchDog] Check resource consumption : memory %d/%d / file_desc %d/%d" (int_of_float mc) (int_of_float t.memory_limit) fdc
78 t.fd_limit;
79 (* VirtualResourceManager.print_stat (Scheduler.get_vrm sched); *)
80 (* Logger.info "[Safeguard] memory %d/%d" (int_of_float mc) (int_of_float t.memory_limit); *)
81 (* Logger.info "[Safeguard] file_desc %d/%d" fdc t.fd_limit; *)
82 (mc >= t.memory_limit) or (fdc >= t.fd_limit)
83 in
84
85 let check () =
86 (* Gc.compact (); *)
87 if too_much ()
88 then
89 begin
90 Logger.info "[WatchDog] Start Gc";
91 Cookie2.gc_cookies (Time.now ());
92 ResourceTracker.Default.collect ();
93 Gc.compact ();
94 while (too_much ())
95 do
96 Logger.info "[WatchDog] kill a group";
97 (* let group = VirtualResourceManager.get_bigger_group vrm in *)
98 (* VirtualResourceGroup.iter (fun rid -> *)
99 (* if !rid >= 0 *)
100 (* then *)
101 (* match VirtualResourceManager.get vrm !rid with *)
102 (* | None -> () *)
103 (* | Some r' -> *)
104 (* VirtualResource.kill *)
105 (* (VirtualResource.unblack_resource r') `Killed) group; *)
106 Cookie2.gc_cookies (Time.now());
107 ResourceTracker.Default.collect ();
108 Gc.compact ();
109 done
110 end
111 in
112 Scheduler.timer sched t.freq check;
113 t
114
115
116 let close _t _sched = ()
Something went wrong with that request. Please try again.