1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync/* $Id$ */
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * IPRT - RTMpGetCount, POSIX.
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync */
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync/*
c58f1213e628a545081c70e26c6b67a841cff880vboxsync * Copyright (C) 2006-2010 Oracle Corporation
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync *
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * available from http://www.virtualbox.org. This file is free software;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * you can redistribute it and/or modify it under the terms of the GNU
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * General Public License (GPL) as published by the Free Software
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync *
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * The contents of this file may alternatively be used under the terms
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * of the Common Development and Distribution License Version 1.0
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * VirtualBox OSE distribution, in which case the provisions of the
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * CDDL are applicable instead of those of the GPL.
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync *
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * You may elect to license modified versions of this file under the
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * terms and conditions of either the GPL or the CDDL or both.
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync */
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync/*******************************************************************************
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync* Header Files *
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync*******************************************************************************/
216d7c61611128ac948a9922abc905b151a58184vboxsync#include <iprt/mp.h>
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#include <iprt/assert.h>
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#include <unistd.h>
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#if !defined(RT_OS_SOLARIS)
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync# include <sys/sysctl.h>
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#endif
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsyncRTDECL(RTCPUID) RTMpGetCount(void)
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync{
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync /*
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * The sysconf way (linux and others).
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync */
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync#if defined(_SC_NPROCESSORS_MAX) || defined(_SC_NPROCESSORS_CONF) || defined(_SC_NPROCESSORS_ONLN)
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int cCpusSC = -1;
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# ifdef _SC_NPROCESSORS_MAX
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int cMax = sysconf(_SC_NPROCESSORS_MAX);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync cCpusSC = RT_MAX(cCpusSC, cMax);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# endif
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# ifdef _SC_NPROCESSORS_CONF
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int cConf = sysconf(_SC_NPROCESSORS_CONF);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync cCpusSC = RT_MAX(cCpusSC, cConf);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# endif
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# ifdef _SC_NPROCESSORS_ONLN
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int cOnln = sysconf(_SC_NPROCESSORS_ONLN);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync cCpusSC = RT_MAX(cCpusSC, cOnln);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync# endif
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync Assert(cCpusSC > 0);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync if (cCpusSC > 0)
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync return cCpusSC;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#endif
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync /*
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync * The BSD 4.4 way.
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync */
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#if defined(CTL_HW) && defined(HW_NCPU)
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync int aiMib[2];
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync aiMib[0] = CTL_HW;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync aiMib[1] = HW_NCPU;
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int cCpus = -1;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync size_t cb = sizeof(cCpus);
5ece3177d897caf5fdc7bdaffbd95a053ba8a158vboxsync int rc = sysctl(aiMib, RT_ELEMENTS(aiMib), &cCpus, &cb, NULL, 0);
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync if (rc != -1 && cCpus >= 1)
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync return cCpus;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync#endif
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync return 1;
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync}
1b132cc0e32ad7603ae736933c6e085dd08b3a90vboxsync