Skip to content

Commit

Permalink
Add os.getnumcpus function to get number of logical CPU cores.
Browse files Browse the repository at this point in the history
  • Loading branch information
tritao committed Sep 1, 2024
1 parent db072b4 commit 41f984f
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Bootstrap.mak
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ linux-clean: nix-clean

linux: linux-clean
mkdir -p build/bootstrap
$(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $(SRC) -lm -ldl -lrt -luuid
$(CC) -o build/bootstrap/premake_bootstrap -DPREMAKE_NO_BUILTIN_SCRIPTS -D_GNU_SOURCE -DLUA_USE_POSIX -DLUA_USE_DLOPEN -I"$(LUA_DIR)" -I"$(LUASHIM_DIR)" $(SRC) -lm -ldl -lrt -luuid
./build/bootstrap/premake_bootstrap embed
./build/bootstrap/premake_bootstrap --to=build/bootstrap gmake2
$(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` config=$(CONFIG)
Expand Down
4 changes: 4 additions & 0 deletions premake5.lua
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@
filter "system:linux or hurd"
links { "dl", "rt" }

filter "system:linux"
defines { "_GNU_SOURCE" }


filter { "system:not windows", "system:not macosx" }
if not _OPTIONS["no-curl"] then
links { "mbedtls-lib" }
Expand Down
84 changes: 84 additions & 0 deletions src/host/os_getnumcpus.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/**
* \file os_getcwd.c
* \brief Retrieve the current working directory.
* \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
*/

#include "premake.h"

#if PLATFORM_LINUX
#include <sched.h>
#elif PLATFORM_SOLARIS | PLATFORM_AIX | PLATFORM_MACOSX
#include <sys/sysctl.h>
#endif

int do_getnumcpus()
{
#if PLATFORM_WINDOWS
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
return sysinfo.dwNumberOfProcessors;
#elif PLATFORM_LINUX
cpu_set_t set;
int count, i;

if (sched_getaffinity(0, sizeof(cpu_set_t), &set) != -1)
{
count = 0;
for (i = 0; i < CPU_SETSIZE; i++)
{
if (CPU_ISSET(i, &set))
{
count++;
}
}

return count;
}
else
{
return 0;
}
#elif PLATFORM_SOLARIS | PLATFORM_AIX | PLATFORM_MACOSX
return sysconf(_SC_NPROCESSORS_ONLN);
#elif PLATFORM_BSD
int mib[4];
int numCPU;
size_t len = sizeof(numCPU);

/* set the mib for hw.ncpu */
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;

/* get the number of CPUs from the system */
sysctl(mib, 2, &numCPU, &len, NULL, 0);

if (numCPU < 1)
{
mib[1] = HW_NCPU;
sysctl(mib, 2, &numCPU, &len, NULL, 0);
if (numCPU < 1)
numCPU = 1;
}

return numCPU;
#else
#warning do_getnumcpus is not implemented for your platform yet
return 0;
#endif
}

int os_getnumcpus(lua_State* L)
{
int result = do_getnumcpus();
if (result > 0)
{
lua_pushinteger(L, result);
return 1;
}
else
{
return 0;
}
}

1 change: 1 addition & 0 deletions src/host/premake.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static const luaL_Reg os_functions[] = {
{ "_is64bit", os_is64bit },
{ "isdir", os_isdir },
{ "getcwd", os_getcwd },
{ "getnumcpus", os_getnumcpus },
{ "getpass", os_getpass },
{ "getWindowsRegistry", os_getWindowsRegistry },
{ "listWindowsRegistry", os_listWindowsRegistry },
Expand Down
1 change: 1 addition & 0 deletions src/host/premake.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ int os_chmod(lua_State* L);
int os_comparefiles(lua_State* L);
int os_copyfile(lua_State* L);
int os_getcwd(lua_State* L);
int os_getnumcpus(lua_State* L);
int os_getpass(lua_State* L);
int os_getWindowsRegistry(lua_State* L);
int os_listWindowsRegistry(lua_State* L);
Expand Down
10 changes: 10 additions & 0 deletions tests/base/test_os.lua
Original file line number Diff line number Diff line change
Expand Up @@ -488,3 +488,13 @@
test.isequal(true, ok)
test.isnil(err)
end


--
-- os.getnumcpus() tests.
--

function suite.numcpus()
local numcpus = os.getnumcpus()
test.istrue(numcpus > 0)
end
17 changes: 17 additions & 0 deletions website/docs/os.getnumcpus.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Gets the number of logical CPU cores.

```lua
cwd = os.getnumcpus()
```

### Parameters ###

None.

### Return Value ###

The number of logical CPU cores of the running system.

### Availability ###

Premake 5.0 or later.

0 comments on commit 41f984f

Please sign in to comment.