andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include "primpl.h" andre@0: #include "prsystem.h" andre@0: #include "prprf.h" andre@0: #include "prlong.h" andre@0: andre@0: #if defined(BEOS) andre@0: #include andre@0: #endif andre@0: andre@0: #if defined(OS2) andre@0: #define INCL_DOS andre@0: #define INCL_DOSMISC andre@0: #include andre@0: /* define the required constant if it is not already defined in the headers */ andre@0: #ifndef QSV_NUMPROCESSORS andre@0: #define QSV_NUMPROCESSORS 26 andre@0: #endif andre@0: #endif andre@0: andre@0: /* BSD-derived systems use sysctl() to get the number of processors */ andre@0: #if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \ andre@0: || defined(OPENBSD) || defined(DARWIN) andre@0: #define _PR_HAVE_SYSCTL andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: #if defined(DARWIN) andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: #if defined(HPUX) andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: #if defined(XP_UNIX) andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: #if defined(LINUX) andre@0: #include andre@0: #include andre@0: #define MAX_LINE 512 andre@0: #endif andre@0: andre@0: #if defined(AIX) andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: PR_IMPLEMENT(char) PR_GetDirectorySeparator(void) andre@0: { andre@0: return PR_DIRECTORY_SEPARATOR; andre@0: } /* PR_GetDirectorySeparator */ andre@0: andre@0: /* andre@0: ** OBSOLETE -- the function name is misspelled. andre@0: */ andre@0: PR_IMPLEMENT(char) PR_GetDirectorySepartor(void) andre@0: { andre@0: #if defined(DEBUG) andre@0: static PRBool warn = PR_TRUE; andre@0: if (warn) { andre@0: warn = _PR_Obsolete("PR_GetDirectorySepartor()", andre@0: "PR_GetDirectorySeparator()"); andre@0: } andre@0: #endif andre@0: return PR_GetDirectorySeparator(); andre@0: } /* PR_GetDirectorySepartor */ andre@0: andre@0: PR_IMPLEMENT(char) PR_GetPathSeparator(void) andre@0: { andre@0: return PR_PATH_SEPARATOR; andre@0: } /* PR_GetPathSeparator */ andre@0: andre@0: PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen) andre@0: { andre@0: PRUintn len = 0; andre@0: andre@0: if (!_pr_initialized) _PR_ImplicitInitialization(); andre@0: andre@0: switch(cmd) andre@0: { andre@0: case PR_SI_HOSTNAME: andre@0: case PR_SI_HOSTNAME_UNTRUNCATED: andre@0: if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen)) andre@0: return PR_FAILURE; andre@0: andre@0: if (cmd == PR_SI_HOSTNAME_UNTRUNCATED) andre@0: break; andre@0: /* andre@0: * On some platforms a system does not have a hostname and andre@0: * its IP address is returned instead. The following code andre@0: * should be skipped on those platforms. andre@0: */ andre@0: #ifndef _PR_GET_HOST_ADDR_AS_NAME andre@0: /* Return the unqualified hostname */ andre@0: while (buf[len] && (len < buflen)) { andre@0: if (buf[len] == '.') { andre@0: buf[len] = '\0'; andre@0: break; andre@0: } andre@0: len += 1; andre@0: } andre@0: #endif andre@0: break; andre@0: andre@0: case PR_SI_SYSNAME: andre@0: /* Return the operating system name */ andre@0: #if defined(XP_UNIX) || defined(WIN32) andre@0: if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) andre@0: return PR_FAILURE; andre@0: #else andre@0: (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME); andre@0: #endif andre@0: break; andre@0: andre@0: case PR_SI_RELEASE: andre@0: /* Return the version of the operating system */ andre@0: #if defined(XP_UNIX) || defined(WIN32) andre@0: if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) andre@0: return PR_FAILURE; andre@0: #endif andre@0: #if defined(XP_OS2) andre@0: { andre@0: ULONG os2ver[2] = {0}; andre@0: DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION, andre@0: &os2ver, sizeof(os2ver)); andre@0: /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially, andre@0: Warp 4 is version 2.40.00, WSeB 2.45.00 */ andre@0: if (os2ver[0] < 30) andre@0: (void)PR_snprintf(buf, buflen, "%s%lu", andre@0: "2.", os2ver[0]); andre@0: else if (os2ver[0] < 45) andre@0: (void)PR_snprintf(buf, buflen, "%lu%s%lu", andre@0: os2ver[0]/10, ".", os2ver[1]); andre@0: else andre@0: (void)PR_snprintf(buf, buflen, "%.1f", andre@0: os2ver[0]/10.0); andre@0: } andre@0: #endif /* OS2 */ andre@0: break; andre@0: andre@0: case PR_SI_ARCHITECTURE: andre@0: /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/ andre@0: (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE); andre@0: break; andre@0: default: andre@0: PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); andre@0: return PR_FAILURE; andre@0: } andre@0: return PR_SUCCESS; andre@0: } andre@0: andre@0: /* andre@0: ** PR_GetNumberOfProcessors() andre@0: ** andre@0: ** Implementation notes: andre@0: ** Every platform does it a bit different. andre@0: ** numCpus is the returned value. andre@0: ** for each platform's "if defined" section andre@0: ** declare your local variable andre@0: ** do your thing, assign to numCpus andre@0: ** order of the if defined()s may be important, andre@0: ** especially for unix variants. Do platform andre@0: ** specific implementations before XP_UNIX. andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) andre@0: { andre@0: PRInt32 numCpus; andre@0: #if defined(WIN32) andre@0: SYSTEM_INFO info; andre@0: andre@0: GetSystemInfo( &info ); andre@0: numCpus = info.dwNumberOfProcessors; andre@0: #elif defined(BEOS) andre@0: system_info sysInfo; andre@0: andre@0: get_system_info(&sysInfo); andre@0: numCpus = sysInfo.cpu_count; andre@0: #elif defined(OS2) andre@0: DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); andre@0: #elif defined(_PR_HAVE_SYSCTL) andre@0: int mib[2]; andre@0: int rc; andre@0: size_t len = sizeof(numCpus); andre@0: andre@0: mib[0] = CTL_HW; andre@0: mib[1] = HW_NCPU; andre@0: rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); andre@0: if ( -1 == rc ) { andre@0: numCpus = -1; /* set to -1 for return value on error */ andre@0: _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); andre@0: } andre@0: #elif defined(HPUX) andre@0: numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); andre@0: if ( numCpus < 1 ) { andre@0: numCpus = -1; /* set to -1 for return value on error */ andre@0: _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); andre@0: } andre@0: #elif defined(IRIX) andre@0: numCpus = sysconf( _SC_NPROC_ONLN ); andre@0: #elif defined(RISCOS) || defined(SYMBIAN) andre@0: numCpus = 1; andre@0: #elif defined(LINUX) andre@0: /* for the benefit of devices with advanced power-saving, that andre@0: actually hotplug their cpus in heavy load, try to figure out andre@0: the real number of CPUs */ andre@0: char buf[MAX_LINE]; andre@0: FILE *fin; andre@0: const char *cpu_present = "/sys/devices/system/cpu/present"; andre@0: size_t strsize; andre@0: numCpus = 0; andre@0: fin = fopen(cpu_present, "r"); andre@0: if (fin != NULL) { andre@0: if (fgets(buf, MAX_LINE, fin) != NULL) { andre@0: /* check that the format is what we expect */ andre@0: if (buf[0] == '0') { andre@0: strsize = strlen(buf); andre@0: if (strsize == 1) { andre@0: /* single core */ andre@0: numCpus = 1; andre@0: } else if (strsize >= 3 && strsize <= 5) { andre@0: /* should be of the form 0-999 */ andre@0: /* parse the part after the 0-, note count is 0-based */ andre@0: if (buf[1] == '-' && isdigit(buf[2])) { andre@0: numCpus = 1 + atoi(buf + 2); andre@0: } andre@0: } andre@0: } andre@0: } andre@0: fclose(fin); andre@0: } andre@0: /* if that fails, fall back to more standard methods */ andre@0: if (!numCpus) { andre@0: numCpus = sysconf( _SC_NPROCESSORS_CONF ); andre@0: } andre@0: #elif defined(XP_UNIX) andre@0: numCpus = sysconf( _SC_NPROCESSORS_CONF ); andre@0: #else andre@0: #error "An implementation is required" andre@0: #endif andre@0: return(numCpus); andre@0: } /* end PR_GetNumberOfProcessors() */ andre@0: andre@0: /* andre@0: ** PR_GetPhysicalMemorySize() andre@0: ** andre@0: ** Implementation notes: andre@0: ** Every platform does it a bit different. andre@0: ** bytes is the returned value. andre@0: ** for each platform's "if defined" section andre@0: ** declare your local variable andre@0: ** do your thing, assign to bytes. andre@0: ** andre@0: */ andre@0: PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void) andre@0: { andre@0: PRUint64 bytes = 0; andre@0: andre@0: #if defined(LINUX) || defined(SOLARIS) andre@0: andre@0: long pageSize = sysconf(_SC_PAGESIZE); andre@0: long pageCount = sysconf(_SC_PHYS_PAGES); andre@0: if (pageSize >= 0 && pageCount >= 0) andre@0: bytes = (PRUint64) pageSize * pageCount; andre@0: andre@0: #elif defined(NETBSD) || defined(OPENBSD) andre@0: andre@0: int mib[2]; andre@0: int rc; andre@0: uint64_t memSize; andre@0: size_t len = sizeof(memSize); andre@0: andre@0: mib[0] = CTL_HW; andre@0: mib[1] = HW_PHYSMEM64; andre@0: rc = sysctl(mib, 2, &memSize, &len, NULL, 0); andre@0: if (-1 != rc) { andre@0: bytes = memSize; andre@0: } andre@0: andre@0: #elif defined(HPUX) andre@0: andre@0: struct pst_static info; andre@0: int result = pstat_getstatic(&info, sizeof(info), 1, 0); andre@0: if (result == 1) andre@0: bytes = (PRUint64) info.physical_memory * info.page_size; andre@0: andre@0: #elif defined(DARWIN) andre@0: andre@0: struct host_basic_info hInfo; andre@0: mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; andre@0: andre@0: int result = host_info(mach_host_self(), andre@0: HOST_BASIC_INFO, andre@0: (host_info_t) &hInfo, andre@0: &count); andre@0: if (result == KERN_SUCCESS) andre@0: bytes = hInfo.max_mem; andre@0: andre@0: #elif defined(WIN32) andre@0: andre@0: MEMORYSTATUSEX memStat; andre@0: memStat.dwLength = sizeof(memStat); andre@0: if (GlobalMemoryStatusEx(&memStat)) andre@0: bytes = memStat.ullTotalPhys; andre@0: andre@0: #elif defined(OS2) andre@0: andre@0: ULONG ulPhysMem; andre@0: DosQuerySysInfo(QSV_TOTPHYSMEM, andre@0: QSV_TOTPHYSMEM, andre@0: &ulPhysMem, andre@0: sizeof(ulPhysMem)); andre@0: bytes = ulPhysMem; andre@0: andre@0: #elif defined(AIX) andre@0: andre@0: if (odm_initialize() == 0) { andre@0: int how_many; andre@0: struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many); andre@0: if (obj != NULL) { andre@0: bytes = (PRUint64) atoi(obj->value) * 1024; andre@0: free(obj); andre@0: } andre@0: odm_terminate(); andre@0: } andre@0: andre@0: #else andre@0: andre@0: PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); andre@0: andre@0: #endif andre@0: andre@0: return bytes; andre@0: } /* end PR_GetPhysicalMemorySize() */