summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2022-07-27 22:44:21 +0800
committerruki <[email protected]>2022-07-27 22:44:21 +0800
commit557b2bf7b8326ecc440db1d67617bc8bae62959d (patch)
tree874de72378ff0ce16228131159926b63142db12e
parentf7d9c1714f894fa04077cebbe7299eaa9b8070bf (diff)
add os.meminfo
-rw-r--r--core/src/xmake/engine.c2
-rw-r--r--core/src/xmake/makefile1
-rw-r--r--core/src/xmake/os/meminfo.c51
-rw-r--r--xmake/core/base/memory.lua34
-rw-r--r--xmake/core/base/os.lua24
-rw-r--r--xmake/core/sandbox/modules/import/core/base/memory.lua24
-rw-r--r--xmake/core/sandbox/modules/os.lua1
7 files changed, 128 insertions, 9 deletions
diff --git a/core/src/xmake/engine.c b/core/src/xmake/engine.c
index 7c904fb38..6458dee1c 100644
--- a/core/src/xmake/engine.c
+++ b/core/src/xmake/engine.c
@@ -106,6 +106,7 @@ tb_int_t xm_os_setenv(lua_State* lua);
tb_int_t xm_os_getenv(lua_State* lua);
tb_int_t xm_os_getenvs(lua_State* lua);
tb_int_t xm_os_cpuinfo(lua_State* lua);
+tb_int_t xm_os_meminfo(lua_State* lua);
tb_int_t xm_os_readlink(lua_State* lua);
tb_int_t xm_os_filesize(lua_State* lua);
tb_int_t xm_os_emptydir(lua_State* lua);
@@ -326,6 +327,7 @@ static luaL_Reg const g_os_functions[] =
, { "getenv", xm_os_getenv }
, { "getenvs", xm_os_getenvs }
, { "cpuinfo", xm_os_cpuinfo }
+, { "meminfo", xm_os_meminfo }
, { "readlink", xm_os_readlink }
, { "emptydir", xm_os_emptydir }
, { "strerror", xm_os_strerror }
diff --git a/core/src/xmake/makefile b/core/src/xmake/makefile
index 5d017f0b0..44fb9a0b3 100644
--- a/core/src/xmake/makefile
+++ b/core/src/xmake/makefile
@@ -38,6 +38,7 @@ xmake_C_FILES += \
os/getenv \
os/getenvs \
os/cpuinfo \
+ os/meminfo \
os/readlink \
os/emptydir \
os/syserror \
diff --git a/core/src/xmake/os/meminfo.c b/core/src/xmake/os/meminfo.c
new file mode 100644
index 000000000..904d0797f
--- /dev/null
+++ b/core/src/xmake/os/meminfo.c
@@ -0,0 +1,51 @@
+/*!A cross-platform build utility based on Lua
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Copyright (C) 2015-present, TBOOX Open Source Group.
+ *
+ * @author ruki
+ * @file meminfo.c
+ *
+ */
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * trace
+ */
+#define TB_TRACE_MODULE_NAME "meminfo"
+#define TB_TRACE_MODULE_DEBUG (0)
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * includes
+ */
+#include "prefix.h"
+
+/* //////////////////////////////////////////////////////////////////////////////////////
+ * implementation
+ */
+
+tb_int_t xm_os_meminfo(lua_State* lua)
+{
+ // check
+ tb_assert_and_check_return_val(lua, 0);
+
+ lua_newtable(lua);
+
+ // get pagesize
+ tb_int_t pagesize = (tb_int_t)tb_page_size();
+ lua_pushstring(lua, "pagesize");
+ lua_pushinteger(lua, pagesize > 0? pagesize : 1);
+ lua_settable(lua, -3);
+
+ return 1;
+}
diff --git a/xmake/core/base/memory.lua b/xmake/core/base/memory.lua
new file mode 100644
index 000000000..da6f4b1af
--- /dev/null
+++ b/xmake/core/base/memory.lua
@@ -0,0 +1,34 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file memory.lua
+--
+
+-- define module
+local memory = memory or {}
+
+-- load modules
+local os = require("base/os")
+
+-- get memory info
+function memory.info(name)
+ local meminfo = os._meminfo()
+ return name and meminfo[name] or meminfo
+end
+
+-- return module
+return memory
diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua
index 392047a39..7dd0f3aad 100644
--- a/xmake/core/base/os.lua
+++ b/xmake/core/base/os.lua
@@ -33,15 +33,16 @@ local process = require("base/process")
-- save original interfaces
os._uid = os._uid or os.uid
os._gid = os._gid or os.gid
-os._getpid = os._getpid or os.getpid
-os._exit = os._exit or os.exit
-os._mkdir = os._mkdir or os.mkdir
-os._rmdir = os._rmdir or os.rmdir
-os._tmpdir = os._tmpdir or os.tmpdir
-os._setenv = os._setenv or os.setenv
-os._getenvs = os._getenvs or os.getenvs
-os._cpuinfo = os._cpuinfo or os.cpuinfo
-os._readlink = os._readlink or os.readlink
+os._getpid = os._getpid or os.getpid
+os._exit = os._exit or os.exit
+os._mkdir = os._mkdir or os.mkdir
+os._rmdir = os._rmdir or os.rmdir
+os._tmpdir = os._tmpdir or os.tmpdir
+os._setenv = os._setenv or os.setenv
+os._getenvs = os._getenvs or os.getenvs
+os._cpuinfo = os._cpuinfo or os.cpuinfo
+os._meminfo = os._meminfo or os.meminfo
+os._readlink = os._readlink or os.readlink
-- syserror code
os.SYSERR_UNKNOWN = -1
@@ -1284,6 +1285,11 @@ function os.cpuinfo(name)
return require("base/cpu").info(name)
end
+-- get memory info
+function os.meminfo(name)
+ return require("base/memory").info(name)
+end
+
-- get the default parallel jobs number
function os.default_njob()
local njob = math.ceil(os.cpuinfo().ncpu * 3 / 2)
diff --git a/xmake/core/sandbox/modules/import/core/base/memory.lua b/xmake/core/sandbox/modules/import/core/base/memory.lua
new file mode 100644
index 000000000..b8fffbad6
--- /dev/null
+++ b/xmake/core/sandbox/modules/import/core/base/memory.lua
@@ -0,0 +1,24 @@
+--!A cross-platform build utility based on Lua
+--
+-- Licensed under the Apache License, Version 2.0 (the "License");
+-- you may not use this file except in compliance with the License.
+-- You may obtain a copy of the License at
+--
+-- http://www.apache.org/licenses/LICENSE-2.0
+--
+-- Unless required by applicable law or agreed to in writing, software
+-- distributed under the License is distributed on an "AS IS" BASIS,
+-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+-- See the License for the specific language governing permissions and
+-- limitations under the License.
+--
+-- Copyright (C) 2015-present, TBOOX Open Source Group.
+--
+-- @author ruki
+-- @file memory.lua
+--
+
+-- load modules
+return require("base/memory")
+
+
diff --git a/xmake/core/sandbox/modules/os.lua b/xmake/core/sandbox/modules/os.lua
index df47653b9..4c2509cc9 100644
--- a/xmake/core/sandbox/modules/os.lua
+++ b/xmake/core/sandbox/modules/os.lua
@@ -65,6 +65,7 @@ sandbox_os.joinenvs = os.joinenvs
sandbox_os.pbpaste = os.pbpaste
sandbox_os.pbcopy = os.pbcopy
sandbox_os.cpuinfo = os.cpuinfo
+sandbox_os.meminfo = os.meminfo
sandbox_os.default_njob = os.default_njob
sandbox_os.emptydir = os.emptydir
sandbox_os.filesize = os.filesize