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