Skip to content

Commit 5af8715

Browse files
authored
Implement system statistic functions into @lute/system (#283)
Closes #194.
1 parent 03765f4 commit 5af8715

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

examples/system_lib.luau

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
local system = require("@lute/system")
22

3-
print(system.os)
4-
print(system.arch)
5-
print(system.threadcount())
3+
print(
4+
string.format(
5+
"%s (%s-%s) - %d cores, uptime for %ss | freemem: %dMB | totalmem: %dMB",
6+
system.hostname(),
7+
system.os,
8+
system.arch,
9+
system.threadcount(),
10+
system.uptime(),
11+
system.freememory(),
12+
system.totalmemory()
13+
)
14+
)
615

716
for i, cpu in system.cpus() do
817
-- print(`[core {string.format("%.02i", i)}] speed: {cpu.speed}, model: {cpu.model}`)

system/include/lute/system.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ static const char kOperatingSystemProperty[] = "os";
1717

1818
int lua_cpus(lua_State* L);
1919
int lua_threadcount(lua_State* L);
20+
int lua_freememory(lua_State* L);
21+
int lua_totalmemory(lua_State* L);
22+
int lua_hostname(lua_State* L);
23+
int lua_uptime(lua_State* L);
2024

2125
static const luaL_Reg lib[] = {
2226
{"cpus", lua_cpus},
2327
{"threadcount", lua_threadcount},
28+
{"freememory", lua_freememory},
29+
{"totalmemory", lua_totalmemory},
30+
{"hostname", lua_hostname},
31+
{"uptime", lua_uptime},
2432

2533
{nullptr, nullptr}
2634
};

system/src/system.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "lute/system.h"
22
#include "lua.h"
3+
#include "lualib.h"
34
#include "uv.h"
5+
#include <cstddef>
6+
#include <cstdio>
7+
#include <cstdlib>
48
#include <iterator>
59
#include <string>
610
#include <vector>
@@ -66,6 +70,59 @@ int lua_threadcount(lua_State* L)
6670
return 1;
6771
}
6872

73+
constexpr size_t BYTES_PER_MB = 1024 * 1024; // 2^20 bytes
74+
75+
int lua_freememory(lua_State* L)
76+
{
77+
lua_pushnumber(L, static_cast<double>(uv_get_free_memory()) / BYTES_PER_MB);
78+
79+
return 1;
80+
}
81+
82+
int lua_totalmemory(lua_State* L)
83+
{
84+
lua_pushnumber(L, static_cast<double>(uv_get_total_memory()) / BYTES_PER_MB);
85+
86+
return 1;
87+
}
88+
89+
int lua_hostname(lua_State* L)
90+
{
91+
size_t sz = 255;
92+
std::string hostname;
93+
hostname.reserve(sz);
94+
95+
int res = uv_os_gethostname(hostname.data(), &sz);
96+
if (res == UV_ENOBUFS)
97+
{
98+
hostname.reserve(sz); // libuv updates the size to what's required
99+
res = uv_os_gethostname(hostname.data(), &sz);
100+
}
101+
102+
if (res != 0)
103+
{
104+
luaL_error(L, "libuv error: %s", uv_strerror(res));
105+
}
106+
107+
lua_pushstring(L, hostname.c_str());
108+
109+
return 1;
110+
}
111+
112+
int lua_uptime(lua_State* L)
113+
{
114+
double uptime = 0;
115+
116+
int res = uv_uptime(&uptime);
117+
if (res != 0)
118+
{
119+
luaL_error(L, "libuv error: %s", uv_strerror(res));
120+
}
121+
122+
lua_pushnumber(L, uptime);
123+
124+
return 1;
125+
}
69126
} // namespace libsystem
70127

71128
int luaopen_system(lua_State* L)

0 commit comments

Comments
 (0)