Module: Most::MemoryOut

Included in:
Box
Defined in:
lib/most/helpers/memory_out.rb

Defined Under Namespace

Classes: Error, ExitException

Constant Summary collapse

THIS_FILE =
/\A#{Regexp.quote(__FILE__)}:/o
CALLER_OFFSET =
((c = caller[0]) && THIS_FILE =~ c) ? 1 : 0

Class Method Summary collapse

Class Method Details

.generic_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, checking_proc = method(:memory), &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/most/helpers/memory_out.rb', line 9

def generic_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, checking_proc = method(:memory), &block)
  return yield if bytes.nil? and block_given?

  current_thread = nil
  checker_thread = nil

  exception = klass || Class.new(ExitException)

  begin
    current_thread = Thread.current
    checker_thread = Thread.start do
      loop do
        memory_report = checking_proc.call(pid)

        GLOBALS[:memory] ||= []
        GLOBALS[:memory] << memory_report

        if memory_report > bytes
          current_thread.raise(exception)
        end

        sleep(precision)
      end
    end

    yield bytes if block_given?

  rescue exception => e
    rej = /\A#{Regexp.quote(__FILE__)}:#{__LINE__-4}\z/o
    (bt = e.backtrace).reject! {|m| rej =~ m}
    level = -caller(CALLER_OFFSET).size

    while THIS_FILE =~ bt[level]
      bt.delete_at(level)
      level += 1
    end

    raise unless klass.nil?

    raise(Error, e.message, e.backtrace)
  ensure
    checker_thread.kill() if not checker_thread.nil? and checker_thread.alive?
  end
end

.memory(pid = Process.pid) ⇒ Object



66
67
68
69
70
71
72
73
# File 'lib/most/helpers/memory_out.rb', line 66

def memory(pid = Process.pid)
  result = 0

  pid ||= GLOBALS[:pid]
  result = SERVICES[:process_table].memory(pid) unless pid.nil?

  result
end

.memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block) ⇒ Object



54
55
56
# File 'lib/most/helpers/memory_out.rb', line 54

def memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block)
  generic_memory_out(bytes, pid, precision, klass, method(:memory), &block)
end

.total_memory(pid = Process.pid) ⇒ Object



84
85
86
# File 'lib/most/helpers/memory_out.rb', line 84

def total_memory(pid = Process.pid)
  memory(pid) + virtual_memory(pid)
end

.total_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block) ⇒ Object



62
63
64
# File 'lib/most/helpers/memory_out.rb', line 62

def total_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block)
  generic_memory_out(bytes, pid, precision, klass, method(:total_memory), &block)
end

.virtual_memory(pid = Process.pid) ⇒ Object



75
76
77
78
79
80
81
82
# File 'lib/most/helpers/memory_out.rb', line 75

def virtual_memory(pid = Process.pid)
  result = 0

  pid ||= GLOBALS[:pid]
  result = SERVICES[:process_table].virtual_memory(pid) unless pid.nil?

  result
end

.virtual_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block) ⇒ Object



58
59
60
# File 'lib/most/helpers/memory_out.rb', line 58

def virtual_memory_out(bytes, pid = nil, precision = 0.1, klass = nil, &block)
  generic_memory_out(bytes, pid, precision, klass, method(:virtual_memory), &block)
end