Class: RbBCC::TableBase
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Fiddle::Importer, CPUHelper
- Defined in:
- lib/rbbcc/table.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#[](_key) ⇒ Object
-
#[]=(_key, _leaf) ⇒ Object
-
#clear ⇒ Object
-
#delete(key) ⇒ Object
-
#each_key ⇒ Object
-
#each_pair ⇒ Object
(also: #each)
-
#each_value ⇒ Object
-
#fetch(key) ⇒ Object
-
#initialize(bpf, map_id, map_fd, keytype, leaftype, name: nil, keysize: nil, leafsize: nil) ⇒ TableBase
constructor
A new instance of TableBase.
-
#items ⇒ Object
-
#next(key) ⇒ Object
-
#pin!(path) ⇒ Object
Just a wrapper to BCC class method.
-
#print_linear_hist(val_type = "value", section_header: "Bucket ptr", section_print_fn: nil, bucket_fn: nil, bucket_sort_fn: nil) ⇒ Object
-
#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) ⇒ Object
-
#structured_key? ⇒ Boolean
-
#values ⇒ Object
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.
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/rbbcc/table.rb', line 132
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
when PerfEventArray
Table::BPF_MAP_TYPE_PERF_EVENT_ARRAY
when RingBuf
Table::BPF_MAP_TYPE_RINGBUF
end
end
@name = name
end
|
Instance Attribute Details
#bpf ⇒ Object
Returns the value of attribute bpf.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def bpf
@bpf
end
|
#flags ⇒ Object
Returns the value of attribute flags.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def flags
@flags
end
|
#keysize ⇒ Object
Returns the value of attribute keysize.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def keysize
@keysize
end
|
#keytype ⇒ Object
Returns the value of attribute keytype.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def keytype
@keytype
end
|
#leafsize ⇒ Object
Returns the value of attribute leafsize.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def leafsize
@leafsize
end
|
#leaftype ⇒ Object
Returns the value of attribute leaftype.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def leaftype
@leaftype
end
|
#map_fd ⇒ Object
Returns the value of attribute map_fd.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def map_fd
@map_fd
end
|
#map_id ⇒ Object
Returns the value of attribute map_id.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def map_id
@map_id
end
|
#name ⇒ Object
Returns the value of attribute name.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def name
@name
end
|
#ttype ⇒ Object
Returns the value of attribute ttype.
156
157
158
|
# File 'lib/rbbcc/table.rb', line 156
def ttype
@ttype
end
|
Class Method Details
.from_pin(path, keytype, leaftype, keysize: nil, leafsize: nil, **kwargs) ⇒ Object
121
122
123
124
125
126
127
128
129
|
# File 'lib/rbbcc/table.rb', line 121
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
179
180
181
182
183
184
185
186
187
188
189
|
# File 'lib/rbbcc/table.rb', line 179
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
195
196
197
198
199
200
201
202
203
|
# File 'lib/rbbcc/table.rb', line 195
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
|
#clear ⇒ Object
241
242
243
244
|
# File 'lib/rbbcc/table.rb', line 241
def clear
each_key {|key| self.delete(key) }
return items end
|
#delete(key) ⇒ Object
205
206
207
208
209
210
211
|
# File 'lib/rbbcc/table.rb', line 205
def delete(key)
res = Clib.bpf_delete_elem(self.map_fd, key)
if res < 0
raise KeyError, "key not found"
end
res
end
|
#each_key ⇒ Object
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/rbbcc/table.rb', line 213
def each_key
k = nil
keys = []
loop do
k = self.next(k)
keys << k
yield k
end
keys
end
|
#each_pair ⇒ Object
Also known as:
each
228
229
230
|
# File 'lib/rbbcc/table.rb', line 228
def each_pair
each_key {|key| yield(key, self[key]) if self[key] }
end
|
#each_value ⇒ Object
224
225
226
|
# File 'lib/rbbcc/table.rb', line 224
def each_value
each_key {|key| yield(self[key]) if self[key] }
end
|
#fetch(key) ⇒ Object
191
192
193
|
# File 'lib/rbbcc/table.rb', line 191
def fetch(key)
self[key] || raise(KeyError, "key not found")
end
|
#items ⇒ Object
237
238
239
|
# File 'lib/rbbcc/table.rb', line 237
def items
enum_for(:each_pair).to_a
end
|
#next(key) ⇒ Object
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/rbbcc/table.rb', line 158
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
|
#pin!(path) ⇒ Object
Just a wrapper to BCC class method
286
287
288
|
# File 'lib/rbbcc/table.rb', line 286
def pin!(path)
BCC.pin!(self.map_fd, path)
end
|
#print_linear_hist(val_type = "value", section_header: "Bucket ptr", section_print_fn: nil, bucket_fn: nil, bucket_sort_fn: nil) ⇒ Object
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/rbbcc/table.rb', line 264
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
|
#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) ⇒ Object
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
# File 'lib/rbbcc/table.rb', line 246
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
281
282
283
|
# File 'lib/rbbcc/table.rb', line 281
def structured_key?
false end
|
#values ⇒ Object
233
234
235
|
# File 'lib/rbbcc/table.rb', line 233
def values
enum_for(:each_value).to_a
end
|