aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergey Nazaryev <sergey@nazaryev.ru>2018-11-06 16:21:29 +0000
committerSergey Nazaryev <sergey@nazaryev.ru>2018-11-06 16:21:29 +0000
commite4fc3d42db148cd4237a9c0393732d88cb89f349 (patch)
tree68359231c7884ba5889135af12d1d16f7e0380cb
parentdd346d1e0c4d26d88b39aa788f01d815b68e5f14 (diff)
downloadbcc-lua-xdp.zip
bcc-lua-xdp.tar.gz
bcc-lua-xdp.tar.bz2
lua: add support for PER_CPU_ARRAY table typelua-xdp
-rw-r--r--src/lua/bcc/table.lua21
-rw-r--r--src/lua/bcc/vendor/helpers.lua29
2 files changed, 48 insertions, 2 deletions
diff --git a/src/lua/bcc/table.lua b/src/lua/bcc/table.lua
index 9729751..f070c29 100644
--- a/src/lua/bcc/table.lua
+++ b/src/lua/bcc/table.lua
@@ -13,6 +13,8 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]
+require("bcc.vendor.helpers")
+
local ffi = require("ffi")
local libbcc = require("bcc.libbcc")
local Posix = require("bcc.vendor.posix")
@@ -40,6 +42,7 @@ function BaseTable:initialize(t_type, bpf, map_id, map_fd, key_type, leaf_type)
self.map_fd = map_fd
self.c_key = ffi.typeof(key_type.."[1]")
self.c_leaf = ffi.typeof(leaf_type.."[1]")
+ self.c_sleaf = ffi.typeof(leaf_type.."[1]")
end
function BaseTable:key_sprintf(key)
@@ -214,10 +217,16 @@ function BaseArray:items(with_index)
return nil
end
+ local result = {}
+ local count = math.floor(ffi.sizeof(self.c_leaf) / ffi.sizeof(self.c_sleaf))
+ for i=0,count-1 do
+ table.insert(result, pvalue[i])
+ end
+
if with_index then
- return n, pvalue[0] -- return 1-based index
+ return n, result
else
- return pvalue[0]
+ return result
end
end
end
@@ -230,7 +239,13 @@ function Array:initialize(bpf, map_id, map_fd, key_type, leaf_type)
BaseArray.initialize(self, BaseTable.BPF_MAP_TYPE_ARRAY, bpf, map_id, map_fd, key_type, leaf_type)
end
+local PerCpuArray = class("PerCpuArray", BaseArray)
+function PerCpuArray:initialize(bpf, map_id, map_fd, key_type, leaf_type)
+ BaseArray.initialize(self, BaseTable.BPF_MAP_TYPE_PERCPU_ARRAY, bpf, map_id, map_fd, key_type, leaf_type)
+ local total_cpu = os.get_possible_cpus()
+ self.c_leaf = ffi.typeof("%s[%d]" % { leaf_type, #total_cpu })
+end
local PerfEventArray = class("PerfEventArray", BaseArray)
@@ -379,6 +394,8 @@ local function NewTable(bpf, name, key_type, leaf_type)
table = PerfEventArray
elseif t_type == BaseTable.BPF_MAP_TYPE_STACK_TRACE then
table = StackTrace
+ elseif t_type == BaseTable.BPF_MAP_TYPE_PERCPU_ARRAY then
+ table = PerCpuArray
end
assert(table, "unsupported table type %d" % t_type)
diff --git a/src/lua/bcc/vendor/helpers.lua b/src/lua/bcc/vendor/helpers.lua
index 28d5a0c..37e359f 100644
--- a/src/lua/bcc/vendor/helpers.lua
+++ b/src/lua/bcc/vendor/helpers.lua
@@ -167,6 +167,35 @@ function os.spawn(...)
return out
end
+local function _read_cpu_range(path)
+ local f = io.open(path,"r")
+
+ local cpus = {}
+ local cpus_range_str = f:read("*all")
+ io.close(f)
+
+ for idx, cpu_range in ipairs(cpus_range_str:split(',', true)) do
+ local rangeop = cpu_range:find('-')
+ if rangeop == nil then
+ table.insert(cpus, tonumber(cpu_range))
+ else
+ local first = tonumber(cpu_range:sub(1,rangeop-1))
+ local last = tonumber(cpu_range:sub(rangeop+1))
+ for i=first,last do table.insert(cpus, i) end
+ end
+ end
+
+ return cpus
+end
+
+function os.get_online_cpus()
+ return _read_cpu_range('/sys/devices/system/cpu/online')
+end
+
+function os.get_possible_cpus()
+ return _read_cpu_range('/sys/devices/system/cpu/possible')
+end
+
local function logline(...)
if not log.enabled then
return