Class: Vernier::Collector

Inherits:
Object
  • Object
show all
Defined in:
lib/vernier/collector.rb,
ext/vernier/vernier.cc

Direct Known Subclasses

CustomCollector, RetainedCollector, TimeCollector

Defined Under Namespace

Classes: CustomCollector, RetainedCollector, TimeCollector

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mode, options = {}) ⇒ Collector



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/vernier/collector.rb', line 112

def initialize(mode, options = {})
  @gc = options.fetch(:gc, true) && (mode == :retained)
  GC.start if @gc

  @mode = mode
  @out = options[:out]
  @format = options[:format]

  @markers = []
  @hooks = []

  @thread_names = ThreadNames.new

  if options[:hooks]
    Array(options[:hooks]).each do |hook|
      add_hook(hook)
    end
  end
  @hooks.each do |hook|
    hook.enable
  end

   = options[:metadata] || {}
end

Instance Attribute Details

#stack_tableObject (readonly)

Returns the value of attribute stack_table.



137
138
139
# File 'lib/vernier/collector.rb', line 137

def stack_table
  @stack_table
end

Class Method Details

.new(mode, options = {}) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/vernier/collector.rb', line 97

def self.new(mode, options = {})
  return super unless Collector.equal?(self)

  case mode
  when :wall
    TimeCollector.new(mode, options)
  when :custom
    CustomCollector.new(mode, options)
  when :retained
    RetainedCollector.new(mode, options)
  else
    raise ArgumentError, "invalid mode: #{mode.inspect}"
  end
end

Instance Method Details

#add_marker(name:, start:, finish:, thread: Thread.current.object_id, phase: Marker::Phase::INTERVAL, data: nil) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/vernier/collector.rb', line 164

def add_marker(name:, start:, finish:, thread: Thread.current.object_id, phase: Marker::Phase::INTERVAL, data: nil)
  @markers << [thread,
               name,
               start,
               finish,
               phase,
               data]
end

#current_timeObject

Get the current time.

This method returns the current time from Process.clock_gettime in integer nanoseconds. It’s the same time used by Vernier internals and can be used to generate timestamps for custom markers.



160
161
162
# File 'lib/vernier/collector.rb', line 160

def current_time
  Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
end

#record_interval(category, name = category) ⇒ Object

Record an interval with a category and name. Yields to a block and records the amount of time spent in the block as an interval marker.



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/vernier/collector.rb', line 176

def record_interval(category, name = category)
  start = current_time
  yield
  add_marker(
    name: category,
    start:,
    finish: current_time,
    phase: Marker::Phase::INTERVAL,
    thread: Thread.current.object_id,
    data: { :type => 'UserTiming', :entryType => 'measure', :name => name }
  )
end

#stopObject



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/vernier/collector.rb', line 189

def stop
  result = finish

  result.meta[:mode] = @mode
  result.meta[:out] = @out
  result.meta[:gc] = @gc
  result.meta[:user_metadata] = 

  result.stack_table = stack_table
  @thread_names.finish

  @hooks.each do |hook|
    hook.disable
  end

  result.threads.each do |obj_id, thread|
    thread[:name] ||= @thread_names[obj_id]
  end

  result.hooks = @hooks

  end_time = Process.clock_gettime(Process::CLOCK_REALTIME, :nanosecond)
  result.pid = Process.pid
  result.end_time = end_time

  marker_strings = Marker.name_table

  markers_by_thread_id = (@markers || []).group_by(&:first)

  result.threads.each do |tid, thread|
    last_fiber = nil
    markers = []

    markers.concat markers_by_thread_id.fetch(tid, [])

    original_markers = thread[:markers] || []
    original_markers += result.gc_markers || []
    original_markers.each do |data|
      type, phase, ts, te, stack, extra_info = data
      if type == Marker::Type::FIBER_SWITCH
        if last_fiber
          start_event = markers[last_fiber]
          markers << [nil, "Fiber Running", start_event[2], ts, Marker::Phase::INTERVAL, start_event[5].merge(type: "Fiber Running", cause: nil)]
        end
        last_fiber = markers.size
      end
      name = marker_strings[type]
      sym = Marker::MARKER_SYMBOLS[type]
      data = { type: sym }
      data[:cause] = { stack: stack } if stack
      data.merge!(extra_info) if extra_info
      markers << [tid, name, ts, te, phase, data]
    end
    if last_fiber
      end_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :nanosecond)
      start_event = markers[last_fiber]
      markers << [nil, "Fiber Running", start_event[2], end_time, Marker::Phase::INTERVAL, start_event[5].merge(type: "Fiber Running", cause: nil)]
    end

    thread[:markers] = markers
  end

  #markers.concat @markers

  #result.instance_variable_set(:@markers, markers)

  if @out
    result.write(out: @out, format: @format)
  end

  result
end