|
| 1 | +#include "lute/system.h" |
| 2 | +#include "lua.h" |
| 3 | +#include "uv.h" |
| 4 | +#include <iterator> |
| 5 | +#include <string> |
| 6 | +#include <vector> |
| 7 | + |
| 8 | +int luaopen_system(lua_State* L) |
| 9 | +{ |
| 10 | + luteopen_system(L); |
| 11 | + lua_setglobal(L, "system"); |
| 12 | + |
| 13 | + return 1; |
| 14 | +} |
| 15 | + |
| 16 | +int luteopen_system(lua_State* L) |
| 17 | +{ |
| 18 | + lua_createtable(L, 0, std::size(system_lib::lib) + std::size(system_lib::properties)); |
| 19 | + |
| 20 | + for (auto& [name, func] : system_lib::lib) |
| 21 | + { |
| 22 | + if (!name || !func) |
| 23 | + break; |
| 24 | + |
| 25 | + lua_pushcfunction(L, func, name); |
| 26 | + lua_setfield(L, -2, name); |
| 27 | + } |
| 28 | + |
| 29 | + // os |
| 30 | + uv_utsname_t sysinfo; |
| 31 | + uv_os_uname(&sysinfo); |
| 32 | + |
| 33 | + lua_pushstring(L, sysinfo.sysname); |
| 34 | + lua_setfield(L, -2, kOperatingSystemProperty); |
| 35 | + |
| 36 | + lua_pushstring(L, sysinfo.machine); |
| 37 | + lua_setfield(L, -2, kArchitectureProperty); |
| 38 | + |
| 39 | + lua_setreadonly(L, -1, 1); |
| 40 | + |
| 41 | + return 1; |
| 42 | +} |
| 43 | + |
| 44 | +namespace system_lib |
| 45 | +{ |
| 46 | +int lua_cpus(lua_State* L) |
| 47 | +{ |
| 48 | + int count = uv_available_parallelism(); |
| 49 | + uv_cpu_info_t* cpus; |
| 50 | + |
| 51 | + uv_cpu_info(&cpus, &count); |
| 52 | + |
| 53 | + lua_createtable(L, count, 0); |
| 54 | + |
| 55 | + int j = 0; |
| 56 | + for (int i = 0; i < count; i++) |
| 57 | + { |
| 58 | + lua_pushinteger(L, ++j); |
| 59 | + |
| 60 | + auto cpuInfo = cpus[i]; |
| 61 | + |
| 62 | + // model, speed, times |
| 63 | + lua_createtable(L, 0, 2); |
| 64 | + |
| 65 | + lua_pushstring(L, cpuInfo.model); |
| 66 | + lua_setfield(L, -2, "model"); |
| 67 | + |
| 68 | + lua_pushinteger(L, (int)cpuInfo.speed); |
| 69 | + lua_setfield(L, -2, "speed"); |
| 70 | + |
| 71 | + // sys, user, idle, irq, nice |
| 72 | + lua_createtable(L, 0, 5); |
| 73 | + |
| 74 | + // cast to double cuz uint64 has higher max n and lua numbers are 52bit |
| 75 | + lua_pushnumber(L, static_cast<double>(cpuInfo.cpu_times.sys)); |
| 76 | + lua_setfield(L, -2, "sys"); |
| 77 | + |
| 78 | + lua_pushnumber(L, static_cast<double>(cpuInfo.cpu_times.idle)); |
| 79 | + lua_setfield(L, -2, "idle"); |
| 80 | + |
| 81 | + lua_pushnumber(L, static_cast<double>(cpuInfo.cpu_times.irq)); |
| 82 | + lua_setfield(L, -2, "irq"); |
| 83 | + |
| 84 | + lua_pushnumber(L, static_cast<double>(cpuInfo.cpu_times.nice)); |
| 85 | + lua_setfield(L, -2, "nice"); |
| 86 | + |
| 87 | + lua_pushnumber(L, static_cast<double>(cpuInfo.cpu_times.user)); |
| 88 | + lua_setfield(L, -2, "user"); |
| 89 | + |
| 90 | + lua_setfield(L, -2, "times"); |
| 91 | + |
| 92 | + lua_settable(L, -3); |
| 93 | + }; |
| 94 | + |
| 95 | + return 1; |
| 96 | +} |
| 97 | + |
| 98 | +int lua_threadcount(lua_State* L) |
| 99 | +{ |
| 100 | + lua_pushinteger(L, uv_available_parallelism()); |
| 101 | + |
| 102 | + return 1; |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace system_lib |
0 commit comments