Skip to content
View 0checksum.lua
local ffi = require("ffi")
local C = ffi.C
local pmu = require("lib.pmu")
require("lib.checksum")
-- Use /etc/passwd contents as a dummy packet
local data = core.lib.readfile("/etc/passwd", "*all")
local ptr = ffi.cast("unsigned char *", data), #data
local len = #data
local loops = 100000
View readme.txt
CPU PMU (Performance Monitoring Unit) support
Howdy!
I have geeked out on a new piece of hardware :-)
This time it is the Performance Monitoring Unit built into the CPU. This is a hardware capability to track fine-grained events inside the processor and give visibility into things like cache misses, branch mispredictions, utilization of internal CPU resources,
Turns out that you only need two special CPU instructions to drive this - WMSR to setup a counter, RDPMC to read it - and a simple but interesting benchmarking tool is only 500 lines of code. This was also a good opportunity to use our new ability to write Lua code that generates machine code at runtime.
View pmu.lua
-- This module counts and reports on CPU events such as cache misses,
-- branch mispredictions, utilization of internal CPU resources such
-- as execution units, and so on.
--
-- Hundreds of low-level counters are available. The exact list
-- depends on CPU model. See pmu_cpu.lua for our definitions.
--
-- API:
View 0readme.md
View 0readme
This is a quick prototype for generating a Lua module that describes Intel's Performance Monitoring Counters.
First download the raw data:
wget -r --no-parent https://download.01.org/perfmon/
Then run the script (attached) to get the output (attached).
Looks like this compiles with "luajit -b" to 211KB of object code. Shrinks to 16KB with gzip.
View First version dump
---- TRACE 3 start script.lua:5
0012 TGETS 7 0 4 ; "add"
0013 MOV 8 2
0014 KSHORT 9 1
0015 CALL 7 1 3
0000 . FUNCF 4 ; counter.lua:80
0001 . TGETS 2 0 0 ; "c"
0000 . . . FUNCC ; ffi.meta.__index
0002 . ISTC 3 1
0003 . JMP 3 => 0005
View Dokerfile
FROM ubuntu:14.04
MAINTAINER Luke Gorrie
# Install the latest versions of all the packages that we depend on
RUN apt-get update
RUN apt-get install -y build-essential gcc pkg-config glib-2.0 libglib2.0-dev libsdl1.2-dev libaio-dev libcap-dev libattr1-dev libpixman-1-dev libncurses5 libncurses5-dev git telnet tmux numactl wget
# Download and compile Snabb Switch
RUN git clone https://github.com/SnabbCo/snabbswitch && cd snabbswitch && make
View dump
---- TRACE 3 start counter.lua:32
0001 TGETS 2 0 0 ; "c"
0000 . . FUNCC ; ffi.meta.__index
0002 ADDVV 2 2 1
0000 . . FUNCC ; ffi.meta.__add
0003 TSETS 2 0 0 ; "c"
0000 . . FUNCC ; ffi.meta.__newindex
0004 RET0 0 1
---- TRACE 3 IR
0001 > cdt SLOAD #1 T
View README.md

This is a simple example of a low-level script that uses the Snabb Switch 82599 10G ethernet device driver directly to feed packets into a C function provided as a shared library. Written for a discussion with Pavel Odintsov.

Here is now to run it:

  1. Compile snabbswitch to get snabb executable.
  2. Compile a C callback function to a shared library e.g. capturecallback.c below.
  3. Run: sudo ./snabb snsh capture2c <pciaddress> <filename.so>

The output should look something like this:

View tap-test.lua
-- tap-test.lua -- test program based on ljsyscall test code
-- Reference example code for ljsyscall here:
-- https://github.com/justincormack/ljsyscall/blob/master/test/linux.lua
-- Run via Snabb Switch like this:
-- sudo snabb snsh tap-test.lua
local syscall = require("syscall") -- ljsyscall
-- Create a new tap device
Something went wrong with that request. Please try again.