Class: Utilrb::RubyObjectGraph
- Defined in:
- lib/utilrb/ruby_object_graph.rb
Defined Under Namespace
Modules: GraphGenerationObjectMaker Classes: ObjectRef
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the value of attribute graph.
-
#references ⇒ Object
readonly
Returns the value of attribute references.
Class Method Summary collapse
-
.dot_snapshot(options = Hash.new) ⇒ Object
Generates a graph of all the objects currently live, in dot format.
-
.snapshot ⇒ Object
Takes a snapshot of all objects currently live.
Instance Method Summary collapse
-
#add_reference(obj_ref, var_ref, desc) ⇒ void
Register a ruby object reference.
- #clear ⇒ Object
- #collapse(*klasses) ⇒ Object
- #display_live_chains(chains) ⇒ Object
-
#initialize ⇒ RubyObjectGraph
constructor
A new instance of RubyObjectGraph.
- #live_chain_for(obj) ⇒ Object
-
#register_references_to(live_objects, options = Hash.new) ⇒ Object
Creates a BGL::Graph of ObjectRef objects which stores the current ruby object graph.
- #test_and_add_reference(obj_ref, var, desc) ⇒ Object
- #to_dot ⇒ Object
Constructor Details
#initialize ⇒ RubyObjectGraph
Returns a new instance of RubyObjectGraph.
56 57 58 59 |
# File 'lib/utilrb/ruby_object_graph.rb', line 56 def initialize @graph = BGL::Graph.new @references = Hash.new end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the value of attribute graph.
17 18 19 |
# File 'lib/utilrb/ruby_object_graph.rb', line 17 def graph @graph end |
#references ⇒ Object (readonly)
Returns the value of attribute references.
18 19 20 |
# File 'lib/utilrb/ruby_object_graph.rb', line 18 def references @references end |
Class Method Details
.dot_snapshot(options = Hash.new) ⇒ Object
Generates a graph of all the objects currently live, in dot format
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/utilrb/ruby_object_graph.rb', line 38 def self.dot_snapshot( = Hash.new) r, w = IO.pipe live_objects = RubyObjectGraph.snapshot fork do , = Kernel. , :collapse => nil ruby_graph = RubyObjectGraph.new ruby_graph.register_references_to(live_objects, ) if [:collapse] ruby_graph.collapse(*[:collapse]) end w.write ruby_graph.to_dot exit! true end w.close r.read end |
.snapshot ⇒ Object
Takes a snapshot of all objects currently live
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/utilrb/ruby_object_graph.rb', line 21 def self.snapshot # Doing any Ruby code that do not modify the heap is impossible. List # all existing objects once, and only then do the processing. # +live_objects+ will be referenced in itself, but we are going to # remove it later GC.disable live_objects = Array.new ObjectSpace.each_object do |obj| if obj.object_id != live_objects.object_id live_objects << obj end end GC.enable live_objects end |
Instance Method Details
#add_reference(obj_ref, var_ref, desc) ⇒ void
This method returns an undefined value.
Register a ruby object reference
98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/utilrb/ruby_object_graph.rb', line 98 def add_reference(obj_ref, var_ref, desc) if graph.linked?(obj_ref, var_ref) desc_set = obj_ref[var_ref, graph] if !desc_set.include?(desc) desc_set << desc end else desc_set = [desc] graph.link(obj_ref, var_ref, desc_set) end end |
#clear ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/utilrb/ruby_object_graph.rb', line 61 def clear graph.each_edge do |from, to, info| info.clear end graph.clear references.clear end |
#collapse(*klasses) ⇒ Object
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/utilrb/ruby_object_graph.rb', line 239 def collapse(*klasses) vertices = graph.vertices.dup vertices.each do |v| case v.obj when *klasses next if v.root?(graph) || v.leaf?(graph) v.each_parent_vertex(graph) do |parent| all_parent_info = parent[v, graph] v.each_child_vertex(graph) do |child| all_child_info = v[child, graph] all_parent_info.each do |parent_info| all_child_info.each do |child_info| add_reference(parent, child, parent_info + "." + child_info) end end end end graph.remove(v) end end end |
#display_live_chains(chains) ⇒ Object
286 287 288 289 290 |
# File 'lib/utilrb/ruby_object_graph.rb', line 286 def display_live_chains(chains) chains.each do |stack| puts stack.map { |obj, link| "#{link} #{obj}" }.join("\n >") end end |
#live_chain_for(obj) ⇒ Object
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/utilrb/ruby_object_graph.rb', line 262 def live_chain_for(obj) result = [] # Find one Roby task and look at the chain ref = references[obj.object_id] stack = [[ref.obj]] graph.reverse.each_dfs(ref, BGL::Graph::TREE) do |source, target, info, kind| source = source.obj target = target.obj if stack.last.first.object_id != source.object_id && stack.any? { |obj, _| obj.object_id == source.object_id } result << stack.dup if stack.size != 1 while stack.last.first.object_id != source.object_id stack.pop end end stack.push [target, info] end if stack.size != 1 result << stack end result end |
#register_references_to(live_objects, options = Hash.new) ⇒ Object
Creates a BGL::Graph of ObjectRef objects which stores the current ruby object graph
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/utilrb/ruby_object_graph.rb', line 117 def register_references_to(live_objects, = Hash.new) = # to exclude it from the graph = Kernel. , :roots => [Object], :excluded_classes => [], :excluded_objects => [], :include_class_relation => false roots_class, roots = [:roots].partition { |obj| obj.kind_of?(Class) } excluded_classes = [:excluded_classes] excluded_objects = [:excluded_objects] include_class_relation = [:include_class_relation] # Create a single ObjectRef per (interesting) live object, so that we # can use a BGL::Graph to represent the reference graph. This will be # what we are going to access later on. Use object IDs since we really # want to refer to objects and not use eql? comparisons desired_seeds = roots.map(&:object_id) excludes = [live_objects, self, graph, references, , , roots, roots_class].to_value_set live_objects_total = live_objects.size live_objects.delete_if do |obj| if obj.kind_of?(DRbObject) true elsif excludes.include?(obj) || obj.respond_to?(:__ruby_object_graph_internal__) true else references[obj.object_id] ||= ObjectRef.new(obj) if roots_class.any? { |k| obj.kind_of?(k) } if !excluded_classes.any? { |k| obj.kind_of?(k) } if !excluded_objects.include?(obj) desired_seeds << obj.object_id end end end false end end desired_seeds.each do |obj_id| graph.insert(references[obj_id]) end ignored_enumeration = Hash.new names = Hash[ :array => "Array", :value_set => "ValueSet[]", :vertex => "Vertex[]", :edge => "Edge[]", :hash_key => "Hash[key]", :hash_value => "Hash[value]", :proc => "Proc", :ancestor => "Ancestor"] puts "RubyObjectGraph: #{live_objects.size} objects found, #{desired_seeds.size} seeds and #{live_objects_total} total live objects" loop do old_graph_size = graph.size live_objects.each do |obj| obj_ref = references[obj.object_id] if include_class_relation test_and_add_reference(obj_ref, obj.class, "class") end for var_name in obj.instance_variables var = obj.instance_variable_get(var_name) test_and_add_reference(obj_ref, var, var_name.to_s) end case obj when Array for var in obj test_and_add_reference(obj_ref, var, names[:array]) end when ValueSet for var in obj test_and_add_reference(obj_ref, var, names[:value_set]) end when BGL::Graph obj.each_vertex do test_and_add_reference(obj_ref, var, names[:vertex]) end obj.each_edge do |_, _, info| test_and_add_reference(obj_ref, info, names[:edge]) end when Hash for var in obj test_and_add_reference(obj_ref, var[0], names[:hash_key]) test_and_add_reference(obj_ref, var[1], names[:hash_value]) end when Proc if obj.respond_to?(:references) for var in obj.references begin test_and_add_reference(obj_ref, ObjectSpace._id2ref(var), names[:proc]) rescue RangeError end end end when Class for ref in obj.ancestors test_and_add_reference(obj_ref, ref, names[:ancestor]) end else if obj.respond_to?(:each) if obj.kind_of?(Module) || obj.kind_of?(Class) if !ignored_enumeration[obj] ignored_enumeration[obj] = true puts "ignoring enumerator class/module #{obj}" end else if !ignored_enumeration[obj.class] ignored_enumeration[obj.class] = true puts "ignoring enumerator object of class #{obj.class}" end end end end end if old_graph_size == graph.size break end end live_objects.clear # to avoid making it a central node in future calls return graph end |
#test_and_add_reference(obj_ref, var, desc) ⇒ Object
83 84 85 86 87 88 |
# File 'lib/utilrb/ruby_object_graph.rb', line 83 def test_and_add_reference(obj_ref, var, desc) var_ref = references[var.object_id] if graph.include?(var_ref) add_reference(obj_ref, var_ref, desc) end end |
#to_dot ⇒ Object
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 |
# File 'lib/utilrb/ruby_object_graph.rb', line 292 def to_dot roots = graph.vertices.find_all do |v| v.root?(graph) end.to_value_set io = StringIO.new("") colors = Hash[ :green => 'green', :magenta => 'magenta', :black => 'black' ] obj_label_format = "obj%i [label=\"%s\",color=%s];" obj_label_format_elements = [] edge_label_format_0 = "obj%i -> obj%i [label=\"" edge_label_format_1 = "\"];" edge_label_format_elements = [] all_seen = ValueSet.new seen = ValueSet.new io.puts "digraph {" roots.each do |obj_ref| graph.each_dfs(obj_ref, BGL::Graph::ALL) do |from_ref, to_ref, all_info, kind| info = [] for str in all_info info << str end if all_seen.include?(from_ref) graph.prune else from_id = from_ref.obj.object_id to_id = to_ref.obj.object_id edge_label_format_elements.clear edge_label_format_elements << from_id << to_id str = edge_label_format_0 % edge_label_format_elements first = true for edge_info in all_info if !first str << "," end str << edge_info first = false end str << edge_label_format_1 io.puts str end seen << from_ref << to_ref end for obj_ref in seen if !all_seen.include?(obj_ref) obj = obj_ref.obj obj_id = obj_ref.obj.object_id str = if obj.respond_to?(:each) "#{obj.class}: #{obj.object_id}" else obj.to_s end str = str.encode('UTF-8', :invalid => :replace, :undef => :replace, :replace => "") str = str.gsub(/[^\w\.:<>]/, "") color = if obj.kind_of?(BGL::Graph) :magenta elsif obj.kind_of?(Hash) || obj.kind_of?(Array) || obj.kind_of?(ValueSet) :green else :black end obj_label_format_elements = [obj_id, str.gsub(/[\\"\n]/, " "), colors[color]] str = obj_label_format % obj_label_format_elements io.puts(str) end end all_seen.merge(seen) seen.clear end roots.clear all_seen.clear seen.clear io.puts "}" io.string end |