Skip to content

Commit 3c1c85d

Browse files
committed
Add os.getnumcpus function to get number of logical CPU cores.
1 parent db072b4 commit 3c1c85d

File tree

7 files changed

+118
-1
lines changed

7 files changed

+118
-1
lines changed

Bootstrap.mak

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ linux-clean: nix-clean
107107

108108
linux: linux-clean
109109
mkdir -p build/bootstrap
110-
$(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
110+
$(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
111111
./build/bootstrap/premake_bootstrap embed
112112
./build/bootstrap/premake_bootstrap --to=build/bootstrap gmake2
113113
$(MAKE) -C build/bootstrap -j`getconf _NPROCESSORS_ONLN` config=$(CONFIG)

premake5.lua

+4
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@
248248
filter "system:linux or hurd"
249249
links { "dl", "rt" }
250250

251+
filter "system:linux"
252+
defines { "_GNU_SOURCE" }
253+
254+
251255
filter { "system:not windows", "system:not macosx" }
252256
if not _OPTIONS["no-curl"] then
253257
links { "mbedtls-lib" }

src/host/os_getnumcpus.c

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* \file os_getcwd.c
3+
* \brief Retrieve the current working directory.
4+
* \author Copyright (c) 2002-2008 Jason Perkins and the Premake project
5+
*/
6+
7+
#include "premake.h"
8+
9+
#if PLATFORM_LINUX
10+
#include <sched.h>
11+
#elif PLATFORM_SOLARIS | PLATFORM_AIX | PLATFORM_MACOSX | PLATFORM_BSD
12+
#include <sys/sysctl.h>
13+
#endif
14+
15+
int do_getnumcpus()
16+
{
17+
#if PLATFORM_WINDOWS
18+
SYSTEM_INFO sysinfo;
19+
GetSystemInfo(&sysinfo);
20+
return sysinfo.dwNumberOfProcessors;
21+
#elif PLATFORM_LINUX
22+
cpu_set_t set;
23+
int count, i;
24+
25+
if (sched_getaffinity(0, sizeof(cpu_set_t), &set) != -1)
26+
{
27+
count = 0;
28+
for (i = 0; i < CPU_SETSIZE; i++)
29+
{
30+
if (CPU_ISSET(i, &set))
31+
{
32+
count++;
33+
}
34+
}
35+
36+
return count;
37+
}
38+
else
39+
{
40+
return 0;
41+
}
42+
#elif PLATFORM_SOLARIS | PLATFORM_AIX | PLATFORM_MACOSX
43+
return sysconf(_SC_NPROCESSORS_ONLN);
44+
#elif PLATFORM_BSD
45+
int mib[4];
46+
int numCPU;
47+
size_t len = sizeof(numCPU);
48+
49+
/* set the mib for hw.ncpu */
50+
mib[0] = CTL_HW;
51+
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
52+
53+
/* get the number of CPUs from the system */
54+
sysctl(mib, 2, &numCPU, &len, NULL, 0);
55+
56+
if (numCPU < 1)
57+
{
58+
mib[1] = HW_NCPU;
59+
sysctl(mib, 2, &numCPU, &len, NULL, 0);
60+
if (numCPU < 1)
61+
numCPU = 1;
62+
}
63+
64+
return numCPU;
65+
#else
66+
#warning do_getnumcpus is not implemented for your platform yet
67+
return 0;
68+
#endif
69+
}
70+
71+
int os_getnumcpus(lua_State* L)
72+
{
73+
int result = do_getnumcpus();
74+
if (result > 0)
75+
{
76+
lua_pushinteger(L, result);
77+
return 1;
78+
}
79+
else
80+
{
81+
return 0;
82+
}
83+
}
84+

src/host/premake.c

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static const luaL_Reg os_functions[] = {
6666
{ "_is64bit", os_is64bit },
6767
{ "isdir", os_isdir },
6868
{ "getcwd", os_getcwd },
69+
{ "getnumcpus", os_getnumcpus },
6970
{ "getpass", os_getpass },
7071
{ "getWindowsRegistry", os_getWindowsRegistry },
7172
{ "listWindowsRegistry", os_listWindowsRegistry },

src/host/premake.h

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ int os_chmod(lua_State* L);
121121
int os_comparefiles(lua_State* L);
122122
int os_copyfile(lua_State* L);
123123
int os_getcwd(lua_State* L);
124+
int os_getnumcpus(lua_State* L);
124125
int os_getpass(lua_State* L);
125126
int os_getWindowsRegistry(lua_State* L);
126127
int os_listWindowsRegistry(lua_State* L);

tests/base/test_os.lua

+10
Original file line numberDiff line numberDiff line change
@@ -488,3 +488,13 @@
488488
test.isequal(true, ok)
489489
test.isnil(err)
490490
end
491+
492+
493+
--
494+
-- os.getnumcpus() tests.
495+
--
496+
497+
function suite.numcpus()
498+
local numcpus = os.getnumcpus()
499+
test.istrue(numcpus > 0)
500+
end

website/docs/os.getnumcpus.md

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Gets the number of logical CPU cores.
2+
3+
```lua
4+
cwd = os.getnumcpus()
5+
```
6+
7+
### Parameters ###
8+
9+
None.
10+
11+
### Return Value ###
12+
13+
The number of logical CPU cores of the running system.
14+
15+
### Availability ###
16+
17+
Premake 5.0 or later.

0 commit comments

Comments
 (0)