-
-
Notifications
You must be signed in to change notification settings - Fork 22k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add OS.get_physical_processor_count()
method
#76932
base: master
Are you sure you want to change the base?
Add OS.get_physical_processor_count()
method
#76932
Conversation
4eede8d
to
9571bb7
Compare
d628a21
to
9f0f5bd
Compare
028c664
to
0f50c62
Compare
@@ -150,6 +150,20 @@ String OS_LinuxBSD::get_unique_id() const { | |||
return machine_id; | |||
} | |||
|
|||
int OS_LinuxBSD::get_physical_processor_count() const { | |||
Ref<FileAccess> f = FileAccess::open("/proc/cpuinfo", FileAccess::READ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This probably won't work on BSD, so something sysctl
based, similar to the macOS, probably should be added.
It seems to be kern.smp.cores
for FreeBSD, not sure about other BSDs (maybe hw.ncpu
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added code to support *BSD but I can't test it, since I currently don't have a VM setup active.
0f50c62
to
34df9eb
Compare
34df9eb
to
e2982fd
Compare
This returns the number of physical CPU cores on the system, as opposed to `OS.get_processor_count()` which returns the number of logical CPU cores on the system. Use cases: - Configure parallelism for algorithms that don't scale well to logical CPU cores. - Troubleshoot performance issues, as you can now detect whether a user has HyperThreading enabled or not (by checking whether `OS.get_physical_processor_count()` and `OS.get_processor_count()` are not equal).
e2982fd
to
13a3e43
Compare
int OS_MacOS::get_physical_processor_count() const { | ||
uint32_t num_cores = 0; | ||
size_t num_cores_len = sizeof(num_cores); | ||
sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sysctlbyname
may error, returning != 0:
https://developer.apple.com/documentation/kernel/1387446-sysctlbyname#return_value
I don't know though if it actually does in any practical situation.
I feel the documentation should mention the result value should be cached because calling |
Any reasons not to cache it internally? Processor count should not change in runtime. |
I was thinking the exact same thing as I wrote it. Only thing to watch out is race conditions when caching the first time. It is possible for complex computers to have CPUs turned off (and maybe become mainstream in the future?), but if that's becomes a reality for the consumer, caching it externally or caching it internally won't matter (plus, a whole lot of other problems more than just this function). So yeah, no reason we cannot cache it internally. |
For people using virtual machines it could be useful to get the actual current CPU count. But that's a bit of a niche case. I'm not entirely convinced that this is entirely useful though, Godot has no way to provide any kind of thread affinity, so getting the physical core count really isn't information you can do much with. Just running your heavy process on half the number of cores doesn't really necessarily prevent sharing on smt logical cores. I don't really care about the method, but if we are going to document it as being good for that we need a whole pile of other infrastructure to actually tell the OS that we want tasks split away from core siblings. |
This returns the number of physical CPU cores on the system, as opposed to
OS.get_processor_count()
which returns the number of logical CPU cores on the system.Use cases:
OS.get_physical_processor_count()
andOS.get_processor_count()
are different).Testing project: test_physical_cpu_count.zip
TODO
8
which sounds expected.