/*
** SellaNMS
** $Id: nprocs.cxx,v 1.5 2006/01/24 07:08:09 sella Exp $
** Copyright (c) 2001-2005 Digital Genesis Software, LLC. All Rights Reserved.
** Released under the GPL Version 2 License.
** http://www.digitalgenesis.com
*/

static const char rcsid[] = "$Id: nprocs.cxx,v 1.5 2006/01/24 07:08:09 sella Exp $";

/* Code from Steven Bosscher posting to Comp-discuss. */

#include "nprocs.h"
#include <unistd.h>

/* Don't override GNU C Library extensions.	*/
#ifndef __GLIBC__

/*	Get the number of configured CPUs.	This may not be the same as the
	number of available CPUs.	Use get_nprocs instead to get that
	number.

	Returns the number of configured CPUs, assumes this is a single CPU
	machine if the number is not defined on the system.
*/

int get_nprocs_conf(void) {
	long nprocs_conf = -1;

#if defined _SC_NPROCESSORS_CONF
	nprocs_conf = sysconf(_SC_NPROCESSORS_CONF);
#elif defined _SC_NPROC_CONF
	nprocs_conf = sysconf(_SC_NPROC_CONF);
#elif defined _SC_CRAY_NCPU
	return get_ncpus();	/* Apparently, CRAY machines always work. */
#endif

	if (nprocs_conf < 1) {
		nprocs_conf = 1;
	}

	return (int) nprocs_conf;
}

/*	Get the number of available CPUs.	This may not be the same number
	as the number of configured CPUs, for example when some CPU slots
	are empty or when there's some kind of system maintenance going on.

	Returns the number of online CPUs, assumes this is a single CPU
	machine if the number is not defined on the system.
*/

int get_nprocs(void) {
	long nprocs = -1;

#if defined _SC_NPROCESSORS_ONLN
	nprocs = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined _SC_NPROC_ONLN
	nprocs = sysconf(_SC_NPROC_ONLN);
#elif defined _SC_CRAY_NCPU
	nprocs = sysconf(_SC_CRAY_NCPU);
#endif

	if (nprocs < 1) {
		nprocs = 1;
	}

	return (int) nprocs;
}

#endif

/*
** Local Variables:
** c-basic-offset: 3
** tab-width: 3
** End:
** vim:
** vim: noet ts=3 sw=3
*/
