Class: Trace::EventBuffer

Inherits:
Object
  • Object
show all
Defined in:
lib/eventbuffer.rb

Defined Under Namespace

Classes: EventStruct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maxsize = nil) ⇒ EventBuffer

then marks will drop out as they disappear from the buffer



13
14
15
16
# File 'lib/eventbuffer.rb', line 13

def initialize(maxsize=nil)
  @maxsize = maxsize
  reset
end

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



8
9
10
# File 'lib/eventbuffer.rb', line 8

def buf
  @buf
end

#marksObject

User position mark into buffer. If buffer is limited,



9
10
11
# File 'lib/eventbuffer.rb', line 9

def marks
  @marks
end

#maxsizeObject (readonly)

Maximum size of buffer or nil if unlimited.



10
11
12
# File 'lib/eventbuffer.rb', line 10

def maxsize
  @maxsize
end

#sizeObject (readonly)

size of buffer



11
12
13
# File 'lib/eventbuffer.rb', line 11

def size
  @size
end

Instance Method Details

#add_markObject

Add mark for the current event buffer position.



45
46
47
# File 'lib/eventbuffer.rb', line 45

def add_mark
  @marks << @pos
end

#add_mark_nodupObject

Like add mark, but do only if the last marked position has changed



51
52
53
# File 'lib/eventbuffer.rb', line 51

def add_mark_nodup
  @marks << @pos unless @marks[-1] == @pos
end

#append(event, frame, arg) ⇒ Object

Add a new event dropping off old events if that was declared marks are also dropped if buffer has a limit.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/eventbuffer.rb', line 27

def append(event, frame, arg)
  if 'c-return' == event 
    arg = frame.sp(2)
  elsif 'return' == event 
    arg = frame.sp(1)
  end
    
  iseq = frame.iseq
  item = EventStruct.new(event, arg, frame.type, frame.thread, frame.method,
                         frame.source_container, frame.source_location,
                         iseq, iseq ? frame.pc_offset : nil)
  @pos = self.succ_pos
  @marks.shift if @marks[0] == @pos
  @buf[@pos] = item
  @size     += 1 unless @maxsize && @size == @maxsize
end

#each(from = nil, to = nil) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/eventbuffer.rb', line 55

def each(from=nil, to=nil)
  from = self.succ_pos unless from
  to   = @pos unless to
  if from <= to
    from.upto(to).each do |pos|
      yield @buf[pos]
    end
  else
    from.upto(@size-1).each do |pos|
      yield @buf[pos]
    end
    0.upto(@pos).each do |pos|
      yield @buf[pos]
    end
  end
end

#each_with_index(from = nil, to = nil) ⇒ Object



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

def each_with_index(from=nil, to=nil)
  from = succ_pos unless from
  to   = @pos     unless to
  if from <= to
    from.upto(to).each do |pos|
      yield [@buf[pos], pos]
    end
  else
    from.upto(@size-1).each do |pos|
      yield [@buf[pos], pos]
    end
    0.upto(@pos).each do |pos|
      yield [@buf[pos], pos]
    end
  end
end

#format_entry(item, long_format = true) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/eventbuffer.rb', line 89

def format_entry(item, long_format=true)
  # require 'rbdbgr'; Debugger.debug
  container = 
    if item.source_container[0] == 'file'
      item.source_container[1].inspect
    else
      item.source_container.inspect
    end
  
  location = 
    if 1 == item.source_location.size 
      item.source_location[0].inspect
    else
      item.source_location.inspect
    end
  
  mess = "#{item.event} #{item.type} #{item.method} " + 
    "#{container} #{location}"
  if long_format && item.iseq
    mess += "\n\t" + "VM offset #{item.pc_offset} of #{item.iseq.name}"
  end
  mess
end

#pred_pos(dec = 1) ⇒ Object

Return the next event buffer position taking into account that we may have a fixed-sized buffer ring.



122
123
124
125
# File 'lib/eventbuffer.rb', line 122

def pred_pos(dec=1)
  pos = @pos - dec
  @maxsize ? pos % @maxsize : pos 
end

#resetObject



18
19
20
21
22
23
# File 'lib/eventbuffer.rb', line 18

def reset
  @buf   = []
  @marks = []
  @pos   = -1
  @size  = 0 
end

#succ_pos(inc = 1) ⇒ Object

Return the next event buffer position taking into account that we may have a fixed-sized buffer ring.



115
116
117
118
# File 'lib/eventbuffer.rb', line 115

def succ_pos(inc=1)
  pos = @pos + inc 
  @maxsize ? pos % @maxsize : pos 
end

#zero_posObject

Return the adjusted zeroth position in @buf.



128
129
130
131
132
133
134
# File 'lib/eventbuffer.rb', line 128

def zero_pos
  if !@maxsize || @buf.size < @maxsize
    0
  else 
    self.succ_pos
  end
end