OLD | NEW |
(Empty) | |
| 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // |
| 3 /// \file tuklib_cpucores.c |
| 4 /// \brief Get the number of CPU cores online |
| 5 // |
| 6 // Author: Lasse Collin |
| 7 // |
| 8 // This file has been put into the public domain. |
| 9 // You can do whatever you want with this file. |
| 10 // |
| 11 /////////////////////////////////////////////////////////////////////////////// |
| 12 |
| 13 #include "tuklib_cpucores.h" |
| 14 |
| 15 #if defined(TUKLIB_CPUCORES_SYSCTL) |
| 16 # ifdef HAVE_SYS_PARAM_H |
| 17 # include <sys/param.h> |
| 18 # endif |
| 19 # include <sys/sysctl.h> |
| 20 |
| 21 #elif defined(TUKLIB_CPUCORES_SYSCONF) |
| 22 # include <unistd.h> |
| 23 |
| 24 // HP-UX |
| 25 #elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC) |
| 26 # include <sys/param.h> |
| 27 # include <sys/pstat.h> |
| 28 #endif |
| 29 |
| 30 |
| 31 extern uint32_t |
| 32 tuklib_cpucores(void) |
| 33 { |
| 34 uint32_t ret = 0; |
| 35 |
| 36 #if defined(TUKLIB_CPUCORES_SYSCTL) |
| 37 int name[2] = { CTL_HW, HW_NCPU }; |
| 38 int cpus; |
| 39 size_t cpus_size = sizeof(cpus); |
| 40 if (sysctl(name, 2, &cpus, &cpus_size, NULL, 0) != -1 |
| 41 && cpus_size == sizeof(cpus) && cpus > 0) |
| 42 ret = cpus; |
| 43 |
| 44 #elif defined(TUKLIB_CPUCORES_SYSCONF) |
| 45 # ifdef _SC_NPROCESSORS_ONLN |
| 46 // Most systems |
| 47 const long cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 48 # else |
| 49 // IRIX |
| 50 const long cpus = sysconf(_SC_NPROC_ONLN); |
| 51 # endif |
| 52 if (cpus > 0) |
| 53 ret = cpus; |
| 54 |
| 55 #elif defined(TUKLIB_CPUCORES_PSTAT_GETDYNAMIC) |
| 56 struct pst_dynamic pst; |
| 57 if (pstat_getdynamic(&pst, sizeof(pst), 1, 0) != -1) |
| 58 ret = pst.psd_proc_cnt; |
| 59 #endif |
| 60 |
| 61 return ret; |
| 62 } |
OLD | NEW |