1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is the Netscape Portable Runtime (NSPR).
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998-2000
20 * the Initial Developer. All Rights Reserved.
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
44 #include <kernel/OS.h>
51 /* define the required constant if it is not already defined in the headers */
52 #ifndef QSV_NUMPROCESSORS
53 #define QSV_NUMPROCESSORS 26
57 /* BSD-derived systems use sysctl() to get the number of processors */
58 #if defined(BSDI) || defined(FREEBSD) || defined(NETBSD) \
59 || defined(OPENBSD) || defined(DARWIN)
60 #define _PR_HAVE_SYSCTL
61 #include <sys/param.h>
62 #include <sys/sysctl.h>
66 #include <mach/mach_init.h>
67 #include <mach/mach_host.h>
71 #include <sys/mpctl.h>
72 #include <sys/pstat.h>
77 #include <sys/utsname.h>
82 #include <sys/cfgodm.h>
86 /* This struct is not present in VC6 headers, so declare it here */
90 DWORDLONG ullTotalPhys;
91 DWORDLONG ullAvailPhys;
92 DWORDLONG ullToalPageFile;
93 DWORDLONG ullAvailPageFile;
94 DWORDLONG ullTotalVirtual;
95 DWORDLONG ullAvailVirtual;
96 DWORDLONG ullAvailExtendedVirtual;
99 /* Typedef for dynamic lookup of GlobalMemoryStatusEx(). */
100 typedef BOOL (WINAPI *GlobalMemoryStatusExFn)(PR_MEMORYSTATUSEX *);
103 PR_IMPLEMENT(char) PR_GetDirectorySeparator(void)
105 return PR_DIRECTORY_SEPARATOR;
106 } /* PR_GetDirectorySeparator */
109 ** OBSOLETE -- the function name is misspelled.
111 PR_IMPLEMENT(char) PR_GetDirectorySepartor(void)
114 static PRBool warn = PR_TRUE;
116 warn = _PR_Obsolete("PR_GetDirectorySepartor()",
117 "PR_GetDirectorySeparator()");
120 return PR_GetDirectorySeparator();
121 } /* PR_GetDirectorySepartor */
123 PR_IMPLEMENT(char) PR_GetPathSeparator(void)
125 return PR_PATH_SEPARATOR;
126 } /* PR_GetPathSeparator */
128 PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen)
132 if (!_pr_initialized) _PR_ImplicitInitialization();
137 case PR_SI_HOSTNAME_UNTRUNCATED:
138 if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen))
141 if (cmd == PR_SI_HOSTNAME_UNTRUNCATED)
144 * On some platforms a system does not have a hostname and
145 * its IP address is returned instead. The following code
146 * should be skipped on those platforms.
148 #ifndef _PR_GET_HOST_ADDR_AS_NAME
149 /* Return the unqualified hostname */
150 while (buf[len] && (len < buflen)) {
151 if (buf[len] == '.') {
161 /* Return the operating system name */
162 #if defined(XP_UNIX) || defined(WIN32)
163 if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
166 (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME);
171 /* Return the version of the operating system */
172 #if defined(XP_UNIX) || defined(WIN32)
173 if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
178 ULONG os2ver[2] = {0};
179 DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION,
180 &os2ver, sizeof(os2ver));
181 /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially,
182 Warp 4 is version 2.40.00, WSeB 2.45.00 */
184 (void)PR_snprintf(buf, buflen, "%s%lu",
186 else if (os2ver[0] < 45)
187 (void)PR_snprintf(buf, buflen, "%lu%s%lu",
188 os2ver[0]/10, ".", os2ver[1]);
190 (void)PR_snprintf(buf, buflen, "%.1f",
196 case PR_SI_ARCHITECTURE:
197 /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/
198 (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE);
201 PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
208 ** PR_GetNumberOfProcessors()
210 ** Implementation notes:
211 ** Every platform does it a bit different.
212 ** numCpus is the returned value.
213 ** for each platform's "if defined" section
214 ** declare your local variable
215 ** do your thing, assign to numCpus
216 ** order of the if defined()s may be important,
217 ** especially for unix variants. Do platform
218 ** specific implementations before XP_UNIX.
221 PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void )
227 GetSystemInfo( &info );
228 numCpus = info.dwNumberOfProcessors;
232 get_system_info(&sysInfo);
233 numCpus = sysInfo.cpu_count;
235 DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus));
236 #elif defined(_PR_HAVE_SYSCTL)
239 size_t len = sizeof(numCpus);
243 rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 );
245 numCpus = -1; /* set to -1 for return value on error */
246 _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
249 numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 );
251 numCpus = -1; /* set to -1 for return value on error */
252 _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
255 numCpus = sysconf( _SC_NPROC_ONLN );
256 #elif defined(RISCOS) || defined(SYMBIAN)
258 #elif defined(XP_UNIX)
259 numCpus = sysconf( _SC_NPROCESSORS_ONLN );
261 #error "An implementation is required"
264 } /* end PR_GetNumberOfProcessors() */
267 ** PR_GetPhysicalMemorySize()
269 ** Implementation notes:
270 ** Every platform does it a bit different.
271 ** bytes is the returned value.
272 ** for each platform's "if defined" section
273 ** declare your local variable
274 ** do your thing, assign to bytes.
277 PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void)
281 #if defined(LINUX) || defined(SOLARIS)
283 long pageSize = sysconf(_SC_PAGESIZE);
284 long pageCount = sysconf(_SC_PHYS_PAGES);
285 bytes = (PRUint64) pageSize * pageCount;
287 #elif defined(NETBSD)
292 size_t len = sizeof(memSize);
295 mib[1] = HW_PHYSMEM64;
296 rc = sysctl(mib, 2, &memSize, &len, NULL, 0);
303 struct pst_static info;
304 int result = pstat_getstatic(&info, sizeof(info), 1, 0);
306 bytes = (PRUint64) info.physical_memory * info.page_size;
308 #elif defined(DARWIN)
310 struct host_basic_info hInfo;
311 mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
313 int result = host_info(mach_host_self(),
315 (host_info_t) &hInfo,
317 if (result == KERN_SUCCESS)
318 bytes = hInfo.max_mem;
322 /* Try to use the newer GlobalMemoryStatusEx API for Windows 2000+. */
323 GlobalMemoryStatusExFn globalMemory = (GlobalMemoryStatusExFn) NULL;
324 HMODULE module = GetModuleHandleW(L"kernel32.dll");
327 globalMemory = (GlobalMemoryStatusExFn)GetProcAddress(module, "GlobalMemoryStatusEx");
330 PR_MEMORYSTATUSEX memStat;
331 memStat.dwLength = sizeof(memStat);
333 if (globalMemory(&memStat))
334 bytes = memStat.ullTotalPhys;
339 /* Fall back to the older API. */
340 MEMORYSTATUS memStat;
341 memset(&memStat, 0, sizeof(memStat));
342 GlobalMemoryStatus(&memStat);
343 bytes = memStat.dwTotalPhys;
349 DosQuerySysInfo(QSV_TOTPHYSMEM,
357 if (odm_initialize() == 0) {
359 struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many);
361 bytes = (PRUint64) atoi(obj->value) * 1024;
369 PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
374 } /* end PR_GetPhysicalMemorySize() */