Skip to content

Commit dd3fac7

Browse files
authored
Merge pull request #2292 from colrdavidson/get_core_count
add get core count
2 parents a11b6a9 + 3935957 commit dd3fac7

8 files changed

Lines changed: 119 additions & 3 deletions

File tree

core/os/os.odin

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,7 @@ heap_allocator :: proc() -> mem.Allocator {
261261
data = nil,
262262
}
263263
}
264+
265+
processor_core_count :: proc() -> int {
266+
return _processor_core_count()
267+
}

core/os/os_darwin.odin

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ foreign libc {
314314
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
315315

316316
@(link_name="strerror") _darwin_string_error :: proc(num : c.int) -> cstring ---
317+
@(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int ---
317318

318319
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
319320
}
@@ -771,6 +772,18 @@ get_page_size :: proc() -> int {
771772
return page_size
772773
}
773774

775+
@(private)
776+
_processor_core_count :: proc() -> int {
777+
count : int = 0
778+
count_size := size_of(count)
779+
if _sysctlbyname("hw.logicalcpu", &count, &count_size, nil, 0) == 0 {
780+
if count > 0 {
781+
return count
782+
}
783+
}
784+
785+
return 1
786+
}
774787

775788
_alloc_command_line_arguments :: proc() -> []string {
776789
res := make([]string, len(runtime.args__))

core/os/os_freebsd.odin

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ foreign libc {
287287

288288
@(link_name="getenv") _unix_getenv :: proc(cstring) -> cstring ---
289289
@(link_name="realpath") _unix_realpath :: proc(path: cstring, resolved_path: rawptr) -> rawptr ---
290+
@(link_name="sysctlbyname") _sysctlbyname :: proc(path: cstring, oldp: rawptr, oldlenp: rawptr, newp: rawptr, newlen: int) -> c.int ---
290291

291292
@(link_name="exit") _unix_exit :: proc(status: c.int) -> ! ---
292293
}
@@ -702,6 +703,19 @@ get_page_size :: proc() -> int {
702703
return page_size
703704
}
704705

706+
@(private)
707+
_processor_core_count :: proc() -> int {
708+
count : int = 0
709+
count_size := size_of(count)
710+
if _sysctlbyname("hw.logicalcpu", &count, &count_size, nil, 0) == 0 {
711+
if count > 0 {
712+
return count
713+
}
714+
}
715+
716+
return 1
717+
}
718+
705719

706720
_alloc_command_line_arguments :: proc() -> []string {
707721
res := make([]string, len(runtime.args__))

core/os/os_linux.odin

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ foreign libc {
404404
@(link_name="__errno_location") __errno_location :: proc() -> ^int ---
405405

406406
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
407+
@(link_name="get_nprocs") _unix_get_nprocs :: proc() -> c.int ---
407408
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
408409
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
409410
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
@@ -878,6 +879,10 @@ get_page_size :: proc() -> int {
878879
return page_size
879880
}
880881

882+
@(private)
883+
_processor_core_count :: proc() -> int {
884+
return int(_unix_get_nprocs())
885+
}
881886

882887
_alloc_command_line_arguments :: proc() -> []string {
883888
res := make([]string, len(runtime.args__))

core/os/os_openbsd.odin

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ foreign libc {
269269
@(link_name="mkdir") _unix_mkdir :: proc(path: cstring, mode: mode_t) -> c.int ---
270270

271271
@(link_name="getpagesize") _unix_getpagesize :: proc() -> c.int ---
272+
@(link_name="sysconf") _sysconf :: proc(name: c.int) -> c.long ---
272273
@(link_name="fdopendir") _unix_fdopendir :: proc(fd: Handle) -> Dir ---
273274
@(link_name="closedir") _unix_closedir :: proc(dirp: Dir) -> c.int ---
274275
@(link_name="rewinddir") _unix_rewinddir :: proc(dirp: Dir) ---
@@ -704,11 +705,17 @@ get_page_size :: proc() -> int {
704705
return page_size
705706
}
706707

708+
_SC_NPROCESSORS_ONLN :: 503
709+
710+
@(private)
711+
_processor_core_count :: proc() -> int {
712+
return int(_sysconf(_SC_NPROCESSORS_ONLN))
713+
}
707714

708715
_alloc_command_line_arguments :: proc() -> []string {
709716
res := make([]string, len(runtime.args__))
710717
for arg, i in runtime.args__ {
711718
res[i] = string(arg)
712719
}
713720
return res
714-
}
721+
}

core/os/os_wasi.odin

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ seek :: proc(fd: Handle, offset: i64, whence: int) -> (i64, Errno) {
8989
current_thread_id :: proc "contextless" () -> int {
9090
return 0
9191
}
92-
92+
@(private)
93+
_processor_core_count :: proc() -> int {
94+
return 1
95+
}
9396

9497
file_size :: proc(fd: Handle) -> (i64, Errno) {
9598
stat, err := wasi.fd_filestat_get(wasi.fd_t(fd))

core/os/os_windows.odin

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package os
33

44
import win32 "core:sys/windows"
55
import "core:runtime"
6+
import "core:intrinsics"
67

78
Handle :: distinct uintptr
89
File_Time :: distinct u64
@@ -126,7 +127,28 @@ get_page_size :: proc() -> int {
126127
return page_size
127128
}
128129

130+
@(private)
131+
_processor_core_count :: proc() -> int {
132+
length : win32.DWORD = 0
133+
result := win32.GetLogicalProcessorInformation(nil, &length)
129134

135+
thread_count := 0
136+
if !result && win32.GetLastError() == 122 && length > 0 {
137+
processors := make([]win32.SYSTEM_LOGICAL_PROCESSOR_INFORMATION, length, context.temp_allocator)
138+
139+
result = win32.GetLogicalProcessorInformation(&processors[0], &length)
140+
if result {
141+
for processor in processors {
142+
if processor.Relationship == .RelationProcessorCore {
143+
thread := intrinsics.count_ones(processor.ProcessorMask)
144+
thread_count += int(thread)
145+
}
146+
}
147+
}
148+
}
149+
150+
return thread_count
151+
}
130152

131153
exit :: proc "contextless" (code: int) -> ! {
132154
runtime._cleanup_runtime_contextless()
@@ -214,4 +236,4 @@ is_windows_10 :: proc() -> bool {
214236
is_windows_11 :: proc() -> bool {
215237
osvi := get_windows_version_w()
216238
return (osvi.dwMajorVersion == 10 && osvi.dwMinorVersion == 0 && osvi.dwBuildNumber >= WINDOWS_11_BUILD_CUTOFF)
217-
}
239+
}

core/sys/windows/kernel32.odin

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ foreign kernel32 {
370370
lpTotalNumberOfBytes: PULARGE_INTEGER,
371371
lpTotalNumberOfFreeBytes: PULARGE_INTEGER,
372372
) -> BOOL ---
373+
374+
GetLogicalProcessorInformation :: proc(buffer: ^SYSTEM_LOGICAL_PROCESSOR_INFORMATION, returnedLength: PDWORD) -> BOOL ---
373375
}
374376

375377

@@ -999,3 +1001,49 @@ foreign kernel32 {
9991001
ConvertThreadToFiber :: proc(lpParameter: LPVOID) -> LPVOID ---
10001002
SwitchToFiber :: proc(lpFiber: LPVOID) ---
10011003
}
1004+
1005+
LOGICAL_PROCESSOR_RELATIONSHIP :: enum c_int {
1006+
RelationProcessorCore,
1007+
RelationNumaNode,
1008+
RelationCache,
1009+
RelationProcessorPackage,
1010+
RelationGroup,
1011+
RelationProcessorDie,
1012+
RelationNumaNodeEx,
1013+
RelationProcessorModule,
1014+
RelationAll = 0xffff,
1015+
}
1016+
1017+
PROCESSOR_CACHE_TYPE :: enum c_int {
1018+
CacheUnified,
1019+
CacheInstruction,
1020+
CacheData,
1021+
CacheTrace,
1022+
}
1023+
1024+
CACHE_DESCRIPTOR :: struct {
1025+
Level: BYTE,
1026+
Associativity: BYTE,
1027+
LineSize: WORD,
1028+
Size: DWORD,
1029+
Type: PROCESSOR_CACHE_TYPE,
1030+
}
1031+
1032+
ProcessorCore :: struct {
1033+
Flags: BYTE,
1034+
}
1035+
NumaNode :: struct {
1036+
NodeNumber: DWORD,
1037+
}
1038+
DUMMYUNIONNAME_u :: struct #raw_union {
1039+
Core: ProcessorCore,
1040+
Node: NumaNode,
1041+
Cache: CACHE_DESCRIPTOR,
1042+
Reserved: [2]ULONGLONG,
1043+
}
1044+
1045+
SYSTEM_LOGICAL_PROCESSOR_INFORMATION :: struct {
1046+
ProcessorMask: ULONG_PTR,
1047+
Relationship: LOGICAL_PROCESSOR_RELATIONSHIP,
1048+
DummyUnion: DUMMYUNIONNAME_u,
1049+
}

0 commit comments

Comments
 (0)