require 'optparse' require 'tempfile' require 'tmpdir' require 'json' require 'etc' module PerfMonger module Command class RecordCommand < BaseCommand LOCKFILE = File.expand_path(".perfmonger.lock", Dir.tmpdir()) register_command 'record', 'Record system performance information' def initialize super end def run(argv) @argv, @option = PerfMonger::Command::RecordOption.parse(argv) session_file = File.expand_path(sprintf("perfmonger-%s-session.pid", Etc.getlogin), Dir.tmpdir) begin session_pid = File.read(session_file).to_i rescue Errno::ENOENT # No session file session_pid = nil end if @option.kill unless session_pid # There is nothing to be killed return true end begin Process.kill(:INT, session_pid) rescue Errno::ESRCH # Session file has invalid (already dead) PID File.open(LOCKFILE, "w") do |f| f.flock(File::LOCK_EX) FileUtils.rm(session_file) f.flock(File::LOCK_UN) end end return true end if @option.status unless session_pid puts "[ERROR] No perfmonger-recorder is running." return false end begin # check if session_pid is valid gid = Process.getpgid(session_pid) cmdline = File.read("/proc/#{session_pid}/cmdline").split("\0") exe = cmdline.shift args = cmdline start_time = File::Stat.new("/proc/#{session_pid}").mtime elapsed_time = Time.now - start_time puts <