Class: StackProf::Remote::ProcessReportCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/stackprof/remote/process_report_collector.rb

Overview

ProcessReportCollector handles the work of actually starting, stopping, and collecting the dumps from the StackProf profiler.

Internally it uses RBTrace to execute the start/stop methods against all runnign processes that match pids found by the :pid_finder option. By default this matches unicorn workers.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :pid_finder => -> {
    `pgrep -f 'unicorn worker'`.strip.split.collect {|p| p.to_i }
  },
  :mode => :cpu,
  :interval => 1000,
  :raw => true,
  :path => 'tmp'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ProcessReportCollector

Returns a new instance of ProcessReportCollector.



23
24
25
26
# File 'lib/stackprof/remote/process_report_collector.rb', line 23

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)
  collect_pids
end

Class Method Details

.report_from_marshaled_results(marshaled_data) ⇒ Object



62
63
64
65
66
67
68
69
70
71
# File 'lib/stackprof/remote/process_report_collector.rb', line 62

def self.report_from_marshaled_results(marshaled_data)
  data = Marshal.load(marshaled_data)
  if data.is_a?(Array)
    data.compact.inject(nil) do |sum, d|
      sum ? StackProf::Report.new(d) + sum : StackProf::Report.new(d)
    end
  else
    StackProf::Report.new(data)
  end
end

Instance Method Details

#loggerObject



28
29
30
# File 'lib/stackprof/remote/process_report_collector.rb', line 28

def logger
  StackProf::Remote::Middleware.logger
end

#marshaled_resultsObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/stackprof/remote/process_report_collector.rb', line 47

def marshaled_results
  if @saved_files
    logger.debug "[stackprof] Saved Files #{@saved_files.inspect}"
    saved_data = @saved_files.collect {|f|
      f = f.gsub(/"/,'') # RBTrace returns double quoted strings
      if File.readable?(f)
         Marshal.load(File.read(f))
      else
        logger.error "[stackprof] File #{f} not readable by process #{Process.pid}"
      end
    }.compact
    Marshal.dump(saved_data)
  end
end

#saveObject



42
43
44
45
# File 'lib/stackprof/remote/process_report_collector.rb', line 42

def save
  command = "StackProf::Remote::ReportSaver.save('#{@options[:path]}')"
  @saved_files = execute(command)
end

#startObject



32
33
34
35
# File 'lib/stackprof/remote/process_report_collector.rb', line 32

def start
  command = "StackProf.start(mode: #{@options[:mode].inspect}, interval: #{@options[:interval].inspect}, raw: #{@options[:raw].inspect})"
  execute(command)
end

#stopObject



37
38
39
40
# File 'lib/stackprof/remote/process_report_collector.rb', line 37

def stop
  command = "StackProf.stop"
  execute(command)
end