Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "primpl.h" |
michael@0 | 7 | #include "prsystem.h" |
michael@0 | 8 | #include "prprf.h" |
michael@0 | 9 | #include "prlong.h" |
michael@0 | 10 | |
michael@0 | 11 | #if defined(BEOS) |
michael@0 | 12 | #include <kernel/OS.h> |
michael@0 | 13 | #endif |
michael@0 | 14 | |
michael@0 | 15 | #if defined(OS2) |
michael@0 | 16 | #define INCL_DOS |
michael@0 | 17 | #define INCL_DOSMISC |
michael@0 | 18 | #include <os2.h> |
michael@0 | 19 | /* define the required constant if it is not already defined in the headers */ |
michael@0 | 20 | #ifndef QSV_NUMPROCESSORS |
michael@0 | 21 | #define QSV_NUMPROCESSORS 26 |
michael@0 | 22 | #endif |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | /* BSD-derived systems use sysctl() to get the number of processors */ |
michael@0 | 26 | #if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \ |
michael@0 | 27 | || defined(OPENBSD) || defined(DARWIN) |
michael@0 | 28 | #define _PR_HAVE_SYSCTL |
michael@0 | 29 | #include <sys/param.h> |
michael@0 | 30 | #include <sys/sysctl.h> |
michael@0 | 31 | #endif |
michael@0 | 32 | |
michael@0 | 33 | #if defined(DARWIN) |
michael@0 | 34 | #include <mach/mach_init.h> |
michael@0 | 35 | #include <mach/mach_host.h> |
michael@0 | 36 | #include <mach/mach_port.h> |
michael@0 | 37 | #endif |
michael@0 | 38 | |
michael@0 | 39 | #if defined(HPUX) |
michael@0 | 40 | #include <sys/mpctl.h> |
michael@0 | 41 | #include <sys/pstat.h> |
michael@0 | 42 | #endif |
michael@0 | 43 | |
michael@0 | 44 | #if defined(XP_UNIX) |
michael@0 | 45 | #include <unistd.h> |
michael@0 | 46 | #include <sys/utsname.h> |
michael@0 | 47 | #endif |
michael@0 | 48 | |
michael@0 | 49 | #if defined(LINUX) |
michael@0 | 50 | #include <string.h> |
michael@0 | 51 | #include <ctype.h> |
michael@0 | 52 | #define MAX_LINE 512 |
michael@0 | 53 | #endif |
michael@0 | 54 | |
michael@0 | 55 | #if defined(AIX) |
michael@0 | 56 | #include <cf.h> |
michael@0 | 57 | #include <sys/cfgodm.h> |
michael@0 | 58 | #endif |
michael@0 | 59 | |
michael@0 | 60 | PR_IMPLEMENT(char) PR_GetDirectorySeparator(void) |
michael@0 | 61 | { |
michael@0 | 62 | return PR_DIRECTORY_SEPARATOR; |
michael@0 | 63 | } /* PR_GetDirectorySeparator */ |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | ** OBSOLETE -- the function name is misspelled. |
michael@0 | 67 | */ |
michael@0 | 68 | PR_IMPLEMENT(char) PR_GetDirectorySepartor(void) |
michael@0 | 69 | { |
michael@0 | 70 | #if defined(DEBUG) |
michael@0 | 71 | static PRBool warn = PR_TRUE; |
michael@0 | 72 | if (warn) { |
michael@0 | 73 | warn = _PR_Obsolete("PR_GetDirectorySepartor()", |
michael@0 | 74 | "PR_GetDirectorySeparator()"); |
michael@0 | 75 | } |
michael@0 | 76 | #endif |
michael@0 | 77 | return PR_GetDirectorySeparator(); |
michael@0 | 78 | } /* PR_GetDirectorySepartor */ |
michael@0 | 79 | |
michael@0 | 80 | PR_IMPLEMENT(char) PR_GetPathSeparator(void) |
michael@0 | 81 | { |
michael@0 | 82 | return PR_PATH_SEPARATOR; |
michael@0 | 83 | } /* PR_GetPathSeparator */ |
michael@0 | 84 | |
michael@0 | 85 | PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen) |
michael@0 | 86 | { |
michael@0 | 87 | PRUintn len = 0; |
michael@0 | 88 | |
michael@0 | 89 | if (!_pr_initialized) _PR_ImplicitInitialization(); |
michael@0 | 90 | |
michael@0 | 91 | switch(cmd) |
michael@0 | 92 | { |
michael@0 | 93 | case PR_SI_HOSTNAME: |
michael@0 | 94 | case PR_SI_HOSTNAME_UNTRUNCATED: |
michael@0 | 95 | if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen)) |
michael@0 | 96 | return PR_FAILURE; |
michael@0 | 97 | |
michael@0 | 98 | if (cmd == PR_SI_HOSTNAME_UNTRUNCATED) |
michael@0 | 99 | break; |
michael@0 | 100 | /* |
michael@0 | 101 | * On some platforms a system does not have a hostname and |
michael@0 | 102 | * its IP address is returned instead. The following code |
michael@0 | 103 | * should be skipped on those platforms. |
michael@0 | 104 | */ |
michael@0 | 105 | #ifndef _PR_GET_HOST_ADDR_AS_NAME |
michael@0 | 106 | /* Return the unqualified hostname */ |
michael@0 | 107 | while (buf[len] && (len < buflen)) { |
michael@0 | 108 | if (buf[len] == '.') { |
michael@0 | 109 | buf[len] = '\0'; |
michael@0 | 110 | break; |
michael@0 | 111 | } |
michael@0 | 112 | len += 1; |
michael@0 | 113 | } |
michael@0 | 114 | #endif |
michael@0 | 115 | break; |
michael@0 | 116 | |
michael@0 | 117 | case PR_SI_SYSNAME: |
michael@0 | 118 | /* Return the operating system name */ |
michael@0 | 119 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 120 | if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) |
michael@0 | 121 | return PR_FAILURE; |
michael@0 | 122 | #else |
michael@0 | 123 | (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME); |
michael@0 | 124 | #endif |
michael@0 | 125 | break; |
michael@0 | 126 | |
michael@0 | 127 | case PR_SI_RELEASE: |
michael@0 | 128 | /* Return the version of the operating system */ |
michael@0 | 129 | #if defined(XP_UNIX) || defined(WIN32) |
michael@0 | 130 | if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen)) |
michael@0 | 131 | return PR_FAILURE; |
michael@0 | 132 | #endif |
michael@0 | 133 | #if defined(XP_OS2) |
michael@0 | 134 | { |
michael@0 | 135 | ULONG os2ver[2] = {0}; |
michael@0 | 136 | DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION, |
michael@0 | 137 | &os2ver, sizeof(os2ver)); |
michael@0 | 138 | /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially, |
michael@0 | 139 | Warp 4 is version 2.40.00, WSeB 2.45.00 */ |
michael@0 | 140 | if (os2ver[0] < 30) |
michael@0 | 141 | (void)PR_snprintf(buf, buflen, "%s%lu", |
michael@0 | 142 | "2.", os2ver[0]); |
michael@0 | 143 | else if (os2ver[0] < 45) |
michael@0 | 144 | (void)PR_snprintf(buf, buflen, "%lu%s%lu", |
michael@0 | 145 | os2ver[0]/10, ".", os2ver[1]); |
michael@0 | 146 | else |
michael@0 | 147 | (void)PR_snprintf(buf, buflen, "%.1f", |
michael@0 | 148 | os2ver[0]/10.0); |
michael@0 | 149 | } |
michael@0 | 150 | #endif /* OS2 */ |
michael@0 | 151 | break; |
michael@0 | 152 | |
michael@0 | 153 | case PR_SI_ARCHITECTURE: |
michael@0 | 154 | /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/ |
michael@0 | 155 | (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE); |
michael@0 | 156 | break; |
michael@0 | 157 | default: |
michael@0 | 158 | PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0); |
michael@0 | 159 | return PR_FAILURE; |
michael@0 | 160 | } |
michael@0 | 161 | return PR_SUCCESS; |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | /* |
michael@0 | 165 | ** PR_GetNumberOfProcessors() |
michael@0 | 166 | ** |
michael@0 | 167 | ** Implementation notes: |
michael@0 | 168 | ** Every platform does it a bit different. |
michael@0 | 169 | ** numCpus is the returned value. |
michael@0 | 170 | ** for each platform's "if defined" section |
michael@0 | 171 | ** declare your local variable |
michael@0 | 172 | ** do your thing, assign to numCpus |
michael@0 | 173 | ** order of the if defined()s may be important, |
michael@0 | 174 | ** especially for unix variants. Do platform |
michael@0 | 175 | ** specific implementations before XP_UNIX. |
michael@0 | 176 | ** |
michael@0 | 177 | */ |
michael@0 | 178 | PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void ) |
michael@0 | 179 | { |
michael@0 | 180 | PRInt32 numCpus; |
michael@0 | 181 | #if defined(WIN32) |
michael@0 | 182 | SYSTEM_INFO info; |
michael@0 | 183 | |
michael@0 | 184 | GetSystemInfo( &info ); |
michael@0 | 185 | numCpus = info.dwNumberOfProcessors; |
michael@0 | 186 | #elif defined(BEOS) |
michael@0 | 187 | system_info sysInfo; |
michael@0 | 188 | |
michael@0 | 189 | get_system_info(&sysInfo); |
michael@0 | 190 | numCpus = sysInfo.cpu_count; |
michael@0 | 191 | #elif defined(OS2) |
michael@0 | 192 | DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus)); |
michael@0 | 193 | #elif defined(_PR_HAVE_SYSCTL) |
michael@0 | 194 | int mib[2]; |
michael@0 | 195 | int rc; |
michael@0 | 196 | size_t len = sizeof(numCpus); |
michael@0 | 197 | |
michael@0 | 198 | mib[0] = CTL_HW; |
michael@0 | 199 | mib[1] = HW_NCPU; |
michael@0 | 200 | rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 ); |
michael@0 | 201 | if ( -1 == rc ) { |
michael@0 | 202 | numCpus = -1; /* set to -1 for return value on error */ |
michael@0 | 203 | _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); |
michael@0 | 204 | } |
michael@0 | 205 | #elif defined(HPUX) |
michael@0 | 206 | numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 ); |
michael@0 | 207 | if ( numCpus < 1 ) { |
michael@0 | 208 | numCpus = -1; /* set to -1 for return value on error */ |
michael@0 | 209 | _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() ); |
michael@0 | 210 | } |
michael@0 | 211 | #elif defined(IRIX) |
michael@0 | 212 | numCpus = sysconf( _SC_NPROC_ONLN ); |
michael@0 | 213 | #elif defined(RISCOS) || defined(SYMBIAN) |
michael@0 | 214 | numCpus = 1; |
michael@0 | 215 | #elif defined(LINUX) |
michael@0 | 216 | /* for the benefit of devices with advanced power-saving, that |
michael@0 | 217 | actually hotplug their cpus in heavy load, try to figure out |
michael@0 | 218 | the real number of CPUs */ |
michael@0 | 219 | char buf[MAX_LINE]; |
michael@0 | 220 | FILE *fin; |
michael@0 | 221 | const char *cpu_present = "/sys/devices/system/cpu/present"; |
michael@0 | 222 | size_t strsize; |
michael@0 | 223 | numCpus = 0; |
michael@0 | 224 | fin = fopen(cpu_present, "r"); |
michael@0 | 225 | if (fin != NULL) { |
michael@0 | 226 | if (fgets(buf, MAX_LINE, fin) != NULL) { |
michael@0 | 227 | /* check that the format is what we expect */ |
michael@0 | 228 | if (buf[0] == '0') { |
michael@0 | 229 | strsize = strlen(buf); |
michael@0 | 230 | if (strsize == 1) { |
michael@0 | 231 | /* single core */ |
michael@0 | 232 | numCpus = 1; |
michael@0 | 233 | } else if (strsize >= 3 && strsize <= 5) { |
michael@0 | 234 | /* should be of the form 0-999 */ |
michael@0 | 235 | /* parse the part after the 0-, note count is 0-based */ |
michael@0 | 236 | if (buf[1] == '-' && isdigit(buf[2])) { |
michael@0 | 237 | numCpus = 1 + atoi(buf + 2); |
michael@0 | 238 | } |
michael@0 | 239 | } |
michael@0 | 240 | } |
michael@0 | 241 | } |
michael@0 | 242 | fclose(fin); |
michael@0 | 243 | } |
michael@0 | 244 | /* if that fails, fall back to more standard methods */ |
michael@0 | 245 | if (!numCpus) { |
michael@0 | 246 | numCpus = sysconf( _SC_NPROCESSORS_CONF ); |
michael@0 | 247 | } |
michael@0 | 248 | #elif defined(XP_UNIX) |
michael@0 | 249 | numCpus = sysconf( _SC_NPROCESSORS_CONF ); |
michael@0 | 250 | #else |
michael@0 | 251 | #error "An implementation is required" |
michael@0 | 252 | #endif |
michael@0 | 253 | return(numCpus); |
michael@0 | 254 | } /* end PR_GetNumberOfProcessors() */ |
michael@0 | 255 | |
michael@0 | 256 | /* |
michael@0 | 257 | ** PR_GetPhysicalMemorySize() |
michael@0 | 258 | ** |
michael@0 | 259 | ** Implementation notes: |
michael@0 | 260 | ** Every platform does it a bit different. |
michael@0 | 261 | ** bytes is the returned value. |
michael@0 | 262 | ** for each platform's "if defined" section |
michael@0 | 263 | ** declare your local variable |
michael@0 | 264 | ** do your thing, assign to bytes. |
michael@0 | 265 | ** |
michael@0 | 266 | */ |
michael@0 | 267 | PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void) |
michael@0 | 268 | { |
michael@0 | 269 | PRUint64 bytes = 0; |
michael@0 | 270 | |
michael@0 | 271 | #if defined(LINUX) || defined(SOLARIS) |
michael@0 | 272 | |
michael@0 | 273 | long pageSize = sysconf(_SC_PAGESIZE); |
michael@0 | 274 | long pageCount = sysconf(_SC_PHYS_PAGES); |
michael@0 | 275 | if (pageSize >= 0 && pageCount >= 0) |
michael@0 | 276 | bytes = (PRUint64) pageSize * pageCount; |
michael@0 | 277 | |
michael@0 | 278 | #elif defined(NETBSD) || defined(OPENBSD) |
michael@0 | 279 | |
michael@0 | 280 | int mib[2]; |
michael@0 | 281 | int rc; |
michael@0 | 282 | uint64_t memSize; |
michael@0 | 283 | size_t len = sizeof(memSize); |
michael@0 | 284 | |
michael@0 | 285 | mib[0] = CTL_HW; |
michael@0 | 286 | mib[1] = HW_PHYSMEM64; |
michael@0 | 287 | rc = sysctl(mib, 2, &memSize, &len, NULL, 0); |
michael@0 | 288 | if (-1 != rc) { |
michael@0 | 289 | bytes = memSize; |
michael@0 | 290 | } |
michael@0 | 291 | |
michael@0 | 292 | #elif defined(HPUX) |
michael@0 | 293 | |
michael@0 | 294 | struct pst_static info; |
michael@0 | 295 | int result = pstat_getstatic(&info, sizeof(info), 1, 0); |
michael@0 | 296 | if (result == 1) |
michael@0 | 297 | bytes = (PRUint64) info.physical_memory * info.page_size; |
michael@0 | 298 | |
michael@0 | 299 | #elif defined(DARWIN) |
michael@0 | 300 | |
michael@0 | 301 | mach_port_t mach_host = mach_host_self(); |
michael@0 | 302 | struct host_basic_info hInfo; |
michael@0 | 303 | mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT; |
michael@0 | 304 | |
michael@0 | 305 | int result = host_info(mach_host, |
michael@0 | 306 | HOST_BASIC_INFO, |
michael@0 | 307 | (host_info_t) &hInfo, |
michael@0 | 308 | &count); |
michael@0 | 309 | mach_port_deallocate(mach_task_self(), mach_host); |
michael@0 | 310 | if (result == KERN_SUCCESS) |
michael@0 | 311 | bytes = hInfo.max_mem; |
michael@0 | 312 | |
michael@0 | 313 | #elif defined(WIN32) |
michael@0 | 314 | |
michael@0 | 315 | MEMORYSTATUSEX memStat; |
michael@0 | 316 | memStat.dwLength = sizeof(memStat); |
michael@0 | 317 | if (GlobalMemoryStatusEx(&memStat)) |
michael@0 | 318 | bytes = memStat.ullTotalPhys; |
michael@0 | 319 | |
michael@0 | 320 | #elif defined(OS2) |
michael@0 | 321 | |
michael@0 | 322 | ULONG ulPhysMem; |
michael@0 | 323 | DosQuerySysInfo(QSV_TOTPHYSMEM, |
michael@0 | 324 | QSV_TOTPHYSMEM, |
michael@0 | 325 | &ulPhysMem, |
michael@0 | 326 | sizeof(ulPhysMem)); |
michael@0 | 327 | bytes = ulPhysMem; |
michael@0 | 328 | |
michael@0 | 329 | #elif defined(AIX) |
michael@0 | 330 | |
michael@0 | 331 | if (odm_initialize() == 0) { |
michael@0 | 332 | int how_many; |
michael@0 | 333 | struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many); |
michael@0 | 334 | if (obj != NULL) { |
michael@0 | 335 | bytes = (PRUint64) atoi(obj->value) * 1024; |
michael@0 | 336 | free(obj); |
michael@0 | 337 | } |
michael@0 | 338 | odm_terminate(); |
michael@0 | 339 | } |
michael@0 | 340 | |
michael@0 | 341 | #else |
michael@0 | 342 | |
michael@0 | 343 | PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0); |
michael@0 | 344 | |
michael@0 | 345 | #endif |
michael@0 | 346 | |
michael@0 | 347 | return bytes; |
michael@0 | 348 | } /* end PR_GetPhysicalMemorySize() */ |