Skip to content

Commit

Permalink
core.counter: Fixes for double-buffering extension
Browse files Browse the repository at this point in the history
Added a method 'counter.delete(name)' to unmap and unlink a
counter. This is needed because the value returned by 'counter.open()'
cannot be manipulated directly by the shm module anymore.

Updated core.link to use counter.delete(name). This fixes breakage in
the core.link selftest function.

Currently have a broken core.counter selftest. The new semantics are not
consistent with the original test case. selftest must be updated (TODO).
  • Loading branch information
lukego committed Jul 22, 2015
1 parent f69b42c commit 4bc6784
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
24 changes: 20 additions & 4 deletions src/core/counter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,31 @@ local counter_t = ffi.typeof("struct counter")
-- to set-associative CPU cache). See SnabbCo/snabbswitch#558.
local public = {}
local private = {}
local numbers = {} -- name -> number

function open (name, readonly)
public[#public+1] = shm.map(name, counter_t, readonly)
if numbers[name] then error("counter already opened: " .. name) end
local n = #public+1
numbers[name] = n
public[n] = shm.map(name, counter_t, readonly)
if readonly then
private[#private+1] = public[#public] -- use counter directly
private[n] = public[#public] -- use counter directly
else
private[#private+1] = ffi.new(counter_t)
private[n] = ffi.new(counter_t)
end
return private[#private]
return private[n]
end

function delete (name)
local ptr = public[numbers[name]]
if not ptr then error("counter not found for deletion: " .. name) end
-- Free shm object
shm.unmap(ptr)
shm.unlink(name)
-- Free local state
numbers[name] = false
public[ptr] = false
private[ptr] = false
end

-- Copy counter private counter values to public shared memory.
Expand Down
13 changes: 6 additions & 7 deletions src/core/link.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,20 @@ local band = require("bit").band
local size = C.LINK_RING_SIZE -- NB: Huge slow-down if this is not local
max = C.LINK_MAX_PACKETS

local counternames = {"rxpackets", "txpackets", "rxbytes", "txbytes", "txdrop"}

function new (name)
local r = shm.map("links/"..name, "struct link")
for _, c
in ipairs({"rxpackets", "txpackets", "rxbytes", "txbytes", "txdrop"}) do
for _, c in ipairs(counternames) do
r.stats[c] = counter.open("counters/"..name.."/"..c)
end
return r
end

function free (r, name)
for _, c
in ipairs({"rxpackets", "txpackets", "rxbytes", "txbytes", "txdrop"}) do
shm.unmap(r.stats[c])
for _, c in ipairs(counternames) do
counter.delete("counters/"..name.."/"..c)
end
shm.unlink("counters/"..name)
shm.unmap(r)
shm.unlink("links/"..name)
end
Expand Down Expand Up @@ -92,7 +91,7 @@ end
function stats (r)
local stats = {}
for _, c
in ipairs({"rxpackets", "txpackets", "rxbytes", "txbytes", "txdrop"}) do
in ipairs(counternames) do
stats[c] = tonumber(counter.read(r.stats[c]))
end
return stats
Expand Down

0 comments on commit 4bc6784

Please sign in to comment.