diff options
author | larryh%netscape.com <devnull@localhost> | 2000-05-23 21:35:16 +0000 |
---|---|---|
committer | larryh%netscape.com <devnull@localhost> | 2000-05-23 21:35:16 +0000 |
commit | f432e6cce39ec3d3ec23df920170075e63f1694e (patch) | |
tree | 029c437e71564d08159a4ccfaa2e0dbc98e02490 | |
parent | c1168b665dea01b9f11cc1e68660d214c219c01d (diff) | |
download | nspr-hg-f432e6cce39ec3d3ec23df920170075e63f1694e.tar.gz |
BugZilla 25981. PR_GetNumberOfCpus()
-rw-r--r-- | pr/include/prsystem.h | 16 | ||||
-rw-r--r-- | pr/src/misc/prsystem.c | 74 | ||||
-rw-r--r-- | pr/tests/system.c | 1 |
3 files changed, 91 insertions, 0 deletions
diff --git a/pr/include/prsystem.h b/pr/include/prsystem.h index 56dc7b03..275adfa9 100644 --- a/pr/include/prsystem.h +++ b/pr/include/prsystem.h @@ -74,6 +74,22 @@ NSPR_API(PRInt32) PR_GetPageSize(void); */ NSPR_API(PRInt32) PR_GetPageShift(void); +/* +** PR_GetNumberOfProcessors() -- returns the number of CPUs +** +** Description: +** PR_GetNumberOfProcessors() extracts the number of processors +** (CPUs available in an SMP system) and returns the number. +** +** Parameters: +** none +** +** Returns: +** The number of available processors or -1 on error +** +*/ +NSPR_API(PRInt32) PR_GetNumberOfProcessors( void ); + PR_END_EXTERN_C #endif /* prsystem_h___ */ diff --git a/pr/src/misc/prsystem.c b/pr/src/misc/prsystem.c index 4c74b3b4..22f1b5db 100644 --- a/pr/src/misc/prsystem.c +++ b/pr/src/misc/prsystem.c @@ -20,6 +20,20 @@ #include "prsystem.h" #include "prprf.h" +#if defined(BEOS) +#include <kernel/OS.h> +#endif + +#if defined(OS2) +#define INCL_DOS +#include os2.h +#endif + +#if defined(HPUX) +#include <sys/scall_define.h> +#include <sys/mp.h> +#endif + #if defined(XP_UNIX) #include <unistd.h> #include <sys/utsname.h> @@ -113,3 +127,63 @@ PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 bufle } return PR_SUCCESS; } + +/* +** PR_GetNumberOfProcessors() +** +** Implementation notes: +** Every platform does it a bit different. +** numCpus is the returned value. +** for each platform's "if defined" section +** declare your local variable +** do your thing, assign to numCpus +** order of the if defined()s may be important, +** especially for unix variants. Do platform +** specific implementations before XP_UNIX. +** Watch order of RHAPSODY (first) and XP_MAC (after RHAPSODY). +** +*/ +PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) +{ + PRInt32 numCpus; +#if defined(WIN32) + SYSTEM_INFO info; + + GetSystemInfo( &info ); + numCpus = info.dwNumberOfProcessors; +#elif defined(RHAPSODY) /* "darwin" or Mac OS/X */ + int mib[2]; + int rc; + size_t len = sizeof(numCpus); + + mib[0] = CTL_HW; + mib[1] = HW_NCPU; + rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); + if ( -1 == rc ) { + numCpus = -1; /* set to -1 for return value on error */ + PR_SetError( PR_UNKNOWN_ERROR, _MD_ERRNO()); + } +#elif defined(XP_MAC) + numCpus = MPProcessors(); +#elif defined(BEOS) + system_info sysInfo; + + get_system_info(&sysInfo); + numCpus = sysInfo.cpu_count; +#elif defined(OS2) + DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); +#elif defined(HPUX11) + numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); + if ( numCpus < 1 ) { + numCpus = -1; /* set to -1 for return value on error */ + PR_SetError( PR_UNKNOWN_ERROR, _MD_ERRNO()); + } +#elif defined(IRIX) + numCpus = sysconf( _SC_NPROC_CONF ); +#elif defined(XP_UNIX) + numCpus = sysconf( _SC_NPROCESSORS_ONLN ); +#else +#error "An implementation is required" +#endif + return(numCpus); +} /* end PR_GetNumberOfProcessors() */ diff --git a/pr/tests/system.c b/pr/tests/system.c index b09d8499..a1a1e004 100644 --- a/pr/tests/system.c +++ b/pr/tests/system.c @@ -55,6 +55,7 @@ PRIntn main(PRIntn argc, char **argv) PR_fprintf(output, "Host page size is %d\n", PR_GetPageSize()); PR_fprintf(output, "Page shift is %d\n", PR_GetPageShift()); + PR_fprintf(output, "Number of processors is: %d\n", PR_GetNumberOfProcessors()); return 0; } /* main */ |