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, RingBuf, StackTrace

Instance Attribute Summary collapse

Class Method 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, keysize: nil, leafsize: nil) ⇒ TableBase

Returns a new instance of TableBase.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/rbbcc/table.rb', line 130

def initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil, keysize: nil, leafsize: nil)
  @bpf, @map_id, @map_fd, @keysize, @leafsize = \
                                    bpf, map_id, map_fd,
                                    keysize || sizeof(keytype),
                                    leafsize || sizeof(leaftype)
  @keytype = keytype
  @leaftype = leaftype
  if @bpf
    @ttype = Clib.bpf_table_type_id(self.bpf.module, self.map_id)
    @flags = Clib.bpf_table_flags_id(self.bpf.module, self.map_id)
  else
    @ttype = case self
             when HashTable
               Table::BPF_MAP_TYPE_HASH
             when ArrayTable
               Table::BPF_MAP_TYPE_ARRAY
             end
  end
  @name = name
end

Instance Attribute Details

#bpfObject (readonly)

Returns the value of attribute bpf.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def bpf
  @bpf
end

#flagsObject (readonly)

Returns the value of attribute flags.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def flags
  @flags
end

#keysizeObject (readonly)

Returns the value of attribute keysize.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def keysize
  @keysize
end

#keytypeObject (readonly)

Returns the value of attribute keytype.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def keytype
  @keytype
end

#leafsizeObject (readonly)

Returns the value of attribute leafsize.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def leafsize
  @leafsize
end

#leaftypeObject (readonly)

Returns the value of attribute leaftype.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def leaftype
  @leaftype
end

#map_fdObject (readonly)

Returns the value of attribute map_fd.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def map_fd
  @map_fd
end

#map_idObject (readonly)

Returns the value of attribute map_id.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def map_id
  @map_id
end

#nameObject (readonly)

Returns the value of attribute name.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def name
  @name
end

#ttypeObject (readonly)

Returns the value of attribute ttype.



150
151
152
# File 'lib/rbbcc/table.rb', line 150

def ttype
  @ttype
end

Class Method Details

.from_pin(path, keytype, leaftype, keysize: nil, leafsize: nil, **kwargs) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/rbbcc/table.rb', line 119

def from_pin(path, keytype, leaftype, keysize: nil, leafsize: nil, **kwargs)
  map_fd = Clib.bpf_obj_get(path)
  if map_fd < 0
    raise SystemCallError.new("Could not open pinned map", Fiddle.last_error)
  end

  new(nil, nil, map_fd, keytype, leaftype,
      keysize: keysize, leafsize: leafsize, **kwargs)
end

Instance Method Details

#[](_key) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rbbcc/table.rb', line 173

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
  leaf.bcc_value_type = leaftype
  return leaf
end

#[]=(_key, _leaf) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/rbbcc/table.rb', line 189

def []=(_key, _leaf)
  key = normalize_key(_key)
  leaf = normalize_leaf(_leaf)
  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



235
236
237
238
# File 'lib/rbbcc/table.rb', line 235

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

#delete(key) ⇒ Object



199
200
201
202
203
204
205
# File 'lib/rbbcc/table.rb', line 199

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/rbbcc/table.rb', line 207

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



222
223
224
# File 'lib/rbbcc/table.rb', line 222

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

#each_valueObject



218
219
220
# File 'lib/rbbcc/table.rb', line 218

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

#fetch(key) ⇒ Object



185
186
187
# File 'lib/rbbcc/table.rb', line 185

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

#itemsObject



231
232
233
# File 'lib/rbbcc/table.rb', line 231

def items
  enum_for(:each_pair).to_a
end

#next(key) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/rbbcc/table.rb', line 152

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


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/rbbcc/table.rb', line 258

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


240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rbbcc/table.rb', line 240

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)


275
276
277
# File 'lib/rbbcc/table.rb', line 275

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

#valuesObject



227
228
229
# File 'lib/rbbcc/table.rb', line 227

def values
  enum_for(:each_value).to_a
end