Module: Leakmon

Defined in:
lib/leakmon.rb,
lib/leakmon/version.rb

Defined Under Namespace

Classes: LeakmonArray, LeakmonHash, LeakmonMutex, LeakmonString, LeakmonTime

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.clear_remaining_objectsObject



42
43
44
45
46
# File 'lib/leakmon.rb', line 42

def clear_remaining_objects
  leakmon_mutex.synchronize do
    @remaining_objects = LeakmonHash.new
  end
end

.include_with_subclasses(klass = Object) ⇒ Object



15
16
17
18
19
20
# File 'lib/leakmon.rb', line 15

def include_with_subclasses(klass = Object)
  ObjectSpace.each_object(class << klass; self; end) do |cls|
    next if out_of_scope?(cls)
    cls.__send__(:include, Leakmon)
  end
end

.included(base) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/leakmon.rb', line 48

def included(base)
  class << base
    @leakmon_included ||= false
    return if @leakmon_included
    @leakmon_included = true
  end

  return unless base.private_methods.include?(:initialize)
  begin
    base.__send__(:alias_method, :initialize_without_leakmon, :initialize)
  rescue NameError
    return
  end
  base.__send__(:alias_method, :initialize, :initialize_with_leakmon)

  def base.release_hook(proc_str)
    @@leakmon_release_hook = proc_str
  end
end

.leakmon_key(obj) ⇒ Object



108
109
110
# File 'lib/leakmon.rb', line 108

def leakmon_key(obj)
  sprintf("%s__%0x", obj.class, obj.object_id)
end

.leakmon_register(obj, caller) ⇒ Object



101
102
103
104
105
106
# File 'lib/leakmon.rb', line 101

def leakmon_register(obj, caller)
  return if out_of_scope?(obj.class)
  leakmon_mutex.synchronize do
    remaining_objects[Leakmon.leakmon_key(obj)] = {:time => Time.now, :caller => caller}
  end
end

.leakmon_release(klass, key) ⇒ Object



119
120
121
122
123
124
# File 'lib/leakmon.rb', line 119

def leakmon_release(klass, key)
  return if out_of_scope?(klass)
  leakmon_mutex.synchronize do
    remaining_objects.delete(key)
  end
end

.leakmon_release_proc(klass, key, proc_str) ⇒ Object



112
113
114
115
116
117
# File 'lib/leakmon.rb', line 112

def leakmon_release_proc(klass, key, proc_str)
  proc {
    instance_eval(proc_str)
    leakmon_release(klass, key)
  }
end

.list_remaining_objects(cond = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/leakmon.rb', line 22

def list_remaining_objects(cond = {})
  leakmon_mutex.synchronize do
    cond.keys.inject(remaining_objects) {|objs, cond_key|
      new_objs = nil

      case cond_key
      when :time
        now = LeakmonTime.now
        new_objs = objs.select do |obj_k, obj_v|
          obj_v[:time] < now - cond[cond_key]
        end
      else
        raise "Invalid list option [#{cond_key}]"
      end

      new_objs
    }.sort_by{|k, v| v[:time]}
  end
end

.tcp_server(host, port) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/leakmon.rb', line 68

def tcp_server(host, port)
  require 'thread'
  require 'socket'

  Thread.new do
    @leakmon_tcp_server = TCPServer.new(host, port)
    @leakmon_tcp_server.setsockopt(:SOCKET, :REUSEADDR, true)
    loop do
      Thread.new(@leakmon_tcp_server.accept) do |c|
        while command_line = c.gets.strip
          next if command_line.empty?

          command, *args = command_line.split(/\s+/)

          case command
          when 'list'
            cond = args.empty? ? {} : {:time => Integer(args[0])}
            c.puts "now: #{Time.now}"
            Leakmon.list_remaining_objects(cond).each do |obj|
              c.puts(obj.inspect)
            end
          when 'quit'
            c.close
            Thread.exit
          else
            c.puts 'unknown command'
          end
        end
      end
    end
  end
end