Skip to content

Commit 329433b

Browse files
committed
fix systrack synchronization
* change luaprobe to sleep=false * split systrack into two runtimes (driver and probes)
1 parent 0bc550b commit 329433b

File tree

5 files changed

+47
-24
lines changed

5 files changed

+47
-24
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ examples_install:
5454
${INSTALL} -m 0644 examples/dnsblock/*.lua ${SCRIPTS_INSTALL_PATH}/examples/dnsblock
5555
${MKDIR} ${SCRIPTS_INSTALL_PATH}/examples/dnsdoctor
5656
${INSTALL} -m 0644 examples/dnsdoctor/*.lua ${SCRIPTS_INSTALL_PATH}/examples/dnsdoctor
57+
${MKDIR} ${SCRIPTS_INSTALL_PATH}/examples/systrack
58+
${INSTALL} -m 0644 examples/systrack/*.lua ${SCRIPTS_INSTALL_PATH}/examples/systrack
5759

5860
examples_uninstall:
5961
${RM} -r ${SCRIPTS_INSTALL_PATH}/examples

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,8 +1469,7 @@ hello kernel!
14691469
14701470
[systrack](examples/systrack.lua)
14711471
is a kernel script that implements a device driver to monitor system calls.
1472-
It prints the amount of times each [system call](examples/systrack.lua#L29)
1473-
was called since the driver has been installed.
1472+
It prints the amount of times each system call was called since the driver has been installed.
14741473
14751474
#### Usage
14761475
@@ -1484,6 +1483,7 @@ write: 1085
14841483
openat: 2036
14851484
read: 4131
14861485
readv: 0
1486+
...
14871487
```
14881488
14891489
### filter

examples/systrack.lua

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,32 @@
33
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
44
--
55

6-
local linux = require("linux")
7-
local probe = require("probe")
8-
local device = require("device")
9-
local systab = require("syscall.table")
10-
11-
local syscalls = {"openat", "read", "write", "readv", "writev", "close"}
6+
local lunatik = require("lunatik")
7+
local runner = require("lunatik.runner")
8+
local linux = require("linux")
9+
local device = require("device")
10+
local rcu = require("rcu")
1211

1312
local function nop() end -- do nothing
1413

1514
local s = linux.stat
1615
local driver = {name = "systrack", open = nop, release = nop, mode = s.IRUGO}
1716

18-
local track = {}
17+
local systrack = rcu.table()
18+
lunatik._ENV.systrack = systrack
19+
1920
local toggle = true
2021
function driver:read()
2122
local log = ""
2223
if toggle then
23-
for symbol, counter in pairs(track) do
24-
log = log .. string.format("%s: %d\n", symbol, counter)
25-
end
24+
rcu.map(systrack, function (symbol, counter)
25+
log = log .. string.format("%s: %d\n", symbol, counter:getnumber(0))
26+
end)
2627
end
2728
toggle = not toggle
2829
return log
2930
end
3031

31-
for _, symbol in ipairs(syscalls) do
32-
local address = systab[symbol]
33-
track[symbol] = 0
34-
35-
local function handler()
36-
track[symbol] = track[symbol] + 1
37-
end
38-
39-
probe.new(address, {pre = handler, post = nop})
40-
end
41-
32+
runner.run("examples/systrack/probes", false)
4233
device.new(driver)
4334

examples/systrack/probes.lua

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--
2+
-- SPDX-FileCopyrightText: (c) 2023-2024 Ring Zero Desenvolvimento de Software LTDA
3+
-- SPDX-License-Identifier: MIT OR GPL-2.0-only
4+
--
5+
6+
local lunatik = require("lunatik")
7+
local probe = require("probe")
8+
local syscall = require("syscall.table")
9+
local data = require("data")
10+
11+
local systrack = lunatik._ENV.systrack
12+
13+
local function nop() end -- do nothing
14+
15+
local function inc(counter)
16+
counter:setnumber(0, counter:getnumber(0) + 1)
17+
end
18+
19+
local sizeofnumber = string.packsize("n")
20+
21+
for symbol, address in pairs(syscall) do
22+
systrack[symbol] = data.new(sizeofnumber)
23+
24+
local function handler()
25+
inc(systrack[symbol])
26+
end
27+
28+
probe.new(address, {pre = handler, post = nop})
29+
end
30+

lib/luaprobe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static const lunatik_class_t luaprobe_class = {
167167
.name = "probe",
168168
.methods = luaprobe_mt,
169169
.release = luaprobe_release,
170-
.sleep = true,
170+
.sleep = false,
171171
};
172172

173173
static int luaprobe_new(lua_State *L)

0 commit comments

Comments
 (0)