Class: RbBCC::TableBase

Inherits:
Object
  • Object
show all
Includes:
Enumerable, Fiddle::Importer, CPUHelper
Defined in:
lib/rbbcc/table.rb

Direct Known Subclasses

ArrayTable, HashTable, PerfEventArray, StackTrace

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from CPUHelper

_read_cpu_range, get_online_cpus, get_possible_cpus

Constructor Details

#initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil) ⇒ TableBase

Returns a new instance of TableBase.



50
51
52
53
54
55
56
57
# File 'lib/rbbcc/table.rb', line 50

def initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil)
  @bpf, @map_id, @map_fd, @keysize, @leafsize = \
                                    bpf, map_id, map_fd, sizeof(keytype), sizeof(leaftype)
  @leaftype = leaftype
  @ttype = Clib.bpf_table_type_id(self.bpf.module, self.map_id)
  @flags = Clib.bpf_table_flags_id(self.bpf.module, self.map_id)
  @name = name
end

Instance Attribute Details

#bpfObject (readonly)

Returns the value of attribute bpf.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def bpf
  @bpf
end

#flagsObject (readonly)

Returns the value of attribute flags.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def flags
  @flags
end

#keysizeObject (readonly)

Returns the value of attribute keysize.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def keysize
  @keysize
end

#leafsizeObject (readonly)

Returns the value of attribute leafsize.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def leafsize
  @leafsize
end

#leaftypeObject (readonly)

Returns the value of attribute leaftype.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def leaftype
  @leaftype
end

#map_fdObject (readonly)

Returns the value of attribute map_fd.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def map_fd
  @map_fd
end

#map_idObject (readonly)

Returns the value of attribute map_id.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def map_id
  @map_id
end

#nameObject (readonly)

Returns the value of attribute name.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def name
  @name
end

#ttypeObject (readonly)

Returns the value of attribute ttype.



58
59
60
# File 'lib/rbbcc/table.rb', line 58

def ttype
  @ttype
end

Instance Method Details

#[](_key) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/rbbcc/table.rb', line 81

def [](_key)
  key = normalize_key(_key)

  leaf = Fiddle::Pointer.malloc(self.leafsize)
  res = Clib.bpf_lookup_elem(self.map_fd, key, leaf)
  if res < 0
    nil
  end
  return leaf
end

#[]=(_key, leaf) ⇒ Object



96
97
98
99
100
101
102
103
# File 'lib/rbbcc/table.rb', line 96

def []=(_key, leaf)
  key = normalize_key(_key)
  res = Clib.bpf_update_elem(self.map_fd, key, leaf, 0)
  if res < 0
    raise SystemCallError.new("Could not update table", Fiddle.last_error)
  end
  leaf
end

#clearObject



141
142
143
144
# File 'lib/rbbcc/table.rb', line 141

def clear
  each_key {|key| self.delete(key) }
  return items # reload contents
end

#delete(key) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/rbbcc/table.rb', line 105

def delete(key)
  res = Clib.bpf_delete_elem(self.map_fd, key)
  if res < 0
    raise KeyError, "key not found"
  end
  res
end

#each_keyObject



113
114
115
116
117
118
119
120
121
122
# File 'lib/rbbcc/table.rb', line 113

def each_key
  k = nil
  keys = []
  loop do
    k = self.next(k)
    keys << k
    yield k
  end
  keys
end

#each_pairObject Also known as: each



128
129
130
# File 'lib/rbbcc/table.rb', line 128

def each_pair
  each_key {|key| yield(key, self[key]) if self[key] }
end

#each_valueObject



124
125
126
# File 'lib/rbbcc/table.rb', line 124

def each_value
  each_key {|key| yield(self[key]) if self[key] }
end

#fetch(key) ⇒ Object



92
93
94
# File 'lib/rbbcc/table.rb', line 92

def fetch(key)
  self[key] || raise(KeyError, "key not found")
end

#itemsObject



137
138
139
# File 'lib/rbbcc/table.rb', line 137

def items
  enum_for(:each_pair).to_a
end

#next(key) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rbbcc/table.rb', line 60

def next(key)
  next_key = Fiddle::Pointer.malloc(self.keysize)

  if !key
    res = Clib.bpf_get_first_key(self.map_fd, next_key,
                                 next_key.size)
  else
    unless key.is_a?(Fiddle::Pointer)
      raise TypeError, key.inspect
    end
    res = Clib.bpf_get_next_key(self.map_fd, key,
                                next_key)
  end

  if res < 0
    raise StopIteration
  end

  return next_key
end


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rbbcc/table.rb', line 164

def print_linear_hist(val_type="value",
                      section_header: "Bucket ptr",
                      section_print_fn: nil,
                      bucket_fn: nil,
                      bucket_sort_fn: nil)
  if structured_key?
    raise NotImplementedError
  else
    vals = Array.new($linear_index_max) { 0 }
    each_pair do |k, v|
      vals[k.to_bcc_value] = v.to_bcc_value
    end
    RbBCC.print_linear_hist(vals, val_type)
  end
  nil
end


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/rbbcc/table.rb', line 146

def print_log2_hist(val_type="value",
                    section_header: "Bucket ptr",
                    section_print_fn: nil,
                    bucket_fn: nil,
                    strip_leading_zero: false,
                    bucket_sort_fn: nil)
  if structured_key?
    raise NotImplementedError
  else
    vals = Array.new($log2_index_max) { 0 }
    each_pair do |k, v|
      vals[k.to_bcc_value] = v.to_bcc_value
    end
    RbBCC.print_log2_hist(vals, val_type, strip_leading_zero)
  end
  nil
end

#structured_key?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/rbbcc/table.rb', line 181

def structured_key?
  false # TODO: implement me in the future
end

#valuesObject



133
134
135
# File 'lib/rbbcc/table.rb', line 133

def values
  enum_for(:each_value).to_a
end