module PerfMonger module Command class RecordOption attr_reader :devices attr_reader :interval attr_reader :verbose attr_reader :report_cpu attr_reader :report_io attr_reader :report_ctx_switch attr_reader :logfile attr_reader :parser def self.parse(argv) option = self.new argv = option.parse(argv) return argv, option end def parse(argv) argv = @parser.parse(argv) if ! @report_io && ! @report_ctx_switch && ! @report_cpu @report_cpu = true @report_io = true @all_devices = true end argv end def make_command # try to search perfmonger-record in build environment # then search installed directory record_bin = [File.expand_path("../../perfmonger_record", __FILE__), File.expand_path("lib/perfmonger/perfmonger_record", PerfMonger::ROOTDIR), File.expand_path("ext/perfmonger/perfmonger_record", PerfMonger::ROOTDIR)].find do |bin| File.executable?(bin) end if record_bin == nil || ! File.executable?(record_bin) puts("ERROR: perfmonger-record(1) not found!") exit(false) end cmd = [record_bin] cmd << '-i' cmd << @interval.to_s if @interval_backoff cmd << '-b' end if @start_delay > 0 cmd << '-s' cmd << @start_delay.to_s end if @timeout cmd << '-t' cmd << @timeout.to_s end cmd << '-C' if @report_cpu cmd << '-S' if @report_ctx_switch cmd << '-l' if @logfile != STDOUT cmd << @logfile if @logfile != STDOUT if @report_io if @all_devices cmd << '-D' else @devices.each do |device| cmd << '-d' cmd << device end end end cmd << '-v' if @verbose cmd end private def initialize @devices = [] @all_devices = false @interval = 1.0 # in second @interval_backoff = true @start_delay = 0.0 # in second @timeout = nil # in second, or nil (= no timeout) @verbose = false @report_cpu = false @report_io = false @report_ctx_switch = false @logfile = STDOUT @parser = OptionParser.new @parser.on('-d', '--device DEVICE', 'Device name to be monitored (e.g. sda, sdb, md0, dm-1).') do |device| @devices.push(device) @report_io = true end @parser.on('-D', '--all-devices', 'Monitor all block devices.') do @all_devices = true @report_io = true end @parser.on('-i', '--interval SEC', 'Amount of time between each measurement report. Floating point is o.k.') do |interval| @interval = Float(interval) end @parser.on('-B', '--no-interval-backoff', 'Prevent interval to be set longer every after 100 records.') do @interval_backoff = false end @parser.on('-s', '--start-delay SEC', 'Amount of wait time before starting measurement. Floating point is o.k.') do |start_delay| @start_delay = Float(start_delay) end @parser.on('-t', '--timeout SEC', 'Amount of measurement time. Floating point is o.k.') do |timeout| @timeout = Float(timeout) end @parser.on('-C', '--cpu', 'Report CPU usage.') do @report_cpu = true end @parser.on('-S', '--context-switch', 'Report context switches per sec.') do @report_ctx_switch = true end @parser.on('-l', '--logfile FILE') do |file| @logfile = file end @parser.on('-v', '--verbose') do @verbose = true end end end end # module Command end # module PerfMonger