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