4 # Condition Symbol :memory_usage
7 # Trigger when the resident memory of a process is above a specified limit.
11 # +pid_file+ is the pid file of the process in question. Automatically
12 # populated for Watches.
13 # +above+ is the amount of resident memory (in kilobytes) above which
14 # the condition should trigger. You can also use the sugar
15 # methods #kilobytes, #megabytes, and #gigabytes to clarify
16 # this amount (see examples).
20 # Trigger if the process is using more than 100 megabytes of resident
21 # memory (from a Watch):
23 # on.condition(:memory_usage) do |c|
24 # c.above = 100.megabytes
27 # Non-Watch Tasks must specify a PID file:
29 # on.condition(:memory_usage) do |c|
30 # c.above = 100.megabytes
31 # c.pid_file = "/var/run/mongrel.3000.pid"
33 class MemoryUsage < PollCondition
34 attr_accessor :above, :times, :pid_file
43 if self.times.kind_of?(Integer)
44 self.times = [self.times, self.times]
47 @timeline = Timeline.new(self.times[1])
55 self.watch.pid || File.read(self.pid_file).strip.to_i
60 valid &= complain("Attribute 'pid_file' must be specified", self) if self.watch.pid_file.nil? && self.pid_file.nil?
61 valid &= complain("Attribute 'above' must be specified", self) if self.above.nil?
67 process = System::Process.new(pid)
68 @timeline.push(process.memory)
70 history = "[" + @timeline.map { |x| "#{x > self.above ? '*' : ''}#{x}kb" }.join(", ") + "]"
72 if @timeline.select { |x| x > self.above }.size >= self.times.first
73 self.info = "memory out of bounds #{history}"
76 self.info = "memory within bounds #{history}"