This answer only provides implementations for Linux and the BSDs. If you're looking to size a thread pool which performs no blocking I/O, and doesn't sleep, then you want to use the number of physical cores, not the number of logical (hyper threading) cores. This is different than the number of logical cores which most of these answers provide. This should return the number of physical cores on the system. Snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu%u/topology/thread_siblings_list", lcores) Sysctlbyname("hw.physicalcpu", &num_cores, &num_cores_len, 0, 0) Size_t num_cores_len = sizeof(num_cores) #if defined(_APPLE_) || defined(_FreeBSD_) Or grep -c ^cpu /proc/stat # subtract 1 from the result The same approach in shell using grep: grep -c ^processor /proc/cpuinfo If( !memcmp(str, "cpu", 3) ) procCount++ Int procCount = -1 // to offset for the first entry The number of processors currently online (available).Ī simple approach would be to read /proc/stat or /proc/cpuinfo and count them: #include So there's a possibility that _SC_NPROCESSORS_ONLN may not be present: These values also exist, but may not be standard. On Linux, it's may not be safe to to use _SC_NPROCESSORS_ONLN as it's not part of POSIX standard and the sysconf manual states as much. It does not require any compilation(C/C++, assembly, et cetera) to use. Py-cpuinfo should work without any extra programs or libraries, beyond what your OS provides. Here are some tips on interpreting the contents: Each physical processor will present as at least one processor Each core will present as at least one processor Each hyperthread will present as one processor. Note that both methods can easily be converted to C/C++98/C++03. Py-cpuinfo gets CPU info with pure Python. The contents of /proc/cpuinfo contains information for all processors down to the virtual level. (Using GetLogicalProcessorInformationEx, see GetLogicalProcessorInformation as well) size_t NumberOfPhysicalCores() noexcept while (offset (system_info.dwNumberOfProcessors) The number of groups of logical processors sharing a single processor core. If (numCPU =10.5 or iOS) NSUInteger a = processorCount] * get the number of CPUs from the system */ Mib = HW_AVAILCPU // alternatively, try HW_NCPU
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |