Module: Cirron

Defined in:
lib/tracer.rb,
lib/injector.rb,
lib/collector.rb

Defined Under Namespace

Classes: Injector

Class Method Summary collapse

Class Method Details

.calculate_overheadObject



51
52
53
54
55
56
57
58
# File 'lib/collector.rb', line 51

def self.calculate_overhead
  10.times do
    counter = collector(measure_overhead: false) {}
    Counter.members.each do |field|
      @overhead[field] = [@overhead[field], counter[field]].compact.min
    end
  end
end

.collector(measure_overhead: true, &blk) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/collector.rb', line 72

def self.collector(measure_overhead: true, &blk)
  calculate_overhead if measure_overhead && @overhead.empty?

  counter = Counter.new
  ret_val = self.start

  yield

  self.end(ret_val, counter)

  if measure_overhead && !@overhead.empty?
    Counter.members.each do |field|
      counter[field] = [counter[field] - @overhead[field], 0].max
    end
  end

  counter
end

.end(fd, counter) ⇒ Object



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

def self.end(fd, counter)
  CirronInterOp.end(fd, counter)
end

.injectorObject



61
62
63
# File 'lib/injector.rb', line 61

def self.injector
  Injector.new
end

.startObject



60
61
62
63
64
65
66
# File 'lib/collector.rb', line 60

def self.start
  ret_val = CirronInterOp.start
  if ret_val == -1
    raise "Failed to start collector"
  end
  ret_val
end

.to_tef(parsed_events) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/tracer.rb', line 131

def self.to_tef(parsed_events)
  events = parsed_events.map do |event|
    case event
    when Syscall
      start_ts = event.timestamp.to_f * 1_000_000
      duration_us = event.duration.to_f * 1_000_000
      {
        name: event.name,
        ph: "X",
        ts: start_ts,
        dur: duration_us,
        pid: event.pid.to_i,
        tid: event.pid.to_i,
        args: { args: event.args, retval: event.retval }
      }
    when TraceSignal
      ts = event.timestamp.to_f * 1_000_000
      {
        name: "Signal: #{event.name}",
        ph: "i",
        s: "g",
        ts: ts,
        pid: event.pid.to_i,
        tid: event.pid.to_i,
        args: { details: event.details }
      }
    end
  end

  JSON.pretty_generate(events)
end

.tracer(timeout = 10, &block) ⇒ Object



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
122
123
124
125
126
127
128
129
# File 'lib/tracer.rb', line 88

def self.tracer(timeout = 10, &block)
  trace_file = Tempfile.new('cirron')
  trace_file.close
  parent_pid = Process.pid
  cmd = "strace --quiet=attach,exit -f -T -ttt -o #{trace_file.path} -p #{parent_pid}"
  
  strace_proc = spawn(cmd, :out => "/dev/null", :err => "/dev/null")
  
  # Wait for the trace file to be created
  deadline = Time.now + timeout
  begin
    until File.exist?(trace_file.path)
      if Time.now > deadline
        raise Timeout::Error, "Failed to start strace within #{timeout}s."
      end
    end
    # :(
    sleep 0.1

    # We use this dummy fstat to recognize when we start executing the block
    File.stat(trace_file.path + ".dummy") rescue nil

    yield if block_given?

    # Same here, to recognize when we're done executing the block
    File.stat(trace_file.path + ".dummy") rescue nil
  ensure
    Process.kill('INT', strace_proc) rescue nil
    Process.wait(strace_proc) rescue nil
  end

  # Parse the trace file into a list of events
  trace = File.open(trace_file.path, 'r') do |file|
    parse_strace(file)
  end

  trace = filter_trace(trace, trace_file.path + ".dummy")

  trace_file.unlink

  trace
end