Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Kbuild
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ obj-$(CONFIG_LUNATIK_CRYPTO_SKCIPHER) += lib/luacrypto_skcipher.o
obj-$(CONFIG_LUNATIK_CRYPTO_AEAD) += lib/luacrypto_aead.o
obj-$(CONFIG_LUNATIK_CRYPTO_RNG) += lib/luacrypto_rng.o
obj-$(CONFIG_LUNATIK_CRYPTO_COMP) += lib/luacrypto_comp.o
obj-$(CONFIG_LUNATIK_CPU) += lib/luacpu.o

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ all: lunatik_sym.h
CONFIG_LUNATIK_NETFILTER=m CONFIG_LUNATIK_COMPLETION=m \
CONFIG_LUNATIK_CRYPTO_SHASH=m CONFIG_LUNATIK_CRYPTO_SKCIPHER=m \
CONFIG_LUNATIK_CRYPTO_AEAD=m CONFIG_LUNATIK_CRYPTO_RNG=m \
CONFIG_LUNATIK_CRYPTO_COMP=m
CONFIG_LUNATIK_CRYPTO_COMP=m CONFIG_LUNATIK_CPU=m

clean:
${MAKE} -C ${MODULES_BUILD_PATH} M=${PWD} clean
Expand Down
2 changes: 1 addition & 1 deletion bin/lunatik
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ local lunatik = {
modules = {"lunatik", "luadevice", "lualinux", "luanotifier", "luasocket", "luarcu",
"luathread", "luafib", "luadata", "luaprobe", "luasyscall", "luaxdp", "luafifo", "luaxtable",
"luanetfilter", "luacompletion", "luacrypto_shash", "luacrypto_skcipher", "luacrypto_aead",
"luacrypto_rng", "luacrypto_comp", "lunatik_run"}
"luacrypto_rng", "luacrypto_comp", "luacpu", "lunatik_run"},
}

function lunatik.prompt()
Expand Down
196 changes: 196 additions & 0 deletions lib/luacpu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
/*
* SPDX-FileCopyrightText: (c) 2025 Enderson Maia <[email protected]
* SPDX-License-Identifier: MIT OR GPL-2.0-only
*/

/***
* Linux CPU Lua interface.
*
* This module provides access to Linux's CPU abstractions.
*
* @module cpu
*/

#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/module.h>
#include <linux/cpumask.h>
#include <linux/kernel_stat.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <lunatik.h>

#define LUACPU_NUM(name) \
static int luacpu_num_##name(lua_State *L) { \
lua_pushinteger(L, (lua_Integer)num_##name##_cpus()); \
return 1; \
}

/***
* Returns the possible CPUs in the system.
*
* @function num_possible
* @treturn integer The number of possible CPUs in the system
*/
LUACPU_NUM(possible)

/***
* Returns the possible CPUs in the system.
*
* @function num_possible
* @treturn integer The number of possible CPUs in the system
*/
LUACPU_NUM(present)

/***
* Returns the possible CPUs in the system.
*
* @function num_possible
* @treturn integer The number of possible CPUs in the system
*/
LUACPU_NUM(online)

