Module: GcMonitor
- Defined in:
- lib/gc_monitor.rb
Constant Summary collapse
- VERSION =
'0.0.5'
Class Method Summary collapse
- .include_in_subclasses(klass = Object) ⇒ Object
- .included(base) ⇒ Object
- .key(obj) ⇒ Object
- .list(cond = {}) ⇒ Object
- .mutex ⇒ Object
- .out_of_scope?(klass) ⇒ Boolean
- .regist(obj, caller) ⇒ Object
- .release(obj, caller) ⇒ Object
- .release_proc(proc_str) ⇒ Object
- .remaining_objects ⇒ Object
- .tcp_server(host, port) ⇒ Object
Class Method Details
.include_in_subclasses(klass = Object) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/gc_monitor.rb', line 21 def include_in_subclasses(klass = Object) ObjectSpace.each_object(class << klass; self; end) do |cls| next if cls.ancestors.include?(Exception) next if out_of_scope?(cls) cls.__send__(:include, GcMonitor) end end |
.included(base) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/gc_monitor.rb', line 74 def included(base) class << base @gc_monitor_included ||= false return if @gc_monitor_included @gc_monitor_included = true end return unless base.private_methods.include?("initialize") begin base.__send__(:alias_method, :initialize_without_gc_monitor_pre, :initialize) rescue NameError return end base.__send__(:alias_method, :initialize, :initialize_with_gc_monitor_pre) def base.release_hook(proc_str) @@gc_monitor_release_hook = proc_str end end |
.key(obj) ⇒ Object
29 30 31 |
# File 'lib/gc_monitor.rb', line 29 def key(obj) sprintf("%s__%0x", obj.class, obj.object_id) end |
.list(cond = {}) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/gc_monitor.rb', line 47 def list(cond = {}) mutex.synchronize do cond.keys.inject(remaining_objects) {|objs, cond_key| new_objs = nil case cond_key when :time now = Time.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 |
.mutex ⇒ Object
12 13 14 15 |
# File 'lib/gc_monitor.rb', line 12 def mutex @mutex ||= Mutex.new @mutex end |
.out_of_scope?(klass) ⇒ Boolean
17 18 19 |
# File 'lib/gc_monitor.rb', line 17 def out_of_scope?(klass) [GcMonitor, Time, Mutex].include?(klass) end |
.regist(obj, caller) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/gc_monitor.rb', line 33 def regist(obj, caller) return if out_of_scope?(obj.class) mutex.synchronize do remaining_objects[GcMonitor.key(obj)] = {:time => Time.now, :caller => caller} end end |
.release(obj, caller) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/gc_monitor.rb', line 40 def release(obj, caller) return if out_of_scope?(obj.class) mutex.synchronize do remaining_objects.delete(GcMonitor.key(obj)) end end |
.release_proc(proc_str) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/gc_monitor.rb', line 67 def release_proc(proc_str) lambda { instance_eval(proc_str) GcMonitor.release(self) } end |
.remaining_objects ⇒ Object
7 8 9 10 |
# File 'lib/gc_monitor.rb', line 7 def remaining_objects @remaining_objects ||= {} @remaining_objects end |
.tcp_server(host, port) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/gc_monitor.rb', line 94 def tcp_server(host, port) require 'socket' Thread.new do s = TCPServer.new(host, port) loop do Thread.new(s.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}" GcMonitor.list(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 |