Class: PerfMonger::Command::RecordCommand

Inherits:
BaseCommand
  • Object
show all
Defined in:
lib/perfmonger/command/record.rb

Constant Summary collapse

LOCKFILE =
File.expand_path(".perfmonger.lock", Dir.tmpdir())

Instance Method Summary collapse

Methods inherited from BaseCommand

register_alias, register_command

Constructor Details

#initializeRecordCommand

Returns a new instance of RecordCommand.



15
16
17
# File 'lib/perfmonger/command/record.rb', line 15

def initialize
  super
end

Instance Method Details

#run(argv) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/perfmonger/command/record.rb', line 19

def run(argv)
  @argv, @option = PerfMonger::Command::RecordOption.parse(argv)

  session_file = File.expand_path(sprintf("perfmonger-%s-session.pid", Etc.getpwuid.name),
                                  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
      $stderr.puts("[ERROR] No perfmonger record session is running.")
      return false
    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

    # wait until the process surely exits
    sleeptime = 0.05
    try = 0
    while true
      begin
        Process.kill(:INT, session_pid)
      rescue Errno::ESRCH
        # no such process
        break
      end
      sleep(sleeptime)
      sleeptime *= 2
      try += 1
      if try >= 5
        $stderr.puts("[ERROR] Cannot stop perfmonger record session correctly. PID=#{session_pid}")
        return false
      end
    end

    return true
  end

  if @option.status
    unless session_pid
      puts "[ERROR] No perfmonger record session 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 "==== perfmonger record is running (PID: \#{session_pid}) ====\n\n* Running executable: \#{exe}\n* Arguments: \#{args.join(\" \")}\n* Started at \#{start_time} (running \#{elapsed_time.to_i} sec)\n\n"
    rescue Errno::ESRCH
      puts "[ERROR] No perfmonger-recorder is running."
    end

    return true
  end

  # run perfmonger-recorder (normal path)

  if @option.background
    # If perfmonger is going to start in background mode,
    # there must be an another session running.

    begin
      if session_pid && Process.getpgid(session_pid)
        $stderr.puts("[ERROR] another perfmonger is already running in background mode")
        return false
      end
    rescue Errno::ESRCH
      # Actually there is no perfmonger running. go through.
    end
  end

  exec_record_cmd()

  true
end