Class: Rbkit::Profiler

Inherits:
Object
  • Object
show all
Defined in:
lib/rbkit.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pub_port, request_port) ⇒ Profiler

Returns a new instance of Profiler.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rbkit.rb', line 10

def initialize(pub_port, request_port)
  @pub_port = pub_port
  @request_port = request_port
  @profiler_thread = nil
  @stop_thread = false
  @server_running = false
  @gc_stats_timer = Rbkit::Timer.new(5) do
    data = GC.stat
    no_of_allocated_pages = data[:heap_length]
    max_objects_per_page = GC::INTERNAL_CONSTANTS[:HEAP_OBJ_LIMIT]
    size_of_one_obj = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]
    data[:total_heap_size] = no_of_allocated_pages * max_objects_per_page *
      size_of_one_obj
    data[:total_memsize] = ObjectSpace.memsize_of_all
    Rbkit.send_hash_as_event(data, "gc_stats")
  end
  @message_dispatch_timer = Rbkit::Timer.new(1) do
    Rbkit.send_messages
  end
end

Instance Attribute Details

#pub_portObject

Returns the value of attribute pub_port.



8
9
10
# File 'lib/rbkit.rb', line 8

def pub_port
  @pub_port
end

#request_portObject

Returns the value of attribute request_port.



8
9
10
# File 'lib/rbkit.rb', line 8

def request_port
  @request_port
end

Instance Method Details

#make_clean_exitObject



68
69
70
71
# File 'lib/rbkit.rb', line 68

def make_clean_exit
  @stop_thread = true
  stop_server
end

#process_incoming_request(incoming_request) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rbkit.rb', line 49

def process_incoming_request(incoming_request)
  case incoming_request
  when "start_memory_profile"
    Rbkit.start_stat_tracing
  when "stop_memory_profile"
    Rbkit.stop_stat_tracing
  when "trigger_gc"
    GC.start
  when "objectspace_snapshot"
    Rbkit.send_objectspace_dump
  end
end

#start_server(enable_profiling: false) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rbkit.rb', line 31

def start_server(enable_profiling: false)
  return if @server_running
  @profiler_thread = Thread.new do
    Rbkit.start_stat_server(pub_port, request_port)
    Rbkit.start_stat_tracing if enable_profiling
    loop do
      break if @stop_thread
      incoming_request = Rbkit.poll_for_request
      process_incoming_request(incoming_request)
      @gc_stats_timer.run
      @message_dispatch_timer.run
      # Let us sleep this thread for a bit, so as other things can run.
      sleep(0.05)
    end
  end
  @server_running = true
end

#stop_serverObject



62
63
64
65
66
# File 'lib/rbkit.rb', line 62

def stop_server
  return if !@server_running
  Rbkit.stop_stat_server
  @server_running = false
end