osxcross/tools/cpucount.c

104 lines
2.9 KiB
C
Raw Normal View History

2014-04-06 15:58:15 +02:00
/***********************************************************************
* OSXCross *
2016-03-01 19:17:29 +01:00
* Copyright (C) 2013-2016 by Thomas Poechtrager *
2014-04-06 15:58:15 +02:00
* t.poechtrager@gmail.com *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
***********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#ifdef __CYGWIN__
#define WIN32
2014-04-06 15:58:15 +02:00
#endif /* __CYGWIN__ */
#ifdef WIN32
#include <windows.h>
2014-04-06 15:58:15 +02:00
#endif /* WIN32 */
#ifdef __linux__
#define __USE_GNU
#include <sched.h>
#undef __USE_GNU
2014-04-06 15:58:15 +02:00
#endif /* __linux__ */
2014-04-06 15:58:15 +02:00
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__) || \
defined(__APPLE__)
#include <unistd.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#ifndef HW_AVAILCPU
#define HW_AVAILCPU 25
2014-04-06 15:58:15 +02:00
#endif /* HW_AVAILCPU */
#endif /* BSD */
2014-04-06 15:58:15 +02:00
int getcpucount() {
#ifdef WIN32
2014-04-06 15:58:15 +02:00
SYSTEM_INFO sysinfo;
GetSystemInfo(&sysinfo);
2014-04-06 15:58:15 +02:00
return sysinfo.dwNumberOfProcessors;
#else
#ifdef __linux__
2014-04-06 15:58:15 +02:00
cpu_set_t cs;
int i, cpucount = 0;
2014-04-06 15:58:15 +02:00
CPU_ZERO(&cs);
2016-03-01 19:17:29 +01:00
if (sched_getaffinity(0, sizeof(cs), &cs))
return 1;
for (i = 0; i < CPU_SETSIZE; i++)
2014-04-06 15:58:15 +02:00
if (CPU_ISSET(i, &cs))
cpucount++;
2016-03-01 19:17:29 +01:00
return cpucount;
#else
2014-04-06 15:58:15 +02:00
#if defined(__FreeBSD__) || defined(__NetBSD__) || \
defined(__OpenBSD__) || defined(__DragonFly__) || \
defined(__APPLE__)
2014-04-06 15:58:15 +02:00
int cpucount = 0;
int mib[4];
size_t len = sizeof(cpucount);
2014-04-06 15:58:15 +02:00
mib[0] = CTL_HW;
mib[1] = HW_AVAILCPU;
2014-04-06 15:58:15 +02:00
sysctl(mib, 2, &cpucount, &len, NULL, 0);
2014-04-06 15:58:15 +02:00
if (cpucount < 1) {
mib[1] = HW_NCPU;
sysctl(mib, 2, &cpucount, &len, NULL, 0);
}
2014-04-06 15:58:15 +02:00
return cpucount ? cpucount : 1;
#else
#warning unknown platform
2014-04-06 15:58:15 +02:00
return 1;
#endif /* BSD */
#endif /* __linux__ */
#endif /* WIN32 */
}
2014-04-06 15:58:15 +02:00
int main(void) {
printf("%d\n", getcpucount());
return 0;
}