summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-28 00:43:54 +0800
committerruki <[email protected]>2022-07-28 00:43:54 +0800
commit48ed21d6ac693e569cfb31d28ddcfbe1274e1d7d (patch)
treedba25e99a5c1d7db8dcc570df2f688a46008f94e
parent43a82d1944819b465d06724abff535f3a4659de6 (diff)
add meminfo
-rw-r--r--core/src/xmake/os/meminfo.c85
-rw-r--r--xmake/core/base/memory.lua5
2 files changed, 56 insertions, 34 deletions
diff --git a/core/src/xmake/os/meminfo.c b/core/src/xmake/os/meminfo.c
index a9a1d9719..4729664f1 100644
--- a/core/src/xmake/os/meminfo.c
+++ b/core/src/xmake/os/meminfo.c
@@ -30,22 +30,31 @@
*/
#include "prefix.h"
#if defined(TB_CONFIG_OS_MACOSX)
-# include <sys/sysctl.h>
# include <mach/mach.h>
# include <mach/machine.h>
+# include <mach/vm_statistics.h>
# include <mach-o/dyld.h>
# include <mach-o/nlist.h>
+#elif defined(TB_CONFIG_OS_LINUX)
+# include <sys/sysinfo.h>
#endif
/* //////////////////////////////////////////////////////////////////////////////////////
* private implementation
*/
+#ifdef TB_CONFIG_OS_LINUX
+static tb_int64_t xm_os_meminfo_get_value(tb_char_t const* buffer, tb_char_t const* name)
+{
+ tb_char_t const* p = tb_strstr(buffer, name);
+ return p? tb_stoi64(p + tb_strlen(name)) : 0;
+}
+#endif
// get the used memory size (MB)
-static tb_bool_t xm_os_meminfo_vmstats(tb_int_t* vm_totalsize, tb_int_t* vm_availsize)
+static tb_bool_t xm_os_meminfo_stats(tb_int_t* ptotalsize, tb_int_t* pavailsize)
{
#if defined(TB_CONFIG_OS_MACOSX)
- vm_statistics64_data_t vmstat;
+ statistics64_data_t vmstat;
mach_msg_type_number_t count = HOST_VM_INFO64_COUNT;
if (host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info_t) &vmstat, &count) == KERN_SUCCESS)
{
@@ -59,27 +68,45 @@ static tb_bool_t xm_os_meminfo_vmstats(tb_int_t* vm_totalsize, tb_int_t* vm_avai
*
*/
tb_int64_t availsize = (tb_int64_t)(vmstat.inactive_count + vmstat.free_count - vmstat.speculative_count) * pagesize;
- *vm_totalsize = (tb_int_t)(totalsize / (1024 * 1024));
- *vm_availsize = (tb_int_t)(availsize / (1024 * 1024));
+ *ptotalsize = (tb_int_t)(totalsize / (1024 * 1024));
+ *pavailsize = (tb_int_t)(availsize / (1024 * 1024));
return tb_true;
}
+#elif defined(TB_CONFIG_OS_LINUX)
+ /* we get meminfo from /proc/meminfo
+ *
+ * @see https://github.com/rfjakob/earlyoom/blob/cba1d599e4a7484c45ac017aa7702ff879f15846/meminfo.c#L52
+ */
+ if (tb_file_info("/proc/meminfo", tb_null))
+ {
+ FILE* fp = fopen("/proc/meminfo", "r");
+ if (fp)
+ {
+ // 8192 should be enough for the foreseeable future.
+ tb_char_t buffer[8192];
+ size_t len = fread(buffer, 1, sizeof(buffer) - 1, fp);
+ if (!ferror(fp) && len)
+ {
+ tb_int64_t totalsize = xm_os_meminfo_get_value(buffer, "MemTotal:");
+ tb_trace_i("totalsize: %lld", totalsize);
+ }
+ fclose(fp);
+ }
+ }
+ else
+ {
+ struct sysinfo info = {0};
+ if (sysinfo(&info) == 0)
+ {
+ *ptotalsize = (tb_int_t)(info.totalram / (1024 * 1024));
+ *pavailsize = (tb_int_t)((info.freeram + info.bufferram/* + cache size */) / (1024 * 1024));
+ return tb_true;
+ }
+ }
#endif
return tb_false;
}
-// get the total physical memory size (MB)
-static tb_int_t xm_os_meminfo_physmem()
-{
-#if defined(TB_CONFIG_OS_MACOSX)
- tb_int_t mib[] = { CTL_HW, HW_MEMSIZE };
- tb_int64_t value = 0;
- size_t length = sizeof(value);
- if (sysctl(mib, 2, &value, &length, tb_null, 0) == 0)
- return (tb_int_t)(value / (1024 * 1024));
-#endif
- return 0;
-}
-
/* //////////////////////////////////////////////////////////////////////////////////////
* implementation
*/
@@ -97,23 +124,17 @@ tb_int_t xm_os_meminfo(lua_State* lua)
lua_pushinteger(lua, pagesize);
lua_settable(lua, -3);
- // get the total memory size (MB)
- tb_int_t physmem = xm_os_meminfo_physmem();
- lua_pushstring(lua, "physmem");
- lua_pushinteger(lua, physmem);
- lua_settable(lua, -3);
-
- // get the vm memory size (MB)
- tb_int_t vm_availsize = 0;
- tb_int_t vm_totalsize = 0;
- if (xm_os_meminfo_vmstats(&vm_totalsize, &vm_availsize))
+ // get the memory size (MB)
+ tb_int_t availsize = 0;
+ tb_int_t totalsize = 0;
+ if (xm_os_meminfo_stats(&totalsize, &availsize))
{
- lua_pushstring(lua, "vm_totalsize");
- lua_pushinteger(lua, vm_totalsize);
+ lua_pushstring(lua, "totalsize");
+ lua_pushinteger(lua, totalsize);
lua_settable(lua, -3);
- lua_pushstring(lua, "vm_availsize");
- lua_pushinteger(lua, vm_availsize);
+ lua_pushstring(lua, "availsize");
+ lua_pushinteger(lua, availsize);
lua_settable(lua, -3);
}
diff --git a/xmake/core/base/memory.lua b/xmake/core/base/memory.lua
index 5545e8c08..53b6fe65e 100644
--- a/xmake/core/base/memory.lua
+++ b/xmake/core/base/memory.lua
@@ -26,9 +26,10 @@ local os = require("base/os")
-- get memory info
function memory.info(name)
+ -- TODO we need cache per 10s
local meminfo = os._meminfo()
- if meminfo.vm_totalsize and meminfo.vm_availsize then
- meminfo.vm_usagerate = (meminfo.vm_totalsize - meminfo.vm_availsize) / meminfo.vm_totalsize
+ if meminfo.totalsize and meminfo.availsize then
+ meminfo.usagerate = (meminfo.totalsize - meminfo.availsize) / meminfo.totalsize
end
return name and meminfo[name] or meminfo
end