Skip to content

Commit

Permalink
Add snabbmark checksum subprogram
Browse files Browse the repository at this point in the history
  • Loading branch information
dpino committed Aug 23, 2018
1 parent 24ee9fb commit 4ab4551
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 35 deletions.
43 changes: 8 additions & 35 deletions src/arch/checksum.dasl
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ end
checksum = assemble("checksum", "uint32_t(*)(uint8_t*, uint32_t, uint16_t)", gen_checksum())

function selftest ()
print("selftest: checksum")
require("lib.checksum_h")
local function hex (num)
return ("0x%.2x"):format(num)
end
local function create_packet (size)
local pkt = {
data = ffi.new("uint8_t[?]", size),
Expand All @@ -147,41 +151,10 @@ function selftest ()
end
return pkt
end
local function benchmark (fn, times)
local now = os.clock()
local csum
for i=1,times do
csum, pkt = fn()
end
local elapse = os.clock() - now
local ns_per_csum = elapse * 10e9 / times
local ns_per_byte = ns_per_csum / pkt.length
local ret = {elapse = elapse, ns_per_csum = ns_per_csum, ns_per_byte = ns_per_byte, csum = csum}
return ("elapse: %.6f; ns_per_csum: %.2f; ns_per_byte: %.2f"):format(ret.elapse, ret.ns_per_csum, ret.ns_per_byte)
end
local function benchmark_report (size, mpps)
local times = mpps*10^6
local ntohs = lib.ntohs
for size=44,1500 do
local pkt = create_packet(size)
print(mpps.."M; "..size.." bytes")
-- Benchmark for different architectures.
print("C: ", benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0), pkt end, times))
print("ASM: ", benchmark(function() return checksum(pkt.data, pkt.length, 0), pkt end, times))
assert(hex(checksum(pkt.data, pkt.length, 0)) == hex(ntohs(checksum_lua(pkt.data, pkt.length))))
assert(hex(checksum(pkt.data, pkt.length, 0)) == hex(C.cksum_generic(pkt.data, pkt.length, 0)))
end
local function verify_correctness ()
local function hex (num)
return ("0x%.2x"):format(num)
end
local ntohs = lib.ntohs
for size=44,1500 do
local pkt = create_packet(size)
assert(hex(checksum(pkt.data, pkt.length, 0)) == hex(ntohs(checksum_lua(pkt.data, pkt.length))))
assert(hex(checksum(pkt.data, pkt.length, 0)) == hex(C.cksum_generic(pkt.data, pkt.length, 0)))
end
end

print("selftest: checksum")
verify_correctness()
benchmark_report(44, 14.4)
benchmark_report(550, 2)
benchmark_report(1500, 1)
end
3 changes: 3 additions & 0 deletions src/program/snabbmark/README
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ Usage:

snabbmark ctable
Benchmark insertion and lookup for the "ctable" data structure.

snabbmark basic1 <npackets>
Benchmark checksum computation implementations in C and DynASM.
40 changes: 40 additions & 0 deletions src/program/snabbmark/snabbmark.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ function run (args)
hash(unpack(args))
elseif command == 'ctable' and #args == 0 then
ctable(unpack(args))
elseif command == 'checksum' and #args == 0 then
checksum_bench(unpack(args))
else
print(usage)
main.exit(1)
Expand Down Expand Up @@ -571,3 +573,41 @@ function ctable ()
stride = stride * 2
until stride > 256
end

function checksum_bench ()
require("lib.checksum_h")
local checksum = require('arch.checksum').checksum
local function create_packet (size)
local pkt = {
data = ffi.new("uint8_t[?]", size),
length = size
}
for i=0,size-1 do
pkt.data[i] = math.random(255)
end
return pkt
end
local function benchmark (fn, times)
local now = os.clock()
local csum
for i=1,times do
csum, pkt = fn()
end
local elapse = os.clock() - now
local ns_per_csum = elapse * 10e9 / times
local ns_per_byte = ns_per_csum / pkt.length
local ret = {elapse = elapse, ns_per_csum = ns_per_csum, ns_per_byte = ns_per_byte, csum = csum}
return ("%.2f ns per checksum; %.2f ns per byte"):format(ret.ns_per_csum, ret.ns_per_byte)
end
local function benchmark_report (size, mpps)
local times = mpps*10^6
local pkt = create_packet(size)
io.stdout:write("C: ", "Size="..size.." bytes".."; MPPS="..mpps.."M; ")
print(benchmark(function() return C.cksum_generic(pkt.data, pkt.length, 0), pkt end, times))
io.stdout:write("ASM: ", "Size="..size.." bytes".."; MPPS="..mpps.."M; ")
print(benchmark(function() return checksum(pkt.data, pkt.length, 0), pkt end, times))
end
benchmark_report(44, 14.4)
benchmark_report(550, 2)
benchmark_report(1516, 1)
end

0 comments on commit 4ab4551

Please sign in to comment.