/***
* Macro to set CPU statistics in Lua table.
* @param L Lua state
* @param idx Index of the table in the Lua stack
* @param kcs Kernel CPU statistics structure (kernel_cpustat)
* @param name Field name in the Lua table
* @param NAME Corresponding C enum name for CPU time
*/
#define luacpu_setstat(L, idx, kcs, name, NAME) \
do { \
lua_pushinteger(L, (lua_Integer)kcs.cpustat[CPUTIME_##NAME]); \
lua_setfield(L, idx - 1, #name); \
} while (0)

/***
* Gets CPU statistics for a specific CPU.
* Fetches kernel CPU statistics including user, nice, system, idle, iowait,
* irq, softirq, steal, guest, guest_nice and forceidle times.
*
* @function stats
* @tparam integer cpu The CPU number (0-based) to query.
* @treturn table A table containing CPU time statistics with the following fields:
* @tfield integer user Time spent in user mode
* @tfield integer nice Time spent in user mode with low priority (nice)
* @tfield integer system Time spent in system mode
* @tfield integer idle Time spent in idle task
* @tfield integer iowait Time waiting for I/O to complete
* @tfield integer irq Time servicing hardware interrupts
* @tfield integer softirq Time servicing software interrupts
* @tfield integer steal Time stolen by other operating systems (virtualized environment)
* @tfield integer guest Time spent running a virtual CPU for guest OS
* @tfield integer guest_nice Time spent running a niced guest
* @tfield integer forceidle Time spent in forced idle (if CONFIG_SCHED_CORE is enabled)
* @raise Error if CPU is offline
* @usage
* local cpu0 = cpu.stats(0)
* print("CPU 0 user time:", cpu0.user)
* print("CPU 0 idle time:", cpu0.idle)
*/
static int luacpu_stats(lua_State *L)
{
unsigned int cpu = luaL_checkinteger(L, 1);
struct kernel_cpustat kcs;

luaL_argcheck(L, cpu_online(cpu), 1, "CPU is offline");

kcpustat_cpu_fetch(&kcs, cpu);

lua_createtable(L, 0, NR_STATS);

luacpu_setstat(L, -1, kcs, user, USER);
luacpu_setstat(L, -1, kcs, nice, NICE);
luacpu_setstat(L, -1, kcs, system, SYSTEM);
luacpu_setstat(L, -1, kcs, idle, IDLE);
luacpu_setstat(L, -1, kcs, iowait, IOWAIT);
luacpu_setstat(L, -1, kcs, irq, IRQ);
luacpu_setstat(L, -1, kcs, softirq, SOFTIRQ);
luacpu_setstat(L, -1, kcs, steal, STEAL);
luacpu_setstat(L, -1, kcs, guest, GUEST);
luacpu_setstat(L, -1, kcs, guest_nice, GUEST_NICE);
#ifdef CONFIG_SCHED_CORE
luacpu_setstat(L, -1, kcs, forceidle, FORCEIDLE);
#endif
return 1;
}

#define LUACPU_FOREACH(name) \
static int luacpu_foreach_##name(lua_State *L) { \
unsigned int cpu; \
luaL_checktype(L, 1, LUA_TFUNCTION); \
for_each_##name##_cpu(cpu) { \
lua_pushvalue(L, 1); \
lua_pushinteger(L, cpu); \
lua_call(L, 1, 0); \
} \
return 0; \
};

/***
* Iterates over all possible CPUs and calls a function for each.
*
* @function foreach_possible
* @tparam function callback Function to call for each possible CPU
* @usage
* cpu.foreach_possible(function(cpu_num)
* print("CPU", cpu.stats(cpu_num))
* end)
*/
LUACPU_FOREACH(possible)

/***
* Iterates over all present CPUs and calls a function for each.
*
* @function foreach_present
* @tparam function callback Function to call for each present CPU
* @usage
* cpu.foreach_present(function(cpu_num)
* print("CPU", cpu.stats(cpu_num))
* end)
*/
LUACPU_FOREACH(present)

/***
* Iterates over all online CPUs and calls a function for each.
*
* @function foreach_online
* @tparam function callback Function to call for each online CPU
* @usage
* cpu.foreach_online(function(cpu_num)
* print("CPU", cpu.stats(cpu_num))
* end)
*/
LUACPU_FOREACH(online)

static const luaL_Reg luacpu_lib[] = {
{"num_possible", luacpu_num_possible},
{"num_present", luacpu_num_present},
{"num_online", luacpu_num_online},
{"stats", luacpu_stats},
{"foreach_possible", luacpu_foreach_possible},
{"foreach_present", luacpu_foreach_present},
{"foreach_online", luacpu_foreach_online},
{NULL, NULL}
};

LUNATIK_NEWLIB(cpu, luacpu_lib, NULL, NULL);

static int __init luacpu_init(void)
{
return 0;
}

static void __exit luacpu_exit(void)
{
}

module_init(luacpu_init);
module_exit(luacpu_exit);
MODULE_LICENSE("Dual MIT/GPL");
MODULE_AUTHOR("Enderson Maia <[email protected]");
MODULE_DESCRIPTION("Lunatik interface to Linux's CPU abstractions.");