-
Notifications
You must be signed in to change notification settings - Fork 40
Add luacpu module #318
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add luacpu module #318
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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."); | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.