1/* 2 * Copyright (c) 2015 Cyril Hrubis <chrubis@suse.cz> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms in version 2 of the GNU General Public License as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See 11 * the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program; if not, write to the Free Software Foundation, 15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 16 */ 17 18/* 19 * This code was extracted form the sched_setparam testcases. 20 */ 21 22#include <unistd.h> 23 24#ifdef BSD 25# include <sys/types.h> 26# include <sys/param.h> 27# include <sys/sysctl.h> 28#endif 29 30#ifdef HPUX 31# include <sys/param.h> 32# include <sys/pstat.h> 33#endif 34 35static int get_ncpu(void) 36{ 37 int ncpu = -1; 38 39 /* This syscall is not POSIX but it should work on many system */ 40#ifdef _SC_NPROCESSORS_ONLN 41 ncpu = sysconf(_SC_NPROCESSORS_ONLN); 42#else 43# ifdef BSD 44 int mib[2]; 45 size_t len = sizeof(ncpu); 46 mib[0] = CTL_HW; 47 mib[1] = HW_NCPU; 48 sysctl(mib, 2, &ncpu, &len, NULL, 0); 49# endif 50# ifdef HPUX 51 struct pst_dynamic psd; 52 pstat_getdynamic(&psd, sizeof(psd), 1, 0); 53 ncpu = (int)psd.psd_proc_cnt; 54# endif 55#endif /* _SC_NPROCESSORS_ONLN */ 56 57 return ncpu; 58} 59