Skip to content

Instantly share code, notes, and snippets.

@zzet
Created October 20, 2015 21:32
Show Gist options
  • Save zzet/a43be0e76025d3bbb276 to your computer and use it in GitHub Desktop.
Save zzet/a43be0e76025d3bbb276 to your computer and use it in GitHub Desktop.
# lens_client/lib/lens/allocations_data.rb
module Lens
if RUBY_ENGINE == 'ruby' && RUBY_VERSION >= '1.9'
require 'objspace'
class AllocationsData
def initialize
@enabled = false
@objects_count = 0
@objects_memory = 0
end
def enable
@enabled = true
end
def objects_count
@objects_count = get_count if @enabled
@objects_count
end
def objects_memory
@objects_memory = get_memory if @enabled
@objects_memory
end
def objects_count_diff
@enabled ? get_count - @objects_count : 0
end
def objects_memory_diff
@enabled ? get_memory - @objects_memory : 0
end
private
def get_count
obj_count = ObjectSpace.count_objects
obj_count[:TOTAL] - obj_count[:FREE]
end
def get_memory
ObjectSpace.count_objects_size[:TOTAL]
end
end
end
unless defined?(::Lens::AllocationsData)
class AllocationsData
attr_reader :objects_count, :objects_memory
def initialize
@objects_count = 0
@objects_memory = 0
end
def enable; end
end
end
end
#
module Lens
class Trace
def initialize(id)
# ...
@allocations_data = Lens::AllocationsData.new
@allocations_data.enable
end
def add(event)
@data.push event.payload.merge(
etype: event.name,
eduration: event.duration,
estart: event.time.to_f,
efinish: event.end.to_f,
objects_count: @allocations_data.objects_count_diff,
objects_memory: @allocations_data.objects_memory_diff
)
end
def complete(event)
formatted_data = Lens::EventFormatter.new(event, @data, @gc_statistics.total_time, @allocations_data).json_formatted
log(formatted_data)
send(formatted_data)
Thread.current[:__lens_trace] = nil
end
# ...
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment