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
4 : *
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/
9 : *
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
13 : * License.
14 : *
15 : * The Original Code is the Netscape Portable Runtime (NSPR).
16 : *
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.
21 : *
22 : * Contributor(s):
23 : *
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.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #include "primpl.h"
39 : #include "prsystem.h"
40 : #include "prprf.h"
41 : #include "prlong.h"
42 :
43 : #if defined(BEOS)
44 : #include <kernel/OS.h>
45 : #endif
46 :
47 : #if defined(OS2)
48 : #define INCL_DOS
49 : #define INCL_DOSMISC
50 : #include <os2.h>
51 : /* define the required constant if it is not already defined in the headers */
52 : #ifndef QSV_NUMPROCESSORS
53 : #define QSV_NUMPROCESSORS 26
54 : #endif
55 : #endif
56 :
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>
63 : #endif
64 :
65 : #if defined(DARWIN)
66 : #include <mach/mach_init.h>
67 : #include <mach/mach_host.h>
68 : #endif
69 :
70 : #if defined(HPUX)
71 : #include <sys/mpctl.h>
72 : #include <sys/pstat.h>
73 : #endif
74 :
75 : #if defined(XP_UNIX)
76 : #include <unistd.h>
77 : #include <sys/utsname.h>
78 : #endif
79 :
80 : #if defined(LINUX)
81 : #include <string.h>
82 : #include <ctype.h>
83 : #define MAX_LINE 512
84 : #endif
85 :
86 : #if defined(AIX)
87 : #include <cf.h>
88 : #include <sys/cfgodm.h>
89 : #endif
90 :
91 : #if defined(WIN32)
92 : /* This struct is not present in VC6 headers, so declare it here */
93 : typedef struct {
94 : DWORD dwLength;
95 : DWORD dwMemoryLoad;
96 : DWORDLONG ullTotalPhys;
97 : DWORDLONG ullAvailPhys;
98 : DWORDLONG ullToalPageFile;
99 : DWORDLONG ullAvailPageFile;
100 : DWORDLONG ullTotalVirtual;
101 : DWORDLONG ullAvailVirtual;
102 : DWORDLONG ullAvailExtendedVirtual;
103 : } PR_MEMORYSTATUSEX;
104 :
105 : /* Typedef for dynamic lookup of GlobalMemoryStatusEx(). */
106 : typedef BOOL (WINAPI *GlobalMemoryStatusExFn)(PR_MEMORYSTATUSEX *);
107 : #endif
108 :
109 1725 : PR_IMPLEMENT(char) PR_GetDirectorySeparator(void)
110 : {
111 1725 : return PR_DIRECTORY_SEPARATOR;
112 : } /* PR_GetDirectorySeparator */
113 :
114 : /*
115 : ** OBSOLETE -- the function name is misspelled.
116 : */
117 0 : PR_IMPLEMENT(char) PR_GetDirectorySepartor(void)
118 : {
119 : #if defined(DEBUG)
120 : static PRBool warn = PR_TRUE;
121 0 : if (warn) {
122 0 : warn = _PR_Obsolete("PR_GetDirectorySepartor()",
123 : "PR_GetDirectorySeparator()");
124 : }
125 : #endif
126 0 : return PR_GetDirectorySeparator();
127 : } /* PR_GetDirectorySepartor */
128 :
129 0 : PR_IMPLEMENT(char) PR_GetPathSeparator(void)
130 : {
131 0 : return PR_PATH_SEPARATOR;
132 : } /* PR_GetPathSeparator */
133 :
134 920 : PR_IMPLEMENT(PRStatus) PR_GetSystemInfo(PRSysInfo cmd, char *buf, PRUint32 buflen)
135 : {
136 920 : PRUintn len = 0;
137 :
138 920 : if (!_pr_initialized) _PR_ImplicitInitialization();
139 :
140 920 : switch(cmd)
141 : {
142 : case PR_SI_HOSTNAME:
143 : case PR_SI_HOSTNAME_UNTRUNCATED:
144 230 : if (PR_FAILURE == _PR_MD_GETHOSTNAME(buf, (PRUintn)buflen))
145 0 : return PR_FAILURE;
146 :
147 230 : if (cmd == PR_SI_HOSTNAME_UNTRUNCATED)
148 0 : break;
149 : /*
150 : * On some platforms a system does not have a hostname and
151 : * its IP address is returned instead. The following code
152 : * should be skipped on those platforms.
153 : */
154 : #ifndef _PR_GET_HOST_ADDR_AS_NAME
155 : /* Return the unqualified hostname */
156 5520 : while (buf[len] && (len < buflen)) {
157 5290 : if (buf[len] == '.') {
158 230 : buf[len] = '\0';
159 230 : break;
160 : }
161 5060 : len += 1;
162 : }
163 : #endif
164 230 : break;
165 :
166 : case PR_SI_SYSNAME:
167 : /* Return the operating system name */
168 : #if defined(XP_UNIX) || defined(WIN32)
169 230 : if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
170 0 : return PR_FAILURE;
171 : #else
172 : (void)PR_snprintf(buf, buflen, _PR_SI_SYSNAME);
173 : #endif
174 230 : break;
175 :
176 : case PR_SI_RELEASE:
177 : /* Return the version of the operating system */
178 : #if defined(XP_UNIX) || defined(WIN32)
179 230 : if (PR_FAILURE == _PR_MD_GETSYSINFO(cmd, buf, (PRUintn)buflen))
180 0 : return PR_FAILURE;
181 : #endif
182 : #if defined(XP_OS2)
183 : {
184 : ULONG os2ver[2] = {0};
185 : DosQuerySysInfo(QSV_VERSION_MINOR, QSV_VERSION_REVISION,
186 : &os2ver, sizeof(os2ver));
187 : /* Formatting for normal usage (2.11, 3.0, 4.0, 4.5); officially,
188 : Warp 4 is version 2.40.00, WSeB 2.45.00 */
189 : if (os2ver[0] < 30)
190 : (void)PR_snprintf(buf, buflen, "%s%lu",
191 : "2.", os2ver[0]);
192 : else if (os2ver[0] < 45)
193 : (void)PR_snprintf(buf, buflen, "%lu%s%lu",
194 : os2ver[0]/10, ".", os2ver[1]);
195 : else
196 : (void)PR_snprintf(buf, buflen, "%.1f",
197 : os2ver[0]/10.0);
198 : }
199 : #endif /* OS2 */
200 230 : break;
201 :
202 : case PR_SI_ARCHITECTURE:
203 : /* Return the architecture of the machine (ie. x86, mips, alpha, ...)*/
204 230 : (void)PR_snprintf(buf, buflen, _PR_SI_ARCHITECTURE);
205 230 : break;
206 : default:
207 0 : PR_SetError(PR_INVALID_ARGUMENT_ERROR, 0);
208 0 : return PR_FAILURE;
209 : }
210 920 : return PR_SUCCESS;
211 : }
212 :
213 : /*
214 : ** PR_GetNumberOfProcessors()
215 : **
216 : ** Implementation notes:
217 : ** Every platform does it a bit different.
218 : ** numCpus is the returned value.
219 : ** for each platform's "if defined" section
220 : ** declare your local variable
221 : ** do your thing, assign to numCpus
222 : ** order of the if defined()s may be important,
223 : ** especially for unix variants. Do platform
224 : ** specific implementations before XP_UNIX.
225 : **
226 : */
227 230 : PR_IMPLEMENT(PRInt32) PR_GetNumberOfProcessors( void )
228 : {
229 : PRInt32 numCpus;
230 : #if defined(WIN32)
231 : SYSTEM_INFO info;
232 :
233 : GetSystemInfo( &info );
234 : numCpus = info.dwNumberOfProcessors;
235 : #elif defined(BEOS)
236 : system_info sysInfo;
237 :
238 : get_system_info(&sysInfo);
239 : numCpus = sysInfo.cpu_count;
240 : #elif defined(OS2)
241 : DosQuerySysInfo( QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &numCpus, sizeof(numCpus));
242 : #elif defined(_PR_HAVE_SYSCTL)
243 : int mib[2];
244 : int rc;
245 : size_t len = sizeof(numCpus);
246 :
247 : mib[0] = CTL_HW;
248 : mib[1] = HW_NCPU;
249 : rc = sysctl( mib, 2, &numCpus, &len, NULL, 0 );
250 : if ( -1 == rc ) {
251 : numCpus = -1; /* set to -1 for return value on error */
252 : _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
253 : }
254 : #elif defined(HPUX)
255 : numCpus = mpctl( MPC_GETNUMSPUS, 0, 0 );
256 : if ( numCpus < 1 ) {
257 : numCpus = -1; /* set to -1 for return value on error */
258 : _PR_MD_MAP_DEFAULT_ERROR( _MD_ERRNO() );
259 : }
260 : #elif defined(IRIX)
261 : numCpus = sysconf( _SC_NPROC_ONLN );
262 : #elif defined(RISCOS) || defined(SYMBIAN)
263 : numCpus = 1;
264 : #elif defined(LINUX)
265 : /* for the benefit of devices with advanced power-saving, that
266 : actually hotplug their cpus in heavy load, try to figure out
267 : the real number of CPUs */
268 : char buf[MAX_LINE];
269 : FILE *fin;
270 230 : const char *cpu_present = "/sys/devices/system/cpu/present";
271 : size_t strsize;
272 230 : numCpus = 0;
273 230 : fin = fopen(cpu_present, "r");
274 230 : if (fin != NULL) {
275 0 : if (fgets(buf, MAX_LINE, fin) != NULL) {
276 : /* check that the format is what we expect */
277 0 : if (buf[0] == '0') {
278 0 : strsize = strlen(buf);
279 0 : if (strsize == 1) {
280 : /* single core */
281 0 : numCpus = 1;
282 0 : } else if (strsize >= 3 && strsize <= 5) {
283 : /* should be of the form 0-999 */
284 : /* parse the part after the 0-, note count is 0-based */
285 0 : if (buf[1] == '-' && isdigit(buf[2])) {
286 0 : numCpus = 1 + atoi(buf + 2);
287 : }
288 : }
289 : }
290 : }
291 0 : fclose(fin);
292 : }
293 : /* if that fails, fall back to more standard methods */
294 230 : if (!numCpus) {
295 230 : numCpus = sysconf( _SC_NPROCESSORS_CONF );
296 : }
297 : #elif defined(XP_UNIX)
298 : numCpus = sysconf( _SC_NPROCESSORS_CONF );
299 : #else
300 : #error "An implementation is required"
301 : #endif
302 230 : return(numCpus);
303 : } /* end PR_GetNumberOfProcessors() */
304 :
305 : /*
306 : ** PR_GetPhysicalMemorySize()
307 : **
308 : ** Implementation notes:
309 : ** Every platform does it a bit different.
310 : ** bytes is the returned value.
311 : ** for each platform's "if defined" section
312 : ** declare your local variable
313 : ** do your thing, assign to bytes.
314 : **
315 : */
316 1725 : PR_IMPLEMENT(PRUint64) PR_GetPhysicalMemorySize(void)
317 : {
318 1725 : PRUint64 bytes = 0;
319 :
320 : #if defined(LINUX) || defined(SOLARIS)
321 :
322 1725 : long pageSize = sysconf(_SC_PAGESIZE);
323 1725 : long pageCount = sysconf(_SC_PHYS_PAGES);
324 1725 : if (pageSize >= 0 && pageCount >= 0)
325 1725 : bytes = (PRUint64) pageSize * pageCount;
326 :
327 : #elif defined(NETBSD) || defined(OPENBSD)
328 :
329 : int mib[2];
330 : int rc;
331 : uint64_t memSize;
332 : size_t len = sizeof(memSize);
333 :
334 : mib[0] = CTL_HW;
335 : mib[1] = HW_PHYSMEM64;
336 : rc = sysctl(mib, 2, &memSize, &len, NULL, 0);
337 : if (-1 != rc) {
338 : bytes = memSize;
339 : }
340 :
341 : #elif defined(HPUX)
342 :
343 : struct pst_static info;
344 : int result = pstat_getstatic(&info, sizeof(info), 1, 0);
345 : if (result == 1)
346 : bytes = (PRUint64) info.physical_memory * info.page_size;
347 :
348 : #elif defined(DARWIN)
349 :
350 : struct host_basic_info hInfo;
351 : mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
352 :
353 : int result = host_info(mach_host_self(),
354 : HOST_BASIC_INFO,
355 : (host_info_t) &hInfo,
356 : &count);
357 : if (result == KERN_SUCCESS)
358 : bytes = hInfo.max_mem;
359 :
360 : #elif defined(WIN32)
361 :
362 : /* Try to use the newer GlobalMemoryStatusEx API for Windows 2000+. */
363 : GlobalMemoryStatusExFn globalMemory = (GlobalMemoryStatusExFn) NULL;
364 : HMODULE module = GetModuleHandleW(L"kernel32.dll");
365 :
366 : if (module) {
367 : globalMemory = (GlobalMemoryStatusExFn)GetProcAddress(module, "GlobalMemoryStatusEx");
368 :
369 : if (globalMemory) {
370 : PR_MEMORYSTATUSEX memStat;
371 : memStat.dwLength = sizeof(memStat);
372 :
373 : if (globalMemory(&memStat))
374 : bytes = memStat.ullTotalPhys;
375 : }
376 : }
377 :
378 : if (!bytes) {
379 : /* Fall back to the older API. */
380 : MEMORYSTATUS memStat;
381 : memset(&memStat, 0, sizeof(memStat));
382 : GlobalMemoryStatus(&memStat);
383 : bytes = memStat.dwTotalPhys;
384 : }
385 :
386 : #elif defined(OS2)
387 :
388 : ULONG ulPhysMem;
389 : DosQuerySysInfo(QSV_TOTPHYSMEM,
390 : QSV_TOTPHYSMEM,
391 : &ulPhysMem,
392 : sizeof(ulPhysMem));
393 : bytes = ulPhysMem;
394 :
395 : #elif defined(AIX)
396 :
397 : if (odm_initialize() == 0) {
398 : int how_many;
399 : struct CuAt *obj = getattr("sys0", "realmem", 0, &how_many);
400 : if (obj != NULL) {
401 : bytes = (PRUint64) atoi(obj->value) * 1024;
402 : free(obj);
403 : }
404 : odm_terminate();
405 : }
406 :
407 : #else
408 :
409 : PR_SetError(PR_NOT_IMPLEMENTED_ERROR, 0);
410 :
411 : #endif
412 :
413 1725 : return bytes;
414 : } /* end PR_GetPhysicalMemorySize() */
|