Class: RbBCC::TableBase
- Inherits:
-
Object
- Object
- RbBCC::TableBase
- Includes:
- Enumerable, Fiddle::Importer, CPUHelper
- Defined in:
- lib/rbbcc/table.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#bpf ⇒ Object
readonly
Returns the value of attribute bpf.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#keysize ⇒ Object
readonly
Returns the value of attribute keysize.
-
#keytype ⇒ Object
readonly
Returns the value of attribute keytype.
-
#leafsize ⇒ Object
readonly
Returns the value of attribute leafsize.
-
#leaftype ⇒ Object
readonly
Returns the value of attribute leaftype.
-
#map_fd ⇒ Object
readonly
Returns the value of attribute map_fd.
-
#map_id ⇒ Object
readonly
Returns the value of attribute map_id.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ttype ⇒ Object
readonly
Returns the value of attribute ttype.
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) ⇒ TableBase
constructor
A new instance of TableBase.
- #items ⇒ Object
- #next(key) ⇒ Object
- #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) ⇒ TableBase
Returns a new instance of TableBase.
50 51 52 53 54 55 56 57 58 |
# 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) @keytype = keytype @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
#bpf ⇒ Object (readonly)
Returns the value of attribute bpf.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def bpf @bpf end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def flags @flags end |
#keysize ⇒ Object (readonly)
Returns the value of attribute keysize.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def keysize @keysize end |
#keytype ⇒ Object (readonly)
Returns the value of attribute keytype.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def keytype @keytype end |
#leafsize ⇒ Object (readonly)
Returns the value of attribute leafsize.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def leafsize @leafsize end |
#leaftype ⇒ Object (readonly)
Returns the value of attribute leaftype.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def leaftype @leaftype end |
#map_fd ⇒ Object (readonly)
Returns the value of attribute map_fd.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def map_fd @map_fd end |
#map_id ⇒ Object (readonly)
Returns the value of attribute map_id.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def map_id @map_id end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def name @name end |
#ttype ⇒ Object (readonly)
Returns the value of attribute ttype.
59 60 61 |
# File 'lib/rbbcc/table.rb', line 59 def ttype @ttype end |
Instance Method Details
#[](_key) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/rbbcc/table.rb', line 82 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
98 99 100 101 102 103 104 105 106 |
# File 'lib/rbbcc/table.rb', line 98 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
144 145 146 147 |
# File 'lib/rbbcc/table.rb', line 144 def clear each_key {|key| self.delete(key) } return items # reload contents end |
#delete(key) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/rbbcc/table.rb', line 108 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
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rbbcc/table.rb', line 116 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
131 132 133 |
# File 'lib/rbbcc/table.rb', line 131 def each_pair each_key {|key| yield(key, self[key]) if self[key] } end |
#each_value ⇒ Object
127 128 129 |
# File 'lib/rbbcc/table.rb', line 127 def each_value each_key {|key| yield(self[key]) if self[key] } end |
#fetch(key) ⇒ Object
94 95 96 |
# File 'lib/rbbcc/table.rb', line 94 def fetch(key) self[key] || raise(KeyError, "key not found") end |
#items ⇒ Object
140 141 142 |
# File 'lib/rbbcc/table.rb', line 140 def items enum_for(:each_pair).to_a end |
#next(key) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rbbcc/table.rb', line 61 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 |
#print_linear_hist(val_type = "value", section_header: "Bucket ptr", section_print_fn: nil, bucket_fn: nil, bucket_sort_fn: nil) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rbbcc/table.rb', line 167 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
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rbbcc/table.rb', line 149 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
184 185 186 |
# File 'lib/rbbcc/table.rb', line 184 def structured_key? false # TODO: implement me in the future end |
#values ⇒ Object
136 137 138 |
# File 'lib/rbbcc/table.rb', line 136 def values enum_for(:each_value).to_a end |